Coverage for python/lsst/obs/lsst/_fitsHeader.py: 21%
18 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 18:52 -0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 18:52 -0800
1# This file is part of obs_lsst.
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 LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <http://www.lsstcorp.org/LegalNotices/>.
22#
24__all__ = ("readRawFitsHeader",)
26import re
27from astro_metadata_translator import fix_header, merge_headers
28import lsst.afw.fits
31def readRawFitsHeader(fileName, translator_class=None):
32 """Read a FITS header from a raw file and fix it up as required.
34 Parameters
35 ----------
36 fileName : `str`
37 Name of the FITS file. Can include a HDU specifier (although 0
38 is ignored).
39 translator_class : `~astro_metadata_translator.MetadataTranslator`,
40 optional
41 Any translator class to use for fixing up the header.
43 Returns
44 -------
45 md : `PropertyList`
46 Metadata from file. We also merge the contents with the
47 next HDU if an ``INHERIT`` key is not specified. If an explicit
48 HDU is encoded with the file name and it is greater than 0 then
49 no merging will occur.
50 """
51 mat = re.search(r"\[(\d+)\]$", fileName)
52 hdu = None
53 if mat:
54 # Treat 0 as a special case
55 # For some instruments the primary header is empty
56 requested = int(mat.group(1))
57 if requested > 0:
58 hdu = requested
60 if hdu is not None:
61 md = lsst.afw.fits.readMetadata(fileName, hdu=hdu)
62 else:
63 # For raw some of these files need the second header to be
64 # read as well. Not all instruments want the double read
65 # but for now it's easiest to always merge.
66 phdu = lsst.afw.fits.readMetadata(fileName, 0)
67 md = lsst.afw.fits.readMetadata(fileName)
68 md = merge_headers([phdu, md], mode="overwrite")
70 fix_header(md, translator_class=translator_class)
71 return md