<jupyter_start><jupyter_text>Interpolation of LLC4320 ocean model===============================The interpolation of this object is based on a[R*Tree](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.htmlpyinterp.RTree)structure. To begin with, we start by building this object. By default, thisobject considers the WGS-84 geodetic coordinate system. But you can defineanother one using the class[System](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.geodetic.System.htmlpyinterp.geodetic.System).<jupyter_code>import numpy
import cartopy.crs
import cartopy.mpl.ticker
import intake
import matplotlib.pyplot
import pyinterp
mesh = pyinterp.RTree()<jupyter_output><empty_output><jupyter_text>Then, we will insert points into the tree. The class allows you to add pointsusing two algorithms. The first one, called[packing](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.packing.htmlpyinterp.RTree.packing),will enable you to enter the values in the tree at once. This mechanism is therecommended solution to create an optimized in-memory structure, both in termsof construction time and queries. When this is not possible, you can insert newinformation into the tree as you go along using the[insert](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.packing.htmlpyinterp.RTree.insert)method.<jupyter_code>cat_url = "https://raw.githubusercontent.com/pangeo-data/pangeo-datastore" \
    "/master/intake-catalogs/ocean/llc4320.yaml"
cat = intake.open_catalog(cat_url)<jupyter_output><empty_output><jupyter_text>Grid subsampling (orginal volume is too huge for this example)<jupyter_code>indices = slice(0, None, 8)<jupyter_output><empty_output><jupyter_text>Reads longitudes and latitudes of the grid<jupyter_code>array = cat.LLC4320_grid.to_dask()
lons = array["XC"].isel(i=indices, j=indices)
lats = array["YC"].isel(i=indices, j=indices)<jupyter_output><empty_output><jupyter_text>Reads SSH values for the first time step of the time series<jupyter_code>ssh = cat.LLC4320_SSH.to_dask()
ssh = ssh["Eta"].isel(time=0, i=indices, j=indices)<jupyter_output><empty_output><jupyter_text>Populates the search tree<jupyter_code>mesh.packing(
    numpy.vstack((lons.values.ravel(), lats.values.ravel())).T,
    ssh.values.ravel())<jupyter_output><empty_output><jupyter_text>When the tree is created, you can interpolate data with two algorithms:-   [Inverse Distance Weighting](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.inverse_distance_weighting.htmlpyinterp.RTree.inverse_distance_weighting)    or IDW-   [Radial Basis Function](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.radial_basis_function.htmlpyinterp.RTree.radial_basis_function)    or RBFYon can also search the[nearest neighbors](https://pangeo-pyinterp.readthedocs.io/en/latest/generated/pyinterp.RTree.query.htmlpyinterp.RTree.query)on the tree.---**Note**When comparing an RBF to IDW, IDW will never predict values higher thanthe maximum measured value or lower than the minimum measured value.However, RBFs can predict values higher than the maximum values andlower than the minimum measured values.---In this example, we will under-sample the source grid at 1/32 degreeover an area of the globe.<jupyter_code>x0, x1 = 80, 170
y0, y1 = -45, 30
res = 1 / 32.0
mx, my = numpy.meshgrid(numpy.arange(x0, x1, res),
                        numpy.arange(y0, y1, res),
                        indexing="ij")<jupyter_output><empty_output><jupyter_text>IDW interpolation<jupyter_code>idw_eta, neighbors = mesh.inverse_distance_weighting(
    numpy.vstack((mx.ravel(), my.ravel())).T,
    within=True,  # Extrapolation is forbidden
    radius=55000,  # In a radius of 5.5 Km
    k=8,  # We are looking for at most 8 neighbours
    num_threads=0)
idw_eta = idw_eta.reshape(mx.shape)<jupyter_output><empty_output><jupyter_text>RBF interpolation<jupyter_code>rbf_eta, neighbors = mesh.radial_basis_function(
    numpy.vstack((mx.ravel(), my.ravel())).T,
    within=True,  # Extrapolation is forbidden
    k=11,  # We are looking for at most 11 neighbours
    num_threads=0)
rbf_eta = rbf_eta.reshape(mx.shape)<jupyter_output><empty_output><jupyter_text>Let's visualize our interpolated data<jupyter_code>fig = matplotlib.pyplot.figure(figsize=(18, 9))
lon_formatter = cartopy.mpl.ticker.LongitudeFormatter(
    zero_direction_label=True)
lat_formatter = cartopy.mpl.ticker.LatitudeFormatter()
ax = fig.add_subplot(121, projection=cartopy.crs.PlateCarree())
ax.pcolormesh(mx,
              my,
              idw_eta,
              cmap='terrain',
              shading='auto',
              transform=cartopy.crs.PlateCarree())
ax.coastlines()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
ax.set_xticks(numpy.arange(x0, x1, 10.0))
ax.set_yticks(numpy.arange(y0, y1, 10))
ax.set_title("Eta (IDW)")

ax = fig.add_subplot(122, projection=cartopy.crs.PlateCarree())
ax.pcolormesh(mx,
              my,
              rbf_eta,
              cmap='terrain',
              shading='auto',
              transform=cartopy.crs.PlateCarree())
ax.coastlines()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
ax.set_xticks(numpy.arange(x0, x1, 10.0))
ax.set_yticks(numpy.arange(y0, y1, 10))
ax.set_title("Eta (RBF)")
fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11864406779661017, 'fraction_numerical': 0.016028002947678702, 'mean_word_length': 5.183371298405467, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 8, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '469388', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1874, 'n_words': 371, 'path': 'notebooks/auto_examples/pangeo_unstructured_grid.ipynb', 'repo_name': 'readthedocs-assistant/pangeo-pyinterp', 'size': '5428'}