NEDAS.grid.grid_irregular module

class NEDAS.grid.grid_irregular.IrregularGrid(proj, x, y, bounds=None, cyclic_dim=None, distance_type='cartesian', triangles=None, dst_grid=None)[source]

Bases: Grid2DBase

Class for handling irregular grids.

Parameters:

triangles (np.ndarray, optional) – None (default), if the triangle indices ind[grid.x.size, 3] for the unstructured mesh are given here, the grid will skip computing triangulation.

change_resolution_level(nlevel)[source]
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.

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’