function iruhxascan2sxt,iru_i,iru_d0,hxa_i,hir_d0,hxa_d,hxa_s,code=code

; various constants
fiscaday = 6363    ;03; = gt_day('2-jun-96') , first day to use hxa scan data
fibliday = 7940    ;03; = gt_day('26-sep-2000') , first day of bad limb data
hxyfudge = -0.3    ;03; fudge constant to match limb and scan data on fiscaday

tstep = 210        ; time step (in sec) used for recalc. linear IRU fit
                   ; Important: 5*tstep must be greater than 1024

; make copies of iru_d, hir_d.  Copies will be modified by program
iru_d = iru_d0
hir_d = hir_d0

; find the good iru values in hir_d : pointer wwp (wwp is always valid)
iru_filt,hxa_i,hir_d,w_good=wwp
; transform IRU-S to IRU-Y values (if necessary)
hir_d = iru_s2y(hxa_i,hir_d)

; fix the bad iru values in iru_d by interpolation.
iru_filt,iru_i,iru_d
; transform IRU-S to IRU-Y values (if necessary)
iru_d = iru_s2y(iru_i,iru_d)

; get commanded suncenter positions as a fallback solution
dtt = anytim2ints(hxa_i(wwp))
dir = hir_d(*,wwp)
dxy = sxt_cmd_pnt(dtt)

; reconstruct the hxa hidden limbs and transform into sxt suncenter coord.
cxy = hxa2hxaxy(dtt,hxa_d(*,wwp),code=hcod)
cxy = hxaxy2sxt(dtt,cxy)
wcp = where(hcod gt 1)
if n_elements(wcp) ge 2 then begin
   ctt = dtt(wcp)
   cir = dir(*,wcp)
   cxy = cxy(*,wcp)
endif else ctt = 0

; calculate suncenter coordinates from hxa scan data
; conditional, so that we can run without it, if necessary
btt = 0
if n_elements(hxa_s) gt 1 and gt_day(iru_i(0)) ge fiscaday then begin  ;03;
   bxy = hxascan2hxaxy(hxa_s,btt,cod=scod)
   bxy = bxy + hxyfudge                                                ;03;
   bxy = hxaxy2sxt(btt,bxy)
   wwbi = tim2dset(dtt,btt,delta_sec=dbi)
   bir = dir(*,wwbi)
   wcp = where(dbi eq 0 and scod eq 1)
   if n_elements(wcp) ge 2 then begin
      btt = btt(wcp)
      bir = bir(*,wcp)
      bxy = bxy(*,wcp)
   endif else btt = 0     ; else part added on 99/3/16 (bug fix)
endif
; flag set extends scan range                                        ;03;
if gt_day(iru_i(0)) ge fibliday then fibliflg = 1 else fibliflg = 0  ;03; 

n_i = n_elements(iru_i)
out = fltarr(2,n_i)                     ; output variable
code = intarr(n_i)                      ; output data quality code

list = bytarr(n_i)+1          ; list to check off data points
wlis = where(list)

repeat begin  ; loop through short time intervals for which gyros are linear

   ; search for the earliest time in the list
   dummy = min(int2secarr(iru_i(wlis)),wlo)
   wlo = wlis(wlo)

   tilo = anytim2ints(iru_i(wlo))
   tihi = anytim2ints(iru_i(wlo),off=tstep)
   wwi = sel_timrange(iru_i(wlis),tilo,tihi)
   wwi = wlis(wwi)
   list(wwi) = 0B                   ; cross off selected elements in list

   tclo = anytim2ints(iru_i(wlo),off=-tstep)
   tchi = anytim2ints(iru_i(wlo),off=2*tstep)
   
   att = 0    ; initially an element, becomes vector as soon as data found

   ; first check if we could use hxa scan data   
   if n_elements(btt) ge 2 then begin
      wwc = sel_timrange(btt,tclo,tchi,/between)
      ; if only one hxa scan data point found, try several larger intervals:
      ; (a) centered
      if n_elements(wwc) lt 2 and wwc(0) ge 0 then begin
         tblo = anytim2ints(iru_i(wlo),off=-2*tstep)
         tbhi = anytim2ints(iru_i(wlo),off=3*tstep)
         wwc = sel_timrange(btt,tblo,tbhi,/between)
      endif
      ; (b) off-center - early
      if n_elements(wwc) lt 2 and wwc(0) ge 0 then begin
         tblo = anytim2ints(iru_i(wlo),off=-3*tstep)
         tbhi = anytim2ints(iru_i(wlo),off=2*tstep)
         wwc = sel_timrange(btt,tblo,tbhi,/between)
      endif
      ; (c) off-center - late
      if n_elements(wwc) lt 2 and wwc(0) ge 0 then begin
         tblo = anytim2ints(iru_i(wlo),off=-tstep)
         tbhi = anytim2ints(iru_i(wlo),off=4*tstep)
         wwc = sel_timrange(btt,tblo,tbhi,/between)
      endif
      ;03; extend search interval after fibliday to avoid hxa limb data
      if fibliflg and n_elements(wwc) lt 2 then begin ;03;
         tblo = anytim2ints(iru_i(wlo),off=-4*tstep)  ;03;
         tbhi = anytim2ints(iru_i(wlo),off=5*tstep)   ;03;
         wwc = sel_timrange(btt,tblo,tbhi,/between)   ;03;
         ; if only one hxa scan data point found, try larger intervals: ;03;
         ; (a) off-center - early                            ;03;
         if n_elements(wwc) lt 2 and wwc(0) ge 0 then begin  ;03;
            tblo = anytim2ints(iru_i(wlo),off=-12*tstep)     ;03;
            tbhi = anytim2ints(iru_i(wlo),off=5*tstep)       ;03;
            wwc = sel_timrange(btt,tblo,tbhi,/between)       ;03;
         endif                                               ;03;
         ; (b) off-center - late                             ;03;
         if n_elements(wwc) lt 2 and wwc(0) ge 0 then begin  ;03;
            tblo = anytim2ints(iru_i(wlo),off=-4*tstep)      ;03;
            tbhi = anytim2ints(iru_i(wlo),off=13*tstep)      ;03;
            wwc = sel_timrange(btt,tblo,tbhi,/between)       ;03;
         endif                                               ;03;
      endif                                                  ;03;
      ; found enough data
      if n_elements(wwc) ge 2 then begin
         tmin = min(int2secarr(btt(wwc)),max=tmax)
         if (tmax-tmin) ge tstep/10 then begin    ; time interval long enough?
            att = btt(wwc)
            air = bir(*,wwc)
            axy = bxy(*,wwc)
            code(wwi) = 8                 ; code for IRU fitted HXA scan data
         endif
      endif
   endif

   ; if hxa scan data didn't work, try hxa address data
   if n_elements(att) lt 2 and n_elements(ctt) ge 2 then begin
      wwc = sel_timrange(ctt,tclo,tchi,/between)
      if n_elements(wwc) ge 2 then begin
         tmin = min(int2secarr(ctt(wwc)),max=tmax)
         if (tmax-tmin) ge tstep/10 then begin    ; time interval long enough?
            att = ctt(wwc)
            air = cir(*,wwc)
            axy = cxy(*,wwc)
            code(wwi) = 4                 ; code for IRU fitted HXA addr data
         endif
      endif
   endif

   ; if hxa didn't work at all, try dejittering commanded data
   if n_elements(att) lt 2 and n_elements(dtt) ge 2 then begin
      wwc = sel_timrange(dtt,tclo,tchi,/between)
      if n_elements(wwc) ge 2 then begin
         tmin = min(int2secarr(dtt(wwc)),max=tmax)
         if (tmax-tmin) ge tstep/10 then begin    ; time interval long enough?
            att = dtt(wwc)
            air = dir(*,wwc)
            axy = dxy(*,wwc)
            code(wwi) = 2                 ; code for IRU fitted S/C com. data
         endif
      endif
   endif

   ; do the fitting procedure

   if n_elements(att) ge 2 then begin
      out(*,wwi) = iruhxa_fit(iru_i(wwi),iru_d(*,wwi),att,air,axy,degree=2)
   endif else begin
      ; use plain S/C commanded value
      out(*,wwi) = sxt_cmd_pnt(iru_i(wwi))
      code(wwi) = 1
   endelse

   wlis = where(list)
endrep until wlis(0) lt 0                  ; loop until list empty

return,out
end