Xarray engine: step range

Get input GRIB2 data containing precipitation forecast for step ranges.

[1]:
import earthkit.data as ekd

ds_fl = ekd.from_source("sample", "lsp_step_range.grib2").to_fieldlist()
ds_fl.ls(
    keys=[
        "parameter.variable",
        "time.step",
        "metadata.step",
        "metadata.stepRange",
        "metadata.startStep",
        "metadata.endStep",
    ]
)
[1]:
parameter.variable time.step metadata.step metadata.stepRange metadata.startStep metadata.endStep
0 lsp 3 days 00:00:00 71-72 71-72 71 72
1 lsp 3 days 01:00:00 72-73 72-73 72 73

When we convert GRIB data to Xarray with to_xarray() the step dimension is defined by the “step” dimension role. By default, this role is using the “time.step” metadata key, which is derived from the “endStep” GRIB key for GRIB data. Please note that ecCodes GRIB keys are prefixed with “metadata.” in earthkit-data.

[2]:
ds = ds_fl.to_xarray()
ds
[2]:
<xarray.Dataset> Size: 2kB
Dimensions:    (step: 2, latitude: 7, longitude: 12)
Coordinates:
  * step       (step) timedelta64[ns] 16B 3 days 3 days 01:00:00
  * latitude   (latitude) float64 56B 90.0 60.0 30.0 0.0 -30.0 -60.0 -90.0
  * longitude  (longitude) float64 96B 0.0 30.0 60.0 90.0 ... 270.0 300.0 330.0
Data variables:
    lsp        (step, latitude, longitude) float64 1kB ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF

We can check the “step” coordinate in the dataset to see that it matches the “endStep” values.

[3]:
# convert to hours from ns
[int(x * 1e-9 / (3600)) for x in ds["step"].values]
[3]:
[72, 73]

This default behaviour can be overridden by specifying custom dim_roles. E.g. to get the step from the “startStep” key we can use:

[4]:
ds = ds_fl.to_xarray(dim_roles={"step": "metadata.startStep"})
[int(x * 1e-9 / (3600)) for x in ds["step"].values]
[4]:
[71, 72]
[ ]: