From 9369440fa9fce7dfc1fd7bf7bf5477d5f934c952 Mon Sep 17 00:00:00 2001 From: Adam Reeve Date: Thu, 25 Jan 2024 21:02:41 +1300 Subject: [PATCH] Fix defragmenting a file with timestamp channel data --- nptdms/test/writer/test_writer.py | 13 +++++++++++++ nptdms/timestamp.py | 3 +++ 2 files changed, 16 insertions(+) diff --git a/nptdms/test/writer/test_writer.py b/nptdms/test/writer/test_writer.py index d0a2360..3c0f268 100644 --- a/nptdms/test/writer/test_writer.py +++ b/nptdms/test/writer/test_writer.py @@ -8,6 +8,7 @@ import tempfile from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject, types +from nptdms.test import scenarios def test_can_read_tdms_file_after_writing(): @@ -550,3 +551,15 @@ def test_write_and_store_index_file(): ]) assert os.path.isfile(tdms_path + "_index") + + +def test_defragment_raw_timestamp_file(): + test_file, expected_data = scenarios.timestamp_data().values + with test_file.get_tempfile() as temp_file: + target_buf = BytesIO() + TdmsWriter.defragment(temp_file, target_buf) + target_buf.seek(0, os.SEEK_SET) + f = TdmsFile.read(target_buf) + for (group, channel), expected_values in expected_data.items(): + channel_data = f[group][channel][:] + np.testing.assert_equal(channel_data, expected_values) diff --git a/nptdms/timestamp.py b/nptdms/timestamp.py index f486d09..4fe68fd 100644 --- a/nptdms/timestamp.py +++ b/nptdms/timestamp.py @@ -16,7 +16,10 @@ class TdmsTimestamp(object): :ivar ~.seconds: Seconds since the epoch as a signed integer :ivar ~.second_fractions: A positive number of 2^-64 fractions of a second """ + + # Attributes that let this class act like a TdmsType when writing data enum_value = 0x44 + size = 16 def __init__(self, seconds, second_fractions): self.seconds = seconds