pro yo_xda2legacy, index, data, xdafiles=xdafiles, outdir=outdir, $
  imager_sxthxi=imager_sxthxi , year=year, month=month, day=day, $ 
  uncert=uncert,prefixx=prefixx, or_his=or_his, $
  nowrite=nowrite,hdr=hdr,muvy=muvy

if n_elements(outdir) eq 0 then outdir=curdir()
sxt=1 
sxt=keyword_set(sxt)
non_sxt=1-sxt
readroute=(['read_sxt','rd_xda'])(keyword_set(non_sxt))
uncert=keyword_set(uncert)
nowrite=keyword_set(nowrite)
; or_his=keyword_set(or_his)
sporor=keyword_set(sporor)

ordir_parent=get_logenv('sxt_orhis_dir')
ydbdef=concat_dir('ydb','orhis')
case 1 of
    data_chk(or_his,/string): orhisdir=or_his
    ordir_parent ne '': orhisdir=ordir_parent
    else: orhisdir=ydbdef
endcase
 
or_his=keyword_set(or_his)

readit=n_params() lt 2 and data_chk(xdafiles,/string)
noread=n_params() eq 2 and data_chk(index,/struct) ; index,data in, skip read
nouter=n_elements(xdafiles) > noread
for i=0,nouter-1 do begin                ; for each xdafiles
   if readit then begin 
      break_file,xdafiles(i),ll,pp,ff,ee,vv           ; AkT (20-may-2006)
      sporor=strmid(ff,0,3) eq 'spr'        ; to allow longer file name 
      uncert=strpos(xdafiles(i),'_unc') eq (strlen(xdafiles(i)) -4)
      if file_exist(xdafiles(i)) then begin 
         box_message,'Processing> '+  xdafiles(i)
         delvarx,index,data
         call_procedure,readroute,xdafiles(i),-1,index,data
      endif else  box_message,'No file> ' + xdafiles(i) 
   endif 
   full_path = yo_index2path(index, parent_dir=outdir, /concat_dirx, /make_dir, $
                    year=year, month=month, day=day)
   if or_his and sporor then begin 
      orname=concat_dir(orhisdir,ff+'.geny')
      if file_exist(orname) then begin
         restgenx,file=orname,hisinfo
      endif  else begin 
         box_message,'No OR HISTORY File Found>> ' + orname
         hisinfo={serial_num:-1} ; dummy 
         sporor=0 ; inhibit downline checks & HISTORY addition
      endelse
   endif
   for rec=0,n_elements(index)-1 do begin
      if sxt then filename=sxt_index2filename(index(rec),uncert=uncert,$
         prefixx=prefixx)
      headstruct=yo_xda2fitshead(index(rec),$ 
         /prepend_inst, /sswstruct,/addorbit, /addtim2orb, allcoms=allcoms)
      headstruct.filename=filename

      if data_chk(hisinfo,/struct) then begin   
         ss=where(index(rec).sxt.serial_num eq hisinfo.serial_num,hcnt)
         if hcnt eq 0 then begin 
            box_message,'No OR History entry for SN# ' + $
               strtrim(index(rec).sxt.serial_num,2)   
         endif else begin 
           orhis=hisinfo(ss(0)).orhis
           hlen=strlen(strtrim(orhis,2))
           ssnew=where(hlen eq 0 and shift(hlen,-1) ne 0,pcnt)
           cnum=strtrim(lindgen(pcnt)+1,2) + ' of ' + strtrim(pcnt,2)
           orhis(ssnew)='-- Assembled OR Component ' +  cnum + ' --
;          ----- this segment could move to sxt_orhis.pro.. ---
           orhis(ssnew+2)=strcompress(orhis(ssnew+2) + ' ' + orhis(ssnew+3))
           orhis(ssnew+2)=str_replace(orhis(ssnew+2),' and ','/')
           orhis(ssnew+2)=str_replace(orhis(ssnew+2),'dark_sub,','')
           orhis(ssnew+4)=str_replace(orhis(ssnew+4),'leak_sub.pro ','')
           orhis(ssnew+3)=''
           orhis=strcompress(strarrcompress(orhis))
           orhis=str_replace(orhis,' Subtraction:','')
           orhis=str_replace(orhis,'Interp','Intrp')     ; AkT(21-may-2006)
           orhis=str_replace(orhis,'DC Orbit Correction','Orbit Corr') ; AkT(21-may-2006)
;          ----------------------------------------------------
           ; if hisinfo(ss(0)).npfi gt 1 then $  ; DECOMMENT??
           update_history,headstruct,strarrcompress(orhis),caller='SXT_ORHIS'

         endelse
      endif

      if nowrite then begin
         if rec eq 0 then begin
            hdr=headstruct
            muvy=data(*,*,rec)
         endif else begin
           hdr=merge_struct(hdr,headstruct)
           muvy=[[[muvy]],[[data(*,*,rec)]]]
         endelse
      endif else begin
         mwritefits,headstruct,data(*,*,rec),outfile=concat_dir(full_path(rec),$
            filename),comments=' ' + allcoms
      endelse
   endfor
endfor 

return
end