function sxt_diffuser2,index,lo8_data,comp_data,iterate=iterate,nn=nn, $
                     spike=spike,dust=dust,ff_smooth=ff_smooth

;+
;	PROGRAM NAME
;		SXT_DIFFUSER2
;		The difference from SXT_DIFFUSER is a variable smoothing
;		width set by the value of iterate..
;	PURPOSE
;		To return a flat-fielded diffuser image for the purpose
;		of monitoring x-ray damage to the CCD.
;	CALLING SEQUENCE
;		out=sxt_diffuser2(index,lo8_data,comp_data, $
;			[iterate=iterate,nn=nn,spike=spike, $
;			 dust=dust,ff_smooth=ff_smooth]
;		out=sxt_diffuser2(index,lo8_data,comp_data)
;		out=sxt_diffuser2(index,lo8_data,com_data,interate=6, $
;                               nn=25,spike=300,/dust)
;	INPUT
;		index = index of either image, used for dark subtraction.
;		lo8_data = diffuser image taken in lo8 mode.
;		comp_data = compressed mode diffuser image.
;	OPTIONAL KEYWORD INPUT
;		interate = number of iterations in smoothing
;			default = 6 iterations
;		nn = initial smoothing window
;			default = 17
;		spike = amplitude of spike to be removed, default=75.
;			set spike = 1000 to keep from removing spikes
;		dust = set this to prevent removal of 2 big dust spots
;		ff_smooth = do final smoothing with FFT.
;	METHOD
;		1.  Form reconstructed 12 bit image.
;		2.  Subtract background, remove dust and fix edges.
;		3.  Remove single pixel spikes.
;		4.  Iteratively prepare a smoothed image.
;		5.  Subtract smooth image from original image.
;		6.  Further smoothing with Fourier Transform.
;	HISTORY
;		Written by LWA on 6-Aug-93
;		Modified to use SPIKES and BIG_SMOOTH_DFSR.   4/29/94  LWA
;-

wdef,28

;   Form the reconstructed image.

img = restore_ffi(lo8_data,comp_data)
tv,img

;   Remove background.

img = dark_sub(index,img)

;   Remove dust features

if n_elements(dust) eq 0 then begin
   img = dustoff(img)
endif

;   Replace spurious data in first and last rows

img(0,*) = img(1,*)
img(511,*) = img(510,*)
tv,img

;   Remove spikes.

if NOT keyword_set(spike) then spike = 75 else spike = spike
if spike ne 1000 then begin
   xx = spikes(img,spike,/remove)
endif

;   Prepare smoothed image.  This may be a crude way to do it but it
;   seems to do a reasonable job.

if NOT keyword_set(iterate) then n = 6 else n = iterate
if NOT keyword_set(nn) then nn = 17 else nn = nn
temp = img
tv,temp
for i = 0,n-1 do begin

   temp = big_smooth_dfsr(temp,nn+3*i)

endfor

out = img - temp
tvscl,out

;   Finally, remove additional low frequency signal with FFT.

if (keyword_set(ff_smooth)) then begin
   f=dist(512,512)
   f=f gt 3
   r=fft(fft(out,-1)*f,1)
   out=float(r)
   tvscl,out
endif

return, out

end
