Coverage for tests/test_time_utils.py : 18%

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