Provide the additional arguments to the ekt.spatial.reduce method to compute the
latitudinally weighted mean, over space and time, for the nuts_regions.
For me details and hints, see the Reducing data-cubes using geometry objects tutorial.
from earthkit import data as ekd
from earthkit import geo as ekg
from earthkit import plots as ekp
from earthkit import transforms as ekt
from earthkit.transforms._tools import earthkit_remote_test_data_fileLoad some test data¶
In this example we will use hourly ERA5 2m temperature data on a 0.5x0.5 spatial grid for the year 2015 as
our physical data; and we will use the NUTS regions geometries which are available in earthkit.geo.
# NUTS regions
nuts_data = ekg.gisco.nuts_regions(level=0).to_geopandas()
nuts_data.head()Loading...
remote_era5_file = earthkit_remote_test_data_file("era5_temperature_europe_20150101.grib")
era5_data = ekd.from_source("url", remote_era5_file)
# Convert to xarray, we keep all time steps for the challenge
era5_xr = era5_data.to_xarray(time_dims=["valid_time"]).rename({"2t": "t2m"})
era5_xrLoading...
Reduce data¶
reduced_data = ekt.spatial.reduce(
era5_xr,
nuts_data,
mask_dim="NAME_ENGL",
how="mean", # Default value
extra_reduce_dims=["valid_time"], # Reduce also in the time dimension
weights="latitude", # Weight the mean with latitude
)
# Add the reduced data back to the original GeoDataFrame for plotting
nuts_data = nuts_data.assign(t2m=reduced_data.t2m)Below we compare the unweighted and weighted means, we do this by adding the difference in t2m to the geopandas and plotting using the geo.choropleth method in earthkit.plots.
fig = ekp.geo.choropleth(
nuts_data,
z="t2m",
domain="Europe",
metadata={"units": "K", "long_name": "2m temperature"},
units="celsius",
labels="{t2m:0.2f}",
)
fig.coastlines()
fig.borders()
fig.show()
<earthkit.plots.components.figures.Figure at 0x12fa9daf0>