lsst.pipe.tasks  13.0-66-gfbf2f2ce+5
coaddHelpers.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2013 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 from __future__ import absolute_import, division, print_function
24 from builtins import zip
25 from lsst.pipe.base import Struct
26 
27 """Helper functions for coaddition.
28 
29 We often want to use a data reference as a key in a dict (e.g., inputs as a
30 function of data reference for a warp/tempExp), but neither data references
31 (lsst.daf.persistence.ButlerDataRef) nor data identifiers (dict) are hashable.
32 One solution is to use tuples (which are hashable) of the data identifier
33 values, and carry the data identifier keys separately. Doing the key/value
34 gymnastics can be annoying, so we provide these helper functions to do this.
35 """
36 
37 
38 def groupDataRefs(keys, dataRefIterable):
39  """Group data references by data identifier value-tuple.
40 
41  Value-tuples are built from the values of the given keys.
42  The effect is that the data references in each group have the same
43  values for the provided keys.
44 
45  @param keys: List of keys to consider when grouping (order is important)
46  @param dataRefIterable: Iterable of data references to group
47  @return Dict of <value-tuple>: <list of data references for group>
48  """
49  groupDict = dict()
50  for dataRef in dataRefIterable:
51  dataId = dataRef.dataId
52  values = tuple(dataId[key] for key in keys) # NOT dataId.values() as we must preserve order
53  group = groupDict.get(values)
54  if group:
55  group.append(dataRef)
56  else:
57  groupDict[values] = [dataRef]
58 
59  return groupDict
60 
61 
62 def groupPatchExposures(patchDataRef, calexpDataRefList, coaddDatasetType="deepCoadd",
63  tempExpDatasetType="deepCoadd_directWarp"):
64  """Group calibrated exposures overlapping a patch by the warped
65  (temporary) exposure they contribute to.
66 
67  For example, if the instrument has a mosaic camera, each group would
68  consist of the subset of CCD exposures from a single camera exposure
69  that potentially overlap the patch.
70 
71  @return Struct with:
72  - groups: Dict of <group tuple>: <list of data references for group>
73  - keys: List of keys for group tuple
74  """
75  butler = patchDataRef.getButler()
76  tempExpKeys = butler.getKeys(datasetType=tempExpDatasetType)
77  coaddKeys = sorted(butler.getKeys(datasetType=coaddDatasetType))
78  keys = sorted(set(tempExpKeys) - set(coaddKeys)) # Keys that will specify an exposure
79  patchId = patchDataRef.dataId
80  groups = groupDataRefs(keys, calexpDataRefList)
81 
82  # Supplement the groups with the coadd-specific information (e.g., tract, patch; these are constant)
83  coaddValues = tuple(patchId[k] for k in coaddKeys)
84  groups = dict((k + coaddValues, v) for k, v in groups.items())
85  keys += tuple(coaddKeys)
86 
87  return Struct(groups=groups, keys=keys)
88 
89 
90 def getGroupDataId(groupTuple, keys):
91  """Reconstitute a data identifier from a tuple and corresponding keys
92 
93  @param groupTuple: Tuple with values specifying a group
94  @param keys: List of keys for group tuple
95  @return Data identifier dict
96  """
97  if len(groupTuple) != len(keys):
98  raise RuntimeError("Number of values (%d) and keys (%d) do not match" % (len(groupTuple), len(keys)))
99  return dict(zip(keys, groupTuple))
100 
101 
102 def getGroupDataRef(butler, datasetType, groupTuple, keys):
103  """Construct a data reference from a tuple and corresponding keys
104 
105  @param butler: Data butler
106  @param datasetType: Name of dataset
107  @param groupTuple: Tuple with values specifying a group
108  @param keys: List of keys for group tuple
109  @return Data reference
110  """
111  dataId = getGroupDataId(groupTuple, keys)
112  return butler.dataRef(datasetType=datasetType, dataId=dataId)
def getGroupDataRef(butler, datasetType, groupTuple, keys)
def getGroupDataId(groupTuple, keys)
Definition: coaddHelpers.py:90
def groupDataRefs(keys, dataRefIterable)
Definition: coaddHelpers.py:38
def groupPatchExposures(patchDataRef, calexpDataRefList, coaddDatasetType="deepCoadd", tempExpDatasetType="deepCoadd_directWarp")
Definition: coaddHelpers.py:63