{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Combining readers using operators\n\nIt is possible to combine readers using operators, to create e.g. a mean reader from different sources, or adding a constant force term to another sources.\n\n.. seealso::\n\n :py:mod:`opendrift.readers.operators`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import timedelta\nfrom opendrift.readers import reader_netCDF_CF_generic, reader_constant\nfrom opendrift.models.leeway import Leeway\n\nlw = Leeway(loglevel=20) # Set loglevel to 0 for debug information\n\n# Atmospheric model for wind\n#reader_arome = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')\nreader_arome = reader_netCDF_CF_generic.Reader(lw.test_data_folder() +\n '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc')\n\n# Ocean model for current\n#reader_norkyst = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')\nreader_norkyst = reader_netCDF_CF_generic.Reader(lw.test_data_folder() +\n '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First a regular simulation, without the added wind:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lw.add_reader([reader_arome, reader_norkyst])\nobject_type = 26 # 26 = Life-raft, no ballast\nlw.seed_elements(lon=4.5, lat=59.6, radius=100, number=1000,\n time=reader_arome.start_time, object_type=object_type)\nlw.run(duration=timedelta(hours=48), time_step=900, time_step_output=3600)\nlw.plot(fast=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then a simulation where we add a constant x_wind component to cause stranding.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader_onshore = reader_constant.Reader({ 'x_wind' : 10., 'y_wind': 0 })" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we just add all readers OpenDrift will read the wind from the first available reader, so we combine the wind reader and add them in order so\nthat the combined reader is used first, then the actual sources are used next.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r0 = reader_onshore + reader_arome\nprint(r0)\n\nlw2 = Leeway(loglevel=20) # Set loglevel to 0 for debug information\nlw2.add_reader([r0, reader_arome, reader_norkyst])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Seed leeway elements at defined position and time\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "object_type = 26 # 26 = Life-raft, no ballast\nlw2.seed_elements(lon=4.5, lat=59.6, radius=100, number=1000,\n time=reader_arome.start_time, object_type=object_type)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running model\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lw2.run(duration=timedelta(hours=48), time_step=900, time_step_output=3600)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print and plot results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(lw2)\nlw2.plot(fast=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first simulation compared to the second with extra wind:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lw.plot(fast=True, compare=lw2)" ] } ], "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 }