;+
; NAME:
;     READ_FCHIST
; PURPOSE:
;     Reads history files of frequency calibration parameters
;     determined during analysis of FCAL segments
; CATEGORY:
;     OVRO APC ANALYSIS FCAL
; CALLING SEQUENCE:
;     result = read_fchist(ant,timstr,fc,tc,tim)
; INPUTS:
;     ant        the antenna number, e.g. 1,2,4, etc., for which info
;                  is desired
;     timstr     the TIMSTR structure element from the current TLS
;                  structure, for which to find the closest entry in time
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     result   gives an indication of the success of the attempt to
;                write the updates to the file.  If -1, the attempt
;                failed, 0 otherwise.
;     fc       the table of frequencies, of size FC(9), three for each
;                oscillator.
;     tc       the corresponding table of tuning currents.
;     tim      the day and time of the actual entry selected, as DDDHHMM.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 28-Nov-2000 by Dale E. Gary
;-
function read_fchist,ant,timstr,fc,tc,tim

   ; Determine the year and month corresponding to TIMSTR
   year = strmid(timstr,0,4)
   doy = strmid(timstr,5,3)
   date = cvdoy(year,doy)
   savmo = date[1]

chkfile:
   ; Determine filename of file to look for
   filename = !defaults.dbdir+year+string(date[1],ant,format='(I2.2,"_",I1)')+'.fch'

   ; See if file exists
   file = findfile(filename,count=n)
   if (n eq 0) then begin
      ; File was not found, so see if we can find one a month earlier
      if (date[1] ne savmo) then return,-1
      date[1] = date[1]-1
      if (date[1] eq 0) then begin
         date[0] = date[0]-1
         date[1]=12   ; Back up to December if January
      endif
      goto, chkfile
   endif

   ; Looks like we found an appropriate file for this antenna and month.
   ; Now just read through the file, and look up the closest entry to the input date.
   hed = strarr(3)   ; Three header lines
   openr,/get_lun,lun,file[0]
   readf,lun,hed
   line = ''
   inline = strarr(120)
   data = fltarr(21)
   j = -1
   while (not EOF(lun)) do begin
      readf,lun,line
      j = j + 1
      inline[j] = line  ; Save each line as it is read
      if (strmid(line,0,8) gt strmid(timstr,0,8)) then begin
         ; We have read a later date than the one we are looking for.
         ; Since the dates are guaranteed to be in order, we must be
         ; beyond where we want to be, so back up one.
         if (j gt 0) then begin
            line = inline[j-1]
            ; If the dates do not match, then bail out
            if (strmid(line,0,8) ne strmid(timstr,0,8)) then return,-1

            ; We have a line for this date, so go ahead and return it
            reads,strmid(line,17),data
            fc = data[[0,2,4,7,9,11,14,16,18]]
            tc = data[[1,3,5,8,10,12,15,17,19]]
            tim = strmid(timstr,5,3)+strmid(line,9,2)+strmid(line,12,2)
            return,0
         endif
      endif
   endwhile
   free_lun,lun
   ; This is the last line of the file.  If dates do not match, then bail out
   if (strmid(line,0,8) ne strmid(timstr,0,8)) then return,-1

   ; We have a line for this date, so go ahead and return it
   reads,strmid(line,17),data
   fc = data[[0,2,4,7,9,11,14,16,18]]
   tc = data[[1,3,5,8,10,12,15,17,19]]
   tim = strmid(timstr,5,3)+strmid(line,9,2)+strmid(line,12,2)
return,0
end