Vertical modes of the Gulf Stream#

In this tutorial, we show how to:

  • compute the stratification from temperature and salinity,

  • compute the vertical modes of each water column and the associated deformation radii,

  • split the currents into the amplitudes of the modes, and put them back together,

  • see how the kinetic energy is shared between the barotropic and baroclinic modes,

  • focus on the upper ocean with a vertical zoom.

The data are 3D fields of the GLORYS12 reanalysis of the Copernicus Marine Service, averaged over 2 x 2 grid cells, on the 1st of October 2019, in the region of the Gulf Stream.

The concepts are explained in the Dynamics diagnostics and polygon masks guide. See also: Dimensionless numbers of the Gulf Stream, Rossby number on the sigma levels of a CROCO simulation.

Initialisations#

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

import xoa
import xoa.dyn as xdyn
import xoa.meta as xmeta
import xoa.thermdyn as xthermdyn
from xoa.plot import plot_fields

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

Register the internal Mercator naming specifications, which are used automatically for the files of the Mercator and GLORYS products.

Read the reanalysis#

The sample goes down to 1942 m, whereas the ocean is deeper than that in the middle of the domain: the modes that we compute are the ones of the upper 1942 m, and the water columns that are shallower have less levels.

ds = xoa.open_data_sample("MODELS/CMEMS-GLORYS/glorys_gulfstream_3d.nc")
ds = ds.xoa.decode().isel(time=0)
temp, sal, u, v = ds.temp, ds.sal, ds.u, ds.v
print(ds)
<xarray.Dataset> Size: 4MB
Dimensions:  (depth: 40, lat: 48, lon: 60)
Coordinates:
  * depth    (depth) float32 160B 0.494 1.541 2.646 ... 1.684e+03 1.942e+03
  * lat      (lat) float32 192B 34.04 34.21 34.38 34.54 ... 41.54 41.71 41.88
  * lon      (lon) float32 240B -71.96 -71.79 -71.62 ... -62.46 -62.29 -62.12
    time     datetime64[ns] 8B 2019-10-01
Data variables:
    temp     (depth, lat, lon) float64 922kB ...
    sal      (depth, lat, lon) float64 922kB ...
    u        (depth, lat, lon) float64 922kB ...
    v        (depth, lat, lon) float64 922kB ...
    ssh      (lat, lon) float64 23kB ...
Attributes:
    institution:               MERCATOR OCEAN
    history:                   2023/06/01 16:20:05 MERCATOR OCEAN Netcdf crea...
    source:                    MERCATOR GLORYS12V1
    references:                http://www.mercator-ocean.fr
    Conventions:               CF-1.4
    comment:                   CMEMS product
    title:                     daily mean fields from Global Ocean Physics An...
    copernicusmarine_version:  2.2.1

Stratification#

The vertical modes are the solutions of a problem that depends only on the stratification of each water column, which is the squared Brunt-Vaisala frequency \(N^2\). xoa.thermdyn.get_n2() computes it from the potential temperature and the salinity with the gsw package, at the levels.

<xarray.DataArray 'n2' (depth: 40, lat: 48, lon: 60)> Size: 922kB
array([[[ 1.62567475e-08,  1.54351425e-08,  1.46134595e-08, ...,
         -8.88671270e-09, -8.63580205e-09, -8.38725669e-09],
        [ 1.66656716e-08,  1.58137023e-08,  1.49557686e-08, ...,
         -9.32183662e-09, -9.07083276e-09, -8.81675708e-09],
        [ 1.70760149e-08,  1.61871406e-08,  1.52890795e-08, ...,
         -9.74699326e-09, -9.50054958e-09, -9.25021709e-09],
        ...,
        [            nan,             nan,             nan, ...,
         -1.04071898e-08, -9.50091457e-09, -8.56550740e-09],
        [            nan,             nan,             nan, ...,
          3.17600177e-06,  1.65057908e-06, -8.06224638e-09],
        [            nan,             nan,             nan, ...,
          8.99505082e-06,  3.50238463e-08,  3.15989142e-06]],

       [[ 1.62566834e-08,  1.54350815e-08,  1.46134019e-08, ...,
         -8.88667716e-09, -8.63576757e-09, -8.38722316e-09],
        [ 1.66656057e-08,  1.58136397e-08,  1.49557094e-08, ...,
         -9.32179936e-09, -9.07079651e-09, -8.81672182e-09],
        [ 1.70759471e-08,  1.61870764e-08,  1.52890188e-08, ...,
         -9.74695430e-09, -9.50051159e-09, -9.25018005e-09],
...
          1.12290127e-06,  9.77041357e-07,  9.59296470e-07],
        [            nan,             nan,             nan, ...,
          1.23475748e-06,  1.16413008e-06,  1.07772674e-06],
        [            nan,             nan,             nan, ...,
          1.17645557e-06,  1.19136215e-06,  1.21335306e-06]],

       [[ 1.78825149e-06,  1.90236655e-06,  1.93172278e-06, ...,
          1.49034390e-06,  1.43495091e-06,  1.51639695e-06],
        [ 2.07637183e-06,  2.14872266e-06,  2.10197022e-06, ...,
          1.40496194e-06,  1.42488603e-06,  1.39616102e-06],
        [ 1.99138408e-06,  1.84932397e-06,  1.73000961e-06, ...,
          1.14681072e-06,  1.18961557e-06,  1.35055763e-06],
        ...,
        [            nan,             nan,             nan, ...,
          9.50052624e-07,  8.15673319e-07,  8.99586783e-07],
        [            nan,             nan,             nan, ...,
          1.30971158e-06,  1.09348247e-06,  8.97169280e-07],
        [            nan,             nan,             nan, ...,
          9.38584450e-07,  8.91186149e-07,  8.13446189e-07]]],
      shape=(40, 48, 60))
Coordinates:
  * depth    (depth) float32 160B 0.494 1.541 2.646 ... 1.684e+03 1.942e+03
  * lat      (lat) float32 192B 34.04 34.21 34.38 34.54 ... 41.54 41.71 41.88
  * lon      (lon) float32 240B -71.96 -71.79 -71.62 ... -62.46 -62.29 -62.12
    time     datetime64[ns] 8B 2019-10-01
Attributes:
    long_name:  Squared Brunt-Vaisala frequency
    units:      s-2

The vertical modes#

The xoa.dyn.VerticalModes class computes the modes of all the water columns at once, with numba. The first mode is the barotropic one, which is constant along the vertical, and the next ones are the baroclinic modes, that have more and more nodes. With the default rigid lid, the barotropic mode has no speed. The deformation radii are computed since the latitudes are found in the coordinates.

vm = xdyn.VerticalModes(n2, nmodes=5)
print(vm)
print(vm.c.dims, vm.rd.dims)
<VerticalModes: 5 modes, bc='rigid', zdim='depth'>
('mode', 'lat', 'lon') ('mode', 'lat', 'lon')

Let’s look at the first modes in a water column of the open ocean, south of the stream, with the stratification that makes them.

LAT, LON = 35.0, -66.0
point = {"lat": LAT, "lon": LON, "method": "nearest"}
depth = ds.depth

fig, axes = plt.subplots(1, 2, figsize=(8, 5.5), sharey=True, constrained_layout=True)
axes[0].plot(n2.sel(**point).clip(min=1e-7), depth, color="0.2", lw=1.8)
axes[0].set_xscale("log")
axes[0].set_xlim(1e-7, 1e-3)
axes[0].set_xlabel("Squared Brunt-Vaisala frequency [s-2]")
axes[0].set_ylabel("Depth [m]")
axes[0].set_title("Stratification")
colors = cmocean.cm.deep(np.linspace(0.15, 0.85, 4))
for mode, color in zip(range(1, 5), colors):
    modes = vm.modes.sel(**point).isel(mode=mode)
    rd = float(vm.rd.sel(**point).isel(mode=mode)) * 1e-3
    axes[1].plot(modes, depth, color=color, lw=1.8, label=f"Mode {mode}: {rd:.0f} km")
axes[1].plot(vm.modes.sel(**point).isel(mode=0), depth, color="0.6", ls="--", label="Mode 0")
axes[1].axvline(0, color="0.8", lw=0.8, zorder=0)
axes[1].set_xlabel("Vertical structure [1]")
axes[1].set_title("Modes and deformation radii")
axes[1].legend(frameon=False, loc="lower right")
axes[0].invert_yaxis()
Stratification, Modes and deformation radii

The mixed layer, where the stratification vanishes, is not shown on the plot. The modes are normalized so that the mean of their squares is one, and they are positive at the surface. The first baroclinic mode has a single node, close to the bottom of the thermocline, and has its largest amplitude at the surface.

Deformation radii#

The deformation radius of a mode is its speed divided by the Coriolis parameter: it is the scale at which the rotation matters. That of the first baroclinic mode is the one of the mesoscale eddies. Here it is mapped for the water columns that have all the levels, i.e. outside of the shelf.

open_ocean = n2.notnull().all("depth")
rd1 = (vm.rd.isel(mode=1) * 1e-3).where(open_ocean).rename("rd1")
rd1.attrs = {"long_name": "First deformation radius", "units": "km"}
print(f"Median first radius: {float(rd1.median()):.1f} km")

MAP_KW = {"gridlines_labels_on": ["bottom", "left"], "land": True}
speed0 = np.hypot(u, v).isel(depth=0)
fig, axes = plot_fields(
    {"First baroclinic deformation radius": rd1},
    figsize=(7.5, 5.8),
    method="contourf",
    cmap="cmo.matter",
    levels=np.linspace(18, 34, 17),
    extend="both",
    map_kw=MAP_KW,
    overlay_contours=[
        {"field": speed0, "levels": [0.8, 1.2], "colors": ["0.35", "k"], "linewidths": 0.8}
    ],
)
First baroclinic deformation radius
Median first radius: 27.7 km

The radius decreases northward with the Coriolis parameter and the warm waters south of the stream, whose stratification is strong, have larger radii than the colder waters on the shelf side. The contours show the surface currents of 0.8 and 1.2 m/s, which is the stream.

A quick estimate#

When only the first radius is needed, xoa.dyn.get_rossby_radius() is much cheaper: it integrates the buoyancy frequency along the water column, \(R_d = \int N dz / (\pi |f|)\) (WKB approximation). Let’s compare it with the exact radius.

rd1_wkb = (xdyn.get_rossby_radius(n2) * 1e-3).where(open_ocean)
diff = ((rd1_wkb / rd1 - 1) * 100).rename("diff")
diff.attrs = {"long_name": "WKB relative difference", "units": "%"}
print(
    f"WKB / modes - 1: median = {float(diff.median()):.1f} %, "
    f"5-95% = {float(diff.quantile(0.05)):.1f} to {float(diff.quantile(0.95)):.1f} %"
)

contours = [{"field": speed0, "levels": [0.8, 1.2], "colors": ["0.35", "k"], "linewidths": 0.8}]
rd_kw = {"cmap": "cmo.matter", "levels": np.linspace(18, 34, 17), "extend": "both"}
diff_kw = {"cmap": "cmo.balance", "levels": np.linspace(-20, 20, 21), "extend": "both"}
fig, axes = plot_fields(
    {"Vertical modes": (rd1, rd_kw), "WKB": (rd1_wkb, rd_kw), "WKB - modes": (diff, diff_kw)},
    ncols=3,
    figsize=(11, 3.8),
    method="contourf",
    map_kw=MAP_KW,
    overlay_contours=contours,
    panel_labels=True,
)
Vertical modes, WKB, WKB - modes
WKB / modes - 1: median = -9.4 %, 5-95% = -14.9 to 4.0 %

The maps are similar. The WKB radius is about 10 % smaller in the south, with a difference that shrinks northward, and it does not depend on the stream. Use VerticalModes for the exact radii or for the other modes.

The integral can be limited to a layer with zrange, in z positive up, like for the modes.

Decompose the currents#

xoa.dyn.VerticalModes.decomp() projects a field on the modes, and returns the amplitudes with the mode dimension first. The amplitude of the barotropic mode is the vertical mean of the currents. It is computed for all the water columns at once, with xarray.apply_ufunc() so that extra dimensions like time and dask arrays work too.

('mode', 'lat', 'lon') (5, 48, 60)

By orthogonality, the mean kinetic energy over the water column is the sum of the squared amplitudes. xoa.dyn.VerticalModes.energy_share() gives the share of each mode, that is averaged here for the open ocean.

share = vm.energy_share(u, v).where(open_ocean).mean(("lat", "lon"))
fig, ax = plt.subplots(figsize=(5, 4.6), constrained_layout=True)
ax.bar(share.mode, share * 100, color=cmocean.cm.deep(np.linspace(0.15, 0.85, 5)))
for k, value in enumerate(share.values):
    ax.text(k, value * 100 + 1.5, f"{value * 100:.0f}%", ha="center", fontsize=8)
ax.set_xlabel("Mode")
ax.set_ylabel("Share of the kinetic energy [%]")
ax.set_title("Kinetic energy")
ax.set_ylim(0, 80)
Kinetic energy
(0.0, 80.0)

The speed of the barotropic mode and of the first baroclinic mode are mapped.

amps = {}
for k, title, vmax in (0, "Barotropic mode", 1.2), (1, "Baroclinic mode 1", 0.8):
    amp = np.hypot(au.isel(mode=k), av.isel(mode=k)).where(open_ocean).rename("amp")
    amp.attrs = {"long_name": f"Speed of mode {k}", "units": "m s-1"}
    amps[title] = (amp, {"levels": np.linspace(0, vmax, 17)})
fig, axes = plot_fields(
    list(amps.values()),
    titles=list(amps),
    figsize=(9, 4),
    method="contourf",
    cmap="cmo.tempo",
    extend="max",
    map_kw=MAP_KW,
)
Barotropic mode, Baroclinic mode 1

The currents of the stream are mostly barotropic and first baroclinic: the first two modes carry most of the kinetic energy, as it is generally observed in the ocean. Note that the barotropic mode is here the vertical mean over the upper 1942 m only.

Recompose the currents#

xoa.dyn.VerticalModes.recomp() puts the amplitudes back together. Using only the first modes gives a smooth version of the profiles, and all the modes give the exact profile. Let’s see it in a column of the stream, where the currents are strong and sheared.

STREAM = {"lat": 37.5, "lon": -66.0, "method": "nearest"}
fig, axes = plt.subplots(1, 2, figsize=(8, 5.2), constrained_layout=True)
axes[0].plot(u.sel(**STREAM), depth, color="k", lw=2.2, label="Reanalysis")
colors = cmocean.cm.matter(np.linspace(0.2, 0.85, 3))
for n, color in zip((1, 2, 3), colors):
    axes[0].plot(
        vm.recomp(au, modes=n).sel(**STREAM),
        depth,
        color=color,
        lw=1.6,
        label=f"{n} mode" + ("s" if n > 1 else ""),
    )
axes[0].invert_yaxis()
axes[0].set_xlabel("Zonal current [m s⁻¹]")
axes[0].set_ylabel("Depth [m]")
axes[0].set_title("Profile in the stream")
axes[0].legend(frameon=False)

# Error over the domain, as a function of the number of modes
vm_all = xdyn.VerticalModes(n2, nmodes=n2.sizes["depth"])
errors = vm_all.truncation_error(u, [1, 2, 3, 5, 8, 12, 20])
axes[1].plot(errors.nmodes, errors, "o-", color="0.2")
axes[1].set_xscale("log")
axes[1].set_yscale("log")
axes[1].set_xlabel("Number of modes")
axes[1].set_ylabel("Root mean square error [m s⁻¹]")
axes[1].set_title("Truncation error")
axes[1].grid(True, which="both", alpha=0.3)
Profile in the stream, Truncation error

The error decreases quickly with the number of modes: three of them are enough for an error of a few centimeters per second, which is why the modal decomposition is a compact description of the vertical structure of the currents.

Focus on the upper ocean#

The zrange parameter restricts the computation to a layer, given as z positive up, i.e. negative in the ocean: only the levels that are in it are used, and the modes have no flux through its limits. The modes of the upper 500 m describe the thermocline, and their radii are smaller since they are faster to oscillate.

zoom = xdyn.VerticalModes(n2, nmodes=5, zrange=(-500, 0))
rd1z = (zoom.rd.isel(mode=1) * 1e-3).where(open_ocean)
print(
    f"Median first radius: {float(rd1.median()):.1f} km for the water column, "
    f"{float(rd1z.median()):.1f} km for the upper 500 m"
)

fig, axes = plt.subplots(1, 2, figsize=(7.5, 5), constrained_layout=True)
for ax, obj, title, ylim in zip(
    axes, (vm, zoom), ("Whole water column", "Upper 500 m"), ((2000, 0), (500, 0))
):
    for mode, color in zip(range(1, 4), colors):
        ax.plot(
            obj.modes.sel(**point).isel(mode=mode), depth, color=color, lw=1.8, label=f"Mode {mode}"
        )
    ax.axvline(0, color="0.8", lw=0.8, zorder=0)
    ax.set_title(title)
    ax.set_xlabel("Vertical structure [1]")
    ax.set_ylim(*ylim)
axes[0].set_ylabel("Depth [m]")
axes[0].legend(frameon=False, loc="lower left")
Whole water column, Upper 500 m
Median first radius: 27.7 km for the water column, 13.0 km for the upper 500 m

<matplotlib.legend.Legend object at 0x738bf97738c0>

The decomposition and the recomposition then only apply to the layer, and are missing outside of it, as the modes are. The amplitudes of the zoom therefore describe the upper 500 m only, like the amplitude of its barotropic mode, which is their vertical mean.

au_zoom = zoom.decomp(u.where(depth <= 500))
rec = zoom.recomp(au_zoom, modes=3)
upper = depth <= 500
error = float(np.sqrt(((rec - u) ** 2).where(upper).mean()))
print(f"Error of the first 3 modes of the upper 500 m: {error:.3f} m/s")
Error of the first 3 modes of the upper 500 m: 0.022 m/s

With a free surface, the barotropic mode also gets a speed, that is close to \(\sqrt{g H}\): VerticalModes(n2, bc="free").

free = xdyn.VerticalModes(n2, nmodes=2, bc="free")
print(
    f"Barotropic speed: {float(free.c.sel(**point).isel(mode=0)):.0f} m/s for a depth of "
    f"{float(free.dz.sel(**point).sum()):.0f} m"
)
plt.show()
Barotropic speed: 143 m/s for a depth of 2071 m

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

Gallery generated by Sphinx-Gallery