function zcloseplt (args)

'numargs  'args
 numargs = result

        num = 0
while ( num < numargs )
        num = num + 1

if( subwrd(args,num) = '-EXPID'   ) ; expid    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-EXPORT'  ) ; EXPORT   = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-ALIAS'   ) ; alias    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-QFILE'   ) ; qfile    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-CFILE'   ) ; cfile    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-OFILE'   ) ; ofile    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-ONAME'   ) ; obsnam   = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-CNAME'   ) ; cmpnam   = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-OBDATE'  ) ; begdateo = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-OEDATE'  ) ; enddateo = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-NMOD'    ) ; nmod     = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-NOBS'    ) ; nobs     = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-QDESC'   ) ; qdesc    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-ODESC'   ) ; odesc    = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-OUTPUT'  ) ; output   = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-SEASON'  ) ; season   = subwrd(args,num+1) ; endif
if( subwrd(args,num) = '-PTOP'    ) ; ptop     = subwrd(args,num+1) ; endif

endwhile

* Get PTOP Attributes
* -------------------
   scale = ''
     len = get_length (ptop)
     if( len>1 )
         bit = substr(ptop,len-1,1)
          if( bit = ":" )
              ptop = substr(ptop,1,len-2)
             scale = 'LOG'
          endif
     endif

* Check for Contour Level Type
* ----------------------------
'run getenv "LEVTYPE"'
             LEVTYPE = result

say '-EXPID 'expid
say '-EXPORT 'EXPORT
say '-ALIAS  'alias
say '-QFILE 'qfile
say '-OFILE 'ofile
say '-CFILE 'cfile
say '-ONAME 'obsnam
say '-CNAME 'cmpnam
say '-OBDATE 'begdateo
say '-OEDATE 'enddateo
say '-NMOD 'nmod
say '-NOBS 'nobs
say '-QDESC 'qdesc
say '-ODESC 'odesc
say '-OUTPUT 'output
say '-SEASON 'season
say '-PTOP   'ptop

say ' LEVTYPE 'LEVTYPE

* Get Dates for Plots
* -------------------
'run getenv "BEGDATE"'
             begdate  = result
'run getenv "ENDDATE"'
             enddate  = result
if( begdate = "NULL" )
   'set dfile 'qfile
   'set t    '1
   'getinfo date'
         begdate = result
endif
if( enddate = "NULL" )
   'set dfile 'qfile
   'getinfo tdim'
            tdim     = result
   'set t  'tdim
   'getinfo date'
         enddate = result
endif

'run getenv "CLIMATE"'
             climate = result
if( begdate = begdateo & enddate = enddateo )
         climate = 'Actual'
endif

'set gxout shaded'

* Get Plotting Values from Resource File
* --------------------------------------
'run getenv "GEOSUTIL"'
             geosutil = result
PLOTRC = geosutil'/plots/grads_util/plot.rc'
 
if( scale = LOG )
    zid = 'zlog'ptop
else
    zid = 'z'ptop
endif

if( EXPORT = "U"   )
    oname = "/horiz_"cmpnam"_uwnd_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' U_DYN_TITLE'           ; title = result
   'getresource 'PLOTRC' U_DYN_Z'scale'_CLEVS'  ; clevs = result
   'getresource 'PLOTRC' U_DYN_Z'scale'_CCOLS'  ; ccols = result
   'getresource 'PLOTRC' U_DYN_Z'scale'_DCOLS'  ; dcols = result
   'getresource 'PLOTRC' U_DYN_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' U_DYN_Z'scale'_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' U_DYN_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' U_DYN_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' U_DYN_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' U_DYN_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "V"   )
    oname = "/horiz_"cmpnam"_vwnd_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' V_DYN_TITLE'           ; title = result
   'getresource 'PLOTRC' V_DYN_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' V_DYN_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' V_DYN_Z_DCOLS'         ; dcols = result
   'getresource 'PLOTRC' V_DYN_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' V_DYN_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' V_DYN_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' V_DYN_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' V_DYN_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "T"   )
    oname = "/horiz_"cmpnam"_tmpu_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' T_DYN_TITLE'           ; title = result
   'getresource 'PLOTRC' T_DYN_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' T_DYN_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' T_DYN_Z_'LEVTYPE'COLS' ; dcols = result
   'getresource 'PLOTRC' T_DYN_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' T_DYN_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' T_DYN_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' T_DYN_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' T_DYN_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "Q"   )
    oname = "/horiz_"cmpnam"_sphu_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' Q_MOIST_TITLE'           ; title = result
   'getresource 'PLOTRC' Q_MOIST_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' Q_MOIST_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' Q_MOIST_Z_DCOLS'         ; dcols = result
   'getresource 'PLOTRC' Q_MOIST_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' Q_MOIST_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' Q_MOIST_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' Q_MOIST_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' Q_MOIST_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "QLTOT"   )
    oname = "/horiz_"cmpnam"_ql_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' QLTOT_AGCM_TITLE'           ; title = result
   'getresource 'PLOTRC' QLTOT_AGCM_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' QLTOT_AGCM_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' QLTOT_AGCM_Z_DCOLS'         ; dcols = result
   'getresource 'PLOTRC' QLTOT_AGCM_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' QLTOT_AGCM_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' QLTOT_AGCM_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' QLTOT_AGCM_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' QLTOT_AGCM_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "QITOT"   )
    oname = "/horiz_"cmpnam"_qi_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' QITOT_AGCM_TITLE'           ; title = result
   'getresource 'PLOTRC' QITOT_AGCM_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' QITOT_AGCM_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' QITOT_AGCM_Z_DCOLS'         ; dcols = result
   'getresource 'PLOTRC' QITOT_AGCM_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' QITOT_AGCM_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' QITOT_AGCM_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' QITOT_AGCM_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' QITOT_AGCM_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "RH2" )
    oname = "/horiz_"cmpnam"_rh_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' RH2_MOIST_TITLE'           ; title = result
   'getresource 'PLOTRC' RH2_MOIST_Z_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' RH2_MOIST_Z_CCOLS'         ; ccols = result
   'getresource 'PLOTRC' RH2_MOIST_Z_DCOLS'         ; dcols = result
   'getresource 'PLOTRC' RH2_MOIST_Z_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' RH2_MOIST_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' RH2_MOIST_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' RH2_MOIST_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' RH2_MOIST_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "ZLE"   )
    oname = "/horiz_"cmpnam"_hght_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' ZLE_DYN_TITLE'                  ; title = result
   'getresource 'PLOTRC' ZLE_DYN_Z'scale'_CLEVS'         ; clevs = result
   'getresource 'PLOTRC' ZLE_DYN_Z_CCOLS'                ; ccols = result
   'getresource 'PLOTRC' ZLE_DYN_Z'scale'_'LEVTYPE'COLS' ; dcols = result
   'getresource 'PLOTRC' ZLE_DYN_Z'scale'_'LEVTYPE'LEVS' ; dlevs = result
   'getresource 'PLOTRC' ZLE_DYN_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' ZLE_DYN_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' ZLE_DYN_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' ZLE_DYN_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "OMEGA"   )
    oname = "/horiz_"cmpnam"_omega_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' OMEGA_DYN_TITLE'           ; title = result
   'getresource 'PLOTRC' OMEGA_DYN_CLEVS'           ; clevs = result
   'getresource 'PLOTRC' OMEGA_DYN_CCOLS'           ; ccols = result
   'getresource 'PLOTRC' OMEGA_DYN_'LEVTYPE'COLS'   ; dcols = result
   'getresource 'PLOTRC' OMEGA_DYN_'LEVTYPE'LEVS'   ; dlevs = result
   'getresource 'PLOTRC' OMEGA_DYN_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' OMEGA_DYN_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' OMEGA_DYN_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' OMEGA_DYN_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
if( EXPORT = "O3"   )
    oname = "/horiz_"cmpnam"_o3_"zid"_closeness_"obsnam
   'getresource 'PLOTRC' O3_CHEMISTRY_TITLE'                    ; title = result
   'getresource 'PLOTRC' O3_CHEMISTRY_Z_CCOLS'                  ; ccols = result
   'getresource 'PLOTRC' O3_CHEMISTRY_Z_DCOLS'                  ; dcols = result
   'getresource 'PLOTRC' O3_CHEMISTRY_Z'scale'_CLEVS'           ; clevs = result
   'getresource 'PLOTRC' O3_CHEMISTRY_Z'scale'_'LEVTYPE'LEVS'   ; dlevs = result
   'getresource 'PLOTRC' O3_CHEMISTRY_CLOSE_PLOT_FACTOR' ; clspltfact = result
   'getresource 'PLOTRC' O3_CHEMISTRY_CLOSE_PLOT_CINT'   ; clspltcint = result
   'getresource 'PLOTRC' O3_CHEMISTRY_FIXED_PLOT_FACTOR' ; fixpltfact = result
   'getresource 'PLOTRC' O3_CHEMISTRY_FIXED_PLOT_CINT'   ; fixpltcint = result
endif
say ' TITLE: 'title
say ' CLEVS: 'clevs
say ' DLEVS: 'dlevs

* Make Mean Plot
* --------------
'set vpage off'
'set parea off'
'set grid  off'
'set frame on'
'set xlopts 1 3 .11'
'set ylopts 1 3 .11'
'set display color white'
'rgbset'
'c'

'set dfile 'qfile
'setlats'
'setlons'
'getinfo  lonmin'
          lonmin = result

'set lon 'lonmin
'set lev 1000 'ptop
'set t 1'

* Top Plot:  Source_EXP - Verfification
* -------------------------------------
'set vpage 0 8.5 0.0 11'
'set parea 1.5 7.0 7.70 10.50'
'set dfile 'qfile
'setlats'
'set lon 'lonmin
'set lev 1000 'ptop
'set t 1'
'set zlog  off'
if( scale = LOG ) ; 'setlevs' ; endif
'set grads off'
'set gxout shaded'

 source_fact = 1
'run getenv "CINTDIFF"'
             CINTDIFF  = result
if( CINTDIFF = 'NULL' & dlevs != 'NULL' & dcols != 'NULL' )
   'set clevs 'dlevs
   'set ccols 'dcols
   'd dif1z'
   'set gxout contour'
   'set ccolor 1'
   'set clevs 'dlevs
   'd dif1z'
else

* Examine Both Source_EXP and Comparison_EXP for Consistent Scaling
* -----------------------------------------------------------------
   'stats dif1z'
     avgdif = subwrd(result,1)
     stddif = subwrd(result,2)
       qmax = stddif/3
   if( qmax > 0 )
      'd log10('qmax')'
       n = subwrd(result,4)
   else
       n = 0
   endif
   say '    Log Factor for Source_EXP: '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
   source_n = n
   say 'Diff Scaling Factor for Source_EXP: 'source_n
      'd 'qmax'/1e'source_n
       source_cint = subwrd(result,4)
   say '   Contour Interval for Source_EXP: 'source_cint

* -----------------------------------------------------------------
   'stats dif2z'
     avgdif = subwrd(result,1)
     stddif = subwrd(result,2)
       qmax = stddif/3
   if( qmax > 0 )
      'd log10('qmax')'
       n = subwrd(result,4)
   else
       n = 0
   endif
   say '    Log Factor for Comparison_EXP: '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
   comparison_n = n
   say 'Diff Scaling Factor for Comparison_EXP: 'comparison_n
      'd 'qmax'/1e'comparison_n
       comparison_cint = subwrd(result,4)
   say '   Contour Interval for Comparison_EXP: 'comparison_cint

* -----------------------------------------------------------------

       if( comparison_n > source_n )
               source_n = comparison_n
            source_cint = comparison_cint
       endif

* Set Hardwired Values for Consistent Plots
* -----------------------------------------
       if( fixpltfact != NULL )
       'd 'fixpltfact
        source_n = subwrd(result,4)
       endif
       say 'Diff Scaling Factor: 'source_n

       if( fixpltcint != NULL )
       'd 'fixpltcint
           fixpltcint = subwrd(result,4)
           source_cint = fixpltcint
       endif

* -----------------------------------------------------------------

      'shades 'source_cint
      'define qzplt = dif1z/1e'source_n
       source_fact = "10**"source_n
      'd qzplt'
      'set gxout contour'
      'set clevs -'source_cint' 'source_cint
      'set ccolor 1'
      'd qzplt'

endif
'set parea 0 8.5 7.0 11'
'cbarn -vert'


* Middle Plot:  Comparison_EXP - Verfification
* --------------------------------------------
'set vpage 0 8.5 0.0 11'
'set parea 1.5 7.0 4.30 7.10'
'set dfile 'cfile
'setlats'
'set lon 'lonmin
'set lev 1000 'ptop
'set t 1'
'set zlog  off'
if( scale = LOG ) ; 'setlevs' ; endif
'set grads off'
'set gxout shaded'

'run getenv "CINTDIFF"'
             CINTDIFF  = result
if( CINTDIFF = 'NULL' & dlevs != 'NULL' & dcols != 'NULL' )
   'set clevs 'dlevs
   'set ccols 'dcols
   'd dif2z'
   'set gxout contour'
   'set ccolor 1'
   'set clevs 'dlevs
   'd dif2z'
else

      'shades 'source_cint
      'define qzplt = dif2z/1e'source_n
      'd qzplt'
      'set gxout contour'
      'set clevs -'source_cint' 'source_cint
      'set ccolor 1'
      'd qzplt'

endif

* Bottom Plot:  Closeness to Verfification
* ----------------------------------------
'set vpage 0 8.5 0.0 11'
'set parea 1.5 7.0 0.90 3.70'
'set dfile 'ofile
'setlats'
'set lon 'lonmin
'set lev 1000 'ptop
'set t 1'
'set zlog  off'
if( scale = LOG ) ; 'setlevs' ; endif
'set grads off'
'set gxout shaded'

 dfact = 1
'run getenv "CINTDIFF"'
             CINTDIFF  = result
   'stats absdifz'
     avgdif = subwrd(result,1)
     stddif = subwrd(result,2)
       qmax = stddif/3
   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

* Set Hardwired Values for Consistent Plots
* -----------------------------------------
   if( clspltfact != NULL )
   'd 'clspltfact
    n = subwrd(result,4)
   endif
   say 'Diff Scaling Factor: 'n

   if( clspltcint != NULL )
   'd 'clspltcint
       clspltcint = subwrd(result,4)
       cint = clspltcint
   else
      'd 'qmax'/1e'n
       cint = subwrd(result,4)
   endif

      'shades 'cint
      'define qzplt = absdifz/1e'n
       dfact = "10**"n
      'd qzplt'
      'cbarn -snum 0.55 -xmid 4.25 -ymid 0.4'
      'set gxout contour'
      'set clevs -'cint' 'cint
      'set ccolor 1'
      'd qzplt'

'set dfile 'qfile
'setlats'
'set lon 'lonmin
'set lev 1000 'ptop
'set t 1'

'draw ylab Pressure (mb)'

'set vpage off'
'set string 1 l 4'
'set strsiz .065'
'draw string 0.05 0.08 ( EXPID:  'expid' )'

'set string 1 c 6'
'set strsiz .13'
if( source_fact = 1 )
   'draw string 4.25 10.85 'title' Difference'
else
   'draw string 4.25 10.85 'title' Difference  x 'source_fact
endif

'set strsiz .10'
'draw string 4.25 10.635 'qdesc' - 'obsnam'  'season' ('nmod')'
'draw string 4.25  7.235 'cmpnam' - 'obsnam'  'season' ('nobs') ('climate')'

'set strsiz .09'
if( dfact = 1 )
'draw string 4.25 3.95 Closeness:  [ |'expid' - 'obsnam'| ] minus'
'draw string 4.25 3.80 [ |'cmpnam' - 'obsnam'| ]'
else
'draw string 4.25 3.95 Closeness:  [ |'expid' - 'obsnam'| ] minus'
'draw string 4.25 3.80 [ |'cmpnam' - 'obsnam'| ]  x 'dfact
endif


                date = getdate (begdate)
bmnthm = subwrd(date,1)
byearm = subwrd(date,2)
                date = getdate (enddate)
emnthm = subwrd(date,1)
eyearm = subwrd(date,2)
                date = getdate (begdateo)
bmntho = subwrd(date,1)
byearo = subwrd(date,2)
                date = getdate (enddateo)
emntho = subwrd(date,1)
eyearo = subwrd(date,2)

'set string 1 l 4'
'set strsiz .08'
'draw string 0.10 10.37 Beg: 'bmnthm' 'byearm
'draw string 0.10 10.24 End: 'emnthm' 'eyearm
'draw string 0.10  6.97 Beg: 'bmntho' 'byearo
'draw string 0.10  6.84 End: 'emntho' 'eyearo
'set string 1 c 6'

'myprint -name 'output'/'oname'.'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

function get_length (string)
tb = ""
i = 1
while (i<=256)
blank = substr(string,i,1)
if( blank = tb )
length = i-1
i = 999
else
i = i + 1
endif
endwhile
return length
