Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-41642: Fix crashes when FLUX keyword is not set in header. #217

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a keyword written by some other task or something that comes from a raw header? Should we issue a log message if you think this should be there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is logged as missing afterwords. I didn't want to add a log in here just to get things unblocked.

But it turns out the source of this problem is that one of the data taking controllers was giving corrupt header information and what got into the repo depended on the order of ingestion of the different detectors. Not great!

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