From 373f9f382260188d94366007853b673982ba1275 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Fri, 30 Aug 2024 11:56:11 -0600 Subject: [PATCH] ENH: Add Enumeration states to XTCE parser (#783) This adds the ability to include derived enumeration states from the "States" tab in the spreadsheets. --- .../tools/xtce-generator.rst | 30 +++ imap_processing/ccsds/excel_to_xtce.py | 33 ++- .../test_data/excel_to_xtce_test_file.xlsx | Bin 13983 -> 0 bytes .../tests/ccsds/test_data/expected_output.xml | 17 +- .../tests/ccsds/test_excel_to_xtce.py | 228 +++++++++++++++++- 5 files changed, 295 insertions(+), 13 deletions(-) delete mode 100644 imap_processing/tests/ccsds/test_data/excel_to_xtce_test_file.xlsx diff --git a/docs/source/code-documentation/tools/xtce-generator.rst b/docs/source/code-documentation/tools/xtce-generator.rst index cc16f5066..27e386222 100644 --- a/docs/source/code-documentation/tools/xtce-generator.rst +++ b/docs/source/code-documentation/tools/xtce-generator.rst @@ -124,6 +124,13 @@ the conversion details. - ANALOG - Apply an analog conversion - + * - MY_INSTRUMENT_HK + - VARIABLE_ENUMERATED + - 1 + - UINT + - STATE + - Apply an enumeration state + - * - MY_INSTRUMENT_HK - VARIABLE_LENGTH_BINARY_SCIENCE - 100 @@ -164,3 +171,26 @@ coefficients defined from ``c0`` to ``c7`` to define the order of the polynomial - - - + +States tab (optional) +~~~~~~~~~~~~~~~~~~~~~ + +Packet parsing can also apply enumeration/state conversions to the data being read in. +For example, to change from a raw unsigned integer value to a "VALID" / "INVALID" string. +The ``States`` tab is used to define these enumerations. + +.. list-table:: States + :header-rows: 1 + + * - packetName + - mnemonic + - value + - state + * - MY_INSTRUMENT_HK + - VARIABLE_ENUMERATED + - 0 + - INVALID + * - MY_INSTRUMENT_HK + - VARIABLE_ENUMERATED + - 1 + - VALID diff --git a/imap_processing/ccsds/excel_to_xtce.py b/imap_processing/ccsds/excel_to_xtce.py index d34aa6d3a..e1521dea1 100644 --- a/imap_processing/ccsds/excel_to_xtce.py +++ b/imap_processing/ccsds/excel_to_xtce.py @@ -251,7 +251,6 @@ def _add_parameter(self, row: pd.Series, total_packet_bits: int) -> None: # Combine the packet name and mnemonic to create a unique parameter name name = f"{row['packetName']}.{row['mnemonic']}" parameter.attrib["name"] = name - # UINT8, ... parameter.attrib["parameterTypeRef"] = name # Add descriptions if they exist @@ -329,6 +328,10 @@ def _add_parameter(self, row: pd.Series, total_packet_bits: int) -> None: # Go look up the conversion in the AnalogConversions tab # and add it to the encoding self._add_analog_conversion(row, encoding) + elif row["convertAs"] == "STATE": + # Go look up the states in the States tab + # and add them to the parameter type + self._add_state_conversion(row, parameter_type) def _add_analog_conversion(self, row: pd.Series, encoding: Et.Element) -> None: """ @@ -363,6 +366,34 @@ def _add_analog_conversion(self, row: pd.Series, encoding: Et.Element) -> None: term.attrib["coefficient"] = str(conversion[col]) term.attrib["exponent"] = str(i) + def _add_state_conversion(self, row: pd.Series, parameter_type: Et.Element) -> None: + """ + Add a state conversion to the parameter type. + + Changing from an IntegerParameterType to an EnumeratedParameterType. Adding + the list of state mappings to the parameter type. + + Parameters + ---------- + row : pandas.Row + Row to be added to the XTCE file, containing mnemonic, packetName. + parameter_type : Element + The parameter type element to add the conversion to. + """ + # It is an EnumeratedParameterType rather than an IntegerParameterType + parameter_type.tag = "xtce:EnumeratedParameterType" + enumeration_list = Et.SubElement(parameter_type, "xtce:EnumerationList") + # Lookup the enumeration states for this parameter from the States sheet + state_sheet = self.sheets["States"] + state_sheet = state_sheet.loc[ + (state_sheet["packetName"] == row["packetName"]) + & (state_sheet["mnemonic"] == row["mnemonic"]) + ] + 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"]) + def to_xml(self, output_xml_path: Path) -> None: """ Create and output an XTCE file from the Element Tree representation. diff --git a/imap_processing/tests/ccsds/test_data/excel_to_xtce_test_file.xlsx b/imap_processing/tests/ccsds/test_data/excel_to_xtce_test_file.xlsx deleted file mode 100644 index 29f0910da8a4e0049d60c73e20c45be08042a716..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 13983 zcmeHuWmFtnx_09l+}+&*1lQp19$bREy99?I0fJkw;7)K4?(S~E-J7qIGc)H*=FHFg z{kgYTt7_4^-e-01dS2W6DMc9wNDKfp02TlMkOCxy-wrx}0RWZI0024w7F&WG!`{?Mm&x76nj{wzoF*Fp4!ZyUj{n6oFq}AK+r@$+cANNs9M_;`l#Q&086ZYH zg!T0bV%hyI1HsY-4!qx5xJOa3G79)tYa`0c##@u^<*U$0ZEB~pqS65^&cjMbS;KtR zPw75U`jcKs1}Nh2Hm|m=;rc^@h9ZIP2LSXR9LY6wkhdM&SeVn`Y=<4=@)kaf;42Ss zZn?=3aA$gjPJIYg;K{Ke;z6$-6ci&;kOnRTi7F)d%uNQiiYw0tQ|#qS-a?Y?IFb7` z=2?YC@fg2SAKbt+WLf<&e)MEhdE)hD!kv;77mip@Ob*98=l=8cl!8^^f^8g@v`$hn zz#>vh4!wWe0|kxVU-6KkT#GSb!T$6trni4**mvml0?Nr5R`!u+;!WaFcpY&#vy)+UHgO|MCtt{m8gM$kU9hGB}xI^hi z531|T^~_C*gtP~ZYikT$$+w~$nSnKO@u_psDwHuM4LmsXe1Z@x{*(ai?{ZqJMt9|4 zGjCOn%R(v}IJ19z8cXw=ODZ^cB^bsndo-DX+3#fZexcmE-z?7Z~y=@XlC54nceIht&HsKtbV4lQZ-$>O)d-{eTx^7gxhIX0jX(c zNJ#dXdh%(r#T9B3Gzj&}H}xt-#rLO9gs}PDmOZXmP}KDmF=MR)w+|On^$bz(sVFK+ z<|(k}*)`GgXlnL7)n)>j;O)I+_>Ae-&o>lU z*j?MvWizbRE@Y>x^@e^x+*E!l7f#znV7!*XEKpMXO2w_g5HkX3x(;c@N3mhE z^Z=`Ygz0%RF(W+6X1HxFwe&WhWSdBU!^WBy$4Um}d!?eRs%n*kF75cnYQ@^jyHI|; z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J=bXu{0gom@MikHb1MJ$H*>sxvK+S3|Yd z9{Ui#9(9+Vde`YSr)am>_3TAC8QqRjJ~8)u6T+A)JqR;>J&7*MK3YR&()X&c?j0^=p9s*7|$)8ivvZHvYE)>RIA0@1%sg}L0MQAANz$|lbzYt zjBw#~FnM+!XgBcOl&Oq+=mPe2>tpO?6!148c@1tHCyNIIid|TP z!jGF+I6nfy_6o(}fJ z)^Bjq3c^#Ru)FsZ&iX9xXVqTOFPubx(Xo!gvI%9d_C^M8qYn2otx>OX@b4dN2t*mNQDk@1+!$ z>V{#(AG9~S3Zus`W@qAC4qbB?;mg)ni$z|AbeUqTgxFKCaBpjjsK!QPYA`mUFa_9Q&FqRU?!zGsy=Fa8jQEUPg z{t`@M65P?P;xFib)Yp)>n6u7UrmbkA>;Di#ydDo#ftCXksYRcW6vAh~jGxh2jz>Z5 ztJBbj<{ulX#{v!GRGzW)Q1T5Ia+M4F21*qU8(HOM=$`hs8~`F0~-X!cN6r#E#g&;$8PL4DKFKj~oR%ex;v z*T-ZJX!i(dz3%Rw`kTz4XZJ@H0S3+psu=$Mm6v}$;eTHL<^>o@Ozv2CjcGwFMvOr&fiUcFOdH - + @@ -59,6 +59,13 @@ + + + + + + + @@ -109,11 +116,14 @@ Float data + + State data + Mission elapsed time - - Variable 1 - long desc + + Variable 1 long description @@ -142,6 +152,7 @@ + diff --git a/imap_processing/tests/ccsds/test_excel_to_xtce.py b/imap_processing/tests/ccsds/test_excel_to_xtce.py index 062a1074c..bbd9b4c6c 100644 --- a/imap_processing/tests/ccsds/test_excel_to_xtce.py +++ b/imap_processing/tests/ccsds/test_excel_to_xtce.py @@ -4,6 +4,7 @@ from pathlib import Path from unittest import mock +import pandas as pd import pytest from imap_processing.ccsds import excel_to_xtce @@ -12,36 +13,245 @@ @pytest.fixture() -def excel_file(): - p = Path(__file__).parent / "test_data" / "excel_to_xtce_test_file.xlsx" - return p +def xtce_excel_file(tmp_path): + """Create an excel file for testing. + + Dataframes for each tab of the spreadsheet that then get written to an excel file. + """ + # Create a pandas DataFrame for global attributes + subsystem = { + "infoField": ["subsystem", "sheetReleaseDate", "sheetReleaseRev"], + "infoValue": ["Test Instrument", "2024-07-26 00:00:00", "v1.2"], + } + + packets = {"packetName": ["TEST_PACKET", "TEST_PACKET2"], "apIdHex": ["0x1", "0xF"]} + + test_packet1 = { + "packetName": ["TEST_PACKET"] * 15, + "mnemonic": [ + "PHVERNO", + "PHTYPE", + "PHSHF", + "PHAPID", + "PHGROUPF", + "PHSEQCNT", + "PHDLEN", + "SHCOARSE", + "VAR_UINT", + "VAR_INT", + "VAR_SINT", + "VAR_BYTE", + "VAR_FILL", + "VAR_FLOAT", + "VAR_STATE", + ], + "lengthInBits": [3, 1, 1, 11, 2, 14, 16, 32, 2, 4, 5, 10000, 3, 32, 1], + "dataType": [ + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "INT", + "SINT", + "BYTE", + "FILL", + "FLOAT", + "UINT", + ], + "convertAs": [ + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "ANALOG", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "STATE", + ], + "units": [ + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + "DN", + ], + "longDescription": [ + "CCSDS Packet Version Number", + "CCSDS Packet Type Indicator", + "CCSDS Packet Secondary Header Flag", + "CCSDS Packet Application Process ID", + "CCSDS Packet Grouping Flags", + "CCSDS Packet Sequence Count", + "CCSDS Packet Length", + "Mission elapsed time", + "Unsgned integer data with conversion", + "Integer data", + "Signed integer data", + "Binary data - variable length", + "Fill data", + "Float data", + "State data", + ], + } + + test_packet2 = { + "packetName": ["TEST_PACKET2"] * 9, + "mnemonic": [ + "PHVERNO", + "PHTYPE", + "PHSHF", + "PHAPID", + "PHGROUPF", + "PHSEQCNT", + "PHDLEN", + "SHCOARSE", + "VAR1", + ], + "lengthInBits": [3, 1, 1, 11, 2, 14, 16, 32, 2], + "dataType": [ + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + "UINT", + ], + "convertAs": [ + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + "NONE", + ], + "units": ["DN", "DN", "DN", "DN", "DN", "DN", "DN", "DN", "DN"], + "longDescription": [ + "CCSDS Packet Version Number", + "CCSDS Packet Type Indicator", + "CCSDS Packet Secondary Header Flag", + "CCSDS Packet Application Process ID", + "CCSDS Packet Grouping Flags", + "CCSDS Packet Sequence Count", + "CCSDS Packet Length", + "Mission elapsed time", + "Variable 1 long description", + ], + "shortDescription": [ + "", + "", + "", + "", + "", + "", + "", + "", + "Variable 1 short description", + ], + } + + analog_conversions = { + "packetName": ["TEST_PACKET"], + "mnemonic": ["VAR_UINT"], + "convertAs": ["UNSEGMENTED_POLY"], + "segNumber": [1], + "lowValue": [0], + "highValue": [100], + "c0": [1.5], + "c1": [2.5], + "c2": [0], + "c3": [0], + "c4": [0], + "c5": [0], + "c6": [0], + "c7": [0], + } + + states = { + "packetName": ["TEST_PACKET"] * 2, + "mnemonic": ["VAR_STATE"] * 2, + "value": [0, 1], + "state": ["OFF", "ON"], + } + + # Write the DataFrame to an excel file + excel_path = tmp_path / "excel_to_xtce_test_file.xlsx" + excel_file = pd.ExcelWriter(excel_path, engine="openpyxl") + + pd.DataFrame(subsystem).to_excel(excel_file, sheet_name="Subsystem", index=False) + pd.DataFrame(packets).to_excel(excel_file, sheet_name="Packets", index=False) + pd.DataFrame(test_packet1).to_excel( + excel_file, sheet_name="TEST_PACKET", index=False + ) + # Test P_ version of sheet name as well + pd.DataFrame(test_packet2).to_excel( + excel_file, sheet_name="P_TEST_PACKET2", index=False + ) + pd.DataFrame(analog_conversions).to_excel( + excel_file, sheet_name="AnalogConversions", index=False + ) + pd.DataFrame(states).to_excel(excel_file, sheet_name="States", index=False) + + # Write the file to disk + excel_file.close() + + return excel_path -def test_generated_xml(excel_file, tmp_path): +def test_generated_xml(xtce_excel_file): """Make sure we are producing the expected contents within the XML file. To produce a new expected output file the following command can be used. imap_xtce imap_processing/tests/ccsds/test_data/excel_to_xtce_test_file.xlsx --output imap_processing/tests/ccsds/test_data/expected_output.xml """ - generator = excel_to_xtce.XTCEGenerator(excel_file) - output_file = tmp_path / "output.xml" + generator = excel_to_xtce.XTCEGenerator(xtce_excel_file) + output_file = xtce_excel_file.parent / "output.xml" generator.to_xml(output_file) - expected_file = excel_file.parent / "expected_output.xml" + expected_file = Path(__file__).parent / "test_data/expected_output.xml" + # Uncomment this line if you want to re-create the expected output file + # generator.to_xml(expected_file) with open(output_file) as f, open(expected_file) as f_expected: assert f.read() == f_expected.read() # General test @mock.patch("imap_processing.ccsds.excel_to_xtce.XTCEGenerator") -def test_main_general(mock_input, excel_file): +def test_main_general(mock_input, xtce_excel_file): """Testing base main function.""" test_args = [ "test_script", "--output", "swe.xml", - f"{excel_file}", + f"{xtce_excel_file}", ] with mock.patch.object(sys, "argv", test_args): excel_to_xtce.main()