Coverage for python / lsst / daf / butler / script / queryDimensionRecords.py: 12%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 08: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 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/>. 

27 

28from __future__ import annotations 

29 

30import logging 

31from operator import attrgetter 

32from typing import Any 

33 

34from astropy.table import Table 

35 

36from lsst.sphgeom import Region 

37 

38from .._butler import Butler 

39from .._timespan import Timespan 

40 

41_LOG = logging.getLogger(__name__) 

42 

43 

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. 

55 

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. 

74 

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.") 

83 

84 with Butler.from_config(repo, without_datastore=True) as butler: 

85 with butler.query() as query: 

86 if datasets: 

87 query_collections = collections or "*" 

88 dataset_types = butler.registry.queryDatasetTypes(datasets) 

89 collections_info = butler.collections.query_info( 

90 query_collections, include_summary=True, summary_datasets=dataset_types 

91 ) 

92 dataset_type_collections = butler.collections._group_by_dataset_type( 

93 {dt.name for dt in dataset_types}, collections_info 

94 ) 

95 

96 if not dataset_type_collections: 

97 return None 

98 

99 for dt, dt_collections in dataset_type_collections.items(): 

100 query = query.join_dataset_search(dt, collections=dt_collections) 

101 

102 query_results = query.dimension_records(element) 

103 

104 if where: 

105 query_results = query_results.where(where) 

106 if order_by: 

107 query_results = query_results.order_by(*order_by) 

108 query_limit = abs(limit) 

109 warn_limit = False 

110 if limit != 0: 

111 if limit < 0: 

112 query_limit += 1 

113 warn_limit = True 

114 

115 query_results = query_results.limit(query_limit) 

116 

117 records = list(query_results) 

118 if warn_limit and len(records) == query_limit: 

119 records.pop(-1) 

120 _LOG.warning("More data IDs are available than the request limit of %d", abs(limit)) 

121 

122 if not records: 

123 return None 

124 

125 if not order_by: 

126 # use the dataId to sort the rows if not ordered already 

127 records.sort(key=attrgetter("dataId")) 

128 

129 # order the columns the same as the record's `field.names`, and add 

130 # units to timespans 

131 keys = records[0].fields.names 

132 headers = ["timespan (TAI)" if name == "timespan" else name for name in records[0].fields.names] 

133 

134 def conform(v: Any) -> Any: 

135 match v: 

136 case Timespan(): 

137 v = str(v) 

138 case bytes(): 

139 v = "0x" + v.hex() 

140 case Region(): 

141 v = "(elided)" 

142 return v 

143 

144 return Table( 

145 [[conform(getattr(record, key, None)) for record in records] for key in keys], names=headers 

146 )