Coverage for python / lsst / obs / base / script / defineVisits.py: 17%
27 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:50 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:50 +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
23from lsst.daf.butler import Butler
24from lsst.obs.base import DefineVisitsConfig, DefineVisitsTask
25from lsst.pipe.base import Instrument
27log = logging.getLogger("lsst.obs.base.defineVisits")
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) -> None:
40 """Implement the command line interface `butler define-visits` subcommand,
41 should only be called by command line tools and unit test code that tests
42 this function.
44 Defines visits from exposures in the butler registry
46 Parameters
47 ----------
48 repo : `str`
49 URI to the location to create the repo.
50 config_file : `str` or `None`
51 Path to a config file that contains overrides to the ingest config.
52 collections : `list` [`str`]
53 An expression specifying the collections to be searched (in order) when
54 reading datasets, and optionally dataset type restrictions on them.
55 If empty it will be passed as `None` to Butler.
56 instrument : `str`
57 The name or fully-qualified class name of an instrument.
58 where : `str`, optional
59 Query clause to use when querying for dataIds. Can be used to limit
60 the relevant exposures.
61 update_records : `bool`, optional
62 Control whether recalculated visit definitions will be accepted or
63 not.
64 incremental : `bool`, optional
65 Declare that the visit definitions are being run in a situation
66 where data from multi-snap visits are being ingested incrementally
67 and so the visit definition could change as new data arrive.
68 skip_conflicting : `bool`, optional
69 If `True` do not raise an error if there is a change in an existing
70 visit definition. This can be used if you solely want to define
71 visits that were somehow missed previously. It has no effect if
72 ``update_records`` is `True` or incremental mode is enabled.
74 Notes
75 -----
76 Camera geometry is not currently found in registry but instead a default
77 camera will be used for the relevant instrument.
78 """
79 if not collections:
80 collections = None
81 with Butler.from_config(repo, collections=collections, writeable=True) as butler:
82 instr = Instrument.from_string(instrument, butler.registry)
83 config = DefineVisitsConfig()
84 instr.applyConfigOverrides(DefineVisitsTask._DefaultName, config)
86 # If this is old schema but is using modern visit grouping algorithm,
87 # (which is the default for new code) revert to one-to-one (which
88 # was the old default).
89 exposure_dimension = butler.dimensions["exposure"]
90 modern = "one-to-one-and-by-counter"
91 if "seq_end" not in exposure_dimension.metadata and config.groupExposures.name == modern:
92 legacy = "one-to-one"
93 log.warning(
94 "Request to use %s grouping algorithm but registry schema is too old. Using %s instead.",
95 modern,
96 legacy,
97 )
98 config.groupExposures.name = legacy
100 if not where:
101 where = ""
103 if config_file is not None:
104 config.load(config_file)
105 task = DefineVisitsTask(config=config, butler=butler)
107 with butler.query() as query:
108 query = query.join_dimensions(["exposure"]).where(where, instrument=instr.getName())
109 data_ids = list(query.data_ids(["exposure"]).with_dimension_records())
111 task.run(
112 data_ids,
113 collections=collections,
114 update_records=update_records,
115 incremental=incremental,
116 skip_conflicting=skip_conflicting,
117 )