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

.. only:: html

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

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

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

.. _sphx_glr_gallery_WRF_NCL_dataonmap_10.py:


NCL_dataonmap_10.py
===================
This script illustrates the following concepts:
    - Plotting WRF data on native grid
    - Plotting data using wrf python functions
    - Overlaying continent outlines on a map
    - Following best practices when choosing a colormap.
      More information on colormap best practices can be found `here <https://geocat-examples.readthedocs.io/en/latest/gallery/Colors/CB_Temperature.html#sphx-glr-gallery-colors-cb-temperature-py>`_.

See following URLs to see the reproduced NCL plot & script:
    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/dataonmap_10.ncl
    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/dataonmap_10_1_lg.png

.. GENERATED FROM PYTHON SOURCE LINES 17-18

Import packages

.. GENERATED FROM PYTHON SOURCE LINES 18-29

.. code-block:: Python


    from netCDF4 import Dataset
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mticker
    import cartopy.crs as ccrs
    from wrf import getvar, to_np, latlon_coords

    import geocat.datafiles as gdf
    import geocat.viz as gv








.. GENERATED FROM PYTHON SOURCE LINES 30-31

Read in the data

.. GENERATED FROM PYTHON SOURCE LINES 31-37

.. code-block:: Python


    wrfin = Dataset(
        gdf.get("netcdf_files/wrfout_d01_2003-07-15_00_00_00"), decode_times=True
    )
    q2 = getvar(wrfin, "Q2")








.. GENERATED FROM PYTHON SOURCE LINES 38-39

Plot the data

.. GENERATED FROM PYTHON SOURCE LINES 39-107

.. code-block:: Python


    # Get the latitude and longitude coordinate. This is usually needed for plotting.
    lats, lons = latlon_coords(q2)

    # Generate figure (set its size (width, height) in inches)
    fig = plt.figure(figsize=(10, 10))

    # Generate axes using Cartopy
    ax = plt.axes(projection=ccrs.PlateCarree())

    # Add filled contours
    plt.contourf(
        to_np(lons),
        to_np(lats),
        q2,
        levels=np.linspace(0.01125, 0.05, 32),
        cmap="magma",
        vmin=0,
        vmax=0.05,
        zorder=4,
    )

    # Add a colorbar
    cbar = plt.colorbar(
        ax=ax,
        orientation="vertical",
        ticks=np.arange(0.0125, 0.0476, 0.0025),
        drawedges=True,
        extendrect=True,
        shrink=0.65,
    )

    # Format colorbar ticks and labels
    cbar.ax.tick_params(size=0, labelsize=10)

    # Draw gridlines
    gl = ax.gridlines(
        crs=ccrs.PlateCarree(),
        draw_labels=True,
        dms=False,
        x_inline=False,
        y_inline=False,
        linewidth=1,
        color="k",
        alpha=0.25,
        zorder=4,
    )

    # Manipulate latitude and longitude gridline numbers and spacing
    gl.top_labels = False
    gl.right_labels = False
    gl.xlocator = mticker.FixedLocator(np.arange(-105, -80, 5))
    gl.ylocator = mticker.FixedLocator(np.arange(18, 35, 2))
    gl.xlabel_style = {"rotation": 0, "size": 10}
    gl.ylabel_style = {"rotation": 0, "size": 10}
    gl.xlines = True
    gl.ylines = True

    # Add titles and labels to projection
    gv.set_titles_and_labels(
        ax,
        maintitle="WRF data on native grid",
        lefttitle="QV at 2 M",
        maintitlefontsize=16,
        lefttitlefontsize=14,
    )

    plt.show()



.. image-sg:: /gallery/WRF/images/sphx_glr_NCL_dataonmap_10_001.png
   :alt: QV at 2 M, WRF data on native grid
   :srcset: /gallery/WRF/images/sphx_glr_NCL_dataonmap_10_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_gallery_WRF_NCL_dataonmap_10.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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