improver.utilities.save module#

Module for saving netcdf cubes with desired attribute types.

_check_metadata(cube)[source]#

Checks cube metadata that needs to be correct to guarantee data integrity

Parameters:

cube (Cube) – Cube to be checked

Raises:
  • ValueError – if time coordinates do not have the required datatypes and units; needed because values may be wrong

  • ValueError – if numerical datatypes are other than 32-bit (except where specified); needed because values may be wrong

  • ValueError – if cube dataset has unknown units; because this may cause misinterpretation on “load”

Return type:

None

_cube_attributes_for_save(cube)[source]#

Separate global and local attributes for saving with iris by ensuring a CubeAttrsDict represents all attributes.

Parameters:

cube (Cube) – The cube for which the attributes are to be separated.

_derive_chunksizes(cubelist)[source]#
_horizontal_grid(cube)[source]#
_order_cell_methods(cube)[source]#

Sorts the cell methods on a cube such that if there are multiple methods they are always written in a consistent order in the output cube. The input cube is modified. Ensure that if there are any identical duplicate cell methods, only one of these is included in the outputs.

Parameters:

cube (Cube) – The cube on which the cell methods are to be sorted.

Return type:

None

save_netcdf(cubelist, filename, complevel=1, zlib=None, shuffle=True, chunksizes=None, **kwargs)[source]#

Save the input Cube or CubeList as a NetCDF file and check metadata where required for integrity.

Uses the functionality provided by iris.fileformats.netcdf.save with local_keys to record non-global attributes as data attributes rather than global attributes. The save is made with iris.FUTURE.context(save_split_attrs=True). iris.fileformats.netcdf.save will add a new “least_significant_digit” attribute, but will not update an existing attribute when saving with different precision. Therefore, we remove the “least_significant_digit” attribute if present.

We further deviate from iris.fileformats.netcdf.save default behaviour as per keyword arguments.

Parameters:
  • cubelist (Union[Cube, CubeList]) – Cube or CubeList to be saved.

  • filename (str | Path) – Filename to save input cube(s)

  • complevel (int) – Compression level for the NetCDF file. Must be an integer between 0 and 9 where 0 disables compression. Default is 1 (iris default is 4).

  • zlib (bool | None) – Whether to use zlib compression. If None (default), set to True if complevel > 0, otherwise False. iris default is False.

  • shuffle (bool) – Whether to use HDF5 shuffle filter. Default is True (iris default is False).

  • chunksizes (tuple | None) – Tuple defining chunk sizes for the output file. If None (default), automatically determined as a 1 for all dimensions except the x and y dimensions, which are set to the full size of the x and y dimensions.

  • **kwargs – Additional keyword arguments to pass to iris.fileformats.netcdf.save.

Raises:

ValueError – If complevel is not between 0 and 9.

Warns:
  • If compression_level is passed via kwargs (deprecated, use complevel instead)

  • If cubelist contains cubes of varying dimensions.

Return type:

None