-
Notifications
You must be signed in to change notification settings - Fork 16
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
ENH/MNT: Add a common initial dataset creation routine #687
Merged
greglucas
merged 5 commits into
IMAP-Science-Operations-Center:dev
from
greglucas:create_dataset
Jul 22, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e6b531a
ENH/MNT: Add a common initial dataset creation routine
greglucas 385b4c0
MNT: Add option to not use derived value from XTCE
greglucas cbdd6ec
MNT: Change IMAP-Hi over to use new dataset accessor
greglucas dc18e11
MNT: Remove unused SWAPI epoch variable
greglucas 9d0c5c6
MNT: Revert to using raw values for SWAPI
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 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 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 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 |
---|---|---|
|
@@ -2,10 +2,8 @@ | |
|
||
import numpy as np | ||
import xarray as xr | ||
from space_packet_parser.parser import Packet | ||
|
||
from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes | ||
from imap_processing.cdf.utils import met_to_j2000ns | ||
|
||
# define the names of the 24 counter arrays | ||
# contained in the histogram packet | ||
|
@@ -35,30 +33,34 @@ | |
TOTAL_COUNTERS = ("total_a", "total_b", "total_c", "fee_de_sent", "fee_de_recd") | ||
|
||
|
||
def create_dataset(packets: list[Packet]) -> xr.Dataset: | ||
def create_dataset(input_ds: xr.Dataset) -> xr.Dataset: | ||
""" | ||
Create dataset for a number of Hi Histogram packets. | ||
|
||
Parameters | ||
---------- | ||
packets : list[space_packet_parser.ParsedPacket] | ||
Packet list. | ||
input_ds : xarray.Dataset | ||
Dataset of packets. | ||
|
||
Returns | ||
------- | ||
dataset : xarray.Dataset | ||
Dataset with all metadata field data in xr.DataArray. | ||
""" | ||
dataset = allocate_histogram_dataset(len(packets)) | ||
dataset = allocate_histogram_dataset(len(input_ds.epoch)) | ||
|
||
# unpack the packets data into the Dataset | ||
for i_epoch, packet in enumerate(packets): | ||
dataset.epoch.data[i_epoch] = met_to_j2000ns(packet.data["CCSDS_MET"].raw_value) | ||
dataset.ccsds_met[i_epoch] = packet.data["CCSDS_MET"].raw_value | ||
dataset.esa_stepping_num[i_epoch] = packet.data["ESA_STEP"].raw_value | ||
# TODO: Move into the allocate dataset function? | ||
dataset["epoch"].data[:] = input_ds["epoch"].data | ||
dataset["ccsds_met"].data = input_ds["ccsds_met"].data | ||
dataset["esa_stepping_num"].data = input_ds["esa_step"].data | ||
|
||
# unpack the packets data into the Dataset | ||
# (npackets, 24 * 90 * 12) | ||
# TODO: Look into avoiding the for-loops below | ||
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. Yes... Good recommendation! |
||
# It seems like we could try to reshape the arrays and do some numpy | ||
# broadcasting rather than for-loops directly here | ||
for i_epoch, counters_binary_data in enumerate(input_ds["counters"].data): | ||
# unpack 24 arrays of 90 12-bit unsigned integers | ||
counters_binary_data = packet.data["COUNTERS"].raw_value | ||
counter_ints = [ | ||
int(counters_binary_data[i * 12 : (i + 1) * 12], 2) for i in range(90 * 24) | ||
] | ||
|
This file contains 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,34 +1,26 @@ | ||
"""Unpack IMAP-Hi housekeeping data.""" | ||
|
||
import xarray as xr | ||
from space_packet_parser.parser import Packet | ||
|
||
from imap_processing.hi.hi_cdf_attrs import ( | ||
hi_hk_l1a_attrs, | ||
) | ||
from imap_processing.utils import create_dataset, update_epoch_to_datetime | ||
|
||
|
||
def process_housekeeping(packets: list[Packet]) -> xr.Dataset: | ||
def process_housekeeping(dataset: xr.Dataset) -> xr.Dataset: | ||
""" | ||
Create dataset for each metadata field. | ||
Parameters | ||
---------- | ||
packets : list[space_packet_parser.ParsedPacket] | ||
Packet list. | ||
dataset : xarray.Dataset | ||
Packet input dataset. | ||
Returns | ||
------- | ||
dataset : xarray.Dataset | ||
Dataset with all metadata field data in xr.DataArray. | ||
""" | ||
dataset = create_dataset( | ||
packets=packets, spacecraft_time_key="ccsds_met", skip_keys=["INSTR_SPECIFIC"] | ||
) | ||
# Update epoch to datetime | ||
dataset = update_epoch_to_datetime(dataset) | ||
|
||
# Add datalevel attrs | ||
dataset.attrs.update(hi_hk_l1a_attrs.output()) | ||
return dataset |
This file contains 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 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 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
Oops, something went wrong.
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.
These changes look fine for now. The
allocate_histogram_dataset
function was really just my way of avoiding having an intermediate data storage. I wanted to go directly from the packet into the xr.DataSet arrays. With your new function, this is no longer achieving that goal. I will write a ticket to look at this Hi code and address the TODOs.