;+
; Project     : SOHO - CDS     
;                   
; Name        : DETDATA()
;               
; Purpose     : Retrieve Spectral Window data from a QL Data Structure
;               
; Explanation : The information in the detector descriptors 
;		(QLDS.DetDesc) is used to retrieve the data from 
;		QLDS.DetData 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.
;               
; Use         : data = DETDATA(QLDS,WINDOW)
;    
; 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 : None.
;               
; Outputs     : Returns an array containing the detector data for
;		the chosen spectral window.
;               
; Opt. Outputs: None.
;               
; Keywords    : NOCHECK :
;			Set to skip QLMGR checking
;			Used to speed up processing inside QL software
;
;		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 = DETDATA(A, I, ERRMSG=ERRMSG)
;				IF ERRMSG NE '' THEN ...
;
; Calls       :   handle_info(), qlmgr, tag_exist(), trim(), windowno()
;
; Common      : None.
;               
; Restrictions: QLDS must adhere to the CDS QL Data Format.
;		WINDOW must be a scalar.
;               
; Side effects: None.
;               
; Category    : Data_Handling, CDS, QuickLook
;               
; Prev. Hist. : Earlier version DETWND_DATA modified to take
;		WINDOW as the window NUMBER (1 and up) instead
;		of the window INDEX (0 and up), and also to
;		recognize the use of a text matching a LABEL.
;
; Written     : Stein Vidar Hagfors Haugan, 1 October 1993
;               
; Modified    : SVHH, 4 October 1993 - 
;			Added verification of data by call to QLMGR
;		SVHH, 8 October 1993 -
;			Separated out WINDOWNO()
;		SVHH, 17 October 1993 -
;			Added NOCHECK keyword
;		Version 2, 23-Jul-1996, William Thompson, GSFC
;			Added ERRMSG keyword
;               Version 3, 12 August 1996, SVHH
;                       Compatible with new and old storage system.
;               Version 4, 15 October 1997
;                       Added error handling for empty windows, old storage
;                       system.
;
; Version     : 4, 15 October 1997
;-            


FUNCTION DetData,QLDS,wnd,wnd_no,NOCHECK=NOCHECK,ERRMSG=ERRMSG
  
  On_Error,2
  
  
  IF n_params()	lt 2 THEN BEGIN
	  message = "Use: array =  detdata(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
  
  oldsys = tag_exist(qlds,"DETDATA",/top_level)
  IF oldsys THEN BEGIN
     ;;
     ;; Shorthand
     ;;
     ixb = QLDS.DetDesc(wnd_no-1).ixstart
     ixe = QLDS.DetDesc(wnd_no-1).ixstop
     
     IF ixe(0) LT 0 THEN BEGIN
        message = "Data not present"
        GOTO,HANDLE_ERROR
     END
     
     ;;
     ;; Decide upon dimensionality/indexing
     ;;
     CASE N_elements(ixb) OF
        1: RETURN,QLDS.DetData(ixb(0):ixe(0))
        2: RETURN,QLDS.DetData(ixb(0):ixe(0),ixb(1):ixe(1))
        3: RETURN,QLDS.DetData(ixb(0):ixe(0),ixb(1):ixe(1),ixb(2):ixe(2))
        4: RETURN,QLDS.DetData(ixb(0):ixe(0),ixb(1):ixe(1),$
                               ixb(2):ixe(2),ixb(3):ixe(3))
     END
  END ELSE BEGIN
     
     IF qlds.detdesc(wnd_no-1).ixstop(0) LT 0 THEN BEGIN
        message = "Data not present"
        GOTO,HANDLE_ERROR
     END
        
     handle = qlds.detdesc(wnd_no-1).handle
     h_status = handle_info(handle,/valid_id)
     d_status = 0
     
     IF h_status THEN BEGIN
        handle_value,handle,data
        IF N_ELEMENTS(data) GT 0 THEN d_status = 1
     END
     
     
     IF h_status NE 1 OR d_status NE 1 THEN BEGIN
        MESSAGE = ["The requested window is not available"]
        GOTO,HANDLE_ERROR
     END
     RETURN,DATA
     
  END
  
;  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
