General temporal aggregation methods

[1]:
from earthkit import data as ekd
from earthkit import plots as ekp
from earthkit import transforms as ekt
from earthkit.transforms._tools import earthkit_remote_test_data_file

Load some test data

All earthkit-transforms methods can be called with earthkit-data objects (Readers and Wrappers) or with the pre-loaded xarray.

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.

First we download (if not already cached) lazily load the ERA5 data (please see tutorials in earthkit-data for more details in cache management).

We convert the data to an xarray dataset using some options which are preferred for our handling of the data we are working with. The earthkit transforms methods can handle out of the box earthkit data objects, but for clarity we create the xarray objects here.

[2]:
# Get some demonstration ERA5 data, this could be any url or path to an ERA5 grib or netCDF file.
remote_era5_file = earthkit_remote_test_data_file("era5_temperature_europe_2015.grib")
era5_data = ekd.from_source("url", remote_era5_file)
era5_xr = era5_data.to_xarray(time_dims=["valid_time"]).rename({"2t": "t2m"})
era5_xr

[2]:
<xarray.Dataset> Size: 660MB
Dimensions:     (valid_time: 1460, latitude: 201, longitude: 281)
Coordinates:
  * valid_time  (valid_time) datetime64[us] 12kB 2015-01-01 ... 2015-12-31T18...
  * latitude    (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0
  * longitude   (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0
Data variables:
    t2m         (valid_time, latitude, longitude) float64 660MB ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF

Reduce the ERA5 data over the time dimension

The default reduction method is mean, other methods can be applied using the how kwarg.

Note that we do not need to worry about the data format of the input array, earthkit will convert it to the required xarray format internally.

The returned object is an xarray dataset, however this may change in future version of the package.

The mean over the time dimension

[3]:
era5_t_mean = ekt.temporal.reduce(era5_xr)
era5_t_mean
[3]:
<xarray.Dataset> Size: 456kB
Dimensions:    (latitude: 201, longitude: 281)
Coordinates:
  * latitude   (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0
  * longitude  (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0
Data variables:
    t2m        (latitude, longitude) float64 452kB 262.5 262.6 ... 297.4 294.4
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF
[4]:
# A simple matplotlib plot to view the data:
ekp.geo.plot(
    era5_t_mean.t2m,
)
[4]:
<earthkit.plots.components.maps.Map at 0x79775c117fd0>
../../_images/tutorials_temporal_01-era5-general-methods_6_1.png

The median over the time dimension

[5]:
era5_t_median = ekt.temporal.reduce(era5_xr, how="median")
era5_t_median
[5]:
<xarray.Dataset> Size: 456kB
Dimensions:    (latitude: 201, longitude: 281)
Coordinates:
  * latitude   (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0
  * longitude  (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0
Data variables:
    t2m        (latitude, longitude) float64 452kB 262.2 262.2 ... 298.2 294.9
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF
[6]:
# A simple matplotlib plot to view the data:
ekp.geo.plot(era5_t_median)
[6]:
<earthkit.plots.components.maps.Map at 0x7977269dec90>
../../_images/tutorials_temporal_01-era5-general-methods_9_1.png

Calling the temporal reduce method with an arbitary function

The temporal.reduce method can take any method which is accepted by the xarray reduce method, typically this means it must take axis as an argument. See the xarray.Dataset.reduce documentation for more details.

[7]:
import numpy as np


def my_method(array, axis=None, **kwargs):
    _data = np.mean(array, axis=axis, **kwargs) * np.std(array, axis=axis, **kwargs)
    _normalised = _data * (np.mean(array) / np.mean(_data))
    return _normalised


era5_t_my_method = ekt.temporal.reduce(era5_xr, how=my_method, how_label="random")
era5_t_my_method
[7]:
<xarray.Dataset> Size: 456kB
Dimensions:     (latitude: 201, longitude: 281)
Coordinates:
  * latitude    (latitude) float64 2kB 80.0 79.75 79.5 79.25 ... 30.5 30.25 30.0
  * longitude   (longitude) float64 2kB -10.0 -9.75 -9.5 ... 59.5 59.75 60.0
Data variables:
    t2m_random  (latitude, longitude) float64 452kB 299.7 300.5 ... 400.9 387.9
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF
[8]:
# A simple matplotlib plot to view the data:
ekp.geo.plot(era5_t_my_method)
[8]:
<earthkit.plots.components.maps.Map at 0x7977268d3d90>
../../_images/tutorials_temporal_01-era5-general-methods_12_1.png
[ ]: