Coverage for tests/test_time_utils.py: 19%
Shortcuts 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
Shortcuts 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# (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 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 <https://www.gnu.org/licenses/>.
22"""Unit tests for methods in butler.time module.
23"""
25import unittest
26import warnings
28# As of astropy 4.2, the erfa interface is shipped independently and
29# ErfaWarning is no longer an AstropyWarning
30try:
31 import erfa
32except ImportError:
33 erfa = None
35import astropy.utils.exceptions
36from astropy.time import Time, TimeDelta
37from lsst.daf.butler.core.time_utils import TimeConverter
40class TimeTestCase(unittest.TestCase):
41 """A test case for time module
42 """
44 def test_time_before_epoch(self):
45 """Tests for before-the-epoch time.
46 """
47 time = Time("1950-01-01T00:00:00", format="isot", scale="tai")
48 value = TimeConverter().astropy_to_nsec(time)
49 self.assertEqual(value, 0)
51 value = TimeConverter().nsec_to_astropy(value)
52 self.assertEqual(value, TimeConverter().epoch)
54 def test_max_time(self):
55 """Tests for after-the-end-of-astronomy time.
56 """
57 # there are rounding issues, need more complex comparison
58 time = Time("2101-01-01T00:00:00", format="isot", scale="tai")
59 value = TimeConverter().astropy_to_nsec(time)
61 value_max = TimeConverter().astropy_to_nsec(TimeConverter().max_time)
62 self.assertEqual(value, value_max)
64 # Astropy will give "dubious year" for UTC five years in the future
65 # so hide these expected warnings from the test output
66 with warnings.catch_warnings():
67 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning)
68 if erfa is not None:
69 warnings.simplefilter("ignore", category=erfa.ErfaWarning)
70 time = Time("2101-01-01T00:00:00", format="isot", scale="utc")
72 # unittest can't test for no warnings so we run the test and
73 # trigger our own warning and count all the warnings
74 with self.assertWarns(Warning) as cm:
75 TimeConverter().astropy_to_nsec(time)
76 warnings.warn("deliberate")
77 self.assertEqual(str(cm.warning), "deliberate")
79 def test_round_trip(self):
80 """Test precision of round-trip conversion.
81 """
82 # do tests at random points between epoch and max. time
83 times = [
84 "1970-01-01T12:00:00.123",
85 "1999-12-31T23:59:57.123456",
86 "2000-01-01T12:00:00.123456",
87 "2030-01-01T12:00:00.123456789",
88 "2075-08-17T00:03:45",
89 "2099-12-31T23:00:50",
90 ]
91 for time in times:
92 atime = Time(time, format="isot", scale="tai")
93 for sec in range(7):
94 # loop over few seconds to add to each time
95 for i in range(100):
96 # loop over additional fractions of seconds
97 delta = sec + 0.3e-9 * i
98 in_time = atime + TimeDelta(delta, format="sec")
99 # do round-trip conversion to nsec and back
100 value = TimeConverter().astropy_to_nsec(in_time)
101 value = TimeConverter().nsec_to_astropy(value)
102 delta2 = value - in_time
103 delta2_sec = delta2.to_value("sec")
104 # absolute precision should be better than half
105 # nanosecond, but there are rounding errors too
106 self.assertLess(abs(delta2_sec), 0.51e-9)
108 def test_times_equal(self):
109 """Test for times_equal method
110 """
111 # time == time should always work
112 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
113 self.assertTrue(TimeConverter().times_equal(time1, time1))
115 # one nsec difference
116 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
117 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="tai")
118 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.))
119 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.))
120 self.assertFalse(TimeConverter().times_equal(time1, time2, .5))
121 self.assertFalse(TimeConverter().times_equal(time2, time1, .5))
123 # one nsec difference, times in UTC
124 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="utc")
125 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="utc")
126 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.))
127 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.))
128 self.assertFalse(TimeConverter().times_equal(time1, time2, .5))
129 self.assertFalse(TimeConverter().times_equal(time2, time1, .5))
131 # 1/2 nsec difference
132 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
133 time2 = time1 + TimeDelta(0.5e-9, format="sec")
134 self.assertTrue(TimeConverter().times_equal(time1, time2))
135 self.assertTrue(TimeConverter().times_equal(time2, time1))
136 self.assertFalse(TimeConverter().times_equal(time1, time2, .25))
137 self.assertFalse(TimeConverter().times_equal(time2, time1, .25))
139 # 1/2 microsec difference
140 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
141 time2 = time1 + TimeDelta(0.5e-6, format="sec")
142 self.assertTrue(TimeConverter().times_equal(time1, time2, 501))
143 self.assertTrue(TimeConverter().times_equal(time2, time1, 501))
144 self.assertFalse(TimeConverter().times_equal(time1, time2, 499))
145 self.assertFalse(TimeConverter().times_equal(time2, time1, 499))
147 # UTC vs TAI
148 time1 = Time('2013-06-17 13:34:45.775000', scale='tai', format='iso')
149 time2 = Time('2013-06-17T13:34:10.775', scale='utc', format='isot')
150 self.assertTrue(TimeConverter().times_equal(time1, time2))
151 self.assertEqual(TimeConverter().astropy_to_nsec(time1), TimeConverter().astropy_to_nsec(time2))
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 unittest.main()