;+
; NAME:
;       FITS_HDR__READ
;
; PURPOSE:
;       FITS_HDR__READ reads EIS FITS headers and stores information
;       in the object fields of an FITS_HDR class object. 
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;       An object of class FITS_HDR is declared with this statement: 
;                   hdrobj = obj_new('fits_hdr')
;
;       FITS_HDR__READ reads EIS FITS headers and stores information
;       into the object, with this statement:
;                     hdrobj-> read, filename
;
;       If called from within an fits_hdr procedure, the
;       FITS_HDR__READ statement looks like this:
;                     self-> read, file
;
; INPUTS:
;       file:  Name of FITS file (string) containing EIS FITS data and headers.
;
; KEYWORD PARAMETERS:
;
; OUTPUTS:
;       Reads EIS FITS files and fills the parameters of the FITS_HDR
;       object with information.
;
; CALLS:
;       headfits, readfits, tbget
;
; COMMON BLOCKS:
;       
;
; PROCEDURE:
;       
;       
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       22-Jan-2003: Oivind Wikstol.
;       16-Feb-2004: Oivind Wikstol - Added main header and bte header read
;       31-OCT-2006: Harry Warren   - Modified to read compressed fits files
;        4-Jul-2008: A. Gardini     - Added the init keyword.
; 
; $Id: fits_hdr__read.pro 405 2010-07-04 11:21:38Z viggoh $
;
;-
pro fits_hdr::read, file, init=init
  if n_elements(init) eq 1 then return
;  catch,error
;  if error ne 0 then begin 
;    catch,/cancel
;    message = dialog_message(!error_state.msg, /error)
;    return
;  endif

  if n_params() ne 1 then begin
    message, 'No datafile specified!', /informational
    message, 'usage: data->read, file', /informational
    return
  endif

  ;; check file size (HPW 31-OCT-2006)
  fileinfo = file_info(file)
  if fileinfo.size eq 0 then begin
    print, 'File is empty!'
    return
  endif
  
  ;; read main header, bte header, and bte data; compressed files are
  ;; handled by these routines (HPW 31-OCT-2006)
  main_hdr = headfits(file)
  bte_data = readfits(file,bte_hdr,exten_no=1,/silent)

  ; extract main and bte headers:
  main_npar = n_elements(main_hdr)
  bte_npar = n_elements(bte_hdr)
  self.main_hdr = ptr_new(strarr(main_npar))
  self.bte_hdr = ptr_new(strarr(bte_npar))
  *self.main_hdr = main_hdr
  *self.bte_hdr = bte_hdr
  hdr = bte_hdr

  ; extract information from fits hdr:
  self.date = fxpar(hdr, 'date')         ; Date FITS file was written
  self.origin = fxpar(hdr, 'origin')     ; Institue where file was created
  self.telescop = fxpar(hdr, 'telescop') ; Solar B
  self.instrume = fxpar(hdr, 'instrume') ; EIS
  self.obs_prog = fxpar(hdr, 'obs_prog') ; Observing program name
  self.obs_title = fxpar(hdr, 'title')   ; Observing program title
  self.datatype = fxpar(hdr, 'datatype')   ; Data type ['pixel data' or 'moments']
  self.date_obs = fxpar(hdr, 'date_obs') ; Start date and time of observation
  self.date_end = fxpar(hdr, 'date_end') ; End date and time of observation
  self.obt_time = fxpar(hdr, 'obt_time') ; Spacecraft start time (TI units)
  self.xcen = fxpar(hdr, 'xcen')         ; Preliminary center pointing in x
  self.ycen = fxpar(hdr, 'ycen')         ; Preliminary center pointing in y
  self.ccd_nr = fxpar(hdr, 'ccdron*')     ; CCD number for each line window
  self.rast_acr = fxpar(hdr, 'rast_acr') ; Raster acronym
  self.stud_acr = fxpar(hdr, 'stud_acr') ; Study Acronym
  ; get line id and wavelengths for each line window
  nwin = fxpar(hdr, 'nwin')    ; number of line windows
  ttype = fxpar(hdr, 'ttype*')
  twave = fxpar(hdr, 'twave*')
  self.line_id = ttype[0:nwin-1]
  self.line_wvl = twave[0:nwin-1]
  ; define line_id

  nwin = fxpar(hdr, 'nwin')
  nexp = fxpar(hdr, 'naxis2')

  ; auxiliary BTE column data, such as timing and pointing:
  self.t1 = ptr_new(ulonarr(nexp))
  self.t2 = ptr_new(ulonarr(nexp))
  self.mhc_dur = ptr_new(ulonarr(nexp))
  self.exp_dur = ptr_new(uintarr(nexp))
  self.fmirr = ptr_new(uintarr(nexp))
  self.hsl = ptr_new(uintarr(nexp))
  self.xrt_flfl = ptr_new(bytarr(nexp))
  self.xrt_fl_x = ptr_new(bytarr(nexp))
  self.xrt_fl_y = ptr_new(bytarr(nexp))
  self.aec_hepc = ptr_new(ulonarr(nexp))
  self.aec_lepc = ptr_new(ulonarr(nexp))
  self.mhcsg = ptr_new(uintarr(nexp))
  ;; extract data from bte using tbget (HPW 31-OCT-2006)
  *self.t1       = tbget(bte_hdr,bte_data,'TI_1')
  *self.t2       = tbget(bte_hdr,bte_data,'TI_2')
  *self.mhc_dur   = tbget(bte_hdr,bte_data,'MHC_DUR')
  *self.exp_dur   = tbget(bte_hdr,bte_data,'EXP_DUR')
  *self.fmirr    = tbget(bte_hdr,bte_data,'FMIRR')
  *self.hsl      = tbget(bte_hdr,bte_data,'HSLSTAT')
  *self.xrt_flfl = tbget(bte_hdr,bte_data,'XRT_FLFL')
  *self.xrt_fl_x = tbget(bte_hdr,bte_data,'XRT_FL_X')
  *self.xrt_fl_y = tbget(bte_hdr,bte_data,'XRT_FL_Y')
  *self.aec_hepc = tbget(bte_hdr,bte_data,'AEC_HEPC')
  *self.aec_lepc = tbget(bte_hdr,bte_data,'AEC_LEPC')
  *self.mhcsg    = tbget(bte_hdr,bte_data,'MHCFMSGO')

end

