#!/bin/csh

# c-shell script to plot MJO life-cycle composite
# seasonally stratified

# !!!!!! What should be modified !!!!!!
# HHH  : home directory
# MJO  : variable (e.g. OLR, PRCP)
# TVV  : data source
# CLV  : contour levels
# CLO  : colors for shading
# OPT  : option number for writing unit
#( 1: doesn't have order, 2: has order )
# UN1  : unit (e.g. W m)
# OR1  : order (e.g. -2)
# TSEA : title (season)
# !!!!!! What should be modified !!!!!!

# setenv HHH /jdata4/cdl/kim/msm_release/msd
#enver setenv HHH /jdata4/cdl/kim/msd
 setenv HHH ~/work/programs/MJOWG/msd


# foreach var ( olr_av trmm gpcp slp_n1 u200_n1 u200_n2 u850_n1 u850_n2 )
 foreach var ( olr_av u200_n1 u850_n1 )

# variable
 if ( $var == 'olr_av' ) then
  setenv MJO OLR
  setenv TVV AVHRR
 endif
 if ( $var == 'trmm' ) then
  setenv MJO PRCP
  setenv TVV TRMM
 endif
 if ( $var == 'gpcp' ) then
  setenv MJO PRCP
  setenv TVV GPCP
 endif
 if ( $var == 'slp_n1' ) then
  setenv MJO SLP
  setenv TVV NCEP1
 endif
 if ( $var == 'u200_n1' ) then
  setenv MJO U200
  setenv TVV NCEP1
 endif
 if ( $var == 'u200_n2' ) then
  setenv MJO U200
  setenv TVV NCEP2
 endif
 if ( $var == 'u200_e' ) then
  setenv MJO U200
  setenv TVV ERA40
 endif
 if ( $var == 'u850_n1' ) then
  setenv MJO U850
  setenv TVV NCEP1
 endif
 if ( $var == 'u850_n2' ) then
  setenv MJO U850
  setenv TVV NCEP2
 endif
 if ( $var == 'u850_e' ) then
  setenv MJO U850
  setenv TVV ERA40
 endif

 if ( $MJO == 'OLR' ) then
  setenv OPT 2
  setenv CLV '-24 -21 -18 -15 -12 -9 -6 -3 3 6 9 12 15 18 21 24'
  setenv CLO '33 34 35 36 37 38 39 40 0 41 42 43 44 45 46 47 48'
  setenv UN1 'W m'
  setenv OR1 -2
 else if ( $MJO == 'PRCP' ) then
  setenv OPT 2
  setenv CLV '-4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0'
  setenv CLO '48 47 46 45 44 43 42 41 0 40 39 38 37 36 35 34 33'
  setenv UN1 'mm day'
  setenv OR1 -1
 else if ( $MJO == 'SLP' ) then
  setenv OPT 1
  setenv CLV '-1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.2 0.4 0.6 0.8 1.0 1.2'
  setenv CLO '35 36 37 38 39 40 0 41 42 43 44 45 46'
  setenv UN1 hPa
  setenv OR1 1
 else if ( $MJO == 'U200' ) then
  setenv OPT 2
  setenv CLV '-8 -7 -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 7 8'
  setenv CLO '33 34 35 36 37 38 39 40 0 41 42 43 44 45 46 47 48'
  setenv UN1 'm s'
  setenv OR1 -1
 else if ( $MJO == 'U850' ) then
  setenv OPT 2
  setenv CLV '-3.5 -3 -2.5 -2 -1.5 -1 -0.5 0.5 1 1.5 2 2.5 3 3.5'
  setenv CLO '34 35 36 37 38 39 40 0 41 42 43 44 45 46 47'
  setenv UN1 'm s'
  setenv OR1 -1
 endif

 foreach sea ( sum win )

# season
 if ( $sea == 'sum' ) then
  setenv TSEA 'May to October'
 else if ( $sea == 'win' ) then
  setenv TSEA 'November to April'
 endif

# file copy
 cd $HHH/level_2
 mkdir -p fig/comp/2d
 cd fig/comp/2d
 cp -f $HHH/level_2/sample/comp/gs_2d.sea.sample .

 sed "s#homedir#$HHH#g"                gs_2d.sea.sample > tmp2
 sed "s/variable/$var/g"                           tmp2 > tmp1
 sed "s/title_var/$TVV/g"                          tmp1 > tmp2
 sed "s/title_mjo/$MJO/g"                          tmp2 > tmp1
 sed "s/season/$sea/g"                             tmp1 > tmp2
 sed "s/title_sea/$TSEA/g"                         tmp2 > tmp1
 sed "s/option/$OPT/g"                             tmp1 > tmp2
 sed "s/levels/$CLV/g"                             tmp2 > tmp1
 sed "s/colors/$CLO/g"                             tmp1 > tmp2
 sed "s/unit1/$UN1/g"                              tmp2 > tmp1
 sed "s/order1/$OR1/g"                             tmp1 > tmp2
 sed "s/vname/$MJO/g"                              tmp2 > comp_2d.sea.gs

grads -pbc << EOF
comp_2d.sea
EOF

 end 
# foreach sea

 end 
# foreach var
