cf-python home page

Go to the cf-python home page

Python cf module documention

An implementation of the CF data model.

Provide the objects necessary to represent a space according to the CF data model, with functions for I/O and basic manipulation.

Space structure

A space is composed as follows (ignoring attributes not composed from cf classes):

Space:
* cell methods
* ancillary variables
* grid:
* coordinates:
* coordinate bounds
* cell measures
* transforms

All components of a space are optional. Coordinates and cell measures are key values of the grid dictionary and all other components are attributes. Refer to each class’s documentation for details.

Data storage, access and manipulation

Some of the objects contain data arrays, namely all objects derived from the base class Variable (spaces, coordinates, coordinate bounds and cell measures). The data storage, access and manipulation model is very similar for all of them. In the following description, ‘variable’ may refer to any such object and it will be noted when certain objects (usually a space) exhibit different behaviour.

Copying and resizing

When a variable is indexed, a new variable is created which is a deep copy of the original but with the data array sliced according to given indices. For a space, the grid is also sliced by applying each dimension’s slice to the appropriate grid elements.

A variable uses similar indexing to a numpy array:

>>> isinstance(v, Variable)
True
>>> v.shape
(10, 20, 30)
>>> v[0:5,...,slice(0,11,2)].shape
(5, 20, 6)
>>> v.shape
(10, 20, 30)
>>> v[:,:,numpy.arange(30) < 4].shape
(10, 20, 4)

The only differences to numpy array indexing are:

  1. More than one dimension’s slice may be a boolean array.
  2. A tuple (as well as a list) of indices for a single dimension is allowed.

Dimensions of size one resulting from a slice are discarded for a space but retained for other types:

>>> s
<CF Space: air_temperature(7070, 64, 128)>
>>> s[0]
<CF Space: air_temperature(64, 128)>
>>> c
<CF Coordinate: latitude(10, 20)>
>>> c[0]
<CF Coordinate: latitude(1, 20)>

Scalar data are not allowed in any case and are stored instead as size 1 arrays with at least one dimension:

>>> s[0,0,0]
<CF Space: air_temperature(1)>
>>> c[-1,-1]
<CF Coordinate: latitude(1, 1)>

Creating a deep copy of a variable

Indexing a variable with an ellipsis or equivalent reads the data from disk (if required) before returning a deep copy of itself, and so both the original variable and its copy contain different numpy array objects in memory. It is, however, also possible to create a deep copy of a variable without altering the type of its data from a file pointer to a numpy array, which can enhance performance (see the ‘Storage’ section). There are three equivalent ways of doing this:

>>> w = v()
>>> w = v.copy()
>>> w = copy.deepcopy(v)

The difference between copying by one of these methods and copying by indexing is as follows:

>>> v.type
<type 'netCDF4.Variable'>
>>> w = v()
>>> w.type, v.type
(<type 'netCDF4.Variable'>, <type 'netCDF4.Variable'>)
>>> w = v[...]
>>> w.type, v.type
(<type 'numpy.ndarray'>, <type 'numpy.ndarray'>)

Note that a call to a space also allows arguments which create a slice of the space according to conditions on its coordinate values.

Assignment to the data array

Assignment to the data array is done with the same indexing as for data access. Assignment can not change the shape of the data:

>>> v.shape
(10, 20, 30)
>>> v[0, 0, 0] = 273.15
>>> v.varray[0,0,0]
273.15
>>> v[0] = 273.15
>>> v.shape
(10, 20, 30)
>>> v[1,2,:] = range(30)
>>> v[...] = numpy.arange(6000).reshape(10, 20, 30)

Storage

The data may be stored as a numpy array or as a file pointer in the form of a netCDF4.Variable instance. Accessing the data makes no distinction between the two storage methods, but there are I/O, memory and speed performance issues to consider. If the data are a file pointer then accessing any part of the array will cause the entire array to be read from disk into memory and stored as a numpy array, replacing the original file pointer. Refer to read and write.

A variable’s repoint method will revert the data back to a file pointer if possible, freeing memory if no other variables are referring to the numpy array (or arrays).

Operator overloading for variables

The following operators, operations and assignments are overloaded in a variable to apply element-wise to the variable’s data numpy array.

Comparison operators:

==, !=, >, <, >=, <=

Binary arithmetic operations:

+, -, *, /, //, %, pow(), **, &, ^, |

Unary arithmetic operations:

-, +, abs(), ~

Augmented arithmetic assignments:

+=, -=, *=, /=, //=, %=, **=, &=, ^=, |=

Either side of an arithmetic operation may be a variable (in which case its data as a numpy array are used) or any object allowed by the equivalent numpy operation. Note that, as usual, if the left hand side object supports the operation with the right hand side then the returned object will be of former’s type:

>>> type(v)
<class 'cf.space.Space'>
>>> type(2.5 + v)
<class 'cf.space.Space'>
>>> type(v.varray + v)
<type 'numpy.ndarray'>

Apart from this exception, the unary and binary arithmetic operations return a new variable with modified data. The augmented arithmetic assignments change a variable’s data an in-place.

Numeric equalities for the == and != comparisons are determined to within a tolerance which may be adjusted.

Tolerance of numeric equality

All objects defined in cf have an equals method which determines the congruence of two instances. The aspects of this equality vary between objects, but for all objects numeric equalities are tested to within a tolerance defined by parameters ‘rtol’ (relative tolerance) and ‘atol’ (absolute tolerance), where two numbers a and b (from the left and right sides respectively of the comparison) are considered equal if:

|a - b| <= atol + rtol*|b|

Default tolerances are taken from the parameters DEFAULT_RTOL and DEFAULT_ATOL.

The values of these parameters may be overridden for any object by setting its _rtol and _atol attributes for relative and absolute tolerances respectively. Either of these attributes may be non-existent or None, which generally results in the default parameters being used. The only exception to this is if the object is an element of a CF iterable (see below).

In a comparison between objects x and y (x==y, for example), the values used for relative and absolute tolerances are the first found from each column of the following table:

Relative tolerance Absolute tolerance
x._rtol x._atol
y._rtol y._atol
DEFAULT_RTOL DEFAULT_ATOL

If the _rtol and _atol attributes of iterable CF objects (those subclassed from CfDict or CfList, such as SpaceList) exist, then they are used if and only if an element does not have its own numeric value of a tolerance parameter.

Grid structure

A grid contains any number of dimension coordinate constructs (or ‘dimensions’ for short), auxiliary coordinate constructs, cell measure constructs and transforms.

The grid must have dimensionality attributes (even if they are null, i.e. empty dictionaries) but all other components are optional.

Dimensionality

The dimensions of the grid, and of its associated space, are given by the dimension_sizes and dimension attributes.

The dimension_sizes attribute is a dictionary whose keys are dimension identifiers and values are positive integer dimension sizes. A dimension identifier is the string ‘dim’ suffixed by an arbitrary, but unique, integer. For example:

>>> g
<CF Grid: (30, 24, 1, 17)>
>>> g.dimension_sizes
{'dim0': 1, 'dim1': 17, 'dim2': 30, 'dim3': 24}

The dimension attribute specifies which dimensions relate to each grid component (coordinates and cell measures) and to the data array of the space which holds the grid. For example:

>>> g.dimensions
{'data': ['dim1', 'dim2', 'dim3'],
 'aux0': ['dim2', 'dim3'],
 'aux1': ['dim2', 'dim3'],
 'cm0' : ['dim2', 'dim3'],
 'dim0': ['dim0'],
 'dim1': ['dim1'],
 'dim2': ['dim2'],
 'dim3': ['dim3']}

Each value of this dictionary is an ordered list which corresponds to the shape of each component.

Note that if the grid contains dimensions of size 1 then the data of the space may have a lesser dimensionality then the grid (see the ‘data’ key in the above example).

Note that it is possible for a grid dimension to have no associated grid components.

Storage of coordinates and cell measures variables

Keys of the grid are identifiers for each of grid’s coordinate and cell measures variables. The key values are the variable objects themselves. The key names are strings which describe which type of variable they store. Recognized types are:

aux : Auxiliary coordinate cm : Cell measures dim : Dimension coordinate

A key string comprises one of these prefixes followed by a non-negative integer to discern between grid components of the same type. For example, a grid’s keys may be:

>>> sorted(g.keys())
['aux0', 'aux1', 'cm0', 'dim0', 'dim1', 'dim2', 'dim3']

The non-negative integer suffixing each key is arbitrary but must be unique amongst keys of the same type.

Note that, similarly to the storage of dimension coordinates in a netCDF file, a dimension coordinate’s dimension name must be the same as its grid identifier.

Transforms

If a coordinate has an associated transform (such as projection parameters or a derived dimensional coordinate), then it will have a transform attribute whose value is a key of the grid’s transform attribute, which in turn is a CfDcit object (cf dictionary). The values of this cf dictionary are Transform objects which contain the information required to realise the transform. For example:

>>> g.transform['atmosphere_sigma_coordinate']
<CF Transform: atmosphere_sigma_coordinate transform>
>>> dump(g.transform['atmosphere_sigma_coordinate'])
atmosphere_sigma_coordinate transform
-------------------------------------
Transform['ps'] = <CF Space: surface_air_pressure(30, 24)>
Transform['ptop'] = <CF Space: ptop(1)>
Transform['sigma'] = 'dim0'

Virtual coordinates

If a coordinate has a transform attribute but no data and no specified dimensions (i.e. its entry in the grid’s dimensions dictionary attribute is an empty list) then it should be considered a container for the coordinate implied by the named transform, should it be realized.

Classes

Class (parent class) Description
Variable(object) Base class
Space(Variable) Space
Coordinate(Variable) Dimension or auxiliary coordinate
CoordinateBounds(Variable) Coordinate bounds
CellMeasures(Variable) Cell measures
CfDict(MutableMapping)) Base class
Grid(CfDict)) Grid
Transform(CfDict)) Coordinate transforms
CfList(MutableSequence)) Base class
VariableList(CfList)) List of variables
SpaceList(VariableList) List of spaces
CellMethods(CfList)) Cell methods
Comparison(object) Comparison expression

Analogously to the above diagram of a space, the composition of a space may be cast in terms of the class of each space element type:

Space:
* CellMethods
* SpaceList
* Grid:
* Coordinate:
* CoordinateBounds
* CellMeasures
* Transform

Note

Ancillary variables are stored in a SpaceList object

Functions

Function Description
dump Print the string returned from an object’s dump method.
eq Create a Comparison object.
equals Determine whether two objects are congruent.
ge Create a Comparison object.
gt Create a Comparison object.
inside Create a Comparison object.
le Create a Comparison object.
lt Create a Comparison object.
ne Create a Comparison object.
outside Create a Comparison object.
read Read spaces from netCDF files.
read1 Read a space from netCDF files.
write Write spaces to a netCDF file.

Function documentation

dump

cf.dump(x, id=None, nc=False, omit=())

Print the full description of an object using its dump method.

This function is exactly equivalent to:

>>> print x.dump(...)

or, if the object has no dump method:

>>> print x

The parameters are only used if the object’s dump method is being utilized.

Parameters:
  • x (object) – Optional. The object to print.
  • id (str) – Optional. Set the common prefix of object’s component names, as for the object’s dump method.
  • nc (bool) – Optional. If True then include attributes whose names begin ‘nc’.
  • omit (sequence) – Optional. Omit the given attributes from a objects’s description.
Returns:

None

eq

cf.eq(value)

Create a Comparison object for evaluating whether a variable is equal to the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “eq” and whose value attribute set to the value parameter.

equals

cf.equals(x, y, rtol=None, atol=None, override=True)

Ascertain if x equals y, assuming that they’re 1) space components, 2) numbers or 3) anything else, in that order.

dch

ge

cf.ge(value)

Create a Comparison object for evaluating whether a variable is greater than or equal to the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “ge” and whose value attribute set to the value parameter.

gt

cf.gt(value)

Create a Comparison object for evaluating whether a variable is greater than the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “gt” and whose value attribute set to the value parameter.

inside

cf.inside(value0, value1)

Create a Comparison object for evaluating whether a variable is inside the given range.

Parameters:
  • value0 (object) – The first element of value attribute tuple in the returned Comparison object.
  • value1 (object) – The second element of value attribute tuple in the returned Comparison object.
Returns:

A Comparison object whose relation attribute is set to the string “inside” and whose value attribute set to the tuple (value0, value1).

le

cf.le(value)

Create a Comparison object for evaluating whether a variable is less than or equal to the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “le” and whose value attribute set to the value parameter.

lt

cf.lt(value)

Create a Comparison object for evaluating whether a variable is less than the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “lt” and whose value attribute set to the value parameter.

ne

cf.ne(value)

Create a Comparison object for evaluating whether a variable is not equal to the given value.

Parameters:value (object) – The value attribute in the returned Comparison object.
Returns:A Comparison object whose relation attribute is set to the string “ne” and whose value attribute set to the value parameter.

outside

cf.outside(value0, value1)

Create a Comparison object for evaluating whether a variable is outside the given range.

Parameters:
  • value0 (object) – The first element of value attribute tuple in the returned Comparison object.
  • value1 (object) – The second element of value attribute tuple in the returned Comparison object.
Returns:

A Comparison object whose relation attribute is set to the string “outside” and whose value attribute set to the tuple (value0, value1).

read

cf.read(files, data=False, verbose=False, index=None, exact=False, **kwargs)

Read spaces from netCDF files.

By default, all data arrays from all space components are not read from disk but are stored in the returned spaces as file pointers. This behaviour may be changed with the data parameter.

The netCDF dimension names are stored in the nc_dimensions attribute of a space’s grid and the netCDF variable names are stored in the ncvar attributes of the space and its grid components (coordinates, coordinate bounds, cell measures and transformations).

Note that if a single space has been requested by setting the index parameter and that space contains other spaces as part of a coordinate transformation or in a list ancillary variables, then the file in which the returned space resides will be scanned in its entirety so that these supplementary spaces may be located stored in the returned space.

Parameters:
  • files (str or sequence) – A string or sequence of strings giving the netCDF file names from which to read spaces The file names may contain shell-style wild cards (as understood by the glob module).
  • data (bool) – Optional. If True then store the data of each space, including the data of its grid components, as numpy arrays. Otherwise store the data as file pointers. Note that ancillary and transform spaces within a space are always returned with file pointers to their data.
  • exact (bool) – Optional. If True then interpret the keywords from **kwargs as exact phenomenon names. Refer to Space.extract for details.
  • index (None or int) – Optional. If None then return all possible spaces from the input files. If a non-negative integer then only return the space with this index in the otherwise full list of spaces. The index position applies to those spaces which have been selected with the **kwargs criteria.
  • verbose (bool) – Optional. If True then print information to stdout.
  • **kwargs

    Optional. Other keyword arguments defining space selection based on phenomena criteria. If none are set then all spaces are selected. Keywords and their arguments are set as for Space.extract. If the data are being read from disk (the data parameter is True), then applying selection criteria as arguments to read, as opposed to the functionally equivalent method of using the extract method on the returned list of spaces, may be more memory efficient.

Returns:

A list of spaces (a SpaceList object) or, if and only if the index parameter is set to a non-negative integer, a space (Space object).

See also

read1

Examples

>>> s = read('file.nc')
>>> type(s)
<class 'cf.space.SpaceList'>
>>> s
[<CF Space: pmsl(30, 24)>,
 <CF Space: z-squared(17, 30, 24)>,
 <CF Space: temperature(17, 30, 24)>,
 <CF Space: temperature_wind(17, 29, 24)>]
>>> read('file.nc')[0:2]
[<CF Space: pmsl(30, 24)>,
 <CF Space: z-squared(17, 30, 24)>]
>>> read('file.nc', units='K')
[<CF Space: temperature(17, 30, 24)>,
 <CF Space: temperature_wind(17, 29, 24)>]
>>> read('file.nc').extract(units='K')
[<CF Space: temperature(17, 30, 24)>,
 <CF Space: temperature_wind(17, 29, 24)>]

Note

read('file.nc', index=1) returns a list of spaces with one element whilst read('file.nc')[1] returns a space.

>>> read('file.nc', index=0)
[<CF Space: pmsl(30, 24)>]
>>> read('file.nc')[0]
<CF Space: pmsl(30, 24)>
>>> read1('file.nc')
<CF Space: pmsl(30, 24)>
>>> read('file.nc').units
['Pa', 'm2', 'K', 'K']   

See also

Space.extract for more examples of space selection by phenomena criteria

read1

cf.read1(files, data=False, verbose=False, exact=False, **kwargs)

Read the first matching space from netCDF files.

This function is exactly equivalent to calling the read function with the index argument set to 0. Refer to read for more details.

Note that if the first space contains other spaces as part of a coordinate transform or in a list ancillary variables, then these supplementary spaces will also be read from the file.

:argument files : Refer to read

:parameter data : Optional. Refer to read

Parameters:
  • exact (bool) – Optional. Refer to read
  • verbose (bool) – Optional. Refer to read
  • **kwargs

    Optional. Refer to read

Returns:

A space.

See also

read

Examples

>>> from cf import read, read1
>>> read('file.nc')
[<CF Space: pmsl(30, 24)>,
 <CF Space: z-squared(17, 30, 24)>,
 <CF Space: temperature(17, 30, 24)>
 <CF Space: temperature_wind(17, 29, 24)>]
>>> read1('file.nc')
<CF Space: pmsl(30, 24)>
>>> read1('file.nc').extract(units='Pa')
True
>>> read1('file.nc').extract(units='K')
False
>>> read1('file.nc', units='K')
<CF Space: temperature(17, 30, 24)>

write

cf.write(spaces, file, format='NETCDF3_CLASSIC', data=False, verbose=False, exact=False, **kwargs)

Write a space, or sequence of spaces, to a CF compliant netCDF file.

NetCDF dimension and variable names will be taken from the attributes ncvar and nc_dimensions if present, otherwise they are inferred from standard_name attributes or set to defaults. NetCDF names may be automatically given a numerical suffix to avoid duplication.

Output netCDF file global attributes are those which occur in the set of CF global attributes and non-standard data variable attributes and which have equal values across all input spaces.

It is possible to write only a subset of the input spaces by providing phenomena conditions with **kwargs parameters.

It does not matter if an input space’s data has not yet been read into memory. The write operation will automatically read the data array from disk if required, but the default is for it not to be saved in the input space if it had to be read during the write operation. However, if the data argument equals True then any input spaces which didn’t contain their full data arrays in memory will be changed in place so that they do. For example, for a space, s:

>>> s.type
    <type 'netCDF4.Variable'>
>>> write(s, 'filename')
>>> s.type
    <type 'netCDF4.Variable'>
>>> write(s, 'filename', data=True)
>>> s.type
    <type 'numpy.ndarray'>

Identical grid components (as determined by their numerically tolerant equals methods) are only written to the file once, apart from when they need to fulfil both dimension coordinate and auxiliary coordinate roles for different data variables.

If a coordinate with a transform attribute is fully specified by another coordinate’s transformation then it will be not written to the file.

Parameters:
  • spaces (Space or sequence of spaces) – The space or spaces to write to the file. If **kwargs are set for selection by phenomena then some spaces may not be written.
  • file (str) – The netCDF file to write spaces to.
  • data (bool) – Optional. If True then ensure that spaces which have been written to disk retain their data in-place as numpy arrays.
  • exact (bool) – Optional. If True then interpret the keywords from **kwargs as exact phenomenon names. Refer to Space.extract for details.
  • format (str) – Optional. The format of the output netCDF file. Valid formats are those supported by the netCDF4 module: “NETCDF3_CLASSIC”, “NETCDF3_64BIT”, “NETCDF4_CLASSIC” and “NETCDF4”. The default is “NETCDF3_CLASSIC”. Certain write operations may be considerably faster using ‘NETCDF4_CLASSIC’.
  • verbose (bool) – Optional. If True then print information to stdout.
  • **kwargs

    Optional. Other keyword arguments defining space selection based on phenomena criteria. Only selected spaces will be written to disk. If none are set then all spaces are selected. Keywords and their arguments are set as for Space.extract. This behaviour is modified by the exact keyword.

Examples

>>> s
[<CF Space: air_pressure(30, 24)>,
 <CF Space: u_compnt_of_wind(19, 29, 24)>,
 <CF Space: v_compnt_of_wind(19, 29, 24)>,
 <CF Space: potential_temperature(19, 30, 24)>]
>>> write(s, 'file')
>>> write(s, 'file', standard_name = 'air_pressure')

See also

Space.extract for more examples of space selection by phenomena criteria.

Class documentation

Cell measures

class cf.space.CellMeasures(**kwargs)

Bases: cf.space.Variable

A CF cell measures object.

Refer to Variable for all details.

Parameters:**kwargs

Optional. refer to Variable

Overloaded operators

Refer to Variable.

Methods

Refer to Variable.

Cell methods

class cf.space.CellMethods(sequence=())

Bases: cf.space.CfList

A CF cell methods object to describe the characteristic of a field that is represented by cell values.

Each cell method is stored in a dictionary and these dictionaries are stored in a list-like object. Similarly to a CF ‘cell_methods’ string, the order of cell methods in the list is important.

The dictionary representing each cell method recognizes the following keys (where all keys are optional; the referenced sections are from NetCDF Climate and Forecast (CF) Metadata Conventions Version 1.5; and words in double quotes (”...”) refer to CF cell_methods components in the referenced sections):

Key Value
method A “method” component. Refer to section 7.3.
name

A list of all of the “name” components involved with the “method” component. Each element is either:

  1. The standard_name of the cell method’s dimension
  2. The string ‘area’
  3. A netCDF variable name or netCDF dimension name

Option 3. only occurs if the cell method’s dimension either has no dimension coordinate or has a dimension coordinate with no standard_name.

This is a deviation from the “name” components as described in sections 7.3, 7.3.1 and 7.3.4, in that standard_names are stored whenever possible, rather than only in the special cases described in section 7.3.4. Inspection of the ‘no_coords’ list retains the information required to fully interpret the cell method.

dim A list of space dimension names corresponding to the ‘name’ list. A ‘name’ list value of ‘area’ always corresponds to a ‘dim’ list value of None.
no_coords A list of booleans, corresponding to the ‘name’ list, indicating whether or not a particular cell method is relevant to the data in a way which may not be precisely defined by the corresponding dimension or dimensions. Refer to section 7.3.4.
interval A list of “interval” component values corresponding to the ‘name’ list. Refer to section 7.3.2.
units A list of “interval” component units corresponding to the ‘interval’ list. Refer to section 7.3.2.
comment A “comment” or non-standardised information, component. Refer to sections 7.3.2 and 7.4
within A “within” climatology component. Refer to section 7.4.
over An “over” cell portion or “over” climatology component. Refer to sections 7.3.3 and 7.4.
where A “where” component. Refer to section 7.3.3.

Note that the above table assumes that the cell methods have been constructed in the context of a space, the only way in which the ‘dim’ and ‘no_coords’ keys may take sensible values. If this is not the case, as would happen if the parse method were called without its space keyword, then elements of the ‘dim’ key default to False and elements of the ‘no_coords’ key default to None.

Parameters:sequence (sequence) – Optional. Initialize new list from sequence’s items.

Examples

>>> c = CellMethods()
>>> c = c.parse('time: minimum within years time: mean over years (ENSO years)')    
>>> print(c)
Cell methods    : time: minimum within years
                  time: mean over years (ENSO years)
>>> list(c)
[
 {'method'   : 'minimum',
  'name'     : ['time'],     
  'dim'      : [False],
  'no_coords': [None], 
  'within'   : 'years'
 },
 {'method'   : 'mean'
  'name'     : ['time'],
  'dim'      : [False], 
  'no_coords': [None],     
  'comment'  : 'ENSO years', 
  'over'     : 'years'
 }
]
>>> c = c.parse('lat: lon: standard_deviation')
>>> list(c)
[
 {'dim'      : [False, False],
  'method'   : 'standard_deviation'
  'name'     : ['lat', 'lon'],
  'no_coords': [None, None],
 }
]

Overloaded operators

Refer to CfList.

Attribute (type) Description
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_elements_atts (bool) If True then do not raise an exception when requesting a non-existent attribute, but broadcast the request to each element of the list.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Method Description
append As for a built-in list.
copy Create a deep copy.
count As for a built-in list but using numerically tolerant equality.
dump Return a string containing a full description of the object.
equals Determine whether two lists are congruent element-wise.
extend As for a built-in list.
index As for a built-in list but using numerically tolerant equality.
insert As for a built-in list.
parse Parse a netCDF(CF) cell_methods string.
pop As for a built-in list.
properties Return a set of writable, public attributes.
remove As for a built-in list.
reverse As for a built-in list.
sort As for a built-in list.
strings Convert each element to a string.
dump(id=None, nc=False, omit=())

Return a string containing a full description of the cell methods.

If a cell methods ‘name’ is followed by a ‘*’ then that cell method is relevant to the data in a way which may not be precisely defined its corresponding dimension or dimensions.

Parameters:
  • id (str) – Optional. Set the common prefix of each line of the output. If None then defaults to the class name.
  • nc (bool) – Optional. Dummy argument required for consistency with the dump function.
  • omit (sequence) – Optional. Dummy argument required for consistency with the dump function.
Returns:

A string containing the description of the cell methods.

See also

dump

parse(string, space=None)

Parse a CF cell_methods string into this CellMethods instance.

Parameters:
  • string (str) – The CF cell_methods string to be parsed into the CellMethods object.
  • space (Space) – Optional. Set the ‘name’, ‘dim’ and ‘no_coord’ key values appropriately for the given space. Refer to CellMethods.
Returns:

A cell methods list.

Examples

>>> c = CellMethods()
>>> c = c.parse('time: minimum within years time: mean over years (ENSO years)')    
>>> print c
Cell methods    : time: minimum within years
                  time: mean over years (ENSO years)
strings()

Convert each element of the cell methods list to a netCDF(CF) cell_methods-like string.

Note that if the intention is to concatenate the output list into a string for creating a netCDF(CF) cell_methods attribute, then the cell methods “name” components may need to be modified, where appropriate, to reflect netCDF variable names. This is done automatically by the write function when creating a file on disk.

Returns:A built-in list of cell method strings.

Examples

>>> c = CellMethods()
>>> c = c.parse('time: minimum within years time: mean over years (ENSO years)')    
>>> print c
Cell methods    : time: minimum within years
                  time: mean over years (ENSO years)
>>> c.strings()
['time: minimum within years',
 'time: mean over years (ENSO years)']

Cf dictionary

class cf.space.CfDict(*args, **kwargs)

Bases: _abcoll.MutableMapping

A dictionary-like object (a ‘cf dictionary’) suitable for storing CF structures.

Parameters:
  • *args

    Keys and values are initialized exactly as for a built-in dict.

  • **kwargs

    Keys and values are initialized exactly as for a built-in dict.

Attribute (type) Description
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
clear As for a built-in dict.
copy Create a deep copy.
equals Determine whether two instances are congruent with each other.
get As for a built-in dict.
get_keys Return grid keys which match a pattern.
has_key As for a built-in dict but using numerically tolerant equality.
items As for a built-in dict.
iteritems As for a built-in dict.
iterkeys As for a built-in dict.
itervalues As for a built-in dict.
keys As for a built-in dict.
pop As for a built-in dict.
popitem As for a built-in dict.
properties Return a set of writable, public attributes.
setdefault As for a built-in dict.
update As for a built-in dict.
values As for a built-in dict.

Overloaded operators

The in (set membership) operator is overloaded to use numerically tolerant equality.

equals(other, rtol=None, atol=None, override=True)

Return True if two instances are congruent in that they have the equal sets of keys and for each key, the two values are equal.

Equality of numbers is to within a tolerance. Refer to cf for details.

Parameters:
  • other (object) – The variable to compare against for equality.
  • atol (None or float) – Optional. If None then use the default method for setting the absolute tolerance for equality of real numbers (refer to cf for details). If a float then set the absolute tolerance to this value for all comparisons (refer to the override parameter).
  • override (bool) – Optional. If False then only use a float value of the rtol or atol parameters if it can not be set from attributes of the objects being compared.
  • rtol (None or float) – Optional. If None then use the default method for setting the relative tolerance for equality of real numbers (refer to cf for details). If a float then set the relative tolerance to this value for all comparisons (refer to the override parameter).
Returns:

True if the two objects are congruent, False otherwise.

get_keys(regex)

Return a list of the cf dictionary’s key names which, at their beginning, match the given regular expression.

Parameters:regex (str) – The regular expression with which to identify key names. A leading ‘^’ special character is assumed if not given.
Returns:A list of keys names.

Examples

>>> d.keys()
['dim2', 'dim0', 'dim1', 'aux0', 'cm0']
>>> d.get_keys('dim')
['dim2', 'dim0', 'dim1']
>>> d.get_keys('aux|dim')
['dim2', 'dim0', 'dim1', 'aux0']
>>> d.get_keys('dim[12]')
['dim2', 'dim1']
has_key(key)
Returns:True if the key exists, using numerically tolerant equality.
properties(nc=False)

Return a set of writable, public attribute names, excluding methods.

An attribute created by assigning directly to the __dict__ dictionary (as opposed to using setattr) will not appear in this set.

Parameters:nc (bool) – If False then exclude attributes whose names begin “nc”.
Returns:A set of attribute names.

Cf list

class cf.space.CfList(sequence=())

Bases: _abcoll.MutableSequence

A list-like object (a ‘cf list’) suitable for CF structures.

When requesting a non-private attribute that the cf list does not have, and the _elements_atts attribute is True, instead of raising an AttributeError a cf list is returned containing the values of the attribute from each of the elements, with a value of None if an element does not have the attribute. The setting of an attribute is as usual, i.e. it is set on the object itself rather that the object’s elements.

Parameters:sequence (iterable) – Optional. Initialize new list from sequence’s items.

Overloaded operators

The in (set membership), == and != operators are overloaded to use numerically tolerant equality.

The +, *, += and *= operators behave as for a built-in list. Note in particular that in-place changes to an element of the result of one of these operations will affect the equivalent element on the right hand side, and possibly other equivalent elements in the result itself.

Attribute (type) Description
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_elements_atts (bool) If True then do not raise an exception when requesting a non-existent attribute, but broadcast the request to each element of the list.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods  
append As for a built-in list.
copy Create a deep copy.
count As for a built-in list but using numerically tolerant equality.
equals Determine whether two lists are congruent element-wise.
extend As for a built-in list.
index As for a built-in list but using numerically tolerant equality.
insert As for a built-in list.
pop As for a built-in list.
properties Return a set of writable, public attributes.
remove As for a built-in list.
reverse As for a built-in list.
sort As for a built-in list.
copy()

Create a deep copy of the list. Equivalent to calling copy.deepcopy on the list.

Returns:A deep copy of the list.
count(value)
Returns:The number of occurrences of value using numerically tolerant equality.
equals(other, rtol=None, atol=None, override=True)

Return True if two instances are congruent in that each pair of their elements are equal.

Equality of numbers is to within a tolerance. Refer to cf for details.

Parameters:
  • other (object) – The variable to compare against for equality.
  • atol (None or float) – Optional. If None then use the default method for setting the absolute tolerance for equality of real numbers (refer to cf for details). If a float then set the absolute tolerance to this value for all comparisons (refer to the override parameter).
  • override (bool) – Optional. If False then only use a float value of the rtol or atol parameters if it can not be set from attributes of the objects being compared.
  • rtol (None or float) – Optional. If None then use the default method for setting the relative tolerance for equality of real numbers (refer to cf for details). If a float then set the relative tolerance to this value for all comparisons (refer to the override parameter).
Returns:

True if the two objects are congruent, False otherwise.

index(value, start=0, stop=None)
Returns:The first index of value using numerically tolerant equality. Restrict the search to the slice ‘start:stop’. If stop is None then the slice is ‘start:’.
insert(index, object)

L.insert(index, object) – insert object before index

properties(nc=False)

Return a set of writable, public attribute names, excluding methods.

An attribute created by assigning directly to the __dict__ dictionary (as opposed to using setattr) will not appear in this set.

Parameters:nc (bool) – If False then exclude attributes whose names begin “nc”.
Returns:A set of attribute names.

Comparison

class cf.utils.Comparison(**kwargs)

Bases: object

Create an object for storing a comparison expression.

A comparison expression comprises a relation (such as ‘less than’) and a value (any object capable of being related to with the standard comparison operators). These are stored in the relation and value attributes respectively.

The comparison is evaluated for an arbitrary object with the evaluate method.

Valid relations are:

Relation Description
lt Strictly less than
le Less than or equal
gt Strictly greater than
ge Greater than or equal
eq Equals (within a tolerance)
ne Not equal to (within a tolerance)
inside Inside a given range (range bounds included)
outside Outside a given range (range bounds excluded)
Parameters:**kwargs

A keyword argument to the call is the relation of the comparison (any one of the relations listed above). The keyword’s argument is the object to test the comparison against. If the keyword argument is “inside” or “outside” then the value should be a two-element list or tuple.

Attribute (type) Description
relation
(str) A string definging the comparison operator. One
of relations given above.
value (object) The object with which to compare.
Methods  
dump Return a string containing a full description of the object.
evaluate Evaluate the comparison for a given variable.

Examples

>>> c = Comparison(le = 5)
>>> c.evaluate(4)
True
>>> c.evaluate(5)
True
>>> c = lt(5)
>>> c.evaluate(4)
True
>>> c = Comparison(inside = (1,2))
>>> a = numpy.arange(4)
>>> c.evaluate(a)
array([False,  True,  True, False], dtype=bool)
>>> (a >= 1) & (a <= 2)
array([False,  True,  True, False], dtype=bool)
evaluate(x)

Evaluate the comparison for a given object.

x : object

Typically a boolean or array of booleans. More generally, the output from the appropriate relation operators (==, !=, <, >, <=, >=) for the given variable, x.

Coordinate

class cf.space.Coordinate(**kwargs)

Bases: cf.space.Variable

A CF coordinate object.

Refer to Variable, Grid and the cf module for details.

Parameters:**kwargs

The new variable is given attributes named by the keywords, whose values are deep copies the keyword values.

Attributes (type) Description
array (numpy array) A numpy array deep copy of the data.
bounds (CoordinateBounds) Optional The coordinate’s cell boundaries.
climatology (CoordinateBounds) Optional. A time coordinate’s climatological cell boundaries.
compress (VariableList) Optional List of coordinates which are compressed by gathering into this coordinate’s single dimension.
dtype (numpy.dtype) Describes the format of the elements in the data array (refer to numpy.ndarray.dtype).
ncvar (string) Optional. Contains the name of the variable in the original netCDF file. If present when the space is written to a netCDF file, then used for the output netCDF variable name.
ndim (int) The data array’s number of dimensions (refer to numpy.ndarray.ndim).
shape (tuple of ints) The shape of the data array (refer to numpy.ndarray.shape).
size (int) The number of elements in the data array (refer to numpy.ndarray.size).
transform (str) Optional. The key of a grid’s transform attribute which contains a transformation for this coordinate.
type (type) The type of the data object, which is either a numpy array or a file pointer.
varray (numpy array view) A numpy view of the data.
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
copy Create a copy.
dump Return a string containing a full description of the coordinate.
equals Determine whether two coordinates are congruent with each other.
extract Determine whether a coordinate matches phenomena criteria.
first_datum Return the first element of the data without replacing a file pointer with a numpy array.
last_datum Return the last element of the data without replacing a file pointer with a numpy array.
name Return the name (standard_name, long_name or ncvar).
ncatts Return the set of attributes which are netCDF attributes.
properties Return a set of writable, public attributes.
repoint Revert the data to a file pointer.
dump(nc=False, id=None, omit=())

Return a string containing a full description of the coordinate.

Parameters:
  • id (str) – Optional. Set the common prefix of component names. If None then defaults to the class name.
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes from the description.
Returns:

A string containing the description of the coordinate.

See also

dump

ncatts()

Return a set of attributes which are netCDF attributes suitable for writing to a netCDF file.

All writable attributes are considered netCDF attributes except those starting with an underscore; (with the exception of ‘_FillValue’); those starting with “nc”; and the bounds, climatology and transform attributes.

To create an attribute which will not be in the set of netCDF attributes, assign directly to the __dict__ dictionary as opposed to using setattr.

Returns:A set of attributes which are netCDF attributes.
repoint(test=False)

Reset the data of all coordinate components to a file pointers if they are all available, regardless of of whether the data are currently stored as a numpy arrays or not.

This may free memory, but any changes to data stored as a numpy arrays will be lost.

Parameters:test (bool) – Optional. If True then do not reinstate a file pointers if it exists.
Returns:True if all file pointers of all coordinate components exist, False otherwise.

Coordinate bounds

class cf.space.CoordinateBounds(**kwargs)

Bases: cf.space.Variable

A CF coordinate bounds object.

Refer to Variable for all details.

Parameters:**kwargs

Optional. refer to Variable

Overloaded operators

Refer to Variable.

Methods

Refer to Variable.

Grid

class cf.space.Grid(*args, **kwargs)

Bases: cf.space.CfDict

A CF grid object defining a space’s dimensionality, coordinates, cell measures and transformations in a dictionary-like object.

Parameters:
  • *args

    Keys and values are initialized exactly as for a built-in dict.

  • **kwargs

    Keys and values are initialized exactly as for a built-in dict.

Overloaded operators

The in (set membership) operator is overloaded to use numerically tolerant equality.

Attribute (type) Description
dimension_sizes (dict) See the ‘Grid dimensionality’ section.
dimensions (dict) See the ‘Grid dimensionality’ section.
nc_dimensions (dict) Optional. The netCDF dimension name for each dimension in the dimension_sizes attribute. If present when the grid is written to a netCDF file, then used for output netCDF dimension names.
transform (dict) Optional. A cf dictionary of Transform objects referred to by grid coordinates with a transform attribute. See the ‘Transforms’ section for more details.
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
clear As for a built-in dict.
coord : Return a coordinate identified by its name.
copy Create a deep copy.
equals Determine whether two instances are congruent with each other.
get As for a built-in dict.
get_keys Return grid keys which match a pattern.
has_key As for a built-in dict but using numerically tolerant equality.
items As for a built-in dict.
iteritems As for a built-in dict.
iterkeys As for a built-in dict.
itervalues As for a built-in dict.
keys As for a built-in dict.
pop As for a built-in dict.
popitem As for a built-in dict.
properties Return a set of writable, public attributes.
setdefault As for a built-in dict.
update As for a built-in dict.
values As for a built-in dict.
coord(name, role=None, key=False, exact=False)

Find a coordinate of the grid by name.

The given name argument is an abbreviation for (or equal to if the exact parameter is True) its standard_name attribute. If the key parameter is True then return the coordinate’s grid key name. If a coordinate does not have standard_name attribute, then its ncvar attribute is used.

Note that the returned coordinate is an object identity to the coordinate stored in the grid so, for example, a coordinate’s attributes may be changed in-place as follows:

>>> g.coord('height').long_name
AttributeError: 'Coordinate' object has no attribute 'long_name'
>>> g.coord('hei').long_name = 'HEIGHT'
>>> g.coord('heigh').long_name
'HEIGHT'

Or a deep copy may be made with the coordinate’s copy method:

>>> h = g.coord('height').copy()
Parameters:
  • name (str) – The string to identify a coordinate by name.
  • exact (bool) – Optional. If True then assume that the value of the name argument is equal to exactly one coordinate’s name.
  • key (str) – Optional. Return the grid key name instead of the coordinate.
  • role (str or None) – Optional. Restrict the search to coordinates of the given role. Valid values are ‘dim’ and ‘aux’, for dimension and auxiliary coordinate types respectively. If None then both types of coordinates will be searched.
Returns:

If a coordinate has been identified, return either a Coordinate instance or, if the keys parameter is True, a grid key name string. otherwise, return None.

Examples

>>> g.coord('lon')
<CF Coordinate: longitude(128)>
>>> g.coord('lon', key=True)
'dim2'
>>> g.coord('lonX', key=True)
None
>>> g.coord('lon', exact=True)
None
>>> g.coord('longitude', exact=True)
<CF Coordinate: longitude(128)>
dump(id=None, nc=False, omit=())

Return a string containing a full description of the grid.

Parameters:
  • idOptional. Set the common prefix of variable component names. If None then defaults to the class name.
  • id – str
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes the description.
Returns:

A string containing the description of the grid.

See also

dump

equals(other, rtol=None, atol=None, override=True)

Return True if two instances are congruent in that, for each group of grid component types (dimension coordinate, auxiliary coordinate, cell measures, etc.) there are an equal number of keys and each key’s value equals a value in the other grid. Note that within a group of component types, the key names need not be the same.

Equality of numbers is to within a tolerance. Refer to cf for details.

Parameters:
  • other (object) – The variable to compare against for equality.
  • atol (None or float) – Optional. If None then use the default method for setting the absolute tolerance for equality of real numbers (refer to cf for details). If a float then set the absolute tolerance to this value for all comparisons (refer to the override parameter).
  • override (bool) – Optional. If False then only use a float value of the rtol or atol parameters if it can not be set from attributes of the objects being compared.
  • rtol (None or float) – Optional. If None then use the default method for setting the relative tolerance for equality of real numbers (refer to cf for details). If a float then set the relative tolerance to this value for all comparisons (refer to the override parameter).
Returns:

True if the two objects are congruent, False otherwise.

Space

class cf.space.Space(**kwargs)

Bases: cf.space.Variable

Represent a space construct according to the CF data model.

Refer to the cf module for details on data access by indexing and data storage.

Slicing a space instance with call arguments

A space is callable and keyword arguments may be used to define slices of the space’s data based on conditions on the space’s coordinate values, creating a new space whose data and grid are a subset of the original.

The keyword arguments are exactly as for the space’s slice method, i.e. for a space, s, s(...) is exactly equivalent to s.slice(...). Refer to the slice method for details and examples.

Overloaded operators

Refer to Variable.

Parameters:**kwargs

The new instance is given attributes named by the keywords, whose values are deep copies the keyword values.

Attributes (type) Description
ancillary_variables (SpaceList) Optional. Contains other spaces comprising the variables described by a CF ancillary_variables string.
array (numpy array) A numpy array deep copy of the data.
bounds (CoordinateBounds) Optional The coordinate’s cell boundaries.
cell_methods (CellMethods) Optional. Contains the information from a CF cell_methods string parsed into a data structure. Refer to CellMethods for more details.
dtype (numpy.dtype) Describes the format of the elements in the data array (refer to numpy.ndarray.dtype).
grid (Grid) Optional. Describes the space’s dimensionality and contains the space’s dimension coordinates, auxiliary coordinates and cell measures. Refer to Grid for more details.
ncvar (string) Optional. Contains the name of the variable in the original netCDF file. If present when the space is written to a netCDF file, then used for the output netCDF variable name.
ndim (int) The data array’s number of dimensions (refer to numpy.ndarray.ndim).
shape (tuple of ints) The shape of the data array (refer to numpy.ndarray.shape).
size (int) The number of elements in the data array (refer to numpy.ndarray.size).
transform (str) Optional. The key of a grid’s transform attribute which contains a transformation for this coordinate.
type (type) The type of the data object, which is either a numpy array or a file pointer.
varray (numpy array view) A numpy view of the data.
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
coord Return a coordinate identified by its name.
copy Create a copy.
dump Return a string containing a full description of the instances.
equals Determine whether two instances are congruent with each other.
extract Determine whether an instance matches phenomena criteria.
first_datum Return the first element of the data without replacing a file pointer with a numpy array.
last_datum Return the last element of the data without replacing a file pointer with a numpy array.
name Return the name (standard_name, long_name or ncvar).
ncatts Return the set of attributes which are netCDF attributes.
properties Return a set of writable, public attributes.
repoint Revert the data to a file pointer.
slice Slice a space with conditions on coordinate values.
coord(name, role=None, key=False, exact=False)

Find a coordinate of the space’s grid by name.

The given name argument is an abbreviation for (or equal to if the exact parameter is True) its standard_name attribute. If the key parameter is True then return the coordinate’s grid key name. If a coordinate does not have standard_name attribute, then its ncvar attribute is used.

Note that the returned coordinate is an object identity to the coordinate stored in the grid so, for example, a coordinate’s attributes may be changed in-place as follows:

>>> s.coord('height').long_name
AttributeError: 'Coordinate' object has no attribute 'long_name'
>>> s.coord('hei').long_name = 'HEIGHT'
>>> s.coord('heigh').long_name
'HEIGHT'

Or a deep copy may be made with the coordinate’s copy method:

>>> h = s.coord('height').copy()
Parameters:
  • name (str) – The string to identify a coordinate by name.
  • exact (bool) – Optional. If True then assume that the value of the name argument is equal to exactly one coordinate’s name.
  • key (str) – Optional. Return the grid key name instead of the coordinate.
  • role (str or None) – Optional. Restrict the search to coordinates of the given role. Valid values are ‘dim’ and ‘aux’, for dimension and auxiliary coordinate types respectively. If None then both types of coordinates will be searched.
Returns:

If a coordinate has been identified, return either a Coordinate instance or, if the keys parameter is True, a grid key name string. otherwise, return None.

Examples

>>> s.coord('lon')
<CF Coordinate: longitude(128)>
>>> s.coord('lon', key=True)
'dim2'
>>> s.coord('lonX', key=True)
None
>>> s.coord('lon', exact=True)
None
>>> s.coord('longitude', exact=True)
<CF Coordinate: longitude(128)>
dump(id=None, nc=False, omit=())

Return a string containing a full description of the space.

Parameters:
  • idOptional. Set the common prefix of variable component names. If None then defaults to the class name.
  • id – str
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes the description.
Returns:

A string containing the description of the space.

See also

dump

ncatts()

Return a set of attributes which are netCDF attributes suitable for writing to a netCDF file.

All writable attributes are considered netCDF attributes except those starting with an underscore, (with the exception of ‘_FillValue’), those starting with “nc” and the grid attribute.

To create an attribute which will not be in the set of netCDF attributes, assign directly to the __dict__ dictionary as opposed to using setattr.

Returns:A set of attributes which are netCDF attributes.
repoint(test=False)

Reset the data of all space components to a file pointers if they are all available, regardless of of whether the data are currently stored as a numpy arrays or not.

This may free memory, but any changes to data stored as a numpy arrays will be lost.

Parameters:test (bool) – Optional. If True then do not reinstate a file pointer if it exists.
Returns:True if all file pointers of all space components exist, False otherwise.
shape

Tuple of the data’s dimension sizes.

If the data originates from a file, the returned data dimensions may differ from the dimensions of the array on disk in that size one dimendef slicesions are not represented in the space’s data, unless the space’s data would otherwise be a scalar, in which case the shape is (1,).

Returns:A tuple of the data’s dimension sizes.
slice(exact=False, **kwargs)

Return a new space whose data and grid are a subset of the original by slicing the space where conditions on coordinate data values are met.

A coordinate and the conditions on its data are specified with **kwargs parameters.

If a coordinate is not specified, then its dimension is returned unchanged. A call with no parameters (kwargs = {}) is returns a deep copy of the space.

The order in which coordinates are given in the parameter list is irrelevant.

Unless the exact keyword is True, the coordinates identified in different space elements may vary. For example, the keyword ‘lon’ could identify the ‘longitude’ coordinate in one variable and ‘longwave’ in another.

Parameters:
  • exact (bool) – Optional. If True then the remaining keyword arguments given by **kwargs are assumed to be unambiguous exact matches for a coordinate names, as opposed to an unambiguous abbreviations.
  • **kwargs

    The keyword arguments to the call may be any unambiguous abbreviation of the standard_name of any one dimensional dimension or auxiliary coordinate which shares its dimension with the space’s data. This behaviour is modified by the exact keyword.

    A keyword argument’s value may be one of the following:

    1. A scalar. The dimension is sliced where coordinate equals (using the == operator) the scalar value.
    2. A sequence. The dimension is sliced where the coordinate equals (using the == operator) any of the sequence’s elements.
    3. A Comparison object (as, for example, returned by the lt function). The dimension is sliced where the comparison for the coordinate evaluates to True.
    4. An array of booleans. The dimension is sliced where elements of this array are True.
Returns:

A space.

See also

__call__

Examples

>>> from cf import *
>>> s = read1('surface_air_temperature.nc')
>>> print s
Data            : air_temperature(time, latitude, longitude)
Cell methods    : time: mean (interval: 1.0 month)
Dimensions      : time(7070) -> 450-11-16 00:00:00 to 1049-12-16 12:00:00
                : latitude(64) -> -87.8638 to 87.8638 degrees_north
                : longitude(128) -> 0 to 357.1875 degrees_east
                : height(1) -> 2 m
Auxiliary coords:
>>> s
<CF Space: air_temperature(7070, 64, 128)>
>>> s.slice(lon = 0)
<CF Space: air_temperature(7070, 64)>
>>> s.slice(lon = [0, 45, 45.1])
<CF Space: air_temperature(7070, 64, 2)>
>>> s.slice(lon = numpy.arange(128)*2.8125)
<CF Space: air_temperature(7070, 64, 128)>
>>> s.slice(lon = s.coord('lon')<90)
<CF Space: air_temperature(7070, 64, 64)>
>>> s.slice(lon = 0, lat = gt(0))
<CF Space: air_temperature(7070, 32)>
>>> s.slice(longitude = 0, exact=True)
<CF Space: air_temperature(7070, 32)>
>>> s.slice(lon = 0, exact=True)
RuntimeError: Can not find dimension from keyword: lon

The next two commands are equivalent:

>>> s.slice(lon = lt(180))
<CF Space: air_temperature(7070, 64, 64)>
>>> s.slice(long = s.coord('lon')<180)
<CF Space: air_temperature(7070, 64, 64)>

The next two commands are equivalent:

>>> s.slice(lon = inside(90,135))
<CF Space: air_temperature(7070, 64, 17)>
>>> lon = s.coord('lon') ; s.slice(lon = (lon>=90) & (lon<=135))
<CF Space: air_temperature(7070, 64, 17)>

The next three commands are equivalent:

>>> s.slice()
>>> s.slice(**{})
>>> copy.deepcopy(s)
>>> s.copy()

Space list

class cf.space.SpaceList(sequence=())

Bases: cf.space.VariableList

A modified list for spaces supporting element-wise slicing, element-wise attribute retrieval and subsetting by phenomena criteria.

When requesting a non-private attribute that the cf list does not have, and the _elements_atts attribute is True, instead of raising an AttributeError a cf list is returned containing the values of the attribute from each of the elements, with a value of None if an element does not have the attribute. The setting of an attribute is as usual, i.e. it is set on the object itself rather that the object’s elements.

Slicing the space list returns a new space list with each element sliced with its slice method.

Parameters:sequence (iterable) – Optional. Initialize new list from sequence’s items.

Overloaded operators

Refer to VariableList.

Methods  
append As for a built-in list.
copy Create a deep copy.
count As for a built-in list but using numerically tolerant equality.
dump Return a string containing a full description of the each of the list’s elements.
equals Determine whether two lists are congruent element-wise.
extend As for a built-in list.
extract Select list elements which match phenomena criteria.
index As for a built-in list but using numerically tolerant equality.
insert As for a built-in list.
pop As for a built-in list.
properties Return a set of writable, public attributes.
remove As for a built-in list.
reverse As for a built-in list.
sort As for a built-in list.

Examples

>>> from cf import *
>>> type(s)
<class 'cf.space.SpaceList'>
>>> s
[<CF Space: pmsl(30, 24)>,
 <CF Space: temperature(17, 30, 24)>]
>>> s[slice(0,2)]
[<CF Space: pmsl(30, 24)>,
 <CF Space: temperature(17, 30, 24)>]
>>> s[0]
<CF Space: pmsl(30, 24)>
>>> type(s[0])
<class 'cf.space.Space'>
>>> s[0].units
'Pa'
>>> s[-1].units
'K'
>>> s.units
['Pa', 'K']   
>>> s.not_an_attribute
[None, None]   
>>> s.extract(units = 'K')
[<CF Space: temperature(17, 30, 24)>]
>>> s.slice(lon = le(355))
[<CF Space: pmsl(30, 2)>,
 <CF Space: temperature(17, 30, 2)>]
slice(exact=False, **kwargs)

Return a new list of spaces in which each space element has been sliced according to conditions on coordinate data values. Refer to Space.slice.

A coordinate and the conditions on its data are specified with **kwargs parameters.

If a coordinate is not specified, then its dimension is returned unchanged in each space. A call with no parameters (kwargs={}) returns a deep copy of the list of spaces.

The order in which coordinates are given in the parameter list is irrelevant.

Unless the exact keyword is True, the coordinates identified in different space elements may vary. For example, the keyword ‘lon’ could identify the ‘longitude’ coordinate in one variable and ‘longwave’ in another.

Parameters:
  • exact (bool) – Optional. If True then the remaining keyword arguments given by **kwargs are assumed to be unambiguous exact matches for coordinate names, as opposed to an unambiguous abbreviations.
  • **kwargs

    Other keyword arguments giving slice criteria are passed as **kwargs to each of the list’s variables’ slice methods. Refer to Space.slice.

Returns:

A space list.

Examples

>>>  print s
Data            : pressure(latitude_30, longitude)
Cell methods    : time: mean
Dimensions      : time(1) -> 1960-01-16 00:00:00
                : latitude_30(30) -> -5.28 to 7.48 degrees_north
                : longitude(24) -> 354.28 to 364.4 degrees_east
Auxiliary coords:  
Data            : u_compnt_of_wind(hybrid_sigma_pressure, latitude_29, longitude)
Cell methods    : time: mean
Dimensions      : time(1) -> 1960-01-16 00:00:00
                : hybrid_sigma_pressure(19) -> 0.9970123 to 0.00460588 1
                : latitude_29(29) -> -5.06 to 7.26 degrees_north
                : longitude(24) -> 354.28 to 364.4 degrees_east
Auxiliary coords:
>>> s
[<CF Space: pressure(30, 24)>,
 <CF Space: u_compnt_of_wind(19, 29, 24)>,
>>> s.slice(long = ge(360))
[<CF Space: pressure(30, 11)>,
 <CF Space: u_compnt_of_wind(19, 29, 11)>]

Refer to Space.slice for more examples.

Transform

class cf.space.Transform(*args, **kwargs)

Bases: cf.space.CfDict

A CF transform object defining a coordinate’s transformation in a dictionary-like object.

The named parameters and their values of the transformation (i.e. the transformation’s mappings) comprise the object’s key-value pairs.

A transformation is equivalent to either a netCDF(CF) ‘formula_terms’ or ‘grid_mapping’ property. The latter is identified by the presence of the ‘grid_mapping_name’ key which contains the mapping’s name.

In the ‘formula_terms’ case, a mapping to a coordinate uses the coordinate’s grid key name as a value. A mapping to another space might be a shallow copy of another space in memory.

Parameters:
  • *args

    Keys and values are initialized exactly as for a built-in dict.

  • **kwargs

    Keys and values are initialized exactly as for a built-in dict.

Overloaded operators

The in (set membership) operator is overloaded to use numerically tolerant equality.

Attribute (type) Description
transform_name (str) Optional. The identifying name of the transformation. Typically a netCDF(CF) ‘formula_terms’ or ‘grid_mapping_name’.
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
clear As for a built-in dict.
copy Create a deep copy.
equals Determine whether two instances are congruent with each other.
get As for a built-in dict.
get_keys Return grid keys which match a pattern.
has_key As for a built-in dict but using numerically tolerant equality.
items As for a built-in dict.
iteritems As for a built-in dict.
iterkeys As for a built-in dict.
itervalues As for a built-in dict.
keys As for a built-in dict.
pop As for a built-in dict.
popitem As for a built-in dict.
properties Return a set of writable, public attributes.
setdefault As for a built-in dict.
update As for a built-in dict.
values As for a built-in dict.

Examples

>>> t
<CF Transform: atmosphere_sigma_coordinate>
>>> dump(t)
atmosphere_sigma_coordinate transform
-------------------------------------
Transform['ps'] = <CF Space: surface_air_pressure(73, 96)>
Transform['ptop'] = 0.05
Transform['sigma'] = 'dim0'
>>> t
<CF Transform: rotated_latitude_longitude>
>>> dump(t)
rotated_latitude_longitude transformation
-----------------------------------------
Transform['grid_mapping_name'] = 'rotated_latitude_longitude'
Transform['grid_north_pole_latitude'] = 33.67
Transform['grid_north_pole_longitude'] = 190.0
dump(id=None, nc=False, omit=())

Return a string containing a full description of the transformation.

Parameters:
  • id (str) – Optional. Set the common prefix of component names. If None then defaults to the class name.
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes from the description.
Returns:

A string containing the description of the object.

See also

dump

Variable

class cf.space.Variable(**kwargs)

Bases: object

Base class for a space and its components.

The following objects are all subclasses of Variable: a space, a coordinate, coordinate bounds and cell measures.

Refer to the cf module for more details.

Parameters:**kwargs

The new variable is given attributes named by the keywords, whose values are deep copies the keyword values.

Overloaded operators

Refer to cf.

Attributes (type) Description
array (numpy array) A numpy array deep copy of the data.
dtype (numpy.dtype) Describes the format of the elements in the data array (refer to numpy.ndarray.dtype).
ncvar (string) Optional. Contains the name of the variable in the original netCDF file. If present when the space is written to a netCDF file, then used for the output netCDF variable name.
ndim (int) The data array’s number of dimensions (refer to numpy.ndarray.ndim).
shape (tuple of ints) The shape of the data array (refer to numpy.ndarray.shape).
size (int) The number of elements in the data array (refer to numpy.ndarray.size).
transform (str) Optional. The key of a grid’s transform attribute which contains a transformation for this coordinate.
type (type) The type of the data object, which is either a numpy array or a file pointer.
varray (numpy array view) A numpy view of the data.
_atol (None or float) Optional. Absolute tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
_rtol (None or float) Optional. Relative tolerance for numeric equality. Unset is equivalent to None, which implies a default value. Refer to cf.
Methods Description
copy Create a copy.
dump Return a string containing a full description of the instances.
equals Determine whether two instances are congruent with each other.
extract Determine whether an instance matches phenomena criteria.
first_datum Return the first element of the data without replacing a file pointer with a numpy array.
last_datum Return the last element of the data without replacing a file pointer with a numpy array.
name Return the name (standard_name, long_name or ncvar).
ncatts Return the set of attributes which are netCDF attributes.
properties Return a set of writable, public attributes.
repoint Revert the data to a file pointer.
array

Create a numpy array deep copy of the data.

If the data was stored as a file pointer then it will be changed in-place to be stored as a numpy array.

Returns:A numpy array.

Examples

>>> a = x.array
>>> type(a)
<type 'numpy.ndarray'>
>>> a = x.array[[0,1]]
>>> type(a)
<type 'numpy.ndarray'>
>>> a.shape
(2,)
copy(shallow=(), omit=())

Create a deep copy of the variable, but with shallow copies of selected attributes.

If a deep copy of a variable’s attribute raises an exception then a shallow copy is attempted. If the shallow copy also raises an exception then the variable’s attribute is assigned to the returned copy.

In particular, netCDF4.Variable instances are copied by assignment.

Parameters:
  • omit (sequence) – Optional. A collection of the variable’s attribute names which are not to be copied to the new variable. Supersedes duplicate entries in the shallow sequence.
  • shallow (sequence) – Optional. A collection of the variable’s attribute names which are to be shallow copied, as opposed to deep copied.
Returns:

A deep copy of the variable with the exceptions of those attributes given by the shallow argument.

dtype

Data-type of the data’s elements.

Returns:A numpy dtype object.
dump(id=None, nc=False, omit=())

Return a string containing a full description of the variable.

Parameters:
  • id (str) – Optional. Set the common prefix of component names. If None then defaults to the class name.
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes from the description.
Returns:

A string containing the description of the variable.

See also

dump

equals(other, rtol=None, atol=None, override=True)

Return True if the variable is congruent to another variable in that they have identical data, property names (as returned by the properties method) and corresponding property values.

Equality of numbers is to within a tolerance. Refer to cf for details.

Parameters:
  • other (object) – The variable to compare against for equality.
  • atol (None or float) – Optional. If None then use the default method for setting the absolute tolerance for equality of real numbers (refer to cf for details). If a float then set the absolute tolerance to this value for all comparisons (refer to the override parameter).
  • override (bool) – Optional. If False then only use a float value of the rtol or atol parameters if it can not be set from attributes of the objects being compared.
  • rtol (None or float) – Optional. If None then use the default method for setting the relative tolerance for equality of real numbers (refer to cf for details). If a float then set the relative tolerance to this value for all comparisons (refer to the override parameter).
Returns:

True if the two objects are congruent, False otherwise.

Examples

>>> y = x
>>> x.equals(y)
True
>>> y =x()
>>> y[...] = y.varray + 1
>>> x.equals(y)
False
>>> y[...] = y.varray - 1
>>> x.equals(y)
True
>>> y.standard_name += '_different'
>>> x.equals(y)
False
extract(exact=False, **kwargs)

Determine whether or not the variable matches conditions on its phenomena.

The variable’s phenomena are its attributes and, if it has any, its size 1 coordinates.

The phenomenon and its conditions are specified with **kwargs parameters.

The variable matches the conditions if and only if it contains all of the specified phenomena and they pass all of their given criteria. A variable always matches no criteria.

Parameters:
  • exact (bool) – Optional If True then the remaining keyword arguments given by **kwargs are assumed to be unambiguous exact matches for a phenomenon names, as opposed to an unambiguous abbreviations.
  • **kwargs

    The remaining keyword arguments may be any unambiguous abbreviation of any phenomenon name, either an attribute name or the standard_name of a coordinate. This behaviour is modified by the exact keyword.

    A keyword argument’s value may be one of the following:

    1. A number. A match requires that a numeric phenomenon equals the number using the variable’s == operator.
    2. A string. A match requires that a string-valued phenomenon passes a regular expression match on the string. The string may contain regular expression special characters. To avoid ambiguities, it is assumed that the regular expression string matches the whole of the phenomenon string, i.e. the regular expression special characters ^ and $ are assumed if not given.
    3. A sequence of numbers or strings. A match requires that a numeric phenomenon equals at least one of the sequence’s elements (as in 1.) or a string-valued phenomenon passes a regular expression match for at least one string in the sequence (as in 2.).
    4. A Comparison object. A match requires that the comparison for the phenomenon evaluates to True.
Returns:

True if the variable matches the criteria, False otherwise.

Examples

>>> S
<CF Space: air_temperature(19, 30, 24)>
>>> s.standard_name
'air_temperature'
>>> s.extract(standard_name = 'air_temperature')
True
>>> s.extract(standard = '^air.*')
True
>>> s.extract(standard_name = lt('b'))
True
>>> s.extract(standard_name = outside('b', 'p'))
True
>>> s.extract(standard = ['.*temp.*', 'pressure'])
True
>>> s.extract(exact=True, standard_name = 'air_temperature')
True
>>> s.extract(exact=True, standard_ = 'air_temperature')
False
>>> s.extract(standard = ['temp', 'pressure'])
False
>>> s.extract(standard_name = inside('o', 'p'))
False
first_datum

Return the first element of the data without replacing a file pointer with a numpy array.

Returns:The scalar value of the first element of the data.
last_datum

Return the last element of the data without replacing a file pointer with a numpy array.

Returns:The scalar value of the last element of the data.
name(long=False, ncvar=False, default=None)

Return the standard_name attribute of the variable.

If there is no standard_name attribute then return one of the long_name attribute, the ncvar attribute or the value of the default parameter, depending on the values of the parameters.

Parameters:
  • long (bool) – Optional. If True, return the long_name if standard_name does not exist.
  • ncvar (bool) – Optional If True, return ncvar if neither standard_name not long_name have already been returned.
  • default (None or object) – Optional. Return default if standard_name, long_name nor ncvar have already been returned.
Returns:

The name of the variable.

ncatts()

Return a set of attributes which are netCDF attributes suitable for writing to a netCDF file.

All writable attributes are considered netCDF attributes except those starting with an underscore, (with the exception of ‘_FillValue’) and those starting with “nc”.

To create an attribute which will not be in the set of netCDF attributes, assign directly to the __dict__ dictionary as opposed to using setattr.

Returns:A set of attributes which are netCDF attributes.
ndim

Number of data dimensions.

Equivalent to the number of elements in shape tuple.

Returns:A non-negative integer
properties(nc=False)

Return a set of writable, public attribute names, excluding methods.

An attribute created by assigning directly to the __dict__ dictionary (as opposed to using setattr) will not appear in this set.

Parameters:nc (bool) – If False then exclude attributes whose names begin “nc”.
Returns:A set of attribute names.
repoint(test=False)

Reset the data to a file pointer if one is available, regardless of of whether the data is currently stored as a numpy array or not.

This may free memory, but any changes to data stored as a numpy array will be lost.

Parameters:test (bool) – Optional. If True then do not reinstate a file pointer if it exists.
Returns:True if a file pointer exists, False otherwise.
shape

Tuple of the data’s dimension sizes.

Returns:A tuple
size

Number of elements in the data.

Equivalent to the product of the data’s dimension sizes.

Returns:A non-negative integer
type

The type of the data object.

Returns:A type object, or None if the variable has no data.
varray

Create a numpy view of the data.

If the data was stored as a file pointer then it will be changed in-place to be stored as a numpy array.

Note that making changes to elements of the returned view changes the underlying data. Refer to numpy.view.

Returns:A numpy view.

Examples

>>> a = x.varray
>>> print a
array([0, 1, 2, 3, 4])
>>> a[0] = 999
>>> print x.varray[0]
999
>>> a = 'a_different_object'
>>> print x.varray
array([999, 1, 2, 3, 4])

Variable list

class cf.space.VariableList(sequence=())

Bases: cf.space.CfList

A modified list for variables (any object which is subclass of Variable, such as a space, coordinate, etc.) supporting element-wise attribute retrieval and subsetting by phenomena criteria.

When requesting a non-private attribute that the cf list does not have, and the _elements_atts attribute is True, instead of raising an AttributeError a cf list is returned containing the values of the attribute from each of the elements, with a value of None if an element does not have the attribute. The setting of an attribute is as usual, i.e. it is set on the object itself rather that the object’s elements.

Parameters:sequence (iterable) – Optional. Initialize new list from sequence’s items.

Overloaded operators

Refer to CfList.

Attributes

Refer to CfList

Methods  
append As for a built-in list.
copy Create a deep copy.
count As for a built-in list but using numerically tolerant equality.
dump Return a string containing a full description of the each of the list’s elements.
equals Determine whether two lists are congruent element-wise.
extend As for a built-in list.
extract Select list elements which match phenomena criteria.
index As for a built-in list but using numerically tolerant equality.
insert As for a built-in list.
pop As for a built-in list.
properties Return a set of writable, public attributes.
remove As for a built-in list.
reverse As for a built-in list.
sort As for a built-in list.
dump(id=None, nc=False, omit=())

Return a string containing a full description of each variable in the list, utilizing each variable’s dump method.

Parameters:
  • idOptional. Set the common prefix of variable component names. If None then defaults to the class name.
  • id – str
  • nc (bool) – Optional. If True then include attributes whose names begin “nc”.
  • omit (sequence) – Optional. Omit the given attributes from each variable’s the description.
Returns:

A string containing the descriptions of each variable in the list.

See also

dump

extract(deep=False, exact=False, **kwargs)

Return a subset of a list of variables by selecting only the variables which match conditions on their phenomena.

A variable’s phenomena are its attributes and, if it has any, its size 1 coordinates.

A phenomenon and its conditions are specified with **kwargs parameters.

A variable in the list matches the conditions if and only if it contains all of the specified phenomena and they pass all of their given criteria. A variable always matches no criteria (kwargs={})

A match for a variable is determined by passing the criteria to the variable’s extract method.

Unless the exact keyword is True, the phenomena identified in different variable elements may vary. For example, the keyword ‘unit’ could identify the ‘units’ phenomenon in one variable and ‘unitary’ in another.

By default, the returned list of variables is a shallow copy of part of the original list (in the same way that a standard slice is). To return a list whose elements are deep copies, set the deep parameter to True.

Parameters:
  • deep (bool) – Optional. If True then return deep copies of variables in the list.
  • exact (bool) – Optional. If True then the remaining keyword arguments given by **kwargs are assumed to be unambiguous exact matches for a phenomenon names, as opposed to an unambiguous abbreviations.
  • **kwargs

    Other keyword arguments giving any phenomena criteria are passed as **kwargs to the extract method of each of the list’s variables. Refer to Variable.extract.

Returns:

A list of variables

Examples

>>> s
[<CF Space: pressure(30, 24)>,
 <CF Space: u_compnt_of_wind(19, 29, 24)>
>>> s.units
['hPa', 'm s-1']
>>> s.extract(units = 'm s-1')
[<CF Space: u_compnt_of_wind(19, 29, 24)>]
>>> t = s.extract(uni = 'm\s*s-1')
>>> t
[<CF Space: u_compnt_of_wind(19, 29, 24)>]
>>> t[0] is s[1]
True
>>> t = s.extract(uni = 'm\s*s-1', deep=True)
>>> t[0] is s[1]
False

Refer to Variable.extract for more examples.

Indices and tables