From 5fb364ab8ac999e3daf4de14660869200519adaa Mon Sep 17 00:00:00 2001 From: Knut-Frode Dagestad Date: Thu, 31 Oct 2024 15:29:08 +0100 Subject: [PATCH 1/2] Made new example illustrating mapping of variables for CMEMS currents. Removed no_metadata_cache from copernicusmarine reader. Added cf-xarray as a new dependency. --- environment.yml | 1 + examples/example_long_cmems.py | 20 +++--- examples/example_long_cmems_currents.py | 64 ++++++++++++++++++++ examples/example_long_cmems_new.py | 45 -------------- opendrift/readers/reader_copernicusmarine.py | 17 +++--- 5 files changed, 82 insertions(+), 65 deletions(-) create mode 100755 examples/example_long_cmems_currents.py delete mode 100755 examples/example_long_cmems_new.py diff --git a/environment.yml b/environment.yml index 839a5676f..c294e60f1 100644 --- a/environment.yml +++ b/environment.yml @@ -13,6 +13,7 @@ dependencies: - xarray - dask - cfgrib + - cf-xarray - pygrib - xhistogram - requests diff --git a/examples/example_long_cmems.py b/examples/example_long_cmems.py index a9e8fb137..b91ef071c 100755 --- a/examples/example_long_cmems.py +++ b/examples/example_long_cmems.py @@ -1,13 +1,12 @@ #!/usr/bin/env python """ -CMEMS -============= - -This example runs an OceanDrift simulation -with current data from CMEMS -To run this example, you need a CMEMS account created at -https://marine.copernicus.eu -with username and password stored as environment variables ``COPERNICUSMARINE_SERVICE_USERNAME`` and ``COPERNICUSMARINE_SERVICE_PASSWORD`` +Copernicus Marine Client (CMEMS) +================================ + +This example runs an OceanDrift simulation with current data from CMEMS. +To run this example, you need a CMEMS account created at https://marine.copernicus.eu +with username and password stored as environment variables +``COPERNICUSMARINE_SERVICE_USERNAME`` and ``COPERNICUSMARINE_SERVICE_PASSWORD`` or in a ``.netrc`` file with contents:: machine copernicusmarine login password @@ -15,7 +14,7 @@ This file must be stored in your home folder (and unreadable by others) or in the main OpenDrift folder Alternatively, an Xarray dataset can be created explicitly with the copernicusmarine client, and provided to reader_netCDF_CF_generic: -https://opendrift.github.io/gallery/example_long_cmems_new.html +https://opendrift.github.io/gallery/example_long_cmems_currents.html """ from datetime import datetime, timedelta @@ -36,6 +35,3 @@ o.animation(fast=True, clabel='Ocean current [m/s]', background=['x_sea_water_velocity', 'y_sea_water_velocity']) - -#%% -# .. image:: /gallery/animations/example_cmems_0.gif diff --git a/examples/example_long_cmems_currents.py b/examples/example_long_cmems_currents.py new file mode 100755 index 000000000..8eacd1caf --- /dev/null +++ b/examples/example_long_cmems_currents.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +""" +CMEMS current components +======================== + +The global CMEMS/copernicus ocean model contains several different surface current fields. +OpenDrift will by default use the variables with "standard_name" attribute equal to what is requested from a module, typically "x_sea_water_velocity"/"y_sea_water_velocity" for currents. +Note that "east/north" counterparts will also be detected, and eventual rotation will be performed automatically. + +This example illustrates how a "standard_name_mapping" can be added to the generic netCDF reader to chose alternative variables. +The example also illustrates the alternative (experimental) mechanism of summing two readers. +""" + +from datetime import datetime, timedelta +import cf_xarray +import copernicusmarine +from opendrift.models.oceandrift import OceanDrift +from opendrift.readers.reader_netCDF_CF_generic import Reader + +#%% +# Get an Xarray dataset from copernicusmarine client +ds = copernicusmarine.open_dataset(dataset_id='cmems_mod_glo_phy_anfc_merged-uv_PT1H-i') +print(ds) # Default Xarray output +print(ds.cf) # Output from cf-xarray + +#%% +# Create an OpenDrift reader from this dataset +reader_default = Reader(ds, name='CMEMS default') + +#%% +# Mapping other variables to required standard_name's +reader_tides_only = Reader(ds, standard_name_mapping={ + 'utide': 'x_sea_water_velocity', + 'vtide': 'y_sea_water_velocity', + }, name='Tides only') +reader_stokes = Reader(ds, standard_name_mapping={ + 'vsdx': 'x_sea_water_velocity', + 'vsdy': 'y_sea_water_velocity', + }, name='Stokes only') +reader_total = Reader(ds, standard_name_mapping={ + 'utotal': 'x_sea_water_velocity', + 'vtotal': 'y_sea_water_velocity', + }, name='Total current') + +#%% +# Run and compare simulations using the different current components +cases = {'Eulerian current': reader_default, + 'Tides only': reader_tides_only, + 'Stokes drift only': reader_stokes, + 'Total current': reader_total, + 'SUM: Eulerian + Tides': reader_default + reader_tides_only, # Experimental feature + 'SUM: Eulerian + Stokes': reader_default + reader_stokes, # Experimental feature + } + +simulations = [] +for cname, reader in cases.items(): + o = OceanDrift() + o.add_reader(reader, variables=['x_sea_water_velocity', 'y_sea_water_velocity']) + o.seed_elements(lon=4.8, lat=60, time=datetime(2024, 10, 31, 6)) + o.run(duration=timedelta(hours=12)) + simulations.append(o) + +simulations[0].plot(filename='cmems_comparison.png', buffer=.05, + compare=simulations[1:], legend=list(cases)) diff --git a/examples/example_long_cmems_new.py b/examples/example_long_cmems_new.py deleted file mode 100755 index 73315c254..000000000 --- a/examples/example_long_cmems_new.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -""" -Copernicus marine client -======================== - -This example runs an OceanDrift simulation -with current data from CMEMS -To run this example, you need a CMEMS account created at -https://marine.copernicus.eu -and the copernicus_marine_client installed from -https://pypi.org/project/copernicusmarine -""" - -from datetime import datetime, timedelta -import copernicus_marine_client as copernicusmarine -from opendrift.readers.reader_netCDF_CF_generic import Reader -from opendrift.models.oceandrift import OceanDrift - -lon = 4.8; lat = 60 # Bergen, Norway -lon = -89; lat = 29.8 # New Orleans -lon = 107; lat = 10 # Ho Chi Minh -lon = 123; lat = -16.3 # Australia - - -o = OceanDrift() - -#%% -# First get a Xarray dataset from copernicus_marine_client -ds = copernicusmarine.open_dataset( - dataset_id='cmems_mod_glo_phy_anfc_merged-uv_PT1H-i', - username='', password='') - -#%% -# Then create an OpenDrift reader from this dataset -r = Reader(ds) -o.add_reader(r) - -o.seed_elements(lon=lon, lat=lat, number=5000, radius=1000, time=datetime.utcnow()) -o.run(duration=timedelta(days=3)) - -o.animation(fast=True, clabel='Ocean current [m/s]', - background=['x_sea_water_velocity', 'y_sea_water_velocity']) - -#%% -# .. image:: /gallery/animations/example_cmems_new_0.gif diff --git a/opendrift/readers/reader_copernicusmarine.py b/opendrift/readers/reader_copernicusmarine.py index 4e50d30ba..072c2930a 100644 --- a/opendrift/readers/reader_copernicusmarine.py +++ b/opendrift/readers/reader_copernicusmarine.py @@ -32,12 +32,13 @@ def __init__(self, dataset_id, username=None, password=None, cache_dir=None): if username is not None and password is not None: logger.info(f'Using CMEMS password for user {username} from provided variables') - if cache_dir is None: - cache_dir = os.getenv('COPERNICUSMARINE_CACHE_DIRECTORY') - if cache_dir == '': # Set env variable COPERNICUSMARINE_CACHE_DIRECTORY to "" to disable cahcing - no_metadata_cache=True - else: - no_metadata_cache=False + # no_metadata_cache will be disabled from version 2.0 + #if cache_dir is None: + # cache_dir = os.getenv('COPERNICUSMARINE_CACHE_DIRECTORY') + #if cache_dir == '': # Set env variable COPERNICUSMARINE_CACHE_DIRECTORY to "" to disable cahcing + # no_metadata_cache=True + #else: + # no_metadata_cache=False if username is None: username = os.getenv('COPERNICUSMARINE_SERVICE_USERNAME') password = os.getenv('COPERNICUSMARINE_SERVICE_PASSWORD') @@ -68,8 +69,8 @@ def __init__(self, dataset_id, username=None, password=None, cache_dir=None): if username is None: raise ValueError('To use CMEMS datasets, provide username and password, or store these in .netrc file in home folder or main opendrift folder with machine name "copernicusmarine". Alternatively, creadentials can be stored as environment variables COPERNICUSMARINE_SERVICE_USERNAME and COPERNICUSMARINE_SERVICE_PASSWORD.') - ds = copernicusmarine.open_dataset(dataset_id=dataset_id, username=username, password=password, - no_metadata_cache=no_metadata_cache) + ds = copernicusmarine.open_dataset(dataset_id=dataset_id, username=username, password=password) + ds['name'] = str(dataset_id) # Run constructor of parent Reader class From 379c453311ffa3d650c9bc024cf6bec968dbc7e1 Mon Sep 17 00:00:00 2001 From: Knut-Frode Dagestad Date: Thu, 31 Oct 2024 15:37:19 +0100 Subject: [PATCH 2/2] Fixed package name for cf-xarray in environment.yml --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index c294e60f1..0a4684562 100644 --- a/environment.yml +++ b/environment.yml @@ -13,7 +13,7 @@ dependencies: - xarray - dask - cfgrib - - cf-xarray + - cf_xarray - pygrib - xhistogram - requests