;+
; Project     : SOHO - CDS     
;
; Name        : DETDESC()
;
; Purpose     : Retrieve Spectral Window info from a QL Data Structure
;
; Category    : Data_Handling, CDS, QuickLook
;
; Explanation : Returns the detector descriptor (QLDS.DetDesc) corresponding to
;		the spectral window that is requested.  The windows may be
;		referred to by NUMBER (1..N) or the name (TLABEL) of the
;		detector window.
;
; Syntax      :	Result = DETDESC(QLDS,WINDOW)
;
; Examples    :	
;
; Inputs      : QLDS	= An IDL structure containing data in the CDS QL data
;			  format.
;
;		WINDOW	= Either the NUMBER of the data window (NOTE: numbering
;			  from 1...N) or a text with the LABEL of the desired
;			  spectral window.
;
; Opt. Inputs :	
;
; Outputs     : The result of the function is the detector descriptor for the
;		chosen spectral window.
;
; Opt. Outputs:	None.
;
; Keywords    : NOCHECK	= Set to skip QLMGR checking.
;
;		ERRMSG = If defined and passed, then any error messages will be
;			 returned to the user in this parameter rather than
;			 depending on the MESSAGE routine in IDL.  If no errors
;			 are encountered, then a null string is returned.  In
;			 order to use this feature, ERRMSG must be defined
;			 first, e.g.
;
;				ERRMSG = ''
;				D = DETDESC(A, I, ERRMSG=ERRMSG)
;				IF ERRMSG NE '' THEN ...
;
; Calls       : Datatype, QLMGR, WINDOWNO
;
; Common      :	None.
;
; Restrictions: QLDS must adhere to the CDS QL Data Format.
;		WINDOW must be a scalar.
;
; Side effects:	None.
;
; Prev. Hist. : Based on DetData() by Stein Vidar Hagfors Haugan
;
; History     :	Version 1, 20-May-1996, William Thompson, GSFC
;		Version 2, 23-Jul-1996, William Thompson, GSFC
;			Added ERRMSG keyword
;
; Contact     :	WTHOMPSON
;-
;


FUNCTION DetDesc,QLDS,wnd,wnd_no,NOCHECK=NOCHECK,ERRMSG=ERRMSG
  
  On_Error,2
  
  
  IF n_params()	lt 2 THEN BEGIN
	  message = "Use: array =  DetDesc(QL_Data,window_ID)"
	  GOTO, HANDLE_ERROR
  ENDIF
  
  IF NOT Keyword_SET(NOCHECK) THEN BEGIN
      QLMGR,QLDS,valid
      IF NOT valid THEN	BEGIN
	  message = "First argument has to be a QL_Data structure"
	  GOTO, HANDLE_ERROR
      ENDIF
  EndIF
  
  wnd_no = windowno(QLDS,wnd,/NOCHECK)
  
  IF wnd_no eq 0 THEN BEGIN
      message = ["Couldn't find window: "+trim(wnd),	$
           'Window ID must be <1,..,' +trim(N_elements(QLDS.DetDesc))+'>', $
	   'or a window name']
      GOTO, HANDLE_ERROR
  END
  
  return, QLDS.DetDesc(wnd_no-1)

;
;  Error handling point.
;
HANDLE_ERROR:
  IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'DETDESC: ' + MESSAGE ELSE BEGIN
	FOR I=0,N_ELEMENTS(MESSAGE)-2 DO MESSAGE, MESSAGE(I), /CONTINUE
	MESSAGE, MESSAGE(N_ELEMENTS(MESSAGE)-1)
  ENDELSE
  RETURN, -1

END
