.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/03_variogram/06_auto_bin_latlon.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_03_variogram_06_auto_bin_latlon.py: Automatic binning with lat-lon data ----------------------------------- In this example we demonstrate automatic binning for a tiny data set containing temperature records from Germany (See the detailed DWD example for more information on the data). We use a data set from 20 meteo-stations choosen randomly. .. GENERATED FROM PYTHON SOURCE LINES 11-44 .. code-block:: Python import numpy as np import gstools as gs # lat, lon, temperature data = np.array( [ [52.9336, 8.237, 15.7], [48.6159, 13.0506, 13.9], [52.4853, 7.9126, 15.1], [50.7446, 9.345, 17.0], [52.9437, 12.8518, 21.9], [53.8633, 8.1275, 11.9], [47.8342, 10.8667, 11.4], [51.0881, 12.9326, 17.2], [48.406, 11.3117, 12.9], [49.7273, 8.1164, 17.2], [49.4691, 11.8546, 13.4], [48.0197, 12.2925, 13.9], [50.4237, 7.4202, 18.1], [53.0316, 13.9908, 21.3], [53.8412, 13.6846, 21.3], [54.6792, 13.4343, 17.4], [49.9694, 9.9114, 18.6], [51.3745, 11.292, 20.2], [47.8774, 11.3643, 12.7], [50.5908, 12.7139, 15.8], ] ) pos = data.T[:2] # lat, lon field = data.T[2] # temperature .. GENERATED FROM PYTHON SOURCE LINES 45-47 Since the overall range of these meteo-stations is too low, we can use the data-variance as additional information during the fit of the variogram. .. GENERATED FROM PYTHON SOURCE LINES 47-56 .. code-block:: Python emp_v = gs.vario_estimate(pos, field, latlon=True, geo_scale=gs.KM_SCALE) sph = gs.Spherical(latlon=True, geo_scale=gs.KM_SCALE) sph.fit_variogram(*emp_v, sill=np.var(field)) ax = sph.plot("vario_yadrenko", x_max=2 * np.max(emp_v[0])) ax.scatter(*emp_v, label="Empirical variogram") ax.legend() print(sph) .. image-sg:: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_001.png :alt: 06 auto bin latlon :srcset: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Spherical(latlon=True, var=9.91, len_scale=4.7e+02, geo_scale=6.37e+03) .. GENERATED FROM PYTHON SOURCE LINES 57-62 As we can see, the variogram fitting was successful and providing the data variance helped finding the right length-scale. Now, we'll use this covariance model to interpolate the given data with ordinary kriging. .. GENERATED FROM PYTHON SOURCE LINES 62-74 .. code-block:: Python # enclosing box for data points grid_lat = np.linspace(np.min(pos[0]), np.max(pos[0])) grid_lon = np.linspace(np.min(pos[1]), np.max(pos[1])) # ordinary kriging krige = gs.krige.Ordinary(sph, pos, field) krige((grid_lat, grid_lon), mesh_type="structured") ax = krige.plot() # plotting lat on y-axis and lon on x-axis ax.scatter(pos[1], pos[0], 50, c=field, edgecolors="k", label="input") ax.legend() .. image-sg:: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_002.png :alt: Field 2D structured: (50, 50) :srcset: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-79 Looks good, doesn't it? This workflow is also implemented in the :any:`Krige` class, by setting ``fit_variogram=True``. Then the whole procedure shortens: .. GENERATED FROM PYTHON SOURCE LINES 79-88 .. code-block:: Python krige = gs.krige.Ordinary(sph, pos, field, fit_variogram=True) krige.structured((grid_lat, grid_lon)) # plot the result krige.plot() # show the fitting results print(krige.model) .. image-sg:: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_003.png :alt: Field 2D structured: (50, 50) :srcset: /examples/03_variogram/images/sphx_glr_06_auto_bin_latlon_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Spherical(latlon=True, var=9.91, len_scale=4.7e+02, geo_scale=6.37e+03) .. GENERATED FROM PYTHON SOURCE LINES 89-91 This example shows, that setting up variogram estimation and kriging routines is straight forward with GSTools! .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.210 seconds) .. _sphx_glr_download_examples_03_variogram_06_auto_bin_latlon.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_auto_bin_latlon.ipynb <06_auto_bin_latlon.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_auto_bin_latlon.py <06_auto_bin_latlon.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_auto_bin_latlon.zip <06_auto_bin_latlon.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_