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

.. only:: html

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

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

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

.. _sphx_glr_gallery_Transect_NCL_trans_1.py:


NCL_trans_1.py
==============
Calculate and plot a transect and transect location

This script illustrates the following concepts:
  - How to calculate a transect with metpy's ``cross_section``
  - How to plot the location of the transect

See following URLs to see the related NCL plot & scripts:
  - NCL_trans_1_1: `script <https://www.ncl.ucar.edu/Applications/Scripts/trans_1.ncl>`_, `plot <https://www.ncl.ucar.edu/Applications/Images/trans_1_1_lg.png>`__
  - NCL_trans_1_2: same script as above, `plot <https://www.ncl.ucar.edu/Applications/Images/trans_1_2_lg.png>`__

.. GENERATED FROM PYTHON SOURCE LINES 16-17

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 17-26

.. code-block:: Python

    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import xarray as xr
    import numpy as np
    from metpy.interpolate import cross_section
    import cartopy.feature as cfeature

    import geocat.datafiles as gdf








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Read in data:

.. GENERATED FROM PYTHON SOURCE LINES 28-60

.. 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/h_avg_Y0191_D000.00.nc"), decode_times=False)

    # Add attrs and parse for CF compliance to work with metpy package
    # only necessary if data is not CF compliant to start with
    # See unidata.github.io/MetPy/latest/tutorials/xarray_tutorial.html#coordinates-and-coordinate-reference-systems
    # and docs.xarray.dev/en/latest/user-guide/io.html#reading-encoded-data for more information
    ds.time_bound.attrs['units'] = 'days since 0000-01-01 00:00:00'
    ds.time_bound.attrs['calendar'] = 'noleap'
    ds.time.attrs['calendar'] = 'noleap'
    ds = xr.decode_cf(ds)

    # format for metpy
    ds = ds.metpy.assign_crs(
        grid_mapping_name='latitude_longitude', earth_radius=6371229.0
    ).rename({"lat_t": "lat", "lon_t": "lon"})


    # Pull out temperature
    t = ds.T[0, :, :, :]

    # Define transect parameters
    leftlat = -60
    rightlat = -30

    leftlon = -60
    rightlon = 20

    npts = 100








.. GENERATED FROM PYTHON SOURCE LINES 61-62

Calculate transect

.. GENERATED FROM PYTHON SOURCE LINES 62-66

.. code-block:: Python


    # calculate with metpy's cross_section
    transect = cross_section(t, (leftlat, leftlon), (rightlat, rightlon), steps=npts)








.. GENERATED FROM PYTHON SOURCE LINES 67-68

Plot transect

.. GENERATED FROM PYTHON SOURCE LINES 68-87

.. code-block:: Python


    # format attributes for plotting
    transect.attrs['long_name'] = transect.long_name + " Transect"

    cmap = 'viridis'

    fig, ax = plt.subplots(1, 1)
    p = transect.plot.contourf(ax=ax, cmap=cmap, vmin=-2, vmax=19, levels=22)
    ax.invert_yaxis()
    plt.title(transect.long_name)

    ax.set_xlabel('(lat, lon) along transect')
    ax.set_xticks([transect.index.min(), transect.index.max()])
    ax.set_xticklabels(['(-60, 60)', '(-30, 20)'])

    # Show plot
    plt.tight_layout()
    plt.show()




.. image-sg:: /gallery/Transect/images/sphx_glr_NCL_trans_1_001.png
   :alt: Potential Temperature Transect
   :srcset: /gallery/Transect/images/sphx_glr_NCL_trans_1_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 88-89

Plot transect location

.. GENERATED FROM PYTHON SOURCE LINES 89-118

.. code-block:: Python


    projection = ccrs.PlateCarree()
    fig = plt.figure()

    ax = fig.add_subplot(1, 1, 1, projection=projection)
    ax.set_global()

    # Draw land
    ax.add_feature(cfeature.LAND, color='lightgrey')

    # Add transect location line
    ax.plot(
        [leftlon, rightlon],
        [leftlat, rightlat],
        transform=projection,
        color='red',
        linewidth=1,
    )

    # title plot
    ax.set_title("Transect Location")

    # add ticks to axes
    ax.set_xticks(np.linspace(-180, 180, 13))
    ax.set_yticks(np.linspace(-90, 90, 7))

    # Show plot
    plt.tight_layout()
    plt.show()



.. image-sg:: /gallery/Transect/images/sphx_glr_NCL_trans_1_002.png
   :alt: Transect Location
   :srcset: /gallery/Transect/images/sphx_glr_NCL_trans_1_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_gallery_Transect_NCL_trans_1.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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