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. Return the cleaned image. ; * Steps only done for square (true) FFIs. ;CALLING SEQUENCE ; nice_img=sxt_clean(index,data,/timer) ; nice_img=sxt_clean(index,data,$ ; [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 (QR), 0.75 (HR), 0.375 (FR)] ; /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, dummy for historical reasons, no useful function. ; 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. ; For PFIs, FFI strips, or FR images only despike. ; 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. ; 6-Jan-04, LWA, Commented out print statement. ; 10-Jan-04, LWA, Clarified PROCEDURE step 8 in header. ; 12-Jul-04, LWA, Added test for FR or non-square images. ; 29-Jul-04, LWA, Mod to include despike of PFIs or FR images. ; Correction of program error that negated despike! ; Disabled 2nd pass despike based upon dark frame. ; Update header. ; 30-Jul-04, LWA, Fixed problem if no noizimg. ; Added check for non-xray images. ; 2-May-08, LWA, Move sxt_xsaa ahead of definition of xrayimg ; to prevent reintroduction of saa features. ;- ; progverno = 2.300*1000 ; new version ;print, ' ***** SXT_CLEAN Ver 2.00 5-Feb-99 ***** ; CHECK FOR NON-XRAY IMAGES. if (gt_expmode(index) ne 0 or gt_filtb(index) eq 1) then begin return,data endif if keyword_set(timer) then begin start_time = systime(1) run_time = 0. 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 ; 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 xrayimg=dat ;Want to keep dat for output. get_dc_image,index,dcindex,dark1,/dc_interpolate,/float smoothy=big_smooth(dat,/medsmooth,/noccd,nsmooth) ;Prepare median smoothed image. skip=0 ; Test for FR, PFI or FFI_strip images. if (gt_res(index) eq 0) or (gt_pfi_ffi(index,/true_ffi) eq 0) then begin skip=1 goto,skip1 endif ; TEST FOR BAD COLUMN BLEM IN DARKFRAME AND IMAGE 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 skip1 : ; NOW DEAL WITH THE NOISY (FAINT) PARTS OF THE IMAGE dsiz=size(dark1) result=poly_fit(indgen(dsiz(2)),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. noiz=where(smoothy lt sdv*nbound,nnr) ;Low-signal parts of the image. xrayz=where(smoothy ge sdv*nbound,nxr) ;High-signal parts of the image. if nnr gt 0 then begin noizimg=sigma_klip(dat,n_sigma=nsigma) ;Sigma filtered image. cleandata=noizimg ;Sigma-filtered image. endif else begin cleandata=dat endelse if nxr gt 0 then cleandata(xrayz)=xrayimg(xrayz) ;Restore high-signal portions. 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 ; REPLACE SPIKES AND HOLES WITH MED-SMOOTHED PIXELS ;spykz=where(abs(alog10(dat>1)-alog10(smoothy>1)) gt level,np) spykz=where(abs(safe_log10(dat)-safe_log10(smoothy)) $ gt level,np) if np gt 0 then begin spy=matchem(spykz,xrayz) cleandata(spy)=smoothy(spy) ;Remove spikes. endif if debug then stop ;Stop and have a look at spikes. ; Following disabled after tests, LWA, 29-Jul-04 ; 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) if skip then goto,skip2 ; 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 skip2 : 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 if keyword_set(debug) then stop return,cleandata end