
FUNCTION eis_find_window, wvl, hdr, ipix=ipix, ll=ll

;+
; NAME
;
;     EIS_FIND_WINDOW()
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     Given the a header structure for an EIS data-set, this routine checks
;     if the specified wavelength, WVL, is found in the data-set's wavelength
;     windows. If yes, then the index of the wavelength window is returned.
;
; INPUTS
;
;     WVL   The wavelength (in angstroms) to be searched for.
;
;     HDR   The structure returned by the routine EIS_GET_HDR_STRUC.
;
; OPTIONAL OUTPUT
;
;     IPIX  The pixel in the window that best matches the specified
;           wavelength. 0 corresponds to the first pixel in the window. If
;           wavelength not found, then -1 is returned.
;
;     LL    The wavelength vector for the window. If the wavelength isn't
;           found, then this is returned as -1.
;
; OUTPUTS
;
;     If the wavelength is present in the wavelength windows, then the index
;     of that window is returned. Note the first window is labelled 0. If the
;     wavelength is not in any of the windows then a value of -1 is returned.
;
; HISTORY
;
;     Ver.1, 3-Feb-2007, Peter Young
;     Ver.2, 30-Jun-2007, Peter Young
;        added IPIX= optional output
;     Ver.3, 15-Jul-2007, Peter Young
;        added LL= optional output.
;     Ver.4, 7-Aug-2007, Peter Young
;        if window wasn't found then llout was undefined;
;        corrected this now.
;     Ver.5, 4-Dec-2008, Peter Young
;        added TIME= keyword in call to eis_pixel_to_wavelength
;-

tdetxw=hdr.tdetxw
tdetx=hdr.tdetx
slit_ind=hdr.slit_ind

bits=str_sep(tdetxw,' ')
tdetxw=fix(bits)

bits=str_sep(tdetx,' ')
tdetx=fix(bits)

n=n_elements(tdetx)

llout=-1
ipix=-1

chck=-1
FOR i=0,n-1 DO BEGIN
  IF tdetx[i] GE 2148 THEN BEGIN
    tdetx[i]=tdetx[i]-2148-50
    ccd=0
  ENDIF ELSE BEGIN
    ccd=1
    tdetx[i]=tdetx[i]-50
  ENDELSE
 ;
  ll=eis_pixel_to_wavelength(tdetx[i]+indgen(tdetxw[i]),ccd,slit_ind,time=hdr.date_obs)
  l0=min(ll)
  l1=max(ll)
 ;
  IF wvl GE l0 AND wvl LE l1 THEN BEGIN
    chck=i
   ;
    getmin=min(abs(wvl-ll),ipix)
    llout=ll
  ENDIF
ENDFOR

ll=llout

return,chck

END
