Coverage for python/lsst/obs/lsst/translators/comCamSim.py: 26%
69 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-07 01:32 -0800
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-07 01:32 -0800
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 Simulated LSST Commissioning Camera"""
13__all__ = ("LsstComCamSimTranslator", )
15import logging
16import math
18import astropy
19import astropy.units as u
20import astropy.utils.exceptions
21from astropy.coordinates import AltAz
22from astro_metadata_translator import cache_translation
24from .lsstCam import LsstCamTranslator
25from .lsst import SIMONYI_TELESCOPE
27log = logging.getLogger(__name__)
30class LsstComCamSimTranslator(LsstCamTranslator):
31 """Metadata translation for the LSST Commissioning Camera."""
33 name = "LSSTComCamSim"
34 """Name of this translation class"""
36 _const_map = {
37 "instrument": "LSSTComCamSim",
38 "has_simulated_content": True,
39 }
41 cameraPolicyFile = 'policy/comCamSim.yaml'
43 @classmethod
44 def can_translate(cls, header, filename=None):
45 """Indicate whether this translation class can translate the
46 supplied header.
48 Looks for "COMCAMSIM" instrument in case-insensitive manner but
49 must be on LSST telescope. This avoids confusion with other
50 telescopes using commissioning cameras.
52 Parameters
53 ----------
54 header : `dict`-like
55 Header to convert to standardized form.
56 filename : `str`, optional
57 Name of file being translated.
59 Returns
60 -------
61 can : `bool`
62 `True` if the header is recognized by this class. `False`
63 otherwise.
64 """
65 if "INSTRUME" in header and "TELESCOP" in header:
66 telescope = header["TELESCOP"]
67 instrument = header["INSTRUME"].lower()
68 if instrument == "comcamsim" and telescope in (SIMONYI_TELESCOPE, "LSST"):
69 return True
71 return False
73 @classmethod
74 def fix_header(cls, header, instrument, obsid, filename=None):
75 """Fix Simulated ComCam headers.
77 Notes
78 -----
79 Content will be added as needed.
81 Corrections are reported as debug level log messages.
83 See `~astro_metadata_translator.fix_header` for details of the general
84 process.
85 """
86 modified = False
88 # Calculate the standard label to use for log messages
89 log_label = cls._construct_log_prefix(obsid, filename)
91 # Some simulated files lack RASTART/DECSTART etc headers. Since these
92 # are simulated they can be populated directly from the RA/DEC headers.
93 synced_radec = False
94 for key in ("RA", "DEC"):
95 for time in ("START", "END"):
96 time_key = f"{key}{time}"
97 if not header.get(time_key):
98 if (value := header.get(key)):
99 header[time_key] = value
100 synced_radec = True
101 if synced_radec:
102 modified = True
103 log.debug("%s: Synced RASTART/RAEND/DECSTART/DECEND headers with RA/DEC headers", log_label)
105 if not header.get("RADESYS") and header.get("RA") and header.get("DEC"):
106 header["RADESYS"] = "ICRS"
107 log.debug("%s: Forcing undefined RADESYS to '%s'", log_label, header["RADESYS"])
108 modified = True
110 if not header.get("TELCODE"):
111 if camcode := header.get("CAMCODE"):
112 header["TELCODE"] = camcode
113 modified = True
114 log.debug("%s: Setting TELCODE header from CAMCODE header", log_label)
115 else:
116 # Get the code from the OBSID.
117 code, _ = obsid.split("_", 1)
118 header["TELCODE"] = code
119 modified = True
120 log.debug("%s: Determining telescope code of %s from OBSID", log_label, code)
122 return modified
124 def _is_on_mountain(self):
125 """Indicate whether these data are coming from the instrument
126 installed on the mountain.
127 Returns
128 -------
129 is : `bool`
130 `True` if instrument is on the mountain.
132 Notes
133 -----
134 TODO: DM-33387 This is currently a terrible hack and MUST be removed
135 once CAP-807 and CAP-808 are done.
136 Until then, ALL non-calib ComCam data will look like it is on sky.
137 """
138 return True
140 @cache_translation
141 def to_altaz_begin(self):
142 # Tries to calculate the value. Simulated files for ops-rehearsal 3
143 # did not have the AZ/EL headers defined.
144 if self.are_keys_ok(["ELSTART", "AZSTART"]):
145 return super().to_altaz_begin()
147 # The time is not consistent with HASTART/AMSTART values.
148 # This means that the elevation may well come out negative.
149 # Rather than attempting to calculate something that is already
150 # known to be junk, return a fixed value.
151 if self.are_keys_ok(["RA", "DEC"]):
153 # If there is an airmass value, use it for the elevation
154 # to try to use the available information even if inconsistent
155 # with the observing date.
156 airmass = self.to_boresight_airmass()
157 if airmass is None:
158 elevation = 45 * u.deg
159 else:
160 # The number does not have to be accurate.
161 elevation = math.asin(1 / airmass) * u.rad
163 return AltAz(
164 0. * u.deg, elevation, obstime=self.to_datetime_begin(), location=self.to_location()
165 )
167 return None
169 @classmethod
170 def observing_date_to_offset(cls, observing_date: astropy.time.Time) -> astropy.time.TimeDelta | None:
171 # Always use the 12 hour offset.
172 return cls._ROLLOVER_TIME