diff --git a/src/ctapipe_io_zfits/conftest.py b/src/ctapipe_io_zfits/conftest.py index 05c497d..66e8704 100644 --- a/src/ctapipe_io_zfits/conftest.py +++ b/src/ctapipe_io_zfits/conftest.py @@ -9,7 +9,7 @@ from protozfits.CTA_DL0_Subarray_pb2 import DataStream, Event from protozfits.CoreMessages_pb2 import AnyArray -from ctapipe_io_zfits.time import time_to_cta_high +from ctapipe_io_zfits.time import time_to_cta_high_res ANY_ARRAY_TYPE_TO_NUMPY_TYPE = { 1: np.int8, @@ -108,7 +108,7 @@ def dummy_dl0(dl0_base): trigger_file.move_to_new_table("Events") for event_id in range(1, 101): - time_s, time_qns = time_to_cta_high(time) + time_s, time_qns = time_to_cta_high_res(time) trigger_file.write_message(Event( event_id=event_id, trigger_type=1, diff --git a/src/ctapipe_io_zfits/tests/test_time.py b/src/ctapipe_io_zfits/tests/test_time.py new file mode 100644 index 0000000..958036b --- /dev/null +++ b/src/ctapipe_io_zfits/tests/test_time.py @@ -0,0 +1,37 @@ +from astropy.time import Time +import astropy.units as u +import numpy as np +import pytest + +roundtrip_times = [ + '2020-01-01T00:00:00.12345678925', + '2020-01-01T00:00:00.1234567895', + '2020-01-01T00:00:00.12345678975', + '2020-01-01T00:00:00.123456790', +] + +@pytest.mark.parametrize("timestamp", roundtrip_times) +def test_cta_high_round_trip(timestamp): + from ctapipe_io_zfits.time import time_to_cta_high_res, cta_high_res_to_time + + # note: precision=9 only affects text conversion, not actual precision + time = Time(timestamp, scale='tai', precision=9) + seconds, quarter_nanoseconds = time_to_cta_high_res(time) + time_back = cta_high_res_to_time(seconds, quarter_nanoseconds) + + roundtrip_error = (time - time_back).to_value(u.ns) + np.testing.assert_almost_equal(roundtrip_error, 0.0) + + +test_data = [ + (Time(0, 12.25e-9, format='unix_tai'), 0, 49), + (Time(12345, 12.25e-9, format='unix_tai'), 12345, 49), +] + +@pytest.mark.parametrize("time,expected_s,expected_qns", test_data) +def test_cta_time_to_cta_high_res(time, expected_s, expected_qns): + from ctapipe_io_zfits.time import time_to_cta_high_res + + seconds, quarter_nanoseconds = time_to_cta_high_res(time) + assert seconds == expected_s + assert quarter_nanoseconds == expected_qns diff --git a/src/ctapipe_io_zfits/time.py b/src/ctapipe_io_zfits/time.py index b9fbf5a..0b09c86 100644 --- a/src/ctapipe_io_zfits/time.py +++ b/src/ctapipe_io_zfits/time.py @@ -32,7 +32,7 @@ def to_seconds(days): return np.divmod(seconds, 1) -def time_to_cta_high(time): +def time_to_cta_high_res(time): '''Convert astropy Time to cta high precision timestamp''' # make sure we are in TAI time = time.tai