function qbo (args)

EXPORT   = subwrd(args,1)
GC       = subwrd(args,2)

expid    = subwrd(args,3)
output   = subwrd(args,4)
debug    = subwrd(args,5)


* Initialize
* ----------
'reinit'
'set display color white'
'rgbset'
'set csmooth on'
'set clab off'
'c'


* Determine Variable Name and Location
* ------------------------------------
'run getvar 'EXPORT' 'GC
         mname  = subwrd(result,1)
         mfile  = subwrd(result,2)
         scale  = subwrd(result,3)
         expdsc = subwrd(result,4)

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

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

'run getenv "ANALYSIS"'
         analysis = result


* Model Experiment Data
* ---------------------
'set dfile 'mfile
'setdates'
'run getenv "BEGDATE"'
             begdate  = result
'run getenv "ENDDATE"'
             enddate  = result

'setlons'
'sety'
'setz'
'sett'

'getinfo tmin'
         tmin = result
'getinfo tmax'
         tmax = result

'set  t 'tmin
'getinfo month'
         begmonth = getmon(result)
'getinfo year'
         begyear  = result

'set  t 'tmax
'getinfo month'
         endmonth = getmon(result)
'getinfo year'
         endyear  = result

mbegdate = begyear''begmonth
menddate = endyear''endmonth

'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
'makezf 'malias' umod z'
'set x 1'
'set y 1'
'define umodzg = ave( umodz,lat=-10,lat=10 )'
 
*******************************************************************
****   Loop over Possible Experiment Datasets for Comparison   ****
*******************************************************************

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

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

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

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

facm = 1
faco = 1

'!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'

'run getvar 'EXPORT' 'GC' 'exp
         oname  = subwrd(result,1)
         ofile  = subwrd(result,2)
        oscale  = subwrd(result,3)
         obsdsc = subwrd(result,4)
         obsnam = subwrd(result,5)

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

'set dfile 'ofile
    'getdates'
    'getinfo   tmin'
               tmin = result
    'getinfo   tmax'
               tmax = result

    'set t    'tmin
    'getinfo   month'
            begmonth = getmon(result)
    'getinfo   year'
            begyear  = result
    'getinfo   date'
               begdateo = result

    'set t    'tmax
    'getinfo   month'
           endmonth = getmon(result)
    'getinfo   year'
            endyear  = result
    'getinfo   date'
               enddateo = result
    'set time 'begdateo' 'enddateo


obegdate = begyear''begmonth
oenddate = endyear''endmonth
say 'Mbegdate: 'mbegdate
say 'Menddate: 'menddate
say 'Obegdate: 'obegdate
say 'Oenddate: 'oenddate

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


                     ' alias ' oname
                      oalias = result
'chckname            'oalias
'makezf 'oalias' uobs z'
'set x 1'
'set y 1'
'define uobszg = ave( uobsz,lat=-10,lat=10 )'
 

* Make PLT
* --------
                       flag = ""
               while ( flag = "" )

'set dfile 'mfile
'getdates'
    'getinfo tmin'
             tmin = result
    'getinfo tmax'
             tmax = result
    'set t  'tmin
    'getinfo date'
             begtime = result
    'set t  'tmax
    'getinfo date'
             endtime = result

if( obegdate < mbegdate )
    'set dfile 'ofile
    'getdates'
    'getinfo tmin'
             tmin = result
    'set t  'tmin
    'getinfo date'
             begtime = result
endif
if( oenddate > menddate )
    'set dfile 'ofile
    'getdates'
    'getinfo   tmax'
               tmax = result
    'set t  'tmax
    'getinfo date'
             endtime = result
endif

    'set time 'begtime' 'endtime
say 'set time 'begtime' 'endtime

'run 'geosutil'/plots/zcmp1/qbo.plt umodzg uobszg 'expdsc' 'obsdsc' 'obsnam' 'output

                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
               endwhile
              'c'

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

* Check next CMPEXP Dataset
* -------------------------
    num = num + 1
  dummy = get_cmpexp (cmpexp,num)
    exp = subwrd(dummy,1)
   type = subwrd(dummy,2)

endwhile
'!/bin/mv HISTORY.Tmp HISTORY.T'

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

facm = 1
faco = 1

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

' getnumrc 'geosutil'/plots/zcmp1'
     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)
              oscale = subwrd(result,3)
              obsdsc = 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'
    'getinfo   tmin'
               tmin = result
    'getinfo   tmax'
               tmax = result

    'set t    'tmin
    'getinfo   month'
            begmonth = getmon(result)
    'getinfo   year'
            begyear  = result
    'getinfo   date'
               begdateo = result

    'set t    'tmax
    'getinfo   month'
           endmonth = getmon(result)
    'getinfo   year'
            endyear  = result
    'getinfo   date'
               enddateo = result
    'set time 'begdateo' 'enddateo


obegdate = begyear''begmonth
oenddate = endyear''endmonth
say 'Mbegdate: 'mbegdate
say 'Menddate: 'menddate
say 'Obegdate: 'obegdate
say 'Oenddate: 'oenddate

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


                     ' alias ' oname
                      oalias = result
'chckname            'oalias
'makezf 'oalias' uobs z'
'set x 1'
'set y 1'
'define uobszg = ave( uobsz,lat=-10,lat=10 )'
 

* Make PLT
* --------
                       flag = ""
               while ( flag = "" )

'set dfile 'mfile
'getdates'
    'getinfo tmin'
             tmin = result
    'getinfo tmax'
             tmax = result
    'set t  'tmin
    'getinfo date'
             begtime = result
    'set t  'tmax
    'getinfo date'
             endtime = result

if( obegdate < mbegdate )
    'set dfile 'ofile
    'getdates'
    'getinfo tmin'
             tmin = result
    'set t  'tmin
    'getinfo date'
             begtime = result
endif
if( oenddate > menddate )
    'set dfile 'ofile
    'getdates'
    'getinfo   tmax'
               tmax = result
    'set t  'tmax
    'getinfo date'
             endtime = result
endif

    'set time 'begtime' 'endtime
say 'set time 'begtime' 'endtime

'run 'geosutil'/plots/zcmp1/qbo.plt umodzg uobszg 'expdsc' 'obsdsc' 'obsnam' 'output

                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
               endwhile
              'c'

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

* Check next Verification Dataset
* -------------------------------
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

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

* 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

