In this example we work through the following steps:
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.
import cf
2. Read a field from disk and find a summary of its contents.
f = cf.read('~/file.nc')
type(f)
f
print f
3. Find all of the field’s CF properties and its data array as a numpy array.
f.properties
f.array
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.
f.long_name
f.long_name = 'Surface Air Temperature'
f.long_name
print f.Units
f.Units -= 273.15
print f.Units
print f.array
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.
f.match('air_temperature', items={'latitude' : cf.gt(0)})
6. Modify the data values.
g = f + 100
print g.array
g = f + f
print g.array
g = f / cf.Data(2, 'seconds')
print g.array
print g.Units
7. Access and modify a subspace of the data values.
g = f.subspace[0::2, 2:4]
print g.array
f.subspace[0::2, ...] = -10
print f.array
8. Write the modified field to disk.
cf.write(f, 'newfile.nc')