#!/bin/tcsh -f
@ loop = 87
while ($loop <= 95)
set yr = $loop
set os = (151)
set ds = (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/RSM_ROMS/MONSOON10km/RCM-CLD2/STAMPEDE/O/ONSET_DEMISE/CFSR
echo 'going to prepare ONSET Composite U,V,T and S Files' $yr1 
@ k = $loop - 86
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
*** Indian Monsoon
** 52.25 to 100.75   X = 105 to 202
** 3.75 to 37.25   Y = 188 to 255
    'set x 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define v=vogrddsl'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrOS_vcur.$yr1.nc'
    'sdfwrite v'
'disable fwrite'
    'set x 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define otemp=potdsl-273.15'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrOS_otemp.$yr1.nc'
    'sdfwrite otemp'
'disable fwrite'
    'set x 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define u=uogrddsl'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrOS_ucur.$yr1.nc'
    'sdfwrite u'
'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 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define v=vogrddsl'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrDS_vcur.$yr1.nc'
    'sdfwrite v'
'disable fwrite'
    'set x 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define otemp=potdsl-273.15'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrDS_otemp.$yr1.nc'
    'sdfwrite otemp'
'disable fwrite'
    'set x 105 202';'set y 188 255'; 'set z 1 40'
    'set t 'tstart' 'tend
    'define u=uogrddsl'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt cfsrDS_ucur.$yr1.nc'
    'sdfwrite u'
'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
#################################
echo 'prepared file  GrADS readable Temperature for' $yr1
 @ loop = $loop + 1
end
exit
