seagrid.grids.metrics#

Cell metrics of horizontal grids: zonal and meridional widths and areas.

For rectilinear latitude/longitude grids the metrics are exact on a sphere of radius EARTH_RADIUS. For curvilinear grids they are approximated from great-circle distances between the midpoints of neighbouring cells, with edge cells linearly extrapolated.

ivar EARTH_RADIUS:

Mean Earth radius in metres (6 371 000 m, as in IUGG).

vartype EARTH_RADIUS:

float

Functions

cell_bounds(centres)

Return cell edges for 1-D cell centres.

chord_to_distance(chord[, radius])

Return the great-circle distance for a chord of the unit sphere.

curvilinear_dx(lats, lons[, radius])

Return approximate zonal cell widths of a curvilinear grid, in metres.

curvilinear_dy(lats, lons[, radius])

Return approximate meridional cell heights of a curvilinear grid, in metres.

great_circle(lon1, lat1, lon2, lat2[, radius])

Return the great-circle distance between points, in metres.

rectilinear_area(lat, lon[, radius])

Return the exact spherical area of each cell of a rectilinear grid, in m².

rectilinear_dx(lat, lon[, radius])

Return the zonal width of each cell of a rectilinear grid, in metres.

rectilinear_dy(lat, lon[, radius])

Return the meridional height of each cell of a rectilinear grid, in metres.

unit_vectors(lon, lat)

Return points as Cartesian vectors on the unit sphere.

seagrid.grids.metrics.cell_bounds(centres)[source]#

Return cell edges for 1-D cell centres.

Inner edges lie halfway between neighbouring centres; the outer edges are half a cell beyond the first and last centre.

Parameters:

centres (array_like) – 1-D, monotonic cell-centre coordinates with at least two values.

Returns:

numpy.ndarray – len(centres) + 1 edges.

Raises:

ValueError – If centres is not 1-D with at least two values.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Examples

>>> cell_bounds([0.5, 1.5, 2.5])
array([0., 1., 2., 3.])
seagrid.grids.metrics.chord_to_distance(chord, radius=EARTH_RADIUS)[source]#

Return the great-circle distance for a chord of the unit sphere.

Parameters:
  • chord (array_like) – Straight-line distance between two unit_vectors().

  • radius (float, optional) – Sphere radius in metres. Default EARTH_RADIUS.

Returns:

numpy.ndarray – Distance in metres.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.grids.metrics.curvilinear_dx(lats, lons, radius=EARTH_RADIUS)[source]#

Return approximate zonal cell widths of a curvilinear grid, in metres.

The width of cell (j, i) is the great-circle distance between the midpoints towards its western and eastern neighbours. The first and last column are extrapolated linearly.

Parameters:
  • lats (array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape (ny, nx) with nx >= 3.

  • lons (array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape (ny, nx) with nx >= 3.

  • radius (float, optional) – Sphere radius in metres.

Returns:

numpy.ndarray – Array of shape (ny, nx).

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.grids.metrics.curvilinear_dy(lats, lons, radius=EARTH_RADIUS)[source]#

Return approximate meridional cell heights of a curvilinear grid, in metres.

The height of cell (j, i) is the great-circle distance between the midpoints towards its southern and northern neighbours. The first and last row are extrapolated linearly.

Parameters:
  • lats (array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape (ny, nx) with ny >= 3.

  • lons (array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape (ny, nx) with ny >= 3.

  • radius (float, optional) – Sphere radius in metres.

Returns:

numpy.ndarray – Array of shape (ny, nx).

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.grids.metrics.great_circle(lon1, lat1, lon2, lat2, radius=EARTH_RADIUS)[source]#

Return the great-circle distance between points, in metres.

Uses the haversine formula, which is accurate for small distances.

Parameters:
  • lon1 (array_like) – Coordinates in decimal degrees. Broadcast against each other.

  • lat1 (array_like) – Coordinates in decimal degrees. Broadcast against each other.

  • lon2 (array_like) – Coordinates in decimal degrees. Broadcast against each other.

  • lat2 (array_like) – Coordinates in decimal degrees. Broadcast against each other.

  • radius (float, optional) – Sphere radius in metres. Default EARTH_RADIUS.

Returns:

numpy.ndarray – Distances in metres, with the broadcast shape of the inputs.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Examples

>>> round(float(great_circle(0, 0, 1, 0)) / 1000, 2)
111.19
seagrid.grids.metrics.rectilinear_area(lat, lon, radius=EARTH_RADIUS)[source]#

Return the exact spherical area of each cell of a rectilinear grid, in m².

R² Δlon |sin(lat_north) - sin(lat_south)|, with the outer latitude edges clipped to ±90°. The areas of a global grid sum to 4 π R².

Parameters:
  • lat (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • lon (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • radius (float, optional) – Sphere radius in metres.

Returns:

numpy.ndarray – Array of shape (len(lat), len(lon)).

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Examples

>>> import numpy as np
>>> lat, lon = np.arange(-89.5, 90), np.arange(-179.5, 180)
>>> bool(np.isclose(rectilinear_area(lat, lon).sum(), 4 * np.pi * EARTH_RADIUS**2))
True
seagrid.grids.metrics.rectilinear_dx(lat, lon, radius=EARTH_RADIUS)[source]#

Return the zonal width of each cell of a rectilinear grid, in metres.

The width is measured along the cell’s central latitude: R cos(lat) Δlon.

Parameters:
  • lat (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • lon (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • radius (float, optional) – Sphere radius in metres.

Returns:

numpy.ndarray – Array of shape (len(lat), len(lon)).

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.grids.metrics.rectilinear_dy(lat, lon, radius=EARTH_RADIUS)[source]#

Return the meridional height of each cell of a rectilinear grid, in metres.

Parameters:
  • lat (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • lon (array_like) – 1-D cell-centre latitudes and longitudes in degrees.

  • radius (float, optional) – Sphere radius in metres.

Returns:

numpy.ndarray – Array of shape (len(lat), len(lon)).

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.grids.metrics.unit_vectors(lon, lat)[source]#

Return points as Cartesian vectors on the unit sphere.

Nearest-neighbour searches on these vectors are exact on the sphere and have no problem at the antimeridian or the poles.

Parameters:
  • lon (array_like) – Coordinates in decimal degrees, of the same shape.

  • lat (array_like) – Coordinates in decimal degrees, of the same shape.

Returns:

numpy.ndarray – Array of shape (n, 3) for the n flattened points.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

See also

chord_to_distance

great-circle distance from a chord length.

Examples

>>> unit_vectors([0, 90], [0, 0]).round(12)
array([[1., 0., 0.],
       [0., 1., 0.]])