;+
; NAME:
;     LASTHREC
; PURPOSE:
;     Returns the record number of the last header in a data file,
;     or if TSTR is set the record number of the next header after
;     the time specified by TSTR.
; CATEGORY:
;     OVRO APC DATA-ANALYSIS
; CALLING SEQUENCE:
;     hrec = lasthrec(a,nrec[,tstr=tstr])
; INPUTS:
;     a       the ASSOC variable associated with the file to
;               examine
;     nrec    the number of records currently in the file
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     tstr    a string of the form [YYYY.DOY ]HH:MM[:SS] giving
;               the time after which the first header record number
;               is to be returned.
; ROUTINES CALLED:
;     get_segments tl_decode
; OUTPUTS:
;     hrec    the record number of the desired header, or -1 if
;               an error occurred or if there are no headers after
;               the time specified.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 11-Jan-1998 by Dale Gary
;-

;----------------------------------------------------------------------
; Internal function to compare two time strings
function t_cmp,yd1,t1,yd2,t2
   reads,yd1,yrda1
   reads,yd2,yrda2
   IF (yrda1 LT yrda2) THEN return,-1
   IF (yrda1 GT yrda2) THEN return,+1
   IF (strlen(t1) EQ 6) THEN t1 = t1+':00.000'
   reads,t1,hh,mm,ss,format='(1X,I2,1X,I2,1X,F6.3)'
   t1l = hh*3600L + mm*60 + ss
   reads,t2,hh,mm,ss,format='(1X,I2,1X,I2,1X,F6.3)'
   t2l = hh*3600L + mm*60 + ss
   IF (t1l LT t2l) THEN return,-1
   IF (t1l GT t2l) THEN return,+1

return,0
end
;----------------------------------------------------------------------


function lasthrec,a,nrec,tstr=tstr

   ; Check whether a time string was given, and if so verify
   ; that it is well-formed.
   IF (keyword_set(tstr)) THEN BEGIN
      IF (strpos(tstr,':') EQ 11) THEN BEGIN
         ; A year and doy were given
         yrday = strmid(tstr,0,8)
         time = strmid(tstr,8,(strlen(tstr)-8))
      ENDIF ELSE IF (strpos(tstr,':') EQ 2) THEN BEGIN
         ; No year was given
         yrday = ' '
         time = ' '+tstr
      ENDIF ELSE return,-1
   ENDIF


   ; Get the list of segments in the file.

   seglist = get_segments(a,nrec)

   ; Isolate the lines that are headers, and extract their rec numbers
   hrec = -1
   FOR i = 0, n_elements(seglist)-1 DO BEGIN
      IF (strpos(seglist(i),'Header') NE -1) THEN BEGIN
         reads,seglist(i),format='(13x,i5)',h
         hrec = [hrec,h]
      ENDIF
   ENDFOR

   nhed = n_elements(hrec)

   ; Error return if there were no headers (only one element in hrec)
   IF (nhed EQ 1) THEN return,-1

   IF (keyword_set(tstr)) THEN BEGIN

      ; A time string was given so get the time for each header
      ; and compare it with the given time.
      FOR i = 1, nhed-1 DO BEGIN

         ; Get time/label structure for header hrec(i)
         tls = tl_decode(a(hrec(i)))

         ; If the original time string did not contain
         ; the year and day of year, set them to the value
         ; in the first header of the file
         IF (yrday EQ ' ') THEN yrday = tls.yrday

         ; Compare the time given in tstr with the header time
         ; and return -1 if tstr < thead, 0 if tstr = thead,
         ; and 1 if tstr > thead.  We will use the record number
         ; of the header if t_cmp returns 0 or -1.
         IF (t_cmp(yrday,time,tls.yrday,tls.timstr) LE 0) THEN $
            return,hrec(i)
      ENDFOR

      ; No header time was after the time given in tstr
      return, -1

   ENDIF ELSE BEGIN
      ; No time string, so return the record number of the last
      ; header in the file
      hrec = hrec(nhed-1)
   ENDELSE


return,hrec
end
