PRO xrt_telemetry_metrics, year=year, month=month, $
        alloc_vol=alloc_vol, planned_vol=planned_vol, $
        actual_vol=actual_vol, st_str1=st_str1, en_str1=en_str1, report=report,$
        exclude = exclude, nbad_index=nbad_index                   

;----------------------------------------------------------------------
;+
;
; NAME: xrt_telemetry_metrics
;       (telemetry metric: allocated minus actual)
;       (telemetry metric: planned minus actual)
;
; EXAMPLE:
;   IDL> xrt_telemetry_metrics, alloc_vol=alloc_vol, planned_vol=planned_vol, $
;   IDL>   actual_vol=actual_vol, st_str1=st_str1, en_str1=en_str1
;
; 2009/03/23 KKR 
;      Changed to read files from exported directory instead of saved
;      dir
;
; 2009/04/15 KKR
;      Fixed bug where str4b variable didn't have '/' between
;      month and day in path.  Caused any timelines with op periods
;      spanning multiple months to be counted low on actual volume.
;
; 2009/07/15 KKR
;      Fixed Planned Volume calculation for the case where the OP_STOP
;      time is not included at the end of the dr_XRT*.txt file.
;
; 2010/01/13 KKR
;      Added a switch to check for bitspp keyword in 64x64 pixel
;      files.  Added nbad_index keyword which reports number of these
;      files without bitspp keyword.
;
; 2011/11/23 KKR - Added '.txt' to definintion of str2 on line 101 to
;                  keep misnamed dr*.png files from showing up.
;-
;----------------------------------------------------------------------


;; Check keywords.
  if (keyword_set(month) xor keyword_set(year)) then begin
    print, "Specify both YEAR and MONTH, or neither."
    return
  endif

; ========================

;; Get current date. Assume we are reporting on previous month if
;; not specified. Get reporting month. Get reporting year. 
;; Watch out for rollovers.
  if not keyword_set(year) then begin
    timestamp = anytim2ex(systim())
    month = ((timestamp[5] - 2 + 12) mod 12) + 1
    if (month eq 12) then year = timestamp[6] - 1 $
                     else year = timestamp[6]
  endif

; ========================

;; Figure out the ALLOCATED time periods from the "op_period" files.

;; Figure out filenames.
  str1 = '/archive/hinode/xrt/timelines/' + strcompress(year,/rem) + $
         '/' + string(month,format='(I02)') + '/*/*exported/op_period*'
  files1 = file_search(str1, count=fcount1)

  if keyword_set(exclude) then files1 = files1[0:(fcount1-1)-exclude] $
  else exclude = 0

;; Loop over files.
  st_str1 = strarr(fcount1)
  en_str1 = strarr(fcount1)

  for ff = 0,(fcount1-1-exclude) do begin
    read_seqfile, lines1, files1[ff]

;; Get START times.
    op_start = lines1(where(strmatch(lines1, 'OP_START*') eq 1))
    st_str1[ff] = strmid(op_start, 11, 10) + ' ' $
                  + strmid(lines1[0], 22, 8)

;; Get END times.
;; If more than one OPPLN_PERMIT, take latest one
;; SUNRISE balloon timelines had two OPPLN_PERMIT commands pre-launch
    match = where(strmatch(lines1, 'OPPLN_PERMIT*') eq 1)
    if n_elements(match) gt 1 then match = match[n_elements(match)-1]
    
    op_permit = lines1(match)
    en_str1[ff] = strmid(op_permit, 15, 10) + ' ' $
                  + strmid(op_permit, 26, 8)

    ;en_str1[ff] = strmid(lines1[2], 17, 10) + ' ' $
    ;              + strmid(lines1[2], 28, 8)
  endfor

; ========================

;; Figure out the ALLOCATED data volumes from the "dr" files.

;; Figure out filenames.
  str2 = strmid(files1, 0, 59) + 'dr_2*.txt'
  files2 = file_search(str2, count=fcount2)

;; Loop over files.
  alloc_vol = lonarr(fcount2)
  for ff = 0,(fcount2-1) do begin
    read_seqfile, lines2, files2[ff]

;; Get ALLOCATED data volume.
    alloc_vol[ff] = long(strmid(lines2[4], 30, 20)) ; Mbits

  endfor  ; (ff-loop)


; ========================

;; Figure out the PLANNED data volumes from the "dr_XRT" files.

;; Figure out filenames.
  str3 = strmid(files2, 0, 62) + 'XRT*.txt'
  files3 = file_search(str3, count=fcount3)

;; Loop over files.
  planned_vol = dblarr(fcount3) ; Mbits
  print, 'Planned volumes...'
  for ff = 0,(fcount3-1) do begin
;    print, 'File #' + strcompress(ff+1,/rem) + ' of ' + strcompress(fcount3,/rem)
    read_seqfile, lines3, files3[ff]

;; Clip header and footer.
    nl3 = n_elements(lines3)
    lines3 = lines3[5:nl3-2]
    nl3 = n_elements(lines3)

;; Loop over lines.
    time1_sec = dblarr(nl3)
    time2_sec = dblarr(nl3)
    for ii = 0,(nl3-1) do begin

;; Get start times relative to OP_START, in sec.
      date1_str = strmid(lines3[ii], 0, 8)
      time1_str = strmid(lines3[ii], 10, 6)
      time1_ex = long([strmid(time1_str,0,2), strmid(time1_str,2,2), $
                  strmid(time1_str,4,2), '0', strmid(date1_str,6,2), $
                  strmid(date1_str,4,2), strmid(date1_str,0,4)])
      time1_sec[ii] = int2secarr(time1_ex, anytim2ints(st_str1[ff]))

;; Get stop times relative to OP_START, in sec.
      date2_str = strmid(lines3[ii], 18, 8)
      time2_str = strmid(lines3[ii], 28, 6)
      time2_ex = long([strmid(time2_str,0,2), strmid(time2_str,2,2), $
                  strmid(time2_str,4,2), '0', strmid(date2_str,6,2), $
                  strmid(date2_str,4,2), strmid(date2_str,0,4)])
      time2_sec[ii] = int2secarr(time2_ex, anytim2ints(st_str1[ff]))

    endfor   ; (ii-loop)

;; Identifying which row has the start time...
;; Find rows *after* start, then back it up by one.
    ss_start = where(time1_sec gt 0d, startcount)
    ss_start = ss_start[0] - 1 
    ;; If there is only one row in time1_sec, then the "where" does not
    ;; work right to get the row with the start time:
    if (startcount eq 0) then ss_start = 0

;; Identifying which row has the stop time...
;; Turn stop time into relative secs.
;; Find row ending at or after the stop time.

    en_sec1 = int2secarr(en_str1[ff], anytim2ints(st_str1[ff]))
    ss_stop = where(time2_sec ge en_sec1)
;; sometimes dr_file doesn't list OP_STOP as last time.  In
;; that case, take all lines
    if ss_stop[0] eq -1 then ss_stop = n_elements(time2_sec)-1 else $
       ss_stop = ss_stop[0]

;; Summing data volume on the start and stop rows...
;; Distinguish whether start/stop are the same row.
    if (ss_stop gt ss_start) then begin

;; Summing data volume on start row...
      planned_rate = double(strmid(lines3[ss_start],36,8))
      planned_vol[ff] = time2_sec[ss_start] * planned_rate

;; Summing data volume on stop row...
      planned_rate = double(strmid(lines3[ss_stop],36,8))
      planned_vol[ff] = planned_vol[ff] + ((en_sec1 - time1_sec[ss_stop]) $
                                           * planned_rate)


    endif else begin ; (ie, if same row)

      planned_rate = double(strmid(lines3[ss_start],36,8))
      planned_vol[ff] = en_sec1 * planned_rate

    endelse   ; (start and stop rows are same)

;; Summing data volume on any rows between start and stop...
;; Determine if any such rows exist.
;; Loop over rows.
    if (ss_stop ge (ss_start+2)) then begin
      for ii = (ss_start+1),(ss_stop-1) do begin
        planned_rate = double(strmid(lines3[ii],36,8))
        planned_vol[ff] = planned_vol[ff] +                              $
                          ((time2_sec[ii] - time1_sec[ii]) * planned_rate)

      endfor   ; (ii-loop)
    endif  ; (rows between start and stop)
 
  endfor  ; (ff-loop)


; ========================

;; Figure out the ACTUAL data volumes from the Level-0 files.

;; Figure out filelist from Level-0 archive.
  str4a = '/archive/hinode/xrt/level0/' + strcompress(year,/rem) $
         + '/' + string(month,format='(I02)') + '/*/*/*.fits'
  files4a = file_search(str4a, count=fcount4a)

  mmb = month + 1
  if (mmb eq 13) then begin
    mmb = 1
    yyb = year + 1
  endif else begin
    yyb = year
  endelse
  str4b = '/archive/hinode/xrt/level0/' + strcompress(yyb,/rem) $
         + '/' + string(mmb,format='(I02)') + '/*/*/*.fits'
  files4b = file_search(str4b, count=fcount4b)

  files4 = [files4a,files4b]

;; Convert the filenames into TAI times so we can compare against 
;; timeline start and stop times.
  filetimes4 = fid2time('_' + strmid(files4,47,25))
  filetai4 = anytim2tai(filetimes4)

;; Loop over timelines.
  actual_vol = dblarr(fcount3) ; Mbits
  print, 'Actual volumes...'

 ;; number of files with missing bitspp keyword
  nbad_index = 0
  for ff = 0,(fcount3-1) do begin
    print, 'File #' + strcompress(ff+1,/rem) + ' of ' $
           + strcompress(fcount3,/rem)

;; Get INDEX structures for the time period of that timeline.
    ss = where_times(filetai4, tstart=anytim2tai(st_str1[ff]), $
                     tend=anytim2tai(en_str1[ff])               )


    if (ss[0] ne -1) then begin
      read_xrt, files4[ss], index

;; some files that are 64x64 pixels don't have the bitspp
;; keyword in the header.  Weird.  Remove these files.

      match = where(index.naxis1 eq 64, comp = good)
      if match[0] ne -1 then begin
         if total(strmatch(tag_names(index[0]), 'bitspp')) eq 0 then begin
            
            bpfiles = files4[ss]
            bpfiles = bpfiles[good]
            read_xrt, bpfiles, index
            print, "Skipping " + trim(n_elements(match),1) +$
                   " 64x64 pixel files."
         
            nbad_index = nbad_index + n_elements(match)
         endif
      endif

;; Calculate total data volume for timeline.
      actual_vol[ff] = total(index.naxis1 * index.naxis2 * index.bitspp) / 1d6
    endif else begin
      actual_vol[ff] = 0.0
    endelse

  endfor  ; (ff-loop)


; ========================

;; Calculate and print statistics.

  print, 'Times                Allocated   Planned   Actual (Mbits)'
  print, '========================================================='

  for ff = 0,(fcount3-1) do begin
    print, st_str1[ff] + '     ' + strcompress(alloc_vol[ff],/rem) $
           + '        ' + strcompress(round(planned_vol[ff]),/rem) $
           + '       ' + strcompress(round(actual_vol[ff]),/rem)
    print, en_str1[ff]
    print
  endfor   ; (ff-loop)

  if keyword_set(report) then begin

     openu, unit, report, /get_lun, /append
     
     printf,unit, 'Times                Allocated   Planned   Actual (Mbits)'
     printf,unit,  '========================================================='

     for ff = 0,(fcount3-1) do begin
        printf,unit, st_str1[ff] + '     ' + strcompress(alloc_vol[ff],/rem) $
               + '        ' + strcompress(round(planned_vol[ff]),/rem) $
               + '       ' + strcompress(round(actual_vol[ff]),/rem)
        printf,unit, en_str1[ff]
        printf, unit
     endfor   

     free_lun, unit
  endif
; ========================

;; Finish

END

; ========================
