Skip to content

Commit

Permalink
Unify time function names, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Aug 31, 2023
1 parent 6cf2690 commit 39e4ea7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ctapipe_io_zfits/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions src/ctapipe_io_zfits/tests/test_time.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/ctapipe_io_zfits/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39e4ea7

Please sign in to comment.