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

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
<xarray.DataArray 'qff' (lat: 186, lon: 340)> Size: 253kB
array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]],
      shape=(186, 340), dtype=float32)
Coordinates:
  * lat      (lat) float64 1kB 30.0 30.25 30.5 30.75 ... 75.5 75.75 76.0 76.25
  * lon      (lon) float64 3kB -30.7 -30.45 -30.2 -29.95 ... 53.55 53.8 54.05
Attributes: (12/14)
    grid_res_deg:          0.25
    sigma_deg:             0.5
    sigma_grid:            2.0
    influence_radius_deg:  2.0
    max_dist_sigma:        4.0
    method:                optimized_convolution
    ...                    ...
    buffer_deg:            5.0
    domain_lon0:           -30.6969
    domain_lat0:           30.0
    domain_grid_x_deg:     84.6302
    domain_grid_y_deg:     46.3
    mask_radius_deg:       2.0


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
<xarray.DataArray 'qff' (lat: 186, lon: 340)> Size: 253kB
array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]],
      shape=(186, 340), dtype=float32)
Coordinates:
  * lat      (lat) float64 1kB 30.0 30.25 30.5 30.75 ... 75.5 75.75 76.0 76.25
  * lon      (lon) float64 3kB -30.7 -30.45 -30.2 -29.95 ... 53.55 53.8 54.05
Attributes: (12/14)
    grid_res_deg:          0.25
    sigma_deg:             0.5
    sigma_grid:            2.0
    influence_radius_deg:  2.0
    max_dist_sigma:        4.0
    method:                optimized_convolution
    ...                    ...
    buffer_deg:            5.0
    domain_lon0:           -30.6969
    domain_lat0:           30.0
    domain_grid_x_deg:     84.6302
    domain_grid_y_deg:     46.3
    mask_radius_deg:       2.0


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")
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")
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")
Method: Rust, optimized_convolution
Text(0.5, 1.0, 'Method: Rust, optimized_convolution')

Total running time of the script: (0 minutes 9.258 seconds)