-
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
Changes from 2 commits
ff4b774
48e12fa
c54076c
d587fbb
ed1ee15
be80f46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """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) | ||
|
|
||
| # Now 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_count"] = xr.DataArray(np.arange(720), dims="data_count") | ||
| # Create a new DataArray with dimensions (epoch, count) | ||
| ds["data"] = xr.DataArray(data, dims=("epoch", "data_count")) | ||
| # Remove the original compressed data field | ||
| ds = ds.drop_vars("data_compressed") | ||
| return ds | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| parse_events, | ||
| parse_histogram, | ||
| ) | ||
| from imap_processing.lo.l0.lo_star_sensor import process_star_sensor | ||
| from imap_processing.utils import convert_to_binary_string, packet_file_to_datasets | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -100,10 +101,24 @@ def lo_l1a(dependency: Path) -> list[xr.Dataset]: | |
| datasets_by_apid[LoAPID.ILO_SCI_DE] = add_dataset_attrs( | ||
| datasets_by_apid[LoAPID.ILO_SCI_DE], attr_mgr, logical_source | ||
| ) | ||
| if LoAPID.ILO_STAR in datasets_by_apid: | ||
| logger.info( | ||
| f"\nProcessing {LoAPID(LoAPID.ILO_STAR).name} " | ||
| f"packet (APID: {LoAPID.ILO_STAR.value})" | ||
| ) | ||
| datasets_by_apid[LoAPID.ILO_STAR] = process_star_sensor( | ||
| datasets_by_apid[LoAPID.ILO_STAR] | ||
| ) | ||
|
|
||
| good_apids = [LoAPID.ILO_SPIN, LoAPID.ILO_SCI_CNT, LoAPID.ILO_SCI_DE] | ||
| logger.info(f"\nReturning datasets: {[LoAPID(apid) for apid in good_apids]}") | ||
| return [datasets_by_apid[good_apid] for good_apid in good_apids] | ||
| good_apids = [ | ||
| LoAPID.ILO_SPIN, | ||
| LoAPID.ILO_SCI_CNT, | ||
| LoAPID.ILO_SCI_DE, | ||
| LoAPID.ILO_STAR, | ||
| ] | ||
| apids_with_data = [apid for apid in good_apids if apid in datasets_by_apid] | ||
|
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. I reorganized this a bit because I think it would have failed if you had a packet file without some apids. We only want to look at the apids that were included in both of your lists, good and present. |
||
| logger.info(f"\nReturning datasets: {[LoAPID(apid) for apid in apids_with_data]}") | ||
| return [datasets_by_apid[apid] for apid in apids_with_data] | ||
|
|
||
|
|
||
| def add_dataset_attrs( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,50 @@ | ||
| 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_science_counts(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] | ||
| validation_data = validation_arr[:, 2:-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) | ||
|
|
||
| # We are only spot checking a few values from the validation file | ||
| small_ds = ds.where(ds["shcoarse"].isin(validation_shcoarse), drop=True) | ||
| 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) | ||
| np.testing.assert_array_equal(small_ds["chksum"], validation_checksum) | ||
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!