Skip to content

Commit

Permalink
Simplify header provenance code.
Browse files Browse the repository at this point in the history
* Move calibration date searches to runQuantum with the rest of the
  header provenance.
* Handle inputs that change name during the header provenance (only
  brighter-fatter kernels currently).
* Retain header validation checks in the run method.
  • Loading branch information
czwa committed Aug 30, 2023
1 parent 7f3caa1 commit 9d42fb5
Showing 1 changed file with 54 additions and 46 deletions.
100 changes: 54 additions & 46 deletions python/lsst/ip/isr/isrTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,9 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):

detector = inputs['ccdExposure'].getDetector()

# This is use for header provenance.
additionalInputDates = {}

if self.config.doCrosstalk is True:
# Crosstalk sources need to be defined by the pipeline
# yaml if they exist.
Expand Down Expand Up @@ -1034,13 +1037,20 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):

# Load the correct style of brighter-fatter kernel, and repack
# the information as a numpy array.
brighterFatterSource = None
if self.config.doBrighterFatter:
brighterFatterKernel = inputs.pop('newBFKernel', None)
if brighterFatterKernel is None:
# This type of kernel must be in (y, x) index
# ordering, as it used directly as the .array
# component of the afwImage kernel.
brighterFatterKernel = inputs.get('bfKernel', None)
brighterFatterSource = 'bfKernel'
additionalInputDates[brighterFatterSource] = self.extractCalibDate(brighterFatterKernel)

if brighterFatterKernel is None:
# This was requested by the config, but none were found.
raise RuntimeError("No brighter-fatter kernel was supplied.")

if brighterFatterKernel is not None and not isinstance(brighterFatterKernel, numpy.ndarray):
# This is a ISR calib kernel. These kernels are
Expand All @@ -1049,6 +1059,9 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
# component of the afwImage kernel. This is done
# explicitly below when setting the ``bfKernel``
# input.
brighterFatterSource = 'newBFKernel'
additionalInputDates[brighterFatterSource] = self.extractCalibDate(brighterFatterKernel)

detName = detector.getName()
level = brighterFatterKernel.level

Expand Down Expand Up @@ -1091,16 +1104,30 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
if self.config.doHeaderProvenance:
# Add calibration provenanace info to header.
exposureMetadata = inputs['ccdExposure'].getMetadata()
for inputName in sorted(inputs.keys()):

# These inputs change name during this step. These should
# have matching entries in the additionalInputDates dict.
additionalInputs = []
if self.config.doBrighterFatter:
additionalInputs.append(brighterFatterSource)

for inputName in sorted(list(inputs.keys()) + additionalInputs):
reference = getattr(inputRefs, inputName, None)
if reference is not None and hasattr(reference, "run"):
runKey = f"LSST CALIB RUN {inputName.upper()}"
runValue = reference.run
idKey = f"LSST CALIB UUID {inputName.upper()}"
idValue = str(reference.id)
dateKey = f"LSST CALIB DATE {inputName.upper()}"

if inputName in additionalInputDates:
dateValue = additionalInputDates[inputName]
else:
dateValue = self.extractCalibDate(inputs[inputName])

exposureMetadata[runKey] = runValue
exposureMetadata[idKey] = idValue
exposureMetadata[dateKey] = dateValue

outputs = self.run(**inputs)
butlerQC.put(outputs, outputRefs)
Expand Down Expand Up @@ -1283,51 +1310,32 @@ def run(self, ccdExposure, *, camera=None, bias=None, linearizer=None,
if (self.config.doDeferredCharge and deferredChargeCalib is None):
raise RuntimeError("Must supply a deferred charge calibration if config.doDeferredCharge=True.")

if self.config.doHeaderProvenance:
# Inputs have been validated, so we can add their date
# information to the output header.
exposureMetadata = ccdExposure.getMetadata()
if self.config.doBias:
exposureMetadata["LSST CALIB DATE BIAS"] = self.extractCalibDate(bias)
self.compareCameraKeywords(exposureMetadata, bias, "bias")
if self.config.doBrighterFatter:
exposureMetadata["LSST CALIB DATE BFK"] = self.extractCalibDate(bfKernel)
self.compareCameraKeywords(exposureMetadata, bfKernel, "brighter-fatter")
if self.config.doCrosstalk:
exposureMetadata["LSST CALIB DATE CROSSTALK"] = self.extractCalibDate(crosstalk)
self.compareCameraKeywords(exposureMetadata, crosstalk, "crosstalk")
if self.config.doDark:
exposureMetadata["LSST CALIB DATE DARK"] = self.extractCalibDate(dark)
self.compareCameraKeywords(exposureMetadata, dark, "dark")
if self.config.doDefect:
exposureMetadata["LSST CALIB DATE DEFECTS"] = self.extractCalibDate(defects)
self.compareCameraKeywords(exposureMetadata, defects, "defects")
if self.config.doDeferredCharge:
exposureMetadata["LSST CALIB DATE CTI"] = self.extractCalibDate(deferredChargeCalib)
self.compareCameraKeywords(exposureMetadata, deferredChargeCalib, "CTI")
if self.config.doFlat:
exposureMetadata["LSST CALIB DATE FLAT"] = self.extractCalibDate(flat)
self.compareCameraKeywords(exposureMetadata, flat, "flat")
if (self.config.doFringe and physicalFilter in self.fringe.config.filters):
exposureMetadata["LSST CALIB DATE FRINGE"] = self.extractCalibDate(fringes.fringes)
self.compareCameraKeywords(exposureMetadata, fringes.fringes, "fringe")
if (self.config.doIlluminationCorrection and physicalFilter in self.config.illumFilters):
exposureMetadata["LSST CALIB DATE ILLUMINATION"] = self.extractCalibDate(illumMaskedImage)
self.compareCameraKeywords(exposureMetadata, illumMaskedImage, "illumination")
if self.doLinearize(ccd):
exposureMetadata["LSST CALIB DATE LINEARIZER"] = self.extractCalibDate(linearizer)
self.compareCameraKeywords(exposureMetadata, linearizer, "linearizer")
if self.config.usePtcGains or self.config.usePtcReadNoise:
exposureMetadata["LSST CALIB DATE PTC"] = self.extractCalibDate(ptc)
self.compareCameraKeywords(exposureMetadata, ptc, "PTC")
if self.config.doStrayLight:
exposureMetadata["LSST CALIB DATE STRAYLIGHT"] = self.extractCalibDate(strayLightData)
self.compareCameraKeywords(exposureMetadata, strayLightData, "straylight")
if self.config.doAttachTransmissionCurve:
exposureMetadata["LSST CALIB DATE OPTICS_TR"] = self.extractCalibDate(opticsTransmission)
exposureMetadata["LSST CALIB DATE FILTER_TR"] = self.extractCalibDate(filterTransmission)
exposureMetadata["LSST CALIB DATE SENSOR_TR"] = self.extractCalibDate(sensorTransmission)
exposureMetadata["LSST CALIB DATE ATMOSP_TR"] = self.extractCalibDate(atmosphereTransmission)
# Validate that the inputs match the exposure configuration.
exposureMetadata = ccdExposure.getMetadata()
if self.config.doBias:
self.compareCameraKeywords(exposureMetadata, bias, "bias")
if self.config.doBrighterFatter:
self.compareCameraKeywords(exposureMetadata, bfKernel, "brighter-fatter")
if self.config.doCrosstalk:
self.compareCameraKeywords(exposureMetadata, crosstalk, "crosstalk")
if self.config.doDark:
self.compareCameraKeywords(exposureMetadata, dark, "dark")
if self.config.doDefect:
self.compareCameraKeywords(exposureMetadata, defects, "defects")
if self.config.doDeferredCharge:
self.compareCameraKeywords(exposureMetadata, deferredChargeCalib, "CTI")
if self.config.doFlat:
self.compareCameraKeywords(exposureMetadata, flat, "flat")
if (self.config.doFringe and physicalFilter in self.fringe.config.filters):
self.compareCameraKeywords(exposureMetadata, fringes.fringes, "fringe")
if (self.config.doIlluminationCorrection and physicalFilter in self.config.illumFilters):
self.compareCameraKeywords(exposureMetadata, illumMaskedImage, "illumination")
if self.doLinearize(ccd):
self.compareCameraKeywords(exposureMetadata, linearizer, "linearizer")
if self.config.usePtcGains or self.config.usePtcReadNoise:
self.compareCameraKeywords(exposureMetadata, ptc, "PTC")
if self.config.doStrayLight:
self.compareCameraKeywords(exposureMetadata, strayLightData, "straylight")

# Begin ISR processing.
if self.config.doConvertIntToFloat:
Expand Down

0 comments on commit 9d42fb5

Please sign in to comment.