Skip to content

Commit

Permalink
Move getCalibratedExposure to MakeCoaddTempExp
Browse files Browse the repository at this point in the history
  • Loading branch information
parejkoj committed Nov 20, 2018
1 parent 6113dde commit 239cf53
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 68 deletions.
61 changes: 0 additions & 61 deletions python/lsst/pipe/tasks/coaddBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import lsst.pex.config as pexConfig
import lsst.afw.geom as afwGeom
import lsst.afw.image as afwImage
import lsst.daf.persistence as dafPersistence
import lsst.pipe.base as pipeBase
import lsst.meas.algorithms as measAlg

Expand Down Expand Up @@ -142,66 +141,6 @@ def getSkyInfo(self, patchRef):
"""
return getSkyInfo(coaddName=self.config.coaddName, patchRef=patchRef)

def getCalibratedExposure(self, dataRef, bgSubtracted):
"""Return one calibrated Exposure, possibly with an updated SkyWcs.
@param[in] dataRef a sensor-level data reference
@param[in] bgSubtracted return calexp with background subtracted? If False get the
calexp's background background model and add it to the calexp.
@return calibrated exposure
If config.doApplyUberCal, the exposure will be photometrically
calibrated via the `jointcal_photoCalib` dataset and have its SkyWcs
updated to the `jointcal_wcs`, otherwise it will be calibrated via the
Exposure's own Calib and have the original SkyWcs.
"""
exposure = dataRef.get("calexp", immediate=True)
if not bgSubtracted:
background = dataRef.get("calexpBackground", immediate=True)
mi = exposure.getMaskedImage()
mi += background.getImage()
del mi

if self.config.doApplyUberCal:
if self.config.useMeasMosaic:
try:
from lsst.meas.mosaic import applyMosaicResultsExposure
# NOTE: this changes exposure in-place, updating its Calib and Wcs.
# Save the calibration error, as it gets overwritten with zero.
fluxMag0Err = exposure.getCalib().getFluxMag0()[1]
applyMosaicResultsExposure(dataRef, calexp=exposure)
fluxMag0 = exposure.getCalib().getFluxMag0()[0]
photoCalib = afwImage.PhotoCalib(1.0/fluxMag0,
fluxMag0Err/fluxMag0**2,
exposure.getBBox())
except ImportError:
msg = "Cannot apply meas_mosaic calibration: meas_mosaic not available."
raise RuntimeError(msg.format(dataRef.dataId))
else:
try:
photoCalib = dataRef.get("jointcal_photoCalib")
except dafPersistence.NoResults:
msg = "Cannot apply jointcal calibration: `jointcal_photoCalib` not found for dataRef {}."
raise RuntimeError(msg.format(dataRef.dataId))
try:
skyWcs = dataRef.get("jointcal_wcs")
exposure.setWcs(skyWcs)
except dafPersistence.NoResults:
msg = "Cannot update to jointcal SkyWcs: `jointcal_wcs` not found for dataRef {}."
raise RuntimeError(msg.format(dataRef.dataId))
else:
fluxMag0 = exposure.getCalib().getFluxMag0()
photoCalib = afwImage.PhotoCalib(1.0/fluxMag0[0],
fluxMag0[1]/fluxMag0[0]**2,
exposure.getBBox())

exposure.maskedImage = photoCalib.calibrateImage(exposure.maskedImage)
exposure.maskedImage /= photoCalib.getCalibrationMean()
exposure.setCalib(afwImage.Calib(1/photoCalib.getCalibrationMean()))
# TODO: The images will have a calibration of 1.0 everywhere once RFC-545 is implemented.
# exposure.setCalib(afwImage.Calib(1.0))
return exposure

def getCoaddDatasetName(self, warpType="direct"):
"""Return coadd name for given warpType and task config
Expand Down
61 changes: 61 additions & 0 deletions python/lsst/pipe/tasks/makeCoaddTempExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import lsst.pex.config as pexConfig
import lsst.afw.image as afwImage
import lsst.daf.persistence as dafPersistence
import lsst.coadd.utils as coaddUtils
import lsst.pipe.base as pipeBase
import lsst.log as log
Expand Down Expand Up @@ -430,6 +431,66 @@ def run(self, calexpRefList, skyInfo, visitId=0):
result = pipeBase.Struct(exposures=coaddTempExps)
return result

def getCalibratedExposure(self, dataRef, bgSubtracted):
"""Return one calibrated Exposure, possibly with an updated SkyWcs.
@param[in] dataRef a sensor-level data reference
@param[in] bgSubtracted return calexp with background subtracted? If False get the
calexp's background background model and add it to the calexp.
@return calibrated exposure
If config.doApplyUberCal, the exposure will be photometrically
calibrated via the `jointcal_photoCalib` dataset and have its SkyWcs
updated to the `jointcal_wcs`, otherwise it will be calibrated via the
Exposure's own Calib and have the original SkyWcs.
"""
exposure = dataRef.get("calexp", immediate=True)
if not bgSubtracted:
background = dataRef.get("calexpBackground", immediate=True)
mi = exposure.getMaskedImage()
mi += background.getImage()
del mi

if self.config.doApplyUberCal:
if self.config.useMeasMosaic:
try:
from lsst.meas.mosaic import applyMosaicResultsExposure
# NOTE: this changes exposure in-place, updating its Calib and Wcs.
# Save the calibration error, as it gets overwritten with zero.
fluxMag0Err = exposure.getCalib().getFluxMag0()[1]
applyMosaicResultsExposure(dataRef, calexp=exposure)
fluxMag0 = exposure.getCalib().getFluxMag0()[0]
photoCalib = afwImage.PhotoCalib(1.0/fluxMag0,
fluxMag0Err/fluxMag0**2,
exposure.getBBox())
except ImportError:
msg = "Cannot apply meas_mosaic calibration: meas_mosaic not available."
raise RuntimeError(msg.format(dataRef.dataId))
else:
try:
photoCalib = dataRef.get("jointcal_photoCalib")
except dafPersistence.NoResults:
msg = "Cannot apply jointcal calibration: `jointcal_photoCalib` not found for dataRef {}."
raise RuntimeError(msg.format(dataRef.dataId))
try:
skyWcs = dataRef.get("jointcal_wcs")
exposure.setWcs(skyWcs)
except dafPersistence.NoResults:
msg = "Cannot update to jointcal SkyWcs: `jointcal_wcs` not found for dataRef {}."
raise RuntimeError(msg.format(dataRef.dataId))
else:
fluxMag0 = exposure.getCalib().getFluxMag0()
photoCalib = afwImage.PhotoCalib(1.0/fluxMag0[0],
fluxMag0[1]/fluxMag0[0]**2,
exposure.getBBox())

exposure.maskedImage = photoCalib.calibrateImage(exposure.maskedImage)
exposure.maskedImage /= photoCalib.getCalibrationMean()
exposure.setCalib(afwImage.Calib(1/photoCalib.getCalibrationMean()))
# TODO: The images will have a calibration of 1.0 everywhere once RFC-545 is implemented.
# exposure.setCalib(afwImage.Calib(1.0))
return exposure

@staticmethod
def _prepareEmptyExposure(skyInfo):
"""Produce an empty exposure for a given patch"""
Expand Down
14 changes: 7 additions & 7 deletions tests/test_coaddBase.py → tests/test_makeCoaddTempExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

import lsst.afw.image
import lsst.daf.persistence
from lsst.pipe.tasks.coaddBase import CoaddBaseTask, CoaddBaseConfig
from lsst.pipe.tasks.makeCoaddTempExp import MakeCoaddTempExpTask, MakeCoaddTempExpConfig


class CoaddBaseTestCase(lsst.utils.tests.TestCase):
class MakeCoaddTempExpTestCase(lsst.utils.tests.TestCase):
def setUp(self):
np.random.seed(10)

self.config = CoaddBaseConfig()
self.config = MakeCoaddTempExpConfig()

# TODO DM-10156: once Calib is replaced, this will be much cleaner
meanCalibration = 1e-4
Expand Down Expand Up @@ -86,7 +86,7 @@ def mockGet(datasetType, dataId=None, immediate=None):
self.dataRef.dataId = {"ccd": 10000, "visit": 1}

def test_getCalibratedExposure(self):
task = CoaddBaseTask(self.config, "testCoaddBase")
task = MakeCoaddTempExpTask(config=self.config)

expect = self.exposurePhotoCalib.calibrateImage(self.exposure.maskedImage)
expect /= self.exposurePhotoCalib.getCalibrationMean()
Expand All @@ -99,14 +99,14 @@ def test_getCalibratedExposure(self):

def test_getCalibratedExposureNoJointcalPhotoCalib(self):
self.config.doApplyUberCal = True
task = CoaddBaseTask(self.config, "testCoaddBase")
task = MakeCoaddTempExpTask(config=self.config)

with(self.assertRaises(RuntimeError)):
task.getCalibratedExposure(self.dataRef, True)

def test_getCalibratedExposureNoJointcalWcs(self):
self.config.doApplyUberCal = True
task = CoaddBaseTask(self.config, "testCoaddBase")
task = MakeCoaddTempExpTask(config=self.config)

self.raiseOnGetPhotoCalib = False

Expand All @@ -115,7 +115,7 @@ def test_getCalibratedExposureNoJointcalWcs(self):

def test_getCalibratedExposureJointcal(self):
self.config.doApplyUberCal = True
task = CoaddBaseTask(self.config, "testCoaddBase")
task = MakeCoaddTempExpTask(config=self.config)

self.raiseOnGetPhotoCalib = False
self.raiseOnGetWcs = False
Expand Down

0 comments on commit 239cf53

Please sign in to comment.