**************************************************************
*                                                            *
* This script computes the Time Average of the INPUT field   *
*                                                            *
**************************************************************
 
function timeave (args)
name   = subwrd(args,1)
file   = subwrd(args,2)
offset = subwrd(args,3)

if( offset="" )
offset = 0
endif

'set t 1'
tmax = gettmax()

xdim = getinfo(xdim)
ydim = getinfo(ydim)
'set x 1 'xdim
'set y 1 'ydim

if( xdim>1 )
'set lon 0 360'
endif


say " "
say "Processing Time Average for: "name
say "t_max = "tmax"  offset = "offset
say " "

t1 = 1  + offset

'define 'name'ave'file' = ave('name',t='t1',t='tmax')'
say " "
say "Defined "name"ave"file



  function gettmax(args)
  'query file '
  tmax  = sublin(result,5)
  tmax  = subwrd(tmax,12)
  return tmax

  function gettbeg(args)
  month = getinfo(month)
  if(month="JAN")
  tbeg = 1
  endif
  if(month="FEB")
  tbeg = 2
  endif
  if(month="MAR")
  tbeg = 3
  endif
  if(month="APR")
  tbeg = 4
  endif
  if(month="MAY")
  tbeg = 5
  endif
  if(month="JUN")
  tbeg = 6
  endif
  if(month="JUL")
  tbeg = 7
  endif
  if(month="AUG")
  tbeg = 8
  endif
  if(month="SEP")
  tbeg = 9
  endif
  if(month="OCT")
  tbeg = 10
  endif
  if(month="NOV")
  tbeg = 11
  endif
  if(month="DEC")
  tbeg = 12
  endif
  return tbeg


  function getinfo (args) 

  'query file'
  xlab       = sublin(result,1)
  file       = subwrd(xlab,2)
  label      = substr( xlab,10,80 )
  tabl_file  = sublin(result,2)
  tabl_file  = substr( tabl_file,14,80)
  data_file  = sublin(result,3)
  data_file  = substr(data_file,11,80)
  resolution = sublin(result,5)
  xdim       = subwrd(resolution,3)
  ydim       = subwrd(resolution,6)
  zdim       = subwrd(resolution,9)
  tdim       = subwrd(resolution,12)


  'q dims'
  lons  = sublin(result,2)
  lons  = substr(lons,16,16)
  lats  = sublin(result,3)
  lats  = substr(lats,16,16)
  levl  = sublin(result,4)
  levl  = substr(levl,21,4)
  time  = sublin(result,5)
  time  = substr(time,22,15)
  month = substr(time,7,3)

  
 if (args="file")
 return file
 endif
 if (args="label")
 return label
 endif
 if (args="time")
 return time
 endif
 if (args="xdim")
 return xdim
 endif
 if (args="ydim")
 return ydim
 endif
 if (args="zdim")
 return zdim
 endif
 if (args="tdim")
 return tdim
 endif
 if (args="month")
 return month
 endif

