function progz (args)

                            n =     1
EXPORT   = subwrd(args,n) ; n = n + 1
GC       = subwrd(args,n) ; n = n + 1

expid    = subwrd(args,n) ; n = n + 1
output   = subwrd(args,n) ; n = n + 1
debug    = subwrd(args,n) ; n = n + 1

ptop = 0.1


* Define Seasons to Process
* -------------------------
seasons  = ''
       k = n
while( k > 0 )
    season = subwrd(args,k)
if( season = '' )
    k = -1
else
    seasons = seasons % ' ' % season
k = k+1
endif
endwhile
uppercase seasons
          seasons = result
                                                                                                                                                     
* Initialize
* ----------
'reinit'
'set display greyscale white'
'set csmooth on'
'set clab off'
'c'


* Determine Variable Name and Location
* ------------------------------------
if ( EXPORT = "HE" )
    'run getvar  ZLE      DYN'
else
    'run getvar 'EXPORT' 'GC
endif
         mname  = subwrd(result,1)
         mfile  = subwrd(result,2)
         scale  = subwrd(result,3)
         qdesc  = subwrd(result,4)
         qtag   = subwrd(result,5)

if( mfile = "NULL" ) ; return ; endif

'run getenv "GEOSUTIL"'
         geosutil = result
                                                                                                   
'run getenv "VERIFICATION"'
         verification = result

'run getenv "ANALYSIS"'
         analysis = result

'run getenv "STD_DEV"'
         std_dev  = result

'run getenv "CMPEXP_ONLY"'
             cmpexp_only = result



* Model Experiment Data
* ---------------------
'set dfile 'mfile
'setdates'
'run getenv "BEGDATE"'
             begdate  = result
'run getenv "ENDDATE"'
             enddate  = result
*'setlons'
*'setlats'
'set lon 0 360'
'set lat -90 90'
'setz'
'sett'

* Get Dimension of Model Environment
* ----------------------------------
'getinfo lonmin'
         lonmin = result
'getinfo lonmax'
         lonmax = result
'getinfo latmin'
         latmin = result
'getinfo latmax'
         latmax = result

                     ' alias ' mname
                      malias = result
'chckname            'malias
'seasonalf -FUNCTION 'malias'*'scale' -NAME mod0'
 modfile  = subwrd(result,1)
 
*******************************************************************
****   Loop over Possible Experiment Datasets for Comparison   ****
*******************************************************************

'!/bin/mv HISTORY.T HISTORY.Tmp'
'run getenv "CMPEXP"'
         cmpexp = result
         numexp = 1

          dummy = get_cmpexp (cmpexp,numexp)
            exp = subwrd(dummy,1)
           type = subwrd(dummy,2)
     exp.numexp = exp
    type.numexp = type

while( exp != 'NULL' )
say ' '
say 'Comparing with: 'exp

* analysis = false  EXP=M CMP=M  => ALEVS
* analysis = false  EXP=M CMP=A  => DLEVS
* analysis = true   EXP=A CMP=A  => ALEVS
* analysis = true   EXP=A CMP=M  => DLEVS

* INPUT Experiment is an Analysis
*********************************
if( analysis != "false" )
    if( type = A | type = V )
*   CMP Experiment is an Analysis
       'run setenv "LEVTYPE" 'A
    else
*   CMP Experiment is an Model
       'run setenv "LEVTYPE" 'D
    endif
else
* INPUT Experiment is a Model
*********************************
    if( type = A )
*   CMP Experiment is an Analysis
       'run setenv "LEVTYPE" 'D
    else
*   CMP Experiment is an Model
       'run setenv "LEVTYPE" 'A
    endif
endif

'!chckfile 'exp'/.HOMDIR'
 'run getenv CHECKFILE'
         CHECKFILE  = result
     if( CHECKFILE != 'NULL' )
        '!/bin/cp `cat 'exp'/.HOMDIR`/HISTORY.rc .'
     else
        '!/bin/cp 'exp'/HISTORY.rc .'
     endif
'!remove CHECKFILE.txt'

'!cat HISTORY.rc | sed -e "s/,/ , /g" | sed -e "s/*/@/g" > HISTORY.T'

if ( EXPORT = "HE" )
    'run getvar  ZLE      DYN 'exp
else
    'run getvar 'EXPORT' 'GC' 'exp
endif
               cname.numexp = subwrd(result,1)
               cfile.numexp = subwrd(result,2)
               cscal.numexp = subwrd(result,3)
               cdesc.numexp = subwrd(result,4)
                ctag.numexp = subwrd(result,5)

* Only Perform Comparison if Variable Exists
* ------------------------------------------
if( cname.numexp != 'NULL' )

'set dfile 'cfile.numexp
    'getdates'
     begdateo = subwrd(result,1)
     enddateo = subwrd(result,2)

'set lon 'lonmin' 'lonmax
'set lat 'latmin' 'latmax
'setz'

                     ' alias ' cname.numexp
                      oalias = result
'chckname            'oalias
say 'seasonalf -FUNCTION 'oalias'*'cscal.numexp' -NAME exp'numexp
    'seasonalf -FUNCTION 'oalias'*'cscal.numexp' -NAME exp'numexp
 expfile.numexp  = subwrd(result,1)
 

* Loop over Seasons to Process
* ----------------------------
                 m = 1
          while( m > 0 )
              season = subwrd(seasons,m)
          if( season = '' )
                   m = -1
          else
                   m = m+1

say 'Processing Season: 'season

'set dfile 'mfile
'count "'season'" 'begdate' 'enddate
 nmod =  result

'set dfile 'cfile.numexp
'count "'season'" 'begdateo' 'enddateo
 nobs =  result


* Set EXPORT Model, Observation Scaling Factors, and PTOPs for plots (for LOG use :L)
* -----------------------------------------------------------------------------------
if( EXPORT = "U"     ) ; facm = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "V"     ) ; facm = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "T"     ) ; facm = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "Q"     ) ; facm = 1000  ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "ZLE"   ) ; facm = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "RH2"   ) ; facm = 100   ; nplots = 2 ; ptops = "30 30:L"       ; endif
if( EXPORT = "OMEGA" ) ; facm = 864   ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "O3"    ) ; facm = 1e6   ; nplots = 2 ; ptops = "30      0.1:L" ; endif
if( EXPORT = "QLTOT" ) ; facm = 1e6   ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "QITOT" ) ; facm = 1e6   ; nplots = 1 ; ptops = "30"            ; endif

faco = facm

* Create Temporary File at 0.25x0.25 degree resolution with consistent levels
* ---------------------------------------------------------------------------
'set dfile 'mfile
'set lon 0 360'
'set lat -90 90'

'set dfile 'modfile
'setz'
'set t 1'
'define mod = mod0'season'*'facm
'makez  mod z'

'set dfile 'expfile.numexp
'setz'
'set t 1'
'define obs = exp'numexp''season'*'faco
'makez  obs z'

'set dfile 'mfile
'set t 1'
'makezdif -q1 mod -q2 obs -file1 'modfile' -file2 'expfile.numexp' -ptop 'ptop


* Make ZPLT
* ---------
                      nplot  = 1
               while( nplot <= nplots )
                      plttop = subwrd( ptops,nplot )

                       flag = ""
               while ( flag = "" )

'run 'geosutil'/plots/zcmp/zplt -EXPID 'expid' -EXPORT 'EXPORT' -ALIAS 'mname' -QFILE 'mfile' -OFILE 'cfile.numexp' -ONAME 'ctag.numexp' -OBDATE 'begdateo' -OEDATE 'enddateo' -NMOD 'nmod' -NOBS 'nobs' -QDESC 'qdesc ' -ODESC 'cdesc.numexp' -OUTPUT 'output' -SEASON 'season' -PTOP 'plttop

                if( debug = "debug" )
                    say "Hit  ENTER  to repeat plot"
                    say "Type 'next' for  next plot, 'done' for next field"
                    pull flag
                else
                    flag = "next"
                endif
               'c'
               endwhile

               nplot = nplot + 1
               endwhile

* End Seasonal Test
* -----------------
endif

* End Seasonal Loop
* -----------------
endwhile

* End CNAME Check
* ---------------
endif

* Check next Comparison Experiment Dataset
* ----------------------------------------
    numexp = numexp + 1
  dummy = get_cmpexp (cmpexp,numexp)
    exp = subwrd(dummy,1)
   type = subwrd(dummy,2)
   exp.numexp = exp
  type.numexp = type

endwhile
'!/bin/mv HISTORY.Tmp HISTORY.T'
    numexp = numexp - 1

* ---------------------------------------------------------------------------
* Now that we have computed plots for each experiment,
* we can compute the Closeness plots to MERRA-2 and any CMPEXP ending with :V
* ---------------------------------------------------------------------------

       k  = 1
while( k <= numexp )
       say 'Looping through experiments, k = 'k' CTAG = 'ctag.k' TYPE = 'type.k

       if( ( ctag.k = "MERRA-2" | type.k = V ) & cname.k != 'NULL' )
            TAG   = k
            say 'Performing Closeness plots to: 'ctag.TAG' k = 'k

*           Loop over Seasons to Process
*           ----------------------------
                   m = 1
            while( m > 0 )
                season = subwrd(seasons,m)
            if( season = '' )
                     m = -1
            else
                     m = m+1
                     say 'Processing Season: 'season

                    'set dfile 'mfile
                    'set gxout shaded'
                    'rgbset'

*                   Zonal Mean Closeness Plot (Experiment_vs_Comparison to Verification)
*                   --------------------------------------------------------------------
                           n  = 1
                    while( n <= numexp )
                           say 'Testing 'qtag' and 'ctag.n' for closeness with 'ctag.TAG

                           if( ctag.n != "merra" & ctag.n != "MERRA-2" & ctag.n != ctag.TAG & type.n != V & cname.n != "NULL" )
                               say 'Closeness plot between  exp: 'qtag
                               say '                       cexp: 'ctag.n
                               say '                        obs: 'ctag.TAG
                               say '              Total  numexp: 'numexp
                               say ''

                              'set dfile 'modfile
                              'set lon  0 360'
                              'set lat -90 90'
                              'setz'
                              'set t 1'
                              'define mod = mod0'season'*'facm

                              'set dfile 'expfile.n
                              'set lon  0 360'
                              'set lat -90 90'
                              'setz'
                              'set t 1'
                              'define cmp = exp'n''season'*'faco

                              'set dfile 'expfile.TAG
                              'set lon  0 360'
                              'set lat -90 90'
                              'setz'
                              'set t 1'
                              'define obs = exp'TAG''season'*'faco

                              'set dfile 'mfile
                              'set lon  0 360'
                              'set lat -90 90'
                              'set t 1'

                              'makezdif -q1 mod -q2 obs -file1 'modfile'   -file2 'expfile.TAG' -ptop 'ptop' -name   dif1     '
                              'makezdif -q1 mod -q2 obs -file1 'modfile'   -file2 'expfile.TAG' -ptop 'ptop' -name  adif1 -abs'
                              'run getenv "ZDIFILE"'
                                           zdifile1 = result

                              'makezdif -q1 cmp -q2 obs -file1 'expfile.n' -file2 'expfile.TAG' -ptop 'ptop' -name   dif2     '
                              'makezdif -q1 cmp -q2 obs -file1 'expfile.n' -file2 'expfile.TAG' -ptop 'ptop' -name  adif2 -abs'
                              'run getenv "ZDIFILE"'
                                           zdifile2 = result

                              'set dfile 'zdifile1
                              'setx'
                              'sety'
                              'setz'
                              'set t 1'
                              'define q1 = q.'zdifile1
                              'set dfile 'zdifile2
                              'setx'
                              'sety'
                              'setz'
                              'set t 1'
                              'define q2 = q.'zdifile2
                              'makezdif -q1 q1 -q2 q2 -file1 'zdifile1' -file2 'zdifile2' -ptop 'ptop' -name absdif'
                              'run getenv "ZDIFILE"'
                                           zdifile3 = result

*                              Make ZCLOSEPLT
*                              --------------
                                      nplot  = 1
                               while( nplot <= nplots )
                                      plttop = subwrd( ptops,nplot )

                                         flag = ""
                                 while ( flag = "" )

                                     'run 'geosutil'/plots/zcmp/zcloseplt -EXPID 'expid' -EXPORT 'EXPORT' -ALIAS 'mname' -QFILE 'zdifile1' -CFILE 'zdifile2' -OFILE 'zdifile3' -ONAME 'ctag.TAG' -CNAME 'ctag.n' -OBDATE 'begdateo' -OEDATE 'enddateo' -NMOD 'nmod' -NOBS 'nobs' -QDESC 'qdesc ' -ODESC 'cdesc.n' -OUTPUT 'output' -SEASON 'season' -PTOP 'plttop

                                      if( debug = "debug" )
                                          say "Hit  ENTER  to repeat plot"
                                          say "Type 'next' for  next plot, 'done' for next field"
                                          pull flag
                                      else
                                          flag = "next"
                                      endif

                                     'c'
                                 endwhile

                              nplot = nplot + 1
                              endwhile


*                          End TAG Check
*                          -------------
                           endif

                    n  = n + 1
                    endwhile ;* END While_N Loop

*           End Season Test
*           ---------------
            endif

            endwhile ;* END While_m>0 (Season) Loop

*      ---------------
       endif

k = k + 1
endwhile ;* END While_k Loop



if( cmpexp_only = TRUE ) ; return ; endif

*********************************************************************
****   Loop over Possible Verification Datasets for Comparison   ****
*********************************************************************

* Set EXPORT Model, Observation Scaling Factors, and PTOPs for plots (for LOG use :L)
* -----------------------------------------------------------------------------------
if( EXPORT = "U"     ) ; facm = 1     ; faco = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "V"     ) ; facm = 1     ; faco = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "T"     ) ; facm = 1     ; faco = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "Q"     ) ; facm = 1000  ; faco = 1000  ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "ZLE"   ) ; facm = 1     ; faco = 1     ; nplots = 3 ; ptops = "30 30:L 0.1:L" ; endif
if( EXPORT = "RH2"   ) ; facm = 100   ; faco = 100   ; nplots = 2 ; ptops = "30 30:L"       ; endif
if( EXPORT = "OMEGA" ) ; facm = 864   ; faco = 864   ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "O3"    ) ; facm = 1e6   ; faco = 1e6   ; nplots = 2 ; ptops = "30      0.1:L" ; endif
if( EXPORT = "QLTOT" ) ; facm = 1e6   ; faco = 1e6   ; nplots = 1 ; ptops = "30"            ; endif
if( EXPORT = "QITOT" ) ; facm = 1e6   ; faco = 1e6   ; nplots = 1 ; ptops = "30"            ; endif

* Get Plotting Values from Resource File
* --------------------------------------
'run getenv "GEOSUTIL"'
         geosutil = result
PLOTRC = geosutil'/plots/grads_util/plot.rc'

' getnumrc 'geosutil'/plots/zcmp'
     rcinfo = result
     numrc  = subwrd( rcinfo,1 )
       num  = 1
       cnt  = 0
while( num <= numrc )
        loc = num + 1
     rcfile = subwrd( rcinfo,loc )
              OBS = EXPORT
     'run getobs 'OBS' 'GC' 'rcfile
               oname = subwrd(result,1)
               ofile = subwrd(result,2)
               oscal = subwrd(result,3)
               odesc = subwrd(result,4)
              obsnam = subwrd(result,5)

if( analysis != "false" )
   'run setenv "LEVTYPE" 'A
else
   'run setenv "LEVTYPE" 'D
endif

* Compute PLOT for Specific Verification
* --------------------------------------
if( oname != 'NULL' ) 

'set dfile 'ofile
    'getdates'
     begdateo = subwrd(result,1)
     enddateo = subwrd(result,2)

'set lon 'lonmin' 'lonmax
'set lat 'latmin' 'latmax
'setz'

                     ' alias ' oname
                      oalias = result
'chckname            'oalias

* Compute ECOPS differently due to missing monthly means
* ------------------------------------------------------
if( obsnam = "ecops" )
   'define   qecops = 'oalias'*'oscal
   'seasonal qecops'
   'define obs'num'jan = qecopsjan'
   'define obs'num'feb = qecopsfeb'
   'define obs'num'mar = qecopsmar'
   'define obs'num'apr = qecopsapr'
   'define obs'num'may = qecopsmay'
   'define obs'num'jun = qecopsjun'
   'define obs'num'jul = qecopsjul'
   'define obs'num'aug = qecopsaug'
   'define obs'num'sep = qecopssep'
   'define obs'num'oct = qecopsoct'
   'define obs'num'nov = qecopsnov'
   'define obs'num'dec = qecopsdec'
   'define obs'num'djf = qecopsdjf'
   'define obs'num'mam = qecopsmam'
   'define obs'num'jja = qecopsjja'
   'define obs'num'son = qecopsson'
   'define obs'num'ann = qecopsann'
    obsfile  = ofile
else
   'seasonalf -FUNCTION 'oalias'*'oscal' -NAME obs'num
    obsfile  = subwrd(result,1)
endif


 

* Loop over Seasons to Process
* ----------------------------
                 m = 1
          while( m > 0 )
              season = subwrd(seasons,m)
          if( season = '' )
                   m = -1
          else
                   m = m+1

say 'Processing Season: 'season

'set dfile 'mfile
'count "'season'" 'begdate' 'enddate
 nmod =  result

'set dfile 'ofile
'count "'season'"'
 nobs =  result


* Create Temporary File at 0.25x0.25 degree resolution with consistent levels
* ---------------------------------------------------------------------------
'set dfile 'mfile
*'setlons'
*'setlats'
'set lon 0 360'
'set lat -90 90'

'set dfile 'modfile
'setz'
'set t 1'
'define mod = mod0'season'*'facm
'makez  mod z'

'set dfile 'obsfile
'setz'
'set t 1'
'define obs = obs'num''season'*'faco
'makez  obs z'

'set dfile 'mfile
'set t 1'
'makezdif -q1 mod -q2 obs -file1 'modfile' -file2 'obsfile' -ptop 'ptop

* Make ZPLT
* ---------
                      nplot  = 1
               while( nplot <= nplots )
                      plttop = subwrd( ptops,nplot )

                       flag = ""
               while ( flag = "" )

'run 'geosutil'/plots/zcmp/zplt -EXPID 'expid' -EXPORT 'EXPORT' -ALIAS 'mname' -QFILE 'mfile' -OFILE 'ofile' -ONAME 'obsnam' -OBDATE 'begdateo' -OEDATE 'enddateo' -NMOD 'nmod' -NOBS 'nobs' -QDESC 'qdesc ' -ODESC 'odesc' -OUTPUT 'output' -SEASON 'season' -PTOP 'plttop

                if( debug = "debug" )
                    say "Hit  ENTER  to repeat plot"
                    say "Type 'next' for  next plot, 'done' for next field"
                    pull flag
                else
                    flag = "next"
                endif
               'c'
               endwhile

               nplot = nplot + 1
               endwhile



* Zonal Mean Closeness Plot (Experiment_vs_Comparison to Verification)
* --------------------------------------------------------------------
       n  = 1
while( n <= numexp )

say 'Checking  plot between  exp: 'qtag
say '                       cexp: 'ctag.n
say '                        obs: 'obsnam
say ''

if( ctag.n != "merra" & ctag.n != "MERRA-2" & ctag.n != "NULL" & type.n != V & ofile.n != "NULL" )

say 'Closeness plot between  exp: 'qtag
say '                       cexp: 'ctag.n
say '                        obs: 'obsnam
say ''

'set dfile 'expfile.n
'set lon  0 360'
'set lat -90 90'
'setz'
'set t 1'
'define cmp = exp'n''season'*'faco

'set dfile 'mfile
'set lon  0 360'
'set lat -90 90'
'set t 1'

'makezdif -q1 mod -q2 obs -file1 'modfile'   -file2 'obsfile' -ptop 'ptop' -name   dif1     '
'makezdif -q1 mod -q2 obs -file1 'modfile'   -file2 'obsfile' -ptop 'ptop' -name  adif1 -abs'
'run getenv "ZDIFILE"'
             zdifile1 = result

'makezdif -q1 cmp -q2 obs -file1 'expfile.n' -file2 'obsfile' -ptop 'ptop' -name   dif2     '
'makezdif -q1 cmp -q2 obs -file1 'expfile.n' -file2 'obsfile' -ptop 'ptop' -name  adif2 -abs'
'run getenv "ZDIFILE"'
             zdifile2 = result

'set dfile 'zdifile1
'setx'
'sety'
'setz'
'set t 1'
'define q1 = q.'zdifile1
'set dfile 'zdifile2
'setx'
'sety'
'setz'
'set t 1'
'define q2 = q.'zdifile2
'makezdif -q1 q1 -q2 q2 -file1 'zdifile1' -file2 'zdifile2' -ptop 'ptop' -name absdif'
'run getenv "ZDIFILE"'
             zdifile3 = result

* Make ZCLOSEPLT
* --------------
                      nplot  = 1
               while( nplot <= nplots )
                      plttop = subwrd( ptops,nplot )

                       flag = ""
               while ( flag = "" )

'run 'geosutil'/plots/zcmp/zcloseplt -EXPID 'expid' -EXPORT 'EXPORT' -ALIAS 'mname' -QFILE 'zdifile1' -CFILE 'zdifile2' -OFILE 'zdifile3' -ONAME 'obsnam' -CNAME 'ctag.n' -OBDATE 'begdateo' -OEDATE 'enddateo' -NMOD 'nmod' -NOBS 'nobs' -QDESC 'qdesc ' -ODESC 'odesc' -OUTPUT 'output' -SEASON 'season' -PTOP 'plttop

                if( debug = "debug" )
                    say "Hit  ENTER  to repeat plot"
                    say "Type 'next' for  next plot, 'done' for next field"
                    pull flag
                else
                    flag = "next"
                endif
               'c'
               endwhile

               nplot = nplot + 1
               endwhile

* End OFILE Check
* ---------------
endif
n = n + 1
endwhile




* End Seasonal Test
* -----------------
endif

* End Seasonal Loop
* -----------------
endwhile

* End check for valid OBS
* -----------------------
endif

* Check next Verification Dataset
* -------------------------------

'undefine obs'num'clim'
'undefine obs'num'jan'
'undefine obs'num'feb'
'undefine obs'num'mar'
'undefine obs'num'apr'
'undefine obs'num'may'
'undefine obs'num'jun'
'undefine obs'num'jul'
'undefine obs'num'aug'
'undefine obs'num'sep'
'undefine obs'num'oct'
'undefine obs'num'nov'
'undefine obs'num'dec'
'undefine obs'num'djf'
'undefine obs'num'jja'
'undefine obs'num'mam'
'undefine obs'num'son'
'undefine obs'num'ann'

num = num + 1
endwhile

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

* Get Next EXP from CMPEXP List
* -----------------------------
function get_cmpexp (cmpexp,num)
      exp  = subwrd(cmpexp,num)
      len = get_length (exp)
      bit = substr(exp,len-1,1)
      if( bit = ":" )
          type = substr(exp,len,1)
          exp  = substr(exp,1,len-2)
      else
          type = M
      endif
return exp' 'type

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

