function makezf (args)

name  = subwrd(args,1)
alias = subwrd(args,2)
tag   = subwrd(args,3)

* Variable is in File
* -------------------
    say 'Computing Zonal Mean for Function: 'name
    say '                             Name: 'alias
    say '                              Tag: 'tag

'getinfo file'
       infile = result

'q gxout'
   gxout = sublin(result,4)
   gxout = subwrd(gxout ,6)

'run getenv "GEOSUTIL"'
             geosutil = result

'getinfo lonmin'
         lonmin = result
'getinfo lonmax'
         lonmax = result

'getinfo latmin'
         latmin = result
'getinfo latmax'
         latmax = result

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

'set t 'tmin
'getinfo date'
      begdate = result

'getinfo zmin'
         zmin = result
'getinfo zmax'
         zmax = result
'set z  'zmin' 'zmax
         zdim = zmax-zmin+1

'sety'
'set t 'tmin' 'tmax
        tdim = tmax-tmin+1

'getinfo ydim'
         ydim = result

'getinfo undef'
         undef = result
'getinfo dlat' 
         dlat  = result
'getinfo dlon' 
         dlon  = result
       lontot  = lonmax - lonmin + dlon/2

* Get INFO from CTL
* -----------------
'q ctlinfo'
         n = 1
      while( n >0 )
             line = sublin(result,n)
             word = subwrd(line,1)
         if( word = 'tdef' )
             dt = subwrd(line,5)
             n  = 0
          else
             n  = n + 1
          endif
      endwhile
         n = 1
      while( n >0 )
             line = sublin(result,n)
             word = subwrd(line,1)
         if( word = 'ydef' )
           lat0 = subwrd(line,4)
             n  = 0
          else
             n  = n + 1
          endif
      endwhile
         n = 1
      while( n >0 )
             line = sublin(result,n)
             word = subwrd(line,1)
         if( word = 'xdef' )
           lon0 = subwrd(line,4)
             n  = 0
          else
             n  = n + 1
          endif
      endwhile
* -----------------

      levs = ''
             z  = zmin
      while( z <= zmax )
            'set z 'z
            'getinfo level'
             levs = levs' 'result
             z = z + 1
      endwhile
       
    say ''
    say 'DFILE: 'infile
    say 'UNDEF: 'undef
    say ' LON0: 'lon0
    say ' DLON: 'dlon
    say ' YDEF: 'ydim
    say ' LAT0: 'lat0
    say ' DLAT: 'dlat
    say ' ZDEF: 'zdim
    say ' TDEF: 'tdim
    say 'BDATE: 'begdate
    say '   DT: 'dt
    say ' LEVS: 'levs
    say ''
    say ' LONMIN:LONMAX = 'lonmin':'lonmax
    say ''

  '!remove sedfile'
  '!touch  sedfile'
  '!remove 'alias''tag'.ddf'

    t = tmin
    while( t<=tmax )
    say '          Computing Zonal Mean Variable: 'alias''tag' for t = 't
   'set t 't
   'sety'
   'set z 'zmin' 'zmax
   'set lon 'lonmin' 'lonmax
   'define temp = 'name
   'set x 1'
    if( lontot > 360 ) 
*   say 'define tempz = ave(temp,lon='lonmin',lon='lonmax',-b)'
        'define tempz = ave(temp,lon='lonmin',lon='lonmax',-b)'
    else
*   say 'define tempz = ave(temp,lon='lonmin',lon='lonmax')'
        'define tempz = ave(temp,lon='lonmin',lon='lonmax')'
    endif
   'getinfo year'
            year  = result
   'getinfo month'
            month = result
            month = getnum(month)

   'getinfo tunit'
            tunit = result
   if( tunit = 'day' | tunit = 'hour' )
    'getinfo date'
             date = result
                   delim = substr( date,3,1 )
       'uppercase 'delim
                   delim = result
       if( delim = 'Z' )
            hour = substr( date,1,2 )
             day = substr( date,4,2 )
           fname = alias''tag'.'year''month''day'_'hour'z.nc4'
          tstamp = '%y4%m2%d2_%h2z'
       endif
       if( delim = ':' )
            hour = substr( date,1,2 )
          minute = substr( date,4,2 )
             day = substr( date,7,2 )
           fname = alias''tag'.'year''month''day'_'hour''minute'z.nc4'
          tstamp = '%y4%m2%d2_%h2%n2z'
       endif
   else
           fname = alias''tag'.'year''month'.nc4'
          tstamp = '%y4%m2'
   endif

   'set sdfwrite -5d 'fname
   'set undef 'undef
   'sdfwrite  tempz'
    t = t+1
    endwhile
    say ''
   'undefine temp'
   'undefine tempz'
   'set gxout 'gxout

'!echo "s?@DT?"'dt'?g >> sedfile'
'!echo "s?@TDIM?"'tdim'?g >> sedfile'
'!echo "s?@BDATE?"'begdate'?g >> sedfile'
'!echo "s?qz.data?"'alias''tag'.'tstamp'.nc4?g >> sedfile'
'!/bin/cp 'geosutil'/plots/grads_util/makez.tmpl .'
'!sed -f   sedfile makez.tmpl > 'alias''tag'.ddf'

   'xdfopen 'alias''tag'.ddf'
   'getinfo    numfiles'
               newfile = result
   'set dfile 'newfile
   'set lon 'lon0
   'sety'
   'setz'
   'getinfo  tdim'
             tdim = result
   'set t 1 'tdim
   'define 'alias''tag' = tempz.'newfile
   'close 'newfile

   'set dfile 'infile
   'set lat 'latmin' 'latmax
   'set lon 'lonmin' 'lonmax
   'set t   '  tmin' '  tmax

return

function getnum(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

