

FUNCTION eis_slot_calib_factor, windata=windata, map=map, wavelength=wavelength, $
                                exptime=exptime, calib=calib, iexp=iexp


;+
; NAME:
;     EIS_SLOT_CALIB_FACTOR
;
; PURPOSE:
;     Computes the conversion factor from DN to erg/cm2/s/sr for EIS
;     slot data. It is intended to be used for slot data that has been
;     prepped with the /noabs keyword. The user can then use this
;     routine to compute the calibration factor.
;
; CATEGORY:
;     Hinode; EIS; slot calibration.
;
; CALLING SEQUENCE:
;     Result = EIS_SLOT_CALIB_FACTOR( )
;
; INPUTS:
;     None.
;
; OPTIONAL INPUTS:
;     Wavelength: Wavelength to be used for computing effective
;                 area. If not specified, then it will be taken from
;                 WINDATA or MAP.
;     Windata:   A structure in the format returned by EIS_GETWINDATA
;                or EIS_READSLOT.
;     Map:       An IDL map structure returned by the routine
;                EIS_SLOT_MAP.
;     Iexp:      Index specifying which exposure to use (only valid
;                for WINDATA).
;	
; OUTPUTS:
;     Returns a scalar that is the conversion factor from DN to
;     erg/cm2/s/sr. If a problem is found then a value of -1 is returned.
;
; EXAMPLE:
;     There are three ways of calling routine.
;
;     IDL> f=eis_slot_calib_factor(wavelength=195.12,exptime=10.)
;
;     IDL> f=eis_slot_calib_factor(windata=windata)
;
;     IDL> f=eis_slot_calib_factor(map=map)
;
; MODIFICATION HISTORY:
;     Ver.1, 23-Aug-2021, Peter Young
;     Ver.2, 27-Aug-2021, Peter Young
;        Added iexp= input; added extra comments and expanded header.
;-



IF n_tags(windata) NE 0 THEN BEGIN
   cal_abs=windata.hdr.cal_abs
   IF cal_abs EQ 1 THEN BEGIN
      print,'% EIS_SLOT_CALIB_FACTOR: the level-1 file has been calibrated. Please re-generate the file with the /noabs keyword applied. Returning...'
      return,-1
   ENDIF
   eis_get_exposure_info,windata.hdr.rast_id,exp_times=exptime
   nexp_prp=windata.hdr.nexp_prp
   IF nexp_prp GT 1 THEN BEGIN
      IF n_elements(iexp) EQ 0 THEN BEGIN 
         print,'% EIS_SLOT_CALIB_FACTOR: there are '+trim(nexp_prp)+' exposure times. Please select the correct one'
         print,'                         with the IEXP= optional input.'
         return,-1
      ENDIF ELSE BEGIN
         print,format='("% EIS_SLOT_CALIB_FACTOR: selected exposure time is ",f5.1," seconds.")',exptime[iexp]
         exptime=exptime[iexp]
      ENDELSE
   ENDIF 
  ;
   wvl=mean(windata.wvl)
ENDIF

;
; The map needs to be created with eis_slot_map otherwise it may not
; have the wavelength tag.
;
IF n_tags(map) NE 0 THEN BEGIN
   exptime=map.dur
   wvl=map.wavelength
   nexp_prp=1
ENDIF

IF n_elements(wavelength) NE 0 THEN wvl=wavelength

IF n_elements(exptime) EQ 0 THEN BEGIN
   print,'% EIS_SLOT_CALIB_FACTOR: exposure time not provided. Using 1 second.'
   exptime=1.0
ENDIF 

IF n_elements(wavelength) EQ 0 THEN BEGIN
   print,'% EIS_SLOT_CALIB_FACTOR: please specify a wavelength with the keyword WAVELENGTH=. Returning...'
   return,-1.
ENDIF 

print,format='("% EIS_SLOT_CALIB_FACTOR: wavelength used for effective area is ",f6.2,".")',wvl

;
; Get effective area (depends on wavelength)
;
ea=eis_eff_area(wvl,/quiet)

;
; The following quantities are all fixed and the values here are those
; used by eis_prep.
;
sr_factor=(725./1.496e8)^2
eis_cal = obj_new('eis_cal',/quiet)
gain=eis_cal->getgain()
obj_destroy,eis_cal
slitsize=1.0   ; i.e., pixels are 1 arcsec^2
ergs_to_photons=6.626e-27*2.998e10*1.e8   ; hc (in Angstrom units)

;
; First get factor that converts from DN to photons
; (see EIS Software Note 1)
;
phot_factor=gain*wvl*3.65/12398.5

;
; Next get factor to convert from photons to ergs/cm2/s/sr
;
erg_factor=ergs_to_photons/ea[0]/wvl/sr_factor/slitsize/exptime

return,erg_factor*phot_factor

END
