;+ ; NAME: ; CleanGlitchBox ; PURPOSE: ; Given that a glitch has been detected (specified as a single pixel, ; check a neighbourhood of the pixel to decide whether the glitch ; covers more than one pixel. ; CATEGORY: ; Avoidable ? ; CALLING SEQUENCE: FUNCTION CleanGlitchBox, Frames, Loc, spotwidth=SpotWidth, frac=frac ; INPUTS: ; Frames 3D-array, any type (float or double needed to use NaN option) ; stack of 2D frames combined in 3D array; the last dimension counts ; the number of frames ; Frame elements set to the value !VALUES.F_NAN or D_NAN are ignored ; Loc 1-dim array, type long integer ; location of pixels already identified as part of glitches ; specified as 1-dim indices into the Frames array. ; OPTIONAL INPUT PARAMETERS: ; spotwidth=SpotWidth ; scalar, integer, default=1 ; should be an odd integer. ; defines a neighbourhood of SpotWidth x Spotwidth pixels ; !! If SpotWidth is not defined the procedure simply ; returns the input Loc array unmodified. ; frac=frac scalar, any type, default=1 ; Used in the criterion for deciding whether a pixel is part of ; a glitch or not. See PROCEDURE. ; OUTPUTS: ; nLoc scalar, long integer ; # elements in Loc (=n_elements(Loc)) ; Loc 1-dim array, type long integer ; location of the glitches as 1-dim indices into the Frames array ; The array will contain all the input values. Added are all pixels ; that are identified as part of the glitch ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; ArrayLocation, BadValue ; PROCEDURE: ; For each element in the Loc array a box of SpotWidth x SpotWidth in the ; appropriate frame is processed. Minimum and median in the box are calculated ; excluding the center of the box. ; The difference between median and minimum is used to identify other pixels ; in the box which are considered part of the central glitch. Any pixel more ; than frac*(median-minimum) above the median is considered part of the glitch. ; MODIFICATION HISTORY: ; OCT-1998, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ;- IF n_elements(SpotWidth) EQ 0 THEN dspot = 0 ELSE dspot = (SpotWidth[0]-1)/2 nLoc = n_elements(Loc) IF nLoc*dspot GT 0 THEN BEGIN IF n_elements(frac) EQ 0 THEN frac = 1. ELSE frac = frac[0] sz = size(Frames) Pos = ArrayLocation(Loc,size=sz) ; x,y position in frame, and frame number b = (Pos[0:1,*]-dspot) > 0 ; Edges of box e = (Pos[0:1,*]+dspot) < (sz[1:2]-1)#replicate(1L,nLoc) FOR n=0,nLoc-1 DO BEGIN ; Loop over glitches F = Frames[b[0,n]:e[0,n],b[1,n]:e[1,n],Pos[2,n]] ; Extract box around glitch F[Pos[0,n]-b[0,n],Pos[1,n]-b[1,n]] = BadValue(F) ; Set center of box to NaN p = where(finite(F)) ; Finite elements of F Fp = F[p] minF = min (Fp) medF = median(Fp) ; KLUDGE: IF medF EQ minF THEN BEGIN ; Flat box: minimum=median m = where(Fp GT minF) ; Find elements above minimum IF m[0] NE -1 THEN medF = min(Fp[m]); Pick the smallest above minimum ENDIF ; The difference between median and minimum is used to identify other pixels ; in the box that are considered part of the central glitch. m = where (Fp-medF GT frac*(medF-minF),s) IF m[0] NE -1 THEN BEGIN m = ArrayLocation(p[m],size=size(F)) m = [b[0,n]+m[0,*],b[1,n]+m[1,*],transpose(replicate(Pos[2,n],s))] Loc = [Loc, ArrayLocation(m, size=sz, /onedim) ] ENDIF ENDFOR Loc = Loc[uniq(Loc,sort(Loc))] ; Remove double entries nLoc = n_elements(Loc) ENDIF RETURN, nLoc & END