@@ -426,7 +426,7 @@ def run(self, template, science, sources, visitSummary=None):
426426 except (RuntimeError , lsst .pex .exceptions .Exception ) as e :
427427 self .log .warning ("Failed to match template. Checking coverage" )
428428 # Raise NoWorkFound if template fraction is insufficient
429- checkTemplateIsSufficient (template [science .getBBox ()], self .log ,
429+ checkTemplateIsSufficient (template [science .getBBox ()], science , self .log ,
430430 self .config .minTemplateFractionForExpectedSuccess ,
431431 exceptionMessage = "Template coverage lower than expected to succeed."
432432 f" Failure is tolerable: { e } " )
@@ -835,7 +835,7 @@ def _prepareInputs(self, template, science, visitSummary=None):
835835 if visitSummary is not None :
836836 self ._applyExternalCalibrations (science , visitSummary = visitSummary )
837837 templateCoverageFraction = checkTemplateIsSufficient (
838- template [science .getBBox ()], self .log ,
838+ template [science .getBBox ()], science , self .log ,
839839 requiredTemplateFraction = self .config .requiredTemplateFraction ,
840840 exceptionMessage = "Not attempting subtraction. To force subtraction,"
841841 " set config requiredTemplateFraction=0"
@@ -1044,7 +1044,7 @@ def run(self, template, science, sources, visitSummary=None):
10441044 except (RuntimeError , lsst .pex .exceptions .Exception ) as e :
10451045 self .log .warning ("Failed to match template. Checking coverage" )
10461046 # Raise NoWorkFound if template fraction is insufficient
1047- checkTemplateIsSufficient (template [science .getBBox ()], self .log ,
1047+ checkTemplateIsSufficient (template [science .getBBox ()], science , self .log ,
10481048 self .config .minTemplateFractionForExpectedSuccess ,
10491049 exceptionMessage = "Template coverage lower than expected to succeed."
10501050 f" Failure is tolerable: { e } " )
@@ -1129,7 +1129,7 @@ def runPreconvolve(self, template, science, matchedScience, selectSources, preCo
11291129 kernelSources = kernelSources )
11301130
11311131
1132- def checkTemplateIsSufficient (templateExposure , logger , requiredTemplateFraction = 0. ,
1132+ def checkTemplateIsSufficient (templateExposure , scienceExposure , logger , requiredTemplateFraction = 0. ,
11331133 exceptionMessage = "" ):
11341134 """Raise NoWorkFound if template coverage < requiredTemplateFraction
11351135
@@ -1159,8 +1159,11 @@ def checkTemplateIsSufficient(templateExposure, logger, requiredTemplateFraction
11591159 """
11601160 # Count the number of pixels with the NO_DATA mask bit set
11611161 # counting NaN pixels is insufficient because pixels without data are often intepolated over)
1162- pixNoData = np .count_nonzero (templateExposure .mask .array
1163- & templateExposure .mask .getPlaneBitMask ('NO_DATA' ))
1162+ noTemplate = templateExposure .mask .array & templateExposure .mask .getPlaneBitMask ('NO_DATA' )
1163+ # Also need to account for missing data in the science image,
1164+ # because template coverage there doesn't help
1165+ noScience = scienceExposure .mask .array & scienceExposure .mask .getPlaneBitMask ('NO_DATA' )
1166+ pixNoData = np .count_nonzero (noTemplate | noScience )
11641167 pixGood = templateExposure .getBBox ().getArea () - pixNoData
11651168 templateCoverageFraction = pixGood / templateExposure .getBBox ().getArea ()
11661169 logger .info ("template has %d good pixels (%.1f%%)" , pixGood , 100 * templateCoverageFraction )
0 commit comments