#!/bin/csh

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

# !!!!!! What should be modified !!!!!!
# HHH  : home directory
# ONL  : name of olr data
# 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)
# ZTO  : number of top level in pressure/height
# ( figure will be drawn from z=1 to z=ZTO )
# TSEA : title (season)
# !!!!!! What should be modified !!!!!!

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


  setenv ONM olr_av

 foreach var ( u3d_n1 t3d_n1 q3d_n1 om3d_n1 )

# variable

 if ( $var == 'u3d_n1' ) then
  setenv MJO U
  setenv TVV NCEP1
 endif
 if ( $var == 't3d_n1' ) then
  setenv MJO T
  setenv TVV NCEP1
 endif
 if ( $var == 'q3d_n1' ) then
  setenv MJO Q
  setenv TVV NCEP1
 endif
 if ( $var == 'om3d_n1' ) then
  setenv MJO OM
  setenv TVV NCEP1
 endif

 if ( $MJO == 'U' ) then
  setenv MJO 'Zonal wind'
  setenv ZTO 12
  setenv OPT 1
  setenv CLV '-5 -4 -3 -2 -1 1 2 3 4 5'
  setenv CLO '36 37 38 39 40 0 41 42 43 44 45'
  setenv UN1 ms
  setenv OR1 -1
 endif
 if ( $var == 'T' ) then
  setenv MJO 'Temperature'
  setenv ZTO 12
  setenv OPT 2
  setenv CLV '-0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8'
  setenv CLO '33 34 35 36 37 38 39 40 0 41 42 43 44 45 46 47 48'
  setenv UN1 K
  setenv OR1 1
 endif
 if ( $var == 'Q' ) then
  setenv MJO 'Specific humidity'
  setenv ZTO 12
  setenv OPT 3
  setenv CLV '-0.5 -0.4 -0.3 -0.2 -0.1 0.1 0.2 0.3 0.4 0.5'
  setenv CLO '45 44 43 42 41 0 40 39 38 37 36'
  setenv UN1 kgkg
  setenv OR1 -1
 endif
 if ( $var == 'OM' ) then
  setenv MJO 'Pressure Velocity'
  setenv ZTO 12
  setenv OPT 4
  setenv CLV '-0.016 -0.014 -0.012 -0.010 -0.008 -0.006 -0.004 -0.002 0.002 0.004 0.006 0.008 0.010 0.012 0.014 0.016
  setenv CLO '33 34 35 36 37 38 39 40 0 41 42 43 44 45 46 47 48'
  setenv UN1 Pas
  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/3d
 cd fig/comp/3d
 cp -f $HHH/level_2/sample/comp/gs_3d.sea.sample .

 sed "s#homedir#$HHH#g"                gs_3d.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/ztop/$ZTO/g"                               tmp1 > tmp2
 sed "s/olr_name/$ONM/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/$var/g"                              tmp2 > comp_3d.sea.gs

grads -lbc << EOF
comp_3d.sea
EOF

 end 
# foreach sea

 end 
# foreach var
