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-16292: Propagate input headers to the output calibration files #70

Merged
merged 3 commits into from
Mar 19, 2019
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
60 changes: 58 additions & 2 deletions python/lsst/pipe/drivers/constructCalibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from builtins import zip
from builtins import range

from astro_metadata_translator import merge_headers, ObservationGroup
from astro_metadata_translator.serialize import dates_to_fits

from lsst.pex.config import Config, ConfigurableField, Field, ListField, ConfigField
from lsst.pipe.base import Task, Struct, TaskRunner, ArgumentParser
import lsst.daf.base as dafBase
Expand Down Expand Up @@ -750,6 +753,8 @@ def combine(self, cache, struct, outputId):
else:
calib = afwImage.DecoratedImageF(calib.getImage()) # n.b. hardwires "F" for the output type

self.calculateOutputHeaderFromRaws(cache.butler, calib, struct.ccdIdList, outputId)

self.updateMetadata(calib, self.exposureTime)

self.recordCalibInputs(cache.butler, calib,
Expand All @@ -761,6 +766,59 @@ def combine(self, cache, struct, outputId):

return afwMath.binImage(calib.getImage(), self.config.binning)

def calculateOutputHeaderFromRaws(self, butler, calib, dataIdList, outputId):
"""!Calculate the output header from the raw headers.

This metadata will go into the output FITS header. It will include all
headers that are identical in all inputs.

@param butler Data butler
@param calib Combined calib exposure.
@param dataIdList List of data identifiers for calibration inputs
@param outputId Data identifier for output
"""
header = calib.getMetadata()

rawmd = [butler.get("raw_md", dataId) for dataId in dataIdList if
dataId is not None]

merged = merge_headers(rawmd, mode="drop")

# Place merged set into the PropertyList if a value is not
# present already
# Comments are not present in the merged version so copy them across
for k, v in merged.items():
if k not in header:
comment = rawmd[0].getComment(k) if k in rawmd[0] else None
header.set(k, v, comment=comment)

# Create an observation group so we can add some standard headers
# independent of the form in the input files.
# Use try block in case we are dealing with unexpected data headers
try:
group = ObservationGroup(rawmd, pedantic=False)
except Exception:
group = None

comments = {"TIMESYS": "Time scale for all dates",
"DATE-OBS": "Start date of earliest input observation",
"MJD-OBS": "[d] Start MJD of earliest input observation",
"DATE-END": "End date of oldest input observation",
"MJD-END": "[d] End MJD of oldest input observation",
"MJD-AVG": "[d] MJD midpoint of all input observations",
"DATE-AVG": "Midpoint date of all input observations"}

if group is not None:
oldest, newest = group.extremes()
dateCards = dates_to_fits(oldest.datetime_begin, newest.datetime_end)
else:
# Fall back to setting a DATE-OBS from the calibDate
dateCards = {"DATE-OBS": "{}T00:00:00.00".format(outputId[self.config.dateCalib])}
comments["DATE-OBS"] = "Date of start of day of calibration midpoint"

for k, v in dateCards.items():
header.set(k, v, comment=comments.get(k, None))

def recordCalibInputs(self, butler, calib, dataIdList, outputId):
"""!Record metadata including the inputs and creation details

Expand All @@ -778,8 +836,6 @@ def recordCalibInputs(self, butler, calib, dataIdList, outputId):
now = time.localtime()
header.add("CALIB_CREATION_DATE", time.strftime("%Y-%m-%d", now))
header.add("CALIB_CREATION_TIME", time.strftime("%X %Z", now))
# add date-obs as its absence upsets ExposureInfo; use the mean date that the calibs were taken
header.add("DATE-OBS", "%sT00:00:00.00" % outputId[self.config.dateCalib])

# Inputs
visits = [str(dictToTuple(dataId, self.config.visitKeys)) for dataId in dataIdList if
Expand Down
1 change: 1 addition & 0 deletions ups/pipe_drivers.table
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ setupRequired(sphgeom)
setupRequired(pex_config)
setupRequired(pipe_base)
setupRequired(pipe_tasks)
setupRequired(astro_metadata_translator)

envPrepend(PYTHONPATH, ${PRODUCT_DIR}/python)
envPrepend(PATH, ${PRODUCT_DIR}/bin)