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

initial hit l1a decom #164

Merged
merged 18 commits into from
Nov 2, 2023
Merged
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: '3.10'

- name: Install dependencies
run: |
Expand Down
18 changes: 18 additions & 0 deletions docs/source/reference/hit.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. _hit:

HIT (High-energy Ion Telescope)
===============================

.. currentmodule:: imap_processing.hit

This is the HIT Instrument module, which contains the code for processing
data from the HIT instrument.

The L0 code to decommutate the CCSDS packet data can be found below.

.. autosummary::
:toctree: generated/
:template: autosummary.rst
:recursive:

l0.hit_l1a_decom
1 change: 1 addition & 0 deletions docs/source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Instruments
:maxdepth: 1

glows
hit
swe


Expand Down
Empty file added imap_processing/hit/__init__.py
Empty file.
Empty file.
138 changes: 138 additions & 0 deletions imap_processing/hit/l0/hit_l1a_decom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import logging
from collections import defaultdict
from enum import IntEnum

import xarray as xr

from imap_processing import decom

logging.basicConfig(level=logging.INFO)


class HitAPID(IntEnum):
"""
HIT APID Mappings.

Attributes
----------
HIT_AUT : int
Autonomy
HIT_HSKP: int
Housekeeping
HIT_SCIENCE : int
Science
HIT_IALRT : int
I-ALiRT
HIT_MEMDUMP : int
Memory dump
"""

HIT_AUT = 1250 # Autonomy
HIT_HSKP = 1251 # Housekeeping
HIT_SCIENCE = 1252 # Science
HIT_IALRT = 1253 # I-ALiRT
HIT_MEMDUMP = 1255 # Memory dump


def decom_hit_packets(packet_file: str, xtce: str):
"""
Unpack and decode HIT packets using CCSDS format and XTCE packet definitions.

Parameters
----------
packet_file : str
Path to the CCSDS data packet file.
xtce : str
Path to the XTCE packet definition file.

Returns
-------
dict
A dictionary containing xr.Dataset for each APID. each dataset in the
dictionary will be converted to a CDF.
"""
# TODO: XTCE Files need to be combined
logging.info(f"Unpacking {packet_file} using xtce definitions in {xtce}")
packets = decom.decom_packets(packet_file, xtce)
logging.info(f"{packet_file} unpacked")
# print(packets[0])
# sort all the packets in the list by their spacecraft time
sorted_packets = sorted(packets, key=lambda x: x.data["SHCOARSE"].derived_value)

# Store data for each apid
# unpacked_data =
# {apid0: {var0: [item0, item1, ...], var1: [item0, item1, ...]}, ...}
unpacked_data = {}
for apid_name, apid in [(id.name, id.value) for id in HitAPID]:
# TODO: if science packet, do decompression
logging.info(f"Grouping packet values for {apid_name}:{apid}")
# get all the packets for this apid and groups them together in a
# dictionary
unpacked_data[apid_name] = group_apid_data(sorted_packets, apid)
logging.info(f"Finished grouping {apid_name}:{apid} packet values")

# create datasets
logging.info("Creating a dataset for HIT L1A data")
dataset_dict = create_datasets(unpacked_data)
logging.info("HIT L1A dataset created")
return dataset_dict


def create_datasets(data):
"""
Create a dataset for each APID in the data.

Parameters
----------
data : dict
A single dictionary containing data for all instances of an APID.

Returns
-------
dict
A dictionary containing xr.Dataset for each APID. each dataset in the
dictionary will be converted to a CDF.
"""
dataset_dict = defaultdict(list)
# create one dataset for each APID in the data
for apid, data_dict in data.items():
# if data for the APID exists, create the dataset
if data_dict != {}:
epoch = xr.DataArray(
name="Epoch", data=data_dict.pop("SHCOARSE"), dims=("Epoch")
)
dataset = xr.Dataset(data_vars={}, coords={"Epoch": epoch})
dataset_dict[apid] = dataset.assign(**data_dict)

return dataset_dict


def group_apid_data(packets, apid):
"""
Create a dictionary of lists containing all the data for the APID.

If packets contain N of the same APIDs, the data
for those N matching APIDs will be grouped together into
a dictionary of lists.

Parameters
----------
packets : list
List of all the unpacked data from decom.decom_packets()
apid : int
APID number for the data you want to group together

Returns
-------
dict
A dictionary where each field in the specified APID
is a key, and the value for that key is a list of
that fields values in all packets within the CCSDS file
"""
data_dict = defaultdict(list)
for packet in packets:
if packet.header["PKT_APID"].derived_value == apid:
for field in packet.data:
# put the value of the field in a dictionary
data_dict[field].append(packet.data[field].derived_value)
return data_dict
102 changes: 102 additions & 0 deletions imap_processing/hit/packet_definitions/P_HIT_AUT.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version='1.0' encoding='utf-8'?>
<xtce:SpaceSystem xmlns:xtce="http://www.omg.org/space/xtce" name="P_HIT_AUT">
<xtce:Header date="2023-11" version="1.0" author="IMAP SDC" />
<xtce:TelemetryMetaData>
<xtce:ParameterTypeSet>
<xtce:IntegerParameterType name="UINT1" signed="false">
<xtce:IntegerDataEncoding sizeInBits="1" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT2" signed="false">
<xtce:IntegerDataEncoding sizeInBits="2" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT3" signed="false">
<xtce:IntegerDataEncoding sizeInBits="3" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT6" signed="false">
<xtce:IntegerDataEncoding sizeInBits="6" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT11" signed="false">
<xtce:IntegerDataEncoding sizeInBits="11" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT14" signed="false">
<xtce:IntegerDataEncoding sizeInBits="14" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT16" signed="false">
<xtce:IntegerDataEncoding sizeInBits="16" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="UINT32" signed="false">
<xtce:IntegerDataEncoding sizeInBits="32" encoding="unsigned" />
</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="SEQ_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:Parameter name="SHCOARSE" parameterTypeRef="UINT32">
<xtce:ShortDescription>CCSDS Packet Sec Header</xtce:ShortDescription>
<xtce:LongDescription>Spacecraft tick</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SPARE1" parameterTypeRef="UINT6" />
<xtce:Parameter name="POWER_CYCLE" parameterTypeRef="UINT1">
<xtce:ShortDescription>Power cycle request (1=power cycle)</xtce:ShortDescription>
</xtce:Parameter>
<xtce:Parameter name="POWER_DOWN" parameterTypeRef="UINT1">
<xtce:ShortDescription>Power down request (1=power down)</xtce:ShortDescription>
</xtce:Parameter>
<xtce:Parameter name="HEATER_STAT" parameterTypeRef="UINT1">
<xtce:ShortDescription>Operational heater status (1=on)</xtce:ShortDescription>
</xtce:Parameter>
<xtce:Parameter name="HEATER_NUM" parameterTypeRef="UINT1">
<xtce:ShortDescription>Heater number (0=primary, 1=secondary)</xtce:ShortDescription>
</xtce:Parameter>
<xtce:Parameter name="SPARE2" parameterTypeRef="UINT6" />
</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="SEQ_FLGS" />
<xtce:ParameterRefEntry parameterRef="SRC_SEQ_CTR" />
<xtce:ParameterRefEntry parameterRef="PKT_LEN" />
</xtce:EntryList>
</xtce:SequenceContainer>
<xtce:SequenceContainer name="P_HIT_AUT">
<xtce:BaseContainer containerRef="CCSDSPacket">
<xtce:RestrictionCriteria>
<xtce:Comparison parameterRef="PKT_APID" value="1250" useCalibratedValue="false" />
</xtce:RestrictionCriteria>
</xtce:BaseContainer>
<xtce:EntryList>
<xtce:ParameterRefEntry parameterRef="SHCOARSE" />
<xtce:ParameterRefEntry parameterRef="SPARE1" />
<xtce:ParameterRefEntry parameterRef="POWER_CYCLE" />
<xtce:ParameterRefEntry parameterRef="POWER_DOWN" />
<xtce:ParameterRefEntry parameterRef="HEATER_STAT" />
<xtce:ParameterRefEntry parameterRef="HEATER_NUM" />
<xtce:ParameterRefEntry parameterRef="SPARE2" />
</xtce:EntryList>
</xtce:SequenceContainer>
</xtce:ContainerSet>
</xtce:TelemetryMetaData>
</xtce:SpaceSystem>
Loading