ugants.regrid package#
Submodules#
ugants.regrid.applications module#
Implementation for the mesh to grid regrid application.
- class ugants.regrid.applications.MeshToGridRegrid(source: CubeList, target_grid: CubeList, horizontal_regrid_scheme: Literal['conservative', 'bilinear', 'nearest'], input_weights: str = '', output_weights: str = '')[source]#
Bases:
ApplicationRegrid unstructured grid data to regular lat lon.
- Parameters:
source – The ugrid source data.
target_grid – The regular gridded target grid.
horizontal_regrid_scheme – The horizontal regrid scheme to be used. Supported schemes are “conservative”, “bilinear”, “nearest”.
input_weights – Path to cached input weights. An optional file containing the pre-generated weights for the mesh being used for regridding.
output_weights – An optional target path for output cached weights. Using pre-cached weights makes the regridding process less computationally expensive.
- class ugants.regrid.applications.SplitMeshToGridByLatitude(source: CubeList, target: CubeList, number_of_bands: int)[source]#
Bases:
ApplicationSplit the provided UGrid source and regular grid target into latitude bands.
- Parameters:
- source_bands: CubeList#
The domain of each cube completely covers the corresponding band in
target_bands. An attributeband_numberis added to distinguish each cube.
- target_bands: list[Mesh]#
Each target band covers a latitude band, approximately evenly spaced. There are no overlapping cells between any two bands, so each band covers a unique region. Together, the bands cover the entire target domain.
- output: str = None#
The output directory to which to write the
source_bandsandtarget_bands.
- run()[source]#
Split the target and source cubes into bands.
The target is first split into bands by latitude, with the resulting bands stored in
target_bands. These bands are then used to calculate the source bands, where each source band fully contains the corresponding target band. The source bands are stored insource_bands.
- save()[source]#
Save source and target bands.
Saves all the source and target bands to individual netCDF files. Source bands are saved to the
outputdirectory, with a name ofsource_band_N.ncfor the Nth source band. The corresponding Nth target band is saved in the same directory with the nametarget_band_N.nc.
- class ugants.regrid.applications.RecombineGridBands(slices: CubeList)[source]#
Bases:
ApplicationCombines individual split results into a single result.
- Parameters:
slices (iris.cube.CubeList) – Paths to files containing individual results.
- run()[source]#
Recombine individual split results into a complete result.
Sets the
resultsto the complete result. If the individual split results have different data types, this promotes the data type of the result to the highest precision data type in the split results. Thehistoryattribute of the combined result is set to be the history of the first individual band.
ugants.regrid.band_utils module#
Utility functions for band regridding.
- ugants.regrid.band_utils.mesh_to_cube(mesh, dtype: ~numpy.dtype = <class 'numpy.float64'>)[source]#
Turn a mesh into a
iris.cube.Cubewith data usingnp.nan.- Parameters:
mesh (
iris.experimental.ugrid.mesh.Mesh) – The mesh to be converted to aniris.cube.Cubedtype (
numpy.dtype) – The data type used for the cube.datanumpy.ndarray, by defaultnp.float64.
- Returns:
The providied mesh as an
iris.cube.Cube.- Return type:
- ugants.regrid.band_utils.generate_band_bounds(start: float, stop: float, n_bands: int)[source]#
Generate a list of tuples representing bands of latitude or longitude.
Divides an arbitrary interval, defined by
startandstop, into equally spaced subdivisions.Example
>>> generate_band_bounds(0.0, 15.0, 3) [(0.0, 5.0), (5.0, 10.0), (10.0, 15.0)]
- Parameters:
- Returns:
A list of tuples, with each tuple defining a band.
- Return type:
- Raises:
ValueError – When n_bands is less than 2.
- ugants.regrid.band_utils.find_cell_centres_within_latitude_bounds(cube, bounds: tuple, min_inclusive: bool = True, max_inclusive: bool = True)[source]#
Return a boolean mask of cells where the centres are within latitude bounds.
- Parameters:
cube (
iris.cube.Cube) – Aniris.cube.Cubewith a mesh.bounds (tuple[float, float]) – The latitude bounds to check.
min_inclusive (bool) – Include cell centres equal to the minimum latitude, by default True
max_inclusive (bool) – Include cell centres equal to the maximum latitude, by default True
- Returns:
An array of bools representing the face indices to subset.
- Return type:
- ugants.regrid.band_utils.subset_mesh_cube_by_indices(cube: Cube, indices)[source]#
Create a subset of a cube with a mesh by indexing its mesh dimension.
- Parameters:
cube (
iris.cube.Cube) – A cube containing the mesh to be subset.indices (
numpy.ndarray) – A boolean or integer array representing the indices of the mesh dimension to be sliced.
- Returns:
The subsetted cube.
- Return type:
Note
Mesh cube subsetting is currently only available for data located on faces.
- ugants.regrid.band_utils.reconstruct_mesh_cube(cube: Cube, mesh_dim: int)[source]#
Reconstruct a cube with a mesh that has been broken into AuxCoords.
When a cube is subset along a mesh dimension, the MeshCoords will be converted to AuxCoords, so the cube will no longer have a mesh. This function reconstructs the mesh from the AuxCoords, and adds corresponding MeshCoords to the cube.
- Parameters:
cube (
iris.cube.Cube) – The cube with a broken mesh to be reconstructed.mesh_dim (int) – The dimension of the cube which the mesh describes.
- Returns:
A copy of the original cube with a reconstructed mesh.
- Return type:
Notes
Note
The reconstructed cube’s mesh
locationis set to “face”. Other mesh locations (edge, node) are not supported.Note
The reconstructed mesh’s
face_node_connectivitywill be 0 indexed.Note
Face order is preserved, but node order is not preserved under reconstruction.
- ugants.regrid.band_utils.cube_subset_latitude_bounds(mesh, coord, fraction=0.05)[source]#
Calculate latitude bounds that subset the cube so that it encloses the mesh.
- Parameters:
mesh (
iris.experimental.ugrid.Mesh) – The target mesh.coord (
iris.coords.Coord) – The latitude coord.
- Returns:
Latitude bounds required to subset a cube.
- Return type:
- ugants.regrid.band_utils.constrain_source_cube_latitude(cube, bounds, padding=0.0)[source]#
Constrain a cube based on latitude bounds and optional padding.
- Parameters:
cube (
iris.cube.Cube) – Source cube to constrain.bounds (tuple[float, float]) – Lower and upper latitude limit.
padding (float) – Optional padding to be applied to both limits.
- Return type:
- ugants.regrid.band_utils.split_cube(cube, n_slices, axis='Y')[source]#
Split a cube along the given dimension into approximately equal sub-cubes.
The generated sub-cubes will be non-overlapping.
- Parameters:
cube (
iris.cube.Cube) – The cube to split.n_slices (int) – Total number of sub-cubes to return.
axis (str) – Axis to split over. Valid values are “Y” (default) to split on latitude, or “X” to split on longitude. This is case insensitive.
- Returns:
The sub cubes as a cube list.
- Return type:
- Raises:
ValueError – If the axis not one of “X” or “Y”.
- ugants.regrid.band_utils.get_faces_that_overlap_bounds(cube, bounds, index=1)[source]#
Get the indices of faces with one or more nodes within given latitude bounds.
- Parameters:
cube (
iris.cube.Cube) – A cube with associated mesh.bounds (tuple[float, float]) – The latitude bounds to check for overlapping faces.
- Return type:
- ugants.regrid.band_utils.cube_latitude_bounds(cube)[source]#
Get the max and min of the latitude bounds from a cube.
A copy of the “latitude” coordinate is taken in order to avoid mutating the provided cube if guessing bounds.
- Parameters:
cube (
iris.cube.Cube) – The cube to find latitude limits of.- Returns:
The minimum and maximum latitude of the cube.
- Return type:
ugants.regrid.command_line module#
Implementation for the regrid application.
- class ugants.regrid.command_line.Regrid(source: CubeList, target_mesh: Mesh, horizontal_regrid_scheme: Literal['conservative', 'bilinear', 'nearest'], tolerance: float = 0, input_weights: str = '', output_weights: str = '')[source]#
Bases:
ApplicationRegrid regular grid data to an unstructured mesh.
- Parameters:
source (
iris.cube.CubeList) – The regular gridded source data to be regridded.target_mesh (
iris.experimental.ugrid.Mesh) – The UGrid target mesh.horizontal_regrid_scheme (
str) – The horizontal regrid scheme to be used. Supported schemes are “conservative”, “bilinear”, “nearest”.tolerance (
float) – Tolerance of missing data. The value returned in each element of the returned array will be masked if the fraction of masked data exceeds this tolerance. If not provided, the default tolerance value is zero. This option is not available for the “nearest” scheme.input_weights – Path to cached input weights. An optional file containing the pre-generated weights for the mesh being used for regridding.
output_weights – An optional target path for output cached weights. Using pre-cached weights makes the regridding process less computationally expensive.
- Raises:
ValueError – If a
toleranceis provided and thehorizontal_regrid_schemeis “nearest”.
Note
The data is always regridded to the faces of the unstructured grid.
- run()[source]#
Regrid
sourcetotarget_mesh.The result of the regrid is stored in
results.
- class ugants.regrid.command_line.RegridMeshToMesh(source: CubeList, target_mesh: Mesh, horizontal_regrid_scheme: Literal['conservative', 'bilinear', 'nearest'], tolerance: float = 0)[source]#
Bases:
ApplicationRegrid unstructured mesh data to an unstructured mesh of a different resolution.
- Parameters:
source – The unstructured mesh data to be regridded.
target_mesh – The UGrid target resolution mesh.
horizontal_regrid_scheme – The horizontal regrid scheme to be used. Supported schemes are “conservative”, “bilinear”, “nearest”.
tolerance – Tolerance of missing data. The value returned in each element of the returned array will be masked if the fraction of masked data exceeds this tolerance. If not provided, the default tolerance value is zero. This option is not available for the “nearest” scheme.
- Raises:
ValueError – If a
toleranceis provided and thehorizontal_regrid_schemeis “nearest”.
Note
The data is always regridded to the faces of the unstructured mesh.
- class ugants.regrid.command_line.SplitGridToMeshByLatitude(source: CubeList, target_mesh: Mesh, number_of_bands: int)[source]#
Bases:
ApplicationSplit the provided regular gridded source and target mesh into latitude bands.
- Parameters:
- results: list[CubeList] = None#
A list of CubeLists, one for each latitude band. Each CubeList contains the same number of cubes as the source CubeList. The domain of each cube completely covers the corresponding mesh band in
mesh_bands. Padding is added ensure that the mesh band is fully enclosed, so there will be overlap between adjacent source bands. Seecube_subset_latitude_bounds()for more details on how the padding is calculated. An attributeband_numberis added to distinguish each cube.
- mesh_bands: list[Mesh]#
Each mesh covers a latitude band, approximately evenly spaced. There are no overlapping cells between any two mesh bands, so each mesh band covers a unique region. Together, the mesh bands cover the entire target mesh.
- mesh_mapping_cube: Cube#
A UGrid cube constructed from the
target_mesh. The data maps each cell to its corresponding latitude band. For example, if a cell has a value of 1, then it belongs in latitude band 1.
- output: str = None#
The output directory to which to write the
results,mesh_bandsandmesh_mapping_cube.
- run()[source]#
Run the application.
The source and target are split into bands of approximately equal latitude.
There is no overlap between target bands, i.e. a cell in the original target mesh will appear in one target band only. The mapping between target mesh cells and band number is recorded in the
mesh_mapping_cube.There is overlap between source bands, i.e. a cell in the original source may appear in more than one source band. This is because the source domain must extend beyond the target domain in order to capture all the required data for regridding.
The following attributes are set by this method:
- save()[source]#
Save the latitude bands to NetCDF.
Three types of file are saved to the directory specified by
self.output:results:number_of_bandssuch files are output, namedsource_band_{band_number}.nc.mesh_bands:number_of_bandssuch files are output, namedmesh_band_{band_number}.nc.mesh_mapping_cube: only one such file is output, namedmesh_band_mapping.nc.
In total,
2*number_of_bands + 1files are output.
- class ugants.regrid.command_line.RecombineMeshBands(mesh_mapping: CubeList, bands: CubeList)[source]#
Bases:
ApplicationRecombine regridded latitude bands into a single cube.
- Parameters:
mesh_mapping (iris.cube.CubeList) – A single-element CubList which maps individual latitude bands to cells in the target mesh. See also
SplitGridToMeshByLatitude.mesh_mapping_cube.bands (iris.cube.CubeList) – The regridded latitude bands to recombine into a single UGrid cube. Each band cube must have a
band_numberattribute, which maps the band to its location on themesh_mapping.
- results: CubeList = None#
The recombined bands in a single cube. The cube’s mesh is identical to that of the mesh mapping, and the data is taken from the cubes in
bands.
- run()[source]#
Recombine the latitude bands into a single cube.
The data from each latitude band cube in the
bandsCubeList are used to fill the corresponding cells in the target mesh, according to the band cube’sband_numberattribute.The
mesh_mapping_cubeprovides the target mesh to be filled with data from the regridded latitude bands. The data inmesh_mapping_cubedescribe which latitude band is to be used to fill the cells.For example, cells with value 2 in the
mesh_mapping_cubeare filled with data from band number 2.This method sets the
resultsattribute.