{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Working with lat-lon random fields\n\nIn this example, we demonstrate how to generate a random field on\ngeographical coordinates.\n\nFirst we setup a model, with ``latlon=True``, to get the associated\nYadrenko model.\n\nIn addition, we will use the earth radius provided by :any:`EARTH_RADIUS`,\nto have a meaningful length scale in km.\n\nTo generate the field, we simply pass ``(lat, lon)`` as the position tuple\nto the :any:`SRF` class.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import gstools as gs\n\nmodel = gs.Gaussian(latlon=True, var=1, len_scale=777, rescale=gs.EARTH_RADIUS)\n\nlat = lon = range(-80, 81)\nsrf = gs.SRF(model, seed=1234)\nfield = srf.structured((lat, lon))\nsrf.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This was easy as always! Now we can use this field to estimate the empirical\nvariogram in order to prove, that the generated field has the correct\ngeo-statistical properties.\nThe :any:`vario_estimate` routine also provides a ``latlon`` switch to\nindicate, that the given field is defined on geographical variables.\n\nAs we will see, everthing went well... phew!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bin_edges = [0.01 * i for i in range(30)]\nbin_center, emp_vario = gs.vario_estimate(\n    (lat, lon),\n    field,\n    bin_edges,\n    latlon=True,\n    mesh_type=\"structured\",\n    sampling_size=2000,\n    sampling_seed=12345,\n)\n\nax = model.plot(\"vario_yadrenko\", x_max=0.3)\nmodel.fit_variogram(bin_center, emp_vario, nugget=False)\nmodel.plot(\"vario_yadrenko\", ax=ax, label=\"fitted\", x_max=0.3)\nax.scatter(bin_center, emp_vario, color=\"k\")\nprint(model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>Note, that the estimated variogram coincides with the yadrenko variogram,\n   which means it depends on the great-circle distance given in radians.\n\n   Keep that in mind when defining bins: The range is at most\n   $\\pi\\approx 3.14$, which corresponds to the half globe.</p></div>\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
}