Copernicus Global Ocean Model Example

This example demonstrates how to:

  • Build a request to the Copernicus Marine Service (CMEMS),

  • Download a subset of the global ocean physics model,

  • Visualise physical fields (salinity, temperature, SSH, currents),

  • Plot surface currents with and without vectors,

  • Save diagnostic plots automatically.

The example uses the visusat.copernicus.CopernicusRequest class and high-level plotting utilities.

Example script for downloading and plotting CMEMS global model data.
 1"""
 2Example — CMEMS Global Physical Model (ANFC) extraction & plotting.
 3
 4This example demonstrates:
 5    - how to request a single-depth subset of the 1/12° global model,
 6    - how to download and open the dataset using VisuSat,
 7    - how to plot scalar fields (SST, Salinity, SSH...),
 8    - how to plot ocean surface currents.
 9
10Logs are saved both to the terminal and to a .log file.
11"""
12
13import logging
14import os
15import sys
16from pathlib import Path
17
18from visusat import copernicus
19
20# ---------------------------------------------------------------------------
21# Logging configuration
22# ---------------------------------------------------------------------------
23LOG_FILE = Path(__file__).with_suffix(".log")
24script_name = os.path.basename(__file__)
25logging.basicConfig(
26    level=logging.INFO,
27    format="%(asctime)s [%(levelname)s] %(name)s.%(funcName)s:%(lineno)d - %(message)s",
28    handlers=[
29        logging.FileHandler(LOG_FILE, encoding="utf-8", mode="w"),
30        logging.StreamHandler(sys.stdout),
31    ],
32    force=True,
33)
34logger = logging.getLogger(Path(__file__).stem)
35logger.info(">>> Example script started.")
36
37# ---------------------------------------------------------------------------
38# Copernicus request — CMEMS GLOBAL 1/12° physical model (ANFC)
39# ---------------------------------------------------------------------------
40
41request = copernicus.CopernicusRequest(
42    dataset_id="cmems_mod_glo_phy_anfc_0.083deg_PT1H-m",
43    variables=["so", "thetao", "uo", "vo", "zos"],  # salinity, temp, u, v, SSH
44    minimum_longitude=-180,
45    maximum_longitude=179.91668701171875,
46    minimum_latitude=-80,
47    maximum_latitude=90,
48    start_datetime="2025-10-27T12:00:00",
49    end_datetime="2025-10-27T12:00:00",
50    minimum_depth=None,  # (surface layer by default)
51    maximum_depth=None,
52    output_filename="glo_anfc_surface_20251027.nc",
53)
54logger.info("Request initialised.")
55
56# ---------------------------------------------------------------------------
57# Download + Open dataset
58# ---------------------------------------------------------------------------
59ds = copernicus.load_dataset(request, force=False)
60logger.info(f"Dataset opened successfully:\n{ds}")
61
62# ---------------------------------------------------------------------------
63# Plot all scalar fields
64# ---------------------------------------------------------------------------
65logger.info("Plotting scalar variables (temperature, salinity, SSH)...")
66copernicus.plot_fields(request, ds)
67
68# ---------------------------------------------------------------------------
69# Plot currents
70# ---------------------------------------------------------------------------
71logger.info("Plotting global currents...")
72copernicus.plot_currents(request, ds, vectors=False)
73
74logger.info("Plotting currents over Mexican Golfe (with vectors)...")
75copernicus.plot_currents(request, ds, domain=[-100, -60, 0, 30], vectors=True)
76
77logger.info(">>> Example script completed successfully.")