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

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

.. _sphx_glr_auto_examples_plot_ensemble_verification.py:


Ensemble verification
=====================

In this tutorial we perform a verification of a probabilistic extrapolation nowcast
using MeteoSwiss radar data.

.. GENERATED FROM PYTHON SOURCE LINES 10-24

.. code-block:: Python


    from datetime import datetime
    from pprint import pprint

    import matplotlib.pyplot as plt
    import numpy as np

    from pysteps import io, nowcasts, rcparams, verification
    from pysteps.motion.lucaskanade import dense_lucaskanade
    from pysteps.postprocessing import ensemblestats
    from pysteps.utils import conversion, dimension, transformation
    from pysteps.visualization import plot_precip_field
    from pysteps.xarray_helpers import geodata_from_dataset








.. GENERATED FROM PYTHON SOURCE LINES 25-31

Read precipitation field
------------------------

First, we will import the sequence of MeteoSwiss ("mch") radar composites.
You need the pysteps-data archive downloaded and the pystepsrc file
configured with the data_source paths pointing to data folders.

.. GENERATED FROM PYTHON SOURCE LINES 31-39

.. code-block:: Python


    # Selected case
    date = datetime.strptime("201607112100", "%Y%m%d%H%M")
    data_source = rcparams.data_sources["mch"]
    n_ens_members = 20
    n_leadtimes = 6
    seed = 24








.. GENERATED FROM PYTHON SOURCE LINES 40-45

Load the data from the archive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The data are upscaled to 2 km resolution to limit the memory usage and thus
be able to afford a larger number of ensemble members.

.. GENERATED FROM PYTHON SOURCE LINES 45-89

.. code-block:: Python


    root_path = data_source["root_path"]
    path_fmt = data_source["path_fmt"]
    fn_pattern = data_source["fn_pattern"]
    fn_ext = data_source["fn_ext"]
    importer_name = data_source["importer"]
    importer_kwargs = data_source["importer_kwargs"]
    timestep = data_source["timestep"]

    # Find the radar files in the archive
    fns = io.find_by_date(
        date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=2
    )

    # Read the data from the archive
    importer = io.get_method(importer_name, "importer")
    precip_dataset = io.read_timeseries(fns, importer, **importer_kwargs)

    # Convert to rain rate
    precip_dataset = conversion.to_rainrate(precip_dataset)
    precip_var = precip_dataset.attrs["precip_var"]

    # Upscale data to 2 km
    precip_dataset = dimension.aggregate_fields_space(precip_dataset, 2000)

    # Build the geodata dict expected by the plotting routines
    geodata = geodata_from_dataset(precip_dataset)

    # Plot the rainfall field
    plot_precip_field(precip_dataset[precip_var][-1], geodata=geodata)
    plt.show()

    # Log-transform the data to unit of dBR, set the threshold to 0.1 mm/h,
    # set the fill value to -15 dBR
    precip_dataset = transformation.dB_transform(
        precip_dataset, threshold=0.1, zerovalue=-15.0
    )

    # Set missing values with the fill value
    precip_dataset[precip_var] = precip_dataset[precip_var].fillna(-15.0)

    # Nicely print the metadata
    pprint(dict(precip_dataset[precip_var].attrs))




.. image-sg:: /auto_examples/images/sphx_glr_plot_ensemble_verification_001.png
   :alt: plot ensemble verification
   :srcset: /auto_examples/images/sphx_glr_plot_ensemble_verification_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    {'accutime': 5,
     'long_name': 'instantaneous precipitation rate',
     'standard_name': 'instantaneous_precipitation_rate',
     'threshold': -10.0,
     'transform': 'dB',
     'units': 'mm/h',
     'zerovalue': -15.0,
     'zr_a': 316.0,
     'zr_b': 1.5}




.. GENERATED FROM PYTHON SOURCE LINES 90-94

Forecast
--------

We use the STEPS approach to produce a ensemble nowcast of precipitation fields.

.. GENERATED FROM PYTHON SOURCE LINES 94-135

.. code-block:: Python


    # Estimate the motion field
    precip_dataset_with_motion = dense_lucaskanade(precip_dataset)

    # Perform the ensemble nowcast with STEPS
    nowcast_method = nowcasts.get_method("steps")
    ensemble_precip_forecast = nowcast_method(
        precip_dataset_with_motion.isel(time=slice(-3, None)),
        n_leadtimes,
        n_ens_members,
        n_cascade_levels=6,
        precip_thr=-10.0,
        kmperpixel=2.0,
        timestep=timestep,
        decomp_method="fft",
        bandpass_filter_method="gaussian",
        noise_method="nonparametric",
        vel_pert_method="bps",
        mask_method="incremental",
        seed=seed,
    )

    # Back-transform to rain rates
    ensemble_precip_forecast = transformation.dB_transform(
        ensemble_precip_forecast, threshold=-10.0, inverse=True
    )

    # Plot some of the realizations
    fig = plt.figure()
    for i in range(4):
        ax = fig.add_subplot(221 + i)
        ax.set_title("Member %02d" % i)
        plot_precip_field(
            ensemble_precip_forecast[precip_var][i, -1],
            geodata=geodata,
            colorbar=False,
            axis="off",
        )
    plt.tight_layout()
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_ensemble_verification_002.png
   :alt: Member 00, Member 01, Member 02, Member 03
   :srcset: /auto_examples/images/sphx_glr_plot_ensemble_verification_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    Inputs validated and initialized successfully.
    Computing STEPS nowcast
    -----------------------

    Inputs
    ------
    input dimensions: 320x355
    km/pixel:         2.0
    time step:        5 minutes

    Methods
    -------
    extrapolation:          semilagrangian
    bandpass filter:        gaussian
    decomposition:          fft
    noise generator:        nonparametric
    noise adjustment:       no
    velocity perturbator:   bps
    conditional statistics: no
    precip. mask method:    incremental
    probability matching:   cdf
    FFT method:             numpy
    domain:                 spatial

    Parameters
    ----------
    number of time steps:     6
    ensemble size:            20
    parallel threads:         1
    number of cascade levels: 6
    order of the AR(p) model: 2
    velocity perturbations, parallel:      10.88,0.23,-7.68
    velocity perturbations, perpendicular: 5.76,0.31,-2.72
    precip. intensity threshold: -10.0
    Nowcast components initialized successfully.
    Rain fraction is: 0.0914612676056338, while minimum fraction is 0.0
    Extrapolation complete and precipitation fields aligned.
    ************************************************
    * Correlation coefficients for cascade levels: *
    ************************************************
    -----------------------------------------
    | Level |     Lag-1     |     Lag-2     |
    -----------------------------------------
    | 1     | 0.998447      | 0.994789      |
    -----------------------------------------
    | 2     | 0.998203      | 0.994117      |
    -----------------------------------------
    | 3     | 0.992448      | 0.976680      |
    -----------------------------------------
    | 4     | 0.964647      | 0.895337      |
    -----------------------------------------
    | 5     | 0.842399      | 0.648995      |
    -----------------------------------------
    | 6     | 0.358683      | 0.206198      |
    -----------------------------------------
    ****************************************
    * AR(p) parameters for cascade levels: *
    ****************************************
    ------------------------------------------------------
    | Level |    Phi-1     |    Phi-2     |    Phi-0     |
    ------------------------------------------------------
    | 1     | 1.676577     | -0.679185    | 0.040887     |
    ------------------------------------------------------
    | 2     | 1.635297     | -0.638241    | 0.046132     |
    ------------------------------------------------------
    | 3     | 1.538117     | -0.549821    | 0.102461     |
    ------------------------------------------------------
    | 4     | 1.453617     | -0.506890    | 0.227180     |
    ------------------------------------------------------
    | 5     | 1.018330     | -0.208845    | 0.526971     |
    ------------------------------------------------------
    | 6     | 0.326762     | 0.088994     | 0.929756     |
    ------------------------------------------------------
    AR model and noise applied to precipitation cascades.
    Velocity perturbations initialized successfully.
    Precipitation mask initialized successfully.
    FFT objects initialized successfully.
    Starting nowcast computation.
    Computing nowcast for time step 1... done.
    Computing nowcast for time step 2... done.
    Computing nowcast for time step 3... done.
    Computing nowcast for time step 4... done.
    Computing nowcast for time step 5... done.
    Computing nowcast for time step 6... done.




.. GENERATED FROM PYTHON SOURCE LINES 136-145

Verification
------------

Pysteps includes a number of verification metrics to help users to analyze
the general characteristics of the nowcasts in terms of consistency and
quality (or goodness).
Here, we will verify our probabilistic forecasts using the ROC curve,
reliability diagrams, and rank histograms, as implemented in the verification
module of pysteps.

.. GENERATED FROM PYTHON SOURCE LINES 145-174

.. code-block:: Python


    # Find the files containing the verifying observations
    fns = io.archive.find_by_date(
        date,
        root_path,
        path_fmt,
        fn_pattern,
        fn_ext,
        timestep,
        0,
        num_next_files=n_leadtimes,
    )

    # Read the observations
    obs_dataset = io.read_timeseries(fns, importer, **importer_kwargs)

    # Convert to mm/h
    obs_dataset = conversion.to_rainrate(obs_dataset)

    # Upscale data to 2 km
    obs_dataset = dimension.aggregate_fields_space(obs_dataset, 2000)

    # Compute the verification for the last lead time

    # compute the exceedance probability of 0.1 mm/h from the ensemble
    P_f = ensemblestats.excprob(
        ensemble_precip_forecast[precip_var][:, -1].values, 0.1, ignore_nan=True
    )





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

 .. code-block:: none

    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(
    /home/docs/checkouts/pysteps/checkouts/631/pysteps/xarray_helpers.py:94: UserWarning: No CF grid mapping available for projection 'somerc'; the dataset is created without a grid_mapping attribute
      warnings.warn(




.. GENERATED FROM PYTHON SOURCE LINES 175-177

ROC curve
~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 177-185

.. code-block:: Python


    roc = verification.ROC_curve_init(0.1, n_prob_thrs=10)
    verification.ROC_curve_accum(roc, P_f, obs_dataset[precip_var][-1].values)
    fig, ax = plt.subplots()
    verification.plot_ROC(roc, ax, opt_prob_thr=True)
    ax.set_title("ROC curve (+%i min)" % (n_leadtimes * timestep))
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_ensemble_verification_003.png
   :alt: ROC curve (+30 min)
   :srcset: /auto_examples/images/sphx_glr_plot_ensemble_verification_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 186-188

Reliability diagram
~~~~~~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 188-196

.. code-block:: Python


    reldiag = verification.reldiag_init(0.1)
    verification.reldiag_accum(reldiag, P_f, obs_dataset[precip_var][-1].values)
    fig, ax = plt.subplots()
    verification.plot_reldiag(reldiag, ax)
    ax.set_title("Reliability diagram (+%i min)" % (n_leadtimes * timestep))
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_ensemble_verification_004.png
   :alt: Reliability diagram (+30 min)
   :srcset: /auto_examples/images/sphx_glr_plot_ensemble_verification_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 197-199

Rank histogram
~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 199-214

.. code-block:: Python


    rankhist = verification.rankhist_init(
        ensemble_precip_forecast[precip_var].shape[0], 0.1
    )
    verification.rankhist_accum(
        rankhist,
        ensemble_precip_forecast[precip_var][:, -1].values,
        obs_dataset[precip_var][-1].values,
    )
    fig, ax = plt.subplots()
    verification.plot_rankhist(rankhist, ax)
    ax.set_title("Rank histogram (+%i min)" % (n_leadtimes * timestep))
    plt.show()

    # sphinx_gallery_thumbnail_number = 5



.. image-sg:: /auto_examples/images/sphx_glr_plot_ensemble_verification_005.png
   :alt: Rank histogram (+30 min)
   :srcset: /auto_examples/images/sphx_glr_plot_ensemble_verification_005.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_plot_ensemble_verification.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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