{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Convolve input\n\nDecreasing the spatial resolution of fields from a reader by convolution.\nThis may improve accuracy, see: https://doi.org/10.1016/j.rse.2019.01.001\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime, timedelta\nimport matplotlib.pyplot as plt\nfrom opendrift.readers import reader_netCDF_CF_generic\nfrom opendrift.models.oceandrift import OceanDrift\n\n\nlon = 4.9; lat = 60.0\no = OceanDrift(loglevel=20)\n\nreader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')\ntime = reader_norkyst.start_time\n\no.add_reader([reader_norkyst])\no.seed_elements(lon, lat, radius=1000, number=1000, time=time)\no.run(steps=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Store final field of x-component of current\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "original_current = reader_norkyst.var_block_after[list(reader_norkyst.var_block_after.keys())[0]].data_dict['x_sea_water_velocity'].copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the second run, the NorKyst currents are convolved with a kernel,\neffectively lowering the spatial resolution.\n.set_convolution_kernel may also be given as an array (kernel) directly\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "N = 10 # Convolusion kernel size\nreader_norkyst.set_convolution_kernel(N) # Using convolution kernel for second run\no2 = OceanDrift(loglevel=20)\no2.add_reader([reader_norkyst])\no2.seed_elements(lon, lat, radius=1000, number=1000, time=time)\no2.run(steps=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Store final field of x-component of (convolved) current\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "convolved_current = reader_norkyst.var_block_after[\n \"['x_sea_water_velocity', 'y_sea_water_velocity']\"].data_dict['x_sea_water_velocity']\n\nplt.subplot(2,1,1)\nplt.imshow(original_current, interpolation='nearest')\nplt.title('Original current field (x-component)')\nclim = plt.gci().get_clim()\nplt.colorbar()\nplt.subplot(2,1,2)\nplt.imshow(convolved_current, interpolation='nearest')\nplt.clim(clim) # Make sure plots are comparable\nplt.colorbar()\nplt.title('Final, convolved current field (x-component)')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print and plot results, with convolved currents as background\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(o)\no.animation(compare=o2, fast=True, legend=[\n 'Original currents', 'Current convoled with kernel of size %s' % N],\n background=['x_sea_water_velocity', 'y_sea_water_velocity'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o.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.11.6" } }, "nbformat": 4, "nbformat_minor": 0 }