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 3 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
2 changes: 1 addition & 1 deletion imap_processing/tests/ccsds/test_excel_to_xtce.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def xtce_excel_file(tmp_path):
states = {
"packetName": ["TEST_PACKET"] * 2,
"mnemonic": ["VAR_STATE"] * 2,
"value": [0, 1],
"value": [0, "0x1"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add this hex test as another one without deleting that one? I think we need to test that also

"state": ["OFF", "ON"],
}

Expand Down
Loading