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

.. only:: html

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

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

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

.. _sphx_glr_gallery_MapProjections_NCL_proj_3.py:


NCL_proj_3.py
=============

This script illustrates the following concepts:
   - Drawing filled contours over an orthographic map
   - Changing the center latitude and longitude for an orthographic projection
   - Turning off map fill

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

.. GENERATED FROM PYTHON SOURCE LINES 15-16

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 16-25

.. code-block:: Python


    import numpy as np
    import xarray as xr
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt

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








.. GENERATED FROM PYTHON SOURCE LINES 26-27

Read in data:

.. GENERATED FROM PYTHON SOURCE LINES 27-32

.. code-block:: Python


    # Open a netCDF data file using xarray default engine and load the data into xarrays
    ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False)
    t = ds.TS.isel(time=0)








.. GENERATED FROM PYTHON SOURCE LINES 33-34

Fix the artifact of not-shown-data around 0 and 360-degree longitudes

.. GENERATED FROM PYTHON SOURCE LINES 34-36

.. code-block:: Python

    wrap_t = gv.xr_add_cyclic_longitudes(t, "lon")








.. GENERATED FROM PYTHON SOURCE LINES 37-38

Plot:

.. GENERATED FROM PYTHON SOURCE LINES 38-79

.. code-block:: Python


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

    # Generate axes using Cartopy and draw coastlines with
    ax = plt.axes(projection=ccrs.Orthographic(central_longitude=-120, central_latitude=50))

    # Set extent to include latitudes between 0 and 90, and longitude between
    # 0 and -180 only
    ax.set_extent([0, -180, 0, 90], ccrs.PlateCarree())
    ax.set_global()
    ax.coastlines(linewidths=0.5)

    # Plot data and add a colorbar
    temp = wrap_t.plot.contourf(
        ax=ax, transform=ccrs.PlateCarree(), levels=11, cmap='coolwarm', add_colorbar=False
    )

    cbar_ticks = np.arange(210, 311, 10)
    cbar = plt.colorbar(
        temp,
        orientation='horizontal',
        shrink=0.75,
        pad=0.05,
        extendrect=True,
        ticks=cbar_ticks,
    )

    cbar.ax.tick_params(labelsize=10)

    # Use geocat.viz.util convenience function to add titles to left and right
    # of the plot axis.
    gv.set_titles_and_labels(
        ax,
        maintitle="Example of Orthogonal Projection",
        lefttitle="Surface Temperature",
        righttitle="K",
    )

    # Show the plot
    plt.show()



.. image-sg:: /gallery/MapProjections/images/sphx_glr_NCL_proj_3_001.png
   :alt: Surface Temperature, Example of Orthogonal Projection, K
   :srcset: /gallery/MapProjections/images/sphx_glr_NCL_proj_3_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_gallery_MapProjections_NCL_proj_3.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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