Coverage for python/astro_metadata_translator/serialize/fits.py : 11%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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.
12"""Transform ObservationInfo into "standard" FITS headers."""
14__all__ = ("info_to_fits", "dates_to_fits", "group_to_fits")
17def dates_to_fits(date_begin, date_end):
18 """Convert two dates into FITS form.
20 Parameters
21 ----------
22 date_begin : `astropy.time.Time`
23 Date representing the beginning of the observation.
24 date_end : `astropy.time.Time`
25 Date representing the end of the observation.
27 Returns
28 -------
29 cards : `dict` of `str` to `str` or `float`
30 Header card keys and values following the FITS standard.
31 If neither date is defined this may be empty.
32 """
33 cards = {}
34 if date_begin is None and date_end is None:
35 # no date headers can be written
36 return cards
38 cards["TIMESYS"] = "TAI"
40 date_avg = None
41 if date_begin is not None and date_end is not None:
42 date_avg = date_begin + (date_end - date_begin)/2.0
44 for fragment, date in (("OBS", date_begin), ("END", date_end), ("AVG", date_avg)):
45 if date is not None:
46 tai = date.tai
47 cards[f"DATE-{fragment}"] = tai.isot
48 cards[f"MJD-{fragment}"] = tai.mjd
50 return cards
53def info_to_fits(obs_info):
54 """Convert an `ObservationInfo` to something suitable for writing
55 to a FITS file.
57 Parameters
58 ----------
59 obs_info : `ObservationInfo`
60 Standardized observation information to transform to FITS headers.
62 Returns
63 -------
64 cards : `dict` of `str` to (`int`, `float`, `str`, `bool`)
65 FITS header keys and values in form understood by FITS.
66 comments : `dict` of `str` to `str`
67 Suitable comment string. There will be at most one entry for each key
68 in ``cards``.
69 """
71 cards = {}
72 comments = {}
74 if obs_info.instrument is not None:
75 cards["INSTRUME"] = obs_info.instrument
76 comments["INSTRUME"] = "Name of instrument"
78 cards.update(dates_to_fits(obs_info.datetime_begin, obs_info.datetime_end))
80 return cards, comments
83def group_to_fits(obs_group):
84 """Convert an `ObservationGroup` to something suitable for writing
85 to a FITS file.
87 Parameters
88 ----------
89 obs_group : `ObservationGroup`
90 Collection of observation information to transform to a single
91 FITS header.
93 Returns
94 -------
95 cards : `dict` of `str` to (`int`, `float`, `str`, `bool`)
96 FITS header keys and values in form understood by FITS.
97 comments : `dict` of `str` to `str`
98 Suitable comment string. There will be at most one entry for each key
99 in ``cards``.
100 """
102 cards = {}
103 comments = {}
105 oldest, newest = obs_group.extremes()
107 instruments = obs_group.property_values("instrument")
108 if len(instruments) == 1:
109 cards["INSTRUME"] = list(instruments)[0]
110 comments["INSTRUME"] = "Name of instrument"
112 cards.update(dates_to_fits(oldest.datetime_begin, newest.datetime_end))
114 return cards, comments