<jupyter_start><jupyter_text>3D interpolationThis example shows how to interpolate values from  arbitrary points in a 3D space of a function defined on a Cartesian grid.Methods used performs an interpolation in 2D space by considering the axes of longitude and latitude of the grid, then performs a linear interpolation in the third dimension using the two values obtained by the 2D interpolation.Let's start by building our interpolator:<jupyter_code>import xarray as xr
import pyinterp.backends.xarray as pxr

ds = xr.load_dataset("../tests/dataset/tcw.nc")
# The grid used organizes the latitudes in descending order. We ask our
# constructor to flip this axis in order to correctly evaluate the bicubic
# interpolation from this 3D cube (only necessary to perform a bicubic
# interpolation).
interpolator = pxr.Grid3D(ds.data_vars["tcw"], increasing_axes=True)
interpolator<jupyter_output><empty_output><jupyter_text>We will build a new grid that will be used to build a new interpolated grid.<jupyter_code>import datetime
import numpy as np

# The coordinates used for interpolation are shifted to avoid using the
# points of the trivariate function.
mx, my, mz = np.meshgrid(
    np.arange(-180, 180, 0.25) + 1 / 3.0,
    np.arange(-80, 80, 0.25) + 1 / 3.0,
    np.array([datetime.datetime(2002, 7, 2, 15, 0)], dtype="datetime64"),
    indexing='ij')<jupyter_output><empty_output><jupyter_text>We interpolate our grid using a classical [trivariate](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.trivariate.htmlpyinterp.trivariate) interpolation, then a [bicubic](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.bicubic.html) interpolation in space followed by a linear interpolation in the temporal axis.<jupyter_code>trivariate = interpolator.trivariate(
    dict(longitude=mx.flatten(), latitude=my.flatten(), time=mz.flatten()))
bicubic = interpolator.bicubic(
    dict(longitude=mx.flatten(), latitude=my.flatten(), time=mz.flatten()))<jupyter_output><empty_output><jupyter_text>We transform our result cubes into a matrix.<jupyter_code>trivariate = trivariate.reshape(mx.shape).squeeze(axis=2)
bicubic = bicubic.reshape(mx.shape).squeeze(axis=2)
lons = mx[:, 0].squeeze()
lats = my[0, :].squeeze()<jupyter_output><empty_output><jupyter_text>Let's visualize our results.<jupyter_code>import matplotlib.pyplot as plt
import cartopy.crs as ccrs
%matplotlib inline

fig = plt.figure(figsize=(18, 9))
ax = fig.add_subplot(121, projection=ccrs.PlateCarree(central_longitude=180))
ax.pcolormesh(lons, lats, trivariate.T, cmap='jet',
              transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_extent([80, 170, -45, 30], crs=ccrs.PlateCarree())
ax.set_title("Trilinear")

ax = fig.add_subplot(122, projection=ccrs.PlateCarree(central_longitude=180))
ax.pcolormesh(lons, lats, bicubic.T, cmap='jet',
              transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_extent([80, 170, -45, 30], crs=ccrs.PlateCarree())
ax.set_title("Bicubic & Linear in time")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11516946363935505, 'fraction_numerical': 0.025008226390259954, 'mean_word_length': 5.893424036281179, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '144267', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1029, 'n_words': 266, 'path': 'notebooks/trivariate.ipynb', 'repo_name': 'Geospatial-Data-Science/pangeo-pyinterp', 'size': '3039'}