
PRO eis_slot_plot, file, iwin=iwin, clean=clean, noplot=noplot, im=im, $
                   _extra=_extra, trim=trim, data=data,  $
                   ct_reverse=ct_reverse, log=log, mirrpos=mirrpos, $
                   quiet=quiet

;+
; NAME
;
;    EIS_SLOT_PLOT
;
; PROJECT
;
;    Hinode/EIS
;
; EXPLANATION
;
;    Given a slot raster this routine plots out an image from the specified
;    wavelength window. Window is specified either from the command line, or
;    with the IWIN= keyword.
;
; INPUTS
;
;    FILE   Either the name of an EIS data file, or an EIS data object.
;
; OPTIONAL INPUTS
;
;    IWIN   The index of the wavelength window to be extracted. If not
;           specified, then IWIN will contain the window index chosen by
;           the user.
;
;    TRIM   A two element array used to trim the slot images. E.g., if the
;           windows are 48 pixels wide, then setting trim=[2,41] will create
;           40 pixel wide images taking pixels 2 to 41.
;
; OPTIONAL OUTPUTS
;
;    IWIN   The index of the wavelength window that has been extracted.
;           If IWIN is defind by the user, then it is used to select the
;           wavelength window
;
;    IM     Contains the 2D image that is plotted.
;
;    DATA   The EIS data object is returned as an output.
;
;    MIRRPOS Contains the mirror positions of each of the individual exposures.
;
; KEYWORDS
;
;    CLEAN  Clean the images of cosmic rays.
;
;    NOPLOT Do not plot the image.
;
;    CT_REVERSE  Reverse the colour table.
;
;    QUIET  Do not print any information to the screen.
;
; CALLS
;
;    EIS_GET_HDR_STRUC, CLEAN_EXPOSURE
;
; HISTORY
;
;    Ver.1, 12-Nov-2007, Peter Young
;        tidied up header
;    Ver.2, 11-Sep-2008, Peter Young
;        added parameter check
;    Ver.3, 28-Jan-2010, Peter Young
;        now automatically trims the individual exposures
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use:  eis_slot_plot, file [, iwin=, /clean, /noplot, im='
  print,'                      trim=, data=, /ct_reverse, /log'
  print,'                      mirrpos=, /quiet ]'
  return
ENDIF


swtch=0
IF datatype(file) NE 'OBJ' THEN BEGIN
  swtch=1
  data=obj_new('eis_data',file,datasource='fits')
ENDIF ELSE BEGIN
  data=file
ENDELSE

mirrpos=*(data->getaux_data()).fmirr

xpos=data->getxpos()
ypos=data->getypos()

hdr=data->gethdr()
id = hdr[0]-> getline_id()

IF n_elements(iwin) EQ 0 THEN BEGIN 
  i=where(id NE '')
  nwind=n_elements(id[i])

  ; -----------------------------------<
  ; Choose which wavelength window needs to be looked at
  ;
  PRINT,''
  PRINT,'Choose a wavelength window to analyse:'
  PRINT,''
 ;
  FOR i=0,nwind-1 DO PRINT,i+1,'.  ',trim(id[i])
 ;
  READ,choice
  iwin=FIX(choice-1)
 ; -----------------------------------<
ENDIF

plot_title=trim(id[iwin])

wd=data->getvar(iwin)
eis_debias,wd
wdsiz=size(wd)
nl=wdsiz[1]

eis_get_hdr_struc,data,hdrstr

fmir_ss=hdrstr.fmir_ss
wid=fmir_ss/4

diff=(nl-wid)/2 > 0

IF n_elements(trim) EQ 0 THEN trim=[diff,wid-1+diff]

IF NOT keyword_set(quiet) THEN BEGIN 
print,''
print,'%EIS_SLOT_PLOT: Window is '+trim(nl)+' pixels wide, step size is '+trim(wid)+' pixels.'
IF wid LT nl THEN BEGIN
  print,'                TRIM has been set to ['+trim(trim[0])+','+trim(trim[1])+']'
  print,'                Use keyword TRIM= to modify this.'
ENDIF
ENDIF 

nexp=hdrstr.nexp
nexp_prp=hdrstr.nexp_prp

IF hdrstr.nexp_prp EQ 1 THEN BEGIN
  IF nexp EQ 1 THEN BEGIN
    plot_image,reform(wd)
  ENDIF ELSE BEGIN
    im=fltarr(wid*wdsiz[3],wdsiz[2])
    FOR i=0,nexp-1 DO BEGIN
      wdi=wd[trim[0]:trim[1],*,i]
      IF keyword_set(clean) THEN BEGIN
        clean_im=clean_exposure(wdi)
        wdi=clean_im
      ENDIF
      im[wid*(nexp-i-1):wid*(nexp-i)-1,*]=reverse(wdi,1)
    ENDFOR
    IF keyword_set(ct_reverse) THEN im=max(im)-im
    IF keyword_set(log) THEN im=alog10(im)
    IF NOT keyword_set(noplot) THEN BEGIN
      orig=[min(xpos)-wid/2.,min(ypos)]
      plot_image,im,_extra=_extra,orig=orig, $
                 xtit='Solar-X / arcsec', $
                 ytit='Solar-Y / arcsec', $
                 tit=plot_title
    ENDIF
  ENDELSE
ENDIF

IF swtch EQ 1 THEN obj_destroy,data

END
