;+
; NAME:
;       EIS_CAL_ABSCAL
;
; PURPOSE:
;	EIS_CAL_ABSCAL performes absolute intensity calibration of Solar-B EIS data. 
;	Intensity is converted from Data Numbers (DN) to physical units, in:
;	photons: [ph cm-2 s-1 sr-1] or
;       cgs    : [erg cm-2 s-1 sr-1] or 
;	SI     : [J m-2 s-1 sr-1] or 
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;
; OUTPUTS:
;
; CALLS:
;
; COMMON BLOCKS:
;
; PROCEDURE:
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
;       01-Sep-2005: Oivind Wikstol - Version 1.0
;       30-Apr-2009: A. Gardini Set keywords to eis_cal.
;
;-
pro eis_cal_abscal, dobj
; open calibration object:
  time_obs=(dobj->gethdr())->getdate_obs()
  slit_ind=dobj->getinfo('slit_ind')
  calobj = obj_new('eis_cal',time_obs=time_obs,slit_ind=slit_ind)

; get effective areas, and various factors needed:
  aeff = calobj-> get_eff_area()
  sr_factor = calobj-> getsr_factor()
  ev_threshold = calobj-> getev_threshold()
  gain = calobj-> getgain()
  offset = calobj -> getoffset()

  fac1 = gain*ev_threshold/12398.5/sr_factor
  fac2 = offset/sr_factor

  ; set up different parameters needed from data object
  nwin = d_obj-> getnwin()
  nexp = d_obj-> getnexp()
  hdrobj = dobj->gethdr()
  bte_hdr = hdrobj-> getbte_hdr()
  lam_struct = dobj-> getlambda()

  for iexp = 0, nexp -1 do begin
    for iwin = 0, nwin -1 do begin
      ; get wavelength band of window (A or B)
      par = 'TWBND'+ strtrim(string(iwin),2)
      wband = fxpar(bte_hdr, par)
      par = 'TDETX'+ strtrim(string(iwin),2)
      xstart = fxpar(bte_hdr, par)
      par = 'TDETXW'+ strtrim(string(iwin),2)
      xwidth = fxpar(bte_hdr, par)
      case wband of 
        'A': begin
          area_eff = aeff.area_a
          wave_eff = aeff.wave_a
          wavelength = lam_struct.scale_a
        end
        'B': begin
          area_eff = aeff.area_b
          wave_eff = aeff.wave_b
          wavelength = lam_struct.scale_b
        end
        else: begin
          message = 'Wavelength band must be A or B. Error in FITS file - No calibration performed'
          ok = dialog_message(message)
          return
        end
      endcase
    
      :read data
      dobj->getwin, iwin, wd, pos
    
      ; interpolate effective areas to get values at correct wavelengths:
      aeff_intp = spline(wave_eff, area_eff, wavelengt[xstart:xstart+xwidth-1])

      phi_lam = wd  ; get array the same size as wd (nlambda, nslit)
      for ilam = 0, xwidth -1 do begin
        phi_lam[ilam, *] = 1./aeff_intp*([ilam]*wd[ilam, *]*wavelength[xstart+ilam]*fac1 - fac2)
      endfor
      dobj-> setvar, phi_lam, iwin
    endfor
  endfor

end

