
;+
; 
;             NAME : eis_get_wininfo
; 
;          PURPOSE : To return information on the windows contained in
;                    an eis fits file.
;
; CALLING SEQUENCE : wininfo = eis_get_wininfo(file)
; 
;           INPUTS : An eis filename.
; 
;  OPTIONAL INPUTS : None
; 
;         KEYWORDS : list - generate a listing of the information
; 
;          OUTPUTS : a structure containting information on the
;                    windows in the file.
;
; OPTIONAL OUTPUTS : nwin - the number of windows.
;                    nexp - the number of exposures.
;                    nraster - the number of raster positions.
;                    ny - the y window length.
;
;    COMMON BLOCKS : None.
; 
;          EXAMPLE : IDL> file = 'eis_l0_20061214_192012'
;                    IDL> wininfo = eis_get_winfo(file,/list,nwin=nwin)
;                    iwin             line_id   wvl_min   wvl_max
;                       0        FE X 184.540    184.29    184.80
;                       1     FE VIII 185.210    184.96    185.47
;                       2       FE XI 188.230    187.97    188.48
;                       3     CA XVII 192.820    192.56    193.07
;                       4      FE XII 195.120    194.85    195.37
;                       5     FE XIII 202.040    201.78    202.30
;                       6       HE II 256.320    256.06    256.58
;                       7      FE XIV 274.200    273.96    274.47
;                       8       FE XV 284.160    283.92    284.43
;                    IDL> print,nwin
;                         9
;
;    SPECIAL CALLS : eis_getfilename
;
;          WRITTEN : HPW : 08-MAY-2007
;                    HPW : 14-MAY-2007 - Added keywords
;                    HPW : 09-MAY-2009 - changed wmin, wmax
; 
;-

function eis_get_wininfo,file,list=list,nwin=nwin,$
                         nexp=nexp,nraster=nraster,ny=ny

  filename = eis_getfilename(file)
  if not(file_exist(filename)) then begin
    message,"FILE NOT FOUND "+filename,/info
    return,-1
  endif

  header  = headfits(filename,exten=1)
  nwin    = fxpar(header,'NWIN')
  nexp    = fxpar(header,'NEXP')
  nraster = fxpar(header,'NRASTER')
  ny      = fxpar(header,'YW')
  wininfo = {iwin:      0,$
             line_id:  '',$
             wvl_min: 0.0,$
             wvl_max: 0.0,$
             nl:        0,$
             xs:        0}
  wininfo = replicate(wininfo,nwin)

  wininfo.iwin    = indgen(nwin)
  wininfo.line_id = (fxpar(header,'TTYPE*'))[0:nwin-1]
  wininfo.wvl_min = (fxpar(header,'TWMIN*'))[0:nwin-1]
  wininfo.wvl_max = (fxpar(header,'TWMAX*'))[0:nwin-1]  
  wininfo.nl      = (fxpar(header,'TDETXW*'))[0:nwin-1]  
  wininfo.xs      = (fxpar(header,'TDETX*'))[0:nwin-1]  

  ;; --- recalculate wmin, wmax using eis_pixel_to_wavelength
  slit_ind = fxpar(header,'SLIT_IND')
  time = fxpar(header,'DATE_OBS')
  for i=0,nwin-1 do begin
    xstart = wininfo[i].xs
    if xstart ge 2148 then begin
      xstart=xstart-2148-50
      ccd = 0
    endif else begin
      xstart=xstart-50
      ccd = 1
    endelse
    xwidth = wininfo[i].nl
    wave   = eis_pixel_to_wavelength(xstart+indgen(xwidth),ccd,slit_ind,time=time)
    wininfo[i].wvl_min = min(wave)
    wininfo[i].wvl_max = max(wave)
  endfor

  if keyword_set(list) then begin
    list = strarr(nwin+1)
    list[0] = string('iwin','line_id','wvl_min','wvl_max',$
                     format='(a5,a20,2a10)')
    print,list[0]
    for i=0,nwin-1 do begin
      list[i+1] = string(i,wininfo[i].line_id,wininfo[i].wvl_min,$
                         wininfo[i].wvl_max,$
                         format='(i5,a20,2f10.2)')
      print,list[i+1]
    endfor
  endif

return,wininfo
end
  
