The Variable object

The Variable class is the parent class of all CF objects which describe data of any type, namely Field, Coordinate, CoordinateBounds and Cellmeasures classes. All of these classes share a common data storage, access and manipulation model

Accessing the data

The Variable’s data may be accessed as a numpy array in two ways. The array attribute return a numpy copy of the data and the varray attribute returns a numpy view of the data.

>>> type(v)
<Variable>
>>> type(v.array)
<type 'numpy.ndarray'>
>>> type(v.varray)
<type 'numpy.ndarray'>
>>> x = v.varray
>>> x.shape
(96, 73)

Note that changes to the numpy view of the data also occur in the Variable’s data.

Resizing a variable by slicing

The Variable’s slice attribute is an indexable property which passes a given index onto the Variable’s data, returning a new Variable instance whose data are resized according to the given slice. The usual numpy slicing methods all lead to valid indices.

>>> type(v)
<Variable (96, 73)>
>>> w = v.slice[...]
>>> type(w)
<Variable (96, 73)>
>>> type(v.slice[0,:])
<Variable (73)>
>>> type(v.slice[0])
<Variable (73)>

Assignment to the data array

Assignment to the Variable’s data array may be done in two ways; either by modifying a numpy view of the data or by assigning to the Variable’s slice method.

>>> 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)

Data Storage

The Variable’s data are stored in an actual or virtual numpy array. A virtual numpy array is an object which can return information about the data’s propoerties (its numpy shape, size, ndim, dtype), but does not store the data in memory. Instead it stores information on how to read the data from a file if required. Upon indexing the object as if for an actual numpy array, the object is replaced within the Variable with the corresponding actual numpy array. Thus, the overhead of reading data from disk is reserved for when the data is required, if that is ever the case.

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.

Data access

A variable’s data is accessed via its slice attribute. This attribute is indexable 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.

Table Of Contents

This Page