function hplot (args)

expid   = subwrd(args,1)
export  = subwrd(args,2)
GC      = subwrd(args,3)
field   = subwrd(args,4)
season  = subwrd(args,5)
output  = subwrd(args,6)
level   = subwrd(args,7)
nseason = subwrd(args,8)
bdate   = subwrd(args,9)
edate   = subwrd(args,10)
expdsc  = subwrd(args,11)
name    = subwrd(args,12)

undef   = 1e15
qmin    = undef
qmax    = undef
qcnt    = undef
'set undef 'undef

 prefix = " "
 suffix = " "

'c'
'define lwmask1 = abs('field''season')-abs('field''season')'
'define lwmask2 = abs('field''season')-abs('field''season')'

say ''
'run getenv "GEOSUTIL"'
             geosutil = result

'run getenv "LEVTYPE"'
             CLEVS = result

*******************************************************************
****                                                           ****
****                    Get Shading Land Mask                  ****
****                                                           ****
*******************************************************************

'getinfo time'
         time = result
'getinfo zpos'
         zpos = result
'getinfo file'
         curfile = result
'open 'geosutil'/plots/grads_util/lwmask1440721.tabl'
'getinfo numfiles'
         newfile = result
'set dfile 'newfile
'set t 1'
'set z 1'
'define mask = lwmask'
'close 'newfile
'set dfile 'curfile
'set t 'time
'set z 'zpos

*******************************************************************
****                                                           ****
****        Get Plotting Values from Resource File             ****
****                                                           ****
*******************************************************************

PLOTRC = geosutil'/plots/grads_util/plot.rc'

say 'LEVTYPE: 'CLEVS
                        'getresource 'PLOTRC' 'export'_'GC'_'level'_CBSCALE' 
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_CBSCALE' ; endif
                                                            cbscale = result

                        'getresource 'PLOTRC' 'export'_'GC'_'level'_FACTOR'
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_FACTOR' ; endif
                                                            fact = result

                        'getresource 'PLOTRC' 'export'_'GC'_'level'_TITLE'
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_TITLE' ; endif
                                                            title = result

                        'getresource 'PLOTRC' 'export'_'GC'_'level'_CCOLS'
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_CCOLS' ; endif
                                                            ccols = result

                        'getresource 'PLOTRC' 'export'_'GC'_'level'_'CLEVS
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_'CLEVS ; endif
if( result = 'NULL' ) ; 'getresource 'PLOTRC' 'export'_'GC'_CLEVS' ; endif
                                                            clevs = result

                        'getresource 'PLOTRC' 'export'_'GC'_REGRID'
                                                            method = result
say ''


* Set Default Plotting Variables
********************************

if( title   = 'NULL' )
  'getdesc 'name
     title  = result
endif
if( fact    = 'NULL' ) ; fact    = 1         ; endif
if( clevs   = 'NULL' ) ; clevs   = "0.0 0.0" ; endif
if( ccols   = 'NULL' ) ; ccols   = "0.0 0.0" ; endif
if( cbscale = 'NULL' ) ; cbscale =  0.65     ; endif
if( method  = 'NULL' ) ; method  = "bs_p1"   ; endif




if( export = 'SRFDIS' )
     title  = 'Frictional Heating Rate from Surface Drag  (K/s Pa) '
   prefix  = "0"
endif


if( export = 'QS' )
     title  = 'Surface Specific Humidity  (g/kg) '
     fact  = 1e3
     qmin  = 2
     qcnt  = 2
   prefix  = "0"
endif


if( export = 'TS' )
     title  = 'Surface Skin Temperature (K) '
     qcnt  = 5
endif

if( export = 'THAT' )
     title  = 'Effective Surface Skin Temperature (K) '
     qcnt  = 5
endif

if( export = 'QHAT' )
     title  = 'Effective Surface Specific Humidity  (g/kg) '
   prefix  = "0"
     qmin  = 2
     qcnt  = 2
endif


if( export = 'CT' )
     title  = 'Surface Exchange Coeeficient for Heat (x10`a-2`n kg/m`a2`n/s) '
   prefix  = "0"
      fact = 100
     ccols = "71  72  55  49   47   45   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = ".3 .4 .5 .7 .9 1.1 1.3 1.5 2 4   6   8   10  12  14  16  18  20  25"
   'set map 1 1 6'
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'CQ' )
     title  = 'Surface Exchange Coeeficient for Moisture  (x10`a-2`n kg/m`a2`n/s) '
   prefix  = "0"
      fact = 100
     ccols = "71  72  55  49   47   45   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = ".3 .4 .5 .7 .9 1.1 1.3 1.5 2 4   6   8   10  12  14  16  18  20  25"
   'set map 1 1 6'
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'CN' )
     title  = 'Surface Neutral Drag Coeeficient (x10`a-2`n)'
   prefix  = "0"
      fact = 100
      qmin =   1
      qcnt =   0.7
     ccols = "71 72  55  49   47   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = "0.08 0.09 .1 .12 .14 .17  .2  .3  .40  .60 .80  1.7  2.5  4.0  7.5  9.5  11.0 12.0  13"
   'set map 1 1 6'
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'CM' )
     title  = 'Surface Exchange Coeeficient for Momentum  (x10`a-2`n kg/m`a2`n/s) '
   prefix  = "0"
      fact = 100
     ccols = "71  72  55  49   47   45   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = ".3 .4 .5 .7 .9 1.1 1.3 1.5 2 4   6   8   10  12  14  16  18  20  25"
    'set map 1 1 6'
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'RI' & GC = 'SURFACE' )
     title  = 'Surface Bulk Richardson Number'
     clevs = "-10  -5  -4  -2  -1  -.9  -.7  -.5 -.3  -.1  .1  .2  .25  .5   1   2   5  10"
   'set map 1 1 6'
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif
if( export = 'Z0' )
     title  = 'Surface Roughness  (mm) '
     fact  = 1000
     ccols = "71  72  55  49   47   45   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = "0.02 0.05 0.07 0.1 0.3 0.5 1 2 4 6 8 10 50 100 500 1000 2000 3000 4000 5000"
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'Z0H' )
     title  = 'Surface Roughness for Heat  (mm) '
     fact  = 1000
     ccols = "71  72  55  49   47   45   44   37   36   34   33   32  31   21   22   23   24   25   26   27   28 "
     clevs = "0.02 0.05 0.07 0.1 0.3 0.5 1 2 4 6 8 10 50 100 500 1000 2000 3000 4000 5000"
   'setmask 2'
   'define  lwmask2 = lwmask2-0.5'
endif

if( export = 'SPEED' )
     title  = 'Lowest Model Layer Wind Speed  (m/s)'
   cbscale = 1.0
     fact  = 1
     clevs = "0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 6 7 8 9 10 11 12 14"
endif

if( export = 'VENT' )
     title  = 'Lowest Model Layer Wind Speed + Gustiness (m/s)'
   cbscale = 1.0
     fact  = 1
     clevs = "0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 6 7 8 9 10 11 12 14"
endif

if( export = 'GUST' )
     title  = 'Boundary Layer Gustiness + Redelsperger (m/s)'
   cbscale = 1.0
     fact  = 1
     clevs = "0.2 0.4 0.6 0.8 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.8  2.0 2.2 2.4 2.6 2.8 3.0"
endif

if( export = 'WET1' )
   'setmask 1'
   'define  lwmask1 = 0.5-lwmask1'
   'define  lwmask2 = lwmask1'
endif

if( export = 'SNOMAS' )
     title  = 'Snow Mass (kg/m`a2`n [mm])'
     fact  =  1
   prefix  = "0"
   cbscale = 1.1
     clevs = "0.1 0.2 0.4 0.6 0.8 1 5 10 25 50 75 100 150 200 250 300 400 500"
endif

* Moist
*******

if( export = 'RI' & GC = 'MOIST' )
     title  = 'Ice phase cloud particle effective radius  (x10`a-6`n m)'
     fact  = 1e6
endif

if( export = 'RL' )
     title  = 'Liquid cloud particle effective radius  (x10`a-6`n m)'
     fact  = 1e6
endif

if( export = 'LAI' )
     clevs = "0.1 0.2 0.4 0.5 0.6 0.8 1.0 1.5 2.0  2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5"
endif



'define qmod = 'field''season'*'fact
'getinfo lon'
         lon = result
'getinfo lat'
         lat = result
'define lwmask2 = regrid2( lwmask1, .25, .25, bs_p1  , 'lon', 'lat' )'
'define qmod2   = regrid2( qmod,    .25, .25,'method', 'lon', 'lat' )'



'set display color white'
'set vpage off'
'set parea off'
'set grads off'
'set clab off'

* Display TAN shading LandMask where QMOD is defined, GREY shading where UNDEF
* ----------------------------------------------------------------------------
'set gxout shaded'
'set ccols 92 0'
'set clevs 0.5'
'd mask'
'set rgb 99 233 216 180'
'set rgb 99 212 190 146'
'set ccols 99 0'
'set clevs 0.5'
'd maskout( mask,abs(qmod2) )'

if( clevs != "0.0 0.0" )
    'set clevs 'clevs

* The rest of this mess is to simply compute an appropriate CINT from defined clevs
* ---------------------------------------------------------------------------------
say 'CLEVS: 'clevs
    length = strlen(clevs)
            i = 1
       bit = substr(clevs,i,1)
              while( bit = ' ' )
                       i = i + 1
                     bit = substr(clevs,i,1)
              endwhile

            k = 1
        val.k = ''
     while( i<=length )
           bit  = substr(clevs,i,1)
       if( bit != ' ' )
          val.k = val.k''bit
*      say 'k: 'k' val: 'val.k
       else
              while( bit = ' ' & i<=length )
                     bit = substr(clevs,i,1)
                       i = i + 1
              endwhile
              i = i - 2
              k = k + 1
          val.k = ''
       endif
       i = i + 1
*      say 'i: 'i
*      pull flag
     endwhile

          beg = 0
            n = 1
     while( n<= k & beg = 0 )
           bit  = substr(val.n,1,1)
       if( bit != "" )
           beg  = n
       endif
            n = n + 1
     endwhile
           bit  = substr(val.k,1,1)
       if( bit != "" )
           last = k
       else
                       k = k - 1
                     bit = substr(val.k,1,1)
              while( bit = ' ' )
                       k = k - 1
                     bit = substr(val.k,1,1)
              endwhile
           last = k
       endif
       begp1 = beg+1
       cint  = val.begp1-val.beg
      say ' beg: 'beg' val: 'val.beg
      say 'last: 'last' val: 'val.last
      say 'cint: 'cint
               n = beg
        while( n<=last-1)
               np1 = n+1
          prod = val.n * val.np1
              say 'n: 'n' np1: 'np1' val.n: 'val.n' val.np1: 'val.np1' prod: 'prod
          if( prod < 0 )
                   cint = ( val.np1 - val.n )/2
              say 'cint: 'cint
                   colors = "55 49 47 45 44 36 34 33 32   0   21 22 23 24 25 26 27 28 69 "
          else
                   colors = "55 49 47 45 44 37 36 34 33 32 31 21 22 23 24 25 26 27 28 "
                   if( prefix != " " )
                       colors = "0 49 47 45 44 37 36 34 33 32 31 21 22 23 24 25 26 27 28 "
                   endif
                   if( suffix != " " )
                       colors = "55 49 47 45 44 37 36 34 33 32 31 21 22 23 24 25 26 27 0"
                   endif
          endif
               n = n+1
        endwhile
        if( ccols != "0.0 0.0" )
               'set ccols 'ccols
                say 'CCOLS: 'ccols
        else
               'set ccols 'colors
                say 'CCOLS: 'colors
        endif
        n = 0
* ----------------------------------------------------------------------------

else

'set gxout stat'
'd qmod2'
qminmax = sublin(result,8)
qmin    = subwrd(qminmax,4)
qmax    = subwrd(qminmax,5)
'set gxout shaded'
'd abs('qmin')'
        qmin = subwrd(result,4)
'd abs('qmax')'
        qmax = subwrd(result,4)
if( qmin > qmax ) ; qmax = qmin ; endif
if( qmax > 0 )
   'd log10('qmax')'
    n = subwrd(result,4)
else
    n = 0
endif
say '    Log Factor: 'n
if( n<0 ) ; n = n-2 ; endif
'getint 'n
         n = result
if( n>0 )
    if( n<=2 )
        n = 0
    else
        n = n-2
    endif
endif
say 'Scaling Factor: 'n

if( n>0 )
say 'define qmod2 = qmod2 / 1e'n
    'define qmod2 = qmod2 / 1e'n
else
m = -1 * n
say 'define qmod2 = qmod2 * 1e'm
    'define qmod2 = qmod2 * 1e'm
endif
      'shades 'qmod2' 0'
       cint = result*2
endif

if( prefix = "0" | suffix = "0" ) ; 'black' ; endif
 'd maskout( maskout(qmod2,lwmask2), abs(qmod2)-'cint' )'
*'d maskout(         qmod2,          abs(qmod2)-'cint' )'

'cbarn -snum 'cbscale

'stats qmod'
 avgmod = subwrd(result,1)
 stdmod = subwrd(result,2)

'set vpage off'
'set string 1 c 6'
'set strsiz .12'
'draw string 5.5 8.1 EXPID: 'expid'  'expdsc
if( level != 0 )
    if( n != 0 )
   'draw string 5.5 7.73 'export':'GC
   'draw string 5.5 7.48 'level'-mb 'title'  'season' ('nseason')    (x10`a'n'`n)'
    else
   'draw string 5.5 7.73 'export':'GC
   'draw string 5.5 7.48 'level'-mb 'title'  'season' ('nseason')'
    endif
else
    if( n != 0 )
   'draw string 5.5 7.73 'export':'GC
   'draw string 5.5 7.48 'title'  'season' ('nseason')    (x10`a'n'`n)'
    else
   'draw string 5.5 7.73 'export':'GC
   'draw string 5.5 7.48 'title'  'season' ('nseason')'
    endif
endif

* Print Beginning and Ending Dates
* --------------------------------
                date = getdate (bdate)
bmnthm = subwrd(date,1)
byearm = subwrd(date,2)
                date = getdate (edate)
emnthm = subwrd(date,1)
eyearm = subwrd(date,2)

'set string 1 l 4'
'set strsiz .08'
'draw string 0.14  8.10 Beg: 'bmnthm' 'byearm
'draw string 0.14  7.95 End: 'emnthm' 'eyearm
'set string 1 c 6'
* --------------------------------

'set string 1 l 4'
'set strsiz .08'
'draw string 0.14 7.70 Mean: 'avgmod
'draw string 0.14 7.55  Std: 'stdmod

'myprint -name 'output'/hdiag_'export'.'GC'_'level'.'season

return

function getdate (date,month,year)
       num = 1
       bit = substr(date,num,1)
while( bit != '' )
       num = num+1
       bit = substr(date,num,1)
endwhile
       loc = num-7
     month = substr(date,loc  ,3)
      year = substr(date,loc+3,4)
return month' 'year

