#! /bin/csh

set ydir = `cat list20-tmp`
set nydir = `cat list20-tmp | wc -l`

set count = 1 
#while ($count <= 1)
while ($count <= $nydir)

if ((($ydir[$count] % 4 == 0) && ($ydir[$count] % 100 != 0))||$ydir[$count] % 400 == 0) then
echo "Leap Year" $ydir[$count]
set tt = 8784
else
echo "Not a leap year" $ydir[$count]
set tt = 8761
endif

if ($ydir[$count] == 2005 ) then
set tt = 8736
endif

cat > V$ydir[$count].ctl << EOF
***output grads ctl file for imt_pb***
dset /Net/elninoshare/abhardwaj/PR/GFDL/NHM/$ydir[$count]/plev.dat
title p-plane result
OPTIONS big_endian sequential 
undef -999.9
pdef 250 100 lccr 18.2 294.1 124.7 49 18.2 294.1 294.1 2000.0000 2000.0000
xdef  250 linear  291.443    0.019
ydef  100 levels
  17.307  17.326  17.344  17.362  17.380  17.398  17.416  17.434  17.452  17.470
  17.488  17.506  17.524  17.542  17.560  17.578  17.596  17.614  17.633  17.651
  17.669  17.687  17.705  17.723  17.741  17.759  17.777  17.795  17.813  17.831
  17.849  17.867  17.885  17.903  17.921  17.939  17.957  17.975  17.993  18.011
  18.029  18.047  18.065  18.083  18.101  18.119  18.137  18.155  18.173  18.191
  18.209  18.227  18.245  18.263  18.281  18.299  18.317  18.335  18.353  18.371
  18.389  18.407  18.425  18.443  18.461  18.479  18.497  18.514  18.532  18.550
  18.568  18.586  18.604  18.622  18.640  18.658  18.676  18.694  18.712  18.730
  18.748  18.766  18.784  18.801  18.819  18.837  18.855  18.873  18.891  18.909
  18.927  18.945  18.963  18.981  18.999  19.016  19.034  19.052  19.070  19.088
zdef   16 levels  1000.00  975.00  950.00  925.00  900.00  850.00  800.00  700.00  600.00  500.00  400.00  300.00  250.00  200.00  150.00  100.00
tdef $tt linear 00:00z01JAN$ydir[$count]   60mn
vars    22
T       16 99 T
Z       16 99 Z
U       16 99 U
V       16 99 V
W       16 99 W
TTD     16 99 TTD
CVR     16 99 CVR
CWC     16 99 CWC
OMG     16 99 OMG
VOR      3 99 VOR
SMQR     0 99 SMQR
SMQI     0 99 SMQI
SMQS     0 99 SMQS
SMQG     0 99 SMQG
SMQH     0 99 SMQH
RAIN     0 99 RAIN
PSEA     0 99 PSEA
Psrf     0 99 Psrf
Usrf     0 99 Usrf
Vsrf     0 99 Vsrf
Tsrf     0 99 Tsrf (Kelvin)
TTDsrf   0 99 TTDsrf
endvars
***end of ctl file***
EOF

cat > V$ydir[$count].gs << EOF
'reinit'
'open V$ydir[$count].ctl'
'set gxout fwrite'
'set fwrite VG$ydir[$count].grd'
'set undef -9999.0'

year=$ydir[$count]
say year
mon=1
while(mon<=$tt)
'set t 'mon
zz=1
while(zz<=16)
if(zz = 1 | zz = 4 |zz = 6 | zz = 9 | zz = 10 | zz = 14)
'set z 'zz
'set y 1 100'
'set x 1 250'
'd V'
endif
zz=zz+1  

endwhile
mon=mon+1  
endwhile

'disable fwrite'
'quit'
EOF

/Net/elninost/abhardwaj/tar/grads-2.0.2.oga.2/Classic/bin/grads -blc "run  V$ydir[$count].gs" 

rm -rf   V$ydir[$count].gs
rm -rf   V$ydir[$count].ctl
rm -rf   V20_$ydir[$count].nc

cat >   VG$ydir[$count].ctl << EOF
***output grads ctl file for imt_pb***
dset   VG$ydir[$count].grd 
undef -9999.0
xdef  250 linear  291.443    0.019
ydef  100 levels
  17.307  17.326  17.344  17.362  17.380  17.398  17.416  17.434  17.452  17.470
  17.488  17.506  17.524  17.542  17.560  17.578  17.596  17.614  17.633  17.651
  17.669  17.687  17.705  17.723  17.741  17.759  17.777  17.795  17.813  17.831
  17.849  17.867  17.885  17.903  17.921  17.939  17.957  17.975  17.993  18.011
  18.029  18.047  18.065  18.083  18.101  18.119  18.137  18.155  18.173  18.191
  18.209  18.227  18.245  18.263  18.281  18.299  18.317  18.335  18.353  18.371
  18.389  18.407  18.425  18.443  18.461  18.479  18.497  18.514  18.532  18.550
  18.568  18.586  18.604  18.622  18.640  18.658  18.676  18.694  18.712  18.730
  18.748  18.766  18.784  18.801  18.819  18.837  18.855  18.873  18.891  18.909
  18.927  18.945  18.963  18.981  18.999  19.016  19.034  19.052  19.070  19.088
*zdef   16 levels  1000.00  975.00  950.00  925.00  900.00  850.00  800.00  700.00  600.00  500.00  400.00  300.00  250.00  200.00  150.00  100.00
zdef 6 levels  1000.00 925.00 850.00  600.00  500.00 200.00 
tdef $tt linear 00:00z01JAN$ydir[$count]   1hr
vars    1
V 6 99  V [m/s] 
endvars
***end of ctl file***
EOF

cdo -r -f nc import_binary VG$ydir[$count].ctl V20_$ydir[$count].nc

rm -rf   VG$ydir[$count].ctl
#rm -rf   VG$ydir[$count].grd

@ count = $count + 1
end
