Convolve input

Decreasing the spatial resolution of fields from a reader by convolution. This may improve accuracy, see: https://doi.org/10.1016/j.rse.2019.01.001

from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.models.oceandrift import OceanDrift


lon = 4.9; lat = 60.0
o = OceanDrift(loglevel=20)

reader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')
time = reader_norkyst.start_time

o.add_reader([reader_norkyst])
o.seed_elements(lon, lat, radius=1000, number=1000, time=time)
o.run(steps=20)

Out:

13:51:12 INFO    opendrift.models.basemodel: OpenDriftSimulation initialised (version 1.7.1 / v1.7.1-87-g074c1d2)
13:51:12 INFO    opendrift.readers.reader_netCDF_CF_generic: Opening dataset: /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc
13:51:13 INFO    opendrift.models.basemodel: Fallback values will be used for the following variables which have no readers:
13:51:13 INFO    opendrift.models.basemodel:    x_wind: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    y_wind: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    upward_sea_water_velocity: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    ocean_vertical_diffusivity: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_surface_wave_significant_height: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    surface_downward_x_stress: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    surface_downward_y_stress: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    turbulent_kinetic_energy: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    turbulent_generic_length_scale: 0.000000
13:51:13 INFO    opendrift.models.basemodel:    sea_floor_depth_below_sea_level: 10000.000000
13:51:13 INFO    opendrift.models.basemodel: Adding a dynamical landmask with max. priority based on assumed maximum speed of 1 m/s. Adding a customised landmask may be faster...
13:51:15 INFO    opendrift.models.basemodel: Using existing reader for land_binary_mask
13:51:15 INFO    opendrift.models.basemodel: All points are in ocean
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated)
13:51:15 INFO    opendrift.models.basemodel: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated)
13:51:16 INFO    opendrift.models.basemodel: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated)
13:51:16 INFO    opendrift.models.basemodel: 2015-11-16 16:00:00 - step 17 of 20 - 1000 active elements (0 deactivated)
13:51:16 INFO    opendrift.models.basemodel: 2015-11-16 17:00:00 - step 18 of 20 - 1000 active elements (0 deactivated)
13:51:16 INFO    opendrift.models.basemodel: 2015-11-16 18:00:00 - step 19 of 20 - 1000 active elements (0 deactivated)
13:51:16 INFO    opendrift.models.basemodel: 2015-11-16 19:00:00 - step 20 of 20 - 1000 active elements (0 deactivated)

Store final field of x-component of current

original_current = reader_norkyst.var_block_after[list(reader_norkyst.var_block_after.keys())[0]].data_dict['x_sea_water_velocity'].copy()

For the second run, the NorKyst currents are convolved with a kernel, effectively lowering the spatial resolution. <reader>.set_convolution_kernel may also be given as an array (kernel) directly

N = 10  # Convolusion kernel size
reader_norkyst.set_convolution_kernel(N)  # Using convolution kernel for second run
o2 = OceanDrift(loglevel=20)
o2.add_reader([reader_norkyst])
o2.seed_elements(lon, lat, radius=1000, number=1000, time=time)
o2.run(steps=20)

Out:

13:51:16 INFO    opendrift.models.basemodel: OpenDriftSimulation initialised (version 1.7.1 / v1.7.1-87-g074c1d2)
13:51:16 INFO    opendrift.models.basemodel: Fallback values will be used for the following variables which have no readers:
13:51:16 INFO    opendrift.models.basemodel:    x_wind: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    y_wind: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    upward_sea_water_velocity: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    ocean_vertical_diffusivity: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_surface_wave_significant_height: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    surface_downward_x_stress: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    surface_downward_y_stress: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    turbulent_kinetic_energy: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    turbulent_generic_length_scale: 0.000000
13:51:16 INFO    opendrift.models.basemodel:    sea_floor_depth_below_sea_level: 10000.000000
13:51:16 INFO    opendrift.models.basemodel: Adding a dynamical landmask with max. priority based on assumed maximum speed of 1 m/s. Adding a customised landmask may be faster...
13:51:18 INFO    opendrift.models.basemodel: Using existing reader for land_binary_mask
13:51:18 INFO    opendrift.models.basemodel: All points are in ocean
13:51:18 INFO    opendrift.models.basemodel: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated)
13:51:18 WARNING opendrift.readers.basereader.structured: Data block from /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc not large enough to cover element positions within timestep. Buffer size (7) must be increased. See `Variables.set_buffer_size`.
13:51:18 INFO    opendrift.models.basemodel: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated)
13:51:18 INFO    opendrift.models.basemodel: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated)
13:51:18 INFO    opendrift.models.basemodel: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated)
13:51:18 INFO    opendrift.models.basemodel: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 16:00:00 - step 17 of 20 - 999 active elements (1 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 17:00:00 - step 18 of 20 - 999 active elements (1 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 18:00:00 - step 19 of 20 - 999 active elements (1 deactivated)
13:51:19 INFO    opendrift.models.basemodel: 2015-11-16 19:00:00 - step 20 of 20 - 999 active elements (1 deactivated)

Store final field of x-component of (convolved) current

convolved_current = reader_norkyst.var_block_after[
    "['x_sea_water_velocity', 'y_sea_water_velocity']"].data_dict['x_sea_water_velocity']

plt.subplot(2,1,1)
plt.imshow(original_current, interpolation='nearest')
plt.title('Original current field (x-component)')
clim = plt.gci().get_clim()
plt.colorbar()
plt.subplot(2,1,2)
plt.imshow(convolved_current, interpolation='nearest')
plt.clim(clim)  # Make sure plots are comparable
plt.colorbar()
plt.title('Final, convolved current field (x-component)')
plt.show()
Original current field (x-component), Final, convolved current field (x-component)

Print and plot results, with convolved currents as background

print(o)
o.animation(compare=o2, fast=True, legend=[
    'Original currents', 'Current convoled with kernel of size %s' % N],
    background=['x_sea_water_velocity', 'y_sea_water_velocity'])

Out:

===========================
--------------------
Reader performance:
--------------------
/root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc
 0:00:00.7  total
 0:00:00.0  preparing
 0:00:00.2  reading
 0:00:00.0  interpolation
 0:00:00.0  interpolation_time
 0:00:00.5  rotating vectors
 0:00:00.0  masking
--------------------
global_landmask
 0:00:00.1  total
 0:00:00.0  preparing
 0:00:00.1  reading
 0:00:00.0  masking
--------------------
Performance:
    3.5 total time
    0.3 configuration
    2.4 preparing main loop
      2.4 making dynamical landmask
      0.0 moving elements to ocean
      0.6 readers
        0.1 global_landmask
        0.0 postprocessing
    0.7 main loop
        0.3 /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc
      0.0 updating elements
    0.0 cleaning up
--------------------
===========================
Model:  OceanDrift     (OpenDrift version 1.7.1)
        1000 active Lagrangian3DArray particles  (0 deactivated, 0 scheduled)
-------------------
Environment variables:
  -----
  x_sea_water_velocity
  y_sea_water_velocity
     1) /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc
  -----
  land_binary_mask
     1) global_landmask
  -----
Readers not added for the following variables:
  ocean_vertical_diffusivity
  sea_floor_depth_below_sea_level
  sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment
  sea_surface_wave_period_at_variance_spectral_density_maximum
  sea_surface_wave_significant_height
  sea_surface_wave_stokes_drift_x_velocity
  sea_surface_wave_stokes_drift_y_velocity
  surface_downward_x_stress
  surface_downward_y_stress
  turbulent_generic_length_scale
  turbulent_kinetic_energy
  upward_sea_water_velocity
  x_wind
  y_wind

Time:
        Start: 2015-11-16 00:00:00
        Present: 2015-11-16 20:00:00
        Calculation steps: 20 * 1:00:00 - total time: 20:00:00
        Output steps: 21 * 1:00:00
===========================

13:51:19 WARNING opendrift.models.basemodel: Plotting fast. This will make your plots less accurate.
13:51:27 INFO    opendrift.models.basemodel: Saving animation to /root/project/docs/source/gallery/animations/example_convolve_input_0.gif...
13:51:27 INFO    opendrift.models.basemodel: Making animated gif...
MovieWriter imagemagick unavailable; using Pillow instead.
13:51:28 INFO    opendrift.models.basemodel: Could not save animation:
13:51:28 INFO    opendrift.models.basemodel: list index out of range
gallery/animations/example_convolve_input_0.gif
o.plot(fast=True)
OpenDrift - OceanDrift 2015-11-16 00:00 to 2015-11-16 20:00 UTC (21 steps)

Out:

13:51:28 WARNING opendrift.models.basemodel: Plotting fast. This will make your plots less accurate.

(<GeoAxesSubplot:title={'center':'OpenDrift - OceanDrift\n2015-11-16 00:00 to 2015-11-16 20:00 UTC (21 steps)'}>, <module 'matplotlib.pyplot' from '/opt/conda/envs/opendrift/lib/python3.9/site-packages/matplotlib/pyplot.py'>)

Total running time of the script: ( 0 minutes 18.809 seconds)

Gallery generated by Sphinx-Gallery