grid_process.helpers#

Module for general helper functions and decorators

Attributes#

Functions#

timefn(→ collections.abc.Callable[..., Any])

Generic decorator to log function processing times

make_accessor(…)

Generic decorator to turn functions operating on xarray objects into

keepattrs(→ collections.abc.Callable[..., xarray.Dataset])

Decorator to keep dataset and data variables attributes for functions

update_vars_attrs(→ None)

Updates data variables attributes in a xarray.Dataset. The operation is

replace_vars_attrs(→ None)

Replaces data variables attributes in a xarray.Dataset. The operation is

reproject(…)

Reprojects x and y coordinates from a source CRS to a new specified

sphere_as_wgs84(→ pyproj.CRS)

Returns a geographic CRS defined on a sphere (e.g. the rotated pole or latitude/longitude of a CF grid mapping

outer_ring(→ tuple[numpy.ndarray, numpy.ndarray])

Coordinates of the outer ring of points of a structured grid, from 1D (dimension) or 2D coordinates in the

Module Contents#

logger#
timefn(func: collections.abc.Callable[..., Any]) → collections.abc.Callable[..., Any][source]#

Generic decorator to log function processing times

make_accessor(func: collections.abc.Callable[..., xarray.Dataset], accessor_class: type) → collections.abc.Callable[..., xarray.Dataset][source]#
make_accessor(func: collections.abc.Callable[..., xarray.DataArray], accessor_class: type) → collections.abc.Callable[..., xarray.DataArray]

Generic decorator to turn functions operating on xarray objects into accessors based on a registered dataset or dataarray accessor_class

keepattrs(func: collections.abc.Callable[..., xarray.Dataset]) → collections.abc.Callable[..., xarray.Dataset][source]#

Decorator to keep dataset and data variables attributes for functions operating on xarray.Datasets

update_vars_attrs(ds: xarray.Dataset, vars_attrs: dict[collections.abc.Hashable, dict[collections.abc.Hashable, Any]]) → None[source]#

Updates data variables attributes in a xarray.Dataset. The operation is done “in-place”.

Parameters:
  • ds (xarray.Dataset) – can contain one or several data variables

  • vars_attrs (dict) – contains the attributes of each data variables in the input dataset

replace_vars_attrs(ds: xarray.Dataset, vars_attrs: dict[str, dict]) → None[source]#

Replaces data variables attributes in a xarray.Dataset. The operation is done “in-place”.

Parameters:
  • ds (xarray.Dataset) – can contain one or several data variables

  • vars_attrs (dict) – contains the new attributes of each data variables in the input dataset to be replaced

reproject(x_coords: numpy.typing.NDArray[numpy.floating], y_coords: numpy.typing.NDArray[numpy.floating], src_crs: pyproj.CRS | str, dst_crs: pyproj.CRS | str) → tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]][source]#
reproject(x_coords: list[float], y_coords: list[float], src_crs: pyproj.CRS | str, dst_crs: pyproj.CRS | str) → tuple[list[float], list[float]]
reproject(x_coords: tuple[float, ...], y_coords: tuple[float, ...], src_crs: pyproj.CRS | str, dst_crs: pyproj.CRS | str) → tuple[tuple[float, ...], tuple[float, ...]]

Reprojects x and y coordinates from a source CRS to a new specified one.

Parameters:
  • x_coords (numpy.ndarray, list or tuple) – contains x coordinates.

  • y_coords (numpy.ndarray, list or tuple) – contains y coordinates

  • src_crs (pyproj.CRS of the source or equivalent string) – The value can be anything accepted by pyproj.CRS.from_user_input, such as an authority string (e.g. “EPSG:4326”) or a WKT string

  • dst_crs (pyproj.CRS of the destination or equivalent string) – The value can be anything accepted by pyproj.CRS.from_user_input, such as an authority string (eg “EPSG:4326”) or a WKT string

Returns:

  • x_coords_new (numpy.ndarray, list or tuple of float64) – same as input x_coords in the new CRS and cast as float64

  • y_coords_new (numpy.ndarray, list or tuple of float64) – same as input y_coords in the new CRS and cast as float64

Note

Geographic CRS defined on a sphere without datum (e.g. the rotated pole of a CF grid mapping with earth_radius) are considered to be based on WGS84, see sphere_as_wgs84(). CRS parsed from such a CF “grid_mapping” attribute are already based on WGS84 (see parse_grid_mapping()), this covers the other ones (e.g. “crs” attribute, POIs CRS or direct calls).

sphere_as_wgs84(crs: pyproj.CRS | str) → pyproj.CRS[source]#

Returns a geographic CRS defined on a sphere (e.g. the rotated pole or latitude/longitude of a CF grid mapping with earth_radius, as used by NWP models) with WGS84 as base CRS instead, keeping the pole rotation if any. Other CRS are returned unchanged.

The latitudes and longitudes on the sphere of NWP models are usually considered as WGS84 latitudes and longitudes (e.g. the 2D latitude and longitude coordinates of their output). Without a defined datum, pyproj transforms them to other datums with a “ballpark” transformation that leaves out the datum shift, e.g. ~180 m in Switzerland between a rotated pole grid and Swiss LV95 coordinates. With WGS84 as base CRS, the datum shift is applied as for WGS84 latitudes and longitudes. The rotation of the pole is the same on any ellipsoid.

Parameters:

crs (pyproj.CRS or equivalent string) – anything accepted by pyproj.CRS.from_user_input

Return type:

pyproj.CRS

outer_ring(x_coords: numpy.ndarray, y_coords: numpy.ndarray) → tuple[numpy.ndarray, numpy.ndarray][source]#

Coordinates of the outer ring of points of a structured grid, from 1D (dimension) or 2D coordinates in the order (y, x). On a grid, it encloses the image of the whole grid in any continuous projection, e.g. the bounding box of a reprojected grid is the bounding box of its reprojected outer ring.

Parameters:

x_coords, y_coords (numpy.ndarray) – 1D coordinates of the x and y dimensions, or 2D coordinates of shape (y, x)

Returns:

x_ring, y_ring – 1D coordinates of the outer ring points (first and last rows, then first and last columns)

Return type:

numpy.ndarray