function sxt_satpix,index,data,subs,sat_limit=sat_limit,sparse=sparse1,	$
		silent=silent,upper_only=upper_only
;+
; NAME:
;   sxt_satpix
; PURPOSE:
;   Locate and flag saturated pixels in SXT images.
;   Includes +1 and -1 pixels in the vertical direction (for blooming).
; CALLING SEQUENCE:
;   satpix = sxt_satpix(index,data)
;   satpix = sxt_satpix(index,data,ss)
;   satpix = sxt_satpix(index,data,ss,sat_limit=sat_limit)
; INPUTS:
;   Index  = SXT index structure
;   data   = SXT data cube (byte type, compressed)
; OPTIONAL INPUTS:
;   ss	   = Indices of a subset of images to process (default is all images)
; OPTIONAL INPUT KEYWORDS:
;   sat_limit	= Vector with 3 elements with the values for the saturation
;		  limits ([full, half, quarter]).  Default is (ge):
;		  sat_limit = [235, 255, 255]	; Full, half, quarter resolution
;   sparse 	= If set, returns result as a "sparse" data structure.
;   upper_only	= If set, only return saturated pixels and 1 pixel above (not
;		  not pixel below saturated region).
; OUTPUTS:
;   Result of call is a 2-d by n_elements(ss) byte array.  
;   Saturated pixels are flagged with a 1.
; MODIFICATION HISTORY:
;    9-mar-93, J. R. Lemen, LPARL
;    20-apr-93, JRL, Changed the check from gt to ge sat_limit
;-

if n_elements(sat_limit) eq 0 then 	$
	sat_limit = [235, 255, 255]		; Full, half, quarter resolution

if n_params() lt 3 then subs = indgen(n_elements(index))
nimg = n_elements(subs)
sz = size(data)
n_x = sz(1) & n_y = sz(2)
satpix = bytarr(n_x,n_y,nimg)           ; Location of saturated pixels

if sz(sz(0)+1) eq 1 then begin		; Can't do anything unless byte-type
  for i=0,nimg-1 do begin
    s2 = where(data(*,*,subs(i)) ge sat_limit(gt_res(index(subs(i)))),nsat)
    if nsat gt 0 then begin
      xx = s2 mod n_x
      yy = s2 / n_x
      s2up = ((yy+1)<(n_x-1))*n_x + xx		; 1 pixels above saturation
      s2dn = ((yy-1)>0)*n_x + xx		; 1 pixels below saturation
      ddata = bytarr(n_x,n_y)
      ddata(s2)   = 1				; Flag saturated pixels with -1
      ddata(s2up) = 1				; Flag +1 (up)   pixels
      if not keyword_set(upper_only) then 	$
	ddata(s2dn) = 1				; Flag -1 (down) pixels 
      satpix(0,0,i) = ddata			; Insert into the cube
    endif
  endfor
endif else if not keyword_set(silent) then begin
  print,' ***  Warning in sxt_satpix ***' & tbeep
  print,'      Input data array is not byte-type'
  help,data
endif
if keyword_set( sparse1 ) then satpix = sparse(satpix,low=1)

return,satpix
end
