Coverage for python/lsst/obs/lsst/translators/lsstCam.py: 40%
50 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-23 11:23 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-23 11:23 +0000
1# This file is currently part of obs_lsst but is written to allow it
2# to be migrated to the astro_metadata_translator package at a later date.
3#
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the LICENSE file in this directory for details of code ownership.
7#
8# Use of this source code is governed by a 3-clause BSD-style
9# license that can be found in the LICENSE file.
11"""Metadata translation code for the main LSST Camera"""
13__all__ = ("LsstCamTranslator", )
15import logging
16import astropy.units as u
18from astro_metadata_translator import cache_translation
19from astro_metadata_translator.translators.helpers import is_non_science
21from .lsst import LsstBaseTranslator, SIMONYI_TELESCOPE
23log = logging.getLogger(__name__)
25# Normalized name of the LSST Camera
26LSST_CAM = "LSSTCam"
29def is_non_science_or_lab(self):
30 """Pseudo method to determine whether this is a lab or non-science
31 header.
33 Raises
34 ------
35 KeyError
36 If this is a science observation and on the mountain.
37 """
38 # Return without raising if this is not a science observation
39 # since the defaults are fine.
40 try:
41 # This will raise if it is a science observation.
42 is_non_science(self)
43 return
44 except KeyError:
45 pass
47 # We are still in the lab, return and use the default.
48 if not self._is_on_mountain():
49 return
51 # This is a science observation on the mountain so we should not
52 # use defaults.
53 raise KeyError(f"{self._log_prefix}: Required key is missing and this is a mountain science observation")
56class LsstCamTranslator(LsstBaseTranslator):
57 """Metadata translation for the main LSST Camera."""
59 name = LSST_CAM
60 """Name of this translation class"""
62 supported_instrument = LSST_CAM
63 """Supports the lsstCam instrument."""
65 _const_map = {
66 "instrument": LSST_CAM,
67 "telescope": SIMONYI_TELESCOPE,
68 # Migrate these to full translations once test data appears that
69 # includes them
70 "altaz_begin": None,
71 "object": "UNKNOWN",
72 "relative_humidity": None,
73 "temperature": None,
74 "pressure": None,
75 }
77 _trivial_map = {
78 "detector_group": "RAFTBAY",
79 "detector_name": "CCDSLOT",
80 "observation_id": "OBSID",
81 "exposure_time": ("EXPTIME", dict(unit=u.s)),
82 "detector_serial": "LSST_NUM",
83 "science_program": (["PROGRAM", "RUNNUM"], dict(default="unknown")),
84 "boresight_rotation_angle": (["ROTPA", "ROTANGLE"], dict(checker=is_non_science_or_lab,
85 default=0.0, unit=u.deg)),
86 }
88 # Use Imsim raft definitions until a true lsstCam definition exists
89 cameraPolicyFile = "policy/lsstCam.yaml"
91 @classmethod
92 def fix_header(cls, header, instrument, obsid, filename=None):
93 """Fix LSSTCam headers.
95 Notes
96 -----
97 See `~astro_metadata_translator.fix_header` for details of the general
98 process.
99 """
101 modified = False
103 # Calculate the standard label to use for log messages
104 log_label = cls._construct_log_prefix(obsid, filename)
106 if "FILTER" not in header and header.get("FILTER2") is not None:
107 ccdslot = header.get("CCDSLOT", "unknown")
108 raftbay = header.get("RAFTBAY", "unknown")
110 log.warning("%s %s_%s: No FILTER key found but FILTER2=\"%s\" (removed)",
111 log_label, raftbay, ccdslot, header["FILTER2"])
112 header["FILTER2"] = None
113 modified = True
115 return modified
117 @classmethod
118 def can_translate(cls, header, filename=None):
119 """Indicate whether this translation class can translate the
120 supplied header.
122 Parameters
123 ----------
124 header : `dict`-like
125 Header to convert to standardized form.
126 filename : `str`, optional
127 Name of file being translated.
129 Returns
130 -------
131 can : `bool`
132 `True` if the header is recognized by this class. `False`
133 otherwise.
134 """
135 # INSTRUME keyword might be of two types
136 if "INSTRUME" in header:
137 instrume = header["INSTRUME"].lower()
138 if instrume == cls.supported_instrument.lower():
139 return True
140 return False
142 @cache_translation
143 def to_physical_filter(self):
144 """Calculate the physical filter name.
146 Returns
147 -------
148 filter : `str`
149 Name of filter. Can be a combination of FILTER, FILTER1, and
150 FILTER2 headers joined by a "~". Trailing "~empty" components
151 are stripped.
152 Returns "unknown" if no filter is declared.
153 """
154 joined = super().to_physical_filter()
155 while joined.endswith("~empty"):
156 joined = joined[:-len("~empty")]
158 return joined