Skip to content

Commit

Permalink
Undo leaks from other PR
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcvey3 committed Feb 22, 2024
1 parent 6765512 commit ef857a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
23 changes: 1 addition & 22 deletions mhkit/dolfyn/io/nortek2.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ def __init__(
self._check_nortek(endian)
self.f.seek(0, 2) # Seek to end
self._eof = self.f.tell()
self.start_pos = self._check_header()
self._index = lib.get_index(
fname, pos=self.start_pos, reload=rebuild_index, debug=debug
)
self._index = lib.get_index(fname, reload=rebuild_index, debug=debug)
self._reopen(bufsize)
self.filehead_config = self._read_filehead_config_string()
self._ens_pos = self._index["pos"][
Expand Down Expand Up @@ -171,24 +168,6 @@ def _check_nortek(self, endian):
)
self.endian = endian

def _check_header(self):
def find_all(s, c):
idx = s.find(c)
while idx != -1:
yield idx
idx = s.find(c, idx + 1)

# Guess that a header won't be longer than 10,000 bytes
self._reopen(10000)
pk = self.f.peek(1)
# Search for multiple saved headers
found = [i for i in find_all(pk, b"GETCLOCKSTR")]
if len(found) < 2:
return 0
else:
start_idx = found[-1] - 11
return start_idx

def _reopen(self, bufsize=None):
if bufsize is None:
bufsize = 1000000
Expand Down
13 changes: 4 additions & 9 deletions mhkit/dolfyn/io/nortek2_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _calc_time(year, month, day, hour, minute, second, usec, zero_is_bad=True):
return dt


def _create_index(infile, outfile, N_ens, init_pos, debug):
def _create_index(infile, outfile, N_ens, debug):
logging = getLogger()
print("Indexing {}...".format(infile), end="")
fin = open(_abspath(infile), "rb")
Expand Down Expand Up @@ -137,18 +137,13 @@ def _create_index(infile, outfile, N_ens, init_pos, debug):
}
while N[21] < N_ens: # Will fail if velocity ping isn't saved first
pos = fin.tell()
if init_pos and not pos:
fin.seek(init_pos, 1)
try:
dat = _hdr.unpack(fin.read(_hdr.size))
except:
break
if dat[2] in ids:
idk = dat[2]
d_ver, d_off, config = struct.unpack("<BBH", fin.read(4))
if d_ver not in [1, 3]:
# 1 for bottom track, 3 for all others
continue
fin.seek(4, 1)
yr, mo, dy, h, m, s, u = struct.unpack("6BH", fin.read(8))
fin.seek(14, 1)
Expand Down Expand Up @@ -186,7 +181,7 @@ def _create_index(infile, outfile, N_ens, init_pos, debug):
fin.seek(dat[4] - (36 + seek_2ens[idk]), 1)
last_ens[idk] = ens[idk]

if debug:
if debug and N[idk] < 5:
# hex: [18, 15, 1C, 17] = [vel_b5, vel, echo, bt]
logging.info(
"%10d: %02X, %d, %02X, %d, %d, %d, %d\n"
Expand Down Expand Up @@ -259,7 +254,7 @@ def _boolarray_firstensemble_ping(index):
return dens


def get_index(infile, pos=0, reload=False, debug=False):
def get_index(infile, reload=False, debug=False):
"""
This function reads ad2cp.index files
Expand All @@ -280,7 +275,7 @@ def get_index(infile, pos=0, reload=False, debug=False):

index_file = infile + ".index"
if not path.isfile(index_file) or reload:
_create_index(infile, index_file, 2**32, pos, debug)
_create_index(infile, index_file, 2**32, debug)
f = open(_abspath(index_file), "rb")
file_head = f.read(12)
if file_head[:10] == b"Index Ver:":
Expand Down

0 comments on commit ef857a4

Please sign in to comment.