{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Automatic binning with lat-lon data\n\nIn this example we demonstrate automatic binning for a tiny data set\ncontaining temperature records from Germany\n(See the detailed DWD example for more information on the data).\n\nWe use a data set from 20 meteo-stations choosen randomly.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport gstools as gs\n\n# lat, lon, temperature\ndata = np.array(\n    [\n        [52.9336, 8.237, 15.7],\n        [48.6159, 13.0506, 13.9],\n        [52.4853, 7.9126, 15.1],\n        [50.7446, 9.345, 17.0],\n        [52.9437, 12.8518, 21.9],\n        [53.8633, 8.1275, 11.9],\n        [47.8342, 10.8667, 11.4],\n        [51.0881, 12.9326, 17.2],\n        [48.406, 11.3117, 12.9],\n        [49.7273, 8.1164, 17.2],\n        [49.4691, 11.8546, 13.4],\n        [48.0197, 12.2925, 13.9],\n        [50.4237, 7.4202, 18.1],\n        [53.0316, 13.9908, 21.3],\n        [53.8412, 13.6846, 21.3],\n        [54.6792, 13.4343, 17.4],\n        [49.9694, 9.9114, 18.6],\n        [51.3745, 11.292, 20.2],\n        [47.8774, 11.3643, 12.7],\n        [50.5908, 12.7139, 15.8],\n    ]\n)\npos = data.T[:2]  # lat, lon\nfield = data.T[2]  # temperature"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Since the overall range of these meteo-stations is too low, we can use the\ndata-variance as additional information during the fit of the variogram.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "emp_v = gs.vario_estimate(pos, field, latlon=True)\nsph = gs.Spherical(latlon=True, rescale=gs.EARTH_RADIUS)\nsph.fit_variogram(*emp_v, sill=np.var(field))\nax = sph.plot(x_max=2 * np.max(emp_v[0]))\nax.scatter(*emp_v, label=\"Empirical variogram\")\nax.legend()\nprint(sph)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As we can see, the variogram fitting was successful and providing the data\nvariance helped finding the right length-scale.\n\nNow, we'll use this covariance model to interpolate the given data with\nordinary kriging.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# enclosing box for data points\ngrid_lat = np.linspace(np.min(pos[0]), np.max(pos[0]))\ngrid_lon = np.linspace(np.min(pos[1]), np.max(pos[1]))\n# ordinary kriging\nkrige = gs.krige.Ordinary(sph, pos, field)\nkrige((grid_lat, grid_lon), mesh_type=\"structured\")\nax = krige.plot()\n# plotting lat on y-axis and lon on x-axis\nax.scatter(pos[1], pos[0], 50, c=field, edgecolors=\"k\", label=\"input\")\nax.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Looks good, doesn't it?\n\nThis workflow is also implemented in the :any:`Krige` class, by setting\n``fit_variogram=True``. Then the whole procedure shortens:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "krige = gs.krige.Ordinary(sph, pos, field, fit_variogram=True)\nkrige.structured((grid_lat, grid_lon))\n\n# plot the result\nkrige.plot()\n# show the fitting results\nprint(krige.model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This example shows, that setting up variogram estimation and kriging routines\nis straight forward with GSTools!\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}