;+
; NAME:  
;    get_xrt_hk_value
;
; PURPOSE: 
;    get XRT housekeeping values for xrt metrics 
;
; CALLING SEQUENCE:
; 
;    plot_xrt_hk_value, 'mod_S_SOFTWARE_MODE_' , start_time, end_time, $
;                            data_out, [,gif_dir=gif_dir]
; INPUTS:
;
;    start_time: [Mandatory] Beginning of timerange to plot
;    end_time:   [Mandatory] End of timerange to plot
;
; OPTIONAL INPUTS:
;
;    None
;
; KEYWORD PARAMETERS:
;
;    dir: directory where HK files live
;
; OUTPUTS:
;    data_out: output structure containing housekeeping data. 
;              contains data_out.time and data_out.data
;
; OPTIONAL OUTPUTS:
;
;    None
;
;
; EXAMPLE: 
;
;    plot_xrt_software_mode, '1-Apr-2008 00:00:00', '30-Apr-2008 00:00:00',$
;                             data_out, gif_dir = '/home/mweber/'
;
;;
; MODIFICATION HISTORY:
;
;    Written 05-08-2008 by KKR. 
;    Based heavily on plot_hk_val_vs2.pro, by KKR & PRJ
;  
;-

pro get_xrt_hk_value, hk_key, start_time, end_time, data_str, $
                       dir=dir, gif_dir=gif_dir

  if not keyword_set(dir) then dir = '/data/solarb/XRT/hk_cron'

  

  day1 = anytim(end_time)
  day2 = anytim(start_time)

  ;;calculate  number of days data to load
  ndays = round((day1 - day2)/(3600.0*24.0) + 1)

  ;; get the directory where the hk data is for this date
  get_hk_dir, dir, start_time, hk_dir

  ;; concatenate directories for all data
  for i=0, ndays do begin
     
     new_hk_files=file_search(concat_dir(hk_dir,hk_key+'*.txt'))
     
     if i eq 0 then hk_files = new_hk_files else $
        hk_files = [hk_files, new_hk_files]
     
     new_date = atime(anytim(start_time) + (i+1)*24.0*3600.0)
     
     get_hk_dir, dir, new_date, hk_dir

  endfor

  ;; check that at least some data exists during this time
  match = where(strmatch(hk_files,'') eq 0)

  if match[0] eq -1 then begin
     message, 'No files found for keyword: '+hk_key+' Returning...',/continue
     return
  endif

  hk_files = hk_files[match]
  nfiles = n_elements(hk_files)

;; get files for time range

  date = strmid(ymd2date(strmid(hk_files,14,8,/reverse)),0,12)
  hour = strmid(hk_files, 5,2,/reverse)
  seconds = anytim(date + hour + ':00:00')

  end_sec = anytim(end_time)
  start_sec = anytim(start_time)

  match = where(seconds ge start_sec and seconds le end_sec)

;; get data if it exists   

  if match[0] eq -1 then begin
     message,'No file for keyword: '+hk_key+'between: '+start_time+$
             ' and '+end_time+' Returning...',/continue
     return
  endif

  data_files = hk_files[match]

  data_str=create_struct(Name=hk_key,'N_elements',n_elements(data_files))

  for i = 0, n_elements(data_files) -1 do begin

     status=0

     ;; Reading in the data
     get_hk_value_vs2, data_files[i],out_time,out_hk_val,status

     if (status eq 0) then begin
        n = n_elements(out_time)

        new_time = out_time

        strput, new_time, ' ', 10  ;; remove underscore in times

        date = anytim2cal(end_time, form=11,/date)

        t1 = new_time[0]
        t2 = new_time[n-1]

        match = where(finite(out_hk_val))

        if match[0] eq -1 then begin
           nvals = n_elements(out_hk_val)
           out_hk_val = fltarr(nvals)*0.0
        endif

        data_str=create_struct(data_str,'time'+trim(i),$
                               new_time,'data'+trim(i),out_hk_val)

     endif
  endfor

  error=0

end
