;+
; NAME:
;     READ_SRBL
; PURPOSE:
;     Read a SRBL lightcurve file and return the frequency, time, and
;     dynamic spectrum data for a given time range
; CATEGORY:
;     OVRO APC DATA CALIBRATION
; CALLING SEQUENCE:
;     read_srbl,filename,tstart,tend,freq,time,data
; INPUTS:
;     filename   the name of the SRBL plot file
;     tstart     the start time of the desired data, in seconds since 0 UT
;     tend       the end time of the desired data, in seconds since 0 UT
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     freq       an array of frequencies, in GHz, of size NF
;     time       an array of times, in seconds, of size NTIMES
;     data       the array of data, of size (NF,NTIMES), in SFU.  Note that
;                  the OVRO SRBL takes only RCP data.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 22-Aug-1999 by Dale Gary
;     07-Nov-2000  DG
;       Added FILENAME argument to make routine more general.
;     29-Apr-2001  DG
;       Added code to allow reading of original (BST) and new versions (PTLF)
;       of SRBL data.
;-
pro read_srbl,filename,tstart,tend,freq,time,data
   ; Open the SRBL plot file
   openr,/get_lun,lun,filename

   if (strpos(filename,'ptlf') ne -1) then begin
      readf,lun,date
      nf = 120
      freq = fltarr(nf)
      readf,lun,freq
   endif else if (strpos(filename,'bst') ne -1) then begin
      ; Declare dummy strings
      hed1 = strarr(5)
      hed2 = strarr(4)

      ; Skip 5 lines
      readf,lun,hed1

      ; Read number of frequencies, NF
      readf,lun,nf

      ; Read frequency strings
      colstr = strarr(nf)
      readf,lun,colstr,format='(6a10)'
      nf = nf-1

      ; Read frequencies (first is a dummy placeholder for the time)
      freq = fltarr(nf)
      readf,lun,dummy,freq

      ; Skip 4 lines
      readf,lun,hed2
   endif else begin
      print,'READ_SRBL: Unknown file type, based on filename.'
      return
   endelse

   freq = freq/1000.   ; Convert frequency from MHz to GHz.

   ; Declare storage.  The value 9.6 is the interval between points in SRBL data
   ntimes = (tend - tstart)/9.6 + 1
   dat = fltarr(nf)
   time = fltarr(ntimes)
   data = fltarr(nf,ntimes)
   i = -1
   t=0L

   ; Read the data
   while (not EOF(lun)) do begin
      readf,lun,t,dat
      if (t gt tend) then goto,done
      if (t ge tstart) then begin
         i = i + 1
         time[i] = t
         data[*,i] = dat
      endif
   endwhile

done:
   ; Truncate to actual size
   if (i ge 0) then begin
      time = time[0:i]
      data = data[*,0:i]
   endif else begin
      print,'READ_SRBL: No data read!'
      time = 0
      data = 0
   endelse

   free_lun,lun

   ; The BST form of the data file has both flux and rms, so eliminate
   ; the latter by truncating to the first half of the data arrays.
   if (strpos(filename,'bst') ne -1) then begin
      nfreq = n_elements(freq)/2
      freq = freq[0:nfreq-1]
      data = data[0:nfreq-1,*]
   endif

return
end