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

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

.. _sphx_glr_auto_examples_plot_custom_precipitation_range.py:


Plot precipitation using custom colormap
=============

This tutorial shows how to plot data using a custom colormap with a specific
range of precipitation values.

.. GENERATED FROM PYTHON SOURCE LINES 10-22

.. code-block:: Python


    import os
    from datetime import datetime
    import matplotlib.pyplot as plt

    import pysteps
    from pysteps import io, rcparams
    from pysteps.utils import conversion
    from pysteps.visualization import plot_precip_field
    from pysteps.datasets import download_pysteps_data, create_default_pystepsrc









.. GENERATED FROM PYTHON SOURCE LINES 23-29

Download the data if it is not available
----------------------------------------

The following code block downloads datasets from the pysteps-data repository
if it is not available on the disk. The dataset is used to demonstrate the
plotting of precipitation data using a custom colormap.

.. GENERATED FROM PYTHON SOURCE LINES 29-39

.. code-block:: Python


    # Check if the pysteps-data repository is available (it would be pysteps-data in pysteps)
    # Implies that you are running this script from the `pysteps/examples` folder

    if not os.path.exists(rcparams.data_sources["mrms"]["root_path"]):
        download_pysteps_data("pysteps_data")
        config_file_path = create_default_pystepsrc("pysteps_data")
        print(f"Configuration file has been created at {config_file_path}")









.. GENERATED FROM PYTHON SOURCE LINES 40-45

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

First thing, load a frame from Multi-Radar Multi-Sensor dataset and convert it
to precipitation rate in mm/h.

.. GENERATED FROM PYTHON SOURCE LINES 45-79

.. code-block:: Python


    # Define the dataset and the date for which you want to load data
    data_source = pysteps.rcparams.data_sources["mrms"]
    date = datetime(2019, 6, 10, 0, 2, 0)  # Example date

    # Extract the parameters from the data source
    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 frame in the archive for the specified date
    fns = io.find_by_date(
        date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=1
    )

    # Read the frame from the archive
    importer = io.get_method(importer_name, "importer")
    R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)

    # Convert the reflectivity data to rain rate
    R, metadata = conversion.to_rainrate(R, metadata)

    # Plot the first rainfall field from the loaded data
    plt.figure(figsize=(10, 5), dpi=300)
    plt.axis("off")
    plot_precip_field(R[0, :, :], geodata=metadata, axis="off")

    plt.tight_layout()
    plt.show()




.. image-sg:: /auto_examples/images/sphx_glr_plot_custom_precipitation_range_001.png
   :alt: plot custom precipitation range
   :srcset: /auto_examples/images/sphx_glr_plot_custom_precipitation_range_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    /home/docs/checkouts/readthedocs.org/user_builds/pysteps/envs/v1.17.0/lib/python3.10/site-packages/pysteps/visualization/utils.py:439: UserWarning: cartopy package is required for the get_geogrid function but it is not installed. Ignoring geographical information.
      warnings.warn(




.. GENERATED FROM PYTHON SOURCE LINES 80-97

Define the custom colormap
--------------------------

Assume that the default colormap does not represent the precipitation values
in the desired range. In this case, you can define a custom colormap that will
be used to plot the precipitation data and pass the class instance to the
`plot_precip_field` function.

It essential for the custom colormap to have the following attributes:

- `cmap`: The colormap object.
- `norm`: The normalization object.
- `clevs`: The color levels for the colormap.

`plot_precip_field` can handle each of the classes defined in the `matplotlib.colors`
https://matplotlib.org/stable/api/colors_api.html#colormaps
There must be as many colors in the colormap as there are levels in the color levels.

.. GENERATED FROM PYTHON SOURCE LINES 97-144

.. code-block:: Python



    # Define the custom colormap

    from matplotlib import colors


    class ColormapConfig:
        def __init__(self):
            self.cmap = None
            self.norm = None
            self.clevs = None

            self.build_colormap()

        def build_colormap(self):
            # Define the colormap boundaries and colors
            # color_list = ['lightgrey', 'lightskyblue', 'blue', 'yellow', 'orange', 'red', 'darkred']
            color_list = ["blue", "navy", "yellow", "orange", "green", "brown", "red"]

            self.clevs = [0.1, 0.5, 1.5, 2.5, 4, 6, 10]  # mm/hr

            # Create a ListedColormap object with the defined colors
            self.cmap = colors.ListedColormap(color_list)
            self.cmap.name = "Custom Colormap"

            # Set the color for values above the maximum level
            self.cmap.set_over("darkmagenta")
            # Set the color for values below the minimum level
            self.cmap.set_under("none")
            # Set the color for missing values
            self.cmap.set_bad("gray", alpha=0.5)

            # Create a BoundaryNorm object to normalize the data values to the colormap boundaries
            self.norm = colors.BoundaryNorm(self.clevs, self.cmap.N)


    # Create an instance of the ColormapConfig class
    config = ColormapConfig()

    # Plot the precipitation field using the custom colormap
    plt.figure(figsize=(10, 5), dpi=300)
    plt.axis("off")
    plot_precip_field(R[0, :, :], geodata=metadata, axis="off", colormap_config=config)

    plt.tight_layout()
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_custom_precipitation_range_002.png
   :alt: plot custom precipitation range
   :srcset: /auto_examples/images/sphx_glr_plot_custom_precipitation_range_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    /home/docs/checkouts/readthedocs.org/user_builds/pysteps/envs/v1.17.0/lib/python3.10/site-packages/pysteps/visualization/utils.py:439: UserWarning: cartopy package is required for the get_geogrid function but it is not installed. Ignoring geographical information.
      warnings.warn(





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

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


.. _sphx_glr_download_auto_examples_plot_custom_precipitation_range.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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