pro xrt_metrics, start_date, end_date, exclude = exclude, sw_plot_start=sw_plot_start, sw_plot_end = sw_plot_end

;; exclude keyword is for xrt_telemetry_metrics, for excluding op
;; periods at the end of the month
;;
;; sw_plot_start, sw_plot_end are keywords for plotting software mode
;; over a different tme period than the metrics (since anomalies are
;; now reported for the current month, and metrics for the previous month)

;; date format:  '1-May-2008'

  report = start_date +'.txt'

;; get start month and year in integers for missing files program

  date = atime(start_date, /hxrbs, /y2k)
  str_year = strmid(date, 0, 4)
  str_month = strmid(date, 5, 2) 

  year = str_year*1
  month = str_month*1

;; check to make sure we're not overwriting an existing report

  file = file_info('$XRT/metrics/reports/'+str_year+'/'+str_month+'/'+report)

  if file.exists then begin
     answer = '' 
     read, answer, prompt='Telemetry report file exists. Continue [y/n]?'
     if strcmp(answer,'y') eq 0 then begin 
        print, 'Exiting...' 
        return
     endif
  endif

;; open report and include header info

openw, unit, report, /get_lun

printf, unit, 'XRT metrics report from ' + start_date + ' to ' + end_date
printf, unit

free_lun,unit

;; make a plot of XRT software mode

print, '========== Plotting XRT software mode....'

if keyword_set(sw_plot_start) then plot_xrt_software_mode, sw_plot_start, sw_plot_end $
else plot_xrt_software_mode, start_date, end_date

;; calculate XRT telemetry metrics

print, '========== Calculating telemetry usage...'

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

;; add telemetry metrics info to report

telemetry_report, report, planned_vol, alloc_vol, actual_vol, nbad_index

;; find missing files and packets

print, '========== Finding missing files and missing packets...'

start_time = start_date + ' 00:00:00'
end_time = end_date + ' 00:00:00'

xrt_missing_imgs, start_time, end_time, miss_files, nfiles, noutliers, /metrics

wrap_xrt_id_miss_pack, year, month,miss_pkts, miss_pfiles, p_idx, files

;; add info on missing files and packets to report

missing_files_report, report, miss_files, miss_pkts, miss_pfiles, nfiles, noutliers
  
;; make directory, move files there

spawn, 'mkdir -p $XRT/metrics/reports/'+str_year+'/'+str_month
spawn, 'mv '+ report +  ' $XRT/metrics/reports/'+str_year+'/'+str_month

if keyword_set(sw_plot_start) then begin
   date = atime(sw_plot_start, /hxrbs, /y2k)
   str_year = strmid(date, 0, 4)
   str_month = strmid(date, 5, 2) 
   spawn, 'mkdir -p $XRT/metrics/reports/'+str_year+'/'+str_month
   spawn, 'mv xrt_software_mode_'+str_year+str_month + $
          '.gif $XRT/metrics/reports/'+str_year+'/'+str_month
endif else begin
   spawn, 'mv xrt_software_mode_'+str_year+str_month + $
          '.gif $XRT/metrics/reports/'+str_year+'/'+str_month
endelse
end

  
