Interpolating numpy arrays
[1]:
from earthkit.regrid import interpolate
from earthkit.data import from_source
Get GRIB data on an octahedral reduced Gaussian global grid:
[2]:
ds = from_source("url",
"https://sites.ecmwf.int/repository/earthkit-regrid/examples/O32_multi.grib2")
Interpolate the values of the first field onto a 1x1 degree global regular latitude-longitude grid. The input and output grids are defined by a gridspec. Both the input and the ouput values are numpy arrays.
[3]:
res = interpolate(ds[0].values, {"grid": "O32"}, {"grid": [1,1]})
res.shape
/var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/ipykernel_42733/1412418366.py:1: DeprecatedWarning: interpolate is deprecated as of 0.5.0. Use regrid() instead
res = interpolate(ds[0].values, {"grid": "O32"}, {"grid": [1,1]})
[3]:
(181, 360)
Define some helper methods for plotting.
[4]:
def make_lat_lon(dx):
import numpy as np
lat_v = np.linspace(90, -90, int(180/dx)+1)
lon_v = np.linspace(0, 360-dx, int(360/dx))
lon, lat = np.meshgrid(lon_v, lat_v)
return lat, lon
def make_plot(r, dx):
import matplotlib.pyplot as plt
lat, lon = make_lat_lon(dx)
print(r.size)
# print(lat.size)
# print(lon.size)
v = r - 273.16
fig, ax = plt.subplots()
tcf = ax.tricontourf(lon.flatten(), lat.flatten(), v.flatten(), vmin=-20, vmax=36, levels=56, cmap="plasma")
fig.colorbar(tcf)
plt.show()
Plot the resulting data.
[5]:
make_plot(res, 1)
65160
[ ]: