ants.io package#
Submodules#
ants.io.load module#
Loading Target grids#
The ANTS library provides a grid loading capability via
ants.io.load.load_grid(). This function will return an iris cube that can be
used to define the target grid for processing. The cube can have a constant
data field or a land sea mask depending on the data source.
Grid loading supports the following formats:
Ancillary (CAP) compliant Fortran Namelist grid definition files (further information can be found at
ants.io.load.load_grid()). Use this format in applications that do not need to use the land sea mask of the target field as part of their processing.Any fileformat that can be interpreted by iris. Use this format to read a grid with a land sea mask.
Loading data#
ANTS uses iris to load common fileformats and therefore has access to all formats that are supported by iris.
In most cases you should, however, use ants.io.load(),
ants.io.load.load_cube() etc. to load data as cubes from files.
These ANTS functions perform additional processing on the input
data. They will attempt to:
Derive the global/regional status of the input data and set the metadata on the cube accordingly.
Guess bounds of latitude and longitude for any sources without horizontal grid bounds.
Remove any forecast_reference_time and forecast_period coordinates.
The ANTS functions add extra capability, depending on the fileformat, over the
base iris functions. You should read the documentation for the ANTS module
for the specific fileformat being used, for example
ants.fileformats.ancil for ancillary fields file loading.
See also
ants.fileformats.ancil.load_cubes(), ants.fileformats.ancil.load_cubes_32bit_ieee(), ants.fileformats.raster.load_cubes(), ants.fileformats.pp.load_cubes(), ants.fileformats.pp.load_cubes_little_endian(), ants.fileformats.namelist.load_cap_horizontal(), ants.fileformats.namelist.load_um_vertical()
- ants.io.load.ants_format_agent()[source]#
Customises iris.fileformats.FORMAT_AGENT for ants loading and reverts the FORMAT_AGENT once the loading is complete.
- ants.io.load.load(*args, **kwargs)[source]#
Loads any number of Cubes for each constraint.
Wraps
iris.load(). Different to the iris functionality, this will remove any forecast_reference_time and forecast_period coordinates. The order of pseudolevels will be preserved from the source file.- Parameters:
*args – See
iris.load()**kwargs – See
iris.load()
- Return type:
A
CubeList.
- ants.io.load.load_cube(*args, **kwargs)[source]#
Loads a single cube.
Wraps
iris.load_cube(). Different to the iris functionality, this will remove any forecast_reference_time and forecast_period coordinates. The order of pseudolevels will be preserved from the source file.- Parameters:
*args – See
iris.load_cube()**kwargs – See
iris.load_cube()
- Return type:
A
Cube.
- ants.io.load.load_cubes(*args, **kwargs)[source]#
Loads exactly one Cube for each constraint.
Wraps
iris.load_cubes(). Different to the iris functionality, this will remove any forecast_reference_time and forecast_period coordinates. The order of pseudolevels will be preserved from the source file.- Parameters:
*args – See
iris.load_cubes()**kwargs – See
iris.load_cubes()
- Return type:
A
CubeList.
- ants.io.load.load_grid(filenames, *args, **kwargs)[source]#
Load a grid definition and return an iris cube.
The purpose of this function is to load a single grid definition, without the data payload, from components derived from one or more file. That is, to merge these components where possible to return a single grid definition. Examples include the merging of a vertical definition with one defining the horizontal domain. Surface altitude (orography) is also supported as a merge component. A hybrid height coordinate will ensue. Where source datasets have conflicting components, an exception will be raised. Where more than one source dataset has identical components, a single grid will still be returned.
- Parameters:
filenames (str) – Pathname of a file, or a list of such paths that contain the grid definition.
*args – See
iris.load()**kwargs – See
iris.load()
- Returns:
Cube representing the grid defined with no data disk dependence and negligible data memory usage (using dummy data).
- Return type:
- Raises:
ValueError – Where we are unable to resolve a single grid due to a conflicting coordinate.
iris.exceptions.ConstraintMismatchError – Where no cubes have been found.
- ants.io.load.load_landsea_mask(filename, land_threshold=None)[source]#
Load a landsea mask from either a landsea mask file or a landfraction file.
- Parameters:
- Returns:
Landsea mask cube.
- Return type:
- ants.io.load.load_raw(*args, **kwargs)[source]#
Loads non-merged cubes.
Wraps
iris.load_raw(). Different to the iris functionality, this will remove any forecast_reference_time and forecast_period coordinates. The order of pseudolevels will be preserved from the source file.- Parameters:
*args – See
iris.load_raw()**kwargs – See
iris.load_raw()
- Return type:
A
CubeList.
ants.io.save module#
This module contains code used to save cubes.
Saving data#
Like loading, ANTS save adds extra file format dependent capability over
the base iris save. Saving the cube to NetCDF uses
ants.io.save.netcdf(). Saving to an F03 UM ancillary file
(using saver='ancil') uses ants.io.save.ancil().
NetCDF is easier to use for verification and
diagnostic purposes, while the fields file is currently needed for the UM.
Additionally, ANTS has support for ‘ukca’ flavoured NetCDF, chosen by
specifying saver='ukca' (see ants.io.save.ukca_netcdf()).
- ants.io.save.ancil(cubes, filename)[source]#
Save one or more cubes to a F03 UM ancillary file.
UM documentation paper UM input and output file formats (F03) defines the ancillary file format.
Every provided cube must have a STASH code attribute defined.
The fields are written to the output file in this order:
for time in time_list: for STASH in STASH_list: for level in level_list: <write out field>
The
time_listis sorted into ascending order.The
STASH_listis sorted into the order in which the STASH codes are first found in the cubes being passed into this function. If your use case requires fields with a particular STASH order, sort the cubes into that order prior to calling the save.The
level_listis either pseudo levels or model level numbers. The behaviour depends on the kind: model level numbers are sorted into ascending order like times, while pseudo levels are sorted in the order first found in the cubes, like STASH codes.Saving cubes with both pseudo levels and model level numbers is not supported.
- Parameters:
cubes (
iris.cube.Cubeoriris.cube.CubeList) – One or more cubes to be saved.filename (str) – The name of the F03 UM ancillary file, including any extension.
Notes
To save a cube as a fields file, it must have a grid staggering set. To set the grid staggering manually you can use the grid_staggering attribute. For example, to set an ENDGame grid staggering:
cube.attributes['grid_staggering'] = 6See UM doc F03 for suitable grid staggering values. Some metadata is set so applications apart from the UM can read the ancillary. This includes:
integer_constants(3) is set to 1 if there’s no valid value for it to support XCONV
- ants.io.save.netcdf(cubes, filename, netcdf_format='NETCDF4_CLASSIC', local_keys=None, unlimited_dimensions=None, zlib=False, complevel=4, fill_value=None, update_history=True, history_message=None)[source]#
Save one or more cubes to a netCDF file.
- Parameters:
cubes (
iris.cube.Cubeoriris.cube.CubeList) – One or more cubes to be saved.filename (str) – The name of the netCDF file. If the .nc extension is missing then it it will be added to enforce CF compliance.
netcdf_format (str) – Underlying netCDF file format, one of
NETCDF4,NETCDF4_CLASSIC,NETCDF3_CLASSICorNETCDF3_64BIT. Default isNETCDF4_CLASSICformat.local_keys (Iterable of str) – Cube attribute keys. Any cube attributes with matching keys will become attributes on the data variable rather than global attributes.
unlimited_dimensions (
Iterableofstrand/oriris.coords.Coord) – Coordinate dimensions ofcubeto save with the netCDF dimension variable lengthUNLIMITED. By default there are no dimensions assigned with lengthUNLIMITED.zlib (bool) – If True, the data will be compressed in the netCDF file using gzip compression (default False).
complevel (int) – An integer between 1 and 9 describing the level of compression desired (default 4). Ignored if
zlibis False.fill_value (Number or list) – The value to use for the
_FillValueattribute on the netCDF variable. If this argument is a list it must have the same number of elements ascubesifcubesis airis.cube.CubeList, or a single element, and each element of this argument will be applied to each cube separately.update_history (bool) – If True, the
historyattribute will be updated with the timestamped command line arguments (default True).history_message (str) – The message to use instead of the command line arguments when updating the
historyattribute. This argument will only be used ifupdate_historyis set to True.
Notes
If the cubes being saved have lazy data, this saver has two potential side effects:
The data of any cubes with a datatype
int64and being saved asNETCDF4_CLASSICare realised.The data for each cube is rechunked such that the innermost dimension is a single chunk. This can be disabled via configuration, see
ants_tuninginants.config.GlobalConfiguration.
See also
- ants.io.save.ukca_netcdf(cubes, filename, netcdf_format='NETCDF4_CLASSIC', local_keys=None, unlimited_dimensions=None, update_history=True)[source]#
Save one or more cubes to a UKCA-specific netCDF file.
The following represents applied UKCA specification or standard UKCA netCDF setup:
Compression used:
zlibwithcomplevel=4.Ensure that specific attributes are local (see
ants.fileformats.netcdf.ukca.LOCAL_ATTS).Bounds are present (guessed where not).
Data types are made either 32-bit integer or 32-bit float.
Masked data is filled with zeros (a warning is issued where this happens).
Old UKCA conventions present are updated:
Replace
emission_typewithupdate_type.All UKCA numeric attributes are converted to 32-bit integers. These include
update_type,update_freq_in_hours,lowest_levelandhighest_level.
- Parameters:
cubes (
iris.cube.Cubeoriris.cube.CubeList) – One or more cubes to be saved.filename (str) – The name of the UKCA-specific netCDF file, including the extension.
netcdf_format (str) – Underlying netCDF file format, one of
NETCDF4,NETCDF4_CLASSIC,NETCDF3_CLASSICorNETCDF3_64BIT. Default isNETCDF4_CLASSICformat.local_keys (Iterable of str) – Cube attribute keys. Any cube attributes with matching keys will become attributes on the data variable rather than global attributes.
unlimited_dimensions (
Iterableofstrand/oriris.coords.Coord) – Coordinate dimensions ofcubeto save with the netCDF dimension variable lengthUNLIMITED. By default there are no dimensions assigned with lengthUNLIMITED.update_history (bool) – If True, the
historyattribute will be updated with the timestamped command line arguments (default True).
See also