LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
getRepositoryData.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008, 2009, 2010, 2011, 2012 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """Retrieve collections of metadata or data based on a set of data references
24 
25 Use this as a base task for creating graphs and reports for a set of data.
26 """
27 import collections
28 import itertools
29 import re
30 
31 import numpy
32 
33 import lsst.pex.config as pexConfig
34 import lsst.pipe.base as pipeBase
35 
36 __all__ = ["DataRefListRunner", "GetRepositoryDataTask"]
37 
38 class DataRefListRunner(pipeBase.TaskRunner):
39  """A task runner that calls run with a list of data references
40 
41  Differs from the default TaskRunner by providing all data references at once,
42  instead of iterating over them one at a time.
43  """
44  @staticmethod
45  def getTargetList(parsedCmd):
46  """Return a list of targets (arguments for __call__); one entry per invocation
47  """
48  return [parsedCmd.id.refList] # one argument consisting of a list of dataRefs
49 
50  def __call__(self, dataRefList):
51  """Run GetRepositoryDataTask.run on a single target
52 
53  @param dataRefList: argument dict for run; contains one key: dataRefList
54 
55  @return:
56  - None if doReturnResults false
57  - A pipe_base Struct containing these fields if doReturnResults true:
58  - dataRefList: the argument dict sent to runDataRef
59  - metadata: task metadata after execution of runDataRef
60  - result: result returned by task runDataRef
61  """
62  task = self.TaskClass(config=self.config, log=self.log)
63  result = task.run(dataRefList)
64 
65  if self.doReturnResults:
66  return pipeBase.Struct(
67  dataRefList = dataRefList,
68  metadata = task.metadata,
69  result = result,
70  )
71 
72 
73 class GetRepositoryDataTask(pipeBase.CmdLineTask):
74  """Retrieve data from a repository, e.g. for plotting or analysis purposes
75  """
76  ConfigClass = pexConfig.Config # nothing to configure
77  RunnerClass = DataRefListRunner
78  _DefaultName = "getTaskData"
79 
80  def __init__(self, *args, **kwargs):
81  pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
82 
83  @pipeBase.timeMethod
84  def run(self, dataRefList):
85  """Get data from a repository for a collection of data references
86 
87  @param dataRefList: a list of data references
88  """
89  raise NotImplementedError("subclass must specify a run method")
90 
91  def getIdList(self, dataRefList):
92  """Get a list of data IDs in a form that can be used as dictionary keys
93 
94  @param dataRefList: a list of data references
95  @return a pipe_base Struct with fields:
96  - idKeyTuple: a tuple of dataRef data ID keys
97  - idValList: a list of data ID value tuples, each tuple contains values in the order in idKeyTuple
98  """
99  if not dataRefList:
100  raise RuntimeError("No data refs")
101  idKeyTuple = tuple(sorted(dataRefList[0].dataId.keys()))
102 
103  idValList = []
104  for dataRef in dataRefList:
105  idValTuple = tuple(dataRef.dataId[key] for key in idKeyTuple)
106  idValList.append(idValTuple)
107 
108  return pipeBase.Struct(
109  idKeyTuple = idKeyTuple,
110  idValList = idValList,
111  )
112 
113  def getDataList(self, dataRefList, datasetType):
114  """Retrieve a list of data
115 
116  @param dataRefList: a list of data references
117  @param datasetType: datasetType of data to be retrieved
118  @return a list of data, one entry per dataRef in dataRefList (in order)
119  """
120  return [dataRef.get(datasetType=datasetType) for dataRef in dataRefList]
121 
122  def getMetadataItems(self, dataRefList, datasetType, nameList):
123  """Retrieve a list of dictionaries of metadata
124 
125  @param dataRefList: a list of data references
126  @param datasetType: datasetType of metadata (or any object that supports get(name))
127  @return a list of dicts of metadata:
128  - each entry in the list corresponds to a dataRef in dataRefList
129  - each dict contains name: item of metadata, for each name in nameList
130  """
131  valList = []
132  for dataRef in dataRefList:
133  metadata = dataRef.get(datasetType=datasetType)
134  valList.append(dict((name, metadata.get(name)) for name in nameList))
135  return valList