
;+
; NAME:
;        SXT2HXAXY
; PURPOSE:
;        Transform SXT suncenter coordinates into HXA coordinates and
;        perform time dependent corrections.  Inverse of hxaxy2sxt.
;        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.
;        s_sunc = array(2,*). suncenter coordinates in SXT fullresolution
;                 pixels (FRE pixels).
;                 sunc(0,*) = sxt_x, sunc(1,*) = sxt_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:
;        h_sunc = array(2,*). suncenter coordinates in HXA detector pixels.
;                 sunc(0,*) = hxa_x, sunc(1,*) = hxa_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:
;	28-Jul-93 (JPW) - from hxaxy2sxt
;       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 sxt2hxaxy,time,sxt_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(sxt_d)

; 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
      sunc(0,ww) = sunc(0,ww) - cmx(0) - cmx(1)*t_day - cmx(2)*t_day*t_day
      sunc(1,ww) = sunc(1,ww) - cmy(0) - cmy(1)*t_day - cmy(2)*t_day*t_day
   endif
endif

; 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

  sunc(0,*) = sunc(0,*) - clx(0) - clx(1)*t_nit - clx(2)*t_nit*t_nit
  sunc(1,*) = sunc(1,*) - cly(0) - cly(1)*t_nit - cly(2)*t_nit*t_nit

  sunc(0,*) = sunc(0,*) - cox(0) - cox(1)*t_orb - cox(2)*t_orb*t_orb
  sunc(1,*) = sunc(1,*) - coy(0) - coy(1)*t_orb - coy(2)*t_orb*t_orb
endif

sunout = sunc  ; create output array

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

sunout(0,*) = sunout(0,*) + h_xx       ; add HXA offset
sunout(1,*) = sunout(1,*) + h_yy

return,sunout
end
