;+
; NAME:
;       EIS_CAL_MKCALDATA
;
; PURPOSE:
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;
;       eis_cal_mkcaldata
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;
;       ccdtemp: If set, the CCD temperature will be written in the IDL saveset. 
;                If not set, it will have a default value of -273 deg C.
;
;       dc: set this (,/dc) to indicate dark currrent data
;
;       ff: set this (,/ff) to indicate flat field data
;
; Outputs:
;       An idl saveset with full CCD calibration data (DC or FF) for both CCDs. Stored as idl saveset.
;
; CALLS:
;
;
; COMMON BLOCKS:
;
; PROCEDURE:
;
;        Opens upper frame dataset and lower frame dataset. Stores both i one variable for each 
;        CCD, e.g. ccda[1024, 2048] and ccdb[1024, 2048] and saves as IDL saveset. The user makes the 
;        filename.
;
; RESTRICTIONS:
;
;         Datasets to be opened must be stored as an EIS FITS file.
;
; MODIFICATION HISTORY:
;       22-Jun-2006: Oivind Wikstol. Version 1.0
;       30-Apr-2009: A. Gardini      Fixed 1 pixel error shift.
;-
pro eis_cal_mkcaldata, ccdtemp = ccdtemp, dc = dc, ff = ff
;
; set up default search directory:
;
@environment
; 
; select cal files
  title = 'Select Calibration FITS files to read'
  path = eis_cal_data + dirsep
  if keyword_set(dc) then path = path + 'dc'
  if keyword_set(ff) then path = path + 'ff'
  calfile =  dialog_pickfile(path = path, /multiple_files, /read, filter = '*.fits',  title = title)
;
; read them and store in variables ccda and ccdb
;
  data_obj = obj_new('eis_data', calfile[0], datasource='fits')
  ccd_sz = data_obj->  getccd_sz()
  ccd_xsz = ccd_sz[0]/2
  ccd_ysz = ccd_sz[1]
  ccda =  fltarr(ccd_xsz, ccd_ysz)
  ccdb =  fltarr(ccd_xsz, ccd_ysz)
  obj_destroy,  data_obj

  for i = 0, n_elements(calfile)-1 do begin
    data_obj = obj_new('eis_data', calfile[i], datasource='fits')
    nwin = data_obj-> getnwin()
    for iwin=0,nwin-1 do begin
      data_obj->getwin, iwin, wd, pos
      if pos[0] gt 2147 then begin
        pos[0] = pos[0]-2148
        ccda(pos[0]:pos[0]+pos[1]-1,pos[2]:pos[2]+pos[3]-1)=wd(*, *, 0)
      endif else begin
        ccdb(pos[0]:pos[0]+pos[1]-1,pos[2]:pos[2]+pos[3]-1)=wd(*, *, 0)
      endelse
    endfor
    obj_destroy,  data_obj
  endfor

  ; set CCD temperature
  if n_elements(ccdtemp) ne 0 then ccdtemp = ccdtemp else ccdtemp = -273

  ; set default file name and title offile dialog
  title = 'Select save file name [eis_cal_dc/ff_yyyy-mm-dd.idlsave]'
  tpos = strpos(calfile[0], '_', /reverse_search) ; '_' should be replaced by 'T'
  date = strmid(calfile[0], tpos-8, 8) ; should be tpos-10,10
  fstart = 'eis_cal'
  if keyword_set(dc) then fstart = fstart+'_dc_'
  if keyword_set(ff) then fstart = fstart+'_ff_'

  file_suggestion = fstart + strtrim(date, 2)
;  select savefile
  savefile = dialog_pickfile(path = path, default_extension = 'idlsave',  $
                             file = file_suggestion, title = title)

; save data:
  save, ccda, ccdb, ccdtemp, date, filename = savefile

  
  return
end

