
pro sxt_his2dbase, indexin, index_dbase, data_dbase, $
	     leak=leak, dark=dark, short=short, long=long, $
	     filelist=filelist, ss=ss
;+
;   Name: sxt_his2dbase
;
;   Purpose: use SXT history record to reference/return dbase "index [,data]"
;
;   Input Parameters:
;      indexin - index record(s), assumed to contain .HIS field
;
;   Output Parameters:
;     index_dbase - dbase index used in correction (.HIS->dbase mapping)
;     data_dbase  - optional, the data array corresponding to index_dbase
;
;   Keyword Parameters:
;      LEAK (switch) - if set, output is from SFC dbase [DEFAULT]
;      DARK (switch) - if set, output is from SDC dbase
;      SHORT (switch) - if set, output is SHORT exposure DARK
;      LONG (switch)  - if set, output is LONG exposure DARK
;      filelist (output) - list of dbase files implied
;      ss (output) - SS vector corresponding to dbase filelist
;
;   Calling Sequence:
;      IDL> sxt_his2dbase, index, dbindex [,dbdata] [,/leak] [,/dark] [,/short]  
;
;   Calling Examples:
;                            IN        OUT      OUT 
;      IDL> sxt_his2dbase, sscindex, sfcindex, sfcdata, /LEAK          ; SFC
;      IDL> sxt_his2dbase, sscindex, sdcindex, sdcdata, /DARK [,/LONG] ; SDC
;      IDL> sxt_his2dbase, sscindex, sdcindex, sdcdata, /SHORT         ; SDC
;  
;   History:
;      11-May-1999 - S.L.Freeland - simplify some dbase verification checks
;
;   Restrictions:
;      Assumes online DBASE is equivilent to the one where corrections
;      were applied (Warning message printed if mismatch which implies
;      correction files not available.
;-

if not required_tags(indexin,'his') then begin
     box_message, 'Input index must include history tag (.HIS)
     return
endif

short=keyword_set(short)
long=keyword_set(long)
dark=keyword_set(dark)  or short or long
nodc=total([short,long]) eq 0
long=long or nodc
smess=(['','[ use /'+ (['SHORT','LONG'])(short) + ' to access other] ' ])(nodc)

leak=1-keyword_set(dark)

reftime=anytim(indexin,/int)

case 1 of
   dark: begin
      box_message,['Returning dark frame info (' + (['SHORT','LONG'])(long)+')' + smess]
      reftime.day =indexin.his.day_dark(long,*)
      reftime.time=indexin.his.time_dark(long,*)
      weekid=anytim2weekinfo(reftime,/year2digit)
      weekid=weekid(uniq(weekid))
      filelist=file_list('$DIR_SXT_SDC','sdc'+weekid+'*',/cd)
      rd_roadmap, filelist, rmap
      ss=tim2dset(rmap, reftime, offset=offset)
      if max(abs(offset)) gt 0 then $
	   box_message,'Warning - history record/dbase mismatch...',nbox=2
      rd_xda, filelist, ss, index_dbase, data_dbase, nodata=(n_params() le 2)
   endcase
   else: begin
      reftime.day =indexin.his.day_leak
      reftime.time=indexin.his.time_leak
      filenames=time2file(reftime,/year2digit,delim='.')
      filelist=file_list('$DIR_SXT_SFC','sfc'+filenames+'*',/cd)
      if n_elements(filelist) ne n_elements(filenames) then $
	   box_message,'Warning - history record/dbase mismatch...',nbox=2
      if filelist(0) ne '' then begin 
         ss=-1
	 rd_xda,filelist, ss, index_dbase, data_dbase, nodata=(n_params() le 2)       
         box_message,'returning leak information 
      endif else box_message,['Cannot find any matching leak files',$
	     concat_dir('$DIR_SXT_SFC','sfc'+filenames+'*')]
    endcase
endcase

return
end
