;+
; NAME:
;           EIS_GET_CCD_TRANSLATION
;
; PROJECT:
;           SOLAR-B/EIS
;
; PURPOSE:
;           Returns the polynomial coefficients required to convert detector
;           pixel positions into wavelengths.
;           Polynomials up to n=4 are allowed.
;
; INPUT
;
;           SLIT_IND The index of the EIS slit.
;                    0   1"
;                    1   266"
;                    2   2"
;                    3   40"
;
; OPTIONAL INPUTS
;
;           TIME     In any format accepted by the routine
;                    anytim2tai(). E.g., '31-Oct-2008'. If not
;                    specified then the time is assumed to be the
;                    current system time (using routine get_utc).
;
; OUTPUT:
;           A 2-element structure containing the following tags:
;               a0 - coefficient for p^0
;               a1 - coefficient for p^1
;               a2 - coefficient for p^2
;               a3 - coefficient for p^3
;               a4 - coefficient for p^4
;
; HISTORY:
;           Ver.1, 16-May-2006, Peter Young
;           Ver.2, 29-Nov-2006, John Mariska
;           Ver.3, 12-Jan-2007, Peter Young
;               added input SLIT_IND so that a different wavelength calibration
;               can apply to different slits.
;           Ver.4, 9-Feb-2007, Peter Young
;               implemented a 6 pixel shift for the 266" slit, and a 3 pixel
;               shift for the 40" slit.
;           Ver.5, 3-Mar-2007, Peter Young
;               following advice from Charlie Brown, the slit offsets for the
;               40" and 266" slits are not being implemented. I have simply
;               commented out the code, so it will be straightforward to
;               re-implement the offsets.
;           Ver.6, 7-Nov-2008, Peter Young
;               Added TIME keyword so that shift parameter is now
;               time-dependent. New shift parameters following grating
;               move on 24-Oct-2008 have been added.
;           Ver.7, 4-Dec-2008, Peter Young
;               Allowed TIME= input to be any format accepted by anytim2tai().
;
; NOTES:
;           Coefficients taken from Lang et al. (2006) EIS calibration paper.
;           Only a 2nd order polynomial fit was used.
;
;           New version uses postlaunch numbers from Charlie Brown also used in
;           text file in response directory--update both whenever a change is
;           made!
;
;           Care must be taken to get the sign of the pixel shifts
;           correct. Suppose the wavelength scale has recently
;           changed...if, when you view the spectrum in a particular
;           wavelength window, the line is not centered in the window
;           but lies 5 pixels away in the *short* wavelength direction,
;           then you will need to specify a pixel shift of
;           +5. Otherwise you will specify a pixel shift of -5. Also,
;           the pixel shifts are relative to the original EIS
;           calibration.
;-
;______________________________________________________________________________
FUNCTION eis_get_ccd_translation, slit_ind , time = time

;
; if slit_ind not specified, then assume 1" slit
;
IF n_elements(slit_ind) EQ 0 THEN slit_ind=0

;
; The structure P defines the pixel shifts that apply for a given time
; period. The tag TIME denotes the time at which shifts become
; effective, e.g., the parameter p[0].c0 applies from p[0].time until
; p[1].time.
; See note in the header about specifying the *sign* of the pixel
; shifts.
;


IF (N_ELEMENTS(time) EQ 0) THEN BEGIN
  get_utc , utc
  time_tai = anytim2tai(utc)
ENDIF ELSE BEGIN
  time_tai=anytim2tai(time)
ENDELSE

time_0 = anytim2tai(' 1-Jan-2000 00:00:00.00')
time_1 = anytim2tai('24-Oct-2008 00:00:00.00')

p = [ { time: time_0 ,  c0: 0.0 , c1: 0.0 , c2: 8.00 , c3: 0.0}      ,     $
      ;{ time: time_1 ,  c0: 0.0 , c1: 0.0 , c2: 8.00 , c3: 0.0}            $
      { time: time_1 ,  c0: 4.62 , c1: 4.62 , c2: 12.62 , c3: 4.62}            $
    ]

chck   = (time_tai GE p.time)
index  = TOTAL(chck) - 1

str    = {a0: 0d0, a1: 0d0, a2: 0d0, a3: 0d0, a4: 0d0}
coeffs = REPLICATE(str,2)


CASE slit_ind OF

3: BEGIN       ; for 40" slit
  p_shift=p[index].c3
 ;
 ; Short wavelength band
 ;
  coeffs[0].a0=165.0059; old value is 165.19564d0
  coeffs[0].a1=0.022318068d0
  coeffs[0].a0=coeffs[0].a0 + p_shift * coeffs[0].a1
  coeffs[0].a2=-1.26823d-8
 ;
 ; Long wavelength band
 ;
  coeffs[1].a0=196.5888; old value is 196.77865d0
  coeffs[1].a1=0.022336d0
  coeffs[1].a0=coeffs[1].a0 + p_shift * coeffs[1].a1
  coeffs[1].a2=-1.32959d-8
END

2: BEGIN       ; for 2" slit
  p_shift=p[index].c2
 ;
 ; Short wavelength band
 ;
  coeffs[0].a0=165.0059; old value is 165.19564d0
  coeffs[0].a1=0.022318068d0
  coeffs[0].a0=coeffs[0].a0 + p_shift * coeffs[0].a1
  coeffs[0].a2=-1.26823d-8
 ;
 ; Long wavelength band
 ;
  coeffs[1].a0=196.5888; old value is 196.77865d0
  coeffs[1].a1=0.022336d0
  coeffs[1].a0=coeffs[1].a0 + p_shift * coeffs[1].a1
  coeffs[1].a2=-1.32959d-8
END

1: BEGIN       ; for 266" slit
  p_shift=p[index].c1
 ;
 ; Short wavelength band
 ;
  coeffs[0].a0=165.0059; old value is 165.19564d0
  coeffs[0].a1=0.022318068d0
  coeffs[0].a0=coeffs[0].a0 + p_shift * coeffs[0].a1
  coeffs[0].a2=-1.26823d-8
 ;
 ; Long wavelength band
 ;
  coeffs[1].a0=196.5888; old value is 196.77865d0
  coeffs[1].a1=0.022336d0
  coeffs[1].a0=coeffs[1].a0 + p_shift * coeffs[1].a1
  coeffs[1].a2=-1.32959d-8
END

ELSE: BEGIN       ; for all other slits
  p_shift=p[index].c0
 ;
 ; Short wavelength band
 ;
  coeffs[0].a0=165.0059; old value is 165.19564d0
  coeffs[0].a1=0.022318068d0
  coeffs[0].a0=coeffs[0].a0 + p_shift * coeffs[0].a1
  coeffs[0].a2=-1.26823d-8
 ;
 ; Long wavelength band
 ;
  coeffs[1].a0=196.5888; old value is 196.77865d0
  coeffs[1].a1=0.022336d0
  coeffs[1].a0=coeffs[1].a0 + p_shift * coeffs[1].a1
  coeffs[1].a2=-1.32959d-8
END

ENDCASE

RETURN , coeffs

END
