pro sxt_sumtime,index,data,new_index,new_data,unc_data,new_unc,		$
		satpix,new_satpix,subs=subs,time=time
;+
;  NAME:
;    sxt_sumtime
;  PURPOSE:
;    Sum SXT X-ray images by in time.
;    Preserve decompression uncertainty and saturated pixel information.
;
;    Warning:  This routine clobbers the input unc_data, satpix
;
;  CALLING SEQUENCE:
;    sxt_sumtime,index,data,new_index,new_data
;    sxt_sumtime,index,data,new_index,new_data,unc_data,new_unc
;    sxt_sumtime,index,data,new_index,new_data,unc_data,new_unc,  $
;			satpix,new_satpix
;    sxt_sumtime,index,data,new_index,new_data,subs=subs,time=time
;
;  INPUTS:
;    index	= SXT index structure
;    data	= Image cube (must be decompressed)
;  OUTPUTS:
;    new_index	= Copy of the first index
;    new_data	= 2-d summed array 
;		  If data is float, new_data = float
;		  If data is integer and new_data lt 2L^16 then new_data = integer
;		  If data is integer and new_data ge 2L^16 then new_data = float
;  OPTIONAL INPUTS:
;    unc_data	= Decompression uncertainties
;    satpix	= Saturated pixels (can be a sparse structure)
;		  (Max value of satpix is 255.)
;  OPTIONAL OUTPUTS:
;     new_unc	= 2-d array of uncertainties 
;     new_satpix= 2-d array of saturated pixels (sum of all satpix's)
;  OPTIONAL INPUT PARAMETERS:
;    subs	= Array of subscripts to sum
;  OPTIONAL OUTPUT PARAMETERS:
;    time	= Total time of summed array (msec).
;  RESTRICTIONS:
;    data must be decompressed and 3-d
;    Allows different filter data to be summed, so be careful.
;
;  MODIFICATION HISTORY:
;    25-Mar-93, J. R. Lemen, LPARL
;    30-mar-93, JRL, Changed the definition of new_satpix
;-

if n_params() le 3 then begin		; Information mode
   doc_library,'sxt_sumtime'
   return
endif

; -------------------------------------------------------------
;  Check to see if data is byte-type.
; -------------------------------------------------------------
sz = size(data) & sz_type = sz(sz(0)+1)
if sz_type eq 1 then begin
    print,'****  Error in sxt_sumtime:  data must be decompressed (not byte type)'
    tbeep & help,data & print,'      No summation was performed'
  return
endif

; -------------------------------------------------------------
;  Check that data is 3-d
; -------------------------------------------------------------

if (sz(0) ne 3) then begin
    print,'**** Error in sxt_sumtime:  data must be 3-d'
    tbeep & help,data
    return
endif

if n_elements(subs) eq 0 then subs = indgen(n_elements(index))

new_index = index(subs(0))		; Return the first index

; -------------------------------------------------------------
;  Check that all the filters are the same
; -------------------------------------------------------------

filtb = gt_filtb(index(subs))
if min(filtb) ne max(filtb) then begin
   filtb = filtb(sort(filtb))
   filtb_str = gt_filtb(filtb(uniq(filtb)),/str)
   print,' **** Warning in sum_times:  summed data contains different filters'
   print,'      Filters = ',filtb_str
   tbeep
endif

; -------------------------------------------------------------
;  Now do the summation of data array
; -------------------------------------------------------------

time = total(gt_expdur(index(subs)))		; Total time (msec)
new_data = total(data(*,*,subs),3)		; Sum over time

; Convert to I*2 or I*4 if possible:
if (sz_type eq 2) and (max(new_data) lt 2L^16) then 	$	; Input was I*2
    	new_data = fix(new_data)			$
else if (sz_type eq 3) then 				$	; Input was I*4
	new_data = long(new_data)

; -------------------------------------------------------------
;  Sum the uncertainty array in quadrature
; -------------------------------------------------------------

if n_params() ge 6 then begin
  nimg = n_elements(subs)
  sz1 = size(unc_data)
  if sz1(sz1(0)+1) ge 4 then roundup = 0. else roundup = 0.5
  new_unc = unc_data(*,*,subs(0))	; Set up the array
  new_unc(0,0) = sqrt(total(float(unc_data(*,*,subs))^2,3)) / nimg + roundup
endif


; -------------------------------------------------------------
;  Generate the new satpix
; -------------------------------------------------------------

if n_params() ge 8 then begin
  new_satpix = total(satpix(*,*,subs),3)	; Sum over time
  mmax = max(new_satpix)			; Find the max value
  sz = size(satpix)
; Try to return as a byte or fix array if input satpix was a byte or fix array
  if (sz1(sz1(0)+1) eq 1) and (mmax le 255) then 	$
	new_satpix = byte(new_satpix) else		$ ; Input was byte
  if (sz1(sz1(0)+1) eq 2) and (mmax le (2L^16)-1) then	$ ; Input was fix
	new_satpix = fix(new_satpix)  else 		$
	new_satpix = long(new_satpix)
endif

end
