diff --git a/python/lsst/ip/diffim/getTemplate.py b/python/lsst/ip/diffim/getTemplate.py index 04e9d5ce6..aeb498e1c 100644 --- a/python/lsst/ip/diffim/getTemplate.py +++ b/python/lsst/ip/diffim/getTemplate.py @@ -398,7 +398,9 @@ def _merge(self, maskedImages, bbox, wcs): merged = afwImage.ExposureF(bbox, wcs) weights = afwImage.ImageF(bbox) for maskedImage in maskedImages: - weight = afwImage.ImageF(maskedImage.variance.array**(-0.5)) + goodPix = maskedImage.variance.array > 0 + weight = afwImage.ImageF(maskedImage.getBBox()) + weight.array[goodPix] = maskedImage.variance.array[goodPix]**(-0.5) bad = np.isnan(maskedImage.image.array) | ~np.isfinite(maskedImage.variance.array) # Note that modifying the patch MaskedImage in place is fine; # we're throwing it away at the end anyway. @@ -419,9 +421,13 @@ def _merge(self, maskedImages, bbox, wcs): # Cannot use `merged.maskedImage /= weights` because that operator # divides the variance by the weight twice; in this case `weights` are # the exact values we want to scale by. - merged.image /= weights - merged.variance /= weights - merged.mask.array |= merged.mask.getPlaneBitMask("NO_DATA") * (weights.array == 0) + invWeights = np.zeros_like(weights.array) + goodPix = weights.array > 0 + invWeights[goodPix] = 1/weights.array[goodPix] + + merged.image.array *= invWeights + merged.variance.array *= invWeights + merged.mask.array |= merged.mask.getPlaneBitMask("NO_DATA") * (invWeights == 0) return merged diff --git a/tests/test_getTemplate.py b/tests/test_getTemplate.py index a9b00e9b1..0da547356 100644 --- a/tests/test_getTemplate.py +++ b/tests/test_getTemplate.py @@ -282,9 +282,7 @@ def testMissingPatches(self): dataIds=self.dataIds, physical_filter="a_test") no_data = (result.template.mask.array & result.template.mask.getPlaneBitMask("NO_DATA")) != 0 - self.assertTrue(all(np.isnan(result.template.image.array[no_data]))) - self.assertTrue(all(np.isnan(result.template.variance.array[no_data]))) - self.assertEqual(no_data.sum(), 21548) + self.assertEqual(no_data.sum(), 20990) def setup_module(module):