;+
; NAME:
;       EIS_CAL_FF
;
; PURPOSE:
;	EIS_CAL_FF performes flat field correction on Solar-B EIS data.
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;
; INPUTS:
;	dobj: Data object of data to be flat field corrected.
;
; KEYWORD PARAMETERS:
;	ff_file: Flat field name (string), with full directory name.
;
; OUTPUTS:
;
; CALLS:
;
; COMMON BLOCKS:
;
; PROCEDURE:
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
;       08-Sep-2005: Oivind Wikstol - Version 1.0
;
;-
pro eis_cal_ff, dobj, ff_file = ff_file
  if n_params() lt 1 then begin
    print, 'eis_cal_ff, dobj, ff_file = ff_file'
    return
  endif

  if n_elements(ff_file) eq 0 then ff_file = dialog_pickfile() 

; make dark current object:
  ff_obj= obj_new('eis_data', ff_file, datasource='fits',hdr=hdr)

  ccd_sz=data_obj->getccd_sz()
  det_ff = fltarr(ccd_sz[0], ccd_sz[1])

  ; make detector for dark current
  nwin = ff_obj->getnwin()
  for i=0,nwin-1 do begin
   ff_obj->getwin, i, wd, pos
   det_ff[pos[0]:pos[0]+pos[1]-1,pos[2]:pos[2]+pos[3]-1]=reform(wd[*, *, 0])
  endfor

  ; for each exposure and line window, correct for dark current

  nwin = data_obj->getnwin()
  nexp = data_obj->getnexp()
  for j = 0, nexp - 1 do begin
    for i = 0, nwin-1 do begin
      data_obj->getwin, i, wd, pos
      x1 = pos[0]
      x2 = pos[0]+pos[1]-1
      y1 = pos[2]
      y2 = pos[2]+pos[3]-1
      wd[*, *, j] = (wd[*, *, j]/det_ff[x1:x2, y1:y2]) > 0
      data_obj->setvar, wd, i
    endfor
  endfor
  data_obj->setcalstat, 'ff'  ; set calibration status to 'true' for ff
end
