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

.. only:: html

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

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

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

.. _sphx_glr_examples_plot_eddy_detect_3d.py:


Detect CROCO-MED1.8 eddies at different depths and link the detections
======================================================================

In this example, 3D eddies are detected from CROCO model currents interpolated at several depths
and collocated at RHO points.

.. GENERATED FROM PYTHON SOURCE LINES 11-15

Initialisations
-----------------

Import needed stuff.

.. GENERATED FROM PYTHON SOURCE LINES 15-27

.. code-block:: Python

    import cmocean  # noqa
    import matplotlib.pyplot as plt
    import xarray as xr

    from shoot.dyn import get_relvort
    from shoot.eddies.eddies3d import Eddies3D
    from shoot.meta import set_meta_specs
    from shoot.plot import create_map, pcarr
    from shoot.samples import get_sample_file

    xr.set_options(display_style="text")





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

 .. code-block:: none


    <xarray.core.options.set_options object at 0x702699232c90>



.. GENERATED FROM PYTHON SOURCE LINES 28-29

Load croco-specific naming conventions to find dims, coords and variables

.. GENERATED FROM PYTHON SOURCE LINES 29-31

.. code-block:: Python

    set_meta_specs("croco")








.. GENERATED FROM PYTHON SOURCE LINES 32-33

Read data

.. GENERATED FROM PYTHON SOURCE LINES 33-38

.. code-block:: Python


    root_path = "MODELS/CROCO/MED/pelops_3d_interp.nc"
    path = get_sample_file(root_path)
    ds_3d = xr.open_dataset(path)








.. GENERATED FROM PYTHON SOURCE LINES 39-45

Detect eddies
-------------
Parameters
~~~~~~~~~~

Window size in km to compute the LNAM and find eddy centers

.. GENERATED FROM PYTHON SOURCE LINES 45-52

.. code-block:: Python

    window_center = 50  # 50

    window_fit = 120

    min_radius = 10









.. GENERATED FROM PYTHON SOURCE LINES 53-64

.. code-block:: Python


    eddies3d = Eddies3D.detect_eddies_3d(
        ds_3d.u,
        ds_3d.v,
        window_center,
        window_fit=window_fit,
        min_radius=min_radius,
        paral=True,
    )









.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: Python

    eddies3d.plot2d(depth=10)





.. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_001.png
   :alt: time = 2024-04-10T00:01:00, depth = -10
   :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 69-115

.. code-block:: Python


    # Plots eddies
    # -----
    #
    # We plot eddies with the relative vorticity as background.
    #

    eddies3d_slice = eddies3d.eddies_byslice

    for i in range(len(ds_3d.depth)):
        fig, ax = create_map(
            ds_3d.isel(depth=i).lon_rho,
            ds_3d.isel(depth=i).lat_rho,
            figsize=(8, 5),
        )
        cb = get_relvort(ds_3d.isel(depth=i).u, ds_3d.isel(depth=i).v).plot(
            x="lon_rho",
            y="lat_rho",
            cmap="cmo.curl",
            ax=ax,
            add_colorbar=False,
            transform=pcarr,
        )
        plt.colorbar(cb, label=r"$\zeta$")

        plt.quiver(
            ds_3d.lon_rho[::5, ::5].values,
            ds_3d.lat_rho[::5, ::5].values,
            ds_3d.isel(depth=i).u[::5, ::5].values,
            ds_3d.isel(depth=i).v[::5, ::5].values,
            color="k",
            transform=pcarr,
        )

        for eddy in eddies3d_slice.eddies3d[i].eddies:
            eddy.plot(transform=pcarr, lw=1)
            # plt.text(eddy.glon, eddy.glat, eddy.z_id, transform=pcarr)
            plt.text(
                eddy.glon,
                eddy.glat,
                f"{eddy.z_id} - {eddy.vmax_contour.mean_velocity:.2f} m/s",
                transform=pcarr,
            )

        plt.title(f"Relative vorticity at {ds_3d.isel(depth=i).depth} m depth")
        plt.tight_layout()



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_002.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-1000) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -1000 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_003.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-500) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -500 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_003.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_004.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-400) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -400 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_004.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_005.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-300) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -300 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_005.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_006.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-200) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -200 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_006.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_007.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-150) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -150 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_007.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_008.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-100) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -100 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_008.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_009.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-50) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -50 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_009.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_plot_eddy_detect_3d_010.png
         :alt: Relative vorticity at <xarray.DataArray 'depth' ()> Size: 8B array(-10) Coordinates:     time     datetime64[ns] 8B 2024-04-10T00:01:00     depth    int64 8B -10 m depth
         :srcset: /examples/images/sphx_glr_plot_eddy_detect_3d_010.png
         :class: sphx-glr-multi-img






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

   **Total running time of the script:** (3 minutes 18.604 seconds)


.. _sphx_glr_download_examples_plot_eddy_detect_3d.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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