
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/thresholding_example_data.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_example_data.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_thresholding_example_data.py:


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

.. GENERATED FROM PYTHON SOURCE LINES 14-18

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 19-22

Load example data
-----------------
Load example realization data for thresholding.

.. GENERATED FROM PYTHON SOURCE LINES 22-35

.. code-block:: Python

    from improver import example_data_path
    from improver.utilities.load import load_cube

    # Get the path to an example file with realizations
    data_path = example_data_path(
        "threshold", "20251120T1500Z-PT0060H00M-wind_speed_at_10m.nc"
    )

    # Load the cube
    realization_cube = load_cube(str(data_path))

    print(realization_cube)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    /home/docs/checkouts/readthedocs.org/user_builds/improver/conda/stable/lib/python3.13/site-packages/iris/common/mixin.py:212: FutureWarning: You are using legacy date precision for Iris units - max precision is seconds. In future, Iris will use microsecond precision - available since cf-units version 3.3 - which may affect core behaviour. To opt-in to the new behaviour, set `iris.FUTURE.date_microseconds = True`.
      warnings.warn(message, category=FutureWarning)
    wind_speed / (m s-1)                (realization: 3; projection_y_coordinate: 970; projection_x_coordinate: 1042)
        Dimension coordinates:
            realization                             x                           -                             -
            projection_y_coordinate                 -                           x                             -
            projection_x_coordinate                 -                           -                             x
        Scalar coordinates:
            forecast_period             216000 seconds
            forecast_reference_time     2025-11-18 03:00:00
            height                      10.0 m
            time                        2025-11-20 15:00:00
        Attributes:
            Conventions                 'CF-1.7, UKMO-1.0'
            history                     '2025-11-18T05:13:56Z: StaGE Decoupler'
            institution                 'Met Office'
            least_significant_digit     1
            mosg__forecast_run_duration 'PT126H'
            mosg__grid_domain           'uk_extended'
            mosg__grid_type             'standard'
            mosg__grid_version          '2.4.0'
            mosg__model_configuration   'uk_ens'
            source                      'Met Office Unified Model'
            title                       'MOGREPS-UK Model Forecast on UK 2 km Standard Grid'
            um_version                  '13.1'




.. GENERATED FROM PYTHON SOURCE LINES 36-37

Threshold the example cube at 5 m/s.

.. GENERATED FROM PYTHON SOURCE LINES 37-43

.. code-block:: Python

    from improver.threshold import Threshold

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








.. GENERATED FROM PYTHON SOURCE LINES 44-46

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 46-58

.. 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_example_data_001.png
   :alt: Thresholded Cube (> 5 m/s), Realization 0, Realization 1, Realization 2
   :srcset: /auto_examples/images/sphx_glr_thresholding_example_data_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 59-60

Collapse the realization dimension as part of thresholding.

.. GENERATED FROM PYTHON SOURCE LINES 60-66

.. 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 67-70

Plot the probabilities of exceeding 5 m/s after collapsing the realization dimension.
Note that these probabilities are now non-binary with areas of intermediate values,
for example, to the north and west of Ireland.

.. GENERATED FROM PYTHON SOURCE LINES 70-87

.. code-block:: Python

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

    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_example_data_002.png
   :alt: Probability of exceeding 5 m/s
   :srcset: /auto_examples/images/sphx_glr_thresholding_example_data_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_thresholding_example_data.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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