cf.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 cf.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 cf.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

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

Previous topic

cf.read1

Next topic

cf classes

This Page