#! /bin/csh

set cdir = `pwd` 

#ls -v *.ctl >list-ctl

set ydir = `cat $cdir/list-ctl`
set yr = `cat $cdir/list-ctl | cut -c1-4`
set nydir = `cat $cdir/list-ctl |wc -l`

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

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

cat > ext.gs << EOF
'reinit'
'open $ydir[$count]'
'set gxout fwrite'
'set fwrite tmp.$yr[$count].grd'
'set undef -9999.0'
t=1
while(t<=$tt)
'set t 't
'set y 1 100'
'set x 1 201'
'd TMPsfc'
'd PRATEsfc'
t=t+6
endwhile
'disable fwrite'
'quit'
EOF
grads -blc "run ext.gs"
@ count = $count + 1
end

