Xarray engine: temporal dimensions - seasonal forecast¶
[1]:
import earthkit.data as ekd
ds_fl = ekd.from_source("sample", "seasonal_monthly.grib").to_fieldlist()
The input data contains seasonal monthly forecast. Because the length of a month varies, for this data the time.forecast_month key is better suited for describing the temporal structure than using the ``step`` keys.
This is how the first few GRIB messages look like:
[2]:
ds_fl[0:4].ls(extra_keys="time.forecast_month")
[2]:
| parameter.variable | time.valid_datetime | time.base_datetime | time.step | vertical.level | vertical.level_type | ensemble.member | geography.grid_type | time.forecast_month | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 2t | 1993-11-01 | 1993-10-01 | 31 days | 0 | surface | 0 | regular_ll | 1 |
| 1 | 2t | 1993-11-01 | 1993-10-01 | 31 days | 0 | surface | 1 | regular_ll | 1 |
| 2 | 2t | 1993-11-01 | 1993-10-01 | 31 days | 0 | surface | 2 | regular_ll | 1 |
| 3 | 2t | 1993-12-01 | 1993-10-01 | 61 days | 0 | surface | 0 | regular_ll | 2 |
In order to use time.forecast_month instead of step we need to use the dim_roles option in to_xarray().
[3]:
ds = ds_fl.to_xarray(time_dims=["forecast_reference_time", "step"], dim_roles={"step": "time.forecast_month"})
ds
[3]:
<xarray.Dataset> Size: 395kB
Dimensions: (member: 3, forecast_reference_time: 4, step: 6,
latitude: 19, longitude: 36)
Coordinates:
* member (member) <U1 12B '0' '1' '2'
* forecast_reference_time (forecast_reference_time) datetime64[ns] 32B 199...
* step (step) int64 48B 1 2 3 4 5 6
* 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:
2t (member, forecast_reference_time, step, latitude, longitude) float64 394kB ...
Attributes:
Conventions: CF-1.8
institution: ECMWFWhen we check the “step” dimension we can see its units are “months”.
[4]:
print(ds["step"])
<xarray.DataArray 'step' (step: 6)> Size: 48B
array([1, 2, 3, 4, 5, 6])
Coordinates:
* step (step) int64 48B 1 2 3 4 5 6
Attributes:
units: months
By default, the dimensions related to dimension roles are named after the roles. So, although the step dimension was generated from the “time.forecast_month” key the dimension name is still “step”. To override this use the dim_name_from_role_name=False option in to_xarray().
[5]:
ds = ds_fl.to_xarray(
time_dims=["forecast_reference_time", "step"],
dim_roles={"step": "time.forecast_month"},
dim_name_from_role_name=False,
)
ds
[5]:
<xarray.Dataset> Size: 395kB
Dimensions: (member: 3, forecast_reference_time: 4,
forecast_month: 6, latitude: 19, longitude: 36)
Coordinates:
* member (member) <U1 12B '0' '1' '2'
* forecast_reference_time (forecast_reference_time) datetime64[ns] 32B 199...
* forecast_month (forecast_month) int64 48B 1 2 3 4 5 6
* 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:
2t (member, forecast_reference_time, forecast_month, latitude, longitude) float64 394kB ...
Attributes:
Conventions: CF-1.8
institution: ECMWF[ ]: