Coverage for tests/test_time_utils.py: 15%
75 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 11:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 11:00 +0000
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# (https://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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <https://www.gnu.org/licenses/>.
28"""Unit tests for methods in butler.time module.
29"""
31import unittest
32import warnings
34# As of astropy 4.2, the erfa interface is shipped independently and
35# ErfaWarning is no longer an AstropyWarning
36try:
37 import erfa
38except ImportError:
39 erfa = None
41import astropy.utils.exceptions
42from astropy.time import Time, TimeDelta
43from lsst.daf.butler.time_utils import TimeConverter
46class TimeTestCase(unittest.TestCase):
47 """A test case for time module"""
49 def test_time_before_epoch(self):
50 """Tests for before-the-epoch time."""
51 time = Time("1950-01-01T00:00:00", format="isot", scale="tai")
52 value = TimeConverter().astropy_to_nsec(time)
53 self.assertEqual(value, 0)
55 value = TimeConverter().nsec_to_astropy(value)
56 self.assertEqual(value, TimeConverter().epoch)
58 def test_max_time(self):
59 """Tests for after-the-end-of-astronomy time."""
60 # there are rounding issues, need more complex comparison
61 time = Time("2101-01-01T00:00:00", format="isot", scale="tai")
62 value = TimeConverter().astropy_to_nsec(time)
64 value_max = TimeConverter().astropy_to_nsec(TimeConverter().max_time)
65 self.assertEqual(value, value_max)
67 # Astropy will give "dubious year" for UTC five years in the future
68 # so hide these expected warnings from the test output
69 with warnings.catch_warnings():
70 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning)
71 if erfa is not None:
72 warnings.simplefilter("ignore", category=erfa.ErfaWarning)
73 time = Time("2101-01-01T00:00:00", format="isot", scale="utc")
75 # unittest can't test for no warnings so we run the test and
76 # trigger our own warning and count all the warnings
77 with self.assertWarns(Warning) as cm:
78 TimeConverter().astropy_to_nsec(time)
79 warnings.warn("deliberate", stacklevel=1)
80 self.assertEqual(str(cm.warning), "deliberate")
82 def test_round_trip(self):
83 """Test precision of round-trip conversion."""
84 # do tests at random points between epoch and max. time
85 times = [
86 "1970-01-01T12:00:00.123",
87 "1999-12-31T23:59:57.123456",
88 "2000-01-01T12:00:00.123456",
89 "2030-01-01T12:00:00.123456789",
90 "2075-08-17T00:03:45",
91 "2099-12-31T23:00:50",
92 ]
93 for time in times:
94 atime = Time(time, format="isot", scale="tai")
95 for sec in range(7):
96 # loop over few seconds to add to each time
97 for i in range(100):
98 # loop over additional fractions of seconds
99 delta = sec + 0.3e-9 * i
100 in_time = atime + TimeDelta(delta, format="sec")
101 # do round-trip conversion to nsec and back
102 value = TimeConverter().astropy_to_nsec(in_time)
103 value = TimeConverter().nsec_to_astropy(value)
104 delta2 = value - in_time
105 delta2_sec = delta2.to_value("sec")
106 # absolute precision should be better than half
107 # nanosecond, but there are rounding errors too
108 self.assertLess(abs(delta2_sec), 0.51e-9)
110 def test_times_equal(self):
111 """Test for times_equal method"""
112 # time == time should always work
113 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
114 self.assertTrue(TimeConverter().times_equal(time1, time1))
116 # one nsec difference
117 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
118 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="tai")
119 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.0))
120 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.0))
121 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.5))
122 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.5))
124 # one nsec difference, times in UTC
125 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="utc")
126 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="utc")
127 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.0))
128 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.0))
129 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.5))
130 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.5))
132 # 1/2 nsec difference
133 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
134 time2 = time1 + TimeDelta(0.5e-9, format="sec")
135 self.assertTrue(TimeConverter().times_equal(time1, time2))
136 self.assertTrue(TimeConverter().times_equal(time2, time1))
137 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.25))
138 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.25))
140 # 1/2 microsec difference
141 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
142 time2 = time1 + TimeDelta(0.5e-6, format="sec")
143 self.assertTrue(TimeConverter().times_equal(time1, time2, 501))
144 self.assertTrue(TimeConverter().times_equal(time2, time1, 501))
145 self.assertFalse(TimeConverter().times_equal(time1, time2, 499))
146 self.assertFalse(TimeConverter().times_equal(time2, time1, 499))
148 # UTC vs TAI
149 time1 = Time("2013-06-17 13:34:45.775000", scale="tai", format="iso")
150 time2 = Time("2013-06-17T13:34:10.775", scale="utc", format="isot")
151 self.assertTrue(TimeConverter().times_equal(time1, time2))
152 self.assertEqual(TimeConverter().astropy_to_nsec(time1), TimeConverter().astropy_to_nsec(time2))
155if __name__ == "__main__":
156 unittest.main()