;+
; NAME:
;       EIS_GETDATA
;
; PURPOSE:
;       EIS_GETDATA is a script to extract data (one, sevceral or all
;       line windows) from a (FITS or CCSDS) data file. 
;
; CATEGORY:
;       Hansteen/Wikstol EIS data analysis SW
;
; CALLING SEQUENCE:
;       eis_getdata, file, wd, (iwin = iwin, /all,hdr = hdr ,/ccsds)
;
; INPUTS:
;       file:  Name of file (string) containing EIS data (and
;              headers). CCSDS or FITS file. FITS is default.
;
; KEYWORD PARAMETERS:
;       iwin: The line number indexes (starting with 0, max 24.)
;       all:  Set this to get all line windows.
;       hdr: set this keyword to get hdr information
;       ccsds: set this keyword if telemetry file. Standard is fits.
;
; OUTPUTS:
;       wd: The line window data (wd[lambda, slit, raster])
;       hdr: The FITS/MD (EIS Missio Data header) - optionally
;
; CALLS:
;       EIS_DATA__READ and eis_data methods. Optionally also eis_hdr methods.
;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;	example 1: Get line window 0 and hdr from CCSDS file1= 'file1.dat':
;                  IDL> eis_getdata, file1, wd, hdr=hdr, /ccsds
;                  If e.g 32 pixel wide line window *wavelength, 
;                  512 slit pos. and 100 raster pos, the dimension of 
;                  wd will now be: wd = uint[32, 512, 100]
;       example 2: Get line windows 2 and 3 from fitsfile file2='file2.fits':
;                  IDL> eis_getdata, file2, wd, iwin=[2,3]
;                  If same dimensions as above, wd will now be:
;                   wd = uintarr[32, 512, 100, 2], i.e. 
;                   wd[nlam, nslit, nraster, nwin]
;       example 3: Get all 25 line windows and hdr from FITS file 'file2.fits'
;                  IDL>eis_getdata, file2, hdr=hdr, /all
;                  wd is now uintarr[32, 512, 100, 25]
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       01-Mar-2004: Oivind Wikstol.
;       18-Oct-2004: Oivind Wikstol. Added hdr and iwin keywords.
;       28-Oct-2004: Oivind Wikstol. Added documentation.
;	08-Nov-2006: Oivind Wikstol - Added aux_data keyword
;-
pro eis_getdata, file, wd, iwin = iwin, all = all, hdr = hdr, ccsds = ccsds, $
                 aux_data = aux_data, exp = exp, data=data

  if n_params() lt 2 then begin
    print, 'eis_getdata, file, wd, (iwin = iwin, /all, hdr=hdr ,/ccsds, exp=exp, aux_data=aux_data)'
    return
  endif

  if keyword_set(ccsds) then datasource = 'ccsds' else datasource = 'fits'

; create data object:
  data = obj_new('eis_data', file, datasource = datasource)
  ; check how many line windows there are in this data set:
  nwin = data-> getnwin()
  xw = data->getxw()  ; number of wavelength pixels
  yw = data->getyw()  ; number of slit positions
  nexp = data->getnexp()
  ; read all lines
  if keyword_set(all) then iwin = indgen(nwin)
  
  ; if both iwin and all is NOT set: 
  if n_elements(iwin) eq 0 then begin
    print, 'eis_getdata: No lines specified. Reading line 0'
    iwin=0
  endif
  
  ; print error if iwin gt nwin:
  if max(iwin) gt nwin-1 then begin
    print, 'eis_getdata: only '+strtrim(nwin, 2)+' line(s) in this file'
    return
  endif

  if min(iwin) lt 0 then begin
    print, 'eis_getdata: minimum value for iwin is 0'
    return
  endif


; extract data:
  xnwin = n_elements(iwin)
  nlam = max(xw[iwin])
  nslit = yw[0]
  wd = uintarr(nlam,nslit,nexp,xnwin)

  for i = 0, xnwin-1 do begin
    line = iwin[i]
    d = data-> getvar(line)
    wd[0:(size(d))[1]-1,*,*,i] = d
  endfor

  wd = reform(wd)
  ; return hdr if hdr keyword set:
    if datasource eq 'fits' then begin
      exp = data->getexp()
      aux_data = data->getaux_data()
      hdrobj = data->gethdr()
      hdr = hdrobj-> getbte_hdr()
    endif else begin
      hdrobj = data-> gethdr()
      hdrobj-> display,hdr
    endelse

  return
end
