Skip to content

Commit

Permalink
Merge pull request #217 from lsst/tickets/DM-41642
Browse files Browse the repository at this point in the history
DM-41642: Fix crashes when FLUX keyword is not set in header.
  • Loading branch information
erykoff committed Dec 8, 2023
2 parents 2f39c65 + ebd75d3 commit 7a272ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 12 additions & 4 deletions python/lsst/cp/pipe/ptc/cpExtractPtcTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,14 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
# containing flat exposures and their IDs.
matchType = self.config.matchExposuresType
if matchType == 'TIME':
inputs['inputExp'] = arrangeFlatsByExpTime(inputs['inputExp'], inputs['inputDims'])
inputs['inputExp'] = arrangeFlatsByExpTime(inputs['inputExp'], inputs['inputDims'], log=self.log)
elif matchType == 'FLUX':
inputs['inputExp'] = arrangeFlatsByExpFlux(inputs['inputExp'], inputs['inputDims'],
self.config.matchExposuresByFluxKeyword)
inputs['inputExp'] = arrangeFlatsByExpFlux(
inputs['inputExp'],
inputs['inputDims'],
self.config.matchExposuresByFluxKeyword,
log=self.log,
)
else:
inputs['inputExp'] = arrangeFlatsByExpId(inputs['inputExp'], inputs['inputDims'])

Expand Down Expand Up @@ -469,7 +473,11 @@ def run(self, inputExp, inputDims, taskMetadata, inputPhotodiodeData=None):
# 'expTime' can stand for exposure time, flux, or ID.
for expTime in inputExp:
exposures = inputExp[expTime]
if len(exposures) == 1:
if not np.isfinite(expTime):
self.log.warning("Illegal/missing %s found (%s). Dropping exposure %d",
self.config.matchExposuresType, expTime, exposures[0][1])
continue
elif len(exposures) == 1:
self.log.warning("Only one exposure found at %s %f. Dropping exposure %d.",
self.config.matchExposuresType, expTime, exposures[0][1])
continue
Expand Down
21 changes: 18 additions & 3 deletions python/lsst/cp/pipe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def funcAstier(pars, x):
return 0.5/(a00*gain*gain)*(np.exp(2*a00*x*gain)-1) + noise/(gain*gain) # C_00


def arrangeFlatsByExpTime(exposureList, exposureIdList):
def arrangeFlatsByExpTime(exposureList, exposureIdList, log=None):
"""Arrange exposures by exposure time.
Parameters
Expand All @@ -457,6 +457,8 @@ def arrangeFlatsByExpTime(exposureList, exposureIdList):
Input list of exposure references.
exposureIdList : `list` [`int`]
List of exposure ids as obtained by dataId[`exposure`].
log : `lsst.utils.logging.LsstLogAdapter`, optional
Log object.
Returns
------
Expand All @@ -470,13 +472,15 @@ def arrangeFlatsByExpTime(exposureList, exposureIdList):
assert len(exposureList) == len(exposureIdList), "Different lengths for exp. list and exp. ID lists"
for expRef, expId in zip(exposureList, exposureIdList):
expTime = expRef.get(component='visitInfo').exposureTime
if not np.isfinite(expTime) and log is not None:
log.warning("Exposure %d has non-finite exposure time.", expId)
listAtExpTime = flatsAtExpTime.setdefault(expTime, [])
listAtExpTime.append((expRef, expId))

return flatsAtExpTime


def arrangeFlatsByExpFlux(exposureList, exposureIdList, fluxKeyword):
def arrangeFlatsByExpFlux(exposureList, exposureIdList, fluxKeyword, log=None):
"""Arrange exposures by exposure flux.
Parameters
Expand All @@ -487,6 +491,8 @@ def arrangeFlatsByExpFlux(exposureList, exposureIdList, fluxKeyword):
List of exposure ids as obtained by dataId[`exposure`].
fluxKeyword : `str`
Header keyword that contains the flux per exposure.
log : `lsst.utils.logging.LsstLogAdapter`, optional
Log object.
Returns
-------
Expand All @@ -500,7 +506,16 @@ def arrangeFlatsByExpFlux(exposureList, exposureIdList, fluxKeyword):
assert len(exposureList) == len(exposureIdList), "Different lengths for exp. list and exp. ID lists"
for expRef, expId in zip(exposureList, exposureIdList):
# Get flux from header, assuming it is in the metadata.
expFlux = expRef.get().getMetadata()[fluxKeyword]
try:
expFlux = expRef.get().getMetadata()[fluxKeyword]
except KeyError:
# If it's missing from the header, continue; it will
# be caught and rejected when pairing exposures.
expFlux = None
if expFlux is None:
if log is not None:
log.warning("Exposure %d does not have valid header keyword %s.", expId, fluxKeyword)
expFlux = np.nan
listAtExpFlux = flatsAtExpFlux.setdefault(expFlux, [])
listAtExpFlux.append((expRef, expId))

Expand Down

0 comments on commit 7a272ac

Please sign in to comment.