Coverage for python / lsst / obs / base / script / defineVisits.py: 17%
27 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-20 01:41 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-20 01:41 -0800
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,
32 config_file,
33 collections,
34 instrument,
35 where=None,
36 update_records=False,
37 incremental=False,
38):
39 """Implement the command line interface `butler define-visits` subcommand,
40 should only be called by command line tools and unit test code that tests
41 this function.
43 Defines visits from exposures in the butler registry
45 Parameters
46 ----------
47 repo : `str`
48 URI to the location to create the repo.
49 config_file : `str` or `None`
50 Path to a config file that contains overrides to the ingest config.
51 collections : `list` [`str`]
52 An expression specifying the collections to be searched (in order) when
53 reading datasets, and optionally dataset type restrictions on them.
54 If empty it will be passed as `None` to Butler.
55 instrument : `str`
56 The name or fully-qualified class name of an instrument.
57 where : `str`, optional
58 Query clause to use when querying for dataIds. Can be used to limit
59 the relevant exposures.
60 update_records : `bool`, optional
61 Control whether recalculated visit definitions will be accepted or
62 not.
63 incremental : `bool`, optional
64 Declare that the visit definitions are being run in a situation
65 where data from multi-snap visits are being ingested incrementally
66 and so the visit definition could change as new data arrive.
68 Notes
69 -----
70 Camera geometry is not currently found in registry but instead a default
71 camera will be used for the relevant instrument.
72 """
73 if not collections:
74 collections = None
75 with Butler.from_config(repo, collections=collections, writeable=True) as butler:
76 instr = Instrument.from_string(instrument, butler.registry)
77 config = DefineVisitsConfig()
78 instr.applyConfigOverrides(DefineVisitsTask._DefaultName, config)
80 # If this is old schema but is using modern visit grouping algorithm,
81 # (which is the default for new code) revert to one-to-one (which
82 # was the old default).
83 exposure_dimension = butler.dimensions["exposure"]
84 modern = "one-to-one-and-by-counter"
85 if "seq_end" not in exposure_dimension.metadata and config.groupExposures.name == modern:
86 legacy = "one-to-one"
87 log.warning(
88 "Request to use %s grouping algorithm but registry schema is too old. Using %s instead.",
89 modern,
90 legacy,
91 )
92 config.groupExposures.name = legacy
94 if not where:
95 where = ""
97 if config_file is not None:
98 config.load(config_file)
99 task = DefineVisitsTask(config=config, butler=butler)
101 with butler.query() as query:
102 query = query.join_dimensions(["exposure"]).where(where, instrument=instr.getName())
103 data_ids = list(query.data_ids(["exposure"]).with_dimension_records())
105 task.run(
106 data_ids,
107 collections=collections,
108 update_records=update_records,
109 incremental=incremental,
110 )