{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# CMEMS current components\n\nThe global CMEMS/copernicus ocean model contains several different surface current fields.\nOpenDrift 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.\nNote that \"east/north\" counterparts will also be detected, and eventual rotation will be performed automatically.\n\nThis example illustrates how a \"standard_name_mapping\" can be added to the generic netCDF reader to chose alternative variables.\nThe example also illustrates the alternative (experimental) mechanism of summing two readers.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime, timedelta\nimport cf_xarray\nimport copernicusmarine\nfrom opendrift.models.oceandrift import OceanDrift\nfrom opendrift.readers.reader_netCDF_CF_generic import Reader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get an Xarray dataset from copernicusmarine client\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ds = copernicusmarine.open_dataset(dataset_id='cmems_mod_glo_phy_anfc_merged-uv_PT1H-i')\nprint(ds) # Default Xarray output\nprint(ds.cf) # Output from cf-xarray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an OpenDrift reader from this dataset\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader_default = Reader(ds, name='CMEMS default')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mapping other variables to required standard_name's\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader_tides = Reader(ds, standard_name_mapping={\n 'utide': 'x_sea_water_velocity',\n 'vtide': 'y_sea_water_velocity',\n }, name='Tides only')\nreader_stokes = Reader(ds, standard_name_mapping={\n 'vsdx': 'x_sea_water_velocity',\n 'vsdy': 'y_sea_water_velocity',\n }, name='Stokes only')\nreader_total = Reader(ds, standard_name_mapping={\n 'utotal': 'x_sea_water_velocity',\n 'vtotal': 'y_sea_water_velocity',\n }, name='Total current')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run and compare simulations using the different current components\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cases = {'Eulerian current': reader_default,\n 'Tides only': reader_tides,\n 'Stokes drift only': reader_stokes,\n 'Total current': reader_total,\n 'SUM: Eulerian + Tides': reader_default + reader_tides, # Experimental feature\n 'SUM: Eulerian + Stokes': reader_default + reader_stokes, # Experimental feature\n 'SUM: Eulerian + Tides + Stokes': reader_default + reader_tides + reader_stokes # Experimental feature\n }\n\nsimulations = []\nfor cname, reader in cases.items():\n o = OceanDrift()\n o.add_reader(reader, variables=['x_sea_water_velocity', 'y_sea_water_velocity'])\n o.seed_elements(lon=4.8, lat=60, time=datetime(2024, 10, 31, 6))\n o.run(duration=timedelta(hours=12))\n simulations.append(o)\n\nsimulations[0].plot(filename='cmems_comparison.png', buffer=.05,\n compare=simulations[1:], legend=list(cases))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 0 }