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

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

.. _sphx_glr_examples_plot_hydrology_diags.py:


Profile anomalies diags
=======================

This example requires 3D dataset including depth dimension
Hydrology anomalies can be performes in every 3D fields.

.. GENERATED FROM PYTHON SOURCE LINES 11-15

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

Import needed stuff.

.. GENERATED FROM PYTHON SOURCE LINES 15-30

.. code-block:: Python

    import time

    import cmocean as cm
    import matplotlib.pyplot as plt
    import xarray as xr

    from shoot.dyn import get_relvort
    from shoot.eddies.eddies2d import Eddies2D
    from shoot.hydrology import Anomaly, compute_anomalies
    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 0x7026857d2b10>



.. GENERATED FROM PYTHON SOURCE LINES 31-32

Read data

.. GENERATED FROM PYTHON SOURCE LINES 32-38

.. code-block:: Python

    set_meta_specs("croco")
    root_path = "MODELS/CROCO/MED/pelops_3d.nc"
    path = get_sample_file(root_path)
    ds_3d = xr.open_dataset(path)
    ds_2d = ds_3d.isel(s_rho=len(ds_3d.s_rho) - 1)








.. GENERATED FROM PYTHON SOURCE LINES 39-44

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


.. GENERATED FROM PYTHON SOURCE LINES 44-53

.. code-block:: Python


    window_center = 50

    window_fit = 120

    min_radius = 10

    ellipse_error = 0.05








.. GENERATED FROM PYTHON SOURCE LINES 54-56

Detection
~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 56-72

.. code-block:: Python


    start = time.time()

    eddies = Eddies2D.detect_eddies(
        ds_2d.u,
        ds_2d.v,
        window_center,
        window_fit=window_fit,
        ssh=ds_2d.zeta,
        min_radius=min_radius,
        paral=True,
    )
    end = time.time()
    print("it takes %.1f s" % (end - start))






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

 .. code-block:: none

    it takes 3.0 s




.. GENERATED FROM PYTHON SOURCE LINES 73-75

Anomalies
---------

.. GENERATED FROM PYTHON SOURCE LINES 75-85

.. code-block:: Python


    # Detect anomaly exemple on a particular eddy
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    eddy = eddies.eddies[0]
    # here you can choose the desire variable : density, salinity, temp, celerity
    anomaly = Anomaly(eddy, eddies, ds_3d.sig0, depth=ds_3d.depth, r_factor=1.2)









.. GENERATED FROM PYTHON SOURCE LINES 86-92

Plots
-----

We plot eddies with the relative vorticity as background.
It shows the selected inside and outside profiles


.. GENERATED FROM PYTHON SOURCE LINES 92-129

.. code-block:: Python

    fig, ax = create_map(ds_2d.lon_rho, ds_2d.lat_rho, figsize=(8, 5))
    vort = get_relvort(ds_2d.u, ds_2d.v) / 1e-4
    cb = vort.plot(
        x="lon_rho",
        y="lat_rho",
        cmap=cm.cm.curl,
        ax=ax,
        add_colorbar=False,
        transform=pcarr,
    )

    plt.colorbar(cb, label=r"$\zeta/f$")

    for eddy in eddies.eddies:
        eddy.plot(transform=pcarr, lw=1, vmax=True, boundary=True)
    ax.scatter(
        anomaly._profils_outside.lon_rho,
        anomaly._profils_outside.lat_rho,
        s=10,
        marker="o",
        transform=pcarr,
        label="outside",
    )

    cmb = ax.scatter(
        anomaly._profils_inside.lon_rho,
        anomaly._profils_inside.lat_rho,
        s=10,
        marker="*",
        transform=pcarr,
        label="inside",
    )

    plt.title("Eddy detection")
    plt.tight_layout()
    plt.legend()




.. image-sg:: /examples/images/sphx_glr_plot_hydrology_diags_001.png
   :alt: Eddy detection
   :srcset: /examples/images/sphx_glr_plot_hydrology_diags_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.legend.Legend object at 0x7026a10dc190>



.. GENERATED FROM PYTHON SOURCE LINES 130-131

Plot anomaly

.. GENERATED FROM PYTHON SOURCE LINES 131-166

.. code-block:: Python


    plt.figure(figsize=(10, 5))
    plt.subplot(121)
    plt.plot(anomaly.anomaly, anomaly.depth_vector, label="averaged profile")
    plt.plot(anomaly.center_anomaly, anomaly.depth_vector, label="center profile")
    plt.xlabel(r"$\Delta\sigma_0$ [kg/m3]")
    # plt.xlabel(r'$\Delta cs$ [m/s]')
    plt.ylabel("Depth [m]")
    plt.ylim(-2000, 0)
    plt.legend()
    ax = plt.subplot(122)
    ax.plot(anomaly.mean_profil_inside, anomaly.depth_vector, c="b", label="inside")
    ax.fill_betweenx(
        anomaly.depth_vector,
        anomaly.mean_profil_inside - anomaly.std_profil_inside,
        anomaly.mean_profil_inside + anomaly.std_profil_inside,
        alpha=0.2,
        color="b",
    )
    ax.plot(anomaly.mean_profil_outside, anomaly.depth_vector, c="k", label="outside")
    ax.fill_betweenx(
        anomaly.depth_vector,
        anomaly.mean_profil_outside - anomaly.std_profil_outside,
        anomaly.mean_profil_outside + anomaly.std_profil_outside,
        alpha=0.2,
        color="k",
    )
    plt.legend()
    plt.xlabel(r"$\sigma_0$ [kg/m3]")
    # plt.xlabel(r'$cs$ [m/s]')
    # plt.ylabel('Depth [m]')
    plt.yticks([], [])
    plt.ylim(-2000, 0)





.. image-sg:: /examples/images/sphx_glr_plot_hydrology_diags_002.png
   :alt: plot hydrology diags
   :srcset: /examples/images/sphx_glr_plot_hydrology_diags_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (-2000.0, 0.0)



.. GENERATED FROM PYTHON SOURCE LINES 167-168

Compute anomalies of all eddies

.. GENERATED FROM PYTHON SOURCE LINES 168-170

.. code-block:: Python


    compute_anomalies(eddies, ds_3d.sig0, r_factor=1.05)








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

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


.. _sphx_glr_download_examples_plot_hydrology_diags.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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