Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT: Add useful error message when packet to datasets fails #763

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions imap_processing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ def test_packet_file_to_datasets(use_derived_value, expected_mode):
np.testing.assert_array_equal(data["mode"], [expected_mode] * len(data["mode"]))


def test_packet_file_to_datasets_flat_definition():
test_file = "tests/idex/imap_idex_l0_raw_20230725_v001.pkts"
packet_files = imap_module_directory / test_file
packet_definition = (
imap_module_directory / "idex/packet_definitions/idex_packet_definition.xml"
)
with pytest.raises(ValueError, match="Packet fields do not match"):
utils.packet_file_to_datasets(packet_files, packet_definition)


def test__create_minimum_dtype_array():
"""Test expected return types for minimum data types."""
result = utils._create_minimum_dtype_array([1, 2, 3], "uint8")
Expand Down
10 changes: 10 additions & 0 deletions imap_processing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ def packet_file_to_datasets(
data_dict: dict[int, dict] = dict()
# Also keep track of the datatype mapping for each field
datatype_mapping: dict[int, dict] = dict()
# Keep track of which variables (keys) are in the dataset
variable_mapping: dict[int, set] = dict()

# Set up the parser from the input packet definition
packet_definition = xtcedef.XtcePacketDefinition(xtce_packet_definition)
Expand All @@ -361,6 +363,14 @@ def packet_file_to_datasets(
# This is the first packet for this APID
data_dict[apid] = collections.defaultdict(list)
datatype_mapping[apid] = dict()
variable_mapping[apid] = packet.data.keys()
if variable_mapping[apid] != packet.data.keys():
raise ValueError(
f"Packet fields do not match for APID {apid}. This could be "
f"due to a conditional packet definition in the XTCE, while this "
f"function currently only supports flat packet definitions."
f"\nExpected: {variable_mapping[apid]},\ngot: {packet.data.keys()}"
)

# TODO: Do we want to give an option to remove the header content?
packet_content = packet.data | packet.header
Expand Down
Loading