From 7fd4a0d27e5a92be93d288fcf6375ba5a8dc0df2 Mon Sep 17 00:00:00 2001
From: John Parejko <parejkoj@uw.edu>
Date: Fri, 18 Oct 2019 14:42:22 -0700
Subject: [PATCH] Disable and deprecate ScaleZeroPointTask

We need to keep it available during the deprecation period, but this
will prevent it from being active by default, and will help generate
diffim templates that have the same units as calibrated exposures.
---
 python/lsst/pipe/tasks/assembleCoadd.py | 55 ++++++++++++++++++-------
 1 file changed, 40 insertions(+), 15 deletions(-)

diff --git a/python/lsst/pipe/tasks/assembleCoadd.py b/python/lsst/pipe/tasks/assembleCoadd.py
index 143e4267e..1292298a2 100755
--- a/python/lsst/pipe/tasks/assembleCoadd.py
+++ b/python/lsst/pipe/tasks/assembleCoadd.py
@@ -164,9 +164,19 @@ class AssembleCoaddConfig(CoaddBaseTask.ConfigClass, pipeBase.PipelineTaskConfig
             "Passed to StatisticsControl.setCalcErrorFromInputVariance()",
         default=True,
     )
+    doScaleZeroPoint = pexConfig.Field(
+        dtype=bool,
+        default=False,
+        doc="Run ScaleZeroPointTask to adjust the photometric zero point of the coadd warps.",
+        deprecated=("This task exists to manage the removal of the scaleZeroPoint config below."
+                    "This will be removed after v19.")
+    )
     scaleZeroPoint = pexConfig.ConfigurableField(
         target=ScaleZeroPointTask,
-        doc="Task to adjust the photometric zero point of the coadd temp exposures",
+        doc="Task to adjust the photometric zero point of the coadd warps.",
+        deprecated=("PhotoCalib calibrates images to nJy, keeping them in the same units."
+                    " See RFC-545 for the deprecation discussion."
+                    " This will be removed after v19.")
     )
     doInterp = pexConfig.Field(
         doc="Interpolate over NaN pixels? Also extrapolate, if necessary, but the results are ugly.",
@@ -374,7 +384,10 @@ def __init__(self, *args, **kwargs):
 
         super().__init__(**kwargs)
         self.makeSubtask("interpImage")
-        self.makeSubtask("scaleZeroPoint")
+        if self.config.doScaleZeroPoint:
+            self.makeSubtask("scaleZeroPoint")
+        else:
+            self.scaleZeroPoint = None
 
         if self.config.doMaskBrightObjects:
             mask = afwImage.Mask()
@@ -649,15 +662,18 @@ def prepareInputs(self, refList):
             if numpy.isnan(tempExp.image.array).all():
                 continue
             maskedImage = tempExp.getMaskedImage()
-            imageScaler = self.scaleZeroPoint.computeImageScaler(
-                exposure=tempExp,
-                dataRef=tempExpRef,
-            )
-            try:
-                imageScaler.scaleMaskedImage(maskedImage)
-            except Exception as e:
-                self.log.warn("Scaling failed for %s (skipping it): %s", tempExpRef.dataId, e)
-                continue
+
+            if self.scaleZeroPoint is not None:
+                imageScaler = self.scaleZeroPoint.computeImageScaler(
+                    exposure=tempExp,
+                    dataRef=tempExpRef,
+                )
+                try:
+                    imageScaler.scaleMaskedImage(maskedImage)
+                except Exception as e:
+                    self.log.warn("Scaling failed for %s (skipping it): %s", tempExpRef.dataId, e)
+                    continue
+
             statObj = afwMath.makeStatistics(maskedImage.getVariance(), maskedImage.getMask(),
                                              afwMath.MEANCLIP, statsCtrl)
             meanVar, meanVarErr = statObj.getResult(afwMath.MEANCLIP)
@@ -672,7 +688,10 @@ def prepareInputs(self, refList):
 
             tempExpRefList.append(tempExpRef)
             weightList.append(weight)
-            imageScalerList.append(imageScaler)
+            if self.scaleZeroPoint is not None:
+                imageScalerList.append(imageScaler)
+            else:
+                imageScalerList.append(None)
 
         return pipeBase.Struct(tempExpRefList=tempExpRefList, weightList=weightList,
                                imageScalerList=imageScalerList)
@@ -763,7 +782,11 @@ def run(self, skyInfo, tempExpRefList, imageScalerList, weightList,
             altMaskList = [None]*len(tempExpRefList)
 
         coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs)
-        coaddExposure.setPhotoCalib(self.scaleZeroPoint.getPhotoCalib())
+        if self.scaleZeroPoint is None:
+            # The coadd is in nJy, and we do not propogate errors from the warps.
+            coaddExposure.setPhotoCalib(afwImage.PhotoCalib(1.0, 0.0))
+        else:
+            coaddExposure.setPhotoCalib(self.scaleZeroPoint.getPhotoCalib())
         coaddExposure.getInfo().setCoaddInputs(self.inputRecorder.makeCoaddInputs())
         self.assembleMetadata(coaddExposure, tempExpRefList, weightList)
         coaddMaskedImage = coaddExposure.getMaskedImage()
@@ -910,7 +933,8 @@ def assembleSubregion(self, coaddExposure, bbox, tempExpRefList, imageScalerList
             mask = maskedImage.getMask()
             if altMask is not None:
                 self.applyAltMaskPlanes(mask, altMask)
-            imageScaler.scaleMaskedImage(maskedImage)
+            if imageScaler is not None:
+                imageScaler.scaleMaskedImage(maskedImage)
 
             # Add 1 for each pixel which is not excluded by the exclude mask.
             # In legacyCoadd, pixels may also be excluded by afwMath.statisticsStack.
@@ -2359,7 +2383,8 @@ def _readAndComputeWarpDiff(self, warpRef, imageScaler, templateCoadd):
                 return None
         warp = warpRef.get(datasetType=warpName, immediate=True)
         # direct image scaler OK for PSF-matched Warp
-        imageScaler.scaleMaskedImage(warp.getMaskedImage())
+        if imageScaler is not None:
+            imageScaler.scaleMaskedImage(warp.getMaskedImage())
         mi = warp.getMaskedImage()
         if self.config.doScaleWarpVariance:
             try: