
FUNCTION eis_recal_mask_spec, spec, calib

;+
; NAME:
;     EIS_RECAL_MASK_SPEC
;
; PURPOSE:
;     This routine re-calibrates a mask spectrum (created with
;     eis_mask_spectrum.pro) based on the option
;     specified by the user.
;
; CATEGORY:
;     Hinode/EIS; calibration.
;
; CALLING SEQUENCE:
;     Result = EIS_RECAL_MASK_SPEC( Spec )
;
; INPUTS:
;     Spec:    A spectrum structure created by EIS_MASK_SPECTRUM.
;     Calib:   An integer specifying which calibration to use. The
;              options are:
;               1 - Del Zanna (2013)
;               2 - Del Zanna (2013), with /undecay
;               3 - Warren et al. (2014)
;               4 - Warren et al. (2014), with /undecay
;               5 - Del Zanna et al. (2025)
;               6 - Del Zanna et al. (2025), with /undecay
;               7 - Young & Mondal (2026)
;              Check the EIS wiki for more details of these options.
;
; OUTPUTS:
;     A structure with the same format as the input SPEC is
;     returned. The .INT and .ERR tags are modified with the new
;     calibration, and the .CALIB tag is updated to indicate the value
;     selected by the user.
;
;     If a problem is found, then the value -1 is returned.
;
; EXAMPLE:
;     Re-calibrate using the calibration of Del Zanna (2013):
;
;     IDL> eis_mask_spectrum, file, mask, swspec=swspec
;     IDL> newspec=eis_recal_mask_spec(swspec,1)
;
; MODIFICATION HISTORY:
;      Ver.1, 21-Apr-2017, Peter Young
;      Ver.2, 09-May-2025, Peter Young
;        Implemented the Del Zanna et al. (2025) calibration.
;      Ver.3, 07-Nov-2025, Peter Young
;        Implemented the continuum effective area curves.
;      Ver.4, 27-Apr-2026, Peter Young
;        Now calls eis_eff_area for the calib=7 option.
;-


IF n_params() LT 2 THEN BEGIN
  print,'Use:  IDL> newspec=eis_recal_mask_spec(spec,calib)'
  print,''
  print,'The input CALIB should take one of the following values:'
  print,'         1 - Del Zanna (2013)'
  print,'         2 - Del Zanna (2013), with /undecay'
  print,'         3 - Warren et al. (2014)'
  print,'         4 - Warren et al. (2014), with /undecay'
  print,'         5 - Del Zanna et al. (2025)'
  print,'         6 - Del Zanna et al. (2025), with /undecay'
  print,'         7 - Continuum calibration (30-Sep-2024)'
  print,'where /undecay undoes the /correct_sensitivity keyword of eis_prep.'
  return,-1
ENDIF 

IF tag_exist(spec,'calib') THEN BEGIN 
  IF spec.calib NE 0 THEN BEGIN
    message,/info,/cont,'This routine only works if the CALIB tag of the input is zero. See routine header for more details. Returning...'
    return,-1
  ENDIF
ENDIF 

outspec=spec

;
; Get the calibration factor (calib_factor) as a function of wavelength.
;
CASE calib OF 
  1: calib_factor=eis_ltds(spec.date_obs,spec.wvl)
  2: calib_factor=eis_ltds(spec.date_obs,spec.wvl,/undecay)
  3: calib_factor=eis_recalibrate_intensity(spec.date_obs,spec.wvl,1)
  4: calib_factor=eis_recalibrate_intensity(spec.date_obs,spec.wvl,1,/undecay)
  5: calib_factor=eis_recalibrate_intensity_new(spec.date_obs,spec.wvl,1)
  6: calib_factor=eis_recalibrate_intensity_new(spec.date_obs,spec.wvl,1,/undecay)
  7: BEGIN
    lab_ea=eis_eff_area(spec.wvl,/quiet)
    new_ea=eis_eff_area(spec.wvl,/continuum,/quiet)
    n=n_elements(spec.wvl)
    calib_factor=fltarr(n)
    k=where(new_ea NE 0.)
    calib_factor[k]=lab_ea[k]/new_ea[k]
  END 
  ELSE: calib_factor=1.0
ENDCASE

IF tag_exist(spec,'missing') EQ 0 THEN missing=-100. ELSE missing=spec.missing

k=where(spec.int NE missing,nk)
IF nk NE 0 THEN BEGIN
  outspec.int[k]=outspec.int[k]*calib_factor[k]
  outspec.err[k]=outspec.err[k]*calib_factor[k]
ENDIF

IF tag_exist(spec,'calib') THEN BEGIN 
  outspec.calib=calib
ENDIF ELSE BEGIN
  outspec=add_tag(outspec,calib,'calib')
ENDELSE 

return,outspec

END
