Skip to content

Commit

Permalink
Merge branch 'tickets/DM-30927'
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenam committed Sep 30, 2022
2 parents 369a80f + 6f240ff commit 3906b76
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 163 deletions.
4 changes: 3 additions & 1 deletion python/lsst/pipe/tasks/calexpCutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ def run(self, in_table, calexp):
raise ValueError('Required column missing from the input table. '
'Required columns are "position", "xspan", and "yspan". '
f'The column names are: {in_table.colnames}')
wcs = calexp.getWcs()
if wcs is None:
raise RuntimeError("Calexp has no WCS, so cannot compute sky positions.")
max_idx = self.config.max_cutouts
cutout_list = []
wcs = calexp.getWcs()
mim = calexp.getMaskedImage()
ras = []
decs = []
Expand Down
88 changes: 55 additions & 33 deletions python/lsst/pipe/tasks/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import math
import warnings
import numpy as np

from lsstDebug import getDebugFrame
import lsst.pex.config as pexConfig
Expand Down Expand Up @@ -357,7 +358,8 @@ class CalibrateTask(pipeBase.PipelineTask):
RuntimeError
Raised if any of the following occur:
- isSourceCat is missing fields specified in icSourceFieldsToCopy.
- PipelineTask form of this task is initialized with reference object loaders.
- PipelineTask form of this task is initialized with reference object
loaders.
Notes
-----
Expand Down Expand Up @@ -498,12 +500,17 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
outputs = self.run(**inputs)

if self.config.doWriteMatches and self.config.doAstrometry:
normalizedMatches = afwTable.packMatches(outputs.astromMatches)
normalizedMatches.table.setMetadata(outputs.matchMeta)
if self.config.doWriteMatchesDenormalized:
denormMatches = denormalizeMatches(outputs.astromMatches, outputs.matchMeta)
outputs.matchesDenormalized = denormMatches
outputs.matches = normalizedMatches
if outputs.astromMatches is not None:
normalizedMatches = afwTable.packMatches(outputs.astromMatches)
normalizedMatches.table.setMetadata(outputs.matchMeta)
if self.config.doWriteMatchesDenormalized:
denormMatches = denormalizeMatches(outputs.astromMatches, outputs.matchMeta)
outputs.matchesDenormalized = denormMatches
outputs.matches = normalizedMatches
else:
del outputRefs.matches
if self.config.doWriteMatchesDenormalized:
del outputRefs.matchesDenormalized
butlerQC.put(outputs, outputRefs)

@timeMethod
Expand All @@ -516,12 +523,13 @@ def run(self, exposure, exposureIdInfo=None, background=None,
exposure : `lsst.afw.image.ExposureF`
Exposure to calibrate.
exposureIdInfo : `lsst.obs.baseExposureIdInfo`, optional
Exposure ID info. If not provided, returned SourceCatalog IDs will not
be globally unique.
Exposure ID info. If not provided, returned SourceCatalog IDs will
not be globally unique.
background : `lsst.afw.math.BackgroundList`, optional
Initial model of background already subtracted from exposure.
icSourceCat : `lsst.afw.image.SourceCatalog`, optional
SourceCatalog from CharacterizeImageTask from which we can copy some fields.
SourceCatalog from CharacterizeImageTask from which we can copy
some fields.
Returns
-------
Expand Down Expand Up @@ -599,34 +607,47 @@ def run(self, exposure, exposureIdInfo=None, background=None,
astromMatches = None
matchMeta = None
if self.config.doAstrometry:
try:
astromRes = self.astrometry.run(
exposure=exposure,
sourceCat=sourceCat,
)
astromMatches = astromRes.matches
matchMeta = astromRes.matchMeta
except Exception as e:
astromRes = self.astrometry.run(
exposure=exposure,
sourceCat=sourceCat,
)
astromMatches = astromRes.matches
matchMeta = astromRes.matchMeta
if exposure.getWcs() is None:
if self.config.requireAstrometry:
raise
self.log.warning("Unable to perform astrometric calibration "
"(%s): attempting to proceed", e)
raise RuntimeError(f"WCS fit failed for {exposureIdInfo.expId} and requireAstrometry "
"is True.")
else:
self.log.warning("Unable to perform astrometric calibration for %r but "
"requireAstrometry is False: attempting to proceed...",
exposureIdInfo.expId)

# compute photometric calibration
if self.config.doPhotoCal:
try:
photoRes = self.photoCal.run(exposure, sourceCat=sourceCat, expId=exposureIdInfo.expId)
exposure.setPhotoCalib(photoRes.photoCalib)
# TODO: reword this to phrase it in terms of the calibration factor?
self.log.info("Photometric zero-point: %f",
photoRes.photoCalib.instFluxToMagnitude(1.0))
self.setMetadata(exposure=exposure, photoRes=photoRes)
except Exception as e:
if np.all(np.isnan(sourceCat["coord_ra"])) or np.all(np.isnan(sourceCat["coord_dec"])):
if self.config.requirePhotoCal:
raise
self.log.warning("Unable to perform photometric calibration "
"(%s): attempting to proceed", e)
raise RuntimeError(f"Astrometry failed for {exposureIdInfo.expId}, so cannot do "
"photoCal, but requirePhotoCal is True.")
self.log.warning("Astrometry failed for %r, so cannot do photoCal. requirePhotoCal "
"is False, so skipping photometric calibration and setting photoCalib "
"to None. Attempting to proceed...", exposureIdInfo.expId)
exposure.setPhotoCalib(None)
self.setMetadata(exposure=exposure, photoRes=None)
else:
try:
photoRes = self.photoCal.run(exposure, sourceCat=sourceCat, expId=exposureIdInfo.expId)
exposure.setPhotoCalib(photoRes.photoCalib)
# TODO: reword this to phrase it in terms of the
# calibration factor?
self.log.info("Photometric zero-point: %f",
photoRes.photoCalib.instFluxToMagnitude(1.0))
self.setMetadata(exposure=exposure, photoRes=photoRes)
except Exception as e:
if self.config.requirePhotoCal:
raise
self.log.warning("Unable to perform photometric calibration "
"(%s): attempting to proceed", e)
self.setMetadata(exposure=exposure, photoRes=None)

self.postCalibrationMeasurement.run(
measCat=sourceCat,
Expand Down Expand Up @@ -706,7 +727,8 @@ def setMetadata(self, exposure, photoRes=None):
def copyIcSourceFields(self, icSourceCat, sourceCat):
"""Match sources in an icSourceCat and a sourceCat and copy fields.
The fields copied are those specified by ``config.icSourceFieldsToCopy``.
The fields copied are those specified by
``config.icSourceFieldsToCopy``.
Parameters
----------
Expand Down

0 comments on commit 3906b76

Please sign in to comment.