Coverage for python/astro_metadata_translator/bin/translateheader.py: 11%

117 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-27 02:38 -0700

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. 

11 

12"""Implementation of the ``translate_header.py`` script. 

13 

14Read file metadata from the specified files and report the translated content. 

15""" 

16 

17from __future__ import annotations 

18 

19__all__ = ("main", "process_files") 

20 

21import argparse 

22import importlib 

23import logging 

24import sys 

25import traceback 

26from collections.abc import Sequence 

27from typing import IO 

28 

29import yaml 

30 

31from astro_metadata_translator import MetadataTranslator, ObservationInfo, fix_header 

32 

33from ..file_helpers import find_files, read_basic_metadata_from_file 

34 

35# Output mode choices 

36OUTPUT_MODES = ("auto", "verbose", "table", "yaml", "fixed", "yamlnative", "fixednative", "none") 

37 

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) 

63 

64 

65def build_argparser() -> argparse.ArgumentParser: 

66 """Construct an argument parser for the ``translate_header.py`` script. 

67 

68 Returns 

69 ------- 

70 argparser : `argparse.ArgumentParser` 

71 The argument parser that defines the ``translate_header.py`` 

72 command-line interface. 

73 """ 

74 

75 parser = argparse.ArgumentParser(description="Summarize headers from astronomical data files") 

76 parser.add_argument( 

77 "files", 

78 metavar="file", 

79 type=str, 

80 nargs="+", 

81 help="File(s) from which headers will be parsed." 

82 " If a directory is given it will be scanned for files matching the regular" 

83 " expression defined in --regex.", 

84 ) 

85 parser.add_argument( 

86 "-q", 

87 "--quiet", 

88 action="store_true", 

89 help="Do not report the translation content from each header. This forces output mode 'none'.", 

90 ) 

91 parser.add_argument( 

92 "-d", 

93 "--dumphdr", 

94 action="store_true", 

95 help="Dump the header in YAML format to standard output rather than translating it." 

96 " This is the same as using mode=yaml", 

97 ) 

98 parser.add_argument( 

99 "--traceback", action="store_true", help="Give detailed trace back when any errors encountered" 

100 ) 

101 parser.add_argument( 

102 "-n", 

103 "--hdrnum", 

104 default=1, 

105 help="HDU number to read. If the HDU can not be found, a warning is issued but " 

106 "translation is attempted using the primary header. " 

107 "The primary header is always read and merged with this header.", 

108 ) 

109 parser.add_argument( 

110 "-m", 

111 "--mode", 

112 default="auto", 

113 choices=OUTPUT_MODES, 

114 help="Display mode for translated parameters. 'verbose' displays all the information" 

115 " available. 'table' displays important information in tabular form." 

116 " 'yaml' dumps the header in YAML format (this is equivalent to -d option)." 

117 " 'fixed' dumps the header in YAML after it has had corrections applied." 

118 " Add 'native' suffix to dump YAML in PropertyList or Astropy native form." 

119 " 'none' displays no translated header information and is an alias for the " 

120 " '--quiet' option." 

121 " 'auto' mode is 'verbose' for a single file and 'table' for multiple files.", 

122 ) 

123 parser.add_argument("-l", "--log", default="warn", help="Python logging level to use.") 

124 

125 re_default = r"\.fit[s]?\b" 

126 parser.add_argument( 

127 "-r", 

128 "--regex", 

129 default=re_default, 

130 help="When looking in a directory, regular expression to use to determine whether" 

131 f" a file should be examined. Default: '{re_default}'", 

132 ) 

133 

134 parser.add_argument( 

135 "-p", 

136 "--packages", 

137 action="append", 

138 type=str, 

139 help="Python packages to import to register additional translators", 

140 ) 

141 

142 return parser 

143 

144 

145def read_file( 

146 file: str, 

147 hdrnum: int, 

148 print_trace: bool, 

149 outstream: IO = sys.stdout, 

150 errstream: IO = sys.stderr, 

151 output_mode: str = "verbose", 

152 write_heading: bool = False, 

153) -> bool: 

154 """Read the specified file and process it. 

155 

156 Parameters 

157 ---------- 

158 file : `str` 

159 The file from which the header is to be read. 

160 hdrnum : `int` 

161 The HDU number to read. The primary header is always read and 

162 merged with the header from this HDU. 

163 print_trace : `bool` 

164 If there is an error reading the file and this parameter is `True`, 

165 a full traceback of the exception will be reported. If `False` prints 

166 a one line summary of the error condition. 

167 outstream : `io.StringIO`, optional 

168 Output stream to use for standard messages. Defaults to `sys.stdout`. 

169 errstream : `io.StringIO`, optional 

170 Stream to send messages that would normally be sent to standard 

171 error. Defaults to `sys.stderr`. 

172 output_mode : `str`, optional 

173 Output mode to use. Must be one of "verbose", "none", "table", 

174 "yaml", or "fixed". "yaml" and "fixed" can be modified with a 

175 "native" suffix to indicate that the output should be a representation 

176 of the native object type representing the header (which can be 

177 PropertyList or an Astropy header). Without this modify headers 

178 will be dumped as simple `dict` form. 

179 "auto" is used to indicate that a single file has been specified 

180 but the output will depend on whether the file is a multi-extension 

181 FITS file or not. 

182 write_heading: `bool`, optional 

183 If `True` and in table mode, write a table heading out before writing 

184 the content. 

185 

186 Returns 

187 ------- 

188 success : `bool` 

189 `True` if the file was handled successfully, `False` if the file 

190 could not be processed. 

191 """ 

192 if output_mode not in OUTPUT_MODES: 

193 raise ValueError(f"Output mode of '{output_mode}' is not understood.") 

194 

195 # This gets in the way in tabular mode 

196 if output_mode != "table": 

197 print(f"Analyzing {file}...", file=errstream) 

198 

199 try: 

200 md = read_basic_metadata_from_file(file, hdrnum, errstream=errstream, can_raise=True) 

201 if md is None: 

202 raise RuntimeError(f"Failed to read file {file} HDU={hdrnum}") 

203 

204 if output_mode.endswith("native"): 

205 # Strip native and don't change type of md 

206 output_mode = output_mode[: -len("native")] 

207 else: 

208 # Rewrite md as simple dict for output 

209 md = {k: v for k, v in md.items()} 

210 

211 if output_mode in ("yaml", "fixed"): 

212 if output_mode == "fixed": 

213 fix_header(md, filename=file) 

214 

215 # The header should be written out in the insertion order 

216 print(yaml.dump(md, sort_keys=False), file=outstream) 

217 return True 

218 

219 # Try to work out a translator class. 

220 translator_class = MetadataTranslator.determine_translator(md, filename=file) 

221 

222 # Work out which headers to translate, assuming the default if 

223 # we have a YAML test file. 

224 if file.endswith(".yaml"): 

225 headers = [md] 

226 else: 

227 headers = list(translator_class.determine_translatable_headers(file, md)) 

228 if output_mode == "auto": 

229 output_mode = "table" if len(headers) > 1 else "verbose" 

230 

231 wrote_heading = False 

232 for md in headers: 

233 obs_info = ObservationInfo(md, pedantic=True, filename=file) 

234 if output_mode == "table": 

235 columns = [ 

236 "{:{fmt}}".format(getattr(obs_info, c["attr"]), fmt=c["format"]) for c in TABLE_COLUMNS 

237 ] 

238 

239 if write_heading and not wrote_heading: 

240 # Construct headings of the same width as the items 

241 # we have calculated. Doing this means we don't have to 

242 # work out for ourselves how many characters will be used 

243 # for non-strings (especially Quantity) 

244 headings = [] 

245 separators = [] 

246 for thiscol, defn in zip(columns, TABLE_COLUMNS): 

247 width = len(thiscol) 

248 headings.append("{:{w}.{w}}".format(defn["label"], w=width)) 

249 separators.append("-" * width) 

250 print(" ".join(headings), file=outstream) 

251 print(" ".join(separators), file=outstream) 

252 wrote_heading = True 

253 

254 row = " ".join(columns) 

255 print(row, file=outstream) 

256 elif output_mode == "verbose": 

257 print(f"{obs_info}", file=outstream) 

258 elif output_mode == "none": 

259 pass 

260 else: 

261 raise RuntimeError(f"Output mode of '{output_mode}' not recognized but should be known.") 

262 except Exception as e: 

263 if print_trace: 

264 traceback.print_exc(file=outstream) 

265 else: 

266 print(f"Failure processing {file}: {e}", file=outstream) 

267 return False 

268 return True 

269 

270 

271def process_files( 

272 files: Sequence[str], 

273 regex: str, 

274 hdrnum: int, 

275 print_trace: bool, 

276 outstream: IO = sys.stdout, 

277 errstream: IO = sys.stderr, 

278 output_mode: str = "auto", 

279) -> tuple[list[str], list[str]]: 

280 """Read and translate metadata from the specified files. 

281 

282 Parameters 

283 ---------- 

284 files : iterable of `str` 

285 The files or directories from which the headers are to be read. 

286 regex : `str` 

287 Regular expression string used to filter files when a directory is 

288 scanned. 

289 hdrnum : `int` 

290 The HDU number to read. The primary header is always read and 

291 merged with the header from this HDU. 

292 print_trace : `bool` 

293 If there is an error reading the file and this parameter is `True`, 

294 a full traceback of the exception will be reported. If `False` prints 

295 a one line summary of the error condition. 

296 outstream : `io.StringIO`, optional 

297 Output stream to use for standard messages. Defaults to `sys.stdout`. 

298 errstream : `io.StringIO`, optional 

299 Stream to send messages that would normally be sent to standard 

300 error. Defaults to `sys.stderr`. 

301 output_mode : `str`, optional 

302 Output mode to use for the translated information. 

303 "auto" switches based on how many files are found. 

304 

305 Returns 

306 ------- 

307 okay : `list` of `str` 

308 All the files that were processed successfully. 

309 failed : `list` of `str` 

310 All the files that could not be processed. 

311 """ 

312 found_files = find_files(files, regex) 

313 

314 # Convert "auto" to correct mode but for a single file keep it 

315 # auto in case that file has multiple headers 

316 if output_mode == "auto": 

317 if len(found_files) > 1: 

318 output_mode = "table" 

319 

320 # Process each file 

321 failed = [] 

322 okay = [] 

323 heading = True 

324 for path in sorted(found_files): 

325 isok = read_file(path, hdrnum, print_trace, outstream, errstream, output_mode, heading) 

326 heading = False 

327 if isok: 

328 okay.append(path) 

329 else: 

330 failed.append(path) 

331 

332 return okay, failed 

333 

334 

335def main() -> int: 

336 """Read metadata from the supplied files and translate the content to 

337 standard form. 

338 

339 Returns 

340 ------- 

341 status : `int` 

342 Exit status to be passed to `sys.exit()`. 0 if any of the files 

343 could be translated. 1 otherwise. 

344 """ 

345 

346 logging.warn( 

347 "This command is deprecated. Please use 'astrometadata translate' " 

348 " or 'astrometadata dump' instead. See 'astrometadata -h' for more details." 

349 ) 

350 

351 args = build_argparser().parse_args() 

352 

353 # Process import requests 

354 if args.packages: 

355 for m in args.packages: 

356 importlib.import_module(m) 

357 

358 output_mode = args.mode 

359 if args.quiet: 

360 output_mode = "none" 

361 elif args.dumphdr: 

362 output_mode = "yaml" 

363 

364 # Set the log level. Convert to upper case to allow the user to 

365 # specify --log=DEBUG or --log=debug 

366 numeric_level = getattr(logging, args.log.upper(), None) 

367 if not isinstance(numeric_level, int): 

368 raise ValueError(f"Invalid log level: {args.log}") 

369 logging.basicConfig(level=numeric_level) 

370 

371 # Main loop over files 

372 okay, failed = process_files(args.files, args.regex, args.hdrnum, args.traceback, output_mode=output_mode) 

373 

374 if failed: 

375 print("Files with failed translations:", file=sys.stderr) 

376 for f in failed: 

377 print(f"\t{f}", file=sys.stderr) 

378 

379 if okay: 

380 # Good status if anything was returned in okay 

381 return 0 

382 else: 

383 return 1