Coverage for python / lsst / obs / base / script / defineVisits.py: 17%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-06 08:33 +0000

1# This file is part of obs_base. 

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/>. 

21import logging 

22 

23from lsst.daf.butler import Butler 

24from lsst.obs.base import DefineVisitsConfig, DefineVisitsTask 

25from lsst.pipe.base import Instrument 

26 

27log = logging.getLogger("lsst.obs.base.defineVisits") 

28 

29 

30def defineVisits( 

31 repo: str, 

32 config_file: str | None, 

33 collections: list[str] | None, 

34 instrument: str, 

35 where: str | None = None, 

36 update_records: bool = False, 

37 incremental: bool = False, 

38 skip_conflicting: bool = False, 

39 prefilter: bool = False, 

40 check_detector_regions: bool = False, 

41) -> None: 

42 """Implement the command line interface `butler define-visits` subcommand, 

43 should only be called by command line tools and unit test code that tests 

44 this function. 

45 

46 Defines visits from exposures in the butler registry 

47 

48 Parameters 

49 ---------- 

50 repo : `str` 

51 URI to the location to create the repo. 

52 config_file : `str` or `None` 

53 Path to a config file that contains overrides to the ingest config. 

54 collections : `list` [`str`] 

55 An expression specifying the collections to be searched (in order) when 

56 reading datasets, and optionally dataset type restrictions on them. 

57 If empty it will be passed as `None` to Butler. 

58 instrument : `str` 

59 The name or fully-qualified class name of an instrument. 

60 where : `str`, optional 

61 Query clause to use when querying for dataIds. Can be used to limit 

62 the relevant exposures. 

63 update_records : `bool`, optional 

64 Control whether recalculated visit definitions will be accepted or 

65 not. 

66 incremental : `bool`, optional 

67 Declare that the visit definitions are being run in a situation 

68 where data from multi-snap visits are being ingested incrementally 

69 and so the visit definition could change as new data arrive. 

70 skip_conflicting : `bool`, optional 

71 If `True` do not raise an error if there is a change in an existing 

72 visit definition. This can be used if you solely want to define 

73 visits that were somehow missed previously. It has no effect if 

74 ``update_records`` is `True` or incremental mode is enabled. 

75 prefilter : `bool`, optional 

76 If `True`, filter out exposures that already have a visit defined 

77 by querying in advance. This is not compatible with 

78 ``incremental=True`` or ``update_records=True``. Note that for 

79 exposures that may be associated with multiple visits, the presence 

80 of any existing visit will cause that exposure to be skipped entirely. 

81 check_detector_regions : `bool`, optional 

82 If `True`, check existing visits to see if they have a 

83 ``visit_detector_region`` record for every detector. The default 

84 is to assume that if a ``visit`` record is present, the 

85 ``visit_detector_region`` records are as well. 

86 

87 Notes 

88 ----- 

89 Camera geometry is not currently found in registry but instead a default 

90 camera will be used for the relevant instrument. 

91 """ 

92 if not collections: 

93 collections = None 

94 with Butler.from_config(repo, collections=collections, writeable=True) as butler: 

95 instr = Instrument.from_string(instrument, butler.registry) 

96 config = DefineVisitsConfig() 

97 instr.applyConfigOverrides(DefineVisitsTask._DefaultName, config) 

98 

99 # If this is old schema but is using modern visit grouping algorithm, 

100 # (which is the default for new code) revert to one-to-one (which 

101 # was the old default). 

102 exposure_dimension = butler.dimensions["exposure"] 

103 modern = "one-to-one-and-by-counter" 

104 if "seq_end" not in exposure_dimension.metadata and config.groupExposures.name == modern: 

105 legacy = "one-to-one" 

106 log.warning( 

107 "Request to use %s grouping algorithm but registry schema is too old. Using %s instead.", 

108 modern, 

109 legacy, 

110 ) 

111 config.groupExposures.name = legacy 

112 

113 if not where: 

114 where = "" 

115 

116 if config_file is not None: 

117 config.load(config_file) 

118 task = DefineVisitsTask(config=config, butler=butler) 

119 

120 with butler.query() as query: 

121 query = query.join_dimensions(["exposure"]).where(where, instrument=instr.getName()) 

122 data_ids = list(query.data_ids(["exposure"]).with_dimension_records()) 

123 

124 task.run( 

125 data_ids, 

126 collections=collections, 

127 update_records=update_records, 

128 incremental=incremental, 

129 skip_conflicting=skip_conflicting, 

130 prefilter=prefilter, 

131 check_detector_regions=check_detector_regions, 

132 )