Quick start guide#
Like most xarray-based projects, xoa provides not only a library but also accessors for convenient access to part of the functionality. Both uses are demonstrated in the Tutorials, which are organized by theme: naming and configuration, graphics, grids and regridding, ocean dynamics, and comparison with observations and statistics. Some of the modules that are special or of particular interest are described in the In-depth guides section.
A tour of xoa with the Gulf Stream#
This tour shows with a single dataset how to open a model output,
plot it, compute dynamical diagnostics, regrid and interpolate.
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.
Most of the features are available as methods of the xoa accessor of xarray objects.
In [1]: import numpy as np
In [2]: import xarray as xr
In [3]: import matplotlib.pyplot as plt
In [4]: import cmocean
In [5]: import xoa
In [6]: import xoa.meta as xmeta
In [7]: from xoa.plot import plot_fields, plot_section
In [8]: map_kw = {"gridlines_labels_on": ["bottom", "left"], "land": True}
Open and decode#
The names are specific to the product: thetao for the temperature, uo for the zonal
current… xoa knows them from the naming specifications, which are found from the
attributes of the file, or set by the user like here for the Mercator products.
decode renames the arrays to generic names and completes
their attributes, so that all the models are accessed in the same way.
See Find variables whatever their names.
In [9]: with xmeta.set_meta_specs("mercator"):
...: ds = xoa.open_data_sample("MODELS/CMEMS-GLORYS/glorys_gulfstream_3d.nc")
...: print(list(ds.data_vars))
...: ds = ds.xoa.decode().isel(time=0)
...: print(list(ds.data_vars))
...:
['thetao', 'so', 'uo', 'vo', 'zos']
['temp', 'sal', 'u', 'v', 'ssh']
Even without decoding, a quantity is found from its generic name.
In [10]: print(ds.xoa.get("lon").name, ds.xoa.get("depth").name, ds.xoa.u.attrs["standard_name"])
lon depth eastward_sea_water_velocity
Plot#
xoa.plot.plot_fields() draws several fields on maps of a single figure. The extent,
the labels and the colorbars come from the data. Plots are also available from the
accessor, like temp.xoa.plot.field().
See Plot fields and currents on maps.
In [11]: surf = ds.isel(depth=0)
In [12]: plot_fields(
....: {
....: "Temperature": (surf.temp, {"cmap": cmocean.cm.thermal}),
....: "Salinity": (surf.sal, {"cmap": cmocean.cm.haline}),
....: "Sea surface height": surf.ssh,
....: },
....: ncols=3,
....: figsize=(10, 3.4),
....: map_kw=map_kw,
....: );
....:
A vertical section along a latitude is drawn with the depths that come with the dataset.
In [13]: section = ds.temp.sel(lat=37.5, method="nearest")
In [14]: fig, ax = plt.subplots(figsize=(7, 3.2), constrained_layout=True)
In [15]: plot_section(section, ax=ax, method="contourf", cmap=cmocean.cm.thermal, levels=np.arange(2, 31, 2))
Out[15]: <matplotlib.contour.QuadContourSet at 0x738be678ce10>
In [16]: ax.set_ylim(1000, 0) # depths are positive down
Out[16]: (1000.0, 0.0)
Diagnostics#
The xoa accessor of a dataset computes the diagnostics from its currents, found by their
generic names, and the grid resolution from its coordinates:
the relative vorticity, the Rossby number, the divergence, the strain
and the Okubo-Weiss parameter.
See Vorticity, strain and eddies in Gulf Stream currents.
In [17]: rossby = surf.xoa.get_rossby()
In [18]: okubo = surf.xoa.get_okuboweiss(normalize=True)
The sea surface height gives the geostrophic currents. Here is the speed error with respect to the currents of the reanalysis.
In [19]: ug, vg = surf.ssh.xoa.get_geos()
In [20]: err = np.hypot(ug, vg) - np.hypot(surf.u, surf.v)
In [21]: err.attrs.update(long_name="Geostrophic speed error", units="m s-1")
In [22]: plot_fields(
....: {"Rossby number": rossby, "Okubo-Weiss": okubo, "Geostrophic speed error": err},
....: ncols=3,
....: figsize=(10, 3.4),
....: method="contourf",
....: cmap=cmocean.cm.balance,
....: map_kw=map_kw,
....: levels=np.linspace(-1, 1, 21),
....: extend="both",
....: );
....:
Regrid and interpolate#
The same temperature is regridded to a regular grid that is twice coarser with the conservative method, and interpolated along a transect. See Regrid and interpolate horizontally.
In [23]: dst = xr.Dataset(
....: coords={
....: "lon": ("lon", np.arange(-72.0, -62.0, 0.5), {"standard_name": "longitude"}),
....: "lat": ("lat", np.arange(34.0, 42.0, 0.5), {"standard_name": "latitude"}),
....: }
....: )
....:
In [24]: coarse = surf.temp.xoa.regrid(dst, method="conservative")
In [25]: tlon = xr.DataArray(np.linspace(-71.0, -63.0, 60), dims="s")
In [26]: tlat = xr.DataArray(np.linspace(35.0, 41.0, 60), dims="s")
In [27]: transect = surf.temp.xoa.interp(tlon, tlat)
In [28]: print(coarse.shape, transect.shape)
(16, 20) (60,)
In [29]: fig, axes = plt.subplots(
....: 1, 2, figsize=(9, 3.2), gridspec_kw={"width_ratios": (1, 1.3)}, constrained_layout=True
....: )
....:
In [30]: coarse.plot(ax=axes[0], cmap=cmocean.cm.thermal);
In [31]: axes[0].plot(tlon, tlat, "k--");
In [32]: axes[0].set_title("Coarse grid and transect");
In [33]: axes[1].plot(np.hypot(tlon - tlon[0], tlat - tlat[0]) * 111.0, transect, "k");
In [34]: axes[1].set_xlabel("Distance along the transect [km]");
In [35]: axes[1].set_ylabel("Temperature [°C]");
In [36]: axes[1].set_title("Interpolation");
In [37]: axes[1].grid(alpha=0.3)
Go further#
Naming and configuration: Find variables whatever their names, Write your own naming specifications.
Grids, vertical coordinates, filtering, kriging: the “Grids” tutorials.
Dynamics, vertical modes, mixed layer, advection: the “Dynamics” tutorials.
Comparison with observations: the “Evaluation” tutorials.