;+
; NAME:
;   	EIS_DATA__GETWINDATA
;
; PURPOSE:
;   	Returns EIS data from a detector window.
;
; CALLING SEQUENCE:
;   	windata = data->getwindata(iwin [,keywords])
;
; INPUTS:
;   	IWIN: scalar with the index of the desired window.
;
; OUTPUTS:
;   	Structure with data and header information.
;
; KEYWORDS:
;   	DATA_ER: data object for the errors. Populates the ERR tag.
;
; EXAMPLE:
;   	Create an object:
;   	    IDL> eisfile = 'eis_l1_20070202_104212.fits'
;   	    IDL> eisfile = 'eis_er_20070202_104212.fits'
;   	    IDL> obj = obj_new('eis_data',eisfile)
;   	    IDL> obj_er = obj_new('eis_data',errfile)
;   	And extract data from window 0.
;   	    IDL> data = obj -> getwindata(0, data_er=obj_er)
;
; WRITTEN: H.P.Warren. NRL. March 2007.
;
; MODIFICATION HISTORY:
;       2007/04/09  I. Ugarte-Urra. NRL. Documentation, new tag definitions and 
;                                        resolving cases with multiple exposures prp.
;       2007/04/11  IUU. NRL. Redefining NX for multiple exposures prp.
;       2007/04/27  IUU. NRL. Release to SSW.
;       2007/05/01  IUU. NRL. Fixed bug for NX=1 
;       2007/09/17  Peter Young. Changed method for computing wavelength
;             vector; now uses eis_pixel_to_wavelength.
;       2008/12/04  Peter Young. Added TIME= input for call to
;             eis_pixel_to_wavelength.
;       2008/12/17  HPW. NRL. Added data quality tag. This is derived from check_ti() and
;             is not foolproof.
;
;-
;; **********************************************************************

function eis_data::getwindata,iwin,data_er=data_er


  if self.filename eq '' then begin
    message,"NO DATA READ YET",/info
    return,0
  endif
  break_file,self.filename,disk,dir,name,ext

  ;; ----------------------------------------------------------------------
  ;; --- get data and header
  ;; ----------------------------------------------------------------------

  data = float(*self.w[iwin])
  hdr  = self -> gethdr()

  nl = (size(data))[1] & ny = (size(data))[2] 
  if (size(data))[0] eq 2 then nx = 1l else nx = (size(data))[3]

  if keyword_set(data_er) then data_e = data_er->getvar(iwin) else $
      data_e = fltarr(nl,ny,nx)
      
  header = self -> getwinheader(self.filename,iwin)
  
  nexp_prp = header.nexp_prp    ;exposures per raster position

  ;; --- times for these observations
  aux_data=self->getaux_data()
  time=self->sec_from_obs_start(*(aux_data.ti_1))
  tai = self->ti2tai(hdr -> gett1())
  time_ccsds = TAI2UTC(tai,/CCSDS)

  ;; --- check for good data, this is not foolproof
  data_quality = byte(self->check_ti())

  ;; ----------------------------------------------------------------------
  ;; --- get wavelength array
  ;; ----------------------------------------------------------------------

  ccd_info = [self.xs[iwin],self.xw[iwin],self.ys[iwin],self.yw[iwin]]

  lambda = self.lambda
  xstart = ccd_info[0] 
  xwidth = ccd_info[1] 
  xend   = xstart + xwidth - 1

  if xstart ge 2148 then begin
    xstart = xstart - 2148 -50              ; PRY  17-Sep-2007
    ccd=0
;    xend   = xend - 2148                   ; PRY
;    wave   = lambda.scale_a[xstart:xend]   ; PRY
  endif else begin
    xstart=xstart-50                        ; PRY
    ccd=1
;    wave   = lambda.scale_b[xstart:xend]   ; PRY
  ENDELSE
  wave=eis_pixel_to_wavelength(xstart+indgen(xwidth),ccd,header.slit_ind,time=time_ccsds[0])

  ;; ----------------------------------------------------------------------
  ;; --- rearrange the data
  ;; ----------------------------------------------------------------------
  if nx eq 1 then begin
    data_new   = fltarr(nl,nx,ny)
    data_e_new = fltarr(nl,nx,ny)
    data_new(*,0,*)   = data
    data_e_new(*,0,*) = data_e
    data   = temporary(data_new)
    data_e = temporary(data_e_new)
  endif else begin
    data   = rearrange(data,[1,3,2])
    data_e = rearrange(data_e,[1,3,2])
  endelse

  ;; ----------------------------------------------------------------------
  ;; --- get additional info
  ;; ----------------------------------------------------------------------

  ;; --- exposure time in seconds - is this the place to get this info?
  expt = self -> getexp() 

  ;; --- id for this window
  line_id = strtrim((hdr -> getline_id())[iwin],2)

  ;; --- the slit
  slit_id = strtrim(self.slit_id,2)

  ;; --- solar X and solar Y (relative)
;  fmir_dx   = 0.1248
;  fmir_ss   = (self->getinfo('fmir_ss'))
  solar_x   = self->getxpos() ;; 2*fmir_dx*fmir_ss*findgen(nt) 
  solar_y   = self->getypos() ;; findgen(ny)

  ;; --- Units
  units = (self -> getunit())[0]

  ;; --- Other
  missing=-100    ; value for missing data

  CASE header.slit_ind OF
    0: scale=[1.0,1.0]
    2: scale=[2.0,1.0]
    ELSE: scale=[1.0,1.0]
  ENDCASE
  
  
  ;; ----------------------------------------------------------------------
  ;; --- Multiples exposures per raster position
  ;; ----------------------------------------------------------------------
  IF nexp_prp GT 1 THEN BEGIN
    ;; --- Redifining x dimension
    nx=nx/nexp_prp
    
    ;; --- Introducing 4th dimension 
    data_new	     = fltarr(nl,nx,ny,nexp_prp)
    data_e_new	     = fltarr(nl,nx,ny,nexp_prp)
    time_new	     = dblarr(nx,nexp_prp)
    time_ccsds_new   = strarr(nx,nexp_prp)
    expt_new	     = fltarr(nx,nexp_prp)
    solar_x_new      = fltarr(nx,nexp_prp)
    data_quality_new = bytarr(nx,nexp_prp)
    
    ;; --- Rearranging 
    ii=indgen(nx)*nexp_prp
    FOR i=0,nexp_prp-1 DO BEGIN
    	data_new[*,*,*,i]     = data[*,ii+i,*]
   	data_e_new[*,*,*,i]   = data_e[*,ii+i,*]
	time_new[*,i]	      = time[ii+i]
	time_ccsds_new[*,i]   = time_ccsds[ii+i]
	expt_new[*,i]	      = expt[ii+i]
	solar_x_new[*,i]      = solar_x[ii+i]
        data_quality_new[*,i] = data_quality[ii+i]
    ENDFOR

    data=0 
    data_e=0
    time=0
    time_ccsds=0
    expt=0
    solar_x=0
    data    	 = temporary(data_new)
    data_e  	 = temporary(data_e_new)
    time    	 = temporary(time_new)
    time_ccsds   = temporary(time_ccsds_new)
    expt    	 = temporary(expt_new)
    solar_x 	 = temporary(solar_x_new)
    data_quality = temporary(data_quality_new)
    
  ENDIF

  ;; ----------------------------------------------------------------------
  ;; --- output structure
  ;; ----------------------------------------------------------------------

  ;; --- a little more rearrangment
  if header.nraster gt 1 then begin
    data     = reverse(data,2)
    data_e   = reverse(data_e,2)
    ;; --- what about these?
    solar_x  	 = reverse(solar_x,1)
    time    	 = reverse(time,1)
    time_ccsds   = reverse(time_ccsds,1)
    expt     	 = reverse(expt,1)
    data_quality = reverse(data_quality,1)
  endif
 
  windata = {filename:	    	name+ext,$
    	     line_id:           line_id,$
             int:           	data,$
             err:            	data_e,$
             wvl:            	wave,$
	     time:  	    	time,$
             time_ccsds:    	time_ccsds,$
             data_quality:      data_quality,$
             exposure_time:   	expt,$
             solar_x:         	solar_x,$
             solar_y:         	solar_y,$
    	     nl:    	    	nl,$
	     nx:    	    	nx,$
	     ny:    	    	ny,$
	     scale: 	    	scale,$
             units:             units,$
	     missing:	    	missing,$
             iwin:            	iwin,$
    	     hdr:   	    	header,$
             time_stamp:      	systime(0)}

return,windata
end