GRIB: generating time seriesΒΆ

We will read temperature data from a GRIB file containing forecast steps. First, we ensure the example file is available, then read it with from_source().

[1]:
from earthkit.geo.distance import nearest_point_haversine

import earthkit.data as ekd
[2]:
ekd.download_example_file("time_series.grib")
ds_in = ekd.from_source("file", "time_series.grib").to_fieldlist()
ds = ds_in.sel({"parameter.variable": "t"})

Our data contains 4 steps.

[3]:
ds.get("time.step")
[3]:
[datetime.timedelta(0),
 datetime.timedelta(seconds=10800),
 datetime.timedelta(seconds=21600),
 datetime.timedelta(seconds=32400)]

We define a reference point and get the index of the nearest gridpoint. We utilise the fact that all the fields have the same grid, so we need not do it field by field.

[4]:
lat, lon = ds.geography.latlons()
p_ref = (51.45, -0.97)
idx, dist = nearest_point_haversine(p_ref, (lat, lon))
idx
[4]:
array([12])

With the resulting index we can get the values at the nearest gridpoint.

[5]:
v = ds.values[:, idx]
v
[5]:
array([[280.44058228],
       [280.31297302],
       [280.2789917 ],
       [280.08499146]])

We extract the datetime for each step.

[6]:
t = ds.get("time.valid_datetime")

With this we can now print out the time series.

[7]:
for v1, v2 in zip(t, v):
    print(v1, v2)
2020-12-21 12:00:00 [280.44058228]
2020-12-21 15:00:00 [280.31297302]
2020-12-21 18:00:00 [280.2789917]
2020-12-21 21:00:00 [280.08499146]