;+
; NAME:
;     READ_TPFAC
; PURPOSE:
;     Reads a total power calibration file (requests selection by the user)
;     and returns the total power factors (units/SFU) corresponding to the
;     frequencies specified on input.
; CATEGORY:
;     OVRO APC DATA CALIBRATION
; CALLING SEQUENCE:
;     tpfac = read_tpfac(frqlist)
; INPUTS:
;     frqlist    an array of frequencies [GHz] for which to return the
;                  factors
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     tpfac      an array of size FLTARR(4,n_elements(frqlist)) containing
;                  the factors, one each for the four 27-m feeds.  The
;                  order of feeds is I1, L1, R2, I2
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 07-Feb-1999 by Dale E. Gary
;-

;--------------------------------------------------------------------------

FUNCTION read_tpfac,flist

   ; Read total power calibration factors
   ; Quick and dirty code here--needs cleaning up.
   tpfile = pickfile(path=!defaults.dbdir,filter='*.tpc',title='Choose a Total power calibration file.')
   openr,tplun,/get_lun,tpfile
   hedline = strarr(3)
   line = ''
   tpdat = fltarr(12)
   ndavg = (tprms = (tpfac = fltarr(4,n_elements(flist))))
   readf,tplun,hedline
   while (not EOF(tplun)) do begin
      readf,tplun,f,tpdat
      j = where(string(flist,format='(f4.1)') eq string(f,format='(f4.1)'))
      if (j(0) ne -1) then begin
         tpfac(*,j) = tpdat([0,3,6,9])
         tprms(*,j) = tpdat([1,4,7,10])
         ndavg(*,j) = tpdat([2,5,8,11])
      endif
   endwhile
   free_lun,tplun

   ; At this stage, should read in the latest gain file and get noise diode info,
   ; then scale the total power calibration factors by the noise diode.

return,tpfac
end
