Skip to content

Commit b86415e

Browse files
committed
dfreader_pandas: add instance filtering
1 parent 4b79339 commit b86415e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

dfreader_pandas.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import os
13+
import re
1314
import json
1415
import hashlib
1516
import pandas as pd
@@ -36,8 +37,10 @@ def parse_log_to_df(path, specs, frequency, cache_dir=None):
3637
next_emit_time = None
3738

3839
rows = []
40+
# Make a list of base (non-instanced) message types
41+
filter_types = [re.sub(r"\[\d+\]", "", m) for m in fields.keys()]
3942
while True:
40-
msg = reader.recv_match(type=fields.keys())
43+
msg = reader.recv_match(type=filter_types)
4144
if msg is None:
4245
break
4346
if not first_timestamp:
@@ -74,19 +77,20 @@ def new_row(reader: DFReader_binary, fields):
7477
def expand_field_specs(specs, reader: DFReader_binary):
7578
out = {}
7679
for spec in specs:
77-
if "." in spec:
78-
msg, field = spec.split(".")
79-
if msg not in reader.name_to_id:
80-
raise ValueError(f"Message {msg} not found in log file")
81-
fmt = reader.formats[reader.name_to_id[msg]]
80+
msg, field = spec.split(".") if "." in spec else (spec, None)
81+
msg_base = re.sub(r"\[\d+\]", "", msg)
82+
if msg_base not in reader.name_to_id:
83+
raise ValueError(f"Message {msg_base} not found in log file")
84+
fmt = reader.formats[reader.name_to_id[msg_base]]
85+
if msg_base != msg and fmt.instance_field is None:
86+
raise ValueError(
87+
f"Message {msg_base} does not support instances, but {msg} was requested"
88+
)
89+
if field is not None:
8290
if field not in fmt.columns:
83-
raise ValueError(f"Field {field} not found in message {msg}")
91+
raise ValueError(f"Field {field} not found in message {msg_base}")
8492
out.setdefault(msg, []).append(field)
8593
else:
86-
msg = spec
87-
if msg not in reader.name_to_id:
88-
raise ValueError(f"Message {msg} not found in log file")
89-
fmt = reader.formats[reader.name_to_id[msg]]
9094
out.setdefault(msg, []).extend(fmt.columns)
9195
return out
9296

@@ -154,7 +158,7 @@ def main():
154158
parser = argparse.ArgumentParser(
155159
description="Parse a log file to a DataFrame.",
156160
usage="python -m pymavlink.dfreader_pandas <log_file> [options]",
157-
epilog="Example usage: python -m pymavlink.dfreader_pandas log.bin --fields TECS EFI CTUN.E2T --frequency 10.0",
161+
epilog="Example usage: python -m pymavlink.dfreader_pandas log.bin --fields TECS EFI CTUN.E2T BAT[0].Volt --frequency 10.0 --cache_dir ./.tmp",
158162
)
159163
parser.add_argument("path", type=str, help="Path to the log file")
160164
parser.add_argument(

0 commit comments

Comments
 (0)