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