LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
getRepositoryData.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010, 2011, 2012 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 """Retrieve collections of metadata or data based on a set of data references
23 
24 Use this as a base task for creating graphs and reports for a set of data.
25 """
26 import lsst.pex.config as pexConfig
27 import lsst.pipe.base as pipeBase
28 
29 __all__ = ["DataRefListRunner", "GetRepositoryDataTask"]
30 
31 class DataRefListRunner(pipeBase.TaskRunner):
32  """A task runner that calls run with a list of data references
33 
34  Differs from the default TaskRunner by providing all data references at once,
35  instead of iterating over them one at a time.
36  """
37  @staticmethod
38  def getTargetList(parsedCmd):
39  """Return a list of targets (arguments for __call__); one entry per invocation
40  """
41  return [parsedCmd.id.refList] # one argument consisting of a list of dataRefs
42 
43  def __call__(self, dataRefList):
44  """Run GetRepositoryDataTask.run on a single target
45 
46  @param dataRefList: argument dict for run; contains one key: dataRefList
47 
48  @return:
49  - None if doReturnResults false
50  - A pipe_base Struct containing these fields if doReturnResults true:
51  - dataRefList: the argument dict sent to runDataRef
52  - metadata: task metadata after execution of runDataRef
53  - result: result returned by task runDataRef
54  """
55  task = self.TaskClass(config=self.config, log=self.log)
56  result = task.run(dataRefList)
57 
58  if self.doReturnResults:
59  return pipeBase.Struct(
60  dataRefList = dataRefList,
61  metadata = task.metadata,
62  result = result,
63  )
64 
65 
66 class GetRepositoryDataTask(pipeBase.CmdLineTask):
67  """Retrieve data from a repository, e.g. for plotting or analysis purposes
68  """
69  ConfigClass = pexConfig.Config # nothing to configure
70  RunnerClass = DataRefListRunner
71  _DefaultName = "getTaskData"
72 
73  def __init__(self, *args, **kwargs):
74  pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
75 
76  @pipeBase.timeMethod
77  def run(self, dataRefList):
78  """Get data from a repository for a collection of data references
79 
80  @param dataRefList: a list of data references
81  """
82  raise NotImplementedError("subclass must specify a run method")
83 
84  def getIdList(self, dataRefList):
85  """Get a list of data IDs in a form that can be used as dictionary keys
86 
87  @param dataRefList: a list of data references
88  @return a pipe_base Struct with fields:
89  - idKeyTuple: a tuple of dataRef data ID keys
90  - idValList: a list of data ID value tuples, each tuple contains values in the order in idKeyTuple
91  """
92  if not dataRefList:
93  raise RuntimeError("No data refs")
94  idKeyTuple = tuple(sorted(dataRefList[0].dataId.keys()))
95 
96  idValList = []
97  for dataRef in dataRefList:
98  idValTuple = tuple(dataRef.dataId[key] for key in idKeyTuple)
99  idValList.append(idValTuple)
100 
101  return pipeBase.Struct(
102  idKeyTuple = idKeyTuple,
103  idValList = idValList,
104  )
105 
106  def getDataList(self, dataRefList, datasetType):
107  """Retrieve a list of data
108 
109  @param dataRefList: a list of data references
110  @param datasetType: datasetType of data to be retrieved
111  @return a list of data, one entry per dataRef in dataRefList (in order)
112  """
113  return [dataRef.get(datasetType=datasetType) for dataRef in dataRefList]
114 
115  def getMetadataItems(self, dataRefList, datasetType, nameList):
116  """Retrieve a list of dictionaries of metadata
117 
118  @param dataRefList: a list of data references
119  @param datasetType: datasetType of metadata (or any object that supports get(name))
120  @return a list of dicts of metadata:
121  - each entry in the list corresponds to a dataRef in dataRefList
122  - each dict contains name: item of metadata, for each name in nameList
123  """
124  valList = []
125  for dataRef in dataRefList:
126  metadata = dataRef.get(datasetType=datasetType)
127  valList.append(dict((name, metadata.get(name)) for name in nameList))
128  return valList