;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           EIS_GET_XY_POINT
;
; PURPOSE
;           Function to return mirror positions MIP and yStart
;
; NOTES
;           This function is the converse of "eis_get_mirror_position"
;
; INPUTS
;           sc_x  - X s/c pointing
;           sc_y  - Y s/c pointing
;           mip   - Mirror Initial Position
;           yip   - yStart
;           ra_id - the raster database ID
;
; CATEGORY
;           EIS timeline planning
;
; WRITTEN
;           John A Rainnie RAL 4-Jul-2007
;
; VERSION
;           v0.1 JAR 5-Jul-2007
;                Updated to calculate both MIP and YIP (yStart)
;           v0.2 JAR 7-Mar-2008
;                1. Subtract EIS/SOT offsets from x and y coords.
;                2. Changed yStart from 256 to 512
;           v0.3 JAR 24-Mar-2014
;                Added inputs "obs_id" and "aia_time" which are passed
;                into "eis_pointing_offset" to calculate eis/aia offset
;           v0.4 JAR 27-Aug-2014
;                Pass in and use sot_offsets from top-level gui.
;
;-
;______________________________________________________________________________
FUNCTION eis_get_xy_point , sc_x       , sc_y                          ,   $
                            mip        , yip      , ra_id              ,   $
                            obs_id     , aia_time , sot_offset

; Get EIS pointing offset
offsets    = eis_pointing_offset(obs_id , aia_time , sot_offset)
def_offset = offsets.def_offset
aia_offset = offsets.aia_offset
; JAR 27-Aug-2014. Use (internal) eis_mk_plan_gui__define sot_offsets,
; passed in as inputs
;sot_offset = offsets.sot_offset

; Get total raster spatial width
ra       = db_read_raster_entry(ra_id)
ra_width = eis_get_total_raster_width(ra)

;
; Need to offset the mirror position by the
; half-width of the slit (this centres the slots on the required pointing)
;
CASE ra.slitindex OF
    0    : sla_offset=1.0/2.
    1    : sla_offset=266.0/2.
    2    : sla_offset=2.0/2.
    3    : sla_offset=40.0/2.
    ELSE : BEGIN
      print,'% EIS_GET_XY_POINT: raster slit_index parameter is out of range!'
      print,'  Allowed values are 0-3.  Current value is '+trim(ra.slitindex)
      print,'  A value of 0 will be assumed.'
      sla_offset=1.0/2.
    ENDELSE
ENDCASE

; Get fine mirror step size from cal_response.txt file
fmirr_ss = eis_get_fmirr_ss()

; Calculate xy_point
x_point = sla_offset + sc_x - (ra_width/2.)  - (mip-1200.)*(2.*fmirr_ss)
;
; JAR & PRY 7-Mar-2008
; Use 512 instead of 256
;
;y_point =              sc_y + (ra.windHeight/2.) +  yip-256.
y_point =              sc_y + (ra.windHeight/2.) +  yip-512.

; DHB/JAR 30-Apr-2014 - apply appropriate offsets according to obs_id
; obs_id = 0: Don't apply either EIS/AIA or SOT/Hinode offsets
; obs_id = 1: Apply EIS/AIA offset but not SOT/Hinode offset
; obs_id = 2: Apply both EIS/AIA and SOT/Hinode offsets
CASE obs_id OF
   0 :  ; do nothing
   1 : BEGIN
           x_point = x_point + aia_offset[0]
           y_point = y_point + aia_offset[1]
       END
   2 : BEGIN
           x_point = x_point + aia_offset[0] - sot_offset[0]
           y_point = y_point + aia_offset[1] - sot_offset[1]
       END
ENDCASE


RETURN , { x_point : x_point - def_offset[0] ,        $
           y_point : y_point - def_offset[1]}

END
