
;+
; NAME:
;        HXAXY2SXT
; PURPOSE:
;        Transform HXA suncenter coordinates into SXT coordinates and
;        perform time dependent corrections.
;        Time dependent corrections are: orbital phase correction,
;        orbit duration correction, mission time correction (non-periodic).
; CATEGORY:
; CALLING SEQUENCE:
;        s_sunc = hxa_suncenter(time_index,h_sunc)
; INPUTS:
;        time_index = time vector, e.g., pnt, atr, or ada_index structure.
;        h_sunc = array(2,*). suncenter coordinates in HXA coordinates.
;                 sunc(0,*) = hxa_x, sunc(1,*) = hxa_y
; KEYWORDS (INPUT):
;        /noorbit = do not correct for orbital variations of the offset
;                   between HXT and SXT.
;        /nomission = do not correct for mission time variation of the offset
;                     between HXT and SXT.
; OUTPUTS:
;        s_sunc = array(2,*). suncenter coordinates in SXT full resolution
;                 pixels.  sunc(0,*) = sxt_x, sunc(1,*) = sxt_y
; KEYWORDS (OUTPUT):
; COMMON BLOCKS:
;        None.
; SIDE EFFECTS:
; RESTRICTIONS:
;        The mission time variation of the offset between HXT and SXT is
;        well corrected for the first year of Yohkoh.  The correction
;        needs to be updated as the project goes on.
; PROCEDURE:
; MODIFICATION HISTORY:
;	19-Jul-93 (JPW) - part of hxa_suncenter
;       22-Sep-94 (JPW)   Changed to apply mission correction only to pre-1993
;                         data.  Extrapolation beyond that caused large errors.
;                         Slightly changed algorithm for orbital phase corr.
;-

function hxaxy2sxt,time,hxa_d,noorbit=noorb,nomission=nomis

; various constants
hxa_parms,hpix=hpix,h_xx=h_xx,h_yy=h_yy,spix=spix,phi=phi,sxt_off=sxt_off, $
              cox=cox,coy=coy,tofac=tofac,clx=clx,cly=cly,tloff=tloff, $
              cmx=cmx,cmy=cmy,tmoff=tmoff,tmcut=tmcut
; hpix = HXA pixel size
; h_xx, h_yy = HXA detector crossing point coordinates
; spix = SXT pixel size  (Note: in hxa_suncenter: spix = hpix/spix!)
; phi = angle between SXT (E->W) and HXA (SE->NW)
; sxt_off = SXT coordinates of HXA detector crossing point
; cox, coy = coefficients for orbital phase correction
; tofac = multiplier for relative orbital phase
; clx, cly = coefficients for orbit duration correction
; tloff = time offset for orbit duration correction (in minutes)
; cmx, cmy = coefficients for mission time correction
; tmoff = time offset for mission time correction (in days)
; tmcut = last day for which mission correction can be used
spix = hpix/spix            ; spix is pixel size ratio now

sunc = float(hxa_d)
sunc(0,*) = sunc(0,*) - h_xx       ; subtract HXA offset
sunc(1,*) = sunc(1,*) - h_yy

sunout = sunc  ; create output array

; sxt transformation
sinphi = sin(phi/!radeg)
cosphi = cos(phi/!radeg)
sunout(0,*) = spix * (sunc(0,*)*cosphi + sunc(1,*)*sinphi) + sxt_off(0)
sunout(1,*) = spix * (sunc(0,*)*sinphi - sunc(1,*)*cosphi) + sxt_off(1)


; orbital time correction and correction for duration of day
if (not keyword_set(noorb)) then begin

  tim2orbit,time,tim2fms=t_orb,tim2night=t_nit,/simple
  t_nit = t_nit+t_orb               ; length of day in minutes
  t_orb = tofac*(t_orb/t_nit)       ; relative orb. phase * tofac
  t_nit = t_nit-tloff               ; subtract a constant for numerics

  sunout(0,*) = sunout(0,*) + cox(0) + cox(1)*t_orb + cox(2)*t_orb*t_orb
  sunout(1,*) = sunout(1,*) + coy(0) + coy(1)*t_orb + coy(2)*t_orb*t_orb

  sunout(0,*) = sunout(0,*) + clx(0) + clx(1)*t_nit + clx(2)*t_nit*t_nit
  sunout(1,*) = sunout(1,*) + cly(0) + cly(1)*t_nit + cly(2)*t_nit*t_nit
endif

; correction for non-periodic variation over mission
if (not keyword_set(nomis)) then begin
   t_day = gt_day(time)
   ww = where(t_day le tmcut,nww)          ; apply correction only before tmcut
   if nww gt 0 then begin
      t_day = float(t_day(ww) - tmoff)     ; time for secular variations
      sunout(0,ww) = sunout(0,ww) + cmx(0) + cmx(1)*t_day + cmx(2)*t_day*t_day
      sunout(1,ww) = sunout(1,ww) + cmy(0) + cmy(1)*t_day + cmy(2)*t_day*t_day
   endif
endif


return,sunout
end
