Coverage for tests/test_time_utils.py : 16%

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