-
Notifications
You must be signed in to change notification settings - Fork 33
Add Lo star tracker packet processing #1864
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
Merged
greglucas
merged 6 commits into
IMAP-Science-Operations-Center:dev
from
greglucas:lo-star-tracker
Jul 2, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff4b774
ENH: IMAP Lo add star sensor packet processing
greglucas 48e12fa
MNT: Remove unused IMAP Lo star sensor dataclasses
greglucas c54076c
DOC: Remove lo dataclasses from documentation
greglucas d587fbb
MNT: Refactor IMAP Lo L1a to enable more datasets to be returned
greglucas ed1ee15
MNT: Separate l1a and l1b star sensor
greglucas be80f46
MNT: Move IMAP Lo star sensor l1b back into l1a
greglucas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,3 @@ The L0 code to decommutate the CCSDS packet data can be found below. | |
| :recursive: | ||
|
|
||
| l0.utils | ||
| l0.data_classes | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """Processing function for Lo star sensor data.""" | ||
|
|
||
| import logging | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from imap_processing.lo.l0.utils.bit_decompression import ( | ||
| DECOMPRESSION_TABLES, | ||
| Decompress, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
| def process_star_sensor(ds: xr.Dataset) -> xr.Dataset: | ||
| """ | ||
| Process Lo star sensor data. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ds : xr.Dataset | ||
| The packet dataset containing Lo star sensor data. | ||
|
|
||
| Returns | ||
| ------- | ||
| xr.Dataset | ||
| Processed dataset with a decompressed data field. | ||
| """ | ||
| # Make one long flat buffer | ||
| # This assumes that all data_compressed entries are of the same length | ||
| # but allows for only one frombuffer call | ||
| buffer = b"".join(ds["data_compressed"].values) | ||
| data = np.frombuffer(buffer, dtype=np.uint8).reshape(-1, 720) | ||
|
|
||
| # Decompress from 8 -> 12 bits using the decompression tables | ||
| decompression = DECOMPRESSION_TABLES[Decompress.DECOMPRESS8TO12].astype(np.uint16) | ||
| # Use the mean value column (2) | ||
| data = decompression[data, 2] | ||
|
|
||
| # There is already a variable called "count" in the dataset that | ||
| # came with the packet | ||
| ds["data_index"] = xr.DataArray(np.arange(720), dims="data_index") | ||
| ds["data"] = xr.DataArray(data, dims=("epoch", "data_index")) | ||
| # Remove the original compressed data field | ||
| ds = ds.drop_vars("data_compressed") | ||
| return ds | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,53 @@ | ||
| from collections import namedtuple | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from imap_processing.ccsds.ccsds_data import CcsdsData | ||
| from imap_processing.lo.l0.data_classes.star_sensor import StarSensor | ||
| from imap_processing import imap_module_directory | ||
| from imap_processing.lo.l0.lo_apid import LoAPID | ||
| from imap_processing.lo.l0.lo_star_sensor import process_star_sensor | ||
| from imap_processing.utils import packet_file_to_datasets | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def star_sensor(): | ||
| fake_data_field = namedtuple("fake_packet", ["raw_value", "derived_value"]) | ||
| star_sensor = StarSensor.__new__(StarSensor) | ||
| star_sensor.ccsds_header = CcsdsData( | ||
| { | ||
| "VERSION": fake_data_field(0, 0), | ||
| "TYPE": fake_data_field(0, 0), | ||
| "SEC_HDR_FLG": fake_data_field(0, 0), | ||
| "PKT_APID": fake_data_field(706, 706), | ||
| "SEQ_FLGS": fake_data_field(0, 0), | ||
| "SRC_SEQ_CTR": fake_data_field(0, 0), | ||
| "PKT_LEN": fake_data_field(0, 0), | ||
| } | ||
| def star_sensor_ds(): | ||
| xtce_file = imap_module_directory / "lo/packet_definitions/lo_xtce.xml" | ||
| dependency = ( | ||
| imap_module_directory / "tests/lo/test_pkts/imap_lo_l0_raw_20240803_v002.pkts" | ||
| ) | ||
| datasets_by_apid = packet_file_to_datasets( | ||
| packet_file=dependency.resolve(), | ||
| xtce_packet_definition=xtce_file.resolve(), | ||
| use_derived_value=False, | ||
| ) | ||
| return star_sensor | ||
| return datasets_by_apid[LoAPID.ILO_STAR] | ||
|
|
||
|
|
||
| def test_science_counts(star_sensor): | ||
| ## Arrange | ||
| star_sensor.DATA_COMPRESSED = "0" * 5760 | ||
| def test_star_sensor(star_sensor_ds): | ||
| validation_file = ( | ||
| imap_module_directory | ||
| / "tests/lo/validation_data" | ||
| / "Instrument_FM1_T104_R129_20240803_ILO_STAR_EU_trimmed.csv" | ||
| ) | ||
| validation_arr = np.loadtxt(validation_file, delimiter=",", skiprows=1, dtype=int) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is easier than pandas read_csv because there are so many columns, so just subset the fields ourselves instead. |
||
| validation_shcoarse = validation_arr[:, 0] | ||
| validation_count = validation_arr[:, 1] | ||
| # The first 720 | ||
| # validation_data_compressed = validation_arr[:, 2:722] | ||
| validation_data_decompressed = validation_arr[:, 722:-1] | ||
| validation_checksum = validation_arr[:, -1] | ||
|
|
||
| ## Act | ||
| star_sensor._decompress_data() | ||
| ds = process_star_sensor(star_sensor_ds) | ||
|
|
||
| ## Assert | ||
| assert star_sensor.DATA.shape == (720,) | ||
| # 45 times and 720 count values | ||
| assert ds["data"].shape == (45, 720) | ||
| assert ds["data"].dtype == np.uint16 | ||
|
|
||
| # We are only spot checking a few values from the validation file | ||
| # the first 3 and the final value. | ||
| small_ds = ds.isel(epoch=[0, 1, 2, -1]) | ||
| assert len(small_ds["epoch"]) == 4 | ||
| np.testing.assert_array_equal(small_ds["shcoarse"], validation_shcoarse) | ||
| np.testing.assert_array_equal(small_ds["count"], validation_count) | ||
| np.testing.assert_array_equal(small_ds["data"], validation_data_decompressed) | ||
| np.testing.assert_array_equal(small_ds["chksum"], validation_checksum) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll need to double check on the 720. Originally I thought it would always be that, but Colin told me at one point that it was 720 or less. I'll ask Lo at our tag-up on Monday
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Let me know if you need any clarification from me or want me to join that tag-up. It won't be hard to change this, but it is a bit unclear what it is stating currently. Section 8.3.5.3 has an explicit 720 listed in the output. Then the following section states:
Should these instead be
(N, COUNT)dimensionality. For CDF we'd need to make arrays that are shape(N, MAX_COUNT)and fill the unused values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sdhoyt any update on the 720 after your tag-up? Nick seemed to indicate he thought it would always be 720 for this packet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet. I'm going to ask in my tag-up tomorrow morning so I'll let you know once I hear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greglucas I confirmed that there will always be 720 data points in the packet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, this is ready for review then!