Coverage for python/lsst/daf/butler/core/time_utils.py : 31%

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 daf_butler.
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 GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = ("astropy_to_nsec", "nsec_to_astropy", "times_equal")
25import logging
27import astropy.time
30# These constants can be used by client code
31EPOCH = astropy.time.Time("1970-01-01 00:00:00", format="iso", scale="tai")
32"""Epoch for calculating time delta, this is the minimum time that can be
33stored in the database.
34"""
36MAX_TIME = astropy.time.Time("2100-01-01 00:00:00", format="iso", scale="tai")
37"""Maximum time value that we can store. Assuming 64-bit integer field we
38can actually store higher values but we intentionally limit it to arbitrary
39but reasonably high value. Note that this value will be stored in registry
40database for eternity, so it should not be changed without proper
41consideration.
42"""
44# number of nanosecons in a day
45_NSEC_PER_DAY = 1_000_000_000 * 24 * 3600
47_LOG = logging.getLogger(__name__)
50def astropy_to_nsec(astropy_time: astropy.time.Time) -> int:
51 """Convert astropy time to nanoseconds since epoch.
53 Input time is converted to TAI scale before conversion to
54 nanoseconds.
56 Parameters
57 ----------
58 astropy_time : `astropy.time.Time`
59 Time to be converted.
61 Returns
62 -------
63 time_nsec : `int`
64 Nanoseconds since epoch.
66 Note
67 ----
68 Only the limited range of input times is supported by this method as it
69 is defined useful in the context of Butler and Registry. If input time is
70 earlier than epoch time then this method returns 0. If input time comes
71 after the max. time then it returns number corresponding to max. time.
72 """
73 # sometimes comparison produces warnings if input value is in UTC
74 # scale, transform it to TAI before doing anyhting
75 value = astropy_time.tai
76 # anything before epoch or after MAX_TIME is truncated
77 if value < EPOCH:
78 _LOG.warning("'%s' is earlier than epoch time '%s', epoch time will be used instead",
79 astropy_time, EPOCH)
80 value = EPOCH
81 elif value > MAX_TIME:
82 _LOG.warning("'%s' is later than max. time '%s', max. time time will be used instead",
83 value, MAX_TIME)
84 value = MAX_TIME
86 delta = value - EPOCH
87 # Special care needed to preserve nanosecond precision.
88 # Usually jd1 has no fractional part but just in case.
89 jd1, extra_jd2 = divmod(delta.jd1, 1)
90 value = int(jd1) * _NSEC_PER_DAY + int(round((delta.jd2 + extra_jd2)*_NSEC_PER_DAY))
91 return value
94def nsec_to_astropy(time_nsec: int) -> astropy.time.Time:
95 """Convert nanoseconds since epoch to astropy time.
97 Parameters
98 ----------
99 time_nsec : `int`
100 Nanoseconds since epoch.
102 Returns
103 -------
104 astropy_time : `astropy.time.Time`
105 Time to be converted.
107 Note
108 ----
109 Usually the input time for this method is the number returned from
110 `astropy_to_nsec` which has a limited range. This method does not check
111 that the number falls in the supported range and can produce output
112 time that is outside of that range.
113 """
114 jd1, jd2 = divmod(time_nsec, _NSEC_PER_DAY)
115 delta = astropy.time.TimeDelta(float(jd1), float(jd2)/_NSEC_PER_DAY, format="jd", scale="tai")
116 value = EPOCH + delta
117 return value
120def times_equal(time1: astropy.time.Time,
121 time2: astropy.time.Time,
122 precision_nsec: float = 1.0) -> bool:
123 """Check that times are equal within specified precision.
125 Parameters
126 ----------
127 time1, time2 : `astropy.time.Time`
128 Times to compare.
129 precision_nsec : `float`, optional
130 Precision to use for comparison in nanoseconds, default is one
131 nanosecond which is larger that round-trip error for conversion
132 to/from integer nanoseconds.
133 """
134 # To compare we need them in common scale, for simplicity just
135 # bring them both to TAI scale
136 time1 = time1.tai
137 time2 = time2.tai
138 delta = (time2.jd1 - time1.jd1) + (time2.jd2 - time1.jd2)
139 delta *= _NSEC_PER_DAY
140 return abs(delta) < precision_nsec