ait.core.dtype module

AIT Primitive Data Types (PDT)

The ait.core.dtype module provides definitions and functions for primitive data types used in the construction and manipulation of OCO-3 commands and telemetry. Originally, this module was named ait.core.types, but types.py conflicts with Python’s builtin module of the same name, which causes all sorts of subtle import issues and conflicts.

Supported PrimitiveType names (which may be passed to ait.core.dtype.get()) are listed in ait.core.dtype.PrimitiveTypes.

The following code, shown via the interactive Python prompt, demonstrates attributes and methods of the ‘LSB_U16’ PrimitiveType. Several related statements are combined into a single line, forming tuples, for succinctness:

>>> from ait.core import dtype
>>> t = dtype.get('LSB_U16')
>>> t.name, t.endian, t.format, t.nbits, t.nbytes
('LSB_U16', 'LSB', '<H', 16, 2)
>>> t.float, t.signed
(False, False)
>>> t.min, t.max
(0, 65535)
>>> bytes = t.encode(42)
>>> ' '.join('0x%02x' % b for b in bytes)
'0x2a 0x00'
>>> t.decode(bytes)
42
>>> t.validate(42)
True

# Both the array of error messages and message prefixes are optional

>>> messages = [ ]
>>> t.validate(65536, messages, prefix='error:')
False
>>> t.validate(1e6, messages, prefix='error:')
False
>>> print "\n".join(messages)
error: value '65536' out of range [0, 65535].
error: float '1e+06' cannot be represented as an integer.
class ait.core.dtype.ArrayType(elemType, nelems)

Bases: object

__init__(elemType, nelems)

Creates a new ArrayType of nelems, each of type elemType.

decode(bytes[[, index], raw=False]) → value1, ..., valueN

Decodes the given sequence of bytes according to this Array’s element type.

If the optional index parameter is an integer or slice, then only the element(s) at the specified position(s) will be decoded and returned.

decodeElem(bytes, index, raw=False)

Decodes a single element at array[index] from a sequence bytes that contain data for the entire array.

encode(value1[, ...]) → bytes

Encodes the given values to a sequence of bytes according to this Array’s underlying element type

static parse(name) → [typename | None, nelems | None]

Parses an ArrayType name to return the element type name and number of elements, e.g.:

>>> ArrayType.parse('MSB_U16[32]')
['MSB_U16', 32]

If typename cannot be determined, None is returned. Similarly, if nelems is not an integer or less than one (1), None is returned.

name

Name of this ArrayType.

nbits

Number of bits required to represent this ArrayType.

nbytes

Number of bytes required to represent this ArrayType.

nelems

Number of elements in this ArrayType.

type

Type of array elements.

class ait.core.dtype.CmdType

Bases: ait.core.dtype.PrimitiveType

This type is used to take a two byte opcode and return the corresponding Command Definition (CmdDefn).

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray and returns the corresponding Command Definition (CmdDefn) for the underlying ‘MSB_U16’ command opcode.

If the optional parameter raw is True, the command opcode itself will be returned instead of the Command Definition (CmdDefn).

encode(value) → bytearray

Encodes the given value to a bytearray according to this PrimitiveType definition.

BASEPDT = 'MSB_U16'
cmddict

PrimitiveType base for the ComplexType

pdt

PrimitiveType base for the ComplexType

class ait.core.dtype.EVRType

Bases: ait.core.dtype.PrimitiveType

This type is used to take a two byte Event Verification Record (EVR) code and return the corresponding EVR Definition (EVRDefn).

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray according the corresponding EVR Definition (EVRDefn) for the underlying ‘MSB_U16’ EVR code.

If the optional parameter raw is True, the EVR code itself will be returned instead of the EVR Definition (EVRDefn).

encode(value) → bytearray

Encodes the given value to a bytearray according to this Complex Type definition.

BASEPDT = 'MSB_U16'
evrs

Getter EVRs dictionary

pdt

PrimitiveType base for the ComplexType

class ait.core.dtype.PrimitiveType(name)

Bases: object

A PrimitiveType contains a number of fields that provide information on the details of a primitive type, including: name (e.g. “MSB_U32”), format (Python C struct format code), endianness (“MSB” or “LSB”), float, signed, nbits, nbytes, min, and max.

PrimitiveTypes can validate() specific values and encode() and decode() binary representations.

__init__(name)

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray according to this PrimitiveType definition.

NOTE: The parameter raw is present to adhere to the decode() inteface, but has no effect for PrimitiveType definitions.

encode(value) → bytearray

Encodes the given value to a bytearray according to this PrimitiveType definition.

toJSON()
validate(value[, messages[, prefix]]) → True | False

Validates the given value according to this PrimitiveType definition. Validation error messages are appended to an optional messages array, each with the optional message prefix.

endian

Endianness of this PrimitiveType, either ‘MSB’ or ‘LSB’.

float

Indicates whether or not this PrimitiveType is a float or double.

format

Python C struct format code for this PrimitiveType.

max

Maximum value for this PrimitiveType.

min

Minimum value for this PrimitiveType.

name

Name of this PrimitiveType (e.g. ‘I8’, ‘MSB_U16’, ‘LSB_F32’, etc.).

nbits

Number of bits required to represent this PrimitiveType.

nbytes

Number of bytes required to represent this PrimitiveType.

signed

Indicates whether or not this PrimitiveType is signed or unsigned.

string

Indicates whether or not this PrimitiveType is a string.

class ait.core.dtype.Time32Type

Bases: ait.core.dtype.PrimitiveType

This four byte time represents the elapsed time in seconds since the GPS epoch.

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray containing the elapsed time in seconds since the GPS epoch and returns the corresponding Python datetime.

If the optional parameter raw is True, the integral number of seconds will be returned instead.

encode(value) → bytearray

Encodes the given value to a bytearray according to this ComplexType definition.

pdt

PrimitiveType base for the ComplexType

class ait.core.dtype.Time40Type

Bases: ait.core.dtype.PrimitiveType

This five byte time is made up of four bytes of seconds and one byte of (1 / 256) subseconds, representing the elapsed time since the GPS epoch.

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray containing the elapsed time in seconds plus 1/256 subseconds since the GPS epoch returns the corresponding Python datetime.

If the optional parameter raw is True, the number of seconds and subseconds will be returned as a floating-point number instead.

encode(value) → bytearray

Encodes the given value to a bytearray according to this ComplexType definition.

pdt

PrimitiveType base for the ComplexType

class ait.core.dtype.Time64Type

Bases: ait.core.dtype.PrimitiveType

This eight byte time is made up of four bytes of seconds and four bytes of nanoseconds, representing the elapsed time since the GPS epoch.

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, False) → value

Decodes the given bytearray containing the elapsed time in seconds plus nanoseconds since the GPS epoch and and returns the corresponding Python datetime. NOTE: The Python datetime class has only microsecond resolution.

If the optional parameter raw is True, the number of seconds and nanoseconds will be returned as a floating-point number instead.

encode(value) → bytearray

Encodes the given value to a bytearray according to this ComplexType definition.

pdt

PrimitiveType base for the ComplexType

class ait.core.dtype.Time8Type

Bases: ait.core.dtype.PrimitiveType

This 8-bit time type represents the fine time in the CCSDS secondary header. This time is calculated where the LSB of the octet is equal to 1/256 seconds (or 2^-8), approximately 4 msec. See SSP 41175-02H for more details on the CCSDS headers.

__init__()

PrimitiveType(name) -> PrimitiveType

Creates a new PrimitiveType based on the given typename (e.g. ‘MSB_U16’ for a big endian, 16 bit short integer).

decode(bytearray, raw=False) → value

Decodes the given bytearray and returns the number of (fractional) seconds.

If the optional parameter raw is True, the byte (U8) itself will be returned.

encode(value) → bytearray

Encodes the given value to a bytearray according to this ComplexType definition.

pdt

PrimitiveType base for the ComplexType

ait.core.dtype.get(typename) → PrimitiveType or ComplexType

Returns the PrimitiveType or ComplexType for typename or None.

ait.core.dtype.getCDT(typename) → ComplexType

Returns the ComplexType for typename or None.

ait.core.dtype.getPDT(typename)

get(typename) -> PrimitiveType

Returns the PrimitiveType for typename or None.