
;+ 
; A routine that returns which band (SW or LW) the input wavelength
; belongs to. An empty string is returned if the input wavelength is
; not observed by EIS.
;
; IDL> print,eis_get_band(195)
; SW
;
;-
function eis_get_band,wave

  band   = ''
  sw_min = 165
  sw_max = 213
  pShort = (wave-sw_min)*(sw_max-wave)
  if pShort ge 0 then band = 'SW'
  lw_min = 245
  lw_max = 292
  pLong  = (wave-lw_min)*(lw_max-wave)
  if pLong ge 0 then band = 'LW'

return,band
end

;; ###################################################################

;+ 
; A routine that returns the NRL modified EIS effective area for a
; given observation time and wavelength. The result is in square
; cm. If the input wavelength is not observed by EIS then 0.0 is
; returned.
;
; For details on the correction see
;   http://adsabs.harvard.edu/abs/2014ApJS..213...11W
;
; An example,
;
; IDL> print,eis_ea_nrl('01-JAN-2012',195)
;      0.334727
;
; Using the "short" or "long" keywords returns the effective area
; curves on a default wavelength grid. For example,
;
; IDL> eaSW = eis_ea_nrl('01-JAN-2012',waveSW,/short)
; IDL> plot,waveSW,eaSW
;
; :History:
;   27-AUG-2013: HPW : Documented version.
;   16-FEB-2016: HPW : Updated URL, added v1.3 coefficients, these are the coefficients in the
;                      paper. They differ from v1.2 <= 3%.
;
; $Id: eis_ea_nrl.pro 5926 2016-02-16 16:35:16Z hwarren $:
;-
function eis_ea_nrl,date,wave,short=short,long=long

  common eis_ea_nrl_com,init,eis
  if n_elements(init) eq 0 then init = 1

  if init then begin
    null = find_with_def('eis_ea_nrl.pro',!path)
    break_file,null,dir,dir,name,ext
    file = file_search(dir,'nrl_ea_coeff_v*.genx',count=nFiles)
    file = file[nFiles-1]
    rd_genx,file,eis
    box_message,['USING      : '+file,'TIME STAMP : '+eis.time_stamp]
    init = 0
  endif

  ;; -----------------------------------------------------------------
  ;; --- compute the knots for this time

  if n_elements(date) eq 0 then begin
    t = 0.0
  endif else begin
    t = (anytim(date) - anytim(eis.t0))/(86400.*365.25)
  endelse

  ea_knots_SW = eis.a0_SW*exp(-t/eis.tau_SW)
  ea_knots_LW = eis.a0_LW*exp(-t/eis.tau_LW)

  ;; -----------------------------------------------------------------
  ;; --- return the effective area on a default wavelength grid

  if keyword_set(short) then wave = eis.wave_area_SW
  if keyword_set(long)  then wave = eis.wave_area_LW

  ;; -----------------------------------------------------------------
  ;; --- spline onto the input wavelength grid

  nWave  = n_elements(wave) 
  ea_out = fltarr(nWave)

  for i=0,nWave-1 do begin

    band = eis_get_band(wave[i])
    case band of
      'SW': begin
        w = eis.wave_knots_SW
        e = alog(ea_knots_SW)
        s = 1
      end
      'LW': begin
        w = eis.wave_knots_LW
        e = alog(ea_knots_LW)
        s = 1
      end
      else: begin
        message,'WAVELEGNTH OUT OF BOUNDS '+trim(wave[i]),/info
        s = 0
      end
    endcase

    if s eq 1 then ea_out[i] = exp(spline(w,e,wave[i])) else ea_out[i] = 0.0

  endfor

  if nWave eq 1 then ea_out = ea_out[0]

return,ea_out
end
