;+
;  Name:
;       RADEC_FILL
; radec_fill looks for gaps in the  pointing data (value is 0 in gaps).
; (There are gaps in the pointing data when the data was sent using the
; omni directional antenna and a compressed data format was used.)
; If there was any good data passed into radec_fill, it will perform a
; linear fit to determine the values to insert into the arrays to fill the
; gaps.  It then saves the latest pointing data and time in radec_save
; in common radec_com.  The next time it is called, if there is no good 
; pointing data in the entire passed interval, it will insert the values
; that were stored in radec_save in the xra,xdec,zra,zdec arrays if the
; the current time is within 12 hours of the time of the saved pointing.
; NOTE:  the sec array passed into radec_fill contains only every other
;        point from the original seconds array, so there is a one-to-one
;        correspondence between elements in sec and xra,xdec,zra,zdec.
;
;Similar to qlradec_fill which is used in qlarchive.  Difference is that 
; pointing arrays are passed in in separate arrays here (can't pass in 
; just the subarrays because then can't change the values), and here we look
; to see if radec_save pointing values were within 12 hours of current time
; before restoring (if current interval doesn't have any good data).
;
; K. Tolbert 3/93
; Mod. 6/2/93 by KT. One use elements that have real times.  Gaps in data
;  will have times of .512 and zero for everything else.  Don't want to 
;  use them.
;
;-----------------------------------------------------------------------

pro radec_fill, sec, xra, xdec, zra, zdec, maxind, error=error

common radec_com, radec_save

error = 0
; Find elements with no pointing data but with a time.
qbad = where (sec(0:maxind) gt 1 and $
              xra(0:maxind) eq 0 and xdec(0:maxind) eq 0 and $
              zra(0:maxind) eq 0, kbad)

; If there weren't any missing data, just save pointing data for next call.
if kbad eq 0 then begin
   n = maxind
   radec_save = [xra(n), xdec(n), zra(n), zdec(n), sec(n)]
   ;print,'#, radec_save', kbad,radec_save
   goto, getout
endif

; If there is no good data in entire arrays, then, if we had saved some
; good pointing data that was within 12 hours of current time, then
; fill arrays with those values.  Otherwise, error return.
qgood = where (sec(0:maxind) ne 0 and $
               (xra(0:maxind) ne 0 or xdec(0:maxind) ne 0 or $
               zra(0:maxind) ne 0), kgood)
if kgood lt 3 then begin
   error = 1
   if n_elements(radec_save) gt 0 then begin
      if (abs(sec(0) - radec_save(4)) lt 43200.d0) then begin
         xra(0:maxind) = radec_save(0)
         xdec(0:maxind) = radec_save(1)
         zra(0:maxind) = radec_save(2)
         zdec(0:maxind) = radec_save(3)
         print,'No pointing data available in requested interval.'
         print,'Using pointing data from a previous data accumulation.'
         error = 0
      endif
   endif
   goto, getout
endif

; Get here, if part of the arrays are good, part bad.  Use poly_fit to
; do a linear fit (good enough) on the good data to fill in the missing data.
if kbad gt 0 then begin

   if kbad eq 1 then qbad = qbad(0)

   c = poly_fit(sec(qgood)-sec(0), xra(qgood), 1)
   xra(qbad) = (sec(qbad)-sec(0))*c(1) + c(0)

   c = poly_fit(sec(qgood)-sec(0), xdec(qgood), 1)
   xdec(qbad) = (sec(qbad)-sec(0))*c(1) + c(0)

   c = poly_fit(sec(qgood)-sec(0), zra(qgood), 1)
   zra(qbad) = (sec(qbad)-sec(0))*c(1) + c(0)

   c = poly_fit(sec(qgood)-sec(0), zdec(qgood), 1)
   zdec(qbad) = (sec(qbad)-sec(0))*c(1) + c(0)

   n = maxind
   radec_save = [xra(n), xdec(n), zra(n), zdec(n), sec(n)]
   ;print,'#, radec_save', kbad,radec_save
endif

getout:
return&end
