#! /bin/csh

set ddir = /hycom/ftp/datasets/GLBu0.08/expt_19.1/data

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

set cnt = 1
while ($cnt <= $n)

if ((($yr[$cnt] % 4 == 0) && ($yr[$cnt] % 100 != 0))||$yr[$cnt] % 400 == 0) then
echo "Leap Year" $yr[$cnt]
set tt = 2928
else
echo "Not a leap year" $yr[$cnt]
set tt = 2920
endif

cat > hycom$yr[$cnt].ctl << EOF
dset $ddir/$yr[$cnt]/hycom_GLBu0.08_191_%y4%m2%d200_t0%h2.nc
title Ocean data  
options template 
undef -30000s _FillValue
UNPACK scale_factor add_offset 
dtype netcdf
xdef 4500 linear -180 0.08
ydef 2001 linear -80 0.08 
zdef 40 levels 5000 4000 3000 2500 2000 1500 1250 1000
 900 800 700 600 500 400 350 300 250 200
 150 125 100 90 80 70 60 50 45 40
 35 30 25 20 15 12 10 8 6 4
 2 0
tdef $tt linear 00Z01JAN$yr[$cnt] 3hr
vars 6
tau=>tau  0  t  Tau
water_u=>water_u  40  t,z,y,x  Eastward Water Velocity
water_v=>water_v  40  t,z,y,x  Northward Water Velocity
water_temp=>water_temp  40  t,z,y,x  Water Temperature
salinity=>salinity  40  t,z,y,x  Salinity
surf_el=>surf_el  0  t,y,x  Water Surface Elevation
endvars
EOF


cat > tmp$yr[$cnt].gs << EOF
'reinit'
'open hycom$yr[$cnt].ctl'
'set gxout fwrite'
'set fwrite ta$yr[$cnt].grd'
'set undef -9999.0'

year=$yr[$cnt]
say year
mon=1
while(mon<=12)
mons='JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'
if ($tt>2927)
days='31 29 31 30 31 30 31 31 30 31 30 31'
else
days='31 28 31 30 31 30 31 31 30 31 30 31'
endif

zz=1
while(zz<=40)
'set z 'zz
'set lat 21 30.525'
'set lon -87 -77'
'df=ave(water_temp,time=00z'01subwrd(mons,mon)''year',time=21z'subwrd(days,mon)''subwrd(mons,mon)''year')'
'd df'
zz=zz+1
endwhile
say mon
mon=mon+1
endwhile
'disable fwrite'
'quit'
EOF
/Net/elninost/abhardwaj/tar/grads-2.0.2.oga.2/Classic/bin/grads -blc "run tmp$yr[$cnt].gs" >& $yr[$cnt].log &
@ cnt = $cnt + 1     
end

