;+
; NAME:
;       EIS_LTDS
;
; PURPOSE:
;       Calculates the long-term decrease in sensitivity (LTDS) of the
;       EIS LW channel, and returns the correction factor to be
;       multiplied to the EIS radiances for data that
;       have been previously calibrated with the ground calibration.
;
;       If the EIS data had been calibrated with the old
;       /correct_sensitivity keyword (which implemented an exponential
;       sensitivity decay equally for all lines), then the user should
;       give the /UNDECAY keyword, which "uncorrects" this correction
;       factor. 
;
; CATEGORY:
;
;       Hinode EIS  Data analysis SW
;
; CALLING SEQUENCE:
;        print, eis_ltds('14-Sept-2010 00:00:00.000',[171,195,211,255,284])
;
; INPUTS:
;      date: a valid string with one single date.
;      wavelengths: an array of EIS wavelengths (Angstroms)
;
; KEYWORD PARAMETERS:
;      QUIET:  If set, then no informational messages will be printed.
;
;      UNDECAY:  If an EIS file had previously been calibrated using the
;                /correct_sensitivity option to eis_prep, then setting
;                the /undecay return will yield the correction factor
;                to go from this calibration to the GDZ calibration. 
;
; OUTPUTS:
;       The function returns the correction factors at the input wavelengths
;
; OPTIONAL OUTPUTS:
;       The routine can return via keywords:
;         lw_corr: the degradation of the LW  channel 
;         effa_sw: the SW effective areas for the input date
;         effa_lw: the LW effective areas for the input date
;
; CALLS:
;       rd_tfile to read the ground-based effective areas and a few
;       standard IDL/SSW routines.
;
; COMMON BLOCKS:
;      None
;
; PROCEDURE:
;
;      This programs calculates the EIS effective areas and the long-term
;      decrease of the LW channel as described in Del Zanna (2013,
;      A&A, 555, A47).
;      It returns  the correction factor to be
;      multiplied to the  EIS radiances for data
;      that have been previously calibrated with the ground
;      calibration (**without any long-term corrections applied to them**).
;
; RESTRICTIONS:
;
;      Input Dates must be between 22-Sept-2006 and 14-Sept-2012
;      Wavelengths must be in the EIS ranges
;
; MODIFICATION HISTORY:
;
;    Ver. 1, 2-May-2013, Giulio Del Zanna
;    Ver. 2, 5-Oct-2013, Peter Young
;       corrected minor bug; introduced /quiet keyword; updated
;       header; added /undecay keyword.
;    Ver. 3, 12-Mar-2024, Peter Young
;       fixed bug whereby /undecay was giving incorrect results.
;-


function eis_ltds, date, wavelengths, lw_corr=lw_corr,effa_sw=effa_sw,effa_lw=effa_lw, $
                   quiet=quiet, undecay=undecay

ind1=where(wavelengths gt 165 and wavelengths lt 212, n1)
ind2=where(wavelengths gt 245 and wavelengths lt 292, n2)

if n1+n2 eq 0 then message,'Error, no input wavelengths in the EIS spectral region! '



; these are the effective areas from the adjusted ground-based  calibration

eff_lw =rd_tfile('$SSW/hinode/eis/response/EIS_EffArea_A.004',/nocom,/auto,/conv)
eff_sw= rd_tfile('$SSW/hinode/eis/response/EIS_EffArea_B.004',/nocom,/auto,/conv)


; these are the spline points for the Del Zanna (2013) calibration:


gdz_sw=[165, 171, 174.5, 177.2, 178.1, 180.4, 182.2, $
 184.5, 185.2, 186.9,  188.3, 190,  $
         192.4, 192.8, 193.5, 194.7, 195.1, $
         196.6, 197.4, 200,  201.1,  202.,  $
         202.7, 204.9, 208., 209.9, 211.3]
 
gdz_eff_sw=[0.000174973/1.5, 0.000255772/1.5,0.00158207/1.5, 0.00476608/1.55, $
            0.00705735 /1.5, 0.0168637/1.45, 0.0316499/1.4, $
 0.0647319/1.35, 0.0779082/1.35, 0.115240/1.4,  0.150199/1.45, 0.194897/1.25, $
            0.255993/1.13, 0.264945/1.1,  0.279607/1.05, 0.298884/1.02,  0.302737*1., $
            0.301859/1.05, 0.287675/1.15, 0.174608*1.05,  0.119586/1.0,  0.0838537 /1.,$
            0.0635698/1.,  0.0332376/1.0 ,0.0189209/1., 0.0133581/1., 0.0105513/1.]

gdz_lw=[245., 252., 255,  257., 259, $
        263. , 265., 268.,  270., $
        272. , 274. , 277.,    281., 286., 292]

gdz_eff_lw=[0.022673*0.8, 0.03908*0.75, 0.05065*0.78,  0.0588  *0.8, 0.06738*0.85,$
            0.0861*0.9,  0.09551*0.95, 0.106984*1.0, 0.110764*1.02, $
            0.10944*1.03,  0.1026*1.03, 0.084775*0.9,  0.05718*0.87, 0.0333*0.85, 0.01679*0.85]/1.1

; these are the values interpolated over the wavelength grid of the
; ground calibration:
effa_sw= interpol(gdz_eff_sw , gdz_sw, reform(eff_sw[0, *]),/spline)
effa_lw= interpol(gdz_eff_lw , gdz_lw, reform(eff_lw[0, *]),/spline)

; convert input time into TAI:
Xtime=ANYTIM2TAI(date)
xtime_save=xtime
 
; this is the reference time
  Xtime_ref=ANYTIM2TAI('22-Sept-2006 21:36:00.000')
  
; this is the last time for which the long-term correction can be
; applied for:
  Xtime_last=ANYTIM2TAI('14-Sept-2012 00:00:00.000')


 corrections=fltarr(n_elements(wavelengths))+1.  
  
  if xtime lt Xtime_ref then BEGIN 
    IF NOT keyword_set(quiet) THEN print,'% EIS_LTDS: input time before launch date! Set correction factor(s) to 1'
    return,corrections
  ENDIF 

  if xtime gt Xtime_last then begin 
    IF NOT keyword_set(quiet) THEN print,'% EIS_LTDS: input time is after 14-Sept-2012 - the correction for the LW channel is given assuming no degradation after that date.'   
     xtime=Xtime_last
  endif
  
  
; coefficients for the long-term decrease of the LW channel:
 coeff1=[1.0326230,  -5.2495791e-09,   1.2055185e-17]
 
; decrease
lw_corr=poly(Xtime - Xtime_ref, coeff1)  


; print, 'EIS LW long-term decrease: ', lw_corr
 
; apply the long-term decrease to the LW channel:
 effa_lw=lw_corr*effa_lw
 
 
; calculate the correction factor for  radiances previously calculated 
; 
 
 if n1 gt 0 then corrections[ind1]=$
    interpol(reform(eff_sw[1, *]), reform(eff_sw[0, *]),wavelengths[ind1],/spline)/$
    interpol(effa_sw, reform(eff_sw[0, *]),wavelengths[ind1],/spline )
    
 if n2 gt 0 then corrections[ind2]=$
    interpol(reform(eff_lw[1, *]), reform(eff_lw[0, *]),wavelengths[ind2],/spline)/$
    interpol(effa_lw, reform(eff_lw[0, *]),wavelengths[ind2],/spline )


;
; For about 2 years, the recommended calibration option for eis_prep
; was to use the keyword /correct_sensitivity. This applied an
; exponential sensitivity decay to all lines, with a decay time of
; 1894 days. The /undecay keyword allows this old correction to be
; undone. 
;
; PRY, 12-Mar-2024
; I changed xtime_last to xtime_save. 
; /undecay was not being correctly applied because xtime_last was not the
; observation time, but the time at which gthe Del Zanna calibration
; ceases to be time-sensitive.
; The results are now consistent with eis_recalibrate_intensity.
;
IF keyword_set(undecay) THEN BEGIN
  tref_tai=anytim2tai('22-sep-2006 21:36')
  dt_days=(xtime_save-tref_tai)/86400.
  decay_factor=exp(-dt_days/1894.0)
  IF NOT keyword_set(quiet) THEN print,'% EIS_TLDS: the output correction factor applies to data that has previously been corrected with a universal exponential decay of 1894 days.'
 ;
  corrections=corrections*decay_factor
ENDIF 


return,corrections
 
end
