Skip to content

Commit b3f5f48

Browse files
committed
minor improvements to ParseBI500 class
1 parent dc48266 commit b3f5f48

1 file changed

Lines changed: 46 additions & 69 deletions

File tree

echopype/convert/parse_bi500.py

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@
1414

1515
FILE_TYPES = ["-Data", "-Info", "-Ping", "-Vlog", "-Snap", "-Work"]
1616

17+
REQUIRED_FILES = ["-Data", "-Info", "-Ping"]
18+
19+
# Common BI500 Ping and Vlog parameters for unpacking
20+
PV_FILE_FORMAT = ">llfffflffllffllll"
21+
22+
PV_FILE_SIZE = 68
23+
24+
PV_FIELDS = (
25+
"Date",
26+
"Time",
27+
"Distance",
28+
"Latitude",
29+
"Longitude",
30+
"BottomDepth",
31+
"EchogramType",
32+
"PelagicUpper",
33+
"PelagicLower",
34+
"PelagicCount",
35+
"PelagicOffset",
36+
"BottomUpper",
37+
"BottomLower",
38+
"BottomCount",
39+
"BottomOffset",
40+
"EchotraceCount",
41+
"EchotraceOffset",
42+
)
43+
1744

1845
class ParseBI500(ParseBase):
1946
"""Class for converting data from Bergen Integrator (BI500) software."""
@@ -26,8 +53,8 @@ def __init__(self, file, file_meta, storage_options={}, sonar_model="BI500"):
2653
self.file_type_map = defaultdict(None)
2754

2855
self.parameters = defaultdict(list)
29-
self.ping_counts = defaultdict(list)
30-
self.vlog_counts = defaultdict(list)
56+
self.ping_data = defaultdict(list)
57+
self.vlog_data = defaultdict(list)
3158
self.index_counts = defaultdict(list)
3259
self.unpacked_data = defaultdict(list)
3360
self.fsmap = self._validate_folder_path(file)
@@ -40,24 +67,26 @@ def _validate_folder_path(self, folder_path):
4067
all_files = fsmap.fs.ls(folder_path)
4168
except NotADirectoryError:
4269
raise ValueError(
43-
"Expecting a folder containing at least '-Data' and '-Info' files, "
70+
"Expecting a folder containing at least '-Data', '-Info' and '-Ping' files, "
4471
f"but got {folder_path}"
4572
)
4673

4774
if isinstance(all_files[0], str):
4875
reqd_files = [
49-
file for file in all_files if file.endswith("-Data") or file.endswith("-Info")
76+
file
77+
for file in all_files
78+
if any(file.endswith(file_type) for file_type in REQUIRED_FILES)
5079
]
5180
else:
5281
reqd_files = [
5382
file
5483
for file in all_files
55-
if file.get("name").endswith("-Data") or file.get("name").endswith("-Info")
84+
if any(file.get("name").endswith(file_type) for file_type in REQUIRED_FILES)
5685
]
5786

5887
if len(reqd_files) < 2:
5988
raise ValueError(
60-
"Expecting a folder containing at least '-Data' and '-Info' files, "
89+
"Expecting a folder containing at least '-Data', '-Info' and '-Ping' files, "
6190
f"but got {folder_path} with at least one required file missing."
6291
)
6392

@@ -72,7 +101,7 @@ def _print_files(self, all_files):
72101
for file_type in self.file_types:
73102
if file_name.endswith(file_type):
74103
self.file_type_map[file_type] = file_name
75-
logger.info(file_name)
104+
logger.info(f"Found file: {file_name}")
76105

77106
def load_BI500_info(self):
78107
"""
@@ -112,88 +141,36 @@ def load_BI500_ping(self):
112141
Parses the BI500 Ping file.
113142
"""
114143

115-
# BI500 Ping file parameters for unpacking
116-
PING_FILE_FORMAT = ">llfffflffllffllll"
117-
PING_FILE_SIZE = 68
118-
ping_vars = (
119-
"Date",
120-
"Time",
121-
"Distance",
122-
"Latitude",
123-
"Longitude",
124-
"BottomDepth",
125-
"EchogramType",
126-
"PelagicUpper",
127-
"PelagicLower",
128-
"PelagicCount",
129-
"PelanvicOffset",
130-
"BottomUpper",
131-
"BottomLower",
132-
"BottomCount",
133-
"BottomOffset",
134-
"EchotraceCount",
135-
"EchotraceOffset",
136-
)
137-
138144
bi500_ping = self.fsmap.fs.open(self.file_type_map["-Ping"], mode="rb")
139145

140146
# Unpack the BI500 Ping file
141147
eof = False
142148
while not eof:
143-
data_read = bi500_ping.read(PING_FILE_SIZE)
149+
data_read = bi500_ping.read(PV_FILE_SIZE)
144150
if data_read:
145-
data = unpack(PING_FILE_FORMAT, data_read)
146-
for name, data in zip(ping_vars, data):
151+
data = unpack(PV_FILE_FORMAT, data_read)
152+
for name, data in zip(PV_FIELDS, data):
147153
if name == "PelagicCount" or name == "BottomCount" or name == "EchotraceCount":
148-
self.ping_counts[camelcase2snakecase(name)].append(data)
149-
else:
150-
self.parameters[camelcase2snakecase(name)].append(data)
154+
self.index_counts[camelcase2snakecase(name)].append(data)
155+
self.ping_data[camelcase2snakecase(name)].append(data)
151156
else:
152157
eof = True
153-
# Set the index counts equal to the ping counts
154-
self.index_counts = self.ping_counts
155158

156159
def load_BI500_vlog(self):
157160
"""
158161
Parses the BI500 Vlog file.
159162
"""
160163

161-
# BI500 Info file parameters for unpacking
162-
VLOG_FILE_FORMAT = ">llfffflffllffllll"
163-
VLOG_FILE_SIZE = 68
164-
vlog_vars = (
165-
"Date",
166-
"Time",
167-
"Distance",
168-
"Latitude",
169-
"Longitude",
170-
"BottomDepth",
171-
"EchogramType",
172-
"PelagicUpper",
173-
"PelagicLower",
174-
"PelagicCount",
175-
"PelanvicOffset",
176-
"BottomUpper",
177-
"BottomLower",
178-
"BottomCount",
179-
"BottomOffset",
180-
"EchotraceCount",
181-
"EchotraceOffset",
182-
)
183-
184164
bi500_vlog = self.fsmap.fs.open(self.file_type_map["-Vlog"], mode="rb")
185165

186-
# Unpack the BI500 info file
166+
# Unpack the BI500 Vlog file
187167
eof = False
188168
while not eof:
189-
data_read = bi500_vlog.read(VLOG_FILE_SIZE)
169+
data_read = bi500_vlog.read(PV_FILE_SIZE)
190170
if data_read:
191-
data = unpack(VLOG_FILE_FORMAT, data_read)
192-
for name, data in zip(vlog_vars, data):
193-
if name == "PelagicCount" or name == "BottomCount" or name == "EchotraceCount":
194-
self.vlog_counts[camelcase2snakecase(name)].append(data)
195-
else:
196-
self.parameters[camelcase2snakecase(name)].append(data)
171+
data = unpack(PV_FILE_FORMAT, data_read)
172+
for name, data in zip(PV_FIELDS, data):
173+
self.vlog_data[camelcase2snakecase(name)].append(data)
197174
else:
198175
eof = True
199176

0 commit comments

Comments
 (0)