* To be substituted by a master script
domain = 'DOMAIN'
deltat = 1
say ' '
say 'Processing 'deltat'-h precip for domain 'domain

*SWUS domain
if (domain = 'swus')
  lats = "29.0 43.6"
  lons = "-121.0 -99.0"
endif
*SEUS domain
if (domain = 'seus')
  lats = "24.5 38.5"
  lons = "-91.8 -74.2"
endif
*Puerto Rico domain
if (domain = 'pr')
  lats = "17.0 19.4"
  lons = "-68.0 -64.5"
endif
*Bahamas domain (for Hurricane Dorian rainfall QPE)
if (domain = 'bahamas')
  lats = "22.9 37.2"
  lons = "-85.9 -65.7"
  labela = "2.3 1.5"
  labelb = "2.3 1.1"
  llint = 2
endif
if (domain = 'dorian')
  lats = "LATLL LATUR"
  lons = "LONLL LONUR"
  labela = "2.9 1.4"
  labelb = "2.9 1.0"
  llint = 1
endif
*Southern US domain
if (domain = 'sus')
  lats = "28 39"
  lons = "-101 -87"
endif
*West Virgina centered domain
if (domain = 'wv')
  lats = "36.0 42.0"
  lons = "-84.0 -76.5"
  labela = "2.7 1.4"
  labelb = "2.7 1.0"
  llint = 2
endif
*Midwest centered domain (2019 flooding)
if (domain = 'midwest')
  lats = "30.5 45.0"
  lons = "-106.0 -87.8"
  labela = "2.7 1.4"
  labelb = "2.7 1.0"
  llint = 4
endif

* time is the grads valid time after time = 1 (TBD in script)
* for the period ending the 24-h accumulation period
* ndays = # of days past grads start time
startday = 1
endday = 2

day=startday
while (day < endday)

*******************
* IMERG plots loop
*******************
* IMERG times
*time = day*24*2 + HH*2
time = TIMETOT
endtime = time + 1
*tback = time - deltat*2
tback = 1
* STIV/MRMS times
timestivmrms = 1 + day*24

while (time < endtime)
  'open imergl.ctl'
  'open imergf.ctl'
  'set t ' time
  'set lat ' lats
  'set lon ' lons

  cdate = mydate()
  chr = subwrd(cdate,1)
  cdy = subwrd(cdate,2)
  cmo = subwrd(cdate,3)
  cyr = subwrd(cdate,4)
  say '------------------------------------------------'
  say 'Doing IMERG 'deltat'-h plots at time = 'cdate
  remain = math_fmod(time,2)
  if (remain = 0)
    hh = substr(chr,1,2)
    cmn = ":00"
    min = "00"
  else
    hh = chr
    cmn = ""
    min = "30"
  endif
  cmm = cmo2cmm(cmo)

********************
*Late NRT IMERG plot
********************
  'c'
  say 'Late'
  'set grads off'
* J.Case (6/29/2015) -- trying to display multi-panel images in one graphics file.
  if (domain = 'pr' | domain = 'dorian')
* Center-left
    'set vpage 0 5.5 2.12 6.37'
  else
* Upper-left
    'set vpage 0 5.5 4.25 8.5'
  endif
  'environ_shaded.ch'
  'set grads off'
  'set xlint 'llint
  'set ylint 'llint
  'set xlopts 1 6 0.16'
  'set ylopts 1 6 0.16'
* mm
*  'set clevs   0.1  1  2  5  10 15 20 25 30 40 50 75 100'
*  'set ccols 0   33 36 39 43 46 49 53 56 59 63 65  67  29'
*  'd sum(prate.1,t='tback',t='time',1)/2'
* AHPS inches scale and approximate color RGBs
  'set clevs   0.5   1    2    3    4   6   8  10  15  20  25  30   35  40  50'
  'set ccols 0   43   46    4   33   36  39  22  23  24  25  27  29  64   55   0'
  'd sum(prate.1,t='tback',t='time',1)/50.8'
  'd amax(sum(prate.1,t='tback',t='time',1)/50.8,g)'
*  say 'Result2: 'result
  maxval = subwrd(result,14)
*  say maxval
  maxvalfmt = math_format('%6.1f',maxval)
  say 'MAXVAL-Late: 'maxvalfmt
  'set line 87'
  xlo = XPOSMAX - 1.0
  ylo = YPOSMAX - 0.175
  xhi = XPOSMAX + 1.0
  yhi = YPOSMAX + 0.175
  'draw recf 'xlo' 'ylo' 'xhi' 'yhi
  'set string 28 c 7 0'
  'set strsiz 0.16'
  'draw string XPOSMAX YPOSMAX MaxVal:'maxvalfmt

* draw U.S. states shapefile
  'set line 1 1 3'
  'draw shp s_06se12.shp'
* draw U.S. counties shapefile
  'set line 1 5 3'
  'draw shp c_15au13.shp'
* draw Colorado RFC basin shapefile
*  'set line 2 1 6'
*  'draw shp b_cbrfc.shp'
* draw SE RFC basin shapefile
*  'set line 2 1 6'
*  'draw shp b_serfc.shp'

  'cbar 1.05 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.18'
* mm accumulation
*  'draw string 5.5 8.1 IMERG-L Storm Tot Prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 IMERG-L Storm Tot Prec (in) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '

********************
*Final IMERG plot
********************
  say 'Final'
  'set grads off'
  if (domain = 'pr' | domain = 'dorian')
* Center-right
    'set vpage 5.5 11.0 2.12 6.37'
  else
* Upper-right
    'set vpage 5.5 11.0 4.25 8.5'
  endif
  'environ_shaded.ch'
  'set grads off'
  'set xlint 'llint
  'set ylint 'llint
  'set xlopts 1 6 0.16'
  'set ylopts 1 6 0.16'
* mm
*  'set clevs   0.1  1  2  5  10 15 20 25 30 40 50 75 100'
*  'set ccols 0   33 36 39 43 46 49 53 56 59 63 65  67  29'
*  'd sum(prate.2,t='tback',t='time',1)/2'
* AHPS inches scale and approximate color RGBs
  'set clevs   0.5   1    2    3    4   6   8  10  15  20  25  30   35  40  50'
  'set ccols 0   43   46    4   33   36  39  22  23  24  25  27  29  64   55   0'
  'd sum(prate.2,t='tback',t='time',1)/50.8'
  'd amax(sum(prate.2,t='tback',t='time',1)/50.8,g)'
*  say 'Result2: 'result
  maxval = subwrd(result,14)
  maxvalfmt = math_format('%6.1f',maxval)
  say 'MAXVAL-Final: 'maxvalfmt
  'set line 87'
  xlo = XPOSMAX - 1.0
  ylo = YPOSMAX - 0.175
  xhi = XPOSMAX + 1.0
  yhi = YPOSMAX + 0.175
  'draw recf 'xlo' 'ylo' 'xhi' 'yhi
  'set string 28 c 7 0'
  'set strsiz 0.16'
  'draw string XPOSMAX YPOSMAX MaxVal:'maxvalfmt

* draw U.S. states shapefile
  'set line 1 1 3'
  'draw shp s_06se12.shp'
* draw U.S. counties shapefile
  'set line 1 5 3'
  'draw shp c_15au13.shp'

  'cbar 1.05 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.18'
* mm accumulation
*  'draw string 5.5 8.1 IMERG-F Storm Tot Prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 IMERG-F Storm Tot Prec (in) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
  'close 2'
  'close 1'

* Block out STIV/MRMS processing for 'pr' domain
if (domain != 'pr' & domain != 'dorian')

***********************
* STIV plot 
***********************
  say 'Doing Stage IV 'deltat'-h plot at time = 'cdate
  'open stiv.ctl'
  'set t ' timestivmrms
  'set lat ' lats
  'set lon ' lons
* Bottom-left
  'set vpage 0 5.5 0 4.25'
  'environ_shaded.ch'
  'set grads off'
  'set xlint 'llint
  'set ylint 'llint
  'set xlopts 1 6 0.16'
  'set ylopts 1 6 0.16'
* mm
*  'set clevs   0.1  1  2  5  10 15 20 25 30 40 50 75 100'
*  'set ccols 0   33 36 39 43 46 49 53 56 59 63 65  67  29'
*  'd APCPsfc.1'
* AHPS inches scale and approximate color RGBs
  'set clevs  0.01 0.10 0.25 0.50 0.75 1.0 1.5 2.0 2.5 3.0 4.0 5.0 6.0 8.0 10.0'
  'set ccols 0   43   46    4   33   36  39  22  23  24  25  27  29  64   55   0'
  'd APCPsfc.1/25.4'
* draw U.S. states shapefile
  'set line 1 1 3'
  'draw shp s_06se12.shp'
* draw U.S. counties shapefile
  'set line 1 5 3'
  'draw shp c_15au13.shp'

  'cbar 1.05 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.18'
* mm accumulation
*  'draw string 5.5 8.1 Stage IV 'deltat'-h prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 Stage IV 'deltat'-h prec (in) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
  'close 1'

***********************
* MRMS plot loop
***********************
  say 'Doing MRMS 'deltat'-h plot at time = 'cdate
  'open mrms.ctl'
  'set t ' timestivmrms
  'set lat ' lats
  'set lon ' lons
* Bottom-right
  'set vpage 5.5 11.0 0 4.25'
  'environ_shaded.ch'
  'set grads off'
  'set xlint 'llint
  'set ylint 'llint
  'set xlopts 1 6 0.16'
  'set ylopts 1 6 0.16'
* mm
*  'set clevs   0.1  1  2  5  10 15 20 25 30 40 50 75 100'
*  'set ccols 0   33 36 39 43 46 49 53 56 59 63 65  67  29'
*  'd APCPsfc.1'
* AHPS inches scale and approximate color RGBs
  'set clevs  0.01 0.10 0.25 0.50 0.75 1.0 1.5 2.0 2.5 3.0 4.0 5.0 6.0 8.0 10.0'
  'set ccols 0   43   46    4   33   36  39  22  23  24  25  27  29  64   55   0'
  'd APCPsfc.1/25.4'

* draw U.S. states shapefile
  'set line 1 1 3'
  'draw shp s_06se12.shp'
* draw U.S. counties shapefile
  'set line 1 5 3'
  'draw shp c_15au13.shp'

  'cbar 1.05 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.18'
* mm accumulation
*  'draw string 5.5 8.1 MRMS 'deltat'-h prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 MRMS 'deltat'-h prec (in) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
*  'gxprint precCompare-'domain'-'deltat'h_'cyr cmm cdy'_'hh min'z.png png x1200 y800 white'
  'close 1'

*if block out for STIV/MRMS for 'pr' and 'dorian' domains
endif

  'gxprint prectotStormCentric-final-'domain'-'deltat'h_'cyr cmm cdy'_'hh min'z.png png x1200 y800 white'

* Increment IMERG time for loop
  time = time + deltat*2
*  tback = time - deltat*2

* Increment STIV/MRMS time
  timestivmrms = timestivmrms + deltat

endwhile

day = day + 1

endwhile
***************
* end day loop
***************

'quit'

************************************
function mydate
'query time'
sres = subwrd(result,3)
i=1
while (substr(sres,i,1)!='Z')
i=i+1
endwhile
hour = substr(sres,1,i-1)
isav = i
i = i + 1
while (substr(sres,i,1)>='0' & substr(sres,i,1)<='9')
i = i + 1
endwhile
day = substr(sres,isav+1,i-isav-1)
month = substr(sres,i,3)
year = substr(sres,i+3,4)
return (hour' 'day' 'month' 'year)
************************************

************************************
function cmo2cmm(cmo)
if (cmo = "JAN") ; cmm = "01" ; endif
if (cmo = "FEB") ; cmm = "02" ; endif
if (cmo = "MAR") ; cmm = "03" ; endif
if (cmo = "APR") ; cmm = "04" ; endif
if (cmo = "MAY") ; cmm = "05" ; endif
if (cmo = "JUN") ; cmm = "06" ; endif
if (cmo = "JUL") ; cmm = "07" ; endif
if (cmo = "AUG") ; cmm = "08" ; endif
if (cmo = "SEP") ; cmm = "09" ; endif
if (cmo = "OCT") ; cmm = "10" ; endif
if (cmo = "NOV") ; cmm = "11" ; endif
if (cmo = "DEC") ; cmm = "12" ; endif
return cmm
************************************

