Skip to content

Commit

Permalink
Fix for reading tdms_index files with extra padding after metadata (#197
Browse files Browse the repository at this point in the history
)
  • Loading branch information
adamreeve committed May 5, 2020
1 parent 560f7b7 commit 3d4ba3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions nptdms/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def read_metadata(self):
# Read metadata first to work out how much space we need
previous_segment = None
while True:
start_position = file.tell()
try:
segment = self._read_segment_metadata(
file, segment_position, previous_segment, reading_index_file)
Expand All @@ -88,8 +89,11 @@ def read_metadata(self):
previous_segment = segment

segment_position = segment.next_segment_pos
if not reading_index_file:
file.seek(segment.next_segment_pos)
if reading_index_file:
lead_size = 7 * 4
file.seek(start_position + lead_size + segment.raw_data_offset, os.SEEK_SET)
else:
file.seek(segment.next_segment_pos, os.SEEK_SET)
finally:
if reading_index_file:
file.close()
Expand Down
23 changes: 23 additions & 0 deletions nptdms/test/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,29 @@ def channel_without_data_or_data_type():
return test_file, expected_data


@scenario
def extra_padding_after_metadata():
test_file = GeneratedFile()
test_file.add_segment(
("kTocMetaData", "kTocRawData", "kTocNewObjList"),
segment_objects_metadata(
channel_metadata("/'group'/'channel1'", TDS_TYPE_INT32, 2),
) + "00 00 00 00 00 00 00 00",
"01 00 00 00" "02 00 00 00"
)
test_file.add_segment(
("kTocMetaData", "kTocRawData", "kTocNewObjList"),
segment_objects_metadata(
channel_metadata("/'group'/'channel1'", TDS_TYPE_INT32, 2),
) + "00 00 00 00 00 00 00 00",
"03 00 00 00" "04 00 00 00"
)
expected_data = {
('group', 'channel1'): np.array([1, 2, 3, 4], dtype=np.int32),
}
return test_file, expected_data


def timestamp_data_chunk(times):
epoch = np.datetime64('1904-01-01T00:00:00')

Expand Down

0 comments on commit 3d4ba3e

Please sign in to comment.