Skip to content

Commit 048afe7

Browse files
authored
Fix for corrupted Nortek files (#372)
If a datafile is filled with bad bytes, the reader will get stuck in one of two while loops.
1 parent b4016b9 commit 048afe7

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

mhkit/dolfyn/io/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def _handle_nan(data):
8383
Finds trailing nan's that cause issues in running the rotation
8484
algorithms and deletes them.
8585
"""
86+
if "time" not in data["coords"]:
87+
raise Exception("No data recorded in file.")
88+
8689
nan = np.zeros(data["coords"]["time"].shape, dtype=bool)
8790
l = data["coords"]["time"].size
8891

mhkit/dolfyn/io/nortek.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def __init__(
262262
self.config["coord_sys_axes"]
263263
]
264264
da["has_imu"] = 0 # Initiate attribute
265+
self._eof = self.pos
265266
if self.debug:
266267
logging.info("Init completed")
267268

@@ -384,13 +385,17 @@ def findnext(self, do_cs=True):
384385
if self.endian == "<":
385386
func = np.uint8
386387
func2 = lib._bitshift8
388+
searching = False
387389
while True:
388390
val = unpack(self.endian + "H", self.read(2))[0]
389391
if np.array(val).astype(func) == 165 and (not do_cs or cs == sum):
390392
self.f.seek(-2, 1)
391393
return hex(func2(val))
392394
sum += cs
393395
cs = val
396+
if self.debug and not searching:
397+
logging.debug("Scanning every 2 bytes for next datablock...")
398+
searching = True
394399

395400
def read_id(self):
396401
"""Read the next 'ID' from the file."""
@@ -456,13 +461,17 @@ def findnextid(self, id):
456461
id = int(id, 0)
457462
nowid = None
458463
while nowid != id:
464+
pos = self.pos
459465
nowid = self.read_id()
460466
if nowid == 16:
461467
shift = 22
462468
else:
463469
sz = 2 * unpack(self.endian + "H", self.read(2))[0]
464470
shift = sz - 4
465471
self.f.seek(shift, 1)
472+
# If we get stuck in a while loop
473+
if self.pos == pos:
474+
self.f.seek(2, 1)
466475
return self.pos
467476

468477
def code_spacing(self, searchcode, iternum=50):

0 commit comments

Comments
 (0)