;+
; NAME:
;       EIS_DATA__DEFINE
;
; PURPOSE:
;       EIS_DATA__DEFINE defines the class EIS_DATA. Objects of this
;       class contains data from the EIS instrument on SOLAR-B. The
;       EIS_DATA class inherits the superclass HW_DATA
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;       The EIS_DATA__DEFINE procedure is not called directly. An
;       object of class EIS_DATA is created with the following
;       statement:
;                   eis_data = obj_new('eis_data')
;       To fill the object with information (see HW_DATA__DEFINE for
;       information about the contents of the object), use the
;       following statement:
;                     eis_data-> read, filename [, eis_hdr = eis_hdr]
;
;       One may also create the object and fill it with information
;       with one statement:
;                     eis_data = obj_new('eis_data', filename).
;       In this case the init-procedure of eis_data__define will be
;       run, this in turn calls EIS_DATA__READ
;
;
; INPUTS:
;       file:  Name of file (string) containing EIS data (and
;       headers).
;       Note: Giving filename as input when declaring the EIS_DATA
;       object is optional. Filename can also be sent to EIS_DATA__READ.
;
; KEYWORD PARAMETERS:
;       eis_hdr: Set this keyword to obtain also the header
;       information in the form of an object of type EIS_HDR.
;
; OUTPUTS:
;       Objects of type EIS_DATA (and optionally EIS_HDR)
;
; CALLS:
;       EIS_DATA__READ;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;       The procedure opens an object of class EIS_DATA. The EIS_DATA
;       class inherits the superclass HW_DATA. If filename is given as
;       input when declaring the object, then EIS_DATA__READ is also
;       run by the init-procedure below. In that case the parameters
;       of the object is filled with information.
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       28-Mar-2001: Oivind Wikstol.
;       21-Sep-2001: Oivind Wikstol. Added documentation.
;       25-Feb-2004: Oivind Wikstol. Added structure array wd_def[nwin].
;       06-May-2004: Oivind Wikstol. Added save method
;       11-May-2004: Oivind Wikstol. Added keyword unit to init-method
;       17-Jun-2004: Oivind Wikstol. Added filename string and get
;                                    method to eis_data
;       02-July-2004:                Added dir (directory) to es_data 
;                                    (string) and get method
;       15-Feb-2005:                 Added number of exp. pr. raster pos.
;       08-Mar-2005: Oivind Wikstol. Added sit_and_stare variable object 
;                                    (0 for raster 1 for sit-and-stare mode)
;       08-Apr-2005: Oivind Wikstol. Added obsmode:
;                                    e.g. 'sit-and-stare' or 'scanning'
;       03-Jan-2006: Oivind Wikstol. Added fits_reformat structure and 
;                                    fitslev keyword
;       14-Jul-2006:                 Changd wavelength definitions - moved 
;                                    dispersion to cal object.
;	07-Nov-2006: Oivind Wikstol. Added slit_id
;       06-Feb-2008: A. Gardini    . Modified calstat fields.
;       20-May-2008: A. Gardini    . Modified calstat fields.
;       30-Apr-2009: A. Gardini    . Added keywords to eis_cal.
;       22-Oct-2019: P. Young      . Modified calstat fields.
;
;-
function eis_data::init, file, datasource=datasource, hdr = hdr,  unit = unit
  if n_elements(datasource) ne 0 then self.datasource=datasource $
  else self.datasource='fits'
  if n_elements(unit) eq 0 then unit = 'Counts/pixel'
  self.unit[0] = unit
  self.ccd_sz = [4296, 1024]
  self.home_inst = getenv('EIS_INST')  ; institute where software is run
;define wavelength scale
  time_obs=(data->gethdr())->getdate_obs()
  slit_ind=data->getinfo('slit_ind')
  cal = obj_new('eis_cal',time_obs=time_obs,slit_ind=slit_ind)
  lambda=cal->getlambda() 
  self.lambda.scale_b=lambda.scale_b
  self.lambda.scale_a=lambda.scale_a
  obj_destroy,cal
;
  if n_params() ge 1 then begin
    case self.datasource of
    'ccsds': self-> readccsds, file, hdr = hdr
    'fits': self-> readfits, file, hdr = hdr
    else: begin
      message, 'No datafile specified!', /informational
      message, 'usage: data->read, file, hdr = hdr', $
                /informational
      return,-99
         end
    endcase
  endif
  return, 1
end

pro eis_data::save,  file =  file,  plan =  plan
  case self.datasource of
    'ccsds': begin
        if n_elements(file) ne 0 then $
          eis_mkfits,  self,  (self-> gethdr()), fitsfile =  file,  plan = plan $
        else eis_mkfits,  self,  (self-> gethdr()),  plan = plan 
      end
    'fits': begin
        if n_elements(file) ne 0 then $
          eis_modfits,  self,  (self-> gethdr()),  fitsfile =  file $
        else eis_modfits,  self,  (self-> gethdr())
      end
  endcase
  return
end

function eis_data::getaux_data
  return, self.aux_data
end

function eis_data::getlambda,lamtype=lamtype
  if n_elements(lamtype) eq 0 then lamtype='wav'
  if lamtype eq 'wav' then begin
    return,self.lambda
  endif
  if lamtype eq 'pix' then begin
    pixel=self.lambda
    pixel.scale_b=findgen(2148)
    pixel.scale_a=findgen(2148)+2148
    return, pixel
  endif
end

function eis_data::getcalstat
  return, self.calstat
end

pro eis_data::setcalstat, cal, val
  case cal of 
    'dc':self.calstat.dc = val
;   'ff':self.calstat.ff = val
    'hp':self.calstat.hp = val
    'wp':self.calstat.wp = val
    'rwp':self.calstat.rwp = val
    'dp':self.calstat.dp = val
    'cr':self.calstat.cr = val
;   'wvl':self.calstat.wvl = val
    'abs':self.calstat.abs = val
    'phot':self.calstat.phot = val
    'retain':self.calstat.retain = val
   endcase
end

function eis_data::getobsmode
  return, self.obsmode
end

function eis_data::getccd_sz
  return, self.ccd_sz
end

function eis_data::getfilename
  return, self.filename
end

function eis_data::getdir
  return, self.dir
end

function eis_data::getslit_id
  return, self.slit_id
end

function eis_data::getnslit
  return, self.nslit
end

function eis_data::getwd_def
  return, self.wd_def
end

function eis_data::getnexp_prp
  return, self.nexp_prp
end

function eis_data::getsit_and_stare
  return, self.sit_and_stare
end

pro eis_data::setsit_and_stare, sit_and_stare
  self.sit_and_stare = sit_and_stare
end

pro eis_data::setline_px, iwin, var
  self.wd_def[iwin].line_px = var
  return
end

pro eis_data::setcont_px, iwin, var
  self.wd_def[iwin].cont_px = var
  return
end

function eis_data::getfitslev
  return,self.fitslev
end

pro eis_data::setfitslev, fitslev
  self.fitslev = fitslev
  return
end

function eis_data::getfits_reformat
  return, self.fits_reformat
end

pro eis_data::setfits_reformat, param, value
  case param of 
    'date_rf0':self.fits_reformat.date_rf0 = value
    'date_rf1':self.fits_reformat.date_rf1 = value
    'orig_rf0':self.fits_reformat.orig_rf0 = value
    'orig_rf1':self.fits_reformat.orig_rf1 = value
    'ver_rf0':self.fits_reformat.ver_rf0 = value
    'ver_rf1':self.fits_reformat.ver_rf1 = value
   endcase
  return
end


pro eis_data__define
  nwin = 25
  wdstruct = create_struct(name = 'wd_def',  'line_px', intarr(2), $
                           'cont_px', intarr(2))
  struct = {eis_data, $
            obsmode:'', $
            slit_id:'', $
            nslit:0, $
            nexp_prp:0, $
            sit_and_stare:0, $
            ccd_sz:intarr(2), $
            filename:'', $
            dir:'', $
            lambda:create_struct(name='lambda', $
                                    'scale_b', dblarr(2148), $
                                    'scale_a', dblarr(2148)), $
            aux_data:create_struct(name='auxdata', $
                                    'ti_1', ptr_new(), $
                                    'ti_2', ptr_new(), $
                                    'mhc_dur', ptr_new(),$ 
                                    'exp_dur', ptr_new(), $
                                    'fmirr', ptr_new(), $
                                    'hslstat', ptr_new(), $
                                    'xrt_flfl', ptr_new(), $
                                    'xrt_fl_x', ptr_new(), $
                                    'xrt_fl_y', ptr_new(), $
                                    'aec_hepc', ptr_new(), $
                                    'aec_lepc', ptr_new(), $
                                    'mhcfmsg', ptr_new()), $
            calstat:create_struct(name='calstat', $
                                      'dc', 0, $
                                      'ff', 0, $
                                      'hp', 0, $
                                      'wp', 0, $
                                      'rwp', 0, $
                                      'cr', 0, $
                                      'wvl', 0, $
                                      'abs', 0, $
                                      'phot', 0, $
                                      'retain', 0), $
            fits_reformat:create_struct(name='fits_reformat', $
                                             'date_rf0','NA', $
                                             'orig_rf0','NA', $
                                             'ver_rf0','NA', $
                                             'date_rf1','NA', $
                                             'orig_rf1','NA', $
                                             'ver_rf1','NA'), $
            fitslev:0, $
            wd_def:replicate(wdstruct, nwin), $
            inherits hw_data}
end
