;+
; Name:
;   	EIS_READSLOT
;
; Purpose:
;   	Reads sequence of slot files, builds a single image from
;   	multiple pointing positions and returns a 2-D image per file,
;   	i.e. a datacube (x,y,nfiles) for a list of n files.
;
; Calling Sequence:
;   	IDL> eis_readslot, file, out, [keywords]
;
; Inputs:
;   	FILES	:   list of EIS files (full path).
;
; Outputs:
;   	OUT 	:   output structure with various tags.
;
; Keywords:
;   	ESTIMATE:   estimates the overlap using EIS_SLOTOVERLAP.PRO. Ignores
;   	    	    OVERLAP keyword.
;   	EXPOSURE:   exposure to be chosen in multiple exposures per raster position.
;   	    	    Default: 0. [Note: uncommon, so not tested thouroughly. Report problems]
;       OVERLAP :   overlap in pixels between slots with adjacent pointing.
;		    Expected input:
;			       OVERLAP=[overlap,leftcrop,rigthcrop]
;		    where: overlap = leftcrop + rightcrop
;		    LEFTCROP and RIGHTCROP determine the amount to be cropped
;		    from each side of individual slots when merging. Their sum is
;   	    	    the total overlap.
;		    Alternative input: OVERLAP=5 (equivalent to OVERLAP=[5,0,5])
;   	WIN 	:   spectral window or wavelength. If none, interactive
;   	    	    selection. Note: use win = [0] to select the first window.
;
; Notes:
;   	If multiple exposures per raster position, it probably needs fixing.
;
; Example:
;   	IDL> file ='eis_l0_20070826_175726.fits.gz'
;   	IDL> file = eis_find_level1(file)
;   	IDL> eis_readslot,file, out, win=[4], overlap=[8,2,6]
;
;   	If the overlap is not know, an estimate can be obtained doing:
;   	IDL> eis_readslot,file, out, win=4, /estimate
;
; Modification History:
;   	2009/03/04  I. Ugarte-Urra. NRL. Added documentation.
;   	2009/06/05  IUU. Added reinit keyword to eis_getwindata
;   	2009/07/28  IUU. Fix bug in rasters with single exposure per raster position.
;       2009/08/05  HPW. Changed win to work with win = 0 (the first window)
;   	2009/08/13  IUU. Added ESTIMATE keyword.
;   	2010/01/26  IUU. Deal with sit-n-stare data and concatenation.
;   	2010/06/18  IUU. Added EXPOSURE keyword to deal with multple exp. per raster position.
;   	2012/11/01  IUU. Added WVL keyword.
;   	2020/05/08  IUU. Clean-up of unused line.
;-

PRO eis_readslot, files, out, win=win, overlap=overlap, estimate=estimate, exposure=exposure

if n_elements(win) eq 0 then begin
  message,'no window number input, select one . . .',/info
  wininfo = eis_get_wininfo(files[0],/list,nwin=nwin)
  iwin = 0
  read,iwin,prompt='Select a window to read [0...'+trim(nwin-1)+']> '
endif else iwin = win[0]

if not (keyword_set(overlap)) then overlap = [0,0,0]
if n_elements(overlap) eq 1 then overlap=[overlap(0),0,overlap(0)]

;; convert eis filenames to tai times
; files_tai=anytim(eisfilename2date(files),/tai)

nfiles=n_elements(files)
FOR i=0,nfiles-1 DO BEGIN

    str = eis_getwindata(files[i],iwin,/reinit)

    y1_i= 0
    y2_i= str.ny-1


    IF str.hdr.nraster ne str.hdr.nexp and str.hdr.nexp_prp le 1 THEN BEGIN   ; Sit-n-stare or multiple exposures per raster position
    	if str.hdr.nexp_prp gt 1 then message,'CAN NOT DEAL WITH THIS TYPE OF DATA YET'
    	if i eq 0 then begin
    	    times=strarr(nfiles*str.hdr.nexp)
    	    exptimes=strarr(nfiles*str.hdr.nexp)
    	    macroimg=fltarr(str.nl,str.ny,nfiles*str.hdr.nexp)
    	    macroerr=fltarr(str.nl,str.ny,nfiles*str.hdr.nexp)
    	    solar_x=fltarr(nfiles*str.hdr.nexp)
    	    solar_y=fltarr(str.ny,nfiles)
	    wvl=str.wvl
    	endif
    	solar_x(i*str.hdr.nexp:i*str.hdr.nexp+str.hdr.nexp-1) = str.solar_x
    	solar_y(*,i) = str.solar_y

    	macroimg(*,*,i*str.hdr.nexp:i*str.hdr.nexp+str.hdr.nexp-1)=transpose(str.int,[0,2,1])
    	macroerr(*,*,i*str.hdr.nexp:i*str.hdr.nexp+str.hdr.nexp-1)=transpose(str.err,[0,2,1])
    	times(i*str.hdr.nexp:i*str.hdr.nexp+str.hdr.nexp-1)=str.time_ccsds
    	exptimes(i*str.hdr.nexp:i*str.hdr.nexp+str.hdr.nexp-1)=str.exposure_time
	sz=size(str.int,/dim)

    ENDIF ELSE BEGIN	    	    	    	    ; Raster
    	image = fltarr(str.nl*str.hdr.nraster,y2_i-y1_i+1)
    	error = fltarr(str.nl*str.hdr.nraster,y2_i-y1_i+1)
    	tt	= strarr(str.nl*str.hdr.nraster)
    	exptt	= strarr(str.nl*str.hdr.nraster)
    	solar_x_i =  fltarr(str.nl*str.hdr.nraster)
    	xpixcen = (str.nl-1) - str.nl/2
    	wvl =  fltarr(str.nl*str.hdr.nraster)

    	if str.hdr.nraster gt 1 then begin	    ; Multiple raster positions
    	    for h=0,str.hdr.nraster-1 do begin
    		image[0+(h*str.nl):str.nl-1+(h*str.nl),*] = str.int[*,str.hdr.nraster-1-h,y1_i:y2_i]
    		error[0+(h*str.nl):str.nl-1+(h*str.nl),*] = str.err[*,str.hdr.nraster-1-h,y1_i:y2_i]
    		tt (0+(h*str.nl):str.nl-1+(h*str.nl))  = str.time_ccsds(h)
    		exptt(0+(h*str.nl):str.nl-1+(h*str.nl))= str.exposure_time(h)
    		solar_x_i(0+(h*str.nl):str.nl-1+(h*str.nl)) = findgen(str.nl) + (str.solar_x(h) - (xpixcen * 1.0)) ;Taking into account that image will be rotated below
    		wvl[0+(h*str.nl):str.nl-1+(h*str.nl)] = rotate(str.wvl,2)   	    	    	    	    	    ;Taking into account that image will be rotated below
    	    endfor
    	endif else begin
    	    if str.hdr.nexp_prp gt 1 then begin     ; Multiple exposures per raster position
    ;		if not keyword_set(exposure) then message,'Multiple exposures per raster position. Specify EXPOSURE'
    	    	if i eq 0 then begin
		    if not (keyword_set(exposure)) then exposure = 0
		    message,'CHOSEN EXPOSURE: '+trim(exposure),/info
		endif
    	    	if exposure gt str.hdr.nexp_prp-1 then begin
		    message,'EXPOSURE is larger than maximum value: '+trim(str.hdr.nexp_prp-1), /info
		    out=0
		    return
		endif
    		image=reform(str.int(*,0,y1_i:y2_i,exposure))
    		error=reform(str.err(*,0,y1_i:y2_i,exposure))
		tt[*]=str.time_ccsds[0,exposure]
    		exptt[*]=str.exposure_time[0,exposure]
    		solar_x_i = findgen(str.nl) + (str.solar_x[0,exposure] - (xpixcen * 1.0)) ;Taking into account that image will be rotated below
		wvl = rotate(str.wvl,2)     	    	    	    	    	    	    ;Taking into account that image will be rotated below
    	    endif else begin
    		image=reform(str.int(*,0,y1_i:y2_i))
    		error=reform(str.err(*,0,y1_i:y2_i))
    		tt[*]=str.time_ccsds
    		exptt[*]=str.exposure_time
    		solar_x_i = findgen(str.nl) + (str.solar_x - (xpixcen * 1.0)) ;Taking into account that image will be rotated below
		wvl = rotate(str.wvl,2)     	    	    	    	    	    	    ;Taking into account that image will be rotated below
    	    endelse
    	endelse

    	image = rotate(image,5)     ;Image reversed on detector
    	error = rotate(error,5)     ;Image reversed on detector

    	;;- M E R G I N G using [overlap, leftcrop, rightcrop]  where overlap=leftcrop+rightcrop
    	if keyword_set(estimate) then overlap=eis_slotoverlap(str)
    	if  overlap(0) ne 0 then begin
    	    dl= str.nl
    	    image_new = fltarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap(0),y2_i-y1_i+1)
    	    error_new = fltarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap(0),y2_i-y1_i+1)
    	    tt_new = strarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap(0))
    	    exptt_new = strarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap(0))
    	    solar_x_i_new =  fltarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap(0))
    	    xpixcen = (str.nl-1 - overlap(1)) - str.nl/2			    ; Find XCEN reference pixel (nl/2) in rotated and cropped image
	    wvl_new = fltarr(str.nl*str.hdr.nraster - str.hdr.nraster*overlap[0])
    	    for h=0,str.hdr.nraster-1 do begin      ;loop on raster positions sections of image
    		dln=str.nl-overlap(0)
    		image_new[0+(dln*h):dln+(dln*h)-1,*]=image[overlap(1)+(dl*h):dl+(dl*h)-1-overlap(2),*]
    		error_new[0+(dln*h):dln+(dln*h)-1,*]=error[overlap(1)+(dl*h):dl+(dl*h)-1-overlap(2),*]
    		tt_new(0+(dln*h):dln+(dln*h)-1)=str.time_ccsds(h)
    		exptt_new(0+(dln*h):dln+(dln*h)-1)=str.exposure_time(h)
    		solar_x_i_new(0+(dln*h):dln+(dln*h)-1) = findgen(str.nl - overlap(0)) + (str.solar_x(h) - (xpixcen * 1.0))
    		wvl_new[0+(dln*h):dln+(dln*h)-1,*]=wvl[overlap(1)+(dl*h):dl+(dl*h)-1-overlap(2)]
    	    endfor
    	    image = temporary(image_new)
    	    error = temporary(error_new)
    	    tt = temporary(tt_new)
    	    exptt = temporary(exptt_new)
    	    solar_x_i = temporary(solar_x_i_new)
	    wvl   = temporary(wvl_new)
    	endif

    	if i eq 0 then begin
    	    sz=size(image,/dim)
    	    times=strarr(sz(0),n_elements(files))
    	    exptimes=strarr(sz(0),n_elements(files))
    	    macroimg=fltarr(sz(0),sz(1),nfiles)
    	    macroerr=fltarr(sz(0),sz(1),nfiles)
    	    solar_x=fltarr(sz(0),nfiles)
    	    solar_y=fltarr(sz(1),nfiles)
    	endif

    	;;Coordinates (taking into account rotation)
    ;	 xpixcen = (str.nl-1 - overlap(1)) - str.nl/2			    ; Find XCEN reference pixel (nl/2) in rotated and cropped image
    ;	 solar_x(*,i) = findgen(sz(0)) + (min(str.solar_x) - (xpixcen * 1.0))
    	solar_x(*,i) = solar_x_i
    	solar_y(*,i) = str.solar_y(y1_i:y2_i)

    	macroimg(*,*,i)=image
    	macroerr(*,*,i)=error
    	times(*,i)=tt	    ;Should crash in multiple exposures per raster position?!
    	exptimes(*,i)=exptt
    ENDElSE
ENDFOR

image=temporary(macroimg)
error=temporary(macroerr)

break_file, files, disk_log, dir, filnam, ext, fversion, node, last_dot=last_dot

out={	filename    	:   filnam+ext, $
    	line_id     	:   str.line_id, $
    	int	    	:   image,$
    	err	    	:   error,$
	time_ccsds  	:   times,$
	exposure_time	:   exptimes, $
	solar_x     	:   solar_x,$
	solar_y     	:   solar_y, $
	wvl 	    	:   wvl,$
	nraster     	:   str.hdr.nraster, $
	nl  	    	:   str.nl,$
	nx  	    	:   sz(0),$
	ny  	    	:   str.ny,$
	overlap     	:   overlap,$
	units	    	:   str.units, $
        hdr             :   str.hdr $
	}

END
