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:
Functions
|
Return cell edges for 1-D cell centres. |
|
Return the great-circle distance for a chord of the unit sphere. |
|
Return approximate zonal cell widths of a curvilinear grid, in metres. |
|
Return approximate meridional cell heights of a curvilinear grid, in metres. |
|
Return the great-circle distance between points, in metres. |
|
Return the exact spherical area of each cell of a rectilinear grid, in m². |
|
Return the zonal width of each cell of a rectilinear grid, in metres. |
|
Return the meridional height of each cell of a rectilinear grid, in metres. |
|
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) + 1edges.- Raises:
ValueError – If centres is not 1-D with at least two values.
- Return type:
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 twounit_vectors().radius (
float, optional) – Sphere radius in metres. DefaultEARTH_RADIUS.
- Returns:
numpy.ndarray– Distance in metres.- Return type:
- 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)withnx >= 3.lons (
array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape(ny, nx)withnx >= 3.radius (
float, optional) – Sphere radius in metres.
- Returns:
numpy.ndarray– Array of shape(ny, nx).- Return type:
- 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)withny >= 3.lons (
array_like) – 2-D cell-centre latitudes and longitudes in degrees, shape(ny, nx)withny >= 3.radius (
float, optional) – Sphere radius in metres.
- Returns:
numpy.ndarray– Array of shape(ny, nx).- Return type:
- 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. DefaultEARTH_RADIUS.
- Returns:
numpy.ndarray– Distances in metres, with the broadcast shape of the inputs.- Return type:
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 to4 π 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:
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:
- 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:
- 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:
See also
chord_to_distancegreat-circle distance from a chord length.
Examples
>>> unit_vectors([0, 90], [0, 0]).round(12) array([[1., 0., 0.], [0., 1., 0.]])