lair.geo.generate_regular_grid#

lair.geo.generate_regular_grid(xmin, xmax, dx, ymin, ymax, dy, x_label='x', y_label='y', chunks=None)[source]#

Generate a regular grid. Grid points are cell centers.

Cells start at xmin/ymin and continue while their center is below xmax/ymax (a center landing exactly on the max edge may or may not be included, depending on float error). Centers are min + d * (i + 0.5), rounded to 10 decimals only to drop float noise (40.35, not 40.349999999999994), which matches PYSTILT’s grid axes.

When the domain is not a whole number of cells, a cell is kept if its center is inside the domain, so the last cell ends within half a cell of the max edge, past it or short of it. ymin=40.45, ymax=40.93 and dy=0.05 give 10 rows, the last centered at 40.925 and ending at 40.95; rounding the count down would give 9 rows.

Parameters:
  • xmin (float) – x value of the left edge of the grid

  • xmax (float) – x value of the right edge of the grid

  • dx (float) – x resolution

  • ymin (float) – y value of the bottom edge of the grid

  • ymax (float) – y value of the top edge of the grid

  • dy (float) – y resolution

  • x_label (str, optional) – Name of the x coordinate, by default ‘x’

  • y_label (str, optional) – Name of the y coordinate, by default ‘y’

  • chunks (dict, optional) – If given, chunk the grid with dask (DataArray.chunk(chunks)).

Returns:

xr.DataArray – The generated grid, dims (y_label, x_label)

Return type:

DataArray

See also

regrid

Regrids data to such a grid.

Examples

Cells of 0.1 degree from 112.2 W to 111.8 W and 40.6 N to 41.0 N:

>>> from lair.geo import generate_regular_grid
>>> grid = generate_regular_grid(
...     -112.2, -111.8, 0.1, 40.6, 41.0, 0.1, x_label="lon", y_label="lat"
... )
>>> grid.lon.values
array([-112.15, -112.05, -111.95, -111.85])
>>> grid.lat.values
array([40.65, 40.75, 40.85, 40.95])

A domain that is not a whole number of cells keeps the cell whose center is inside it:

>>> grid = generate_regular_grid(
...     -112.2, -111.8, 0.1, 40.45, 40.93, 0.05, x_label="lon", y_label="lat"
... )
>>> grid.lat.size, float(grid.lat[-1])
(10, 40.925)