NEDAS.grid.grid_regular module

class NEDAS.grid.grid_regular.RegularGrid(proj, x, y, bounds=None, cyclic_dim=None, distance_type='cartesian', pole_dim=None, pole_index=None, neighbors=None, dst_grid=None)[source]

Bases: Grid2DBase

Regular 2D grid class

Parameters:
  • pole_dim (str, optional) – None (default), if one of the dimension has poles, 'x' or 'y'

  • pole_index (str, optional) – None (default), tuple of the pole index(s) in pole_dim

  • neighbors (np.ndarray, optional) – None (default), for regular grid with special geometry (e.g. tripolar ocean grid), neighbors stores the j,i index of 4 neighors (east, north, west, and south) on each grid point. Since specifying neighbors already take care of cyclic boundary conditions, cyclic_dim will be discarded if neighbors is set.

pole_dim: str | None
pole_index: list[int] | None
nx: int
ny: int
change_resolution_level(nlevel)[source]

Generate a new grid with changed resolution.

Parameters:

nlevel (int) – Positive number, downsample grid abs(nlevel) times, each time doubling the grid spacing; Negative number, upsample grid abs(nlevel) times, each time halving the grid spacing

Returns:

A new grid object with changed resolution.

find_index(x_, y_)[source]

Find indices of self.x, self.y corresponding to the given x_, y_.

Parameters:
  • x (float or np.ndarray) – x-coordinates of target point(s).

  • y (float or np.ndarray) – y-coordinates of target point(s).

Outputs:
inside (np.ndarray of bool): Boolean array of shape (x_.size,) indicating whether

each x_, y_ point lies inside the grid.

indices (np.ndarray of int or None): Indices of grid elements containing the input points.
  • For regular grids, this is None since vertices suffice to locate the grid box.

  • For unstructured meshes, these are indices into tri.triangles, from tri_finder.

vertices (np.ndarray of int): Array of shape (inside_size, n), where

n = 4 for regular grid boxes or n = 3 for mesh triangles. These are indices into self.x, self.y (flattened) for the vertices of the grid element that each point falls in.

in_coords (np.ndarray of float): Array of shape (inside_size, n) giving internal coordinates

of each point within the containing element. Used to compute interpolation weights.

nearest (np.ndarray of int): Array of shape (inside_size,) with indices of the grid nodes

closest to each point.

Notes

  • This function assumes self.x, self.y define either a regular or triangular grid.

  • Internal coordinates are used for interpolation and vary in dimension based on the grid type.

rotate_vectors(vec_fld)[source]

Apply the rotate_matrix to a vector field

Parameters:

vec_fld (np.array) – The input vector field, shape (2, self.x.shape)

Returns:

The vector field rotated to the dst_grid.

get_corners(fld)[source]

given fld defined on a regular grid, obtain its value on the 4 corners/vertices

interp(fld, x=None, y=None, method='linear')[source]

Interpolation of 2D field data (fld) from one grid (self or given x,y) to another (dst_grid). This can be used for grid refining (low->high resolution) or grid thinning (high->low resolution). This also converts between different grid geometries.

Parameters:
  • fld (np.array) – Input field defined on self, should have same shape as self.x

  • x (float or np.array) – Optional; If x,y are specified, the function computes the weights and apply them to fld If x,y are None, the self.dst_grid.x,y are used. Since their interp_weights are precalculated by dst_grid.setter it will be efficient to run interp for many different input flds quickly.

  • y (float or np.array) – Optional; If x,y are specified, the function computes the weights and apply them to fld If x,y are None, the self.dst_grid.x,y are used. Since their interp_weights are precalculated by dst_grid.setter it will be efficient to run interp for many different input flds quickly.

  • method (str) – Interpolation method, can be ‘nearest’ or ‘linear’

Returns:

The interpolated field defined on the destination grid

coarsen(fld)[source]

Coarse-graining is sometimes needed when the dst_grid is at lower resolution than self. Since many points of self.x,y falls in one dst_grid box/element, it is better to average them to represent the field on the low-res grid, instead of interpolating only from the nearest points that will cause representation errors.

Parameters:

fld (np.array) – Input field to perform coarse-graining on, it is defined on self.

Returns:

The coarse-grained field defined on self.dst_grid.

plot_field(ax, fld, vmin=None, vmax=None, cmap='viridis', **kwargs)[source]

Plot a scalar field using pcolor/tripcolor

Parameters:
  • ax (matplotlib.pyplot.Axes) – Axes handle for plotting

  • fld (np.array) – The scalar field for plotting

  • vmin (float, optional) – The minimum and maximum value range for the colormap, if not specified (None) the np.min, np.max of the input fld will be used.

  • vmax (float, optional) – The minimum and maximum value range for the colormap, if not specified (None) the np.min, np.max of the input fld will be used.

  • cmap (matplotlib colormap, or str, optional) – Colormap used in the plot, default is ‘viridis’