
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/thresholding_simple_example.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_thresholding_simple_example.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_thresholding_simple_example.py:


===========================================================
Simple example: Threshold realizations to get probabilities
===========================================================
This is an example of thresholding a input containing realizations or ensemble members.
The thresholding will be applied to each ensemble member/realization independently.
The realization dimension can then be collapsed.

.. GENERATED FROM PYTHON SOURCE LINES 13-17

.. code-block:: Python


    # Authors: The IMPROVER developers
    # SPDX-License-Identifier: BSD-3-Clause








.. GENERATED FROM PYTHON SOURCE LINES 18-21

Generate data
-------------
Generate a synthetic dataset for thresholding.

.. GENERATED FROM PYTHON SOURCE LINES 21-36

.. code-block:: Python

    import numpy as np

    from improver.synthetic_data.set_up_test_cubes import set_up_variable_cube

    # Create a 3x3x3 3D numpy array with random values
    data = np.array(
        [
            [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
            [[2, 3, 4], [5, 6, 7], [8, 9, 10]],
            [[3, 4, 5], [6, 7, 8], [9, 10, 11]],
        ]
    )

    realization_cube = set_up_variable_cube(data=data, units="m/s")








.. GENERATED FROM PYTHON SOURCE LINES 37-38

Threshold the example cube at 5 m/s.

.. GENERATED FROM PYTHON SOURCE LINES 38-44

.. code-block:: Python

    from improver.threshold import Threshold

    thresholded_cube = Threshold(threshold_values=5, comparison_operator=">")(
        realization_cube
    )








.. GENERATED FROM PYTHON SOURCE LINES 45-47

Plot of the probability of exceeding 5 m/s for each realization.
Note that these are binary fields (0s and 1s).

.. GENERATED FROM PYTHON SOURCE LINES 47-59

.. code-block:: Python

    import iris.quickplot as qplt
    import matplotlib.pyplot as plt

    plt.figure(figsize=(10, 5))
    for i in range(thresholded_cube.coord("realization").points.size):
        plt.subplot(1, 3, i + 1)
        qplt.pcolormesh(thresholded_cube[i])
        plt.title(f"Realization {i}")
    plt.suptitle("Thresholded Cube (> 5 m/s)")
    plt.tight_layout()
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_thresholding_simple_example_001.png
   :alt: Thresholded Cube (> 5 m/s), Realization 0, Realization 1, Realization 2
   :srcset: /auto_examples/images/sphx_glr_thresholding_simple_example_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 60-61

Collapse the realization dimension as part of thresholding.

.. GENERATED FROM PYTHON SOURCE LINES 61-67

.. code-block:: Python

    from improver.threshold import Threshold

    collapsed_cube = Threshold(
        threshold_values=5, comparison_operator=">", collapse_coord="realization"
    )(realization_cube)








.. GENERATED FROM PYTHON SOURCE LINES 68-73

Plot the probabilities of exceeding 5 m/s after collapsing the realization dimension.
Note that these probabilities are now non-binary. The centre-left grid square
has a probability of 1/3 as only one out of the three realizations exceeded 5 m/s.
The centre grid square has a probability of 2/3 as two out of the three realizations
exceeded 5 m/s.

.. GENERATED FROM PYTHON SOURCE LINES 73-89

.. code-block:: Python

    import iris.quickplot as qplt
    import matplotlib as mpl
    import matplotlib.pyplot as plt

    plt.figure(figsize=(10, 5))
    qplt.pcolormesh(collapsed_cube, colorbar=False)
    cmap = mpl.cm.viridis
    norm = mpl.colors.BoundaryNorm(np.arange(0, 1.1, 0.2), cmap.N)
    plt.colorbar(
        mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
        ax=plt.gca(),
        orientation="horizontal",
        shrink=0.4,
    )
    plt.title("Probability of exceeding 5 m/s")
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_thresholding_simple_example_002.png
   :alt: Probability of exceeding 5 m/s
   :srcset: /auto_examples/images/sphx_glr_thresholding_simple_example_002.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.222 seconds)


.. _sphx_glr_download_auto_examples_thresholding_simple_example.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: thresholding_simple_example.ipynb <thresholding_simple_example.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: thresholding_simple_example.py <thresholding_simple_example.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: thresholding_simple_example.zip <thresholding_simple_example.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
