Xarray engine: splitting options

The GRIB data in this example contains pressure and model level fields. Since it cannot form a hypercube to_xarray() fails.

[1]:
import earthkit.data as ekd

ds_fl = ekd.from_source("sample", "mixed_pl_ml.grib").to_fieldlist()
try:
    ds_xr = ds_fl.to_xarray(profile="earthkit")
except Exception as e:
    print(e)

Dimension 'level_type' of variable 't' cannot have multiple values=['hybrid', 'pressure']

In this case we can use the split_dims option to split the hypercube along the problematic dimensions. split_dims` does not use dimension names but takes a single or multiple GRIB keys to perform the splitting on. The result is a tuple of two lists:

  • the first list contains the Xarray datasets

  • the second list contains the corresponding dictionaries with the spitting keys/values (one dictionary per dataset)

Please note that this option cannot be used when the Xarray is directly generated via xarray.open_dataset().

[2]:
ds_xr, keys_xr = ds_fl.to_xarray(profile="earthkit", split_dims="vertical.level_type")
len(ds_xr)
[2]:
2

The first dataset:

[3]:
ds_xr[0]
[3]:
<xarray.Dataset> Size: 176kB
Dimensions:                  (forecast_reference_time: 4, step: 2, level: 2,
                              latitude: 19, longitude: 36)
Coordinates:
  * forecast_reference_time  (forecast_reference_time) datetime64[ns] 32B 202...
  * step                     (step) timedelta64[ns] 16B 00:00:00 06:00:00
  * level                    (level) int64 16B 90 137
  * latitude                 (latitude) float64 152B 90.0 80.0 ... -80.0 -90.0
  * longitude                (longitude) float64 288B 0.0 10.0 ... 340.0 350.0
Data variables:
    t                        (forecast_reference_time, step, level, latitude, longitude) float64 88kB ...
    u                        (forecast_reference_time, step, level, latitude, longitude) float64 88kB ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF

The related dictionary:

[4]:
keys_xr[0]
[4]:
{'vertical.level_type': 'hybrid'}
[ ]: