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")
ds_fl.ls(keys=["param", "step", "stepRange", "startStep", "endStep"])
[1]:
| param | step | stepRange | startStep | endStep | |
|---|---|---|---|---|---|
| 0 | lsp | 71-72 | 71-72 | 71 | 72 |
| 1 | lsp | 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 “step_timedelta” generated metadata key that is the timedelta representation of the “endStep” GRIB key.
[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:
param: lsp
paramId: 142
class: d1
stream: oper
levtype: sfc
type: fc
expver: 0001
date: 20250527
time: 0
domain: g
Conventions: CF-1.8
institution: ECMWFWe 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": "startStep"})
[int(x* 1E-9/(3600)) for x in ds["step"].values]
[4]:
[71, 72]
[ ]: