#!/bin/tcsh -f
@ loop = 86
while ($loop <= 95)
set yr = $loop
set os = (164 157 161 153 130 153 168 152 151 163)
set ds = (282 294 281 274 287 269 259 293 264 272)
echo 'Onset Date is =' $os
echo 'Demise Date is =' $ds

@ yr1 = $loop + 1900
@ yrp = $yr1 - 1
echo 'YEAR=' $yr1
setenv HD /Net/water/amishra/RSM_ROMS/MONSOON10km/RCM-CLD2/STAMPEDE/A/Daily
echo 'going to prepare ONSET Composite SST Files' $yr1 
@ k = $loop - 85
echo $k
### Onset
@ tstos = $os[$k] - 60
@ tedos = $os[$k] + 60
### Demise 
@ tstds = $ds[$k] - 60
@ tedds = $ds[$k] + 60
echo 'Onset Start time = ', $tstos,'and end time  =', $tedos
echo 'Demise Start time = ', $tstds,'and end time  =', $tedds
####
rm -rf ext-vt_${yr1}.gs
cat > ext-vt_${yr1}.gs << EOF
rc = gsfallow("on")
'reinit'
'open /Net/elninoshare/amishra/RSM_ROMS/MONSOON_M/plots/imdpt25.ctl'
'set t '$tstos
'q dims'
ts = sublin(result, 5)
ts = subwrd(ts, 9)
'set t '$tedos
'q dims'
te = sublin(result, 5)
te = subwrd(te, 9)
'set time 1z31dec$yrp'
'q dims'
tcnt = sublin(result, 5)
tcnt = subwrd(tcnt, 9)
say tcnt
tstart=tcnt+ts
tend=tcnt+te
say tstart
say tend
    'set x 1 135';'set y 1 129'; 'set z 1'
    'set t 'tstart' 'tend
    'define pcp=rf'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt IMDOS_pcp.$yr1.nc'
    'sdfwrite pcp'
'disable fwrite'
****** For Demise date
'set t '$tstds
'q dims'
ts = sublin(result, 5)
ts = subwrd(ts, 9)
'set t '$tedds
'q dims'
te = sublin(result, 5)
te = subwrd(te, 9)
'set time 1z31dec$yrp'
'q dims'
tcnt = sublin(result, 5)
tcnt = subwrd(tcnt, 9)
say tcnt
tstart=tcnt+ts
tend=tcnt+te
say tstart
say tend
***  Monsoon
    'set x 1 135';'set y 1 129'; 'set z 1'
    'set t 'tstart' 'tend
    'define pcp=rf'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt IMDDS_pcp.$yr1.nc'
    'sdfwrite pcp'
'disable fwrite'
'close 1'
'quit'
EOF
################################
/usr/people/amishra/pkg/grads-2.1.a1/bin/grads -blc "run ext-vt_${yr1}.gs" >> ext_$yr1.log &
wait
rm -rf ext-vt_${yr1}.gs
#################################
echo 'prepared file  GrADS readable Rain for' $yr1
 @ loop = $loop + 1
end
exit
