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
[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)
    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)
../_images/examples_numpy_arrays_9_0.png
[ ]: