#!/bin/tcsh -f
@ loop = 86
while ($loop <= 95)
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)
set os = (162 153 164 153 133 155 172 148 152 164)
set ds = (283 272 280 280 287 270 260 277 264 275)
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/netcdf/HT
echo 'going to prepare ONSET Composite U,V,T and S 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 ${HD}/hc20.ctl'
'open ${HD}/umt20C.ctl'
'open ${HD}/vmt20C.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
*** MM10 
** LON = 53.0528  to 101.074
** LAT = 4.1612  to 37.3168
    'set x 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define vmt=vm.3'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10OS_vmt20.$yr1.nc'
    'sdfwrite vmt'
'disable fwrite'
    'set x 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define hc20=hc.1'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10OS_hc20.$yr1.nc'
    'sdfwrite hc20'
'disable fwrite'
    'set x 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define umt=um.2'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10OS_umt20.$yr1.nc'
    'sdfwrite umt'
'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 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define vmt=vm.3'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10DS_vmt20.$yr1.nc'
    'sdfwrite vmt'
'disable fwrite'
    'set x 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define hc20=hc.1'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10DS_hc20.$yr1.nc'
    'sdfwrite hc20'
'disable fwrite'
    'set x 1 513';'set y 1 386'; 'set z 1'
    'set t 'tstart' 'tend
    'define umt=um.2'
    'set undef -9.99e8'
    'set sdfwrite -4d -flt MM10DS_umt20.$yr1.nc'
    'sdfwrite umt'
'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
