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