reduce

earthkit.transforms.spatial.reduce(dataarray: Dataset | DataArray, geodataframe: GeoDataFrame | None = None, mask_arrays: DataArray | list[DataArray] | None = None, area: dict | None = None, **kwargs) Dataset | DataArray[source]

Apply a shape object to an xarray.DataArray object using the specified ‘how’ method.

Geospatial coordinates are reduced to a dimension representing the list of features in the shape object.

Parameters:
  • dataarray – Xarray data object (must have geospatial coordinates).

  • geodataframe – Geopandas Dataframe containing the polygons for aggregations. Cannot be provided together with area.

  • area (dict, optional) – Dictionary with keys "north", "south", "east", "west" defining a bounding box. Converted to a single-polygon GeoDataFrame internally. Areas that cross the anti-meridian (west > east) are not currently supported.

  • mask_arrays – precomputed mask array[s], if provided this will be used instead of creating a new mask. They must be on the same spatial grid as the dataarray.

  • how – method used to apply mask. Default=’mean’, which calls xp.nanmean

  • weights – Provide weights for aggregation, also accepts recognised keys for weights, e.g. ‘latitude’

  • lat_key/lon_key – key for latitude/longitude variable, default behaviour is to detect variable keys.

  • extra_reduce_dims – any additional dimensions to aggregate over when reducing over spatial dimensions

  • mask_dim – dimension that will be created after the reduction of the spatial dimensions, default is the index of the dataframe

  • all_touched – If True, all pixels touched by geometries will be considered in, if False, only pixels whose center is within. Default is False. Only valid for regular data.

  • mask_kwargs – Any kwargs to pass into the mask method

  • mask_arrays – precomputed mask array[s], if provided this will be used instead of creating a new mask. They must be on the same spatial grid as the dataarray.

  • return_as – what format to return the data object, pandas or xarray. Work In Progress

  • compact – If True, return a compact pandas.DataFrame with the reduced data as a new column. If False, return a fully expanded pandas.DataFrame. Only valid if return_as is pandas

  • how_label – label to append to variable name in returned object, default is not to append

  • kwargs – kwargs recognised by the how function

Returns:

A data array with dimensions features + data.dims not in ‘lat’,’lon’. Each slice of layer corresponds to a feature in layer.

Return type:

xarray.Dataset | xarray.DataArray