Skip to content

Instantly share code, notes, and snippets.

@gmaze
Last active June 13, 2023 15:31
Show Gist options
  • Save gmaze/f5a15bfc62bf092f3cfa15535c00aa18 to your computer and use it in GitHub Desktop.
Save gmaze/f5a15bfc62bf092f3cfa15535c00aa18 to your computer and use it in GitHub Desktop.
How to retrieve CMEMS product using xarray and opendap
# Must be installed in python env:
# - motuclient (via pip, https://github.com/clstoulouse/motu-client-python)
# - pydap (via conda, https://github.com/pydap/pydap)
import os
import requests
import xarray as xr
# Set-up a request session that will know how to authenticate on the CMEMS server
MOTU_USERNAME, MOTU_PASSWORD = (
os.getenv("MOTU_USERNAME"),
os.getenv("MOTU_PASSWORD"),
)
if not MOTU_USERNAME:
raise ValueError("No MOTU_USERNAME in environment ! ")
session = requests.Session()
session.auth = (MOTU_USERNAME, MOTU_PASSWORD)
# We will retrieve the CMEMS product:
# https://resources.marine.copernicus.eu/product-detail/GLOBAL_ANALYSISFORECAST_PHY_001_024
# Define the real time data server:
opendap_nrt_server = 'https://nrt.cmems-du.eu/thredds/dodsC'
# point toward daily velocity fields:
serverset = opendap_nrt_server + '/cmems_mod_glo_phy-cur_anfc_0.083deg_P1D-m'
# Then open the dataset lazily:
store = xr.backends.PydapDataStore.open(serverset, session=session)
ds = xr.open_dataset(store)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment