#!/bin/tcsh -f
@ loop = 79
while ($loop <= 105)
##while ($loop <= 80)
set yr = $loop
set os = (121 171 146 142 150 143 161 153 177 157 157 144 133 155 148 149 153 142 132 186 149 160 143 157 138 154 151)
set ds = (275 249 262 281 298 276 265 255 325 254 285 286 283 279 305 322 294 293 363 275 291 279 274 270 274 272 299)
echo 'Onset Date is =' $os
echo 'Demise Date is =' $ds

@ yr1 = $loop + 1900
@ yrp = $yr1 - 1
echo 'YEAR=' $yr1
setenv HD /Net/water/amishra/DATA/CFSR_HT/SST
echo 'going to prepare ONSET Composite SST Files' $yr1 
@ k = $loop - 78
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/elninost/obs/CFSR/daily/ocnh01.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
**** Lon = 270.75 to 284.75   X = 542 to 570
**** LAT = 18.75 to 30.75   Y = 218 to 242
****    'set x 90 220'; 'set y 120 232'
*** FLorida Monsoon
**Lon = 260.25 to 310.25   X = 521 to 621
**Lat = 0.25 to 35.75   Y = 181 to 252
    'set x 521 621';'set y 181 252'
    'set t 'tstart' 'tend
    'define sst=TMPsfc-273.15'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrOS_sst.$yr1.nc'
    'sdfwrite sst'
'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
*** FLorida Monsoon
    'set x 521 621';'set y 181 252'
    'set t 'tstart' 'tend
    'define sst=TMPsfc-273.15'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrDS_sst.$yr1.nc'
    'sdfwrite sst'
'disable fwrite'
'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 Temperature for' $yr1
 @ loop = $loop + 1
end
exit
