#! /bin/csh

set ydir = `cat list21-tmp`
set nydir = `cat list21-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
set t1 = 366
else
echo "Not a leap year" $ydir[$count]
set tt = 8761
set t1 = 365
endif

if ($ydir[$count] == 2060 ) then
set tt = 8760
set t1 = 365
endif

cat > SMQI$ydir[$count].ctl << EOF
***output grads ctl file for imt_pb***
dset /Net/elninoshare/abhardwaj/PR/CCSM4/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 > SMQI$ydir[$count].gs << EOF
'reinit'
'open SMQI$ydir[$count].ctl'
'set gxout fwrite'
'set fwrite SMQI$ydir[$count].grd'
'set undef -9999.0'

year=$ydir[$count]
say year
mon=1
while(mon<=12)
mons='JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'
if ($tt>8783)
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
if (year=2060)
days='31 29 31 30 31 30 31 31 30 31 30 30'
endif
i=1
while(i<=subwrd(days,mon))

'set y 1 100'
'set x 1 250'

'df=ave(SMQI,time=00:00z'i''subwrd(mons,mon)''year',time=23:00z'i''subwrd(mons,mon)''year')'

'd df'
i=i+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 SMQI$ydir[$count].gs" 

rm -rf SMQI$ydir[$count].gs
rm -rf SMQI$ydir[$count].ctl

rm -rf SMQI21_$ydir[$count].nc

cat > SMQIG$ydir[$count].ctl << EOF
***output grads ctl file for imt_pb***
dset SMQI$ydir[$count].grd 
undef -9999.
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   1 linear 1 1 
tdef $t1 linear 00:00z01JAN$ydir[$count]   1dy
vars    1
SMQI  0 99 SMQI 
endvars
***end of ctl file***
EOF

cdo -f nc import_binary SMQIG$ydir[$count].ctl SMQI21_$ydir[$count].nc

rm -rf SMQIG$ydir[$count].ctl
rm -rf SMQI$ydir[$count].grd

@ count = $count + 1
end
