In this example we work through the following steps:

  1. Import the cf package.
  2. Read a field from disk and find a summary of its contents.
  3. Find all of the field’s CF properties and its data array as a numpy array.
  4. Modify the field’s long name CF property and change the field’s data from units of Kelvin to Celsius.
  5. Check that the field has ‘air_temperature’ as its standard name and that at least one of its latitude coordinate values is greater than 0.0.
  6. Modify the data values.
  7. Access and modify a subspace of the data values.
  8. Write the modified field to disk.

The example may be reproduced by downloading the sample netCDF file (file.nc) (taking care not to overwrite an existing file with that name). This file may be also found in the docs/build/_downloads/ directory of the installation.

1. Import the cf package.

In [1]:
import cf

2. Read a field from disk and find a summary of its contents.

In [2]:
f = cf.read('~/file.nc')
type(f)
Out[2]:
cf.field.Field
In [3]:
f
Out[3]:
<CF Field: air_temperature(latitude(4), longitude(5)) K>
In [4]:
print f
air_temperature field summary
-----------------------------
Data           : air_temperature(latitude(4), longitude(5)) K
Cell methods   : time: mean
Axes           : time(1) = [2000-01-16 00:00:00] 360_day
               : height(1) = [2.0] m
               : latitude(4) = [-2.5, ..., 5.0] degrees_north
               : longitude(5) = [0.0, ..., 15.0] degrees_east

3. Find all of the field’s CF properties and its data array as a numpy array.

In [5]:
f.properties
Out[5]:
{'Conventions': 'CF-1.5',
 '_FillValue': 1e+20,
 'experiment_id': 'stabilization experiment (SRES A1B)',
 'long_name': 'Air Temperature',
 'standard_name': 'air_temperature',
 'title': 'SRES A1B'}
In [6]:
f.array
Out[6]:
array([[ 274.1499939,  276.1499939,  275.1499939,  277.1499939,
         278.1499939],
       [ 274.1499939,  275.1499939,  276.1499939,  277.1499939,
         276.1499939],
       [ 277.1499939,  275.1499939,  278.1499939,  274.1499939,
         278.1499939],
       [ 276.1499939,  274.1499939,  275.1499939,  277.1499939,
         274.1499939]], dtype=float32)

4. Modify the field’s long name CF property and change the field’s data from units of Kelvin to Celsius.

Note: Changing the units automatically changes the data when it is next accessed.

In [7]:
f.long_name
Out[7]:
'Air Temperature'
In [8]:
f.long_name = 'Surface Air Temperature'
f.long_name
Out[8]:
'Surface Air Temperature'
In [9]:
print f.Units
K
In [10]:
f.Units -= 273.15
print f.Units
K @ 273.15
In [11]:
print f.array
[[ 0.99999392  2.9999938   1.99999392  3.9999938   4.9999938 ]
 [ 0.99999392  1.99999392  2.9999938   3.9999938   2.9999938 ]
 [ 3.9999938   1.99999392  4.9999938   0.99999392  4.9999938 ]
 [ 2.9999938   0.99999392  1.99999392  3.9999938   0.99999392]]

5. Check that the field has ‘air_temperature’ as its standard name and that at least one of its latitude coordinate values is greater than 0.0.

In [12]:
f.match('air_temperature', items={'latitude' : cf.gt(0)})
Out[12]:
True

6. Modify the data values.

In [13]:
g = f + 100
print g.array
[[ 100.99999237  102.99999237  101.99999237  103.99999237  104.99999237]
 [ 100.99999237  101.99999237  102.99999237  103.99999237  102.99999237]
 [ 103.99999237  101.99999237  104.99999237  100.99999237  104.99999237]
 [ 102.99999237  100.99999237  101.99999237  103.99999237  100.99999237]]
In [14]:
g = f + f
print g.array
[[ 1.99998784  5.9999876   3.99998784  7.9999876   9.9999876 ]
 [ 1.99998784  3.99998784  5.9999876   7.9999876   5.9999876 ]
 [ 7.9999876   3.99998784  9.9999876   1.99998784  9.9999876 ]
 [ 5.9999876   1.99998784  3.99998784  7.9999876   1.99998784]]
In [15]:
g = f / cf.Data(2, 'seconds')
print g.array
[[ 0.49999696  1.4999969   0.99999696  1.9999969   2.4999969 ]
 [ 0.49999696  0.99999696  1.4999969   1.9999969   1.4999969 ]
 [ 1.9999969   0.99999696  2.4999969   0.49999696  2.4999969 ]
 [ 1.4999969   0.49999696  0.99999696  1.9999969   0.49999696]]
In [16]:
print g.Units
s-1.K

7. Access and modify a subspace of the data values.

In [17]:
g = f.subspace[0::2, 2:4]
print g.array
[[ 1.99999392  3.9999938 ]
 [ 4.9999938   0.99999392]]
In [18]:
f.subspace[0::2, ...] = -10
print f.array
[[-10.         -10.         -10.         -10.         -10.        ]
 [  0.99999392   1.99999392   2.9999938    3.9999938    2.9999938 ]
 [-10.         -10.         -10.         -10.         -10.        ]
 [  2.9999938    0.99999392   1.99999392   3.9999938    0.99999392]]

8. Write the modified field to disk.

In [19]:
cf.write(f, 'newfile.nc')