.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ex_unstructured.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ex_unstructured.py: ***************** Unstructured grid ***************** Interpolation of unstructured grids. The interpolation of this object is based on a :py:class:`R*Tree ` structure. To begin with, we start by building this object. By default, this object considers the WGS-84 geodetic coordinate system. But you can define another one using the class :py:class:`Spheroid `. .. note:: By default, the class converts coordinates from the WGS-84 geodetic system to a Cartesian coordinate system. However, if you set the parameter ``ecef`` to ``True``, this transformation is disabled. In this case, both input and output coordinates are expected to be in the Cartesian coordinate system, and the RTree will handle only Cartesian coordinates without any conversion. .. GENERATED FROM PYTHON SOURCE LINES 24-31 .. code-block:: Python import matplotlib.pyplot import numpy import pyinterp mesh = pyinterp.RTree() .. GENERATED FROM PYTHON SOURCE LINES 32-39 Then, we will insert points into the tree. The class allows you to add points using two algorithms. The first one called :py:meth:`packing `, will enable you to enter the values in the tree at once. This mechanism is the recommended solution to create an optimized in-memory structure, both in terms of construction time and queries. When this is not possible, you can insert new information into the tree as you go along using the :py:meth:`insert ` method. .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python SIZE = 2000 X0, X1 = 80, 170 Y0, Y1 = -45, 30 generator = numpy.random.Generator(numpy.random.PCG64(0)) lons = generator.uniform(low=X0, high=X1, size=(SIZE, )) lats = generator.uniform(low=Y0, high=Y1, size=(SIZE, )) data = generator.uniform(low=-1.0, high=1.0, size=(SIZE, )) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Populates the search tree .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python mesh.packing(numpy.vstack((lons, lats)).T, data) .. GENERATED FROM PYTHON SOURCE LINES 52-101 When the tree is created, you can interpolate data with four algorithms: * :py:meth:`Inverse Distance Weighting ` or IDW * :py:meth:`Radial Basis Function ` or RBF * :py:meth:`Window Function ` * :py:meth:`Universal Kriging ` Inverse Distance Weighting (IDW), Radial Basis Function (RBF), and Kriging are all interpolation methods used to estimate a value for a target location based on the values of surrounding sample points. However, each method approaches this estimation differently. IDW uses a weighted average of the surrounding sample points, where the weight assigned to each point is inversely proportional to its distance from the target location. The further away a sample point is from the target location, the less influence it has on the estimated value. This method is relatively simple to implement and computationally efficient, but it can produce over-smoothed results in areas with a lot of sample points and under-smoothed results in areas with few sample points. RBF, on the other hand, models the spatial relationship between sample points and the target location by using a mathematical function (radial basis function) that is based on the distance between the points. The radial basis function is usually Gaussian, multiquadric, or inverse multiquadric. The estimated value at the target location is obtained by summing up the weighted contributions of all sample points. This method is more flexible than IDW as it can produce a wide range of interpolation results, but it can also be computationally expensive and susceptible to overfitting if not implemented carefully. Kriging, also known as Gaussian process regression, is a geostatistical method that models the spatial structure of the underlying data by using a covariance matrix. The estimated value at the target location is obtained by solving a set of linear equations that balance the fit to the sample points and the smoothness of the estimated surface. Kriging can produce more accurate results than IDW and RBF in many cases, but it requires a good understanding of the spatial structure of the data and can be computationally demanding. In summary, IDW is a simple and computationally efficient method, RBF is flexible but can be susceptible to overfitting, and Kriging is more accurate but requires a good understanding of the spatial structure of the data. The choice of method depends on the nature of the data, the spatial resolution required, and the computational resources available. We start by interpolating using the IDW method .. GENERATED FROM PYTHON SOURCE LINES 101-113 .. code-block:: Python STEP = 1 / 32 mx, my = numpy.meshgrid(numpy.arange(X0, X1 + STEP, STEP), numpy.arange(Y0, Y1 + STEP, STEP), indexing='ij') idw, neighbors = mesh.inverse_distance_weighting( numpy.vstack((mx.ravel(), my.ravel())).T, within=False, # Extrapolation is forbidden k=11, # We are looking for at most 11 neighbors num_threads=0) idw = idw.reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 114-115 Interpolation with RBF method .. GENERATED FROM PYTHON SOURCE LINES 115-124 .. code-block:: Python rbf, neighbors = mesh.radial_basis_function( numpy.vstack((mx.ravel(), my.ravel())).T, within=False, # Extrapolation is forbidden k=11, # We are looking for at most 11 neighbors rbf='linear', smooth=1e-4, num_threads=0) rbf = rbf.reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 125-126 Interpolation with a Window Function .. GENERATED FROM PYTHON SOURCE LINES 126-134 .. code-block:: Python wf, neighbors = mesh.window_function( numpy.vstack((mx.ravel(), my.ravel())).T, within=False, # Extrapolation is forbidden k=11, wf='parzen', num_threads=0) wf = wf.reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 135-136 Interpolation with a Universal Kriging .. GENERATED FROM PYTHON SOURCE LINES 136-145 .. code-block:: Python kriging, neighbors = mesh.universal_kriging( numpy.vstack((mx.ravel(), my.ravel())).T, within=False, # Extrapolation is forbidden k=11, covariance='matern_12', alpha=100_000, num_threads=0) kriging = kriging.reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 146-147 Let's visualize our interpolated data .. GENERATED FROM PYTHON SOURCE LINES 147-187 .. code-block:: Python vmin = -1 vmax = 1 fig = matplotlib.pyplot.figure(figsize=(10, 20)) ax1 = fig.add_subplot(411) pcm = ax1.pcolormesh(mx, my, idw, cmap='jet', shading='auto', vmin=vmin, vmax=vmax) ax1.set_title('IDW interpolation') ax2 = fig.add_subplot(412) pcm = ax2.pcolormesh(mx, my, rbf, cmap='jet', shading='auto', vmin=vmin, vmax=vmax) ax2.set_title('RBF interpolation') ax3 = fig.add_subplot(413) pcm = ax3.pcolormesh(mx, my, wf, cmap='jet', shading='auto', vmin=vmin, vmax=vmax) ax3.set_title('Window function interpolation') ax4 = fig.add_subplot(414) pcm = ax4.pcolormesh(mx, my, kriging, cmap='jet', shading='auto', vmin=vmin, vmax=vmax) ax4.set_title('Universal Kriging interpolation') fig.colorbar(pcm, ax=[ax1, ax2, ax3, ax4], shrink=0.8) .. image-sg:: /auto_examples/images/sphx_glr_ex_unstructured_001.png :alt: IDW interpolation, RBF interpolation, Window function interpolation, Universal Kriging interpolation :srcset: /auto_examples/images/sphx_glr_ex_unstructured_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 29.740 seconds) .. _sphx_glr_download_auto_examples_ex_unstructured.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/CNES/pangeo-pyinterp/master?urlpath=lab/tree/notebooks/auto_examples/ex_unstructured.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex_unstructured.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex_unstructured.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex_unstructured.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_