function sxt_clean,index,data,satpix,cleanindex=cleanindex,$
        level=level,dat=dat,dark1=dark1,timer=timer,$
        nsmooth=nsmooth,nsigma=nsigma,nbound=nbound,debug=debug,$
        xsaa=xsaa

;+
;NAME
;	sxt_clean
;PURPOSE
;	To remove spikes and holes from an SXT image.
;PROCEDURE
;	1. If data are byte-type run SXT_PREP on the data.
;	2. If /xsaa is set and image is taken in SAA call sxt_xsaa to
;	   attempt to remove "cosmic ray" streaks.
;	3. Test for and repair persistent column blem in HR column 182.
;	4. Use a dark frame to determine the expected variance.
;	5. Use the standard deviation [sqrt(variance)] to decide which
;	   parts of the image to handle as noise and which as signal.
;	6. Use SIGMA_KLIP to despike the noisy parts of the image.
;	   This assumes that these parts of the image are Poissonian.
;	7. Use a median filter to despike the signal parts of the image.
;	   Spike replacement level determined SXT PSF and experiment.
;       8. Finally, replace all 4 sigma hot pixels in the dark image
;	   with pixels from the median-smoothed cleaned image, just
;	   to be sure they are corrected.
;	9. Return the cleaned image.
;CALLING SEQUENCE
;	nice_img=sxt_clean(index,data,/timer)
;	nice_img=sxt_clean(index,data,[cleanindex=cleanindex,$
;          level=level,dat=dat,dark1=dark1,timer=timer,$
;          nsmooth=nsmooth,nsigma=nsigma,nbound=nbound,debug=debug,$
;	   xsaa=xsaa]
;INPUTS
;	index, SXT index structure
;	data, either sxt_prep'd image or raw compressed SXT image
;OPTIONAL KEYWORD INPUTS
;	level, logarithmic spike threshold for signal parts of the image.
;		[default = 1.125 (FR), 0.75 (HR), 0.375 (QR)]
;	/timer, prints out how long it took program to run.
;	nsmooth, median smoothing parameter [default = 3]
;	nsigma, gaussian smoothing parameter [default = 3, sigma]
;	nbound, sets boundary between noisy and signal parts of image.
;		[default = 0.5, i.e., half the std dev of darkframe.]
;	/xsaa, removes SAA streaks if image taken in saa.
;	  *******WARNING: xsaa will affect small linear x-ray features!***
;	/debug, stops program from time to time for debugging.
;OPTIONAL OUTPUTS
;	satpix, saturated pixels in byte-type input image.
;OPTIONAL KEYWORD OUTPUTS
;	cleanindex, index structure with exposure adjusted in .his
;	    (Note: Flagging of cleaning not yet implemented in .his.)
;	dat, returns input data or sxt_prep'd input data.
;	dark1, returns darkframe used to determine std dev of noise.
;RESTRICTIONS
; 	THIS CODE ONLY HANDLES ONE IMAGE AT A TIME.
;	Assumes noise obeys poisson statistics.
;	sxt_clean should be run before registration.
;	This program alters the data -- which is ALWAYS dangerous!
;HISTORY
;	04-Nov-96, LWA, Written
;	01-Jan-98, LWA, Added satpix output.
;	15-Dec-98, LWA, Temporary until goes online, fixed std screwup.
;	16-Dec-98, LWA, removed dekanorm option.
;	17-Dec-98, LWA, Corrected error in use of stdev and set nbound
;			default to 0.5.  Tested on HR and QR images.
;       18-Jan-99, LWA, Use flattened darkframe to identify spikes
;			as a final step in cleaning process.
;	06-Feb-99, LWA, Added hot column and SAA streak removal.
;			Renamed program sxt_clean.pro.
;	28-Oct-99, LWA, Edited header.
;-
;

progverno = 2.000*1000  ; new version
print, '           *****   SXT_CLEAN  Ver 2.00  5-Feb-99   *****

if keyword_set(timer) then begin
   start_time = systime(1)
   run_time = 0.
endif

; SXT_PREP IF BYTE TYPE INPUT AND INDEX PRESENT
if (datatype(index,2)) eq 8 and (datatype(data,2)) eq 1 then begin
   sxt_prep,index,data,cleanindex,dat,/dc_interpolate,/float,unc,satpix,$
	/dc_orbit_correct
endif else begin
   dat=float(data) & cleanindex=index
endelse

; DEAL WITH SAA STREAKS IF xsaa KEYWORD SET
if keyword_set(xsaa) then begin
   tim2orbit,cleanindex,saa=saa
      if saa(0) then begin
         image = dat
         dat = sxt_xsaa(cleanindex,image)
   endif
endif

; SET THE DESPIKING/SMOOTHING PARAMETERS
if keyword_set(nsmooth) then nsmooth=nsmooth else nsmooth=3
if keyword_set(nsigma) then nsigma=nsigma else nsigma=3
if keyword_set(nbound) then nbound=nbound else nbound=0.5
if keyword_set(debug)  then debug=1 else debug=0

; TEST FOR BAD COLUMN BLEM IN DARKFRAME AND IMAGE
get_dc_image,index,dcindex,dark1,/dc_interpolate,/float
blem = 0
hotness = 0
  if gt_res(dcindex) eq 1 then pix = [182,66]
  if gt_res(dcindex) eq 2 then pix = [90,33]

; FIRST CHECK DARK FRAME FOR BLEM
dkdata = dark1
snip = dkdata(pix(0)-2:pix(0)+2,pix(1)-2:pix(1)+2)
good = kill_index(indgen(25),12)
snip = snip(good)
sigma = stdev(snip,mean)
hotness = dkdata(pix(0),pix(1))
if hotness - mean gt 5*sigma then blem = 1

; NOW CHECK IMAGE FOR BLEM
hotness = 0
chkdata = dat
snip = chkdata(pix(0)-2:pix(0)+2,pix(1)-2:pix(1)+2)
good = kill_index(indgen(25),12)
snip = snip(good)
sigma = stdev(snip,mean)
hotness = chkdata(pix(0),pix(1))
if hotness - mean gt 5*sigma then blem = 1

; NOW GO AHEAD AND DEAL WITH THE NOISY PARTS OF THE IMAGE
dsiz=size(dark1)
result=poly_fit(indgen(dsiz(1)),total(dark1,1)/dsiz(1),1,yfit)
dc=dark1
for i=0,dsiz(1)-1 do dc(i,0)=dc(i,*)-yfit  ;Remove average slope of dark frame.
sdv=stdev(dc)                      ;Estimate statistics of flattened dark frame.
smoothy=big_smooth(dat,/medsmooth,/noccd,nsmooth)    ;Prepare median smoothed image.
noiz=where(smoothy lt sdv*nbound)           ;Low-signal parts of the image.
xrayz=where(smoothy ge sdv*nbound,nxr)      ;High-signal parts of the image.
noizimg=sigma_klip(dat,n_sigma=nsigma)        ;Sigma filtered image.  
if debug then stop                            ;Stop and have a look at boundaries.


; CHOOSE SPIKE REMOVAL FACTOR
if keyword_set(level) then begin
   level=level 
endif else begin
   res=gt_res(index(0))
   case 1 of
      (res eq 0) : level=0.5*0.75
      (res eq 1) : level=1.0*0.75
      (res eq 2) : level=1.5*0.75
   endcase
endelse

spykz=where(abs(alog10(dat>1)-alog10(smoothy>1)) gt level,np)
if debug then stop			;Stop and have a look at spikes.

cleandata=noizimg                       ;Sigma-filtered image.
xrayimg=dat                             ;Want to keep dat for output.
if np gt 0 then xrayimg(spykz)=smoothy(spykz)    ;Remove spikes.
if nxr gt 0 then cleandata(xrayz)=xrayimg(xrayz) ;Restore high-signal portions.

;  REVISIT BIG SPIKES IDENTIFIED IN DARK IMAGE
kk=where(dc gt 4*sdv,aa)
smoothy=big_smooth(cleandata,/medsmooth,nsmooth)    ;Median smoothed clean image.
if aa gt 0 then cleandata(kk)=smoothy(kk)

; FINALLY, REMOVE THE COLUMN BLEM IF IT IS PRESENT
if blem then begin
   cldata = cleandata
   cldata(pix(0),pix(1)) = mean
   cldata(pix(0),pix(1)+1) = mean
   cldata(pix(0),pix(1):*) = $
      (cldata(pix(0)-1,pix(1):*)+cldata(pix(0)+1,pix(1):*))/2.
   cleandata = cldata
endif

if keyword_set(timer) then begin
   end_time = systime(1)
   run_time = (end_time-start_time)/60.
   print, 'SXT_CLEAN took: ', run_time, ' minutes to process your request'
endif

return,cleandata

end


