;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           EIS_GET_MIRROR_POSITION
;
; PURPOSE
;           Function to return mirror positions MIP and yStart
;
; NOTES
;
;
; INPUTS
;           delx  - difference between s/c and raster pointings in X
;           dely  - difference between s/c and raster pointings in Y
;           ra_id - the raster database ID
;           sot_offset - fltarr(2)
;
; CATEGORY
;           EIS timeline planning
;
; WRITTEN
;           John A Rainnie RAL 3-Aug-2006
;
; VERSION
;           v0.1 JAR 12-March-2007
;               Use function "eis_get_total_raster_width" to calculate
;               the raster width
;           v0.2 Peter Young, 24-Apr-2007
;               Added sla_offset for Sit-and-stare studies.
;           v0.3 JAR 25-Apr-2007
;               Use "eis_get_fmirr_ss" function to return fine mirror
;               step size from $SSW/hinode/eis/response/cal_response.txt
;           v0.4 JAR 15-Jun-2007
;               Just for insurance, catch negative mip values and set to
;               zero
;           v0.5 JAR 4-Jul-2007
;               Return ROUNDed MIP
;           v0.6 JAR 5-Jul-2007
;               Return ROUNDed yStart (YIP)
;           v0.7 JAR 7-Mar-2008
;               Adding EIS/SOT offsets to yip and mip
;           v0.8 JAR 18-Mar-2008
;               Removed setting yStart(YIP) minimum (to 1)
;           v0.9 DRW 07-Jan-2009
;               Added an 8-arcset offset to the east for the 2-arcsec slit
;               to compensate for the fact that the 2as image centre lies
;               8 arcsec west of the centres of the 1as and 40s images on
;               the Sun.
;               (N.B.Change originally made by DRW on 07-Nov-2008, but
;                subsequently lost. However, it WAS in effect on EISCO only
;                from that date until its SSW update on 15-Dec-2008).
;           v0.91 DHB 24-Apr-2009
;                Clarification: The 2" slit image lies to the *Solar* West
;                of the 1" slit image so that an 8" correction to the *Solar* East
;                East is needed to coalign them. Therefore the MIP is increased
;                by 32 steps.
;           v1.0 JAR 24-Mar-2014
;                Added "obs_id" and "aia_time" as inputs which are to be
;                passed into "eis_pointing_offset" to calculate eis/aia
;                offset
;           v1.1 JAR 27-Aug-2014
;                Pass in and use sot_offsets from top-level gui.
;
;
;-
;______________________________________________________________________________
FUNCTION eis_get_mirror_position , delx   , dely     , 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)

; DRW: added a term to correct for the fact that the 2 arcsec slit image
; centre falls 8 arcseconds east of the 40" and 1" slot image centres.
; This is corrected by adding an offset in the CASE statement below
; where INTERNAL_SLA_OFFSET_2AS is defined so that the MIP is 32 steps
; larger now for the 2as slit.

internal_sla_offset_2as = 8.

;
; 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.
    ; Changed to the line below
    2    : sla_offset=2.0/2. + internal_sla_offset_2as
    3    : sla_offset=40.0/2.
    ELSE : BEGIN
      print,'% EIS_GET_MIRROR_POSITION: 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()

;yStart   = 256  + (-dely - (ra.windHeight / 2.))
mip      = 1200 + ((delx + sla_offset - (ra_width / 2.)) / (2.*fmirr_ss))
yip      = 512  + (-dely - (ra.windHeight / 2.))

; 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
       mip = mip + (aia_offset[0] / (2.*fmirr_ss))
       yip = yip -  aia_offset[1]
     END
  2: BEGIN
       mip = mip + ((aia_offset[0] - sot_offset[0]) / (2.*fmirr_ss))
       yip = yip -   aia_offset[1] + sot_offset[1]
     END
ENDCASE

; Just for insurance
; JAR 18-Mar-2008. Remove minimum value operation
mip = mip + def_offset[0] > 0.
yip = yip + def_offset[1] ; > 1.

RETURN , {yStart:ROUND(yip) , mip:ROUND(mip)}

END
