PRO SXT_PREP0, index, data, new_index, new_data, unc_data, satpix,	$
	subs=subs, sum=sum, ntimes=ntimes, upper_only=upper_only,	$
	sparse=sparse1, error=error, normalize=normalize, 		$
	register=register
;+
; NAME:
;       SXT_PREP0
; PURPOSE:
;       Prepare SXT images for further processing:
;		decompression, background subtraction, saturated pix (+1 above/
;		below), temporal interpolation, rebin, exposure normalization.
;
;	Will set negative values (after subtraction) to 0.
;	Saturated pixels are flagged in satpix
;
;       sxt_prep0, index, data, new_index, new_data
;       sxt_prep0, index, data, new_index, new_data[, unc_data, satpix, sum=2]
;       sxt_prep0, index, data, new_index, new_data[, ntimes=ntimes, subs=subs]
;       sxt_prep0, index, data, new_index, new_data[, unc_data, satpix, /sparse]
;       sxt_prep0, index, data, new_index, new_data, /norm     ; Normalize to 1s
;       sxt_prep0, index, data, new_index, new_data, /register ; Simple alignment
;
; INPUTS:
;	index	= index (structure)
;	data	= 2-d or cube of SXT images.
; OUTPUTS:
;	new_index = New index (structure)
;	new_data  = New 2-d or cube of SXT images.
; OPTIONAL OUTPUTS:
;	unc_data = Compression error
;	satpix	= byte array same size as new_data.   Set to 1 if pixel is 
;		  saturated.  If sum=1 or greater, satpix = number of saturated
;		  pixels in the macro pixel.  If /sparse is specified,
;		  satpix is returned as a "sparse" data structure.
; OPTIONAL INPUT KEYWORDS:
;	sum	= Sum over SUM X SUM pixels (if vector, sum = [sumx,sumy])
;	subs	= Subarray of images to process.
;	ntimes	= If data is a cube and there are at least 2 images, interpolate
;		  to the times given in ntimes (can be any standard Yohkoh 
;		  format).  If subs is not given, will interpolate using all
;		  available images in data cube.  If subs is present, will use
;		  only the images given by subs to interpolate.
;	sparse	= If set, return satpix as a "sparse" data structure (see
;		  the header of the routine sparse.pro for more information).
;	upper_only = If set, only flag +1 pixels above (in a column) the 
;		  saturated area (not the -1 pixels) [see call to sxt_satpix].
;	normalize = If set, renormalize to 1 sec exposures.
;	register  = If set, will perform simple registration of the data set.
; OPTIONAL OUTPUT KEYWORDS:
;	error	= Set to 1 if sxt_prep0 returns with an error.
; RESTRICTIONS:
; PROCEDURE:
;	o Calls leak_sub, sxt_decomp, sxt_satpix, sxt_interp, sxt_sumxy, 
;	        align_ar, exp_norm
;       o The registration portion of this routine fairly preliminary.
; MODIFICATION HISTORY:
;	22-feb-93, J. Lemen,  LPARL, Written.
;	26-feb-93, JRL, Changed satpix to positional and added /sparse keyword.
;	 9-mar-93, JRL, Modularized the various sections.
;	15-mar-93, JRL, Added /norm keyword (exp_norm)
;	25-mar-93, JRL, Added /register keyword (align_ar)
;	31-mar-93, JRL, if /norm set, renormalize unc_data array
;	20-apr-93, JRL, if /norm is set, check if unc is required or not
;       19-nov-93, JRL, Renamed to sxt_prep0 (should no longer be used)  --
;			use MDM's new sxt_prep instead.
;-	--------------------------------------------------------------

error = 0			; Set this if there is a problem

message,'** Do Not Use This Program **'+string(byte([7,7,7,7])),/info
message,'** Use sxt_prep instead **',/info

; Give the user information if no parameters specified:
if n_params() lt 4 then begin 
  doc_library,'sxt_prep0'
  fastdoc,'sxt_prep0',/summ
  return
endif

;----------------------------------------------------------------------------
;  ****  Step 1:  Perform Checks on index and data
;----------------------------------------------------------------------------

new_index = 0. & new_data = 0.			; In case we return early

; *****
; Make sure index is a structure 
; *****
sz1 = size(index)

if sz1(sz1(0)+1) ne 8 then begin	; index must be a structure
   print,' *** Error in SXT_PREP0 ***'
   print,'     index must be a structure'
   help,index,data & tbeep
   error = 1				; Set the error keyword
   return
endif

; *****
; Make sure data is byte-type and 3-d or less
; *****

sz2 = size(data)
if (sz2(sz2(0)+1) ne 1) or (sz2(0) gt 3) then begin	; data must be byte
   print,' *** Error in SXT_PREP0 ***'
   print,'     data must be byte type and 3-d or less'
   help,index,data & tbeep
   error = 1				; Set the error keyword
   return
endif

if n_elements(subs) eq 0 then subs = indgen(n_elements(index))
new_index = index(subs)			; Create new index array

;----------------------------------------------------------------------------
;  ****  Step 2:  Search for saturated pixels
;----------------------------------------------------------------------------

sat_limit = [235, 255, 255]			; Full, half, quarter resolution
satpix = sxt_satpix(index,data,subs,sat_limit=sat_limit,upper_only=upper_only)

;----------------------------------------------------------------------------
;  ****  Step 3:  Data decompression and background subtraction
;----------------------------------------------------------------------------

if n_params() ge 5 then 		$
  unc_data = sxt_decomp(data(*,*,subs),/uncert)	; Decompression error
  new_data = leak_sub(new_index,data(*,*,subs))	; subtract the background
  new_data = new_data > 0			; Set negative values to 0

;----------------------------------------------------------------------------
;  ****  Step 4:  Image registration
;----------------------------------------------------------------------------

; The treatment for unc_data is not strickly correct, but since derivative
; d(unc_data) / d(decompressed_data) is small, it basically o.k.

if keyword_set(register) then begin
  new_data = align_ar(new_data,new_index,miss=0)
  if n_params() ge 5 then unc_data = align_ar(unc_data,new_index,miss=0)
  if n_params() ge 6 then begin
     satpix = align_ar(satpix*100b,new_index,miss=0)	; Assume max(satpix)=1
     ii = where(satpix gt 0, npix) 			; Cheap way to preserve	
     if npix gt 0 then satpix(ii) = 1			;-    fractional values
  endif
endif
;----------------------------------------------------------------------------
;  ****  Step 5:  Temporal interpolation
;----------------------------------------------------------------------------
if (n_elements(ntimes) ne 0) then begin
  
  case n_params() of 
    4: sxt_interp,new_index,new_data,ntimes
    5: sxt_interp,new_index,new_data,ntimes,unc_data
    6: sxt_interp,new_index,new_data,ntimes,unc_data,satpix
  endcase
  
endif

;----------------------------------------------------------------------------
;  ****  Step 6:  Rebin to desired sumX by sumY
;----------------------------------------------------------------------------

if n_elements(sum) ne 0 then sumX = sum(0)>1 else sumX = 1
if n_elements(sum) gt 1 then sumY = sum(1)>1 else sumY = sumX

case n_params() of 
   4: sxt_sumxy,new_index,new_data,sumX,sumY
   5: sxt_sumxy,new_index,new_data,sumX,sumY,unc_data
   6: sxt_sumxy,new_index,new_data,sumX,sumY,unc_data,satpix
endcase


;----------------------------------------------------------------------------
;  ****  Step 7:  Exposure normalization: Renormalize to 1 sec
;----------------------------------------------------------------------------

if keyword_set(normalize) then begin
	new_data = exp_norm(new_data,new_index,0.)
   if n_params() ge 5 then 		$
	unc_data = byte(exp_norm(unc_data,new_index,0.)+.5)
endif
; Represent satpix in a "sparse" structure if keyword is set:
if keyword_set( sparse1 ) then satpix = sparse(satpix,low=1)

end
