;+
; Project     : SOHO - CDS     
;                   
; Name        : EXPSLICE()
;               
; Purpose     : Returns slice of a Spectral Window at specific exposure
;
; Explanation : 
;               
; Use         : exposure = EXPSLICE(QLDS,window,exposure_no)
;    
; Inputs      : QLDS:	A CDS QL data structure.
;		
;		window:	The number or name of a detector window.
;
;		exposure_no: The number of the desired exposure.
;
;
; Opt. Inputs : VALID:	Supply to return a value of 0 instead of
;			generating an error message.
;               
; Outputs     : Returns a 1- (GIS) or 2- (NIS) dimensional array 
;		corresponding to the data from a specific exposure
;		in a specified detector data window for a specific 
;		exposure. (A slice in the SOLAR_Y - WAVELNTH plane).
;
; Opt. Outputs: VALID:	Returns TRUE (NOT 0) if the slice went ok
;			and 0 if some error was detected.
;               
; Keywords    : NOCHECK :
;			Set to skip QLMGR parameter check.
;
; Calls       : WINDOWNO(),QLMGR
;
; Common      : None.
;               
; Restrictions: QLDS must be a valid CDS QL data structure
;		WINDOW must be a scalar integer or string,
;		with the number or name of an existing detector
;		window.
;		EXPOSURE_NO must be <1, ... , nexposures(QLDS)-1 >
;               
; Side effects: 
;               
; Category    : Data_Handling, QuickLook, Interface.
;               
; Prev. Hist. : Requested by Paal Brekke 19 October 1993
;		Made by modifying WAVESLICE() - bugs here
;		should be checked out in WAVESLICE(), and
;		vice versa.
;
; Written     : Stein Vidar Hagfors Haugan, 19 October 1993
;               
; Modified    : SVHH, 21 November 1993 - Changed name:
;						rstslice -> expslice
;		SVHH, 1-June-1994, Fixed minor bug in calling windowno.
;		SVHH, 20-Feb-1995, New data storage formats causes
;			some changes. Needs to be looked at. What
;			does EXPSLICE mean with data of dimensions
;			(SOLAR_X,SOLAR_Y,DEL_TIME) ?
;			For now I'm assuming (WAVELNTH,SOLAR_X,SOLAR_Y).
;		SVHH, 21-Aug-1995, Version 1.2
;			Fixed a mis-interpretation when both solar_x
;			AND dimensions.del_time are singular.
;
; Version     : 1.2, 21-Aug-1995
;-            

FUNCTION ExpSlice,QLDS,window,exposure_no,valid,NOCHECK=NOCHECK
  
  IF N_params() lt 3 THEN message,'Use: expslice,QL_Data,window_no,exposure_no'
  
  IF N_params() eq 4 THEN Valid=0 $ ; On error: if NOT valid : return status
  ELSE Valid=NOT 0		    ; 	      else	   : message
  
  IF NOT Keyword_SET(NOCHECK) THEN BEGIN
      QLMGR,QLDS,qlvalid
      IF NOT qlvalid THEN $
	  IF NOT valid THEN return,0 $
	  ELSE message,'First argument must be a QL data structure'
  EndIF
  
;
; Check WINDOW, convert to int by windowno() if a scalar string,
; else make sure it's a scalar int/long
;
  sz = Size(window)
  IF total(sz eq [0,7,1]) eq 3 THEN window=windowno(QLDS,window,/NOCHECK) $
  ELSE $
      IF total(sz ne [0,2,1]) and total(sz ne [0,3,1]) THEN $
	  IF NOT valid THEN return,0 $
	  ELSE message,'Window number must be scalar integer'
  
;
; Check range of WINDOW
;
  IF window lt 1 or window gt N_elements(QLDS.DetDesc) THEN $
	  IF NOT valid THEN return,0 $
	  ELSE message,'Detector window must be <1, ... ,'+$
			  trim(N_elements(QLDS.DetDesc))+'>'
;
; Check EXPOSURE_NO, make sure it's a scalar int/long
;
  rtype = datatype(exposure_no)
  IF N_elements(exposure_no) ne 1 or (rtype ne 'INT' and rtype ne 'LON') THEN $
	  IF NOT valid THEN return,0 $
	  ELSE message,'Exposure number must be scalar integer'
  
  DIMENSIONS=QL_DIMENSIONS(QLDS,/NOCHECK)
  
  IF dimensions.solar_x or not dimensions.del_time THEN BEGIN
      rsz = DIMENSIONS.SSOLAR_X
      
;
; Check range of EXPOSURE_NO
;
      
      IF exposure_no lt 1 or exposure_no gt rsz THEN $
	    IF NOT valid THEN return,0 $
	    ELSE message,'Exposure number must be <1,..,'+$
						    trim(rsz)+'>'
;
; All tests passed VALID -> NOT FALSE
;
      valid = NOT valid
      
      d = QLDS.DetDesc(window-1)
      
      CASE N_elements(QLDS.DetDesc(0).Axes) OF
	  1 : return, reform( QLDS.DetData(d.ixstart(0):d.ixstop(0))   )
	  2 : return, reform( QLDS.DetData(d.ixstart(0):d.ixstop(0),*) )
	  3 : return, reform(QLDS.DetData(d.ixstart(0):d.ixstop(0),$
			     exposure_no-1,*))
	  4 : return, reform(QLDS.DetData(d.ixstart(0):d.ixstop(0),$
			     exposure_no-1,*,*))
      EndCASE
      
  End else begin

      rsz = DIMENSIONS.SDEL_TIME
      
;
; Check range of EXPOSURE_NO
;
      
      IF exposure_no lt 1 or exposure_no gt rsz THEN $
	    IF NOT valid THEN return,0 $
	    ELSE message,'Exposure number must be <1,..,'+$
						    trim(rsz)+'>'
;
; All tests passed VALID -> NOT FALSE
;
      valid = NOT valid
      
      d = QLDS.DetDesc(window-1)
      
      CASE N_elements(QLDS.DetDesc(0).Axes) OF
	  1 : return, reform( QLDS.DetData(d.ixstart(0):d.ixstop(0))   )
	  2 : return, reform( QLDS.DetData(d.ixstart(0):d.ixstop(0),*) )
	  3 : return, reform(QLDS.DetData(d.ixstart(0):d.ixstop(0),$
			     *,exposure_no-1))
	  4 : return, reform(QLDS.DetData(d.ixstart(0):d.ixstop(0),$
			     *,*,exposure_no-1))
      EndCASE
  end
  
  
  
END
