;+
; Project     : SOHO - CDS     
;                   
; Name        : WINDOWNO()
;               
; Purpose     : Return the number of a window within a QL data structure.
;               
; Explanation : If the (text) name of a detector data window present in the
;		QLDS is supplied, the corresponding number (from 1..up)
;		of the window is returned.
;
;		If the number of a window is supplied, the return
;		value will be the same number, as an integer.
;		If the name of a detector data window is not found,
;		or if the supplied number is not between 1 and the
;		number of windows present, zero will be returned.
;               
; Use         : window = WINDOWNO(QLDS,WINDOW_ID)
;    
; Inputs      : QLDS:	CDS QuickLook data structure
;               
;		WINDOW_ID:
;			Either a text with the name of a detector
;			data window (corresponding to TTYPEx), or
;			the number of a window.
;
; Opt. Inputs : 
;               
; Outputs     : Returns the number of the window, or 0 if not a
;		valid window ID.
;               
; Opt. Outputs: None.
;               
; Keywords    : NOCHECK :
;			Set to skip QLMGR checking
;			Use to speed up processing inside QL software
;
; Calls       : QLMGR, DATATYPE
;
; Common      : None.
;               
; Restrictions: WINDOW_ID must be scalar.
;               
; Side effects: Initiation of QLMGR if not running.
;               
; Category    : Data_Handling, CDS, QuickLook
;               
; Prev. Hist. : Extracted from DETDATA
;
; Written     : Stein Vidar Hagfors Haugan, 8 October, 1993
;               
; Modified    : SVHH, 17 October 1993 -
;			Added NOCHECK keyword
;
; Version     : 1.1
;-            

FUNCTION windowno,QLDS,WND,NOCHECK=NOCHECK
  
  IF n_params()	lt 2 THEN $
	  message,"Use: wnd_no =  WINDOWNO(QLDS,WINDOW_ID)"
  
  IF NOT Keyword_SET(NOCHECK) THEN BEGIN
      QLMGR,QLDS,valid
      IF NOT valid THEN	message,"First argument has to be a QL_Data structure"
  EndIF
  
  IF N_elements(wnd) ne	1 THEN $
	  message,"Window ID has to be a scalar"
  
  
  N = N_elements(QLDS.DetDesc)
  
;
; In case of a window name string, search for that window
;
  IF datatype(wnd) eq "STR" THEN BEGIN
      i=0 & wnd_no=0
      WHILE i lt N DO BEGIN
	  IF QLDS.DetDesc(i).label eq wnd THEN BEGIN
	      wnd_no = i+1
	      i	= N
	  END
	  i = i+1
      EndWHILE
  END ELSE wnd_no = wnd
  
  
  IF wnd_no gt N or wnd_no le 0	THEN wnd_no = 0
  
  IF datatype(wnd_no) eq "LON" or datatype(wnd_no) eq 'FLO' or $
     datatype(wnd_no) eq 'DOU' THEN wnd_no=fix(wnd_no)
  
  IF datatype(wnd_no) ne "INT" THEN $
	  message,"Window ID has to be number or string, not "+datatype(wnd)
  
  return,wnd_no
  
END
