grid_process.data_model#

Module with utility functions to deal with the numerous supported data models

Attributes#

Functions#

get_t_dim(→ str)

Returns name of temporal dimension according to the possible options defined

get_temporal_dims(→ tuple[str | None, str | None, ...)

Returns names of temporal dimensions according to the possible options defined

get_temporal_coords(→ tuple[str | None, str | None, ...)

Returns names of temporal coordinates according to the possible options defined

get_reftime(→ str | None)

Returns name of forecast reference time dimension or coordinate according to

get_validtime(…)

Returns name of valid time dimension or coordinate according to the possible

get_leadtime(→ str | None)

Returns name of lead time dimension or coordinate according to the possible

get_ensemble_dim(→ str)

Returns name of ensemble members dimension according to the possible options

set_grid_mapping(→ xarray.Dataset)

Set grid_mapping attribute according to internal conventions when the input

get_spatial_dims(→ tuple[str, ...])

Returns names of spatial dimensions according to the possible options defined

get_spatial_coords(→ tuple[str, str])

Returns names of non-dimension spatial coordinates according to the

get_flat_dim(→ str | None)

Returns spatial dimension name for an unstructured grid according to the

get_latlon_coords(→ tuple[str, str] | None)

Returns names of latitude and longitude coordinates if they exist according

get_latlon_dims(→ tuple[str, str] | None)

Returns names of latitude and longitude dimensions if they exist according

get_surf_alt(→ str)

Returns name of the surface altitude coordinate variable according to the

get_poi_alt(→ numpy.typing.NDArray[numpy.float32])

Returns POIs altitudes from poi_info according to the column names defined

get_grid_crs(→ pyproj.CRS)

Get correct CRS according to supported conventions.

parse_grid_mapping(→ pyproj.CRS)

Parse internal grid database style "grid_mapping" attribute and decode it with Pyproj.

is_metric_crs(→ bool)

Whether crs is a projected CRS with all axes in meters (e.g. LV95, LV03 or UTM), i.e. in which euclidean

get_coords_name_crs(→ tuple[str, str, pyproj.CRS])

Get correct coordinate names and CRS according to supported conventions. When

is_decreasing1d(→ bool)

Whether 1 dimensional coords are in decreasing order.

is_decreasing(→ bool)

Checks whether coords are in decreasing order along dimension dim.

Module Contents#

logger#
F#
DATA_MODEL#
FLAT_DIM: list[str]#
YX: tuple[tuple[str, str], ...]#
LATLON: tuple[tuple[str, str], ...]#
SURF_ALT: list[str]#
REFTIME: list[str]#
VALIDTIME: list[str]#
LEADTIME: list[str]#
ENS_DIM: list[str]#
POI_ALT_COL: list[str]#
get_t_dim(ds: xarray.Dataset) → str[source]#

Returns name of temporal dimension according to the possible options defined in the config (could be overwritten by setting appropriate env variable). Temporal dimension could be expressed as valid time or lead time.

get_temporal_dims(data: xarray.Dataset | xarray.DataArray) → tuple[str | None, str | None, str | None][source]#

Returns names of temporal dimensions according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name. The returned tuple contains dimension names in the following order: (reftime, leadtime, validtime).

get_temporal_coords(data: xarray.Dataset | xarray.DataArray) → tuple[str | None, str | None, str | None][source]#

Returns names of temporal coordinates according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name. The returned tuple contains coordinate names in the following order: (reftime, leadtime, validtime).

get_reftime(data: xarray.Dataset | xarray.DataArray, kind: Literal['dim', 'coord'] = 'dim') → str | None[source]#

Returns name of forecast reference time dimension or coordinate according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name.

get_validtime(data: xarray.Dataset | xarray.DataArray, kind: Literal['dim', 'coord'] = 'dim', raise_notfound: Literal[True] = True) → str[source]#
get_validtime(data: xarray.Dataset | xarray.DataArray, kind: Literal['dim', 'coord'] = 'dim', raise_notfound: Literal[False] = False) → str | None

Returns name of valid time dimension or coordinate according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name. When raise_notfound is set to True, raise a ValueError if a valid time dimension/coordinate can not be found.

get_leadtime(data: xarray.Dataset | xarray.DataArray, kind: Literal['dim', 'coord'] = 'dim') → str | None[source]#

Returns name of lead time dimension or coordinate according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name.

get_ensemble_dim(data: xarray.Dataset | xarray.DataArray) → str[source]#

Returns name of ensemble members dimension according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF standard name.

set_grid_mapping(ds: xarray.Dataset) → xarray.Dataset[source]#

Set grid_mapping attribute according to internal conventions when the input grid follow CF conventions (grid mapping encoded as a data variable). The data variable encoding the grid mapping is removed.

get_spatial_dims(grid: xarray.Dataset | xarray.DataArray) → tuple[str, ...][source]#

Returns names of spatial dimensions according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF conventions. The returned tuple has size one in case of unstructured grid otherwise contains dimension names in the order (y, x).

get_spatial_coords(grid: xarray.Dataset | xarray.DataArray) → tuple[str, str][source]#

Returns names of non-dimension spatial coordinates according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF conventions. The returned tuple contains coordinate names in the order (y, x).

get_flat_dim(grid: xarray.Dataset | xarray.DataArray) → str | None[source]#

Returns spatial dimension name for an unstructured grid according to the possible options defined in the config (could be overwritten by setting appropriate env variable).

get_latlon_coords(grid: xarray.Dataset | xarray.DataArray, nondim: bool = True) → tuple[str, str] | None[source]#

Returns names of latitude and longitude coordinates if they exist according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF conventions. The returned tuple contains coordinate names in the order (latitude, longitude). When nondim is True (default), only checks coordinates that are non-dimensional.

get_latlon_dims(grid: xarray.Dataset | xarray.DataArray) → tuple[str, str] | None[source]#

Returns names of latitude and longitude dimensions if they exist according to the possible options defined in the config (could be overwritten by setting appropriate env variable) or according to CF conventions. The returned tuple contains coordinate names in the order (latitude, longitude).

get_surf_alt(grid: xarray.Dataset | xarray.DataArray) → str[source]#

Returns name of the surface altitude coordinate variable according to the possible options defined in the config (could be overwritten by setting appropriate env variable).

get_poi_alt(poi_info: geopandas.GeoDataFrame) → numpy.typing.NDArray[numpy.float32][source]#

Returns POIs altitudes from poi_info according to the column names defined in the config (could be overwritten by setting appropriate env variable) or from a “z” geometry. Altitude data is expected to be in meters above sea level.

get_grid_crs(grid: xarray.Dataset | xarray.DataArray) → pyproj.CRS[source]#

Get correct CRS according to supported conventions.

Parameters:

grid (xarray Dataset or DataArray) – contains spatial horizontal coordinates. Must have a “grid_mapping” or “crs” attribute referencing the spatial dimensions unless longitude and latitude (or configured equivalents) are dimensions.

Returns:

pyproj representation of input grid coordinate reference system

Return type:

pyproj CRS

See also

set_grid_mapping()

parse_grid_mapping(grid_mapping: str | dict[str, Any]) → pyproj.CRS[source]#

Parse internal grid database style “grid_mapping” attribute and decode it with Pyproj.

Parameters:

grid_mapping (dict or dict encapsulated in a string) – contains CF conventions attributes parsable by pyproj.CRS.from_cf() or the key “epsg_code” with value being an authority string (e.g. EPSG:4326)

Returns:

pyproj representation of input grid_mapping

Return type:

pyproj CRS

See also

get_grid_crs()

Note

CF latitude_longitude and rotated_latitude_longitude mappings without ellipsoid parameters other than earth_radius (as NWP models on a sphere) are built directly on WGS84, which is much faster than pyproj.CRS.from_cf(). The result is the same as sphere_as_wgs84(pyproj.CRS.from_cf(grid_mapping)), see sphere_as_wgs84().

is_metric_crs(crs: pyproj.CRS) → bool[source]#

Whether crs is a projected CRS with all axes in meters (e.g. LV95, LV03 or UTM), i.e. in which euclidean distances can be computed directly from the coordinates. The axis units are compared by their conversion factor to the meter, so that units written with another name (e.g. “m” or “Meter” in a WKT without authority) are recognized, while kilometers or feet are not.

get_coords_name_crs(grid: xarray.Dataset | xarray.DataArray, spatial_dims: tuple[str, ...] | None = None, prefer_projected: bool = False) → tuple[str, str, pyproj.CRS][source]#

Get correct coordinate names and CRS according to supported conventions. When non-dimensional longitude and latitude coordinates arrays are present, these are used instead of the native spatial dimension coordinates, unless prefer_projected is True and the native CRS is projected with axes in meters.

Parameters:
  • grid (xarray Dataset or DataArray) – contains spatial horizontal coordinates. Must have a “grid_mapping” or “crs” attribute referencing the spatial dimensions unless longitude and latitude coordinates are present.

  • spatial_dims (tuple of string(s), optional) – name of the grid spatial dimension(s). Has size one in case of unstructured grid otherwise contains dimension names in the order (y, x). If not provided, calls get_spatial_dims()

  • prefer_projected (bool, optional) – If True, return the native spatial coordinates when their CRS is projected with axes in meters (e.g. LV95), even if longitude and latitude coordinates are also present. Native coordinates in degrees (e.g. rotated pole) still give the longitude and latitude coordinates when present. Default is False.

Returns:

  • grid_y (str) – name of the northward coordinate. Usually ‘x’ or ‘longitude’

  • grid_x (str) – name of the eastward coordinate. Usually ‘y’ or ‘latitude’’

  • grid_crs (pyproj CRS) – pyproj representation of input grid coordinate reference system

is_decreasing1d(coords: xarray.DataArray) → bool[source]#

Whether 1 dimensional coords are in decreasing order.

is_decreasing(coords: xarray.DataArray, dim: str) → bool[source]#

Checks whether coords are in decreasing order along dimension dim.