Coverage for python/lsst/source/injection/bin/ingest_injection_catalog.py: 21%
38 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-16 04:47 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-16 04:47 -0700
1# This file is part of source_injection.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22from __future__ import annotations
24import logging
25import time
26from argparse import SUPPRESS, ArgumentParser
28from astropy.table import Table
29from lsst.daf.butler import Butler
31from ..utils.ingest_injection_catalog import ingest_injection_catalog
32from .source_injection_help_formatter import SourceInjectionHelpFormatter
35def build_argparser():
36 """Build an argument parser for this script."""
37 parser = ArgumentParser(
38 description="""Ingest a source injection catalog into the butler.
40This script reads an on-disk input catalog or multiple per-band input catalogs
41and ingests these data into the butler. Catalogs are read in using the astropy
42Table API.
44An attempt at auto-identification of the input catalog file format type will be
45made for each input. A manually specified format can instead be specified for
46all input catalogs using the ``--format`` option.
48Each injection catalog must be associated with at least one band, specified
49immediately after the path to the input catalog. Multiple space-separated bands
50can be provided for a single input catalog. The injection catalog option may
51also be called multiple times to ingest multiple per-band catalogs.
52""",
53 formatter_class=SourceInjectionHelpFormatter,
54 epilog="More information is available at https://pipelines.lsst.io.",
55 add_help=False,
56 argument_default=SUPPRESS,
57 )
58 parser.add_argument(
59 "-b",
60 "--butler-config",
61 type=str,
62 help="Location of the butler/registry config file.",
63 required=True,
64 metavar="TEXT",
65 )
66 parser.add_argument(
67 "-i",
68 "--injection-catalog",
69 type=str,
70 help="Location of the input source injection catalog and all associated bands.",
71 required=True,
72 metavar=("FILE BAND", "BAND"),
73 nargs="+",
74 action="append",
75 )
76 parser.add_argument(
77 "-o",
78 "--output-collection",
79 type=str,
80 help="Name of the output collection to ingest the injection catalog into.",
81 required=True,
82 metavar="COLL",
83 )
84 parser.add_argument(
85 "-t",
86 "--dataset-type-name",
87 type=str,
88 help="Output dataset type name for the ingested source injection catalog.",
89 metavar="TEXT",
90 default="injection_catalog",
91 )
92 parser.add_argument(
93 "--format",
94 type=str,
95 help="Format of the input injection catalog(s), overriding auto-identification.",
96 metavar="TEXT",
97 )
98 parser.add_argument(
99 "-h",
100 "--help",
101 action="help",
102 help="Show this help message and exit.",
103 )
104 return parser
107def main():
108 """Use this as the main entry point when calling from the command line."""
109 # Set up logging.
110 tz = time.strftime("%z")
111 logging.basicConfig(
112 format="%(levelname)s %(asctime)s.%(msecs)03d" + tz + " - %(message)s", datefmt="%Y-%m-%dT%H:%M:%S"
113 )
114 logger = logging.getLogger(__name__)
115 logger.setLevel(logging.DEBUG)
117 args = build_argparser().parse_args()
119 injection_catalogs = []
120 for injection_catalog_schema in args.injection_catalog:
121 if len(injection_catalog_schema) < 2:
122 raise RuntimeError("Each injection catalog must be associated with at least one band.")
123 injection_catalog = injection_catalog_schema.pop(0)
124 for band in injection_catalog_schema:
125 injection_catalogs.append((injection_catalog, band))
127 writeable_butler = Butler(args.butler_config, writeable=True)
128 injection_catalog_format = vars(args).get("format", None)
130 for injection_catalog, band in injection_catalogs:
131 # The character_as_bytes=False option is preferred, if possible.
132 try:
133 table = Table.read(injection_catalog, format=injection_catalog_format, character_as_bytes=False)
134 except TypeError:
135 table = Table.read(injection_catalog, format=injection_catalog_format)
137 _ = ingest_injection_catalog(
138 writeable_butler=writeable_butler,
139 table=table,
140 band=band,
141 **{
142 k: v
143 for k, v in vars(args).items()
144 if k not in ["butler_config", "injection_catalog", "format"]
145 },
146 )