66
77import numpy as np
88import pandas as pd
9+ import space_packet_parser as spp
910import xarray as xr
10- from space_packet_parser import definitions , encodings , parameters
11+ from space_packet_parser .exceptions import UnrecognizedPacketTypeError
12+ from space_packet_parser .xtce import definitions , encodings , parameter_types
1113
1214from imap_processing .spice .time import met_to_ttj2000ns
1315
@@ -158,11 +160,11 @@ def _get_minimum_numpy_datatype( # noqa: PLR0912 - Too many branches
158160 datatype : str
159161 The minimum datatype.
160162 """
161- data_encoding = definition .named_parameters [name ].parameter_type .encoding
163+ data_encoding = definition .parameters [name ].parameter_type .encoding
162164
163165 if use_derived_value and isinstance (
164- definition .named_parameters [name ].parameter_type ,
165- parameters .EnumeratedParameterType ,
166+ definition .parameters [name ].parameter_type ,
167+ parameter_types .EnumeratedParameterType ,
166168 ):
167169 # We don't have a way of knowing what is enumerated,
168170 # let numpy infer the datatype
@@ -254,11 +256,18 @@ def packet_file_to_datasets(
254256 variable_mapping : dict [int , set ] = dict ()
255257
256258 # Set up the parser from the input packet definition
257- packet_definition = definitions . XtcePacketDefinition (xtce_packet_definition )
259+ packet_definition = spp . load_xtce (xtce_packet_definition )
258260
259261 with open (packet_file , "rb" ) as binary_data :
260- packet_generator = packet_definition .packet_generator (binary_data )
261- for packet in packet_generator :
262+ for binary_packet in spp .ccsds_generator (binary_data ):
263+ try :
264+ packet = packet_definition .parse_bytes (binary_packet )
265+ except UnrecognizedPacketTypeError as e :
266+ # NOTE: Not all of our definitions have all of the APIDs
267+ # we may encounter, so we only want to process ones
268+ # we can actual parse.
269+ logger .debug (e )
270+ continue
262271 apid = packet ["PKT_APID" ]
263272 if apid not in data_dict :
264273 # This is the first packet for this APID
@@ -274,10 +283,7 @@ def packet_file_to_datasets(
274283 f"got: { packet .keys ()} "
275284 )
276285
277- # TODO: Do we want to give an option to remove the header content?
278- packet_content = packet .user_data | packet .header
279-
280- for key , value in packet_content .items ():
286+ for key , value in packet .items ():
281287 val = value if use_derived_value else value .raw_value
282288 data_dict [apid ][key ].append (val )
283289 if key not in datatype_mapping [apid ]:
@@ -289,8 +295,15 @@ def packet_file_to_datasets(
289295 dataset_by_apid = {}
290296
291297 for apid , data in data_dict .items ():
292- # The time key is always the first key in the data dictionary on IMAP
293- time_key = next (iter (data .keys ()))
298+ # The time key is the secondary header, right after the primary header
299+ # in the data dictionary on IMAP (8th key overall)
300+ try :
301+ time_key = list (data .keys ())[7 ]
302+ except IndexError :
303+ logger .debug (
304+ f"Could not determine time key for APID { apid } , skipping dataset."
305+ )
306+ continue
294307 # Convert to J2000 time and use that as our primary dimension
295308 time_data = met_to_ttj2000ns (data [time_key ])
296309 ds = xr .Dataset (
0 commit comments