{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Customising Xarray Dataset\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime, timedelta\nimport xarray as xr\nfrom opendrift import test_data_folder as tdf\nfrom opendrift.readers import reader_netCDF_CF_generic\nfrom opendrift.models.oceandrift import OceanDrift\n\no = OceanDrift(loglevel=20) # Set loglevel to 0 for debug information" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Opening the currents netCDF file with xarray\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ds = xr.open_dataset(tdf + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creating and adding a landmask DataArray (variable) based on the u-current component\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ds['landmask'] = ds.u.isel(time=0).isel(depth=0) * 0 # 0 (ocean) where current is finite\nds['landmask'] = ds.landmask.fillna(1) # 1 (land) where current is NaN\nds['landmask'] = ds.landmask.assign_attrs(standard_name='land_binary_mask') # Adding attribute standard_name so that this variable is recognised as landmask" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creating an OpenDrift reader from this modified xarray dataset, and confirming that landmask is recognised\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r = reader_netCDF_CF_generic.Reader(ds)\nprint(r)\no.add_reader(r)\n\no.set_config('general:use_auto_landmask', False) # Disable the automatic GSHHG landmask, so that the custom landmask is used" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Seeding a particle and running simulation, and confirming that the custom landmask is used for stranding\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o.seed_elements(lon=4.9, lat=60.0, time=r.start_time)\no.run(end_time=r.end_time)\no.plot(fast=True)" ] } ], "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.14.2" } }, "nbformat": 4, "nbformat_minor": 0 }