{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Creating an Ensemble of conditioned 2D Fields\n\nLet's create an ensemble of conditioned random fields in 2D.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport gstools as gs\n\n# conditioning data (x, y, value)\ncond_pos = [[0.3, 1.9, 1.1, 3.3, 4.7], [1.2, 0.6, 3.2, 4.4, 3.8]]\ncond_val = [0.47, 0.56, 0.74, 1.47, 1.74]\n\n# grid definition for output field\nx = np.arange(0, 5, 0.1)\ny = np.arange(0, 5, 0.1)\n\nmodel = gs.Gaussian(dim=2, var=0.5, len_scale=5, anis=0.5, angles=-0.5)\nkrige = gs.Krige(model, cond_pos=cond_pos, cond_val=cond_val)\ncond_srf = gs.CondSRF(krige)\ncond_srf.set_pos([x, y], \"structured\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To generate the ensemble we will use a seed-generator.\nBy specifying ``store=[f\"fld{i}\", False, False]``, only the conditioned field\nis stored with the specified name. The raw random field and the raw kriging\nfield is not stored. This way, we can access each conditioned field by index\n``cond_srf[i]``:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "seed = gs.random.MasterRNG(20170519)\nens_no = 4\nfor i in range(ens_no):\n    cond_srf(seed=seed(), store=[f\"fld{i}\", False, False])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's have a look at the pairwise differences between the generated\nfields. We will see, that they coincide at the given conditions.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots(ens_no + 1, ens_no + 1, figsize=(8, 8))\n# plotting kwargs for scatter and image\nvmax = np.max(cond_srf.all_fields)\nsc_kw = dict(c=cond_val, edgecolors=\"k\", vmin=0, vmax=vmax)\nim_kw = dict(extent=2 * [0, 5], origin=\"lower\", vmin=0, vmax=vmax)\nfor i in range(ens_no):\n    # conditioned fields and conditions\n    ax[i + 1, 0].imshow(cond_srf[i].T, **im_kw)\n    ax[i + 1, 0].scatter(*cond_pos, **sc_kw)\n    ax[i + 1, 0].set_ylabel(f\"Field {i}\", fontsize=10)\n    ax[0, i + 1].imshow(cond_srf[i].T, **im_kw)\n    ax[0, i + 1].scatter(*cond_pos, **sc_kw)\n    ax[0, i + 1].set_title(f\"Field {i}\", fontsize=10)\n    # absolute differences\n    for j in range(ens_no):\n        ax[i + 1, j + 1].imshow(np.abs(cond_srf[i] - cond_srf[j]).T, **im_kw)\n\n# beautify plots\nax[0, 0].axis(\"off\")\nfor a in ax.flatten():\n    a.set_xticklabels([]), a.set_yticklabels([])\n    a.set_xticks([]), a.set_yticks([])\nfig.subplots_adjust(wspace=0, hspace=0)\nfig.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To check if the generated fields are correct, we can have a look at their\nnames:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(cond_srf.field_names)"
      ]
    }
  ],
  "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
}