;+
; NAME:
;     WRITE_FCHIST
; PURPOSE:
;     Writes a history file of frequency calibration update parameters
;     determined during analysis of MONITOR segments (FCAL_UPDATE)
; CATEGORY:
;     OVRO APC ANALYSIS FCAL
; CALLING SEQUENCE:
;     result = write_fchist(VF_tab,tls)
; INPUTS:
;     VF_tab     the update table of tuning current changes corresponding
;                  to the three oscillators of each antenna
;     tls        a standard time/label structure from the scan header
;                  segment record of the scan.
; 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.
; COMMENTS:
;     The data are written to the standard files, yyyymm.UPD, where yyyy
;     is the 4-digit year, mm is the 2-digit month.  If a file does not
;     exist, it is created.  If the file exists, the new entries are
;     appended to the file.
; SIDE EFFECTS:
;     Data are written to standard files.
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 18-Aug-1999 by Dale E. Gary
;     23-May-2000  DG
;       Modified to work with 6 antennas.
;-
function write_fchist,VF_tab,tls

   ; Set error return on IO error
   ON_IOERROR,bail

   ; Get the number of antennas from the size of the VF_TAB array
   nant = n_elements(VF_tab(0,*))
   nosc = n_elements(VF_tab(*,0))

   ; Strip out year and doy from yrday string, to use as arguments to
   ; CVDOY routine.  CVDOY returns a three element array with year, month,
   ; and day, all numeric.
   year = strmid(tls.yrday,0,4)
   doy = strmid(tls.yrday,5,3)
   datestr = cvdoy(year,doy)

   ; Read the time/label string time data into numerical variables
   reads,tls.yrday+tls.timstr,yr,day,hr,mn,format='(I4,1X,I3,1X,I2,1X,I2)'

   ; Create the output line.  Line starts with date.  NB: This has to be
   ; changed to reflect a different number of antennas--I am not sure how to
   ; specify a variable length format statement in IDL.
   newline = tls.yrday+strmid(tls.timstr,0,9)+string(VF_tab,format='(18I3)')
   ; Create filename from elements
   filename = !defaults.dbdir + $
           string(datestr(0),datestr(1),format='(I4,I2.2)') + '.UPD'

   ; See if the filename already exists and act accordingly
   f = findfile(filename,count=n)
   if (n ne 0) then begin
      ; File exists.  Open it for updating.
      openu,lun,/get_lun,filename,/append
      printf,lun,newline

   endif else begin

      ; File does not already exist, so just open a new file and write
      ; the entry to it.
      openw,lun,/get_lun,filename

      ; Write out the header lines
      hline1 = '--Date-- --Time-- --Ant1-- --Ant2-- --Ant4-- --Ant5-- --Ant6-- --Ant7--'
      hline2 = 'YYYY.DOY HH:MM:SS Lo Mi Hi Lo Mi Hi Lo Mi Hi Lo Mi Hi Lo Mi Hi Lo Mi Hi'
      hline3 = '-------- -------- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --'
      printf,lun,hline1
      printf,lun,hline2
      printf,lun,hline3
      printf,lun,newline
   endelse
   free_lun,lun
   return,0

bail:
   return,-1
end