improver.utilities.pad_spatial module

Utilities for spatial padding of iris cubes.

_create_cube_with_padded_data(source_cube, data, coord_x, coord_y)[source]

Create a cube with newly created data where the metadata is copied from the input cube and the supplied x and y coordinates are added to the cube.

Parameters:
  • source_cube (Cube) – Template cube used for copying metadata and non x and y axes coordinates.

  • data (ndarray) – Data to be put into the new cube.

  • coord_x (DimCoord) – Coordinate to be added to the new cube to represent the x axis.

  • coord_y (DimCoord) – Coordinate to be added to the new cube to represent the y axis.

Return type:

Cube

Returns:

Cube built from the template cube using the requested data and the supplied x and y axis coordinates.

create_cube_with_halo(cube, halo_radius)[source]

Create a template cube defining a new grid by adding a fixed width halo on all sides to the input cube grid. The cube contains no meaningful data.

Parameters:
  • cube (Cube) – Cube on original grid

  • halo_radius (float) – Size of border to pad original grid, in metres

Return type:

Cube

Returns:

New cube defining the halo-padded grid (data set to zero)

pad_coord(coord, width, method)[source]

Construct a new coordinate by extending the current coordinate by the padding width.

Parameters:
  • coord (Coord) – Original coordinate which will be used as the basis of the new extended coordinate.

  • width (int) – The width of padding in grid cells (the extent of the neighbourhood radius in grid cells in a given direction).

  • method (str) – A string determining whether the coordinate is being expanded or contracted. Options: ‘remove’ to remove points from coord; ‘add’ to add points to coord.

Return type:

Coord

Returns:

Coordinate with expanded or contracted length, to be added to the padded or unpadded iris cube.

Raises:

ValueError – Raise an error if non-uniform increments exist between grid points.

pad_cube_with_halo(cube, width_x, width_y, pad_method='constant')[source]

Method to pad a halo around the data in an iris cube. If halo_with_data is False, the halo is filled with zeros. Otherwise the padding calculates a mean within half the padding width with which to fill the halo region.

Parameters:
  • cube (Cube) – The original cube prior to applying padding. The cube should contain only x and y dimensions, so will generally be a slice of a cube.

  • width_x (int) – The width in x directions of the neighbourhood radius in grid cells. This will be the width of padding to be added to the numpy array.

  • width_y (int) – The width in y directions of the neighbourhood radius in grid cells. This will be the width of padding to be added to the numpy array.

  • pad_method (str) – The numpy.pad method with which to populate the halo. The default is ‘constant’ which will populate the region with zeros. All other np.pad methods are accepted, though they are not fully configurable.

Return type:

Cube

Returns:

Cube containing the new padded cube, with appropriate changes to the cube’s dimension coordinates.

remove_cube_halo(cube, halo_radius)[source]

Remove halo of halo_radius from a cube.

This function converts the halo radius into the number of grid points in the x and y coordinate that need to be removed. It then calls remove_halo_from_cube which only acts on a cube with x and y coordinates so we need to slice the cube and them merge the cube back together ensuring the resulting cube has the same dimension coordinates.

Parameters:
  • cube (Cube) – Cube on extended grid

  • halo_radius (float) – Size of border to remove, in metres

Return type:

Cube

Returns:

New cube with the halo removed.

remove_halo_from_cube(cube, width_x, width_y)[source]

Method to remove rows/columns from the edge of an iris cube. Used to ‘unpad’ cubes which have been previously padded by pad_cube_with_halo.

Parameters:
  • cube (Cube) – The original cube to be trimmed of edge data. The cube should contain only x and y dimensions, so will generally be a slice of a cube.

  • width_x (float) – The width in x directions of the neighbourhood radius in grid cells. This will be the width of padding to be added to the numpy array.

  • width_y (float) – The width in y directions of the neighbourhood radius in grid cells. This will be the width of padding to be added to the numpy array.

Return type:

Cube

Returns:

Cube containing the new trimmed cube, with appropriate changes to the cube’s dimension coordinates.