CCSDSPy / ccsdspy

I/O interface and utilities for CCSDS binary spacecraft data in Python. Library used in flight missions at NASA, NOAA, and SWRI
https://ccsdspy.org
BSD 3-Clause "New" or "Revised" License
75 stars 18 forks source link

Add the ability to define an array variable in a PacketField #28

Closed ehsteve closed 1 year ago

ehsteve commented 1 year ago

Packets may include arrays which would could be returned as a numpy array. For example, an image array may consist of 128 8-bit elements. Currently the field would have to be defined as

PacketField(name='IMAGE', data_type='uint', bit_length=1024)

and the user would have to further parse this field into an array.

PacketArray(name='IMAGE, shape=128, data_type='uint', bit_length=8)

which would return np.array(128, dtype=uint8)

and maybe even allow for 2d arrays

PacketArray(name='IMAGE, shape=(8,8), data_type='uint', bit_length=8)

which would return np.array((8,8), dtype=uint8)

Given how easy it is to reshape a numpy array maybe it is not worth supporting multidimensional arrays.

ddasilva commented 1 year ago

This is a reasonable feature. We could implement this by expanding arrays into var[0], var[[1] fields before the list of fields is passed to decode(), and the repacking those into an array after decode() is called. It should also be possible to modify decode() directly. Either is acceptable.

If there are multidimensional arrays, may also include an order= option to fields that specifies Fortran or C ordering (sometimes called row major or column major order).

ddasilva commented 1 year ago

Thinking back to this, I think the most sensible thing to do is to accept the shape=(8,8) or shape=8 as an option to PacketField like @ehsteve suggested, I also think there should be another option to specify Fortran vs C order for the array (in the case of >=2 dimensions), with the default being C order like in NumPy.

To implement this, I think the simplest thing is to add code in FixedLength.load() that intercepts the packet fields so it can expand an array into multiple fields and then recollect them after the decoding function is called.

rstrub commented 1 year ago

Ok, I just made a fork to temporarily use my fix, but I guess I'll go ahead and try to understand this change as it seems to apply to my request. Thanks!