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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_tutorials_vertical_plot_layer_thicknesses.py>`
        to download the full example code or to run this example in your browser via Binder.

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

.. _sphx_glr_tutorials_vertical_plot_layer_thicknesses.py:


Depths and layer thicknesses
============================

In this tutorial, we show how to:

* integrate layer thicknesses to get the depths of the interfaces and of the layer centers,
* go back from depths to layer thicknesses,
* find or compute the thicknesses of a dataset, whatever the vertical coordinate,
* use the thicknesses as weights, to compute a depth average.

The first data are the thicknesses of the upper layers of the Hycom model, which vary with time
and position. The second are the currents of a CROCO simulation on sigma levels.
The concepts are explained in the :ref:`indepth.vertical` guide.
See also: :ref:`sphx_glr_tutorials_vertical_plot_croco_section.py`,
:ref:`sphx_glr_tutorials_evaluation_plot_hycom_gdp.py`.

.. GENERATED FROM PYTHON SOURCE LINES 21-23

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

.. GENERATED FROM PYTHON SOURCE LINES 23-37

.. code-block:: Python


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

    import xoa
    import xoa.meta as xmeta
    from xoa.grid import dz2depth, dz2z, z2dz
    from xoa.plot import plot_fields

    xr.set_options(display_style="text")
    plt.rcParams.update({"font.size": 9, "axes.titlesize": 10, "axes.titleweight": "bold"})








.. GENERATED FROM PYTHON SOURCE LINES 38-42

From thicknesses to depths
--------------------------

Hycom provides the thickness of its layers, which vary in time and space.

.. GENERATED FROM PYTHON SOURCE LINES 42-48

.. code-block:: Python


    with xmeta.set_meta_specs("hycom"):
        hycom = xoa.open_data_sample("MODELS/HYCOM/hycom.gdp.h.nc").xoa.decode()
    dz = hycom.dz
    print(dz.dims, dz.isel(time=0, x=0, y=0).values)





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

 .. code-block:: none

    ('time', 'depth', 'y', 'x') [3.0211954 3.4743788 3.995539  4.5948734 5.2841086 6.07673  ]




.. GENERATED FROM PYTHON SOURCE LINES 49-55

:func:`xoa.grid.dz2z` integrates the thicknesses from the top, that is at ``z=0``, and
gives ``z`` positive up at the interfaces: there is one more level than layers.
:func:`xoa.grid.dz2depth` does the same with positive down depths.
With ``centered=True``, the output is at the center of the layers.
The reference of the integration is the surface by default, and it can be the sea surface
height with ``top`` or the sea floor with ``bottom`` when the whole water column is described.

.. GENERATED FROM PYTHON SOURCE LINES 55-62

.. code-block:: Python


    z_interfaces = dz2z(dz.isel(time=0))
    depth_centers = dz2depth(dz.isel(time=0), centered=True)
    print(z_interfaces.sizes["depth"], "interfaces for", depth_centers.sizes["depth"], "layers")
    print(z_interfaces.isel(x=0, y=0).values.round(1))
    print(depth_centers.isel(x=0, y=0).values.round(1))





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

 .. code-block:: none

    7 interfaces for 6 layers
    [  0.   -3.   -6.5 -10.5 -15.1 -20.4 -26.4]
    [ 1.5  4.8  8.5 12.8 17.7 23.4]




.. GENERATED FROM PYTHON SOURCE LINES 63-65

The layers of this hybrid-coordinate model are thin and stretch with depth, and they follow
the changes of the mixed layer in time. Here are the interfaces at a given point.

.. GENERATED FROM PYTHON SOURCE LINES 65-75

.. code-block:: Python


    point = dict(x=6, y=5)
    zt = dz2z(dz.isel(**point))
    fig, ax = plt.subplots(figsize=(7, 3.2), constrained_layout=True)
    for k in range(zt.sizes["depth"]):
        ax.plot(zt.time, zt.isel(depth=k), color="0.2", lw=1)
    ax.fill_between(zt.time, zt.isel(depth=0), zt.isel(depth=1), color=cmocean.cm.deep(0.3), alpha=0.5)
    ax.set_ylabel("Interface height [m]")
    ax.set_title("Hycom layers at a point")




.. image-sg:: /tutorials/vertical/images/sphx_glr_plot_layer_thicknesses_001.png
   :alt: Hycom layers at a point
   :srcset: /tutorials/vertical/images/sphx_glr_plot_layer_thicknesses_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    Text(0.5, 1.0, 'Hycom layers at a point')



.. GENERATED FROM PYTHON SOURCE LINES 76-83

From depths to thicknesses
--------------------------

:func:`xoa.grid.z2dz` does the opposite, from the depths of the layer centers. The interfaces
are the middle of consecutive centers, and the outer ones are extrapolated, unless the
top and the bottom are provided. The result is exact for evenly spaced levels, and
approximate for others, like here where the layers thicken with depth.

.. GENERATED FROM PYTHON SOURCE LINES 83-89

.. code-block:: Python


    centers = dz2z(dz.isel(time=0), centered=True)
    dz_back = z2dz(centers, top=0.0)
    rel_err = abs(dz_back - dz.isel(time=0)) / dz.isel(time=0)
    print(f"Relative error: {float(rel_err.max()):.1%} at most")





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

 .. code-block:: none

    Relative error: 6.5% at most




.. GENERATED FROM PYTHON SOURCE LINES 90-97

With sigma levels
-----------------

On the sigma levels of CROCO, the depth of each level is given at each horizontal point
and the thicknesses are not stored. :func:`xoa.grid.get_dz` searches for a thickness
variable, and else computes it from the depths with :func:`~xoa.grid.z2dz`, whose
``top`` is the sea surface height.

.. GENERATED FROM PYTHON SOURCE LINES 97-105

.. code-block:: Python


    with xmeta.set_meta_specs("croco"):
        croco = xoa.open_data_sample("MODELS/CROCO/MED/pelops_3d.nc").xoa.decode()
    croco.depth.attrs["positive"] = "up"  # The file does not say that the depths are negative
    dzc = z2dz(croco.depth, top=croco.ssh)
    print(dzc.dims)
    print(f"Layer thicknesses from {float(dzc.min()):.1f} to {float(dzc.max()):.0f} m")





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

 .. code-block:: none

    ('sig_rho', 'y_rho', 'x_rho')
    Layer thicknesses from 0.2 to 604 m




.. GENERATED FROM PYTHON SOURCE LINES 106-109

Thicknesses are the weights of vertical integrals. The depth-averaged currents are very
different from the surface ones: the former are the barotropic component, and
the water columns are very different in depth over the steep topography.

.. GENERATED FROM PYTHON SOURCE LINES 109-128

.. code-block:: Python


    u, v = croco.u, croco.v
    ubar = (u * dzc).sum("sig_rho") / dzc.sum("sig_rho")
    vbar = (v * dzc).sum("sig_rho") / dzc.sum("sig_rho")
    speed_bar = np.hypot(ubar, vbar).rename("speed_bar")
    speed_bar.attrs.update(long_name="Depth-averaged speed", units="m s-1")
    speed_surf = np.hypot(
        u.isel(sig_rho=-1), v.isel(sig_rho=-1)
    )  # The last level is the surface.rename("speed_surf")
    speed_surf.attrs.update(long_name="Surface speed", units="m s-1")
    depth_bottom = dzc.sum("sig_rho").rename("column")
    depth_bottom.attrs.update(long_name="Water column thickness", units="m")
    plot_fields(
        {"Surface": speed_surf, "Depth average": speed_bar, "Water column": depth_bottom},
        ncols=3,
        figsize=(10, 3.4),
        cmap=cmocean.cm.speed,
        map_kw={"land": True, "gridlines_labels_on": ["bottom", "left"]},
    )



.. image-sg:: /tutorials/vertical/images/sphx_glr_plot_layer_thicknesses_002.png
   :alt: Surface, Depth average, Water column
   :srcset: /tutorials/vertical/images/sphx_glr_plot_layer_thicknesses_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (<Figure size 1000x340 with 6 Axes>, [<GeoAxes: title={'center': 'Surface'}>, <GeoAxes: title={'center': 'Depth average'}>, <GeoAxes: title={'center': 'Water column'}>])




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

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


.. _sphx_glr_download_tutorials_vertical_plot_layer_thicknesses.py:

.. only:: html

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

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/shom-fr/xoa/master?urlpath=lab/tree/notebooks/tutorials/vertical/plot_layer_thicknesses.ipynb
        :alt: Launch binder
        :width: 150 px

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

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

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

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

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

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


.. only:: html

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

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