
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/regrid/plot_kriging.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_tutorials_regrid_plot_kriging.py>`
        to download the full example code or to run this example in your browser via Binder.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorials_regrid_plot_kriging.py:


Interpolate scattered data with kriging
=======================================

In this tutorial, we show how to:

* compute and fit an empirical semi-variogram of scattered observations,
* interpolate the observations on a regular grid with ordinary kriging,
* get the estimation error and check the result against the truth.

The data are synthetic, so that the interpolation can be evaluated: a spatially correlated
field is sampled at random positions and corrupted with a small noise.
See also: :ref:`sphx_glr_tutorials_filtering_plot_filtering.py` and
:ref:`sphx_glr_tutorials_regrid_plot_kriging_sst.py`.

.. GENERATED FROM PYTHON SOURCE LINES 19-21

Initialisations
---------------

.. GENERATED FROM PYTHON SOURCE LINES 21-37

.. code-block:: Python


    import time

    import cartopy.crs as ccrs
    import cmocean
    import matplotlib.pyplot as plt
    import numpy as np
    import xarray as xr

    from xoa.filter import generate_kernel, smooth
    from xoa.krig import Kriger, LocalKriger, VariogramModel, empirical_variogram
    from xoa.plot import add_colorbar, plot_field, plot_minimap

    xr.set_options(display_style="text")
    plt.rcParams.update({"font.size": 9, "axes.titlesize": 10, "axes.titleweight": "bold"})








.. GENERATED FROM PYTHON SOURCE LINES 38-41

The truth is white noise that is smoothed with :func:`~xoa.filter.smooth` and a kernel from
:func:`~xoa.filter.generate_kernel` to get a correlation length of about 40 km,
on a regular grid. It is sampled at 150 random positions.

.. GENERATED FROM PYTHON SOURCE LINES 41-63

.. code-block:: Python


    rng = np.random.default_rng(3)
    nx, ny, nobs = 50, 40, 150
    lon = xr.DataArray(np.linspace(-6, -4, nx), dims="lon", attrs={"units": "degrees_east"})
    lat = xr.DataArray(np.linspace(46, 48, ny), dims="lat", attrs={"units": "degrees_north"})
    noise = xr.DataArray(
        rng.normal(size=(ny, nx)), dims=("lat", "lon"), coords={"lon": lon, "lat": lat}
    )
    kernel = generate_kernel(15, noise, window_func="hanning", isotropic=True)
    truth = smooth(noise, kernel, na_thres=1)
    truth = 15 + 2 * truth / truth.std()
    truth.attrs.update(long_name="Temperature", units="degC")

    iy, ix = np.unravel_index(rng.choice(ny * nx, nobs, replace=False), (ny, nx))
    obs = xr.DataArray(
        truth.values[iy, ix] + 0.1 * rng.normal(size=nobs),
        dims="npts",
        coords={"lon": ("npts", lon.values[ix]), "lat": ("npts", lat.values[iy])},
        name="temp",
        attrs=truth.attrs,
    )








.. GENERATED FROM PYTHON SOURCE LINES 64-72

Semi-variogram
--------------

The semi-variogram is half the mean squared difference of the pairs of points,
as a function of their distance. It tells how fast the points get uncorrelated.
:func:`~xoa.krig.empirical_variogram` computes it. We restrict it to the distances where
there are enough pairs, and :class:`~xoa.krig.VariogramModel` fits a model
with a nugget (noise), a sill (variance) and a range (correlation length).

.. GENERATED FROM PYTHON SOURCE LINES 72-85

.. code-block:: Python


    ev = empirical_variogram(obs, nbin=15, dist_units="km", distmax=120)
    vario = VariogramModel("spherical", dist_units="km")
    vario.fit(ev)
    print(vario)

    fig, ax = plt.subplots(figsize=(6, 3.5))
    ev.plot(ax=ax, marker="o", ls="", label="Empirical")
    vario.plot(ax=ax, rmax=120)
    ax.legend(loc="upper left")
    ax.set_title("Semi-variogram")
    ax.set_ylabel("Semi-variance [degC2]")




.. image-sg:: /tutorials/regrid/images/sphx_glr_plot_kriging_001.png
   :alt: Semi-variogram
   :srcset: /tutorials/regrid/images/sphx_glr_plot_kriging_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    <VariogramModel('spherical', dist_units='km', n=4.713800904559998e-06, s=4.1318725271224555, r=37.94751038354297)>

    Text(51.722222222222214, 0.5, 'Semi-variance [degC2]')



.. GENERATED FROM PYTHON SOURCE LINES 86-92

Ordinary kriging
----------------

The fitted model is given to the :class:`~xoa.krig.Kriger`, which returns the
estimation and its error at the positions of a target dataset.
The result is on the grid of the target.

.. GENERATED FROM PYTHON SOURCE LINES 92-119

.. code-block:: Python


    kriger = Kriger(obs, "ordinary", vario.get_func(), dist_units="km")
    out = kriger.interp(xr.Dataset(coords={"lon": lon, "lat": lat}))
    est = out.temp
    err = out.temp_error

    vmin, vmax = float(truth.min()), float(truth.max())
    kw = dict(cmap=cmocean.cm.thermal, vmin=vmin, vmax=vmax, add_colorbar=False)
    map_kw = {"gridlines_labels_on": ["left", "bottom"]}
    fig, axes = plt.subplots(
        1, 2, figsize=(9, 3.8), subplot_kw={"projection": ccrs.PlateCarree()}, constrained_layout=True
    )
    mappable = plot_field(truth, ax=axes[0], title="Truth and observations", map_kw=map_kw, **kw)
    axes[0].scatter(
        obs.lon,
        obs.lat,
        c=obs,
        s=18,
        edgecolors="w",
        linewidths=0.5,
        **{k: v for k, v in kw.items() if k != "add_colorbar"},
        transform=ccrs.PlateCarree(),
    )
    plot_field(est, ax=axes[1], title="Kriging", map_kw=map_kw, **kw)
    add_colorbar(mappable, axes, da=truth)
    plot_minimap(truth, parent=axes[0], loc="lower right", size=0.25)




.. image-sg:: /tutorials/regrid/images/sphx_glr_plot_kriging_002.png
   :alt: Truth and observations, Kriging
   :srcset: /tutorials/regrid/images/sphx_glr_plot_kriging_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <GeoAxes: >



.. GENERATED FROM PYTHON SOURCE LINES 120-121

The kriged field retrieves the structures of the truth from the sparse observations.

.. GENERATED FROM PYTHON SOURCE LINES 123-129

Error
-----

The kriging error only depends on the positions of the observations and on the variogram.
It is small near the observations and it grows with the distance to them.
The actual error is also shown, with its root mean square value.

.. GENERATED FROM PYTHON SOURCE LINES 129-153

.. code-block:: Python


    diff = est - truth
    rmse = float(np.sqrt((diff**2).mean()))
    print(f"RMSE: {rmse:.2f} degC")
    fig, axes = plt.subplots(
        1, 2, figsize=(9, 3.8), subplot_kw={"projection": ccrs.PlateCarree()}, constrained_layout=True
    )
    m0 = plot_field(
        err, ax=axes[0], title="Kriging error", cmap=cmocean.cm.amp, map_kw=map_kw, add_colorbar=False
    )
    add_colorbar(m0, axes[0], label="Error [degC]")
    axes[0].scatter(obs.lon, obs.lat, c="k", s=4, transform=ccrs.PlateCarree())
    m1 = plot_field(
        diff,
        ax=axes[1],
        title=f"Estimation - truth (RMSE {rmse:.2f})",
        cmap=cmocean.cm.balance,
        vmin=-2,
        vmax=2,
        map_kw=map_kw,
        add_colorbar=False,
    )
    add_colorbar(m1, axes[1], label="Difference [degC]")




.. image-sg:: /tutorials/regrid/images/sphx_glr_plot_kriging_003.png
   :alt: Kriging error, Estimation - truth (RMSE 0.74)
   :srcset: /tutorials/regrid/images/sphx_glr_plot_kriging_003.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    RMSE: 0.74 degC

    <matplotlib.colorbar.Colorbar object at 0x738c04650830>



.. GENERATED FROM PYTHON SOURCE LINES 154-161

Local kriging
-------------

The :class:`~xoa.krig.Kriger` uses all the observations at once, which gets slow and heavy
with many observations. The :class:`~xoa.krig.LocalKriger` only uses the ``nneigh`` nearest
observations of each target point, found with a KD-tree, and solves a small system for each
of them with numba. Beyond the range of the variogram, far observations hardly matter.

.. GENERATED FROM PYTHON SOURCE LINES 161-170

.. code-block:: Python


    local = LocalKriger(obs, "ordinary", vario, nneigh=40, dist_units="km")
    target = xr.Dataset(coords={"lon": lon, "lat": lat})
    start = time.perf_counter()
    out_local = local.interp(target)
    print(f"Local kriging: {time.perf_counter() - start:.3f} s")
    est_local = out_local.temp
    print(f"Max difference with the global kriging: {float(abs(est_local - est).max()):.3f} degC")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Local kriging: 5.618 s
    Max difference with the global kriging: 0.266 degC




.. GENERATED FROM PYTHON SOURCE LINES 171-175

The local estimation is close to the global one, the largest differences being far from
the observations. The number of neighbors and the optional ``maxdist`` set the compromise
between speed and accuracy. Where there are fewer than ``nmin`` neighbors, for instance
far from the observations with a small ``maxdist``, the result is masked.


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_tutorials_regrid_plot_kriging.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/shom-fr/xoa/master?urlpath=lab/tree/notebooks/tutorials/regrid/plot_kriging.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_kriging.ipynb <plot_kriging.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_kriging.py <plot_kriging.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_kriging.zip <plot_kriging.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
