CODA Python
The CODA Python interface consists of a Python package 'coda' containing extensive functionality to access data inside product files.
The CODA Python interface consists of a high-level interface and a low-level interface.
The high level interface include an object oriented interface using classes for Products, Cursors, and Types. This is the recommended interface to use. In addition, there are high-level module functions that resemble the CODA MATLAB and CODA IDL interfaces.
The low-level interface contains a direct wrapping (using CFFI) of the CODA C interface. It is not recommended to use this interface directly, unless you need to use specific functions that are not available in the high-level interface.
The CODA Python interface depends on the cffi and numpy python packages. These packages must be installed in order to be able to use the Python interface.
Contents
- CODA Definition Path
- High level CODA Data Types
- High level CODA Classes
- High level CODA Functions
coda.open
coda.open_as
coda.close
coda.get_attributes
coda.get_description
coda.fetch
coda.get_field_available
coda.get_field_count
coda.get_field_names
coda.get_size
coda.time_to_string
coda.time_to_utcstring
coda.get_unit
coda.version
coda.set_option_filter_record_fields
coda.get_option_filter_record_fields
- Low level CODA Data Types
- Low level CODA Functions
coda.init
coda.done
coda.set_option_bypass_special_types
coda.get_option_bypass_special_types
coda.set_option_perform_boundary_checks
coda.get_option_perform_boundary_checks
coda.set_option_perform_conversions
coda.get_option_perform_conversions
coda.set_option_use_fast_size_expressions
coda.get_option_use_fast_size_expressions
coda.set_option_use_mmap
coda.get_option_use_mmap
coda.NaN
coda.isNaN
coda.PlusInf
coda.MinInf
coda.isInf
coda.isPlusInf
coda.isMinInf
coda.c_index_to_fortran_index
coda.match_filefilter
coda.time_double_to_parts
coda.time_double_to_parts_utc
coda.time_parts_to_double
coda.time_parts_to_double_utc
coda.time_parts_to_string
coda.time_string_to_parts
coda.time_double_to_string
coda.time_double_to_string_utc
coda.time_string_to_double
coda.time_string_to_double_utc
coda.recognize_file
coda.open
coda.open_as
coda.close
coda.get_product_filename
coda.get_product_file_size
coda.get_product_format
coda.get_product_class
coda.get_product_type
coda.get_product_version
coda.get_product_root_type
coda.get_product_variable_value
coda.type_get_format_name
coda.type_get_class_name
coda.type_get_native_type_name
coda.type_get_special_type_name
coda.type_has_attributes
coda.type_get_format
coda.type_get_class
coda.type_get_read_type
coda.type_get_string_length
coda.type_get_bit_size
coda.type_get_name
coda.type_get_description
coda.type_get_unit
coda.type_get_fixed_value
coda.type_get_attributes
coda.type_get_num_record_fields
coda.type_get_record_field_index_from_name
coda.type_get_record_field_index_from_real_name
coda.type_get_record_field_type
coda.type_get_record_field_name
coda.type_get_record_field_real_name
coda.type_get_record_field_hidden_status
coda.type_get_record_field_available_status
coda.type_get_record_union_status
coda.type_get_array_num_dims
coda.type_get_array_dim
coda.type_get_array_base_type
coda.type_get_special_type
coda.type_get_special_base_type
coda.cursor_set_product
coda.cursor_goto
coda.cursor_goto_first_record_field
coda.cursor_goto_next_record_field
coda.cursor_goto_record_field_by_index
coda.cursor_goto_record_field_by_name
coda.cursor_goto_available_union_field
coda.cursor_goto_first_array_element
coda.cursor_goto_next_array_element
coda.cursor_goto_array_element
coda.cursor_goto_array_element_by_index
coda.cursor_goto_attributes
coda.cursor_goto_root
coda.cursor_goto_parent
coda.cursor_use_base_type_of_special_type
coda.cursor_has_ascii_content
coda.cursor_has_attributes
coda.cursor_get_string_length
coda.cursor_get_bit_size
coda.cursor_get_byte_size
coda.cursor_get_num_elements
coda.cursor_get_product_file
coda.cursor_get_depth
coda.cursor_get_index
coda.cursor_get_file_bit_offset
coda.cursor_get_file_byte_offset
coda.cursor_get_format
coda.cursor_get_type_class
coda.cursor_get_read_type
coda.cursor_get_special_type
coda.cursor_get_type
coda.cursor_get_record_field_index_from_name
coda.cursor_get_record_field_available_status
coda.cursor_get_available_union_field_index
coda.cursor_get_array_dim
coda.cursor_read_int8
coda.cursor_read_uint8
coda.cursor_read_int16
coda.cursor_read_uint16
coda.cursor_read_int32
coda.cursor_read_uint32
coda.cursor_read_int64
coda.cursor_read_uint64
coda.cursor_read_float
coda.cursor_read_double
coda.cursor_read_char
coda.cursor_read_string
coda.cursor_read_bits
coda.cursor_read_bytes
coda.cursor_read_int8_array
coda.cursor_read_uint8_array
coda.cursor_read_int16_array
coda.cursor_read_uint16_array
coda.cursor_read_int32_array
coda.cursor_read_uint32_array
coda.cursor_read_int64_array
coda.cursor_read_uint64_array
coda.cursor_read_float_array
coda.cursor_read_double_array
coda.cursor_read_char_array
coda.cursor_read_int8_partial_array
coda.cursor_read_uint8_partial_array
coda.cursor_read_int16_partial_array
coda.cursor_read_uint16_partial_array
coda.cursor_read_int32_partial_array
coda.cursor_read_uint32_partial_array
coda.cursor_read_int64_partial_array
coda.cursor_read_uint64_partial_array
coda.cursor_read_float_partial_array
coda.cursor_read_double_partial_array
coda.cursor_read_char_partial_array
coda.cursor_read_complex_double_pair
coda.cursor_read_complex_double_pairs_array
coda.cursor_read_complex_double_split
coda.cursor_read_complex_double_split_array
coda.expression_get_type_name
coda.expression_from_string
coda.expression_delete
coda.expression_get_type
coda.expression_is_constant
coda.expression_is_equal
coda.expression_eval_bool
coda.expression_eval_integer
coda.expression_eval_float
coda.expression_eval_string
coda.expression_eval_node
CODA Definition Path
To access products whose formats are defined using .codadef files, you should let CODA now where these .codadef files are stored. By default the CODA Python interface will look for .codadef files in a directory relative to the location of the CODA Python package (../../../../share/coda/definitions
).
You can override the default location by setting the CODA_DEFINITION environment variable. This environment variable should be a ':' separated (';' on Windows) list of absolute paths to directories containing .codadef files or absolute paths to .codadef files themselves (or a mix of those).
When you import the CODA Python package this will trigger an initialisation of CODA, so you should make sure that the CODA_DEFINITION environment variable is set before you import the CODA Python package. Setting the environment variable can be performed from within Python using:
import os os.putenv('CODA_DEFINITION', '<your codadef search path>') import coda
High level CODA Data Types
When reading data from a product file, CODA will use the following mapping to translate the ingested data into Python data structures:
CODA class | CODA read type / CODA special type | Python data type | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
record | coda.Record | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
array | This will be a numpy array (numpy.array ) object. The following table relates the CODA array base type to the numpy base type:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | int8 | Python Integer | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | uint8 | Python Integer | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | int16 | Python Integer | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | uint16 | Python Integer | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | int32 | Python Integer | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | uint32 | Python Long | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | int64 | Python Long | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer | uint64 | Python Long | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
real | float | Python Float | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
real | double | Python Float | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text | char | Python String | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text | string | Python String | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raw | bytes | numpy.array object with base type uint8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
special | no_data | None | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
special | time | Python Float | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
special | complex | Python Complex Number |
High level CODA Classes
The high-level classes form an object-oriented wrapper layer around the low-level functionality and add some additional convenience methods
A CODA Product is represented by an instance of class Product. One or more instances of class Cursor can be used to navigate a product, and extract CODA types and product data. CODA types are represented as instances of class Type. There are several subclasses of Type, corresponding to the different CODA type classes. A CODA expression is represented by an instance of class Expression.
Example of basic usage:
import coda with coda.Product('somefile.nc') as product: # use cursor cursor = product.cursor() cursor.goto('a/b') data = cursor.fetch() # use convenience method data = product.fetch('a/b')
Error Objects
python
class Error(Exception)
Exception base class for all CODA Python interface errors.
FetchError Objects
python
class FetchError(Error)
Exception raised when an errors occurs when fetching data.
Attributes:
str
- error message
CodacError Objects
python
class CodacError(Error)
Exception raised when an error occurs inside the CODA C library.
Attributes:
errno
- error code; if None, the error code will be retrieved from the CODA C library.strerror
- error message; if None, the error message will be retrieved from the CODA C library.
Node Objects
python
class Node(object)
Base class of 'Product' and 'Cursor' classes.
This class contains shared functionality between Product and Cursor. For this functionality, a Product can be used as if it were a Cursor (pointing at the product root).
For a description of the 'path' argument that is used in several methods, see the introduction of the high-level functions section
fetch
python
| fetch(*path)
Return all product data (recursively) for the current data item (or as specified by a path).
This can result in a combination of nested 'Record' instances, numpy arrays, scalars, strings and so on.
Some examples:
data = product.fetch('fieldname') data = cursor.fetch('a/b')
Arguments:
path
- path description (optional)
get_attributes
python
| get_attributes(*path)
Return a 'Record' instance containing the attributes for the current data item (or as specified by a path).
Arguments:
path
- path description (optional)
attributes
python
| @property
| attributes()
Return a 'Record' instance containing the attributes for the current data item.
get_description
python
| get_description(*path)
Return the description in the product format definition for the current data item (or as specified by a path).
Arguments:
path
- path description (optional)
description
python
| @property
| description()
Return the description (as a string) in the product format definition for the current data item.
get_unit
python
| @property
| get_unit(*path)
Return unit information (as a string) in the product format definition for the current data item (or as specified by a path).
Arguments:
path
- path description (optional)
unit
python
| @property
| unit()
Return unit information (as a string) in the product format definition for the current data item.
cursor
python
| cursor(*path)
Return a new 'Cursor' instance, pointing to the same data item (or as specified by a path).
Arguments:
path
- path description (optional)
read_partial_array
python
| read_partial_array(offset, count)
Return partial (flat) array data, using specified offset and count.
C array ordering conventions are used.
Arguments:
offset
- (flat) array indexcount
- number of elements to read
field_available
python
| field_available(*path)
Return a boolean indicating whether a record field is available.
The last item of the path description must point to a record field.
Arguments:
path
- path description (optional)
field_count
python
| field_count(*path)
Return the number of fields in a record.
The last item of the path must point to a record.
Arguments:
path
- path description (optional)
field_names
python
| field_names(*path)
Return the names of the fields in a record.
The last item of the path must point to a record.
Arguments:
path
- path description (optional)
Product Objects
python
class Product(Node)
CODA Product class.
An instance of this class represents a CODA product.
It is a wrapper class around the low-level coda_product struct.
It implements the context-manager protocol for conveniently closing (these low-level) products.
__init__
python
| __init__(path)
Initialize a 'Product' instance for specified product file.
The instance should be cleaned up after use via 'with' keyword or by calling the 'close' method (or global function).
Arguments:
path
- path to product file
close
python
| close()
Close the low-level CODA product.
Note that it is also possible to use the 'with' keyword for this.
version
python
| @property
| version()
Return the product type version.
product_class
python
| @property
| product_class()
Return the name of the product class.
product_type
python
| @property
| product_type()
Return the name of the product type.
format
python
| @property
| format()
Return the name of the product format.
definition_file
python
| @property
| definition_file()
Return the path to the coda definition file that describes the product format.
file_size
python
| @property
| file_size()
Return the product file size.
filename
python
| @property
| filename()
Return the product filename.
root_type
python
| @property
| root_type()
Return the CODA type of the root of the product.
variable_value
python
| variable_value(variable, index=0)
Return the value for a product variable.
Product variables are used to store frequently needed information of a product (information that is needed to calculate byte offsets or array sizes within a product).
Consult the CODA Product Definition Documentation for an overview of product variables for a certain product type.
Product variables can be one-dimensional arrays, in which an index must be passed.
Arguments:
variable
- name of product variableindex
- array index of the product variable (optional)
Cursor Objects
python
class Cursor(Node)
CODA Cursor class.
An instance of this class represents a CODA cursor.
It is a wrapper class around the low-level coda_cursor struct.
Cursors are used to navigate a product hierarchy, and extract CODA types and product data.
Internally, a 'Cursor' instance consists of a stack of pointers, making it is possible to easily move up and down a product hierarchy.
__init__
python
| __init__(obj=None, *path)
Initialize a 'Cursor' instance.
If a 'Cursor' instance is passed, the cursor will point to the same location.
If a 'Product' instance is passed, the cursor will point to the product root.
If a path is given, the cursor location will then be changed to point as specified.
If no arguments are given, the 'set_product' method should be used to point to a 'Product' instance.
Arguments:
obj
- existing 'Cursor' or 'Product' instance (optional)path
- path description (optional)
goto
python
| goto(*path)
Move the cursor as specified by 'path'.
Arguments:
path
- path description (optional)
goto_parent
python
| goto_parent()
Move the cursor one level up in the hierarchy.
goto_root
python
| goto_root()
Move the cursor to the product root.
goto_first_record_field
python
| goto_first_record_field()
Move the cursor to the first record field.
goto_next_record_field
python
| goto_next_record_field()
Move the cursor to the next record field.
goto_record_field_by_index
python
| goto_record_field_by_index(index)
Move the cursor to the record field with the given index.
Arguments:
index
- field index
goto_record_field_by_name
python
| goto_record_field_by_name(name)
Move the cursor to the record field with the given name.
Arguments:
index
- field name
goto_first_array_element
python
| goto_first_array_element()
Move the cursor to the first array element.
goto_next_array_element
python
| goto_next_array_element()
Move the cursor to the next array element.
goto_array_element
python
| goto_array_element(idcs)
Move the cursor to the array element with the given indices.
Arguments:
idcs
- sequence of indices (one per dimension)
goto_array_element_by_index
python
| goto_array_element_by_index(index)
Move the cursor to the array element with the given index.
A multi-dimensional array is treated as a one-dimensional array (with the same number of elements).
The ordering in such a one dimensional array is by definition chosen to be equal to the way the array elements are stored as a sequence in the product file.
The mapping of a one dimensional index for each multidimensional data array to an array of subscripts (and vice versa) is defined in such a way that the last element of a subscript array is the one that is the fastest running index (i.e. C array ordering).
All multidimensional arrays have their dimensions defined using C array ordering in CODA.
Arguments:
index
- array index
goto_available_union_field
python
| goto_available_union_field()
Move the cursor to the available union field.
goto_attributes
python
| goto_attributes()
Move the cursor to a (virtual) record containing the attributes of the current data element.
set_product
python
| set_product(product)
Initialize the cursor to point to the given product root.
Arguments:
product
- 'Product' instance
product
python
| @property
| product()
Return the corresponding 'Product' instance.
num_elements
python
| num_elements()
Return the number of array or record elements (or 1 for other types.
string_length
python
| string_length()
Return the length in bytes of a string.
use_base_type_of_special_type
python
| use_base_type_of_special_type()
Reinterpret special data using the special type base type.
All special data types have a base type that can be used to read the data in its raw form (e.g. for ASCII time data the type will change to a string type and for binary compound time data the type will change to a record with fields containing binary numbers).
coda_type
python
| @property
| coda_type()
Return a 'Type' instance corresponding to the CODA type for the current location.
type_class
python
| @property
| type_class()
Return the name of the CODA type class for the current location.
special_type
python
| @property
| special_type()
Return the name of the special type for the current location.
format
python
| @property
| format()
Return the name of the storage format for the current location.
has_attributes
python
| @property
| has_attributes()
Return a boolean indicating if there are attributes for the current location.
has_ascii_content
python
| @property
| has_ascii_content()
Return a boolean indicating if the data for the current location is stored in ASCII format.
available_union_field_index
python
| @property
| available_union_field_index()
Return the index of the available union field.
record_field_is_available
python
| record_field_is_available(index)
Return a boolean indicating if a record field is available.
Arguments:
index
- record field index
record_field_index_from_name
python
| record_field_index_from_name(name)
Return record field index for the field with the given name.
Arguments:
name
- record field name
array_dim
python
| @property
| array_dim()
Return a list containing the dimensions of an array.
depth
python
| @property
| depth()
Return the hierarchical depth of the current location.
index
python
| @property
| index()
Return the array or record field index for the current location.
For arrays, a 'flat' index is returned (similator to the argument of the 'gotoarrayelementbyindex' method).
bit_size
python
| bit_size()
Return the bit size for the current location.
byte_size
python
| byte_size()
Return the byte size for the current location.
It is calculated by rounding up the bit size to the nearest byte.
file_bit_offset
python
| @property
| file_bit_offset()
Return the file offset in bits for the current location.
file_byte_offset
python
| @property
| file_byte_offset()
Return the file offset in bytes for the current location.
It is calculated by rounding down the bit offset to the nearest byte.
Record Objects
python
class Record(object)
CODA Record class.
An instance of this class represents a CODA record.
Each record field will appear as an instance attribute. The field name is used as the name of the attribute, and its value is read from the product file.
Type Objects
python
class Type(object)
CODA Type base class.
An instance of this class represents a CODA type.
It is a wrapper class around the low-level coda_type struct.
Specialized functionality corresponding to the different CODA types is provided by the following subclasses:
- 'IntegerType'
- 'RealType'
- 'RecordType'
- 'ArrayType'
- 'SpecialType'
- 'TextType'
- 'RawType'
type_class
python
| @property
| type_class()
Return the name of the type class.
format
python
| @property
| format()
Return the name of the type storage format.
special_type
python
| @property
| special_type()
Return the name of the special type.
description
python
| @property
| description()
Return the type description.
has_attributes
python
| @property
| has_attributes()
Return a boolean indicating whether the type has any attributes.
attributes
python
| @property
| attributes()
Return the type for the associated attribute record.
read_type
python
| @property
| read_type()
Return the best native type for reading the data.
unit
python
| @property
| unit()
Return the type unit.
bit_size
python
| @property
| bit_size()
Return the bit size for the type.
fixed_value
python
| @property
| fixed_value()
Return the associated fixed value string for the type.
IntegerType Objects
python
class IntegerType(Type)
CODA Integer Type class.
RealType Objects
python
class RealType(Type)
CODA Real Type class.
RecordType Objects
python
class RecordType(Type)
CODA Record Type class.
Unions are implemented in CODA as records where only one field is 'available' at a time.
num_fields
python
| num_fields()
Return the total number of fields.
field
python
| field(index)
Arguments:
index
- field index or name
Return an instance of 'RecordTypeField' corresponding to the specified field index or name.
fields
python
| fields()
Return a list of 'RecordTypeField' instances corresponding to the fields.
is_union
python
| @property
| is_union()
Return a boolean indicating whether the record is a union.
RecordTypeField Objects
python
class RecordTypeField(object)
is_hidden
python
| @property
| is_hidden()
Return a boolean indicating whether the field is hidden.
is_optional
python
| @property
| is_optional()
Return a boolean indicating whether the field is optional (not always available).
coda_type
python
| @property
| coda_type()
Return 'Type' instance corresponding to the field.
name
python
| @property
| name()
Return the name (identifier) of the field
real_name
python
| @property
| real_name()
Return the real (original) name of the field.
This may be different from the regular name (identifier) because of restrictions on identifier names.
ArrayType Objects
python
class ArrayType(Type)
CODA Array Type class.
base_type
python
| @property
| base_type()
Return a 'Type' instance corresponding to the array elements.
dim
python
| @property
| dim()
Return a list with array dimension sizes.
The size of a variable dimension is represented as -1.
SpecialType Objects
python
class SpecialType(Type)
CODA Special Type class.
base_type
python
| @property
| base_type()
Return a 'Type' instance corresponding to the special type base type.
TextType Objects
python
class TextType(Type)
CODA Text Type class.
string_length
python
| @property
| string_length()
Return the string length in bytes.
RawType Objects
python
class RawType(Type)
CODA Raw Type class.
Expression Objects
python
class Expression(object)
CODA Expression class.
An instance of this class represents a CODA expression.
It is a wrapper class around the low-level coda_expression struct.
Consult the CODA documentation for information about the the CODA expression language.
__init__
python
| __init__(s)
Initialize an 'Expression' instance.
The instance should be cleaned up after use via 'with' keyword or by calling the 'delete' method.
Arguments:
s
- string containing CODA expression
is_constant
python
| is_constant()
Return a boolean indicating whether the expression is constant.
An expression is constant if it does not depend on the contents of a product and hence can be evaluated without requiring a cursor.
is_equal
python
| is_equal(expr)
Return a boolean indicating whether the expression is equal to another 'Expression' instance.
For two expressions to be considered as equal, all operands to an operation need to be equal and operands need to be provided in the same order.
For example, the expression '1!=2' is not considered equal to the expression '2!=1'.
Arguments:
expr
- 'Expression' instance
eval
python
| eval(cursor=None)
Evaluate the expression and return the resulting value.
For a constant expression, the 'cursor' argument is optional.
For a node expression, the cursor is moved to the resulting location and no value is returned.
Arguments:
cursor
- 'Cursor' instance (optional)
expression_type
python
| @property
| expression_type()
Return the name of the expression type.
delete
python
| delete()
Delete the low-level CODA expression object.
Note that it is also possible to use the 'with' keyword for this.
High level CODA Functions
Most high level CODA functions require a start
and path
parameter. These two parameters together define the location of a data item in a product.
The start
parameter determines the offset in the product from which the path
parameter is expanded. It can either be a file handle or a CODA Cursor (see Low level CODA Data Types). When a file handle is passed we start at the root of the product and when it is a cursor we start at the position of the cursor.
The path
argument is actually a series of parameters (which can also be empty). Starting from the start
position, the path
parameters should provide valid fieldnames and array indices to navigate deeper into the product. For example, suppose we have a product that has a measurements
data set with 100 data set records and in each data set record there is a time
field containing a time value of type double. If we want to read the time value in the first data set record (using the file handle filehandle
as start
parameter) we would use: coda.fetch(filehandle, "measurements", 0, "time")
. You can provide each path argument as a separate parameter or combine them together into a single string argument. Alternative calls that have the same result could thus be coda.fetch(filehandle, "measurements[0]", "time")
or coda.fetch(filehandle, "measurements[0]/time")
.
It is also possible to read several data elements at once. We can for instance read the full data set record using coda.fetch(filehandle, "measurements", 0)
or even read the whole product using coda.fetch(filehandle)
. When you read a group of data at once, CODA will create a dynamic data structure in Python (consisting of coda.Record
and numpy.array
objects to represent records and arrays) for the product data that is read.
The types of arguments that you can use in the list of arguments for path
are:
- Array index: You need to provide an array index that has the same number of elements as there are dimensions in the array that you are referring to. So if the array is two dimensional you have to pass indices for both dimensions like
[4,5]
. If the array is one dimensional you can just provide a single index value (without the '[]') to get to the k-th element. You can usecoda.get_size
to check the number of dimensions of an array and the size of each dimension. - Field name: To go to a certain field inside the record that you are pointing to, just provide the field name as a string. You should be aware that some record fields can be dynamically available. In such cases it is better to check in advance (using
coda.get_field_available
) whether the field is available, before traversing it. - Path: You can provide a string containing a path reference, such as
"measurements[0]/time"
. Using paths will also allow you to navigate to attributes by using"@"
as a path component. For instance, '"temperature", "@", "units"
' or"temperature@units"
will point to the units attribute of the temperature data. Note that array indices used in these string paths need to be 0-based indices on the flattened view of an array. This means that if an array is defined as having more than one dimension then the index as used in a path expression should be between 0 and the total number of array elements (exclusive). For example, for a [10,8] array, the index should be >= 0 and <= 79.
Just as the low level functions, the high level CODA Python functions will throw an exception when an error condition occurs. For the high level functions the exception will be of type coda.CodaError
.
coda.open(filename)
This function opens a file and returns a handle to the opened product file.
The high and lowel level coda.open
functions are actually one and the same. You can thus use the product file handle that is returned by coda.open
both as start
parameter in the high level CODA functions mentioned below as well as pf
parameter in the low level CODA functions.
coda.open_as(filename, product_class, product_type, version)
This function will try to open the specified file for reading similar to coda.open
, but instead of trying to automatically recognise the applicable product class/type/version as coda.open
does, this function will impose the format definition that is associated with the given product_class, product_type, and version parameters.
Note that you normally won't need this function as CODA will be able to recognize which format definition to use automatically. However, for the rare occasions where coda.open
is not sufficient, you can use this function to force the use of a specific format definition.
You can specify -1 for the version to request the latest available version of the format definition.
The high and lowel level coda.open_as
functions are one and the same, just as for coda.open
.
coda.close(filehandle)
This function closes the file associated with the file handle filehandle
.
Just as the coda.open
function, the high and lowel level coda.close
functions are one and the same.
coda.get_attributes(start, *path)
Retrieve the attributes of the specified data item.
This function returns a coda.Record
containing the attributes of the specified data item.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
This function is deprecated. You can replace it by using coda.fetch(start, *path, "@")
.
coda.get_description(start, *path)
Retrieve the description of a field.
This function returns a string containing the description in the CODA product format definition of the specified data element.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.fetch(start, *path)
Retrieve data from a product file.
Reads the specified data element from the product file. For instance if pf
is a product file handle obtained by calling coda.open()
and the product contains a dataset of records then you can retrieve a time field from the first record using:
>>> value = coda.fetch(pf, "datasetname", 0, "time")
You can also combine the path into a single string parameter:
>>> value = coda.fetch(pf, "datasetname[0]/time")
Which path to provide depends on the format of the product you are trying to access.
Instead of just reading individual values, like strings, integers, doubles, etc. it is also possible to read complete arrays or records of data. For instance, you could read the whole first record of the dataset using:
>>> record = coda.fetch(pf, "datasetname", 0)
This gives you a coda.Record
containing all the record fields.
It is also possible to read an entire product at once by leaving the data specification argument list empty:
>>> product = coda.fetch(pf)
To read attributes, you can pass '@' as path element. For instance, the following command reads the 'units' attribute value from a temperature variable:
>>> unit = coda.fetch(pf, "temperature@units")
For the coda.fetch
function there is an additional feature. If you provide a -1 for one or more of the dimensions of an array you will fetch all elements in the specified dimension(s). For example, with coda.fetch(pf, "datasetname", -1, "dsr_time")
you can fetch all dsr_time
values for all measurements into a single array. Note that passing -1 only works when passing it as an explicit argument (i.e. calling coda.fetch(pf, "dataset[-1]/dsr_time")
, where the -1 index is part of a string argument, will not work).
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
, a valid CODA Cursor _or_ a product file path. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
Note that coda.fetch()
is implemented for a large part in Python. It uses the C library underneath for cursor navigation and core reading functionality. This is the reason why, for instance, the -1 argument can only be provided as a separate argument to the function (and not as part of a path string, such as "dataset[-1]"
), since the -1 argument is specific to the Python interface of CODA.
coda.get_field_available(start, *path)
Find out whether a dynamically available record field is available or not.
This function returns True if the record field is available and False if it is not. The last item of the path
argument should point to a record field. An empty path
is considered an error, even if the start argument is a CODA cursor.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.get_field_count(start, *path)
Retrieve the number of fields in a record.
This function returns the number of fields in the coda.Record
instance that will be returned if coda.fetch()
is called with the same arguments. The last node on the path should reference a record.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.get_field_names(start, *path)
Retrieve the names of the fields in a record.
This function returns the names of the fields in the coda.Record
instance that will be returned if coda.fetch()
is called with the same arguments. The last node on the path should reference a record.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.get_size(start, *path)
Retrieve the dimensions of the specified array.
This function returns the dimensions of the array that will be returned if coda.fetch()
is called with the same arguments. Thus, you can check what the dimensions of an array are without having to retrieve the entire array with coda.fetch()
. The last node on the path should reference an array.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.time_to_string(n_seconds_since_2000)
Convert a number of seconds since 2000-01-01 to a human readable string format. For example:
>>> coda.time_to_string(68260079.0)
would return the string '2002-03-01 01:07:59.000000'
.
It is possible to input a list or tuple of doubles, in which case a list of strings will be returned.
coda.time_to_utcstring(n_seconds_since_2000)
Convert a TAI number of seconds since 2000-01-01 (TAI) to a human readable string format in UTC format. For example:
>>> coda.time_to_utcstring(68260111.0)
would return the string '2002-03-01 01:07:59.000000'
.
It is possible to input a list or tuple of doubles, in which case a list of strings will be returned.
coda.get_unit(start, *path)
Retrieve unit information.
This function returns a string containing the unit information which is stored in the CODA product format definition for the specified data item.
The start
parameter must be a valid CODA file handle that was retrieved with coda.open()
or a valid CODA Cursor. If the start argument is a cursor, then the specified path is traversed starting from the position represented by the cursor. The format of the path
argument is described at the top of this section.
coda.version()
Retrieve CODA version information.
This function returns a string containing the version number of CODA. The version number is always of the format 'x.y.z', i.e., major, minor, and revision numbers, separated by dots.
coda.set_option_filter_record_fields(enable)
Records like for instance a Main Product Header contain fields that have a fixed value (fieldnames like 'PRODUCT=', quote characters, end of line characters, etc.) or are spare fields. If this option is set to 1 then these kinds of fields will be filtered out when retrieving a record from a product file (using e.g. coda.fetch
). If this option is set to 0 then all fields will be returned.
The default value for this option is: 1
This option only effects the higher level CODA Python functions. The lower level functions do not perform filtering on record fields.
coda.get_option_filter_record_fields()
Retrieve the current setting for filtering of record fields.
See also coda.set_option_filter_record_fields(enable)
.
Low level CODA Data Types
Just as in the C interface the coda_product
, coda_type
, and coda_cursor
types are opaque types. This means that you can not print or inspect these types, but can only pass them around.
To create a new CODA Cursor there is a special coda.Cursor
class from which you instantiate new Cursor objects (these objects are opaque wrappers of the underlying cursors in the C domain). You can create a new cursors with: cursor = coda.Cursor()
. After creation you will have to initialize it using the coda.cursor_set_product
function (just like in C). It is possible to create a copy of a CODA Cursor by using a so-called deep copy:
import copy cursor = coda.Cursor() cursor2 = copy.deepcopy(cursor)
Low level CODA Functions
For a description of all low level CODA functions please consult the CODA C interface documentation. There are a few differences between the Python and C interface with respect to certain parameters and error handling. Below you will find an overview of the calling signature for each of the supported low level CODA functions. You'll notice that most differences are rather straightforward (parameters have moved from the parameter list to the list of return values or have been removed). If a change requires more explanation a comment is added to the function definition.
The low level CODA Python functions do not return error codes to indicate succes or failure (as is the case for the C functions). If an error condition occurs, an exception (of type coda.CodacError
) will be thrown. You can catch this exception using e.g.:
try: # call your CODA function(s) here except coda.CodacError, e: # handle CODA-specific exception print "ERROR: %s" % e
If you do not catch the exception, the error message will be printed to the console.
coda.init()
You do not have to call this function yourself to initialize CODA. When CODA is imported in Python the init
function will already be called for you. If, however, you call coda.done
at any time, you can use this function to re-initialize CODA again.
Note that the coda_set_definition_path
function is not provided in the CODA Python interface. You should use the CODA_DEFINITION environment variable to set the definition path as explained in the CODA Definition Path section.
coda.done()
If, for some reason, you want to unload the CODA package, you should first clean up CODA by calling this function. However, unloading a Python package is not a common activity, so you should rarely have to call coda.done()
.
coda.set_option_bypass_special_types(enable)
coda.get_option_bypass_special_types()
coda.set_option_perform_boundary_checks(enable)
coda.get_option_perform_boundary_checks()
coda.set_option_perform_conversions(enable)
coda.get_option_perform_conversions()
coda.set_option_use_fast_size_expressions(enable)
coda.get_option_use_fast_size_expressions()
coda.set_option_use_mmap(enable)
coda.get_option_use_mmap()
coda.NaN()
coda.isNaN(x)
coda.PlusInf()
coda.MinInf()
coda.isInf(x)
coda.isPlusInf(x)
coda.isMinInf(x)
coda.c_index_to_fortran_index(num_dims, dim, index)
coda.match_filefilter(filter, filepaths, callbackfunc)
The callbackfunc
parameter should be a Python function that accepts filepath
, status
and error
as parameters. For example:
>>> def findhelper(filepath, status, error): ... if status == coda.coda_ffs_match: ... print "File %s matches filter!" % filepath ... elif ((status == coda.coda_ffs_unsupported_file) ... or (status == coda.coda_ffs_no_match)): ... # don't print anything if the file does not positively match the filter ... pass ... else: ... print "ERROR: %s (%s)" % (error, filepath) ... return 0 >>> coda.match_filefilter('', '/home/codauser', findhelper)
[YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC] = coda.time_double_to_parts(datetime)
[YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC] = coda.time_double_to_parts_utc(datetime)
datetime = coda.time_parts_to_double(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC)
datetime = coda.time_parts_to_double_utc(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC)
str = coda.time_parts_to_string(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC, format)
[YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MUSEC] = coda.time_string_to_parts(format, str)
str = coda.time_double_to_string(datetime, format)
str = coda.time_double_to_string_utc(datetime, format)
datetime = coda.time_string_to_double(format, str)
datetime = coda.time_string_to_double_utc(format, str)
[file_size, format, product_class, product_type, version] = coda.recognize_file(filename)
pf = coda.open(filename)
The product file handle returned by this function can also be used with the high level CODA functions.
pf = coda.open_as(filename, product_class, product_type, version)
coda.close(pf)
filename = coda.get_product_filename(pf)
file_size = coda.get_product_file_size(pf)
format = coda.get_product_format(pf)
product_class = coda.get_product_class(pf)
product_type = coda.get_product_type(pf)
version = coda.get_product_version(pf)
type = coda.get_product_root_type(pf)
backend = coda.get_product_backend(pf)
value = coda.get_product_variable_value(pf, variable, index)
coda.type_get_format_name(type_class)
coda.type_get_class_name(type_class)
coda.type_get_native_type_name(native_type)
coda.type_get_special_type_name(special_type)
has_attributes = coda.type_has_attributes(type)
format = coda.type_get_format(type)
type_class = coda.type_get_class(type)
read_type = coda.type_get_read_type(type)
length = coda.type_get_string_length(type)
bit_size = coda.type_get_bit_size(type)
name = coda.type_get_name(type)
description = coda.type_get_description(type)
unit = coda.type_get_unit(type)
fixed_value = coda.type_get_fixed_value(type)
attributes = coda.type_get_attributes(type)
num_fields = coda.type_get_num_record_fields(type)
index = coda.type_get_record_field_index_from_name(type, name)
index = coda.type_get_record_field_index_from_real_name(type, real_name)
field_type = coda.type_get_record_field_type(type, index)
name = coda.type_get_record_field_name(type, index)
name = coda.type_get_record_field_real_name(type, index)
hidden = coda.type_get_record_field_hidden_status(type, index)
available = coda.type_get_record_field_available_status(type, index)
is_union = coda.type_get_record_union_status(type)
num_dims = coda.type_get_array_num_dims(type)
dim = coda.type_get_array_dim(type)
base_type = coda.type_get_array_base_type(type)
special_type = coda.type_get_special_type(type)
base_type = coda.type_get_special_base_type(type)
coda.cursor_set_product(cursor, product)
You can create a new cursor with cursor = coda.Cursor()
. With coda.cursor_set_product
this cursor can then be initialized to the root of a product. It is also possible to use a cursor as start
parameter in the high level CODA functions.
coda.cursor_goto(cursor, path)
coda.cursor_goto_first_record_field(cursor)
coda.cursor_goto_next_record_field(cursor)
coda.cursor_goto_record_field_by_index(cursor, index)
coda.cursor_goto_record_field_by_name(cursor, name)
coda.cursor_goto_available_union_field(cursor)
coda.cursor_goto_first_array_element(cursor)
coda.cursor_goto_next_array_element(cursor)
coda.cursor_goto_array_element(cursor, subs)
It is not needed to provide the number of dimensions as a parameter (num_subs
) since this value is determined from inspecting the length of subs
.
coda.cursor_goto_array_element_by_index(cursor, index)
coda.cursor_goto_attributes(cursor)
coda.cursor_goto_root(cursor)
coda.cursor_goto_parent(cursor)
coda.cursor_use_base_type_of_special_type(cursor)
has_ascii_content = coda.cursor_has_ascii_content(cursor)
has_attributes = coda.cursor_has_attributes(cursor)
length = coda.cursor_get_string_length(cursor)
bit_size = coda.cursor_get_bit_size(cursor)
byte_size = coda.cursor_get_byte_size(cursor)
num_elements = coda.cursor_get_num_elements(cursor)
pf = coda.cursor_get_product_file(cursor)
depth = coda.cursor_get_depth(cursor)
index = coda.cursor_get_index(cursor)
bit_offset = coda.cursor_get_file_bit_offset(cursor)
byte_offset = coda.cursor_get_file_byte_offset(cursor)
format = coda.cursor_get_format(cursor)
type_class = coda.cursor_get_type_class(cursor)
read_type = coda.cursor_get_read_type(cursor)
special_type = coda.cursor_get_special_type(cursor)
type = coda.cursor_get_type(cursor)
index = coda.cursor_get_record_field_index_from_name(cursor, name)
available = coda.cursor_get_record_field_available_status(cursor, index)
index = coda.cursor_get_available_union_field_index(cursor)
dim = coda.cursor_get_array_dim(cursor)
dst = coda.cursor_read_int8(cursor)
dst = coda.cursor_read_uint8(cursor)
dst = coda.cursor_read_int16(cursor)
dst = coda.cursor_read_uint16(cursor)
dst = coda.cursor_read_int32(cursor)
dst = coda.cursor_read_uint32(cursor)
dst = coda.cursor_read_int64(cursor)
dst = coda.cursor_read_uint64(cursor)
dst = coda.cursor_read_float(cursor)
dst = coda.cursor_read_double(cursor)
dst = coda.cursor_read_char(cursor)
Since Python does not have a native char type the character data will be returned as a string of length 1.
dst = coda.cursor_read_string(cursor)
dst = coda.cursor_read_bits(cursor, bit_offset, bit_length)
dst = coda.cursor_read_bytes(cursor, offset, length)
dst = coda.cursor_read_int8_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_uint8_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_int16_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_uint16_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_int32_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_uint32_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_int64_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_uint64_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_float_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_double_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
dst = coda.cursor_read_char_array(cursor)
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
Since Python does not have a native char type the character array will be returned as an array of signed 8 bit integers.
dst = coda.cursor_read_int8_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_uint8_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_int16_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_uint16_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_int32_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_uint32_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_int64_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_uint64_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_float_partial_array(curso, offset, lengthr)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_double_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
dst = coda.cursor_read_char_partial_array(cursor, offset, length)
CODA will always return the array data in a flat numpy array object using C array ordering.
Since Python does not have a native char type the character array will be returned as an array of signed 8 bit integers.
dst = coda.cursor_read_complex_double_pair()
dst = coda.cursor_read_complex_double_pairs_array()
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.
[dst_re, dst_im] = coda.cursor_read_complex_double_split()
[dst_re, dst_im] = coda.cursor_read_complex_double_split_array()
No array_ordering
parameter is required; CODA will always return the array data in a numpy array object using C array ordering.