Coverage for python/astro_metadata_translator/bin/translateheader.py: 11%
117 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:30 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:30 +0000
1# This file is part of astro_metadata_translator.
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 LICENSE file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12"""Implementation of the ``translate_header.py`` script.
14Read file metadata from the specified files and report the translated content.
15"""
17from __future__ import annotations
19__all__ = ("main", "process_files")
21import argparse
22import importlib
23import logging
24import sys
25import traceback
26from collections.abc import Sequence
27from typing import IO
29import yaml
31from astro_metadata_translator import MetadataTranslator, ObservationInfo, fix_header
33from ..file_helpers import find_files, read_basic_metadata_from_file
35# Output mode choices
36OUTPUT_MODES = ("auto", "verbose", "table", "yaml", "fixed", "yamlnative", "fixednative", "none")
38# Definitions for table columns
39TABLE_COLUMNS = (
40 {"format": "32.32s", "attr": "observation_id", "label": "ObsId"},
41 {
42 "format": "8.8s",
43 "attr": "observation_type",
44 "label": "ImgType",
45 },
46 {
47 "format": "16.16s",
48 "attr": "object",
49 "label": "Object",
50 },
51 {
52 "format": "16.16s",
53 "attr": "physical_filter",
54 "label": "Filter",
55 },
56 {"format": ">8.8s", "attr": "detector_unique_name", "label": "Detector"},
57 {
58 "format": "5.1f",
59 "attr": "exposure_time",
60 "label": "ExpTime",
61 },
62)
65def build_argparser() -> argparse.ArgumentParser:
66 """Construct an argument parser for the ``translate_header.py`` script.
68 Returns
69 -------
70 argparser : `argparse.ArgumentParser`
71 The argument parser that defines the ``translate_header.py``
72 command-line interface.
73 """
74 parser = argparse.ArgumentParser(description="Summarize headers from astronomical data files")
75 parser.add_argument(
76 "files",
77 metavar="file",
78 type=str,
79 nargs="+",
80 help="File(s) from which headers will be parsed."
81 " If a directory is given it will be scanned for files matching the regular"
82 " expression defined in --regex.",
83 )
84 parser.add_argument(
85 "-q",
86 "--quiet",
87 action="store_true",
88 help="Do not report the translation content from each header. This forces output mode 'none'.",
89 )
90 parser.add_argument(
91 "-d",
92 "--dumphdr",
93 action="store_true",
94 help="Dump the header in YAML format to standard output rather than translating it."
95 " This is the same as using mode=yaml",
96 )
97 parser.add_argument(
98 "--traceback", action="store_true", help="Give detailed trace back when any errors encountered"
99 )
100 parser.add_argument(
101 "-n",
102 "--hdrnum",
103 default=1,
104 help="HDU number to read. If the HDU can not be found, a warning is issued but "
105 "translation is attempted using the primary header. "
106 "The primary header is always read and merged with this header.",
107 )
108 parser.add_argument(
109 "-m",
110 "--mode",
111 default="auto",
112 choices=OUTPUT_MODES,
113 help="Display mode for translated parameters. 'verbose' displays all the information"
114 " available. 'table' displays important information in tabular form."
115 " 'yaml' dumps the header in YAML format (this is equivalent to -d option)."
116 " 'fixed' dumps the header in YAML after it has had corrections applied."
117 " Add 'native' suffix to dump YAML in PropertyList or Astropy native form."
118 " 'none' displays no translated header information and is an alias for the "
119 " '--quiet' option."
120 " 'auto' mode is 'verbose' for a single file and 'table' for multiple files.",
121 )
122 parser.add_argument("-l", "--log", default="warn", help="Python logging level to use.")
124 re_default = r"\.fit[s]?\b"
125 parser.add_argument(
126 "-r",
127 "--regex",
128 default=re_default,
129 help="When looking in a directory, regular expression to use to determine whether"
130 f" a file should be examined. Default: '{re_default}'",
131 )
133 parser.add_argument(
134 "-p",
135 "--packages",
136 action="append",
137 type=str,
138 help="Python packages to import to register additional translators",
139 )
141 return parser
144def read_file(
145 file: str,
146 hdrnum: int,
147 print_trace: bool,
148 outstream: IO = sys.stdout,
149 errstream: IO = sys.stderr,
150 output_mode: str = "verbose",
151 write_heading: bool = False,
152) -> bool:
153 """Read the specified file and process it.
155 Parameters
156 ----------
157 file : `str`
158 The file from which the header is to be read.
159 hdrnum : `int`
160 The HDU number to read. The primary header is always read and
161 merged with the header from this HDU.
162 print_trace : `bool`
163 If there is an error reading the file and this parameter is `True`,
164 a full traceback of the exception will be reported. If `False` prints
165 a one line summary of the error condition.
166 outstream : `io.StringIO`, optional
167 Output stream to use for standard messages. Defaults to `sys.stdout`.
168 errstream : `io.StringIO`, optional
169 Stream to send messages that would normally be sent to standard
170 error. Defaults to `sys.stderr`.
171 output_mode : `str`, optional
172 Output mode to use. Must be one of "verbose", "none", "table",
173 "yaml", or "fixed". "yaml" and "fixed" can be modified with a
174 "native" suffix to indicate that the output should be a representation
175 of the native object type representing the header (which can be
176 PropertyList or an Astropy header). Without this modify headers
177 will be dumped as simple `dict` form.
178 "auto" is used to indicate that a single file has been specified
179 but the output will depend on whether the file is a multi-extension
180 FITS file or not.
181 write_heading: `bool`, optional
182 If `True` and in table mode, write a table heading out before writing
183 the content.
185 Returns
186 -------
187 success : `bool`
188 `True` if the file was handled successfully, `False` if the file
189 could not be processed.
190 """
191 if output_mode not in OUTPUT_MODES:
192 raise ValueError(f"Output mode of '{output_mode}' is not understood.")
194 # This gets in the way in tabular mode
195 if output_mode != "table":
196 print(f"Analyzing {file}...", file=errstream)
198 try:
199 md = read_basic_metadata_from_file(file, hdrnum, errstream=errstream, can_raise=True)
200 if md is None:
201 raise RuntimeError(f"Failed to read file {file} HDU={hdrnum}")
203 if output_mode.endswith("native"):
204 # Strip native and don't change type of md
205 output_mode = output_mode[: -len("native")]
206 else:
207 # Rewrite md as simple dict for output
208 md = {k: v for k, v in md.items()}
210 if output_mode in ("yaml", "fixed"):
211 if output_mode == "fixed":
212 fix_header(md, filename=file)
214 # The header should be written out in the insertion order
215 print(yaml.dump(md, sort_keys=False), file=outstream)
216 return True
218 # Try to work out a translator class.
219 translator_class = MetadataTranslator.determine_translator(md, filename=file)
221 # Work out which headers to translate, assuming the default if
222 # we have a YAML test file.
223 if file.endswith(".yaml"):
224 headers = [md]
225 else:
226 headers = list(translator_class.determine_translatable_headers(file, md))
227 if output_mode == "auto":
228 output_mode = "table" if len(headers) > 1 else "verbose"
230 wrote_heading = False
231 for md in headers:
232 obs_info = ObservationInfo(md, pedantic=True, filename=file)
233 if output_mode == "table":
234 columns = [
235 "{:{fmt}}".format(getattr(obs_info, c["attr"]), fmt=c["format"]) for c in TABLE_COLUMNS
236 ]
238 if write_heading and not wrote_heading:
239 # Construct headings of the same width as the items
240 # we have calculated. Doing this means we don't have to
241 # work out for ourselves how many characters will be used
242 # for non-strings (especially Quantity)
243 headings = []
244 separators = []
245 for thiscol, defn in zip(columns, TABLE_COLUMNS):
246 width = len(thiscol)
247 headings.append("{:{w}.{w}}".format(defn["label"], w=width))
248 separators.append("-" * width)
249 print(" ".join(headings), file=outstream)
250 print(" ".join(separators), file=outstream)
251 wrote_heading = True
253 row = " ".join(columns)
254 print(row, file=outstream)
255 elif output_mode == "verbose":
256 print(f"{obs_info}", file=outstream)
257 elif output_mode == "none":
258 pass
259 else:
260 raise RuntimeError(f"Output mode of '{output_mode}' not recognized but should be known.")
261 except Exception as e:
262 if print_trace:
263 traceback.print_exc(file=outstream)
264 else:
265 print(f"Failure processing {file}: {e}", file=outstream)
266 return False
267 return True
270def process_files(
271 files: Sequence[str],
272 regex: str,
273 hdrnum: int,
274 print_trace: bool,
275 outstream: IO = sys.stdout,
276 errstream: IO = sys.stderr,
277 output_mode: str = "auto",
278) -> tuple[list[str], list[str]]:
279 """Read and translate metadata from the specified files.
281 Parameters
282 ----------
283 files : iterable of `str`
284 The files or directories from which the headers are to be read.
285 regex : `str`
286 Regular expression string used to filter files when a directory is
287 scanned.
288 hdrnum : `int`
289 The HDU number to read. The primary header is always read and
290 merged with the header from this HDU.
291 print_trace : `bool`
292 If there is an error reading the file and this parameter is `True`,
293 a full traceback of the exception will be reported. If `False` prints
294 a one line summary of the error condition.
295 outstream : `io.StringIO`, optional
296 Output stream to use for standard messages. Defaults to `sys.stdout`.
297 errstream : `io.StringIO`, optional
298 Stream to send messages that would normally be sent to standard
299 error. Defaults to `sys.stderr`.
300 output_mode : `str`, optional
301 Output mode to use for the translated information.
302 "auto" switches based on how many files are found.
304 Returns
305 -------
306 okay : `list` of `str`
307 All the files that were processed successfully.
308 failed : `list` of `str`
309 All the files that could not be processed.
310 """
311 found_files = find_files(files, regex)
313 # Convert "auto" to correct mode but for a single file keep it
314 # auto in case that file has multiple headers
315 if output_mode == "auto":
316 if len(found_files) > 1:
317 output_mode = "table"
319 # Process each file
320 failed = []
321 okay = []
322 heading = True
323 for path in sorted(found_files):
324 isok = read_file(path, hdrnum, print_trace, outstream, errstream, output_mode, heading)
325 heading = False
326 if isok:
327 okay.append(path)
328 else:
329 failed.append(path)
331 return okay, failed
334def main() -> int:
335 """Read metadata from the supplied files and translate the content to
336 standard form.
338 Returns
339 -------
340 status : `int`
341 Exit status to be passed to `sys.exit`. ``0`` if any of the files
342 could be translated. ``1`` otherwise.
343 """
344 logging.warn(
345 "This command is deprecated. Please use 'astrometadata translate' "
346 " or 'astrometadata dump' instead. See 'astrometadata -h' for more details."
347 )
349 args = build_argparser().parse_args()
351 # Process import requests
352 if args.packages:
353 for m in args.packages:
354 importlib.import_module(m)
356 output_mode = args.mode
357 if args.quiet:
358 output_mode = "none"
359 elif args.dumphdr:
360 output_mode = "yaml"
362 # Set the log level. Convert to upper case to allow the user to
363 # specify --log=DEBUG or --log=debug
364 numeric_level = getattr(logging, args.log.upper(), None)
365 if not isinstance(numeric_level, int):
366 raise ValueError(f"Invalid log level: {args.log}")
367 logging.basicConfig(level=numeric_level)
369 # Main loop over files
370 okay, failed = process_files(args.files, args.regex, args.hdrnum, args.traceback, output_mode=output_mode)
372 if failed:
373 print("Files with failed translations:", file=sys.stderr)
374 for f in failed:
375 print(f"\t{f}", file=sys.stderr)
377 if okay:
378 # Good status if anything was returned in okay
379 return 0
380 else:
381 return 1