#! /bin/csh

set dr = /Net/elninoshare/abhardwaj/PR/CCSM4/RSM
set cdir = `pwd`

set yr = `cat list-19`
set n = `cat list-19 |wc -l`

set cnt = 1
while ($cnt <= $n)
cd $dr/$yr[$cnt]
ls -v r_pgb.ft* > $cdir/list-$yr[$cnt]

set y1dir = `cat $cdir/list-$yr[$cnt]`
set n1dir = `cat $cdir/list-$yr[$cnt] |wc -l`
grib2ctl.pl -verf $dr/$yr[$cnt]/r_pgb.ft%f2 > $cdir/$yr[$cnt].ctl
pwd

sed  s:$dr/$yr[$cnt]/r_pgb.ft00.idx:./$yr[$cnt].idx:g $cdir/$yr[$cnt].ctl >$cdir/$yr[$cnt]
#if ( $cnt <= 2 ) then
#sed  -i -e s/'tdef 1000 linear '00Z01jan$yr[$cnt]' 1hr'/'tdef '$n1dir' linear '00Z01jan$yr[$cnt]' 1hr'/g $cdir/$yr[$cnt] 
#else
sed  -i -e s/'tdef 997 linear '00Z01jan$yr[$cnt]' 1hr'/'tdef '$n1dir' linear '00Z01jan$yr[$cnt]' 6hr'/g $cdir/$yr[$cnt] 
#endif

cd $cdir
mv -f $yr[$cnt] $yr[$cnt].ctl
gribmap -i $yr[$cnt].ctl
pwd

@ cnt = $cnt + 1
end

