************************************************************************
*
*  To use:  "grads -l"
*
*  Script finds min/max values of variables
*
************************************************************************
function var_min_max

'reinit'

*-----------------------------------------------------------------------
*  Batch mode?
*-----------------------------------------------------------------------

batch_mode = 'no'

*-----------------------------------------------------------------------
*  Specify GrADS ctl file
*-----------------------------------------------------------------------

ctl_file = '/archive/cjs/zetac/KWAJEX-ndzmax=1/history/SKEWT.06:30z24Jul1999_1.ctl'

*-----------------------------------------------------------------------
*  Specify names for min/max variables
*-----------------------------------------------------------------------

var_name = 'pp ta td wspd'

*-----------------------------------------------------------------------
*  Specify time level number(s)...
*-----------------------------------------------------------------------

tlev = '1 180'

*-----------------------------------------------------------------------
*  Allow external GrADS functions
*-----------------------------------------------------------------------

rc = gsfallow("on")

*-----------------------------------------------------------------------
*  Open GrADS ctl file...
*-----------------------------------------------------------------------

'open 'ctl_file

*-----------------------------------------------------------------------
*  Define GrADS dataset file information...
*-----------------------------------------------------------------------

'q file'
rec5 = sublin(result,5)
nx = subwrd(rec5,3) ; ny = subwrd(rec5,6) ; nz = subwrd(rec5,9) ; nt = subwrd(rec5,12)

*-----------------------------------------------------------------------
*  Find min/max values...
*-----------------------------------------------------------------------

nv=1 ; var = subwrd(var_name,nv)
while ( var != '' )
  say
  say 'Variable: 'var
  say
  gmin = 1e6 ; gmax = -1e6
  n=1 ; tt = subwrd(tlev,n)
  while ( tt != '' )
    zz=1
    while ( zz <= nz )
      'set x 1 'nx
      'set y 1 'ny
      'set z 'zz
      'set t 'tt
      'set gxout stat'
      'd 'var
      min_max = sublin(result,8)
      min = subwrd(min_max,4)
      max = subwrd(min_max,5)
*      say ' zz = 'zz',  'min_max
*      say '    min = 'min ',   max = 'max
      if( min < gmin )
        say '  zz, min = 'zz', 'min
        gmin = min
        zzmin = zz ; ttmin = tt
      endif
      if( max > gmax )
        say '  zz, max = 'zz', 'max
        gmax = max
        zzmax = zz ; ttmax = tt
      endif
      zz=zz+1
    endwhile
    say ' tt = 'tt':    gmin = 'gmin ',   gmax = 'gmax
    pause()
    n=n+1 ; tt = subwrd(tlev,n)
  endwhile
  say 'Variable: 'var
  say
  say '  gmin = 'gmin ' at (zz,tt) = ('zzmin','ttmin')'
  say '  gmax = 'gmax ' at (zz,tt) = ('zzmax','ttmax')'
  pause()
  nv=nv+1 ; var = subwrd(var_name,nv)
endwhile

say
say '************************************************'
say ' Finished with this script.'
say '************************************************'
say

if( batch_mode = 'yes' )
  quit
endif

return
