Skip to content

Commit 5b87fe2

Browse files
committed
Rewrite method for files that rewrite header configuration strings
1 parent 67518d4 commit 5b87fe2

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

mhkit/dolfyn/io/nortek2.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
from ..time import epoch2dt64, _fill_time_gaps
1515

1616

17-
int32_max = np.iinfo(np.int32).max
18-
19-
2017
def read_signature(
2118
filename,
2219
userdata=True,
@@ -157,10 +154,9 @@ def __init__(
157154
self._check_nortek(endian)
158155
self.f.seek(0, 2) # Seek to end
159156
self._eof = self.f.tell()
160-
self.start_pos = 0 # self._check_header()
161157
self._index, self._dp = lib.get_index(
162158
fname,
163-
pos=self.start_pos,
159+
pos=0,
164160
eof=self._eof,
165161
rebuild=rebuild_index,
166162
debug=debug,
@@ -201,28 +197,6 @@ def _check_nortek(self, endian):
201197
)
202198
self.endian = endian
203199

204-
def _check_header(self):
205-
def find_all(s, c):
206-
idx = s.find(c)
207-
while idx != -1:
208-
yield idx
209-
idx = s.find(c, idx + 1)
210-
211-
# Open the entire file to find start header
212-
if self._eof >= int32_max:
213-
init_buffer = int32_max
214-
else:
215-
init_buffer = self._eof
216-
self._open(init_buffer)
217-
pk = self.f.peek(1)
218-
# Search for multiple saved headers
219-
found = [i for i in find_all(pk, b"GETCLOCKSTR")]
220-
if len(found) < 2:
221-
return 0
222-
else:
223-
start_idx = found[-1] - 11
224-
return start_idx
225-
226200
def _open(self, bufsize=None):
227201
if bufsize is None:
228202
bufsize = 1000000

mhkit/dolfyn/io/nortek2_lib.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ def _calc_time(year, month, day, hour, minute, second, usec, zero_is_bad=True):
106106
return dt
107107

108108

109+
def __check_header(infile_obj, eof):
110+
def find_all(s, c):
111+
idx = s.find(c)
112+
while idx != -1:
113+
yield idx
114+
idx = s.find(c, idx + 1)
115+
116+
# Save current position
117+
current_pos = infile_obj.tell()
118+
119+
# Close and reopen file with larger buffer
120+
infile_obj.close()
121+
int32_max = np.iinfo(np.int32).max
122+
if eof >= int32_max:
123+
buffer = int32_max
124+
else:
125+
buffer = eof
126+
fin = open(_abspath(infile_obj.name), "rb", buffer)
127+
fin.seek(current_pos, 1)
128+
129+
# Search for multiple saved headers
130+
pk = fin.peek(1)
131+
found = [i for i in find_all(pk, b"GETCLOCKSTR")]
132+
if found:
133+
start_idx = found[0] - 11 # assuming next header is 10 bytes
134+
else:
135+
start_idx = 0
136+
137+
return fin, start_idx
138+
139+
109140
def _create_index(infile, outfile, init_pos, eof, debug):
110141
logging = getLogger()
111142
print("Indexing {}...".format(infile), end="")
@@ -156,8 +187,20 @@ def _create_index(infile, outfile, init_pos, eof, debug):
156187
fin.seek(-10, 1)
157188
dat = struct.unpack("<BBBBIhh", fin.read(12))
158189
else:
190+
# Lost header
159191
if debug:
160192
logging.debug("Header is not 10 or 12 bytes: %10d\n" % (pos))
193+
# check for if file was terminated and restarted
194+
# by searching for start of header configuration string
195+
fin, to_skip = __check_header(fin, eof)
196+
if to_skip:
197+
fin.seek(to_skip, 1)
198+
if debug:
199+
logging.debug(
200+
"Found new header string as position: %10d\n"
201+
% (pos + to_skip)
202+
)
203+
continue
161204
if dat[2] in ids:
162205
idk = dat[2]
163206
# version, byte offset to actual data, configuration bit mask

0 commit comments

Comments
 (0)