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

Add Hexadecimal state handling to excel_to_xtce #1159

Merged
merged 4 commits into from
Nov 19, 2024
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
36 changes: 34 additions & 2 deletions imap_processing/ccsds/excel_to_xtce.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,40 @@ def _add_state_conversion(self, row: pd.Series, parameter_type: Et.Element) -> N
]
for _, state_row in state_sheet.iterrows():
enumeration = Et.SubElement(enumeration_list, "xtce:Enumeration")
enumeration.attrib["value"] = str(state_row["value"])
enumeration.attrib["label"] = str(state_row["state"])
valid_state = self._ensure_state_value_is_int(state_row)
enumeration.attrib["value"] = str(valid_state["value"])
enumeration.attrib["label"] = str(valid_state["state"])

def _ensure_state_value_is_int(self, state: dict) -> dict:
"""
Ensure the telemetry state value is an integer.

Some telemetry state values are documented as a hex string,
which space packet parser cannot handle. If the value of a
state is a hex string rather than an int, convert it to an integer.
If the value is neither a hex string or an integer, raise an error.

Parameters
----------
state : dict
Dictionary with telemetry state and value.

Returns
-------
dict
The dictionary for the state.
"""
value = state["value"]
# return if already an int
if isinstance(value, int):
return state
# convert hex string to int
elif isinstance(value, str) and value.startswith("0x"):
state["value"] = int(value, 16)
return state
# raise error if value is neither a hex string or integer
else:
raise ValueError(f"Invalid value of {value} for state {state['state']}")

def to_xml(self, output_xml_path: Path) -> None:
"""
Expand Down
1 change: 1 addition & 0 deletions imap_processing/tests/ccsds/test_data/expected_output.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<xtce:EnumerationList>
<xtce:Enumeration value="0" label="OFF" />
<xtce:Enumeration value="1" label="ON" />
<xtce:Enumeration value="2" label="NONE" />
</xtce:EnumerationList>
</xtce:EnumeratedParameterType>
<xtce:IntegerParameterType name="TEST_PACKET2.SHCOARSE" signed="false">
Expand Down
8 changes: 4 additions & 4 deletions imap_processing/tests/ccsds/test_excel_to_xtce.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ def xtce_excel_file(tmp_path):
}

states = {
"packetName": ["TEST_PACKET"] * 2,
"mnemonic": ["VAR_STATE"] * 2,
"value": [0, 1],
"state": ["OFF", "ON"],
"packetName": ["TEST_PACKET"] * 3,
"mnemonic": ["VAR_STATE"] * 3,
"value": [0, 1, "0x2"],
"state": ["OFF", "ON", "NONE"],
}

# Write the DataFrame to an excel file
Expand Down
Loading