24"""Helper functions for coaddition.
26We often want to use a data reference as a key in a dict (e.g., inputs as a
27function of data reference for a warp/tempExp), but neither data references
28(lsst.daf.persistence.ButlerDataRef) nor data identifiers (dict) are hashable.
29One solution is to use tuples (which are hashable) of the data identifier
30values, and carry the data identifier keys separately. Doing the key/value
31gymnastics can be annoying, so we provide these helper functions to do this.
35def groupDataRefs(keys, dataRefIterable):
36 """Group data references by data identifier value-tuple.
38 Value-tuples are built from the values of the given keys.
39 The effect
is that the data references
in each group have the same
40 values
for the provided keys.
42 @param keys: List of keys to consider when grouping (order
is important)
43 @param dataRefIterable: Iterable of data references to group
44 @return Dict of <value-tuple>: <list of data references
for group>
47 for dataRef
in dataRefIterable:
48 dataId = dataRef.dataId
49 values = tuple(dataId[key]
for key
in keys)
50 group = groupDict.get(values)
54 groupDict[values] = [dataRef]
59def groupPatchExposures(patchDataRef, calexpDataRefList, coaddDatasetType="deepCoadd",
60 tempExpDatasetType="deepCoadd_directWarp"):
61 """Group calibrated exposures overlapping a patch by the warped
62 (temporary) exposure they contribute to.
64 For example, if the instrument has a mosaic camera, each group would
65 consist of the subset of CCD exposures
from a single camera exposure
66 that potentially overlap the patch.
69 - groups: Dict of <group tuple>: <list of data references
for group>
70 - keys: List of keys
for group tuple
72 butler = patchDataRef.getButler()
73 tempExpKeys = butler.getKeys(datasetType=tempExpDatasetType)
74 coaddKeys = sorted(butler.getKeys(datasetType=coaddDatasetType))
75 keys = sorted(set(tempExpKeys) - set(coaddKeys))
76 patchId = patchDataRef.dataId
80 coaddValues = tuple(patchId[k]
for k
in coaddKeys)
81 groups = dict((k + coaddValues, v)
for k, v
in groups.items())
82 keys += tuple(coaddKeys)
84 return Struct(groups=groups, keys=keys)
88 """Reconstitute a data identifier from a tuple and corresponding keys
90 @param groupTuple: Tuple
with values specifying a group
91 @param keys: List of keys
for group tuple
92 @return Data identifier dict
94 if len(groupTuple) != len(keys):
95 raise RuntimeError(
"Number of values (%d) and keys (%d) do not match" % (len(groupTuple), len(keys)))
96 return dict(zip(keys, groupTuple))
99def getGroupDataRef(butler, datasetType, groupTuple, keys):
100 """Construct a data reference from a tuple and corresponding keys
102 @param butler: Data butler
103 @param datasetType: Name of dataset
104 @param groupTuple: Tuple
with values specifying a group
105 @param keys: List of keys
for group tuple
106 @return Data reference
109 return butler.dataRef(datasetType=datasetType, dataId=dataId)
def groupDataRefs(keys, dataRefIterable)
def getGroupDataId(groupTuple, keys)