@@ -1891,6 +1891,49 @@ def _remeasure_star_background(self, result, background_to_photometric_ratio=Non
18911891 ).background
18921892 result .background .append (star_background [0 ])
18931893
1894+ # Perform one more round of background subtraction that is just an
1895+ # overall pedestal (order = 0). This is intended to account for
1896+ # any potential gross oversubtraction imposed by the higher-order
1897+ # subtraction.
1898+ # Dilate DETECTED mask a bit more if it's below 50% detected.
1899+ nPixToDilate = 2
1900+ if detected_fraction < 0.5 :
1901+ dilatedMask = result .exposure .mask .clone ()
1902+ for maskName in detected_mask_planes :
1903+ # Compute the grown detection mask plane using SpanSet
1904+ detectedMaskBit = dilatedMask .getPlaneBitMask (maskName )
1905+ detectedMaskSpanSet = SpanSet .fromMask (dilatedMask , detectedMaskBit )
1906+ detectedMaskSpanSet = detectedMaskSpanSet .dilated (nPixToDilate )
1907+ detectedMaskSpanSet = detectedMaskSpanSet .clippedTo (dilatedMask .getBBox ())
1908+ # Clear the detected mask plane
1909+ detectedMask = dilatedMask .getMaskPlane (maskName )
1910+ dilatedMask .clearMaskPlane (detectedMask )
1911+ # Set the mask plane to the dilated one
1912+ detectedMaskSpanSet .setMask (dilatedMask , detectedMaskBit )
1913+
1914+ detected_fraction_dilated = self ._compute_mask_fraction (dilatedMask ,
1915+ detected_mask_planes ,
1916+ bad_mask_planes )
1917+ result .exposure .mask = dilatedMask
1918+ self .log .debug ("detected_fraction_orig = %.3f detected_fraction_dilated = %.3f" ,
1919+ detected_fraction_orig , detected_fraction_dilated )
1920+
1921+ pedestalBackgroundConfig = lsst .meas .algorithms .SubtractBackgroundConfig ()
1922+ pedestalBackgroundConfig .statisticsProperty = "MEDIAN"
1923+ pedestalBackgroundConfig .approxOrderX = 0
1924+ pedestalBackgroundConfig .binSize = 64
1925+ pedestalBackgroundTask = lsst .meas .algorithms .SubtractBackgroundTask (config = pedestalBackgroundConfig )
1926+ pedestalBackgroundList = pedestalBackgroundTask .run (
1927+ exposure = result .exposure ,
1928+ background = result .background ,
1929+ backgroundToPhotometricRatio = background_to_photometric_ratio ,
1930+ ).background
1931+ # Isolate the final pedestal background to log the computed value
1932+ pedestalBackground = afwMath .BackgroundList ()
1933+ pedestalBackground .append (pedestalBackgroundList [1 ])
1934+ pedestalBackgroundLevel = pedestalBackground .getImage ().array [0 , 0 ]
1935+ self .log .warning ("Subtracted pedestalBackgroundLevel = %.4f" , pedestalBackgroundLevel )
1936+
18941937 # Clear detected mask plane before final round of detection
18951938 mask = result .exposure .mask
18961939 for mp in detected_mask_planes :
0 commit comments