Note
Go to the end to download the full example code.
Interpolation from points to grid¶
Interpolate surface pressure observations to a regular grid using Barnes interpolation.
import easyclimate as ecl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
Open sample surface pressure data for the European region
Warning
Although the original version only support specific geographical domain and projection (as in Zürcher, B. K., 2023), which is fixed to the European latitudes and Lambert conformal projection and cannot be freely chosen.
However, with moderate modifications, it now supports interpolation across global regions.
Except for method "optimized_convolution_S2" in the function easyclimate.interp.interp_spatial_barnesS2, which for objective reasons can only support interpolation for a single hemisphere (either northern or southern) and cannot exceed 180 degrees across the entire interpolation longitude range.
station_data = ecl.open_tutorial_dataset("PressQFF_202007271200_872.csv")
print(station_data)
PressQFF_202007271200_872.csv ━━━━━━━━━ 100.0% | 7.7/7.7 | 92.8 MB/s | 0:00:00
kB
lat lon qff
0 43.8569 4.4064 1016.2
1 56.3265 -3.7273 995.1
2 55.7350 24.4170 1015.5
3 60.3000 -4.2000 997.8
4 57.8500 45.7667 1020.5
.. ... ... ...
867 38.8766 22.4360 1011.8
868 68.8493 28.2991 1018.2
869 39.0633 26.5983 1010.9
870 49.4464 2.1272 1009.8
871 61.3667 30.8833 1021.2
[872 rows x 3 columns]
easyclimate.interp.interp_spatial_barnes enables interpolation from site data to grid point data.
See also
Zürcher, B. K.: Fast approximate Barnes interpolation: illustrated by Python-Numba implementation fast-barnes-py v1.0, Geosci. Model Dev., 16, 1697–1711, https://doi.org/10.5194/gmd-16-1697-2023, 2023.
meshdata_numba = ecl.interp.interp_spatial_barnes(
station_data,
"qff",
lon_dim="lon",
lat_dim="lat",
grid_res_deg=0.25,
sigma_deg=0.5,
influence_radius_deg=None, # Default = 4*sigma
mask_radius_deg=None, # Default = influence radius
method="optimized_convolution",
buffer_deg=5.0,
)
meshdata_numba
Or use functions easyclimate.interp.interp_spatial_barnes_rs accelerated by Rust
meshdata_rust = ecl.interp.interp_spatial_barnes_rs(
station_data,
"qff",
lon_dim="lon",
lat_dim="lat",
grid_res_deg=0.25,
sigma_deg=0.5,
influence_radius_deg=None, # Default = 4*sigma
mask_radius_deg=None, # Default = influence radius
method="optimized_convolution",
buffer_deg=5.0,
)
meshdata_rust
Plotting interpolated grid point data and corresponding station locations
fig, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree(central_longitude=0)})
ax.gridlines(draw_labels=["bottom", "left"], color="grey", alpha=0.5, linestyle="--")
ax.coastlines(edgecolor="black", linewidths=0.5)
ax.set_extent([-30, 50, 32, 75])
# Draw interpolation results
meshdata_numba.plot.contourf(
ax=ax,
transform=ccrs.PlateCarree(),
cbar_kwargs={"location": "bottom", "aspect": 50, "shrink": 0.9},
cmap="RdBu_r",
levels=21,
)
# Draw observation stations
ax.scatter(
station_data["lon"], station_data["lat"], s=1, c="r", transform=ccrs.PlateCarree()
)
ax.set_title("Method: Numba, optimized_convolution")

Text(0.5, 1.0, 'Method: Numba, optimized_convolution')
Next, we consider the results obtained using Rust.
fig, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree(central_longitude=0)})
ax.gridlines(draw_labels=["bottom", "left"], color="grey", alpha=0.5, linestyle="--")
ax.coastlines(edgecolor="black", linewidths=0.5)
ax.set_extent([-30, 50, 32, 75])
# Draw interpolation results
meshdata_rust.plot.contourf(
ax=ax,
transform=ccrs.PlateCarree(),
cbar_kwargs={"location": "bottom", "aspect": 50, "shrink": 0.9},
cmap="RdBu_r",
levels=21,
)
# Draw observation stations
ax.scatter(
station_data["lon"], station_data["lat"], s=1, c="r", transform=ccrs.PlateCarree()
)
ax.set_title("Method: Rust, optimized_convolution")

Text(0.5, 1.0, 'Method: Rust, optimized_convolution')
Now, when we compare the computational results of the two, we can see that only the machine precision differs.
fig, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree(central_longitude=0)})
ax.gridlines(draw_labels=["bottom", "left"], color="grey", alpha=0.5, linestyle="--")
ax.coastlines(edgecolor="black", linewidths=0.5)
ax.set_extent([-30, 50, 32, 75])
# Draw interpolation results
(meshdata_rust - meshdata_numba).plot.contourf(
ax=ax,
transform=ccrs.PlateCarree(),
cbar_kwargs={"location": "bottom", "aspect": 50, "shrink": 0.9},
cmap="RdBu_r",
)
# Draw observation stations
ax.scatter(
station_data["lon"], station_data["lat"], s=1, c="r", transform=ccrs.PlateCarree()
)
ax.set_title("Method: Rust, optimized_convolution")

Text(0.5, 1.0, 'Method: Rust, optimized_convolution')
Total running time of the script: (0 minutes 9.258 seconds)