Coverage for python/lsst/daf/butler/script/queryDataIds.py: 22%
29 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
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 GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from astropy.table import Table as AstropyTable
23import numpy as np
25from .. import Butler
26from ..cli.utils import sortAstropyTable
29class _Table:
30 """Aggregates DataIds and creates an astropy table with one DataId per
31 row. Eliminates duplicate rows.
32 """
34 def __init__(self):
35 self.dataIds = set()
37 def add(self, dataId):
38 """Add a DataId to the table.
40 Parameters
41 ----------
42 dataId : ``DataId``
43 The DataId to add to the table.
44 """
45 self.dataIds.add(dataId)
47 def getAstropyTable(self):
48 """Get the table as an astropy table.
50 Returns
51 -------
52 table : `astropy.table.Table`
53 The dataIds, sorted by spatial and temporal columns first, and then
54 the rest of the columns, with duplicate dataIds removed.
55 """
56 # Should never happen; adding a dataset should be the action that
57 # causes a _Table to be created.
58 if not self.dataIds:
59 raise RuntimeError("No DataIds were provided.")
61 dataId = next(iter(self.dataIds))
62 dimensions = list(dataId.full.keys())
63 columnNames = [str(item) for item in dimensions]
65 # Need to hint the column types for numbers since the per-row
66 # constructor of Table does not work this out on its own and sorting
67 # will not work properly without.
68 typeMap = {float: np.float64, int: np.int64}
69 columnTypes = [typeMap.get(type(value)) for value in dataId.full.values()]
71 rows = [[value for value in dataId.full.values()] for dataId in self.dataIds]
73 table = AstropyTable(np.array(rows), names=columnNames, dtype=columnTypes)
74 return sortAstropyTable(table, dimensions)
77def queryDataIds(repo, dimensions, datasets, where, collections):
78 # Docstring for supported parameters is the same as Registry.queryDataIds
80 butler = Butler(repo)
81 results = butler.registry.queryDataIds(dimensions,
82 datasets=datasets,
83 where=where,
84 collections=collections)
86 if len(results.graph) > 0:
87 table = _Table()
88 for dataId in results:
89 table.add(dataId)
90 return table.getAstropyTable()
91 else:
92 return None