Xarray engine: seasonal forecast
[1]:
import earthkit.data as ekd
ds_fl = ekd.from_source("sample", "seasonal_monthly.grib")
The input data contains seasonal monthly forecast. Because the length of a month varies, for this data the forecastMonth 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="forecastMonth")
[2]:
| centre | shortName | typeOfLevel | level | dataDate | dataTime | stepRange | dataType | number | gridType | forecastMonth | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | lfpw | 2t | surface | 0 | 19931001 | 0 | 744 | fcmean | 0 | regular_ll | 1 |
| 1 | lfpw | 2t | surface | 0 | 19931001 | 0 | 744 | fcmean | 1 | regular_ll | 1 |
| 2 | lfpw | 2t | surface | 0 | 19931001 | 0 | 744 | fcmean | 2 | regular_ll | 1 |
| 3 | lfpw | 2t | surface | 0 | 19931001 | 0 | 1464 | fcmean | 0 | regular_ll | 2 |
In order to use forecastMonth instead of step we need to use the dim_roles option in to_xarray().
[3]:
ds = ds_fl.to_xarray(time_dim_mode="forecast",
dim_roles={"step": "forecastMonth"})
ds
[3]:
<xarray.Dataset> Size: 395kB
Dimensions: (number: 3, forecast_reference_time: 4, step: 6,
latitude: 19, longitude: 36)
Coordinates:
* number (number) int64 24B 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 (number, forecast_reference_time, step, latitude, longitude) float64 394kB ...
Attributes: (12/15)
param: 2t
paramId: 167
class: c3
stream: msmm
levtype: sfc
type: fcmean
... ...
fcmonth: 1
origin: lfpw
domain: g
method: 1
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 “forecastMonth” GRIB 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_dim_mode="forecast",
dim_roles={"step": "forecastMonth"},
dim_name_from_role_name=False)
ds
[5]:
<xarray.Dataset> Size: 395kB
Dimensions: (number: 3, forecast_reference_time: 4,
forecastMonth: 6, latitude: 19, longitude: 36)
Coordinates:
* number (number) int64 24B 0 1 2
* forecast_reference_time (forecast_reference_time) datetime64[ns] 32B 199...
* forecastMonth (forecastMonth) 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 (number, forecast_reference_time, forecastMonth, latitude, longitude) float64 394kB ...
Attributes: (12/15)
param: 2t
paramId: 167
class: c3
stream: msmm
levtype: sfc
type: fcmean
... ...
fcmonth: 1
origin: lfpw
domain: g
method: 1
Conventions: CF-1.8
institution: ECMWF