diff --git a/imap_processing/ccsds/excel_to_xtce.py b/imap_processing/ccsds/excel_to_xtce.py index e1521dea1..8a7e5b287 100644 --- a/imap_processing/ccsds/excel_to_xtce.py +++ b/imap_processing/ccsds/excel_to_xtce.py @@ -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: """ diff --git a/imap_processing/tests/ccsds/test_data/expected_output.xml b/imap_processing/tests/ccsds/test_data/expected_output.xml index ea6d378cf..51b96d4e8 100644 --- a/imap_processing/tests/ccsds/test_data/expected_output.xml +++ b/imap_processing/tests/ccsds/test_data/expected_output.xml @@ -64,6 +64,7 @@ + diff --git a/imap_processing/tests/ccsds/test_excel_to_xtce.py b/imap_processing/tests/ccsds/test_excel_to_xtce.py index bbd9b4c6c..6134358ba 100644 --- a/imap_processing/tests/ccsds/test_excel_to_xtce.py +++ b/imap_processing/tests/ccsds/test_excel_to_xtce.py @@ -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