spac_kit.parser¶
The parser module provides tools for parsing CCSDS binary files and converting them to structured data formats.
spac_kit.parser¶
Generic code to parse a downlink binary file encoded using CCSDS.
spac_kit.parser.parse_ccsds_downlink¶
CCSDS parser for binary file with multiple APIDs.
- exception spac_kit.parser.parse_ccsds_downlink.CCSDSParsingException¶
Bases:
ExceptionCCSDS packet parsing Exception.
- exception spac_kit.parser.parse_ccsds_downlink.CRCNotCalculatedError¶
Bases:
ExceptionCRC Calculation exception.
- class spac_kit.parser.parse_ccsds_downlink.CalculatedChecksum¶
Bases:
ConverterConverter which calculates a CRC checksum from a parsed packet and compare it with the one found in the packet.
TODO: make something better, by supporting any packets as input…
- CRC¶
alias of
Crc16Ibm3740
- JUMBO_CRC¶
alias of
Crc32Mpeg2
- JUMBO_TLM_PKT_LEN_BYTES = 4089¶
- __init__()¶
Initialization.
- classmethod calculate_crc(ccsds_version_number, ccsds_packet_type, ccsds_secondary_flag, ccsds_apid, ccsds_sequence_flag, ccsds_sequence_count, ccsds_packet_length, body)¶
Calculate one CRC from the parsed fields of one packet.
Parsed fields must be the CCSDS header and one body excluding the CRC at the end of the packet.
- convert(ccsds_version_number_array, ccsds_packet_type_array, ccsds_secondary_flag_array, ccsds_apid_array, ccsds_sequence_flag_array, ccsds_sequence_count_array, ccsds_packet_length_array, body_array)¶
Converter to add a calculated CRC to the parsed packets.
- Parameters:
ccsds_version_number_array – from the CCSDS header
ccsds_packet_type_array – from the CCSDS header
ccsds_secondary_flag_array – from the CCSDS header
ccsds_apid_array – from the CCSDS header
ccsds_sequence_flag_array – from the CCSDS header
ccsds_sequence_count_array – from the CCSDS header
ccsds_packet_length_array – from the CCSDS header
body_array – body of the packet, without the trailing CRC.
- Returns:
The array of calculated CRCs.
- spac_kit.parser.parse_ccsds_downlink.calculate_crc(f, crc_size_bytes=2)¶
Calculate a CRC for each packet so to compare it with the CRC sent at the end of the packets.
- spac_kit.parser.parse_ccsds_downlink.cast_to_list(d)¶
Casts any multidimensional arrays to lists.
- spac_kit.parser.parse_ccsds_downlink.distribute_packets(keyss, stream1)¶
Distribute binary stream into multiple binary stream with consistent sub-packet structures.
Used when single APID does not have consistent packet structure.
- spac_kit.parser.parse_ccsds_downlink.get_packet_definitions()¶
Select packet definitions which will be parsed in the first round or second round, as a refinement for some APIDs.
First round parsing: object instances of _BasePackets which have an apid but no sub_apid Second round parsing: object instances of _BasePackets which have an apid and a sub_apid
- spac_kit.parser.parse_ccsds_downlink.get_sub_packet_keys(parsed_apids, sub_apid: dict)¶
Identify sub-packet keys when single APId does not have consistent packet structures.
- spac_kit.parser.parse_ccsds_downlink.get_tab_name(apid, pkt_def, existing_names)¶
Proposes a tab name for each APID or sub-packet structure of an APID.
The tab name can be used as keys in the dictionary of DataFrames or as tab names in the Excel spreadsheet.
- Parameters:
apid – APID
pkt_def – current packet definition. Preferably, the packet definition has a “name” property which will be used to name the tab. If not available the name of the class implementing the packet structure definition is used.
existing_names – already used names, to avoid duplicates. A counter is added to duplicate names.
- Returns:
A unique tab name for the current APID and packet structure definition.
- spac_kit.parser.parse_ccsds_downlink.parse_ccsds_file(ccsds_file: str, do_calculate_crc: bool = False)¶
Parse a pure CCSDS binary file (only CCSDS packets).
spac_kit.parser.downlink_to_excel¶
Utility to convert a downlink CCSDS binary file to excel.
- spac_kit.parser.downlink_to_excel.add_tab_to_xlsx(dfs, writer, name='')¶
Add tab to excel writer from a dictionary, recursively.
Only use the name in the leaf of the dictionary tree.
- Parameters:
dfs – dictionary (of dictionary) of pandas dataframes or single pandas dataframe
writer – pandas.ExcelWriter
name – name of the tab to be used, optional
- spac_kit.parser.downlink_to_excel.export_ccsds_to_excel(ccsds_file, output_filename, do_calculate_crc)¶
Export a binary file of CCSDS packets into an Excel file.
- spac_kit.parser.downlink_to_excel.export_dfs_to_xlsx(dfs, filename1)¶
Export a dictionnary of pandas dataframes to an Excel file.
- spac_kit.parser.downlink_to_excel.get_parser()¶
Parser for the command line utility.
- spac_kit.parser.downlink_to_excel.main()¶
Command line interface to parse downlink binary file and export to Excel file.
spac_kit.parser.Packets¶
CCSDSpy packet specialized for configurable APID parsing.
- class spac_kit.parser.Packets.ParserSubAPIDPacket(fields: list[PacketField], name: str, apid: int, sub_apid: int)¶
Bases:
SimpleAPIDPacketPacket definition associated to a specific flavor of structure within a single APID.
- __init__(fields: list[PacketField], name: str, apid: int, sub_apid: int)¶
Constructor.
- class spac_kit.parser.Packets.PreParserAPIDPacket(fields: list[~ccsdspy.packet_fields.PacketField], name: str, apid: int, decision_field: str = None, decision_fun: ~typing.Callable = <function PreParserAPIDPacket.<lambda>>)¶
Bases:
SimpleAPIDPacketPacket definition used to pre-parse packets.
This is used when the structure vary for a single APID depending on one field in this packet.
- __init__(fields: list[~ccsdspy.packet_fields.PacketField], name: str, apid: int, decision_field: str = None, decision_fun: ~typing.Callable = <function PreParserAPIDPacket.<lambda>>)¶
Constructor.
spac_kit.parser.util¶
Utilities shared.
- spac_kit.parser.util.import_ccsds_packet_packages()¶
Import subpackages of ccsds.packets containing CCSDSpy definitions.
Stolen from https://packaging.python.org/en/latest/guides/ creating-and-discovering-plugins/#using-namespace-packages
- Returns:
‘packet’ (the packet object), ‘variable_name’, ‘module_path’
- Return type:
List of dictionaries with keys
spac_kit.parser.remove_non_ccsds_headers¶
Remove non CCSDS binary code from input streams.
- spac_kit.parser.remove_non_ccsds_headers.remove_bdsem(f)¶
Remove BDSEM headers.
- spac_kit.parser.remove_non_ccsds_headers.remove_bdsem_and_message_headers(f)¶
Removes extra headers provided by the BDSEM data generation.
- Parameters:
f – file handler
- Returns:
File handler
- spac_kit.parser.remove_non_ccsds_headers.remove_mise_and_headers(f)¶
Remove packet markers from Raw mode file.
- spac_kit.parser.remove_non_ccsds_headers.start_sequence(seq)¶
Returns True if seq is on the beginning of a marker between CCSDS packets.
- spac_kit.parser.remove_non_ccsds_headers.strip_non_ccsds_headers(file_handler, is_bdsem: bool = False, has_pkt_header: bool = False, has_json_header: bool = False)¶
Remove all cases of non CCSDS headers which can occur in Europa-Clipper SDS inputs.
Mostly used in test cases.
- Parameters:
file_handler – input binary file handler
is_bdsem – file coming from BDSEM, else RAW
has_pkt_header – whether the file has packet headers
has_json_header – whether the file has a JSON header
- Returns:
The file handler where the CCSDS packets start
spac_kit.parser.spac_ls¶
Command line utility to list available CCSDS packet packages.
- spac_kit.parser.spac_ls.format_packet_info(packet_info, long_format=False)¶
Format packet information into a row for display.
- Parameters:
packet_info – Either a dict with ‘packet’, ‘variable_name’, ‘module_path’ keys, or a packet object for backward compatibility
long_format – If True, include additional fields like packet type and field count
- spac_kit.parser.spac_ls.get_parser()¶
Parser for the command line utility.
- spac_kit.parser.spac_ls.list_packages(delimiter=None, long_format=False)¶
List all available CCSDS packet packages.
- Parameters:
delimiter – If specified, output as delimited format (e.g., ‘,’ for CSV, ‘ ‘ for TSV)
long_format – If True, display additional fields like packet type and field information
- spac_kit.parser.spac_ls.main()¶
Command line interface to list CCSDS packet packages.
spac_kit.parser.test_utils¶
Utilities to test the packet parsing.
- spac_kit.parser.test_utils.compare(local_dir: str, is_bdsem: bool = False, has_pkt_header: bool = False, has_json_header: bool = False, create_output: bool = False, create_spreadsheet: bool = False)¶
Run parsing and compare results with a reference.
- Parameters:
local_dir – local directory containing input file
is_bdsem – whether file is from BDSEM
has_pkt_header – whether file has packet headers
has_json_header – whether file has JSON header
create_output – whether to create output pickle file
create_spreadsheet – whether to create Excel spreadsheet
- spac_kit.parser.test_utils.recursive_compare(dfs, dfs_expected)¶
Compare embedded dictionary of dictionaries of pandas dataframes.
Compare the keys and the actual dataframes. None should be missing and all should match.
- Parameters:
dfs – dictionary of dictionaries of dataframes
dfs_expected – expected dictionary of dictionaries
- Returns:
True if the pandas dataframes are identical at the same location