Coverage for python/lsst/daf/butler/script/queryDimensionRecords.py: 12%
54 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-14 01:18 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-14 01:18 -0800
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30import logging
31from operator import attrgetter
32from typing import Any
34from astropy.table import Table
36from lsst.sphgeom import Region
38from .._butler import Butler
39from .._timespan import Timespan
41_LOG = logging.getLogger(__name__)
44def queryDimensionRecords(
45 repo: str,
46 element: str,
47 datasets: tuple[str, ...],
48 collections: tuple[str, ...],
49 where: str,
50 order_by: tuple[str, ...],
51 limit: int,
52 offset: int,
53) -> Table | None:
54 """Query dimension records.
56 Parameters
57 ----------
58 repo : `str`
59 Butler location.
60 element : `str`
61 Name of relevant dimension record.
62 datasets : `tuple` of `str`
63 Dataset types to restrict query by.
64 collections : `~collections.abc.Iterable` of `str`
65 Collections to search.
66 where : `str`
67 Query string.
68 order_by : `tuple` of `str`
69 Columns to order results by.
70 limit : `int`
71 Maximum number of results.
72 offset : `int`
73 Offset into the results.
75 Notes
76 -----
77 Docstring for supported parameters is the same as
78 `~lsst.daf.butler.Registry.queryDimensionRecords` except for ``no_check``,
79 which is the inverse of ``check``.
80 """
81 if offset:
82 raise NotImplementedError("--offset is no longer supported. It will be removed after v28.")
84 butler = Butler.from_config(repo, without_datastore=True)
86 with butler.query() as query:
87 if datasets:
88 query_collections = collections or "*"
89 dataset_types = butler.registry.queryDatasetTypes(datasets)
90 collections_info = butler.collections.query_info(
91 query_collections, include_summary=True, summary_datasets=dataset_types
92 )
93 dataset_type_collections = butler.collections._group_by_dataset_type(
94 {dt.name for dt in dataset_types}, collections_info
95 )
97 if not dataset_type_collections:
98 return None
100 for dt, dt_collections in dataset_type_collections.items():
101 query = query.join_dataset_search(dt, collections=dt_collections)
103 query_results = query.dimension_records(element)
105 if where:
106 query_results = query_results.where(where)
107 if order_by:
108 query_results = query_results.order_by(*order_by)
109 query_limit = abs(limit)
110 warn_limit = False
111 if limit != 0:
112 if limit < 0:
113 query_limit += 1
114 warn_limit = True
116 query_results = query_results.limit(query_limit)
118 records = list(query_results)
119 if warn_limit and len(records) == query_limit:
120 records.pop(-1)
121 _LOG.warning("More data IDs are available than the request limit of %d", abs(limit))
123 if not records:
124 return None
126 if not order_by:
127 # use the dataId to sort the rows if not ordered already
128 records.sort(key=attrgetter("dataId"))
130 # order the columns the same as the record's `field.names`, and add units
131 # to timespans
132 keys = records[0].fields.names
133 headers = ["timespan (TAI)" if name == "timespan" else name for name in records[0].fields.names]
135 def conform(v: Any) -> Any:
136 match v:
137 case Timespan():
138 v = str(v)
139 case bytes():
140 v = "0x" + v.hex()
141 case Region():
142 v = "(elided)"
143 return v
145 return Table([[conform(getattr(record, key, None)) for record in records] for key in keys], names=headers)