
FUNCTION eis_eff_area, lamb, wvl=wvl, ea=ea, plot=plot, quiet=quiet, $
                       continuum=continuum


;+
; NAME:
;     EIS_EFF_AREA
;
; PURPOSE:
;     Extracts the EIS effective area at the specified wavelength(s).
;
; CATEGORY:
;     Hinode; EIS; effective area.
;
; CALLING SEQUENCE:
;     Result = EIS_EFF_AREA( Lamb )
;
; INPUTS:
;     Lamb:   Wavelength (angstroms) for which effective area required.
;
; KEYWORD PARAMETERS:
;     PLOT:  Does not do anything.
;     QUIET:  If set, then information messages are suppressed.
;     CONTINUUM: If set, then use the continuum-derived effective area
;                curves from Young & Mondal (2026).
;
; OUTPUTS:
;     Returns an array of same size as LAMB, giving the effective area in
;     units cm^2 for the wavelengths specified in LAMB. For wavelengths
;     outside the EIS wavelength ranges, the effective area is set to zero.
;     Note that the effective area from the original laboratory calibration
;     of EIS is returned.
;
; OPTIONAL OUTPUTS:
;     Wvl:  The wavelengths at which the effective area curve is tabulated.
;     Ea:   The effective area curve.
;
; EXAMPLE:
;     IDL> wvl=findgen(41)+170.
;     IDL> ea=eis_eff_area(wvl)
;
; MODIFICATION HISTORY:
;     Ver.1, 17-Mar-03, Peter Young
;     Ver.2, 9-May-03, Peter Young
;          added WVL and EA outputs
;     Ver.3, 15-Nov-03, Peter Young
;          updated to find the latest effective area file
;     Ver.4, 28-May-06, Peter Young
;          added /quiet keyword
;     Ver.5, 4-Dec-06, Peter Young
;          now takes arrays of wavelengths as input
;     Ver.6, 11-Dec-06, Peter Young
;          I've modified the format of the effective area file name.
;     Ver.7, 17-Feb-2012, Peter Young
;          now uses $EIS_RESPONSE envrionment variable.
;     Ver.8, 07-Jul-2022, Peter Young
;          Prints a warning if $EIS_RESPONSE is not defined. Effective area
;          set to zero if no effective area files are found. Updated header.
;          Removed code for /plot keyword.
;     Ver.9, 14-Aug-2025, Peter Young
;          Now performs interpolation of the logarithm of the EA curve.
;     Ver.10, 27-Apr-2026, Peter Young
;          Added /continuum keyword.
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use: IDL> ea=eis_eff_area(lamb, wvl=, ea=, /quiet, /continuum)'
  return,-1.
ENDIF

dir=getenv('EIS_RESPONSE')

IF dir EQ '' THEN BEGIN
  message,/info,/cont,'Warning: The environment variable $EIS_RESPONSE is not set. There may be a problem with your Solarsoft installation.'
ENDIF 


nl=n_elements(lamb)
out_eff=fltarr(nl)

i_b=where(lamb GE 160. AND lamb LE 215.,n_b)
i_a=where(lamb GE 235. AND lamb LE 295.,n_a)

;
; Get detector B file (SW channel)
;
IF n_b GT 0 THEN BEGIN 
  IF keyword_set(continuum) THEN BEGIN
    search_dir=concat_dir(dir,'continuum')
    file_b=concat_dir(search_dir,'eis_eff_area_cont_20240930_sw.txt')
  ENDIF ELSE BEGIN
    list=file_search(dir,'EIS_*Area_B.*',count=n)
    IF n EQ 0 THEN BEGIN
      message,/cont,/info,'No effective area files found for channel-A! Effective area set to zero.'
      file_b=''
    ENDIF ELSE BEGIN 
      ext=intarr(n)
      FOR i=0,n-1 DO BEGIN
        bits=str_sep(list[i],'.')
        ext[i]=fix(bits[1])
      ENDFOR
      i=where(ext EQ max(ext))
      file_b=list[i]
    ENDELSE 
  ENDELSE
;
  IF file_b NE '' THEN BEGIN
    IF NOT keyword_set(quiet) THEN message,/info,/cont,'Effective area file for SW channel: '+file_basename(file_b)
 ;
    str1=''
    wvl=0. & ea=0.
    openr,lun,file_b,/get_lun
    WHILE eof(lun) NE 1 DO BEGIN
      readf,lun,str1
      IF strpos(str1,'#') LT 0 THEN BEGIN
        reads,str1,format='(2f13.0)',a,b
        wvl=[wvl,a]
        ea=[ea,b]
      ENDIF
    ENDWHILE
    free_lun,lun

    k=where(ea GT 0.)
    wvl=wvl[k]
    ea=ea[k]

    y2=spl_init(wvl,alog10(ea))
    result=spl_interp(wvl,alog10(ea),y2,lamb[i_b])
    out_eff[i_b]=10.^result
  ENDIF
ENDIF 

;
; Get detector A file (LW channel)
;
IF n_a GT 0 THEN BEGIN 
  IF keyword_set(continuum) THEN BEGIN
    search_dir=concat_dir(dir,'continuum')
    file_a=concat_dir(search_dir,'eis_eff_area_cont_20240930_lw.txt')
  ENDIF ELSE BEGIN
    list=file_search(dir,'EIS_*Area_A.*',count=n)
    IF n EQ 0 THEN BEGIN
      message,/cont,/info,'No effective area files found for channel-A! Effective area set to zero.'
      file_a=''
    ENDIF ELSE BEGIN 
      ext=intarr(n)
      FOR i=0,n-1 DO BEGIN
        bits=str_sep(list[i],'.')
        ext[i]=fix(bits[1])
      ENDFOR
      i=where(ext EQ max(ext))
      file_a=list[i]
    ENDELSE 
  ENDELSE
;
  IF file_a NE '' THEN BEGIN
    IF NOT keyword_set(quiet) THEN message,/info,/cont,'Effective area file for LW channel: '+file_basename(file_a)
 ;
    str1=''
    wvl=0. & ea=0.
    openr,lun,file_a,/get_lun
    WHILE eof(lun) NE 1 DO BEGIN
      readf,lun,str1
      IF strpos(str1,'#') LT 0 THEN BEGIN
        reads,str1,format='(2f13.0)',a,b
        wvl=[wvl,a]
        ea=[ea,b]
      ENDIF
    ENDWHILE
    free_lun,lun
    
    k=where(ea GT 0.)
    wvl=wvl[k]
    ea=ea[k]

    y2=spl_init(wvl,alog10(ea))
    result=spl_interp(wvl,alog10(ea),y2,lamb[i_a])
    out_eff[i_a]=10.^result
  ENDIF
ENDIF 

return,out_eff

END
