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]:
import earthkit.data as ekd
from earthkit.geo import nearest_point_haversine
[2]:
ekd.download_example_file("time_series.grib")
ds = ekd.from_source("file", "time_series.grib").sel(param="t")

Our data contains 4 steps.

[3]:
ds.metadata("step")
[3]:
[0, 3, 6, 9]

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]:
latlon = ds.to_latlon()
lat = latlon["lat"]
lon = latlon["lon"]

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.metadata("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-21T12:00:00 [280.44058228]
2020-12-21T15:00:00 [280.31297302]
2020-12-21T18:00:00 [280.2789917]
2020-12-21T21:00:00 [280.08499146]