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 import test_data_folder as tdf
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(tdf + '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)
17:19:46 INFO    opendrift:513: OpenDriftSimulation initialised (version 1.14.2 / v1.14.2-88-gf09b7bd)
17:19:46 INFO    opendrift.readers:61: Opening file with xr.open_dataset
17:19:46 INFO    opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
17:19:46 INFO    opendrift.models.basemodel.environment:206: Adding a global landmask from GSHHG
17:19:50 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
17:19:50 INFO    opendrift.models.basemodel.environment:232:    sea_surface_height: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
17:19:50 INFO    opendrift.models.basemodel.environment:232:    sea_floor_depth_below_sea_level: 10000.000000
17:19:50 INFO    opendrift:1732: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
17:19:50 INFO    opendrift:1732: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
17:19:50 INFO    opendrift:1743: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:19:50 INFO    opendrift:1743: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:19:50 INFO    opendrift:1751: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
17:19:50 INFO    opendrift:899: Using existing reader for land_binary_mask
17:19:50 INFO    opendrift:928: All points are in ocean
17:19:50 INFO    opendrift:2035: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated)
17:19:50 INFO    opendrift:2035: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 16:00:00 - step 17 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 17:00:00 - step 18 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 18:00:00 - step 19 of 20 - 1000 active elements (0 deactivated)
17:19:51 INFO    opendrift:2035: 2015-11-16 19:00:00 - step 20 of 20 - 1000 active elements (0 deactivated)
<xarray.Dataset> Size: 2MB
Dimensions:                                   (trajectory: 1000, time: 21)
Coordinates:
  * trajectory                                (trajectory) int64 8kB 0 1 ... 999
  * time                                      (time) datetime64[ns] 168B 2015...
Data variables: (12/21)
    status                                    (trajectory, time) float32 84kB ...
    moving                                    (trajectory, time) float32 84kB ...
    age_seconds                               (trajectory, time) float32 84kB ...
    origin_marker                             (trajectory, time) float32 84kB ...
    lon                                       (trajectory, time) float32 84kB ...
    lat                                       (trajectory, time) float32 84kB ...
    ...                                        ...
    upward_sea_water_velocity                 (trajectory, time) float32 84kB ...
    sea_surface_wave_significant_height       (trajectory, time) float32 84kB ...
    sea_surface_wave_stokes_drift_x_velocity  (trajectory, time) float32 84kB ...
    sea_surface_wave_stokes_drift_y_velocity  (trajectory, time) float32 84kB ...
    sea_floor_depth_below_sea_level           (trajectory, time) float32 84kB ...
    land_binary_mask                          (trajectory, time) float32 84kB ...
Attributes: (12/119)
    Conventions:                                                           CF...
    standard_name_vocabulary:                                              CF...
    featureType:                                                           tr...
    title:                                                                 Op...
    summary:                                                               Ou...
    keywords:                                                              tr...
    ...                                                                    ...
    geospatial_lon_units:                                                  de...
    geospatial_lon_resolution:                                             point
    runtime:                                                               0:...
    geospatial_vertical_min:                                               0.0
    geospatial_vertical_max:                                               0.0
    geospatial_vertical_positive:                                          up


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)
17:19:54 INFO    opendrift:513: OpenDriftSimulation initialised (version 1.14.2 / v1.14.2-88-gf09b7bd)
17:19:54 INFO    opendrift.models.basemodel.environment:206: Adding a global landmask from GSHHG
17:19:54 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
17:19:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_height: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
17:19:54 INFO    opendrift.models.basemodel.environment:232:    sea_floor_depth_below_sea_level: 10000.000000
17:19:54 INFO    opendrift:1732: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
17:19:54 INFO    opendrift:1732: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
17:19:54 INFO    opendrift:1743: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:19:54 INFO    opendrift:1743: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:19:54 INFO    opendrift:1751: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
17:19:54 INFO    opendrift:899: Using existing reader for land_binary_mask
17:19:54 INFO    opendrift:928: All points are in ocean
17:19:54 INFO    opendrift:2035: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated)
17:19:54 INFO    opendrift:2035: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 16:00:00 - step 17 of 20 - 999 active elements (1 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 17:00:00 - step 18 of 20 - 999 active elements (1 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 18:00:00 - step 19 of 20 - 999 active elements (1 deactivated)
17:19:55 INFO    opendrift:2035: 2015-11-16 19:00:00 - step 20 of 20 - 999 active elements (1 deactivated)
<xarray.Dataset> Size: 2MB
Dimensions:                                   (trajectory: 1000, time: 21)
Coordinates:
  * trajectory                                (trajectory) int64 8kB 0 1 ... 999
  * time                                      (time) datetime64[ns] 168B 2015...
Data variables: (12/21)
    status                                    (trajectory, time) float32 84kB ...
    moving                                    (trajectory, time) float32 84kB ...
    age_seconds                               (trajectory, time) float32 84kB ...
    origin_marker                             (trajectory, time) float32 84kB ...
    lon                                       (trajectory, time) float32 84kB ...
    lat                                       (trajectory, time) float32 84kB ...
    ...                                        ...
    upward_sea_water_velocity                 (trajectory, time) float32 84kB ...
    sea_surface_wave_significant_height       (trajectory, time) float32 84kB ...
    sea_surface_wave_stokes_drift_x_velocity  (trajectory, time) float32 84kB ...
    sea_surface_wave_stokes_drift_y_velocity  (trajectory, time) float32 84kB ...
    sea_floor_depth_below_sea_level           (trajectory, time) float32 84kB ...
    land_binary_mask                          (trajectory, time) float32 84kB ...
Attributes: (12/119)
    Conventions:                                                           CF...
    standard_name_vocabulary:                                              CF...
    featureType:                                                           tr...
    title:                                                                 Op...
    summary:                                                               Ou...
    keywords:                                                              tr...
    ...                                                                    ...
    geospatial_lon_units:                                                  de...
    geospatial_lon_resolution:                                             point
    runtime:                                                               0:...
    geospatial_vertical_min:                                               0.0
    geospatial_vertical_max:                                               0.0
    geospatial_vertical_positive:                                          up


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'])
===========================
--------------------
Reader performance:
--------------------
/root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc
 0:00:00.2  total
 0:00:00.0  preparing
 0:00:00.1  reading
 0:00:00.0  interpolation
 0:00:00.0  interpolation_time
 0:00:00.0  rotating vectors
 0:00:00.0  masking
--------------------
global_landmask
 0:00:01.4  total
 0:00:00.0  preparing
 0:00:01.4  reading
 0:00:00.0  masking
--------------------
Performance:
    8.0 total time
    4.4 configuration
    0.0 preparing main loop
      0.0 moving elements to ocean
    2.0 main loop
      0.0 updating elements
    1.5 cleaning up
--------------------
===========================
Model:  OceanDrift     (OpenDrift version 1.14.2)
        938 active Lagrangian3DArray particles  (62 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:
  sea_floor_depth_below_sea_level
  sea_surface_height
  sea_surface_wave_significant_height
  sea_surface_wave_stokes_drift_x_velocity
  sea_surface_wave_stokes_drift_y_velocity
  upward_sea_water_velocity
  x_wind
  y_wind

Discarded readers:

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

17:19:55 WARNING opendrift:2391: Plotting fast. This will make your plots less accurate.
17:20:10 INFO    opendrift:4553: Saving animation to /root/project/docs/source/gallery/animations/example_convolve_input_0.gif...
17:21:46 INFO    opendrift:2994: Time to make animation: 0:01:51.283350
../_images/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)
17:21:46 WARNING opendrift:2391: Plotting fast. This will make your plots less accurate.

(<GeoAxes: title={'center': 'OpenDrift - OceanDrift\n2015-11-16 00:00 to 2015-11-16 20:00 UTC (21 steps)'}>, <Figure size 705.673x1100 with 1 Axes>)

Total running time of the script: (2 minutes 9.461 seconds)

Gallery generated by Sphinx-Gallery