: # using wgrib to make ctl files for grads # # script makes a control file for GrADS # usage: grib2ctl [options] [grib file] [optional index file] >[control file] # # note: this script does not make the index file .. you have to run gribmap # # *** How to make a control and index file for GrADS *** # # (1) make a control file of the grib file (example.grib) # % grib2ctl example.grib >example.ctl # this make a control file (example.ctl) # the control file will use index file (example.grib.idx) # (2) make the index file # % gribmap -0 -i example.ctl # makes index file (example.grib.idx) # if used grib2ctl -verf ... then use # % gribmap -i example.ctl # (3) in GrADS, enter # ga> open example.ctl # # bugs: # many # will only run under UNIX (no PC version) # will fail under number of situations # very NCEP-centric # limited number of NCEP grids are supported # # requires wgrib 1.6.0 or higher # note: the syntax of the head command change from older UNIXes to POSIX # if your machine complains, replace "head -n 1" by "head -1" # http://wesley.wwb.noaa.gov/grib2ctl.html version=0.9.12.5.a set -u if [ $# -eq 0 ] ; then echo >&2 "$0 $version wesley ebisuzaki" echo >&2 " makes a Grads control file for grib files" echo >&2 " usage: $0 [options] [grib file] [optional index file] >[ctl file]" echo >&2 " -ncep_opn .. use NCEP opn grib table for T62 NCEP fields" echo >&2 " -ncep_rean .. use NCEP reanalysis grib table for T62 NCEP fields" echo >&2 " -verf .. use forecast verification times" echo >&2 " -no_prs .. no prs suffix on variable name" echo >&2 " -no_suffix .. no suffix on variable name" exit 8 fi wflag= file= index= prs=prs suffix= for i do case "$i" in "-verf") wflag="$wflag -verf" ;; "-ncep_opn") wflag="$wflag -ncep_opn" ;; "-ncep_rean") wflag="$wflag -ncep_rean" ;; "-no_prs") prs= ;; "-no_suffix") suffix=no ;; *) if [ "$file" = "" ] ; then file="$i" else index="$i" fi esac done [ "$index" = "" ] && index="$file.idx" if [ "`uname`" = "win32" ] ; then tmpname="/grib2ctl" else tmpname="/tmp/$LOGNAME.junk.$$" fi ListA=$tmpname.tm1 ListD=$tmpname.tm2 List1=$tmpname.tm3 ListN=$tmpname.tm4 ListT=$tmpname.tm5 # [ -f $ListA ] && rm $ListA # [ -f $ListD ] && rm $ListD # [ -f $List1 ] && rm $List1 # [ -f $ListN ] && rm $ListN # [ -f $ListT ] && rm $ListT # List of All records wgrib $wflag -v $file > $ListA if [ ! -s $ListA ] ; then echo >&2 "either missing or not a grib file: $file" exit 8 fi # List of Dates cut -d: -f3 $ListA | sort -u >$ListD ntime=`wc -l <$ListD` # Read 1st time read time <$ListD # List of variables and levels (for a single time) egrep $time $ListA | sed -e 's/kpds=//g' -e 's/,/:/g' >$List1 # get 1st date year=`echo $time | cut -c3-6` mo=`echo $time | cut -c7-8` day=`echo $time | cut -c9-10` hour=`echo $time | cut -c11-12` if [ $ntime -gt 1 ] ; then time1="`head -n 2 $ListD | fgrep -v $time`" mo1=`echo $time1 | cut -c7-8` day1=`echo $time1 | cut -c9-10` hour1=`echo $time1 | cut -c11-12` fi case $mo in 01) month=jan;; 02) month=feb;; 03) month=mar;; 04) month=apr;; 05) month=may;; 06) month=jun;; 07) month=jul;; 08) month=aug;; 09) month=sep;; 10) month=oct;; 11) month=nov;; 12) month=dec;; *) echo "month error $mm" exit 1;; esac # Get a list of Names:kpds5:kpds6 with with no duplicates cut -f4,6-7 -d: $List1 | sort -u >$ListN nvar=`wc -l <$ListN` # ---------------intro------------------------------------ # new: add ^ in front of relative pathnames caret= [ "`echo $file | cut -c1 `" != '/' ] && caret='^' echo "dset $caret$file" caret= [ "`echo $index | cut -c1 `" != '/' ] && caret='^' echo "index $caret$index" echo 'undef 9.999E+20' echo "title $file" echo "* produced by grib2ctl v$version" # ------------------- grid ----------------------- grid="`wgrib $wflag -V $file -d 1 -o /dev/null 2>/dev/null | egrep 'grid=' | sed 's/.*grid=//' \ | sed 's/ .*//'`" if [ $grid -ne 29 -a $grid -ne 30 -a $grid -ne 33 -a $grid -ne 34 \ -a $grid -ne 85 -a $grid -ne 86 ] ; then echo 'options yrev' fi echo "dtype grib $grid" if [ $grid -eq 2 ] ; then # 2.5 x 2.5 lola echo 'xdef 144 linear 0 2.5' echo 'ydef 73 linear -90 2.5' elif [ $grid -eq 3 ] ; then # 1 x 1 lola echo 'xdef 360 linear 0 1' echo 'ydef 181 linear -90 1' elif [ $grid -eq 6 ] ; then echo "pdef 53 45 nps 27 49 -105 190.5" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 29 ] ; then echo 'xdef 145 linear 0 2.5' echo 'ydef 37 linear 0 2.5' elif [ $grid -eq 30 ] ; then echo 'xdef 145 linear 0 2.5' echo 'ydef 37 linear -90 2.5' elif [ $grid -eq 33 ] ; then echo 'xdef 181 linear 0 2' echo 'ydef 46 linear 0 2' elif [ $grid -eq 34 ] ; then echo 'xdef 181 linear 0 2' echo 'ydef 46 linear -90 2' elif [ $grid -eq 85 ] ; then echo 'xdef 360 linear 0.5 1' echo 'ydef 90 linear 0.5 1' elif [ $grid -eq 86 ] ; then echo 'xdef 360 linear 0.5 1' echo 'ydef 90 linear -89.5 1' elif [ $grid -eq 98 ] ; then # 192x94 gaussian grid echo "xdef 192 linear 0 1.875" echo "ydef 94 levels" echo "-88.542 -86.653 -84.753 -82.851 -80.947 -79.043 -77.139 -75.235 -73.331 -71.426" echo "-69.522 -67.617 -65.713 -63.808 -61.903 -59.999 -58.094 -56.189 -54.285 -52.380" echo "-50.475 -48.571 -46.666 -44.761 -42.856 -40.952 -39.047 -37.142 -35.238 -33.333" echo "-31.428 -29.523 -27.619 -25.714 -23.809 -21.904 -20.000 -18.095 -16.190 -14.286" echo "-12.381 -10.476 -8.571 -6.667 -4.762 -2.857 -0.952 0.952 2.857 4.762" echo " 6.667 8.571 10.476 12.381 14.286 16.190 18.095 20.000 21.904 23.809" echo " 25.714 27.619 29.523 31.428 33.333 35.238 37.142 39.047 40.952 42.856" echo " 44.761 46.666 48.571 50.475 52.380 54.285 56.189 58.094 59.999 61.903" echo " 63.808 65.713 67.617 69.522 71.426 73.331 75.235 77.139 79.043 80.947" echo " 82.851 84.753 86.653 88.542" elif [ $grid -eq 87 ] ; then echo "pdef 81 62 nps 31.9 112.53 -105 68.513" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 101 ] ; then echo "pdef 113 91 nps 58.5 92.5 -105 91.452" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 104 ] ; then echo "pdef 147 110 nps 75.5 109.5 -105 90.75464" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 105 ] ; then echo "pdef 83 83 nps 40.5 88.5 -105 90.75464" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 106 ] ; then echo "pdef 165 117 nps 80 176 -105 45.37732" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 107 ] ; then echo "pdef 120 92 nps 46 167 -105 45.37732" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 211 ] ; then # awips labert conformal echo "pdef 93 65 lcc 12.19 -133.459 1 1 25 25 -95 81270.5 81270.5" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 212 ] ; then # awips labert conformal echo "pdef 185 129 lcc 35.0 -95.0 105 49 25 25 -95 40635 40635" echo "xdef 181 linear 220 0.5" echo "ydef 091 linear 015 0.5" elif [ $grid -eq 216 ] ; then echo "pdef 147 110 nps 75.5 109.5 -105 91.452" echo "xdef 181 linear -180 1" echo "ydef 91 linear 0 1" elif [ $grid -eq 218 ] ; then # awips labert conformal echo "pdef 737 513 lcc 12.19 -133.459 1 1 25 25 -95 10.159 10.159" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 241 ] ; then echo "pdef 386 293 nps 147.315 534.0 -105 14.2875" echo "xdef 161 linear -140 0.5" echo "ydef 81 linear 20 0.5" elif [ $grid -eq 126 ] ; then # 384x190 gaussian grid echo "xdef 384 linear 0 0.9375" echo "ydef 190 levels" echo "-89.277 -88.340 -87.397 -86.454 -85.509 -84.565 -83.620 -82.676 -81.731 -80.786" echo "-79.841 -78.897 -77.952 -77.007 -76.062 -75.117 -74.173 -73.228 -72.283 -71.338" echo "-70.393 -69.448 -68.503 -67.559 -66.614 -65.669 -64.724 -63.779 -62.834 -61.889" echo "-60.945 -60.000 -59.055 -58.110 -57.165 -56.220 -55.275 -54.330 -53.386 -52.441" echo "-51.496 -50.551 -49.606 -48.661 -47.716 -46.771 -45.827 -44.882 -43.937 -42.992" echo "-42.047 -41.102 -40.157 -39.212 -38.268 -37.323 -36.378 -35.433 -34.488 -33.543" echo "-32.598 -31.653 -30.709 -29.764 -28.819 -27.874 -26.929 -25.984 -25.039 -24.094" echo "-23.150 -22.205 -21.260 -20.315 -19.370 -18.425 -17.480 -16.535 -15.590 -14.646" echo "-13.701 -12.756 -11.811 -10.866 -9.921 -8.976 -8.031 -7.087 -6.142 -5.197" echo " -4.252 -3.307 -2.362 -1.417 -0.472 0.472 1.417 2.362 3.307 4.252" echo " 5.197 6.142 7.087 8.031 8.976 9.921 10.866 11.811 12.756 13.701" echo " 14.646 15.590 16.535 17.480 18.425 19.370 20.315 21.260 22.205 23.150" echo " 24.094 25.039 25.984 26.929 27.874 28.819 29.764 30.709 31.653 32.598" echo " 33.543 34.488 35.433 36.378 37.323 38.268 39.212 40.157 41.102 42.047" echo " 42.992 43.937 44.882 45.827 46.771 47.716 48.661 49.606 50.551 51.496" echo " 52.441 53.386 54.330 55.275 56.220 57.165 58.110 59.055 60.000 60.945" echo " 61.889 62.834 63.779 64.724 65.669 66.614 67.559 68.503 69.448 70.393" echo " 71.338 72.283 73.228 74.173 75.117 76.062 77.007 77.952 78.897 79.841" echo " 80.786 81.731 82.676 83.620 84.565 85.509 86.454 87.397 88.340 89.277" elif [ $grid -eq 255 ] ; then # user-defined grids: # this section cannot handle arbitary user-defined grids # if the dimensions match a common grid .. we will assume a match # echo >&2 "user defined grid .. only partially decoded .. pot. problems" resolution="`wgrib $wflag -V $file -d 1 -o /dev/null 2>/dev/null | egrep ' nx '`" if [ `echo $resolution | egrep -c ' nx 144 ny 73'` -eq 1 ] then # 2.5 x 2.5 lola echo 'xdef 144 linear 0 2.5' echo 'ydef 73 linear -90 2.5' elif [ `echo $resolution | egrep -c ' nx 360 ny 181 '` -eq 1 ] ; then # 1 x 1 lola echo 'xdef 360 linear 0 1' echo 'ydef 181 linear -90 1' elif [ `echo $resolution | egrep -c ' nx 180 ny 91 '` -eq 1 ] ; then # 2 x 2 lola echo 'xdef 180 linear 0 2' echo 'ydef 91 linear -90 2' elif [ `echo $resolution | egrep -c ' nx 192 ny 94 '` -eq 1 ] ; then # 192x94 gaussian grid echo "xdef 192 linear 0 1.875" echo "ydef 94 levels" echo "-88.542 -86.653 -84.753 -82.851 -80.947 -79.043 -77.139 -75.235 -73.331 -71.426" echo "-69.522 -67.617 -65.713 -63.808 -61.903 -59.999 -58.094 -56.189 -54.285 -52.380" echo "-50.475 -48.571 -46.666 -44.761 -42.856 -40.952 -39.047 -37.142 -35.238 -33.333" echo "-31.428 -29.523 -27.619 -25.714 -23.809 -21.904 -20.000 -18.095 -16.190 -14.286" echo "-12.381 -10.476 -8.571 -6.667 -4.762 -2.857 -0.952 0.952 2.857 4.762" echo " 6.667 8.571 10.476 12.381 14.286 16.190 18.095 20.000 21.904 23.809" echo " 25.714 27.619 29.523 31.428 33.333 35.238 37.142 39.047 40.952 42.856" echo " 44.761 46.666 48.571 50.475 52.380 54.285 56.189 58.094 59.999 61.903" echo " 63.808 65.713 67.617 69.522 71.426 73.331 75.235 77.139 79.043 80.947" echo " 82.851 84.753 86.653 88.542" elif [ `echo $resolution | egrep -c ' nx 320 ny 160 '` -eq 1 ] ; then # 320x160 gaussian grid echo "xdef 320 linear 0 1.125" echo "ydef 160 levels" echo "-89.142 -88.029 -86.911 -85.791 -84.670 -83.549 -82.428 -81.307 -80.185 -79.064" echo "-77.943 -76.821 -75.700 -74.578 -73.457 -72.336 -71.214 -70.093 -68.971 -67.850" echo "-66.728 -65.607 -64.485 -63.364 -62.242 -61.121 -60.000 -58.878 -57.757 -56.635" echo "-55.514 -54.392 -53.271 -52.149 -51.028 -49.906 -48.785 -47.663 -46.542 -45.420" echo "-44.299 -43.177 -42.056 -40.934 -39.813 -38.691 -37.570 -36.448 -35.327 -34.205" echo "-33.084 -31.962 -30.841 -29.719 -28.598 -27.476 -26.355 -25.234 -24.112 -22.991" echo "-21.869 -20.748 -19.626 -18.505 -17.383 -16.262 -15.140 -14.019 -12.897 -11.776" echo "-10.654 -9.533 -8.411 -7.290 -6.168 -5.047 -3.925 -2.804 -1.682 -0.561" echo " 0.561 1.682 2.804 3.925 5.047 6.168 7.290 8.411 9.533 10.654" echo " 11.776 12.897 14.019 15.140 16.262 17.383 18.505 19.626 20.748 21.869" echo " 22.991 24.112 25.234 26.355 27.476 28.598 29.719 30.841 31.962 33.084" echo " 34.205 35.327 36.448 37.570 38.691 39.813 40.934 42.056 43.177 44.299" echo " 45.420 46.542 47.663 48.785 49.906 51.028 52.149 53.271 54.392 55.514" echo " 56.635 57.757 58.878 60.000 61.121 62.242 63.364 64.485 65.607 66.728" echo " 67.850 68.971 70.093 71.214 72.336 73.457 74.578 75.700 76.821 77.943" echo " 79.064 80.185 81.307 82.428 83.549 84.670 85.791 86.911 88.029 89.142" else echo >&2 '*** script needs to be modified ***' echo >&2 'unknown user-defined grid' fi else echo >&2 '*** script needs to be modified ***' echo >&2 "unimplimented grid type: $grid" fi # ------------------time----------------------------------- if [ $ntime -eq 1 ] ; then echo "tdef 1 linear ${hour}Z$day$month$year 1mo" elif [ `expr \( $hour + 6 \) % 24` -eq $hour1 ] ; then echo "tdef $ntime linear ${hour}Z$day$month$year 6hr" elif [ `expr \( $hour + 12 \) % 24` -eq $hour1 ] ; then echo "tdef $ntime linear ${hour}Z$day$month$year 12hr" elif [ $mo -ne $mo1 ] ; then echo "tdef $ntime linear ${hour}Z$day$month$year 1mo" elif [ $day -ne $day1 ] ; then echo "tdef $ntime linear ${hour}Z$day$month$year 1dy" else # using default tdef echo "tdef $ntime linear ${hour}Z$day$month$year 1mo" fi # ------------------var------------------------------------- nlevelmax=0 for name in `cat $ListN` do name2=`echo $name | cut -f1 -d:` kpds5=`echo $name | cut -f2 -d:` kpds6=`echo $name | cut -f3 -d:` k=`egrep ":$name2:[^:]*:$kpds5:$kpds6:" $List1 | cut -f8 -d: | sort -u -nr` kpds7s=`echo $k` nlevels=`echo "$kpds7s" | wc -w` comment=`grep "$name2" $List1 | head -n 1 | cut -f2 '-d"'` [ $name2 = 'PV___' ] && name2=PV [ $name2 = 'U-GWD' ] && name2=UGWD [ $name2 = 'V-GWD' ] && name2=VGWD case $kpds6 in 1) tail=sfc;; 2) tail=clb;; 3) tail=clt;; 4) tail=zdg;; 5) tail=lcl;; 6) tail=mwl;; 7) tail=trp;; 8) tail=toa;; 9) tail=bos;; 10) tail=clm;; 12) tail=lcb;; 13) tail=lct;; 14) tail=loc;; 22) tail=mcb;; 23) tail=mct;; 24) tail=mdc;; 32) tail=hcb;; 33) tail=hct;; 34) tail=hic;; 100) tail=$prs;; 101) tail=plr;; 102) tail=msl;; 103) tail=hml;; 104) tail=zlr;; 105) tail=hag;; 106) tail=hlr;; 107) tail=sig;; 108) tail=slr;; 109) tail=hbl;; 110) tail=blr;; 111) tail=dpl;; 112) tail=dlr;; 113) tail=tht;; 114) tail=tlr;; 116) tail=plg;; 121) tail=plr;; 128) tail=slr;; 141) tail=plr;; 160) tail=dsl;; 200) tail=clm;; 212) tail=lcb;; 213) tail=lct;; 214) tail=lcl;; 222) tail=mcb;; 223) tail=mct;; 224) tail=mcl;; 232) tail=hcb;; 233) tail=hct;; 234) tail=hcl;; *) tail=QQQ;; esac [ $kpds6 -eq 111 -a "$kpds7s" = "300" ] && tail=SoilB [ $kpds6 -eq 105 -a "$kpds7s" = "10" ] && tail=10m [ $kpds6 -eq 105 -a "$kpds7s" = "2" ] && tail=2m [ $kpds6 -eq 107 -a "$kpds7s" = "9950" ] && tail=sig995 [ "$suffix" = "no" ] && tail= if [ $kpds6 -eq 112 -a "$kpds7s" = '2760 10' ] ; then echo >>$ListT "${name2}SoilT 0 $kpds5,$kpds6,10 ** $comment" echo >>$ListT "${name2}SoilM 0 $kpds5,$kpds6,2760 ** $comment" nvar=`expr $nvar + 1` continue fi if [ $nlevels -eq 1 ] ; then echo >>$ListT "$name2$tail 0 $kpds5,$kpds6,$kpds7s ** $comment" else nlevels=`echo $nlevels` echo >>$ListT "$name2$tail $nlevels $kpds5,$kpds6,0 ** $comment" if [ $nlevels -gt $nlevelmax ] ; then nlevelmax=$nlevels levelsmax=$kpds7s fi fi done #------------------levels------------------------- if [ $nlevelmax -le 1 ] ; then echo "zdef 1 linear 1 1" else echo "zdef $nlevelmax levels" echo "$levelsmax" fi echo "vars $nvar" cat $ListT echo "ENDVARS" rm $ListA $ListD $List1 $ListN $ListT exit 0