Coverage for python/lsst/obs/lsst/translators/lsst.py: 23%
426 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 support code for LSST headers"""
13__all__ = ("TZERO", "SIMONYI_LOCATION", "read_detector_ids",
14 "compute_detector_exposure_id_generic", "LsstBaseTranslator",
15 "SIMONYI_TELESCOPE")
17import os.path
18import yaml
19import logging
20import re
21import datetime
22import hashlib
24import astropy.coordinates
25import astropy.units as u
26from astropy.time import Time, TimeDelta
27from astropy.coordinates import EarthLocation
29from lsst.utils import getPackageDir
31from astro_metadata_translator import cache_translation, FitsTranslator
32from astro_metadata_translator.translators.helpers import tracking_from_degree_headers, \
33 altaz_from_degree_headers
36TZERO = Time("2015-01-01T00:00", format="isot", scale="utc")
37TZERO_DATETIME = TZERO.to_datetime()
39# Delimiter to use for multiple filters/gratings
40FILTER_DELIMITER = "~"
42# Regex to use for parsing a GROUPID string
43GROUP_RE = re.compile(r"^(\d\d\d\d\-\d\d\-\d\dT\d\d:\d\d:\d\d)\.(\d\d\d)(?:[\+#](\d+))?$")
45# LSST Default location in the absence of headers
46SIMONYI_LOCATION = EarthLocation.from_geodetic(-70.749417, -30.244639, 2663.0)
48# Name of the main survey telescope
49SIMONYI_TELESCOPE = "Simonyi Survey Telescope"
51# Supported controller codes.
52# The order here directly relates to the resulting exposure ID
53# calculation. Do not reorder. Add new ones to the end.
54# OCS, CCS, pHosim, P for simulated OCS, Q for simulated CCS, S for
55# simulated images.
56SIMULATED_CONTROLLERS = "HPQS"
57CONTROLLERS = "OC" + SIMULATED_CONTROLLERS
59# Number of decimal digits allocated to the sequence number in exposure_ids.
60_SEQNUM_MAXDIGITS = 5
62# Number of decimal digits allocated to the day of observation (and controller
63# code) in exposure_ids.
64_DAYOBS_MAXDIGITS = 8
66# Value added to day_obs for controllers after the default.
67_CONTROLLER_INCREMENT = 1000_00_00
69# Number of decimal digits used by exposure_ids.
70EXPOSURE_ID_MAXDIGITS = _SEQNUM_MAXDIGITS + _DAYOBS_MAXDIGITS
72obs_lsst_packageDir = getPackageDir("obs_lsst")
74log = logging.getLogger(__name__)
77def read_detector_ids(policyFile):
78 """Read a camera policy file and retrieve the mapping from CCD name
79 to ID.
81 Parameters
82 ----------
83 policyFile : `str`
84 Name of YAML policy file to read, relative to the obs_lsst
85 package.
87 Returns
88 -------
89 mapping : `dict` of `str` to (`int`, `str`)
90 A `dict` with keys being the full names of the detectors, and the
91 value is a `tuple` containing the integer detector number and the
92 detector serial number.
94 Notes
95 -----
96 Reads the camera YAML definition file directly and extracts just the
97 IDs and serials. This routine does not use the standard
98 `~lsst.obs.base.yamlCamera.YAMLCamera` infrastructure or
99 `lsst.afw.cameraGeom`. This is because the translators are intended to
100 have minimal dependencies on LSST infrastructure.
101 """
103 file = os.path.join(obs_lsst_packageDir, policyFile)
104 try:
105 with open(file) as fh:
106 # Use the fast parser since these files are large
107 camera = yaml.load(fh, Loader=yaml.CSafeLoader)
108 except OSError as e:
109 raise ValueError(f"Could not load camera policy file {file}") from e
111 mapping = {}
112 for ccd, value in camera["CCDs"].items():
113 mapping[ccd] = (int(value["id"]), value["serial"])
115 return mapping
118def compute_detector_exposure_id_generic(exposure_id, detector_num, max_num):
119 """Compute the detector_exposure_id from the exposure id and the
120 detector number.
122 Parameters
123 ----------
124 exposure_id : `int`
125 The exposure ID.
126 detector_num : `int`
127 The detector number.
128 max_num : `int`
129 Maximum number of detectors to make space for.
131 Returns
132 -------
133 detector_exposure_id : `int`
134 Computed ID.
136 Raises
137 ------
138 ValueError
139 The detector number is out of range.
140 """
142 if detector_num is None:
143 raise ValueError("Detector number must be defined.")
144 if detector_num >= max_num or detector_num < 0:
145 raise ValueError(f"Detector number out of range 0 <= {detector_num} < {max_num}")
147 return max_num*exposure_id + detector_num
150class LsstBaseTranslator(FitsTranslator):
151 """Translation methods useful for all LSST-style headers."""
153 _const_map = {}
154 _trivial_map = {}
156 # Do not specify a name for this translator
157 cameraPolicyFile = None
158 """Path to policy file relative to obs_lsst root."""
160 detectorMapping = None
161 """Mapping of detector name to detector number and serial."""
163 detectorSerials = None
164 """Mapping of detector serial number to raft, number, and name."""
166 DETECTOR_MAX = 1000
167 """Maximum number of detectors to use when calculating the
168 detector_exposure_id.
170 Note that because this is the maximum number *of* detectors, for
171 zero-based ``detector_num`` values this is one greater than the maximum
172 ``detector_num``. It is also often rounded up to the nearest power of
173 10 anyway, to allow ``detector_exposure_id`` values to be easily decoded by
174 humans.
175 """
177 _DEFAULT_LOCATION = SIMONYI_LOCATION
178 """Default telescope location in absence of relevant FITS headers."""
180 _ROLLOVER_TIME = TimeDelta(12*60*60, scale="tai", format="sec")
181 """Time delta for the definition of a Rubin Observatory start of day.
182 Used when the header is missing. See LSE-400 or SITCOMTN-032 for details.
183 """
185 @classmethod
186 def __init_subclass__(cls, **kwargs):
187 """Ensure that subclasses clear their own detector mapping entries
188 such that subclasses of translators that use detector mappings
189 do not pick up the incorrect values from a parent."""
191 cls.detectorMapping = None
192 cls.detectorSerials = None
194 super().__init_subclass__(**kwargs)
196 def search_paths(self):
197 """Search paths to use for LSST data when looking for header correction
198 files.
200 Returns
201 -------
202 path : `list`
203 List with a single element containing the full path to the
204 ``corrections`` directory within the ``obs_lsst`` package.
205 """
206 return [os.path.join(obs_lsst_packageDir, "corrections")]
208 @classmethod
209 def observing_date_to_offset(cls, observing_date: astropy.time.Time) -> astropy.time.TimeDelta | None:
210 """Return the offset to use when calculating the observing day.
212 Parameters
213 ----------
214 observing_date : `astropy.time.Time`
215 The date of the observation. Unused.
217 Returns
218 -------
219 offset : `astropy.time.TimeDelta`
220 The offset to apply. The default implementation returns a fixed
221 number but subclasses can return a different value depending
222 on whether the instrument is in the instrument lab or on the
223 mountain.
224 """
225 return cls._ROLLOVER_TIME
227 @classmethod
228 def compute_detector_exposure_id(cls, exposure_id, detector_num):
229 """Compute the detector exposure ID from detector number and
230 exposure ID.
232 This is a helper method to allow code working outside the translator
233 infrastructure to use the same algorithm.
235 Parameters
236 ----------
237 exposure_id : `int`
238 Unique exposure ID.
239 detector_num : `int`
240 Detector number.
242 Returns
243 -------
244 detector_exposure_id : `int`
245 The calculated ID.
246 """
247 from .._packer import RubinDimensionPacker
249 return RubinDimensionPacker.pack_id_pair(exposure_id, detector_num)
251 @classmethod
252 def max_detector_exposure_id(cls):
253 """The maximum detector exposure ID expected to be generated by
254 this instrument.
256 Returns
257 -------
258 max_id : `int`
259 The maximum value.
260 """
261 max_exposure_id = cls.max_exposure_id()
262 # We subtract 1 from DETECTOR_MAX because LSST detector_num values are
263 # zero-based, and detector_max is the maximum number *of* detectors,
264 # while this returns the (inclusive) maximum ID value.
265 return cls.compute_detector_exposure_id(max_exposure_id, cls.DETECTOR_MAX - 1)
267 @classmethod
268 def max_exposure_id(cls):
269 """The maximum exposure ID expected from this instrument.
271 Returns
272 -------
273 max_exposure_id : `int`
274 The maximum value.
275 """
276 max_date = "2050-12-31T23:59.999"
277 max_seqnum = 99_999
278 # This controller triggers the largest numbers
279 max_controller = CONTROLLERS[-1]
280 return cls.compute_exposure_id(max_date, max_seqnum, max_controller)
282 @classmethod
283 def detector_mapping(cls):
284 """Returns the mapping of full name to detector ID and serial.
286 Returns
287 -------
288 mapping : `dict` of `str`:`tuple`
289 Returns the mapping of full detector name (group+detector)
290 to detector number and serial.
292 Raises
293 ------
294 ValueError
295 Raised if no camera policy file has been registered with this
296 translation class.
298 Notes
299 -----
300 Will construct the mapping if none has previously been constructed.
301 """
302 if cls.cameraPolicyFile is not None:
303 if cls.detectorMapping is None:
304 cls.detectorMapping = read_detector_ids(cls.cameraPolicyFile)
305 else:
306 raise ValueError(f"Translation class '{cls.__name__}' has no registered camera policy file")
308 return cls.detectorMapping
310 @classmethod
311 def detector_serials(cls):
312 """Obtain the mapping of detector serial to detector group, name,
313 and number.
315 Returns
316 -------
317 info : `dict` of `tuple` of (`str`, `str`, `int`)
318 A `dict` with the serial numbers as keys and values of detector
319 group, name, and number.
320 """
321 if cls.detectorSerials is None:
322 detector_mapping = cls.detector_mapping()
324 if detector_mapping is not None:
325 # Form mapping to go from serial number to names/numbers
326 serials = {}
327 for fullname, (id, serial) in cls.detectorMapping.items():
328 raft, detector_name = fullname.split("_")
329 if serial in serials:
330 raise RuntimeError(f"Serial {serial} is defined in multiple places")
331 serials[serial] = (raft, detector_name, id)
332 cls.detectorSerials = serials
333 else:
334 raise RuntimeError("Unable to obtain detector mapping information")
336 return cls.detectorSerials
338 @classmethod
339 def compute_detector_num_from_name(cls, detector_group, detector_name):
340 """Helper method to return the detector number from the name.
342 Parameters
343 ----------
344 detector_group : `str`
345 Name of the detector grouping. This is generally the raft name.
346 detector_name : `str`
347 Detector name.
349 Returns
350 -------
351 num : `int`
352 Detector number.
353 """
354 fullname = f"{detector_group}_{detector_name}"
356 num = None
357 detector_mapping = cls.detector_mapping()
358 if detector_mapping is None:
359 raise RuntimeError("Unable to obtain detector mapping information")
361 if fullname in detector_mapping:
362 num = detector_mapping[fullname]
363 else:
364 log.warning(f"Unable to determine detector number from detector name {fullname}")
365 return None
367 return num[0]
369 @classmethod
370 def compute_detector_info_from_serial(cls, detector_serial):
371 """Helper method to return the detector information from the serial.
373 Parameters
374 ----------
375 detector_serial : `str`
376 Detector serial ID.
378 Returns
379 -------
380 info : `tuple` of (`str`, `str`, `int`)
381 Detector group, name, and number.
382 """
383 serial_mapping = cls.detector_serials()
384 if serial_mapping is None:
385 raise RuntimeError("Unable to obtain serial mapping information")
387 if detector_serial in serial_mapping:
388 info = serial_mapping[detector_serial]
389 else:
390 raise RuntimeError("Unable to determine detector information from detector serial"
391 f" {detector_serial}")
393 return info
395 @staticmethod
396 def compute_exposure_id(dayobs, seqnum, controller=None):
397 """Helper method to calculate the exposure_id.
399 Parameters
400 ----------
401 dayobs : `str` or `int`
402 Day of observation in either YYYYMMDD or YYYY-MM-DD format.
403 If the string looks like ISO format it will be truncated before the
404 ``T`` before being handled.
405 seqnum : `int` or `str`
406 Sequence number.
407 controller : `str`, optional
408 Controller to use. If this is "O", no change is made to the
409 exposure ID. If it is "C" a 1000 is added to the year component
410 of the exposure ID. If it is "H" a 2000 is added to the year
411 component. This sequence continues with "P" and "Q" controllers.
412 `None` indicates that the controller is not relevant to the
413 exposure ID calculation (generally this is the case for test
414 stand data).
416 Returns
417 -------
418 exposure_id : `int`
419 Exposure ID in form YYYYMMDDnnnnn form.
420 """
421 # We really want an integer but the checks require a str.
422 if isinstance(dayobs, int):
423 dayobs = str(dayobs)
425 if "T" in dayobs:
426 dayobs = dayobs[:dayobs.find("T")]
428 dayobs = dayobs.replace("-", "")
430 if len(dayobs) != 8:
431 raise ValueError(f"Malformed dayobs: {dayobs}")
433 # Expect no more than 99,999 exposures in a day
434 if seqnum >= 10**_SEQNUM_MAXDIGITS:
435 raise ValueError(f"Sequence number ({seqnum}) exceeds limit")
437 dayobs = int(dayobs)
438 if dayobs > 20231004 and controller == "C":
439 # As of this date the CCS controller has a unified counter
440 # with the OCS, so there is no need to adjust the dayobs
441 # to make unique exposure IDs.
442 controller = None
444 # Camera control changes the exposure ID
445 if controller is not None:
446 index = CONTROLLERS.find(controller)
447 if index == -1:
448 raise ValueError(f"Supplied controller, '{controller}' is not "
449 f"in supported list: {CONTROLLERS}")
451 # Increment a thousand years per controller
452 dayobs += _CONTROLLER_INCREMENT * index
454 # Form the number as a string zero padding the sequence number
455 idstr = f"{dayobs}{seqnum:0{_SEQNUM_MAXDIGITS}d}"
457 # Exposure ID has to be an integer
458 return int(idstr)
460 @staticmethod
461 def unpack_exposure_id(exposure_id):
462 """Unpack an exposure ID into dayobs, seqnum, and controller.
464 Parameters
465 ----------
466 exposure_id : `int`
467 Integer exposure ID produced by `compute_exposure_id`.
469 Returns
470 -------
471 dayobs : `str`
472 Day of observation as a YYYYMMDD string.
473 seqnum : `int`
474 Sequence number.
475 controller : `str`
476 Controller code. Will be ``O`` (but should be ignored) for IDs
477 produced by calling `compute_exposure_id` with ``controller=None``.
478 """
479 dayobs, seqnum = divmod(exposure_id, 10**_SEQNUM_MAXDIGITS)
480 controller_index = dayobs // _CONTROLLER_INCREMENT - 2
481 dayobs -= controller_index * _CONTROLLER_INCREMENT
482 return (str(dayobs), seqnum, CONTROLLERS[controller_index], )
484 def _is_on_mountain(self):
485 """Indicate whether these data are coming from the instrument
486 installed on the mountain.
488 Returns
489 -------
490 is : `bool`
491 `True` if instrument is on the mountain.
492 """
493 if "TSTAND" in self._header:
494 return False
495 return True
497 def is_on_sky(self):
498 """Determine if this is an on-sky observation.
500 Returns
501 -------
502 is_on_sky : `bool`
503 Returns True if this is a observation on sky on the
504 summit.
505 """
506 # For LSST we think on sky unless tracksys is local
507 if self.is_key_ok("TRACKSYS"):
508 if self._header["TRACKSYS"].lower() == "local":
509 # not on sky
510 return False
512 # These are obviously not on sky
513 if self.to_observation_type() in ("bias", "dark", "flat"):
514 return False
516 return self._is_on_mountain()
518 @cache_translation
519 def to_location(self):
520 # Docstring will be inherited. Property defined in properties.py
521 if not self._is_on_mountain():
522 return None
523 try:
524 # Try standard FITS headers
525 return super().to_location()
526 except (KeyError, TypeError):
527 return self._DEFAULT_LOCATION
529 @cache_translation
530 def to_datetime_begin(self):
531 # Docstring will be inherited. Property defined in properties.py
532 # Prefer -BEG over -OBS. Let it fail with KeyError if no headers
533 # can be found.
534 date_key = "MJD-BEG"
535 date_fmt = "mjd"
536 for k in ("MJD-BEG", "DATE-BEG", "MJD-OBS", "DATE-OBS"):
537 if self.is_key_ok(k):
538 date_key = k
539 date_fmt = "mjd" if k.startswith("MJD") else "fits"
540 break
542 self._used_these_cards(date_key)
543 return Time(self._header[date_key], scale="tai", format=date_fmt)
545 @cache_translation
546 def to_datetime_end(self):
547 # Docstring will be inherited. Property defined in properties.py
548 if self.is_key_ok("DATE-END"):
549 return super().to_datetime_end()
551 exposure_time = self.to_exposure_time()
552 if exposure_time.value < 0.0:
553 # Some translators deliberately return -1.0s if the exposure
554 # time can not be determined. In that scenario set end time
555 # to the same value as the start time.
556 return self.to_datetime_begin()
558 return self.to_datetime_begin() + exposure_time
560 @cache_translation
561 def to_detector_num(self):
562 # Docstring will be inherited. Property defined in properties.py
563 raft = self.to_detector_group()
564 detector = self.to_detector_name()
565 return self.compute_detector_num_from_name(raft, detector)
567 @cache_translation
568 def to_detector_exposure_id(self):
569 # Docstring will be inherited. Property defined in properties.py
570 exposure_id = self.to_exposure_id()
571 num = self.to_detector_num()
572 return self.compute_detector_exposure_id(exposure_id, num)
574 @cache_translation
575 def to_observation_type(self):
576 # Docstring will be inherited. Property defined in properties.py
577 obstype = self._header["IMGTYPE"]
578 self._used_these_cards("IMGTYPE")
579 obstype = obstype.lower()
580 if obstype in ("skyexp", "object"):
581 obstype = "science"
582 return obstype
584 @cache_translation
585 def to_observation_reason(self):
586 # Docstring will be inherited. Property defined in properties.py
587 for key in ("REASON", "TESTTYPE"):
588 if self.is_key_ok(key):
589 reason = self._header[key]
590 self._used_these_cards(key)
591 return reason.lower()
592 # no specific header present so use the default translation
593 return super().to_observation_reason()
595 @cache_translation
596 def to_dark_time(self):
597 """Calculate the dark time.
599 If a DARKTIME header is not found, the value is assumed to be
600 identical to the exposure time.
602 Returns
603 -------
604 dark : `astropy.units.Quantity`
605 The dark time in seconds.
606 """
607 if self.is_key_ok("DARKTIME"):
608 darktime = self._header["DARKTIME"]*u.s
609 self._used_these_cards("DARKTIME")
610 else:
611 log.warning("%s: Unable to determine dark time. Setting from exposure time.",
612 self._log_prefix)
613 darktime = self.to_exposure_time()
614 return darktime
616 def _get_controller_code(self) -> str | None:
617 """Return the controller code.
619 Returns
620 -------
621 code : `str`
622 Single character code representing the controller. Returns
623 `None` if no controller can be determined.
624 """
625 key = "CONTRLLR"
626 if self.is_key_ok(key):
627 controller = self._header[key]
628 self._used_these_cards(key)
629 else:
630 controller = None
631 return controller
633 @cache_translation
634 def to_exposure_id(self):
635 """Generate a unique exposure ID number
637 This is a combination of DAYOBS and SEQNUM, and optionally
638 CONTRLLR.
640 Returns
641 -------
642 exposure_id : `int`
643 Unique exposure number.
644 """
645 if "CALIB_ID" in self._header:
646 self._used_these_cards("CALIB_ID")
647 return None
649 dayobs = self._header["DAYOBS"]
650 seqnum = self._header["SEQNUM"]
651 self._used_these_cards("DAYOBS", "SEQNUM")
653 controller = self._get_controller_code()
655 return self.compute_exposure_id(dayobs, seqnum, controller=controller)
657 @cache_translation
658 def to_visit_id(self):
659 """Calculate the visit associated with this exposure.
661 Notes
662 -----
663 For LATISS and LSSTCam the default visit is derived from the
664 exposure group. For other instruments we return the exposure_id.
665 """
667 exposure_group = self.to_exposure_group()
668 # If the group is an int we return it
669 try:
670 visit_id = int(exposure_group)
671 return visit_id
672 except ValueError:
673 pass
675 # A Group is defined as ISO date with an extension
676 # The integer must be the same for a given group so we can never
677 # use datetime_begin.
678 # Nominally a GROUPID looks like "ISODATE+N" where the +N is
679 # optional. This can be converted to seconds since epoch with
680 # an adjustment for N.
681 # For early data lacking that form we hash the group and return
682 # the int.
683 matches_date = GROUP_RE.match(exposure_group)
684 if matches_date:
685 iso_str = matches_date.group(1)
686 fraction = matches_date.group(2)
687 n = matches_date.group(3)
688 if n is not None:
689 n = int(n)
690 else:
691 n = 0
692 iso = datetime.datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%S")
694 tdelta = iso - TZERO_DATETIME
695 epoch = int(tdelta.total_seconds())
697 # Form the integer from EPOCH + 3 DIGIT FRAC + 0-pad N
698 visit_id = int(f"{epoch}{fraction}{n:04d}")
699 else:
700 # Non-standard string so convert to numbers
701 # using a hash function. Use the first N hex digits
702 group_bytes = exposure_group.encode("us-ascii")
703 hasher = hashlib.blake2b(group_bytes)
704 # Need to be big enough it does not possibly clash with the
705 # date-based version above
706 digest = hasher.hexdigest()[:14]
707 visit_id = int(digest, base=16)
709 # To help with hash collision, append the string length
710 visit_id = int(f"{visit_id}{len(exposure_group):02d}")
712 return visit_id
714 @cache_translation
715 def to_physical_filter(self):
716 """Calculate the physical filter name.
718 Returns
719 -------
720 filter : `str`
721 Name of filter. Can be a combination of FILTER, FILTER1 and FILTER2
722 headers joined by a "~". Returns "unknown" if no filter is declared
723 """
724 joined = self._join_keyword_values(["FILTER", "FILTER1", "FILTER2"], delim=FILTER_DELIMITER)
725 if not joined:
726 joined = "unknown"
728 # Replace instances of "NONE" with "none".
729 joined = joined.replace("NONE", "none")
731 return joined
733 @cache_translation
734 def to_tracking_radec(self):
735 # RA/DEC are *derived* headers and for the case where the DATE-BEG
736 # is 1970 they are garbage and should not be used.
737 try:
738 if self._header["DATE-OBS"] == self._header["DATE"]:
739 # A fixed up date -- use AZEL as source of truth
740 altaz = self.to_altaz_begin()
741 radec = astropy.coordinates.SkyCoord(altaz.transform_to(astropy.coordinates.ICRS()),
742 obstime=altaz.obstime,
743 location=altaz.location)
744 else:
745 radecsys = ("RADESYS",)
746 radecpairs = (("RASTART", "DECSTART"), ("RA", "DEC"))
747 radec = tracking_from_degree_headers(self, radecsys, radecpairs)
748 except Exception:
749 # If this observation was not formally on sky then we are allowed
750 # to return None.
751 if self.is_on_sky():
752 raise
753 radec = None
755 return radec
757 @cache_translation
758 def to_altaz_begin(self):
759 if not self._is_on_mountain():
760 return None
762 # H controller data are sometimes science observations without
763 # having AZSTART header. The code lets those return nothing.
764 if self._get_controller_code() == "H" and not self.are_keys_ok(["ELSTART", "AZSTART"]):
765 return None
767 # Always attempt to find the alt/az values regardless of observation
768 # type.
769 return altaz_from_degree_headers(self, (("ELSTART", "AZSTART"),),
770 self.to_datetime_begin(), is_zd=False)
772 @cache_translation
773 def to_exposure_group(self):
774 """Calculate the exposure group string.
776 For LSSTCam and LATISS this is read from the ``GROUPID`` header.
777 If that header is missing the exposure_id is returned instead as
778 a string.
779 """
780 if self.is_key_ok("GROUPID"):
781 exposure_group = self._header["GROUPID"]
782 self._used_these_cards("GROUPID")
783 return exposure_group
784 return super().to_exposure_group()
786 @cache_translation
787 def to_focus_z(self):
788 """Return the defocal distance of the camera in units of mm.
789 If there is no ``FOCUSZ`` value in the header it will return
790 the default 0.0mm value.
792 Returns
793 -------
794 focus_z: `astropy.units.Quantity`
795 The defocal distance from header in mm or the 0.0mm default
796 """
797 if self.is_key_ok("FOCUSZ"):
798 # Some broken files have strings instead of floats.
799 focus_z = float(self._header["FOCUSZ"])
800 return focus_z * u.mm
801 return super().to_focus_z()
803 @staticmethod
804 def _is_filter_empty(filter):
805 """Return true if the supplied filter indicates an empty filter slot
807 Parameters
808 ----------
809 filter : `str`
810 The filter string to check.
812 Returns
813 -------
814 is_empty : `bool`
815 `True` if the filter string looks like it is referring to an
816 empty filter slot. For example this can be if the filter is
817 "empty" or "empty_2".
818 """
819 return bool(re.match(r"empty_?\d*$", filter.lower()))
821 def _determine_primary_filter(self):
822 """Determine the primary filter from the ``FILTER`` header.
824 Returns
825 -------
826 filter : `str`
827 The contents of the ``FILTER`` header with some appropriate
828 defaulting.
829 """
831 if self.is_key_ok("FILTER"):
832 physical_filter = self._header["FILTER"]
833 self._used_these_cards("FILTER")
835 if self._is_filter_empty(physical_filter):
836 physical_filter = "empty"
837 else:
838 # Be explicit about having no knowledge of the filter
839 # by setting it to "unknown". It should always have a value.
840 physical_filter = "unknown"
842 # Warn if the filter being unknown is important
843 obstype = self.to_observation_type()
844 if obstype not in ("bias", "dark"):
845 log.warning("%s: Unable to determine the filter",
846 self._log_prefix)
848 return physical_filter
850 @cache_translation
851 def to_observing_day(self):
852 """Return the day of observation as YYYYMMDD integer.
854 For LSSTCam and other compliant instruments this is the value
855 of the DAYOBS header.
857 Returns
858 -------
859 obs_day : `int`
860 The day of observation.
861 """
862 if self.is_key_ok("DAYOBS"):
863 self._used_these_cards("DAYOBS")
864 return int(self._header["DAYOBS"])
866 return super().to_observing_day()
868 @cache_translation
869 def to_observation_counter(self):
870 """Return the sequence number within the observing day.
872 Returns
873 -------
874 counter : `int`
875 The sequence number for this day.
876 """
877 if self.is_key_ok("SEQNUM"):
878 # Some older LATISS data may not have the header
879 # but this is corrected in fix_header for LATISS.
880 self._used_these_cards("SEQNUM")
881 return int(self._header["SEQNUM"])
883 # This indicates a problem so we warn and return a 0
884 log.warning("%s: Unable to determine the observation counter so returning 0",
885 self._log_prefix)
886 return 0
888 @cache_translation
889 def to_boresight_rotation_coord(self):
890 """Boresight rotation angle.
892 Only relevant for science observations.
893 """
894 unknown = "unknown"
895 if not self.is_on_sky():
896 return unknown
898 self._used_these_cards("ROTCOORD")
899 coord = self._header.get("ROTCOORD", unknown)
900 if coord is None:
901 coord = unknown
902 return coord
904 @cache_translation
905 def to_boresight_airmass(self):
906 """Calculate airmass at boresight at start of observation.
908 Notes
909 -----
910 Early data are missing AMSTART header so we fall back to calculating
911 it from ELSTART.
912 """
913 if not self.is_on_sky():
914 return None
916 # This observation should have AMSTART
917 amkey = "AMSTART"
918 if self.is_key_ok(amkey):
919 self._used_these_cards(amkey)
920 return self._header[amkey]
922 # Instead we need to look at azel
923 altaz = self.to_altaz_begin()
924 if altaz is not None:
925 return altaz.secz.to_value()
927 log.warning("%s: Unable to determine airmass of a science observation, returning 1.",
928 self._log_prefix)
929 return 1.0
931 @cache_translation
932 def to_group_counter_start(self):
933 # Effectively the start of the visit as determined by the headers.
934 counter = self.to_observation_counter()
935 # Older data does not have the CURINDEX header.
936 if self.is_key_ok("CURINDEX"):
937 # CURINDEX is 1-based.
938 seq_start = counter - self._header["CURINDEX"] + 1
939 self._used_these_cards("CURINDEX")
940 return seq_start
941 else:
942 # If the counter is 0 we need to pick something else
943 # that is not going to confuse the visit calculation
944 # (since setting everything to 0 will make one big visit).
945 return counter if counter != 0 else self.to_exposure_id()
947 @cache_translation
948 def to_group_counter_end(self):
949 # Effectively the end of the visit as determined by the headers.
950 counter = self.to_observation_counter()
951 # Older data does not have the CURINDEX or MAXINDEX headers.
952 if self.is_key_ok("CURINDEX") and self.is_key_ok("MAXINDEX"):
953 # CURINDEX is 1-based. CURINDEX == MAXINDEX indicates the
954 # final exposure in the sequence.
955 remaining = self._header["MAXINDEX"] - self._header["CURINDEX"]
956 seq_end = counter + remaining
957 self._used_these_cards("CURINDEX", "MAXINDEX")
958 return seq_end
959 else:
960 # If the counter is 0 we need to pick something else
961 # that is not going to confuse the visit calculation
962 # (since setting everything to 0 will make one big visit).
963 return counter if counter != 0 else self.to_exposure_id()
965 @cache_translation
966 def to_has_simulated_content(self):
967 # Check all the simulation flags.
968 # We do not know all the simulation flags that we may have so
969 # must check every header key. Ideally HIERARCH SIMULATE would
970 # be a hierarchical header so _header["SIMULATE"] would return
971 # everything. The header looks like:
972 #
973 # HIERARCH SIMULATE ATMCS = / ATMCS Simulation Mode
974 # HIERARCH SIMULATE ATHEXAPOD = 0 / ATHexapod Simulation Mode
975 # HIERARCH SIMULATE ATPNEUMATICS = / ATPneumatics Simulation Mode
976 # HIERARCH SIMULATE ATDOME = 1 / ATDome Simulation Mode
977 # HIERARCH SIMULATE ATSPECTROGRAPH = 0 / ATSpectrograph Simulation Mode
978 #
979 # So any header that includes "SIMULATE" in the key name and has a
980 # true value implies that something in the data is simulated.
981 for k, v in self._header.items():
982 if "SIMULATE" in k and v:
983 self._used_these_cards(k)
984 return True
986 # If the controller is H, P, S, or Q then the data are simulated.
987 controller = self._get_controller_code()
988 if controller:
989 if controller in SIMULATED_CONTROLLERS:
990 return True
992 # No simulation flags set.
993 return False
995 @cache_translation
996 def to_relative_humidity(self) -> float | None:
997 key = "HUMIDITY"
998 if self.is_key_ok(key):
999 self._used_these_cards(key)
1000 return self._header[key]
1002 return None
1004 @cache_translation
1005 def to_pressure(self):
1006 key = "PRESSURE"
1007 if self.is_key_ok(key):
1008 value = self._header[key]
1009 self._used_these_cards(key)
1010 # There has been an inconsistency in units for the pressure reading
1011 # so we need to adjust for this.
1012 if value > 10_000:
1013 unit = u.Pa
1014 else:
1015 unit = u.hPa
1016 return value * unit
1018 return None
1020 @cache_translation
1021 def to_temperature(self):
1022 key = "AIRTEMP"
1023 if self.is_key_ok(key):
1024 self._used_these_cards(key)
1025 return self._header[key] * u.deg_C
1026 return None
1028 @cache_translation
1029 def to_can_see_sky(self) -> bool | None:
1030 key = "SHUTTIME"
1031 if self.is_key_ok(key) and self._header[key] == 0.0:
1032 # Shutter never opened so impossible to see sky.
1033 self._used_these_cards(key)
1034 return False
1036 key = "VIGN_MIN"
1037 if self.is_key_ok(key):
1038 self._used_these_cards(key)
1039 vignetted = self._header[key]
1040 if vignetted == "FULLY":
1041 return False
1042 return True
1044 # Fallback to using the observation type if the key is missing.
1045 # May not want to allow this for non-simulated cameras after
1046 # comcam goes on sky.
1047 return super().to_can_see_sky()