;+
; NAME:
;     DOTIMEAVG
; PURPOSE:
;     Performs time averaging of data for SOLAID
; CATEGORY:
;     OVRO APC SOLAID
; CALLING SEQUENCE:
;     result = dotimeavg(tavg,time,amparr[,phzarr])
; INPUTS:
;     tavg      The averaging time, in seconds
;     time      On input, the array of times corresponding to the data
;     amparr    On input, the array of amplitudes or total power values
;                 corresponding to the times in array TIME
;     phzarr    On input, the array of phases corresponding to the times
;                 in array TIME.  If omitted, AMPARR is interpreted as an
;                 array of total power values
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     poly_2d, juldate, pandb
; OUTPUTS:
;     time      On output, the array of times derived from averaging TIME
;                 by TAVG.
;     amparr    On output, the array of amplitudes or total powers derived
;                 from averaging array AMPARR by TAVG.
;     phzarr    On output, the array of phases derived from averaging array
;                 PHZARR by TAVG.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 28-Jun-2000 by Dale E. Gary  (derived from earlier, buggy version)
;-
FUNCTION dotimeavg,navg,time,amparr,phzarr

   ; No sample averaging to be done
   IF (navg EQ 1) THEN return,0
   nt = n_elements(time)
   if (nt le 2) then return,0

   ; Get total number of points that should be returned, as total number of points
   ; divided by NAVG (integer value)
   npts = nt/navg
   if (npts lt 2) then return,0         ; If too few points, nothing to plot

   ; Number of points that will go evenly into the average
   n2avg = npts*navg
   ; Will use REBIN to average, e.g. REBIN(TIME[0:n2avg-1],npts)

   ; Create parallel flag array, with zeroes where amparr is not finite
   flag = float(finite(amparr))
   ; Set non-finite (bad) values to zero in amparr and phzarr
   bad = where(flag eq 0,nbad)
   if (nbad ne 0) then begin
      amparr[bad] = 0
      if (n_elements(phzarr) ne 0) then phzarr[bad] = 0
   endif

   ; Use REBIN to perform average of flag array (note: FLAG must be a FLOAT!)
   flgout = rebin(flag[0:n2avg-1],npts)
   ; Find where all of the data were missing
   bad = where(flgout eq 0,nbad)

   ; See if phase array is undefined or has only one element.  If so, this is total power data
   ; and a straight average is called for
   IF (n_elements(phzarr) LE 1) THEN BEGIN

      ; Use REBIN to perform average of amplitude array
      ampout = rebin(amparr[0:n2avg-1],npts)

      ; The FLGOUT array will normally be all ones, but when there are
      ; missing data it will be less than 1 in the same proportion as
      ; decrease in ampout.  Dividing by FLGOUT will correct for these
      ; missing data.  Note when FLGOUT = 0, AMPOUT = 0 also.
      ampout = ampout/(flgout>(1./navg))

      ; Restore NaNs where all of the data were missing
      if (nbad gt 0) then ampout[bad] = !values.f_nan

      amparr = ampout
   ENDIF ELSE BEGIN
      ; Convert to sin and cos components for vector averaging
      x = amparr*cos(phzarr*!dtor)
      y = amparr*sin(phzarr*!dtor)

      ; Use REBIN to perform average of x and y arrays
      xout = rebin(x[0:n2avg-1],npts)
      yout = rebin(y[0:n2avg-1],npts)

      ; The FLGOUT array will normally be all ones, but when there are
      ; missing data it will be less than 1 in the same proportion as
      ; decrease in ampout.  Dividing by FLGOUT will correct for these
      ; missing data.  Note when FLGOUT = 0, AMPOUT = 0 also.
      xout = xout/(flgout>(1./navg))
      yout = yout/(flgout>(1./navg))

      ; Form back to amplitude and phase
      ampout = sqrt(xout^2 + yout^2)
      phzout = (atan(yout,xout)/!dtor + 360.) mod 360.

      ; Restore NaNs where all of the data were missing
      if (nbad gt 0) then begin
         ampout[bad] = !values.f_nan
         phzout[bad] = !values.f_nan
      endif

      amparr = ampout
      phzarr = phzout

   ENDELSE
   ; Average the time in the same way
   timout = rebin(time[0:n2avg-1],npts)
   time = timout
return,0
end
