{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Creating a 2D Synthetic Precipitation Field\n\nIn this example we'll create a time series of a 2D synthetic precipitation\nfield.\n\nVery similar to the previous tutorial, we'll start off by creating a Gaussian\nrandom field with an exponential variogram, which seems to reproduce the\nspatial correlations of precipitation fields quite well. We'll create a daily\ntimeseries over a two dimensional domain of 50km x 40km. This workflow is\nsuited for sub daily precipitation time series.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.animation as animation\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport gstools as gs\n\n# fix the seed for reproducibility\nseed = 20170521\n# 1st spatial axis of 50km with a resolution of 1km\nx = np.arange(0, 50, 1.0)\n# 2nd spatial axis of 40km with a resolution of 1km\ny = np.arange(0, 40, 1.0)\n# half daily timesteps over three months\nt = np.arange(0.0, 90.0, 0.5)\n\n# total spatio-temporal dimension\nst_dim = 2 + 1\n# space-time anisotropy ratio given in units d / km\nst_anis = 0.4\n\n# an exponential variogram with a corr. lengths of 5km, 5km, and 2d\nmodel = gs.Exponential(dim=st_dim, var=1.0, len_scale=5.0, anis=st_anis)\n# create a spatial random field instance\nsrf = gs.SRF(model, seed=seed)\n\npos, time = [x, y], [t]\n\n# the Gaussian random field\nsrf.structured(pos + time)\n\n# account for the skewness and the dry periods\ncutoff = 0.55\ngs.transform.boxcox(srf, lmbda=0.5, shift=-1.0 / cutoff)\n\n# adjust the amount of precipitation\namount = 4.0\nsrf.field *= amount"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "plot the 2d precipitation field over time as an animation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def _update_ani(time_step):\n    im.set_array(srf.field[:, :, time_step].T)\n    return (im,)\n\n\nfig, ax = plt.subplots()\nim = ax.imshow(\n    srf.field[:, :, 0].T,\n    cmap=\"Blues\",\n    interpolation=\"bicubic\",\n    origin=\"lower\",\n)\ncbar = fig.colorbar(im)\ncbar.ax.set_ylabel(r\"Precipitation $P$ / mm\")\nax.set_xlabel(r\"$x$ / km\")\nax.set_ylabel(r\"$y$ / km\")\n\nani = animation.FuncAnimation(\n    fig, _update_ani, len(t), interval=100, blit=True\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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}