Skip to content

Commit

Permalink
Merge pull request IMAP-Science-Operations-Center#530 from bourque/co…
Browse files Browse the repository at this point in the history
…dice-l1a-lo-nsw-species-counts

CoDICE L1a lo-nsw-species-counts
  • Loading branch information
bourque authored Apr 23, 2024
2 parents 64283ab + 9b7621d commit ada98e9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 27 deletions.
13 changes: 12 additions & 1 deletion imap_processing/codice/cdf_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@
l1a_lo_sw_species_attrs = GlobalDataLevelAttrs(
data_type="L1A_SCI->Level-1A Science Data",
logical_source="imap_codice_l1a_lo-sw-species-counts",
logical_source_desc="IMAP Mission CoDICE Instrument Level-1A Lo Sunward Species Data", # noqa
logical_source_desc=(
"IMAP Mission CoDICE Instrument Level-1A Lo Sunward Species Data"
),
instrument_base=codice_base,
)

l1a_lo_nsw_species_attrs = GlobalDataLevelAttrs(
data_type="L1A_SCI->Level-1A Science Data",
logical_source="imap_codice_l1a_lo-nsw-species-counts",
logical_source_desc=(
"IMAP Mission CoDICE Instrument Level-1A Lo Non-sunward Species Data"
),
instrument_base=codice_base,
)

Expand Down
1 change: 1 addition & 0 deletions imap_processing/codice/codice_l0.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
PACKET_TO_XTCE_MAPPING = {
"raw_ccsds_20230822_122700Z_idle.bin": "P_COD_NHK.xml",
"lo_fsw_view_5_ccsds.bin": "P_COD_LO_SW_SPECIES_COUNTS.xml",
"lo_fsw_view_6_ccsds.bin": "P_COD_LO_NSW_SPECIES_COUNTS.xml",
}


Expand Down
57 changes: 35 additions & 22 deletions imap_processing/codice/codice_l1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ESA_SWEEP_TABLE_ID_LOOKUP,
LO_COLLAPSE_TABLE_ID_LOOKUP,
LO_COMPRESSION_ID_LOOKUP,
LO_NSW_SPECIES_NAMES,
LO_STEPPING_TABLE_ID_LOOKUP,
LO_SW_SPECIES_NAMES,
)
Expand Down Expand Up @@ -104,22 +105,24 @@ def create_science_dataset(self, packets: list) -> xr.Dataset:
)

energy_steps = xr.DataArray(
np.arange(128),
np.arange(self.num_energy_steps),
name="energy",
dims=["energy"],
attrs=cdf_attrs.energy_attrs.output(),
)

dataset = xr.Dataset(
coords={"epoch": epoch, "energy": energy_steps},
attrs=cdf_attrs.l1a_lo_sw_species_attrs.output(),
attrs=self.cdf_attrs.output(),
)

# Create a data variable for each species
for species_data, name in zip(self.data, LO_SW_SPECIES_NAMES):
for species_data, name in zip(self.data, self.species_names):
varname, fieldname = name
species_data_arr = [int(item) for item in species_data]
species_data_arr = np.array(species_data_arr).reshape(-1, 128)
species_data_arr = np.array(species_data_arr).reshape(
-1, self.num_energy_steps
)

dataset[varname] = xr.DataArray(
species_data_arr,
Expand Down Expand Up @@ -197,11 +200,24 @@ def get_esa_sweep_values(self):
sweep_table = sweep_data[sweep_data["table_idx"] == sweep_table_id]
self.esa_sweep_values = sweep_table["esa_v"].values

def get_lo_data_products(self):
"""Retrieve the lo data products table."""
# TODO: implement this
def get_lo_data_products(self, apid: int):
"""Retrieve the lo data products table.
pass
Parameters
----------
apid : int
The APID of interest.
"""
if apid == CODICEAPID.COD_LO_SW_SPECIES_COUNTS:
self.num_species = 16
self.num_energy_steps = 128
self.species_names = LO_SW_SPECIES_NAMES
self.cdf_attrs = cdf_attrs.l1a_lo_sw_species_attrs
elif apid == CODICEAPID.COD_LO_NSW_SPECIES_COUNTS:
self.num_species = 8
self.num_energy_steps = 112
self.species_names = LO_NSW_SPECIES_NAMES
self.cdf_attrs = cdf_attrs.l1a_lo_nsw_species_attrs

def unpack_science_data(self, packets: list):
"""Unpack the science data from the packet.
Expand All @@ -216,20 +232,16 @@ def unpack_science_data(self, packets: list):
packet : list[space_packet_parser.parser.Packet]
List of packets for the APID of interest
TODO: Make this method more generalized for other APIDs
TODO: Check to see if we expect to have multiple packets?
"""
print("Unpacking science data")

self.compression_algorithm = LO_COMPRESSION_ID_LOOKUP[self.view_id]
self.collapse_table_id = LO_COLLAPSE_TABLE_ID_LOOKUP[self.view_id]

science_values = packets[0].data["DATA"].raw_value

# Divide up the data by the number of species
num_bytes = len(science_values)
num_species = 16
chunk_size = len(science_values) // num_species
chunk_size = len(science_values) // self.num_species
self.data = [
science_values[i : i + chunk_size] for i in range(0, num_bytes, chunk_size)
]
Expand Down Expand Up @@ -285,19 +297,20 @@ def process_codice_l1a(packets) -> str:
The path to the CDF file that was created
"""
# Group data by APID and sort by time
print("Grouping the data by APID")
grouped_data = group_by_apid(packets)

for apid in grouped_data.keys():
print(f"\nProcessing {CODICEAPID(apid).name} packet")

if apid == CODICEAPID.COD_NHK:
print("Processing COD_NHK packet")
packets = grouped_data[apid]
sorted_packets = sort_by_time(packets, "SHCOARSE")
dataset = create_hskp_dataset(packets=sorted_packets)

elif apid == CODICEAPID.COD_LO_SW_SPECIES_COUNTS:
print("Processing COD_LO_SW_SPECIES_COUNTS packet")

elif apid in [
CODICEAPID.COD_LO_SW_SPECIES_COUNTS,
CODICEAPID.COD_LO_NSW_SPECIES_COUNTS,
]:
packets = sort_by_time(grouped_data[apid], "SHCOARSE")

# Get the four "main" parameters for processing
Expand All @@ -307,19 +320,19 @@ def process_codice_l1a(packets) -> str:
pipeline = CoDICEL1aPipeline(table_id, plan_id, plan_step, view_id)
pipeline.get_esa_sweep_values()
pipeline.get_acquisition_times()
pipeline.get_lo_data_products()
pipeline.get_lo_data_products(apid)
pipeline.unpack_science_data(packets)
dataset = pipeline.create_science_dataset(packets)

elif apid == CODICEAPID.COD_LO_PHA:
print(f"{apid} is currently not supported")
continue

elif apid == CODICEAPID.COD_LO_PRIORITY_COUNTS:
elif apid == CODICEAPID.COD_LO_SW_PRIORITY_COUNTS:
print(f"{apid} is currently not supported")
continue

elif apid == CODICEAPID.COD_LO_NSW_SPECIES_COUNTS:
elif apid == CODICEAPID.COD_LO_NSW_PRIORITY_COUNTS:
print(f"{apid} is currently not supported")
continue

Expand All @@ -346,5 +359,5 @@ def process_codice_l1a(packets) -> str:
# Write dataset to CDF
print(f"\nFinal data product:\n{dataset}\n")
cdf_filename = write_cdf(dataset)
print(f"Created CDF file: {cdf_filename}")
print(f"\tCreated CDF file: {cdf_filename}")
return cdf_filename
12 changes: 12 additions & 0 deletions imap_processing/codice/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
("cnoplus", "SW - CNO+ (PUI)"),
]

# CDF-friendly FIELDNAMES and VARNAMES for lo-nsw-species-counts data product
LO_NSW_SPECIES_NAMES = [
("hplus", "NSW - H+"),
("heplusplus", "NSW - He++"),
("c", "NSW - C"),
("o", "NSW - O"),
("ne-si-mg", "NSW - Ne_Si_Mg"),
("fe", "NSW - Fe"),
("heplus", "NSW - He+"),
("cnoplus", "NSW - CNO+"),
]

# Compression ID lookup table for Lo data products
# The key is the view_id and the value is the ID for the compression algorithm
# (see utils.CoDICECompression to see how the values correspond)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@
<xtce:IntegerParameterType name="UINT32" signed="false">
<xtce:IntegerDataEncoding sizeInBits="32" encoding="unsigned" />
</xtce:IntegerParameterType>
<xtce:BinaryParameterType name="BYTE37748736">
<xtce:BinaryParameterType name="LO_NSW_SPECIES_COUNTS_BITS">
<xtce:UnitSet />
<xtce:BinaryDataEncoding bitOrder="mostSignificantBitFirst">
<xtce:SizeInBits>
<xtce:FixedValue>37748736</xtce:FixedValue>
<xtce:DynamicValue>
<!--
Data is of variable length.
Slope is always 8 (8 bits per byte) and the intercept
is the total number of fixed length bits in the packet)
This currently works with an intercept of -216, but
this was determined empirically with test data. This
number should be revisited once an updated packet
definition is acquired.
-->
<xtce:ParameterInstanceRef parameterRef="PKT_LEN"/>
<xtce:LinearAdjustment intercept="-216" slope="8"/>
</xtce:DynamicValue>
</xtce:SizeInBits>
</xtce:BinaryDataEncoding>
</xtce:BinaryParameterType>
Expand Down Expand Up @@ -134,7 +148,7 @@
<xtce:ShortDescription>Number of bytes in the Data array</xtce:ShortDescription>
<xtce:LongDescription>Number of bytes in the Data array. If compressed, this value represents the length of the compressed data.</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="DATA" parameterTypeRef="BYTE37748736">
<xtce:Parameter name="DATA" parameterTypeRef="LO_NSW_SPECIES_COUNTS_BITS">
<xtce:ShortDescription>Data Array</xtce:ShortDescription>
<xtce:LongDescription>Counter Data

Expand Down
7 changes: 6 additions & 1 deletion imap_processing/codice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ class CODICEAPID(IntEnum):
"""

COD_AUT = 1120
COD_BOOT_HK = 1121
COD_BOOT_MEMDMP = 1122
COD_COUNTS_COMMON = 1135
COD_NHK = 1136
COD_EVTMSG = 1137
COD_MEMDMP = 1138
COD_SHK = 1139
COD_RTS = 1141
COD_DIAG_CDHFPGA = 1144
COD_DIAG_SNSR_HV = 1145
COD_DIAG_OPTC_HV = 1146
COD_DIAG_APDFPGA = 1147
Expand All @@ -38,11 +42,12 @@ class CODICEAPID(IntEnum):
COD_LO_IAL = 1152
COD_LO_PHA = 1153
COD_LO_INSTRUMENT_COUNTERS = 1154
COD_LO_PRIORITY_COUNTS = 1155
COD_LO_SW_PRIORITY_COUNTS = 1155
COD_LO_SW_SPECIES_COUNTS = 1156
COD_LO_NSW_SPECIES_COUNTS = 1157
COD_LO_SW_ANGULAR_COUNTS = 1158
COD_LO_NSW_ANGULAR_COUNTS = 1159
COD_LO_NSW_PRIORITY_COUNTS = 1160
COD_HI_IAL = 1168
COD_HI_PHA = 1169
COD_HI_INSTRUMENT_COUNTERS = 1170
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions imap_processing/tests/codice/test_codice_l1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
Path(f"{imap_module_directory}/tests/codice/data/lo_fsw_view_5_ccsds.bin"),
"imap_codice_l1a_lo-sw-species-counts_20240319_v001.cdf",
),
(
Path(f"{imap_module_directory}/tests/codice/data/lo_fsw_view_6_ccsds.bin"),
"imap_codice_l1a_lo-nsw-species-counts_20240319_v001.cdf",
),
]


Expand Down

0 comments on commit ada98e9

Please sign in to comment.