pycdfpp package

pycdfpp

Indices and tables

class pycdfpp.Attribute

Bases: pybind11_object

property name
set_values(entries_values: List[ndarray], entries_types: List[DataType] = None)

Sets the values of the attribute with the given values. If entries_types is not None, the attribute is created with the given data type. Otherwise, the data type is inferred from the values.

Parameters:
entries_valuesList[np.ndarray or List[float or int or datetime] or str]

The values entries to set for the attribute. When a list is passed, the values are converted to a numpy.ndarray with the appropriate data type, with integers, it will choose the smallest data type that can hold all the values.

entries_typesList[DataType] or None, optional

The data type for each entry of the attribute. If None, the data type is inferred from the values. (Default is None)

type(self: pycdfpp._pycdfpp.Attribute, arg0: int) pycdfpp._pycdfpp.DataType
class pycdfpp.CDF

Bases: pybind11_object

A CDF file object.

Attributes:
attributes: dict

file attributes

variables: dict

file variables

majority: cdf_majority

file majority

distribution_version: int

file distribution version

lazy_loaded: bool

file lazy loading state

compression: CompressionType

file compression type

Methods

add_attribute(name, entries_values[, ...])

Adds a new attribute with the given values to the CDF object.

add_variable(name[, values, data_type, ...])

Adds a new variable to the CDF.

add_attribute(name: str, entries_values: List[ndarray], entries_types: List[DataType] = None)

Adds a new attribute with the given values to the CDF object. If entries_types is not None, the attribute is created with the given data type. Otherwise, the data type is inferred from the values.

Parameters:
namestr

The name of the attribute to add.

entries_valuesList[np.ndarray or List[float or int or datetime] or str]

The values entries to set for the attribute. When a list is passed, the values are converted to a numpy.ndarray with the appropriate data type, with integers, it will choose the smallest data type that can hold all the values.

entries_typesList[DataType] or None, optional

The data type for each entry of the attribute. If None, the data type is inferred from the values. (Default is None)

Returns:
Attribute or None

Returns the newly created attribute if successful. Otherwise, returns None.

Raises:
ValueError

If the attribute already exists.

Examples

>>> from pycdfpp import CDF, DataType
>>> import numpy as np
>>> from datetime import datetime
>>> cdf = CDF()
>>> cdf.add_attribute("attr1", [np.arange(10, dtype=np.int32)], [DataType.CDF_INT4])
attr1: [ [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ] ]
>>> cdf.add_attribute("multi", [np.arange(2, dtype=np.int32), [1.,2.,3.], "hello", [datetime(2010,1,1), datetime(2020,1,1)]])
add_variable(name: str, values: ~numpy.ndarray = None, data_type: ~pycdfpp._pycdfpp.DataType = None, is_nrv: bool = False, compression: ~pycdfpp._pycdfpp.CompressionType = <CompressionType.no_compression: 0>, attributes: ~typing.Mapping[str, ~typing.List[~typing.Any]] = None)

Adds a new variable to the CDF. If values is None, the variable is created with no values. Otherwise, the variable is created with the given values. If attributes is not None, the variable is created with the given attributes. If data_type is not None, the variable is created with the given data type. Otherwise, the data type is inferred from the values.

Parameters:
namestr

The name of the variable to add.

valuesnumpy.ndarray or list or None, optional

The values to set for the variable. If None, the variable is created with no values. Otherwise, the variable is created with the given values. (Default is None) When a list is passed, the values are converted to a numpy.ndarray with the appropriate data type, with integers, it will choose the smallest data type that can hold all the values.

data_typeDataType or None, optional

The data type of the variable. If None, the data type is inferred from the values. (Default is None)

is_nrvbool, optional

Whether or not the variable is a non-record variable. (Default is False)

compressionCompressionType, optional

The compression type to use for the variable. (Default is CompressionType.no_compression)

attributesMapping[str, List[Any]] or None, optional

The attributes to set for the variable. If None, the variable is created with no attributes. (Default is None)

Returns:
Variable or None

Returns the newly created variable if successful. Otherwise, returns None.

Raises:
ValueError

If the variable already exists.

Examples

>>> from pycdfpp import CDF, DataType, CompressionType
>>> import numpy as np
>>> cdf = CDF()
>>> cdf.add_variable("var1", np.arange(10, dtype=np.int32), DataType.CDF_INT4, compression=CompressionType.gzip_compression)
var1:
  shape: [ 10 ]
  type: CDF_INT1
  record varry: True
  compression: GNU GZIP
  ...
property attributes
property compression
property distribution_version
items(self: pycdfpp._pycdfpp.CDF) Iterator
property lazy_loaded
property majority
class pycdfpp.CompressionType

Bases: pybind11_object

Members:

no_compression

gzip_compression

rle_compression

ahuff_compression

huff_compression

ahuff_compression = <CompressionType.ahuff_compression: 3>
gzip_compression = <CompressionType.gzip_compression: 5>
huff_compression = <CompressionType.huff_compression: 2>
property name
no_compression = <CompressionType.no_compression: 0>
rle_compression = <CompressionType.rle_compression: 1>
property value
class pycdfpp.DataType

Bases: pybind11_object

Members:

CDF_BYTE

CDF_CHAR

CDF_INT1

CDF_INT2

CDF_INT4

CDF_INT8

CDF_NONE

CDF_EPOCH

CDF_FLOAT

CDF_REAL4

CDF_REAL8

CDF_UCHAR

CDF_UINT1

CDF_UINT2

CDF_UINT4

CDF_DOUBLE

CDF_EPOCH16

CDF_TIME_TT2000

CDF_BYTE = <DataType.CDF_BYTE: 41>
CDF_CHAR = <DataType.CDF_CHAR: 51>
CDF_DOUBLE = <DataType.CDF_DOUBLE: 45>
CDF_EPOCH = <DataType.CDF_EPOCH: 31>
CDF_EPOCH16 = <DataType.CDF_EPOCH16: 32>
CDF_FLOAT = <DataType.CDF_FLOAT: 44>
CDF_INT1 = <DataType.CDF_INT1: 1>
CDF_INT2 = <DataType.CDF_INT2: 2>
CDF_INT4 = <DataType.CDF_INT4: 4>
CDF_INT8 = <DataType.CDF_INT8: 8>
CDF_NONE = <DataType.CDF_NONE: 0>
CDF_REAL4 = <DataType.CDF_REAL4: 21>
CDF_REAL8 = <DataType.CDF_REAL8: 22>
CDF_TIME_TT2000 = <DataType.CDF_TIME_TT2000: 33>
CDF_UCHAR = <DataType.CDF_UCHAR: 52>
CDF_UINT1 = <DataType.CDF_UINT1: 11>
CDF_UINT2 = <DataType.CDF_UINT2: 12>
CDF_UINT4 = <DataType.CDF_UINT4: 14>
property name
property value
class pycdfpp.Majority

Bases: pybind11_object

Members:

row

column

column = <Majority.column: 0>
property name
row = <Majority.row: 1>
property value
class pycdfpp.Variable

Bases: pybind11_object

A CDF Variable (either R or Z variable)

Attributes:
attributes: dict

variable attributes

name: str

variable name

type: DataType

variable data type (ie CDF_DOUBLE, CDF_TIME_TT2000, …)

shape: List[int]

variable shape (records + record shape)

majority: cdf_majority

variable majority as writen in the CDF file, note that pycdfpp will always expose row major data.

values_loaded: bool

True if values are availbale in memory, this is usefull with lazy loading to know if values are already loaded.

compression: CompressionType

variable compression type (supported values are no_compression, rle_compression, gzip_compression)

values: numpy.array

returns variable values as a numpy.array of the corresponding dtype and shape, note that no copies are involved, the returned array is just a view on variable data.

values_encoded: numpy.array

same as values except that string variable are encoded wihch involves a data copy and since numpy uses UTF-32, expect a 4x memory increase for string values

Methods

add_attribute(name, values[, data_type])

Adds a new attribute to the variable.

set_compression_type

Sets the variable compression type

set_values

Sets the variable values

add_attribute(name: str, values: ndarray, data_type=None)

Adds a new attribute to the variable. If values is None, the attribute is created with no values. Otherwise, the attribute is created with the given values. If data_type is not None, the attribute is created with the given data type. Otherwise, the data type is inferred from the values.

Parameters:
namestr

The name of the attribute to add.

valuesnp.ndarray or List[float or int or datetime] or str

The values to set for the attribute. When a list is passed, the values are converted to a numpy.ndarray with the appropriate data type, with integers, it will choose the smallest data type that can hold all the values.

data_typeDataType or None, optional

The data type of the attribute. If None, the data type is inferred from the values. (Default is None)

Returns:
Attribute or None

Returns the newly created attribute if successful. Otherwise, returns None.

Raises:
ValueError

If the attribute already exists.

Examples

>>> from pycdfpp import CDF, DataType
>>> import numpy as np
>>> cdf = CDF()
>>> cdf.add_variable("var1", np.arange(10, dtype=np.int32), DataType.CDF_INT4)
var1:
  shape: [ 10 ]
  type: CDF_INT1
  record varry: True
  compression: None
  ...
>>> cdf["var1"].add_attribute("attr1", np.arange(10, dtype=np.int32), DataType.CDF_INT4)
attr1: [ [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ] ]
property attributes
property compression
property is_nrv
property majority
property name
set_values(values: ndarray, data_type: DataType = None)
property shape
property type
property values
property values_encoded
property values_loaded
class pycdfpp.epoch

Bases: pybind11_object

property value
class pycdfpp.epoch16

Bases: pybind11_object

property picoseconds
property seconds
pycdfpp.load(file_or_buffer: str, iso_8859_1_to_utf8: bool = False, lazy_load: bool = True)[source]

Load and parse a CDF file.

Parameters:
file_or_bufferstr or ByteString

Either a filename to be loaded or an in-memory file implementing the Python buffer protocol.

iso_8859_1_to_utf8bool, optional

Automatically convert Latin-1 characters to their equivalent UTF counterparts when True. For CDF files prior to version 3.8, UTF-8 wasn’t supported and some CDF files might contain “illegal” Latin-1 characters. (Default is False)

lazy_loadbool, optional

Controls whether variable values are loaded immediately or only when accessed by the user. If True, variables’ values are loaded on demand. If False, all variable values are loaded during parsing. (Default is True)

Returns:
CDF or None

Returns a CDF object upon successful read. If there’s an issue with the read, None is returned.

pycdfpp.save(*args, **kwargs)

Overloaded function.

  1. save(cdf: pycdfpp._pycdfpp.CDF, fname: str) -> bool

  2. save(cdf: pycdfpp._pycdfpp.CDF) -> pycdfpp._pycdfpp._cdf_bytes

pycdfpp.to_datetime(values)[source]

to_datetime

Parameters:
values: Variable or epoch or List[epoch] or epoch16 or List[epoch16] or tt2000_t or List[tt2000_t]

input value(s)

to convert to datetime.datetime
Returns:
List[datetime.datetime]
pycdfpp.to_datetime64(values)[source]

to_datetime64

Parameters:
values: Variable or epoch or List[epoch] or numpy.ndarray[epoch] or epoch16 or List[epoch16] or numpy.ndarray[epoch16] or tt2000_t or List[tt2000_t] or numpy.ndarray[tt2000_t]

input value(s)

to convert to numpy.datetime64
Returns:
numpy.ndarray[numpy.datetime64]
class pycdfpp.tt2000_t

Bases: pybind11_object

property value