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

XTCE decom with CoDICE example #72

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions imap_processing/codice/CcsdsHeaderXtceGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CCSDSParameters:
def __init__(self):
self.parameters = [
{
"name": "VERSION",
"parameterTypeRef": "uint3",
"description": "CCSDS Packet Version Number (always 0)",
},
{
"name": "TYPE",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Type Indicator (0=telemetry)",
},
{
"name": "SEC_HDR_FLG",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Secondary Header Flag (always 1)",
},
{
"name": "PKT_APID",
"parameterTypeRef": "uint11",
"description": "CCSDS Packet Application Process ID",
},
{
"name": "SEG_FLGS",
"parameterTypeRef": "uint2",
"description": "CCSDS Packet Grouping Flags (3=not part of group)",
},
{
"name": "SRC_SEQ_CTR",
"parameterTypeRef": "uint14",
"description": "CCSDS Packet Sequence Count (increments with each new packet)",
},
{
"name": "PKT_LEN",
"parameterTypeRef": "uint16",
"description": "CCSDS Packet Length (number of bytes after Packet length minus 1)",
},
{
"name": "SHCOARSE",
"parameterTypeRef": "uint32",
"description": "CCSDS Packet Time Stamp (coarse time)",
},
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
]


# Other utility functions related to CCSDS parameters can also be added here
72 changes: 72 additions & 0 deletions imap_processing/codice/L0/ccsds-header.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version='1.0' encoding='utf-8'?>
<xtce:SpaceSystem xmlns:xtce="http://www.omg.org/space" name="CCSDS_Headers">
<xtce:Header date="2023" version="1.0" author="IMAP SDC" />
<xtce:TelemetryMetaData>
<xtce:ParameterTypeSet>
<xtce:IntegerParameterType name="uint1" signed="false">
<xtce:IntegerDataEncoding sizeInBits="1" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint2" signed="false">
<xtce:IntegerDataEncoding sizeInBits="2" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint3" signed="false">
<xtce:IntegerDataEncoding sizeInBits="3" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint11" signed="false">
<xtce:IntegerDataEncoding sizeInBits="11" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint14" signed="false">
<xtce:IntegerDataEncoding sizeInBits="14" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint16" signed="false">
<xtce:IntegerDataEncoding sizeInBits="16" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint32" signed="false">
<xtce:IntegerDataEncoding sizeInBits="32" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
</xtce:ParameterTypeSet>
<xtce:ParameterSet>
<xtce:Parameter name="VERSION" parameterTypeRef="uint3">
<xtce:LongDescription>CCSDS Packet Version Number (always 0)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="TYPE" parameterTypeRef="uint1">
<xtce:LongDescription>CCSDS Packet Type Indicator (0=telemetry)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SEC_HDR_FLG" parameterTypeRef="uint1">
<xtce:LongDescription>CCSDS Packet Secondary Header Flag (always 1)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="PKT_APID" parameterTypeRef="uint11">
<xtce:LongDescription>CCSDS Packet Application Process ID</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SEG_FLGS" parameterTypeRef="uint2">
<xtce:LongDescription>CCSDS Packet Grouping Flags (3=not part of group)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SRC_SEQ_CTR" parameterTypeRef="uint14">
<xtce:LongDescription>CCSDS Packet Sequence Count (increments with each new packet)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="PKT_LEN" parameterTypeRef="uint16">
<xtce:LongDescription>CCSDS Packet Length (number of bytes after Packet length minus 1)</xtce:LongDescription>
</xtce:Parameter>
</xtce:ParameterSet>
<xtce:ContainerSet>
<xtce:SequenceContainer name="CCSDSPacket">
<xtce:EntryList>
<xtce:ParameterRefEntry parameterRef="VERSION" />
<xtce:ParameterRefEntry parameterRef="TYPE" />
<xtce:ParameterRefEntry parameterRef="SEC_HDR_FLG" />
<xtce:ParameterRefEntry parameterRef="PKT_APID" />
<xtce:ParameterRefEntry parameterRef="SEG_FLGS" />
<xtce:ParameterRefEntry parameterRef="SRC_SEQ_CTR" />
<xtce:ParameterRefEntry parameterRef="PKT_LEN" />
</xtce:EntryList>
</xtce:SequenceContainer>
</xtce:ContainerSet>
</xtce:TelemetryMetaData>
</xtce:SpaceSystem>
73 changes: 45 additions & 28 deletions imap_processing/codice/L0/decom_python_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
"""
This script contains functions to extract and decommute parameters from a binary file containing packets.
This is a hard coded version of the decom.py file. It is used to test the decom.py file.
"""
Main script to extract and decommute parameters from a binary file containing packets.

This script reads a binary file containing packet data, searches for a packet with a specific Application Process Identifier (APID),
and then decommutes the packet's parameters using a provided decommutation table. The extracted parameter values are printed.

Usage:
1. Set the 'bin_file_path' variable to the path of the binary file containing packet data.
2. Replace 'target_apid' with the desired APID to search for.
3. Define the 'decomm_table' with the decommutation information for different parameters.
4. Run the script to extract and print decommuted parameter values.

Example:
Assuming 'read_binary_file' and other functions are defined:
- Given a binary file at 'bin_file_path' and a desired 'target_apid':
- If a packet with the 'target_apid' is found, its parameters are extracted and printed.
- If no matching packet is found, a message indicating such is printed.
"""

'''
Parameters
----------
bin_file_path : str
Path to the binary file containing packet data.
target_apid : int
APID of the packet to search for.
decomm_table : list
List of dictionaries, each containing decommutation information for a parameter.
Each dictionary should contain:
- "mnemonic": A unique identifier for the parameter.
- "sequence": An optional parameter sequence number.
- "startByte": Starting byte index in the packet.
- "startBitInByte": Starting bit index within the starting byte.
- "startBit": Overall starting bit index in the packet.
- "lengthInBits": Number of bits to extract for this parameter.
- "dataType": Data type of the parameter, e.g., "unsigned_int", "float", etc.
'''
def read_binary_file(file_path):
with open(file_path, "rb") as file:
data = file.read()
Expand All @@ -15,12 +46,14 @@ def read_binary_file(file_path):

This function is used to extract a value from a sequence of binary data by specifying the starting bit position and the number of bits to consider. The bits are interpreted as an unsigned integer value.

Parameters:
Parameters
----------
data (bytes): The binary data from which the value will be extracted.
start_bit (int): The index of the starting bit for extraction.
length (int): The number of bits to extract.

Returns:
Returns
-------
int: The extracted value represented as an integer.

"""
Expand All @@ -44,11 +77,13 @@ def extract_bits(data, start_bit, length):

This function searches through a sequence of binary data to find the index of the first packet that matches the specified Application Process Identifier (APID). The APID is a unique identifier used in packet-based data communication protocols.

Parameters:
Parameters
----------
bin_data (bytes): The binary data to search within.
target_apid (int): The target APID to search for.

Returns:
Returns
-------
int: The index of the first occurrence of the packet with the specified APID, or -1 if not found.

Example:
Expand All @@ -71,7 +106,8 @@ def find_packet_with_apid(bin_data, target_apid):

This function takes a packet's binary data and a decommutation table as input, and returns a dictionary of parameter values extracted from the packet according to the table.

Parameters:
Parameters
----------
packet_data (bytes): Binary data of the packet to decommute.
decomm_table (list): List of dictionaries, each containing decommutation information for a parameter.
Each dictionary should contain:
Expand Down Expand Up @@ -105,27 +141,8 @@ def decommute_packet(packet_data, decomm_table):
return parameters


"""
Main script to extract and decommute parameters from a binary file containing packets.

This script reads a binary file containing packet data, searches for a packet with a specific Application Process Identifier (APID),
and then decommutes the packet's parameters using a provided decommutation table. The extracted parameter values are printed.

Usage:
1. Set the 'bin_file_path' variable to the path of the binary file containing packet data.
2. Replace 'target_apid' with the desired APID to search for.
3. Define the 'decomm_table' with the decommutation information for different parameters.
4. Run the script to extract and print decommuted parameter values.

Example:
Assuming 'read_binary_file' and other functions are defined:
- Given a binary file at 'bin_file_path' and a desired 'target_apid':
- If a packet with the 'target_apid' is found, its parameters are extracted and printed.
- If no matching packet is found, a message indicating such is printed.
"""

if __name__ == "__main__":
bin_file_path = "/Users/gamo6782/Desktop/RAW.bin"
bin_file_path = "../RAW.bin"
target_apid = 0x460 # Replace with the APID of the desired packet
decomm_table = [
{
Expand Down
4 changes: 2 additions & 2 deletions imap_processing/codice/L0/p_cod_aut_test.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
<xtce:SpaceSystem xmlns:xtce="http://www.omg.org/space/xtce" nsmap="{'xtce': 'http://www.omg.org/space'}" name="P_COD_AUT">
<xtce:Header date="2023-08-21" version="1.0" author="Gabriel Moraga" />
<xtce:SpaceSystem xmlns:xtce="http://www.omg.org/space" nsmap="{'xtce': 'http://www.omg.org/space/xtce'}" name="packet_name">
<xtce:Header date="2023-08-21" version="1.0" author="IMAP SDC" />
<xtce:TelemetryMetaData>
<xtce:ParameterTypeSet>
<xtce:IntegerParameterType name="uint0" signed="false">
Expand Down
11 changes: 9 additions & 2 deletions imap_processing/codice/decom.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
""" This is an example of how to use the space_packet_parser module to parse with a XTCE file.
""" This is an example of how to use the 'space_packet_parser' module to parse with a XTCE file specifically for the
CODICE L0 data. This is a modified version of the example found in the 'space_packet_parser' module documentation.
This is the start of CODICE L0 data processing.

"""

from pathlib import Path

from space_packet_parser import parser, xtcedef

# Define the APID. This is the APID for the CODICE L0 data that is in the 'RAW.bin' file.
# Data bins like 'RAW.bin' will encompass multiple APIDs. This is why we need to specify the APID.
# The APID should be in the packet definition file given by instrument team.
apid = 0x460
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved

# Define paths
packet_file = Path("RAW.bin")
xtce_document = Path("L0/p_cod_aut_test.xml")

packet_definition = xtcedef.XtcePacketDefinition(xtce_document)
my_parser = parser.PacketParser(packet_definition, 0x460)
my_parser = parser.PacketParser(packet_definition, apid)

with packet_file.open("rb") as binary_data:
packet_generator = my_parser.generator(binary_data)
Expand Down
121 changes: 121 additions & 0 deletions imap_processing/codice/generate_ccsds_header_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
""" All L0 data is written with a CCSDS header. This script creates the CCSDS header
parameters and writes them to an XML file. The XML file is then used to create
the CCSDS header parameters in the XTCE file.
"""

import xml.etree.ElementTree as ET

ET.register_namespace("xtce", "http://www.omg.org/space")

# Create the root element

root = ET.Element("{http://www.omg.org/space}SpaceSystem")
root.attrib["name"] = "CCSDS_Headers"

# Create the Header element and its attributes
header = ET.SubElement(root, "{http://www.omg.org/space}Header")
header.attrib["date"] = "2023"
header.attrib["version"] = "1.0"
header.attrib["author"] = "IMAP SDC"

# Create the TelemetryMetaData element
telemetry_metadata = ET.SubElement(root, "{http://www.omg.org/space}TelemetryMetaData")

# Create the ParameterTypeSet element
parameter_type_set = ET.SubElement(
telemetry_metadata, "{http://www.omg.org/space}ParameterTypeSet"
)

# Create integer parameter types
integer_sizes = [1, 2, 3, 11, 14, 16, 32]
for size in integer_sizes:
parameter_type = ET.SubElement(
parameter_type_set, "{http://www.omg.org/space}IntegerParameterType"
)
parameter_type.attrib["name"] = f"uint{size}"
parameter_type.attrib["signed"] = "false"

encoding = ET.SubElement(
parameter_type, "{http://www.omg.org/space}IntegerDataEncoding"
)
encoding.attrib["sizeInBits"] = str(size)
encoding.attrib["encoding"] = "unsigned"

unit_set = ET.SubElement(parameter_type, "{http://www.omg.org/space}UnitSet")

# Create the ParameterSet element
parameter_set = ET.SubElement(
telemetry_metadata, "{http://www.omg.org/space}ParameterSet"
)

# Create CCSDS Header parameters
ccsds_parameters = [
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
{
"name": "VERSION",
"parameterTypeRef": "uint3",
"description": "CCSDS Packet Version Number (always 0)",
},
{
"name": "TYPE",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Type Indicator (0=telemetry)",
},
{
"name": "SEC_HDR_FLG",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Secondary Header Flag (always 1)",
},
{
"name": "PKT_APID",
"parameterTypeRef": "uint11",
"description": "CCSDS Packet Application Process ID",
},
{
"name": "SEG_FLGS",
"parameterTypeRef": "uint2",
"description": "CCSDS Packet Grouping Flags (3=not part of group)",
},
{
"name": "SRC_SEQ_CTR",
"parameterTypeRef": "uint14",
"description": "CCSDS Packet Sequence Count (increments with each new packet)",
},
{
"name": "PKT_LEN",
"parameterTypeRef": "uint16",
"description": "CCSDS Packet Length (number of bytes after Packet length minus 1)",
},
]

for parameter_data in ccsds_parameters:
parameter = ET.SubElement(parameter_set, "{http://www.omg.org/space}Parameter")
parameter.attrib["name"] = parameter_data["name"]
parameter.attrib["parameterTypeRef"] = parameter_data["parameterTypeRef"]

description = ET.SubElement(parameter, "{http://www.omg.org/space}LongDescription")
description.text = parameter_data["description"]

# Create the ContainerSet element
container_set = ET.SubElement(
telemetry_metadata, "{http://www.omg.org/space}ContainerSet"
)

# Create the SequenceContainer element
sequence_container = ET.SubElement(
container_set, "{http://www.omg.org/space}SequenceContainer"
)
sequence_container.attrib["name"] = "CCSDSPacket"

# Create the EntryList element and add ParameterRefEntry elements
entry_list = ET.SubElement(sequence_container, "{http://www.omg.org/space}EntryList")
for parameter_data in ccsds_parameters:
parameter_ref_entry = ET.SubElement(
entry_list, "{http://www.omg.org/space}ParameterRefEntry"
)
parameter_ref_entry.attrib["parameterRef"] = parameter_data["name"]

# Create the XML tree
tree = ET.ElementTree(root)
ET.indent(tree, space="\t", level=0)
# Save the XML document to a file
tree.write("L0/ccsds-header.xml", encoding="utf-8", xml_declaration=True)
Loading