pro trace_cat2data, catalog, out1, out2, $
	       check_files=check_files, filedset=filedset, absolute=absolute, $
	       _extra=_extra, loud=loud
;+
;   Name: trace_cat2data
;
;   Purpose: use catalog entries to get the data (or filelist/dset)
;
;   Input Parameters:
;      catalog - catalog entries to check or extract
;
;   Output Parameters:
;      out1, out2 - output parameters - definition depends on /FILEDSET
;                   switch - the two possibilities are:
;
;      trace_cat2data, catalog, index [,data]        - return 'index,data'
; -OR- trace_cat2data, catalog, file, ss, /FILEDSET  - return 'file,dsetss' 
;                                              
;   Keyword Parameters:
;      FILEDSET - switch - DETERMINES OUTPUT PARAMETER DEFINITION
;                          --------------------------------------
;                 Default: return 'index,data' via read_trace call
;
;      check_files - switch - if set, just check (no read) online
;                    (implied if no output parameters supplied)  
;      /XXXX - all others passed to read_trace via keyword inheritance
;      ex: /all - convert all trace tags
;          /image - use image default tags, etc.
;
;   Calling Examples:
;       trace_cat2data, cat(ss), index, data, /image   ; read index/data
;                                                      ; /IMAGE->read_trace  
;  
;   History:
;      16-April-1998 - S.L.Freeland
;      28-April-1998 - S.L.Freeland - dont try read if /FILEDSET is set
;      26-Nov-1998   - S.L.Freeland - pass unknown keywords to read_trace
;      28-May-1999   - S.L.Freeland - if /FILEDSET set, return absolute SS
;      26-Jun-1999   - RDB - corrected type (call to box_message)
;-
if not required_tags(catalog,'time,day,image_ss') then return
loud=keyword_set(loud)
absolute=keyword_set(absolute)

; -----------------------------------------------------
files=trace_struct2filename(catalog,/hourly)  ; hourly derived
ndesired=n_elements(files)
weeks=strarr(ndesired)
days=gt_tagval(catalog,/day)
uday=uniq(days)
; -----------------------------------------------------
for i=0, n_elements(uday)-1 do begin 
   ssday=where(catalog.day eq days(uday(i)))
   weeks(ssday)=anytim2weekinfo(catalog(ssday(0)),/first)
endfor
; -----------------------------------------------------

weeks='week'+weeks                       ; ->TRACE convention weekYYYYMMDD

position=gt_tagval(catalog,/image_ss)

online=concat_dir('TRACE_I1_DIR',concat_dir(weeks,files))
exist=file_exist(online)

wexist=where(exist,ecnt)
nexist=where(1-exist,necnt)
filedset=keyword_set(filedset)

; -----------------------------------------------------
case 1 of
  filedset: mess='Returing "file and dset array"'
  ecnt eq ndesired:  mess='All requested files online'
   necnt eq ndesired: begin
      box_message,'None of requested files online in $TRACE_I1_DIR'
      return
   endcase
   else: begin
      box_message,'Only '+ strtrim(ecnt,2) + ' out of ' + strtrim(ndesired,2) + ' files are online'
      return
   endcase
endcase
; -----------------------------------------------------

box_message,mess
check_files=keyword_set(check_files) or n_params() le 1

if check_files then begin
   box_message,['/check_files set or no output parameters supplied', $
		'Returning without reading...']
   return
endif

; ------ if possible, remap image_ss -> dset map -----------
ssall=gt_tagval(catalog,/image_ss)      ; image# in file 
ufiles=uniq(online,/first)              ; uniq files in the request

if absolute or (filedset and (1-file_exist(online(0))) and (n_elements(ufiles) gt 1))  then begin
   if loud then box_message,'Returning absolute image numbers'
   ufss=lindgen(n_elements(online))        ; return complete file list
endif else begin   
   if loud then box_message,'Mapping absolute image numbers->relative'
   ufss=[ufiles,ndesired]                  ; file pointers
   mxf_read_header,online(ufiles),header   ; read primary headers
   offset=totvect([0,header.ext_nrow])     ; 'running' total
   for i=0,n_elements(ufss)-2 do $
      ssall(ufss(i):ufss(i+1)-1)=ssall(ufss(i):ufss(i+1)-1)+offset(i)
endelse

; -----------------------------------------------------

; ------------- read the index,data --------------------
case 1 of 
  keyword_set(filedset): begin
     out1=online(ufiles)            ; uniq file list
     out2=ssall                     ; relative image pointers 
  endcase
  n_params() le 2: read_trace, online(ufiles), ssall, out1, _extra=_extra 
  else:            read_trace, online(ufiles), ssall, out1, out2, _extra=_extra
endcase  
; -----------------------------------------------------

return
end