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

if (domain = 'swus')
*SWUS domain
  lats = "29.0 43.6"
  lons = "-121.0 -99.0"
endif
if (domain = 'seus')
*SEUS domain
  lats = "24.5 38.5"
  lons = "-91.8 -74.2"
endif
if (domain = 'pr')
*Puerto Rico domain
  lats = "17.0 19.4"
  lons = "-68.0 -64.5"
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 = 3

day=startday
while (day < endday)

'environ_shaded.ch'
'c'
'open imerge.ctl'
'open imergl.ctl'
'set lat ' lats
'set lon ' lons

*******************
* IMERG plots loop
*******************
time = day*24*2 + 12*2
endtime = time + 1
tback = time - deltat*2

while (time < endtime)
*  say 'time = ' time
  'set t ' time
  cdate = mydate()
  chr = subwrd(cdate,1)
  cdy = subwrd(cdate,2)
  cmo = subwrd(cdate,3)
  cyr = subwrd(cdate,4)
  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)

********************
*Early NRT IMERG plot
********************
  'c'
  say 'Early'
  'set grads off'
* J.Case (6/29/2015) -- trying to display multi-panel images in one graphics file.
* Upper-left
*  'set vpage 0 5.5 4.25 8.5'
* Center-left
  'set vpage 0 5.5 2.12 6.37'
  'environ_shaded.ch'
  'set grads off'
  'set xlint 1'
  'set ylint 1'
  '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.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'
* J.Case (7/30/2015) -- bug fix. Summing over one too many times (24.5 hours)
* Since IMERG is valid from t to t + 30 min, sum from tback to time-1
*  'd sum(prate.1,t='tback',t='time',1)/50.8'
  'd sum(prate.1,t='tback',t='time-1',1)/50.8'

* draw U.S. states shapefile
  'set line 1 1 8'
  'draw shp s_06se12.shp'
* draw U.S. counties shapefile
  'set line 1 5 4'
  '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.1 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.19'
* mm accumulation
*  'draw string 5.5 8.1 Early IMERG 'deltat'-h prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 Early IMERG 'deltat'-h prec (in) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '

********************
*Late NRT IMERG plot
********************
  say 'Late'
  'set grads off'
* J.Case (6/29/2015) -- trying to display multi-panel images in one graphics file.
* Upper-right
*  'set vpage 5.5 11.0 4.25 8.5'
* Center-right
  'set vpage 5.5 11.0 2.12 6.37'
  'environ_shaded.ch'
  'set grads off'
  'set xlint 1'
  'set ylint 1'
  '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.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'
* J.Case (7/30/2015) -- bug fix. Summing over one too many times (24.5 hours)
* Since IMERG is valid from t to t + 30 min, sum from tback to time-1
*  'd sum(prate.2,t='tback',t='time',1)/50.8'
  'd sum(prate.2,t='tback',t='time-1',1)/50.8'

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

  'cbar 1.1 1 10.2 4.3'
  'set string 1 c 10 0'
  'set strsiz 0.19'
* mm accumulation
*  'draw string 5.5 8.1 Late IMERG 'deltat'-h prec (mm) ending: 'chr''cmn'z 'cdy' 'cmo' 'cyr' '
* in accumulation
  'draw string 5.5 8.1 Late IMERG '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'

  time = time + deltat*2
endwhile
'close 2'
'close 1'
*******************
* end IMERG loop
*******************

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
************************************
