Skip to content

Commit

Permalink
Add Hexadecimal state handling to excel_to_xtce (#1159)
Browse files Browse the repository at this point in the history
* added hex telem state value handling
  • Loading branch information
sdhoyt authored Nov 19, 2024
1 parent 9394095 commit f7847ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
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

0 comments on commit f7847ab

Please sign in to comment.