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

fixed linting issues #87

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
177 changes: 96 additions & 81 deletions imap_processing/codice/L0/decom_python_example.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
"""
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
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.
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 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.
1. Set the `bin_file_path` variable to the path containing packet data.
2. Replace `target_apid` with the desired APID to search for.
3. Define the `decomm_table` with the decommutation info 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.
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, parameters are extracted and printed.
- If no matching packet is found, a message indicating such is printed.
"""

GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
"""
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):
"""
Parameters
----------
file_path : str
Path to the binary file containing packet data.

Returns
--------
data : dict
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: "unsigned_int", "float", etc.
"""
with open(file_path, "rb") as file:
data = file.read()
return data


"""
Extracts a value from binary data by interpreting a specified range of bits.

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
----------
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
-------
int: The extracted value represented as an integer.

"""


def extract_bits(data, start_bit, length):
"""
Extracts a value from binary data by interpreting a specified range of bits.

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
----------
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
-------
int
The extracted value represented as an integer.
"""
byte_offset = start_bit // 8
bit_shift = start_bit % 8
mask = (1 << length) - 1
Expand All @@ -74,58 +77,70 @@ def extract_bits(data, start_bit, length):
return value & mask


"""
Finds the index of the first occurrence of a packet with a specific APID in binary data.

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
----------
bin_data (bytes): The binary data to search within.
target_apid (int): The target APID to search for.

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

Example:
binary_data = bytes([0x12, 0x34, 0x56, 0x12, 0x78, 0x90]) # Example binary data
target_apid = 0x1234 # Example target APID
packet_index = find_packet_with_apid(binary_data, target_apid)
"""


def find_packet_with_apid(bin_data, target_apid):
"""
Finds the index of the first occurrence of a packet with a specific
APID in binary data.

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
----------
bin_data : bytes
The binary data to search within.
target_apid : int
The target APID to search for.

Returns
-------
idx : int
The index of the first occurrence of the packet with the specified APID

Example:
binary_data = bytes ([0x12, 0x34, 0x56, 0x12, 0x78, 0x90])
target_apid = 0x1234
Example target APID
packet_index = find_packet_with_apid
(binary_data, target_apid)
"""
# Search for the target APID in the binary data
target_apid_bytes = target_apid.to_bytes(2, byteorder="big")
idx = bin_data.find(target_apid_bytes)

return idx


"""
Decommutes packet data using a provided decommutation table.
# Decommutation table variables may not all be the same
def decommute_packet(packet_data, decomm_table):
"""
Decommutes packet data using a provided decommutation table.

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.
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
----------
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:
Parameters
----------
packet_data : bytes
Binary data of the packet to decommute.
decomm_table : list
List of dictionaries, each containing decommutation information for a parameter.

GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
parameters : dict
The decommuted parameters. 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.

"""


# Decommutation table variables may not all be the same
def decommute_packet(packet_data, decomm_table):
- "dataType": Data type of the parameter: "unsigned_int", "float", etc.
"""
parameters = {}

for entry in decomm_table:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ def __init__(self):
{
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
"name": "SRC_SEQ_CTR",
"parameterTypeRef": "uint14",
"description": "CCSDS Packet Sequence Count (increments with each new packet)",
"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)",
"description": "CCSDS Packet Length "
"(number of bytes after Packet length minus 1)",
},
{
"name": "SHCOARSE",
Expand Down
13 changes: 7 additions & 6 deletions imap_processing/codice/decom.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
""" 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.

""" This is an example of how to use the 'space_packet_parser' module to parse with a
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
XTCE file specifically for the CODICE L0 data. This is a modified version of the
example found in the 'space_packet_parser' module documentation.
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
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.
# 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

Expand Down
39 changes: 20 additions & 19 deletions imap_processing/codice/generate_ccsds_header_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@
the CCSDS header parameters in the XTCE file.
"""

import xml.etree.ElementTree as ET
import xml.etree.ElementTree as Et
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved

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

# Create the root element

root = ET.Element("{http://www.omg.org/space}SpaceSystem")
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 = 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")
telemetry_metadata = Et.SubElement(root, "{http://www.omg.org/space}TelemetryMetaData")

# Create the ParameterTypeSet element
parameter_type_set = ET.SubElement(
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 = 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(
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")
unit_set = Et.SubElement(parameter_type, "{http://www.omg.org/space}UnitSet")

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

Expand Down Expand Up @@ -83,39 +83,40 @@
{
"name": "PKT_LEN",
"parameterTypeRef": "uint16",
"description": "CCSDS Packet Length (number of bytes after Packet length minus 1)",
"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 = 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 = Et.SubElement(parameter, "{http://www.omg.org/space}LongDescription")
description.text = parameter_data["description"]

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

# Create the SequenceContainer element
sequence_container = ET.SubElement(
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")
entry_list = Et.SubElement(sequence_container, "{http://www.omg.org/space}EntryList")
for parameter_data in ccsds_parameters:
parameter_ref_entry = ET.SubElement(
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)
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