.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/example_convolve_input.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_example_convolve_input.py: 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 .. GENERATED FROM PYTHON SOURCE LINES 9-27 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 13:54:26 INFO opendrift:576: OpenDriftSimulation initialised (version 1.14.7 / v1.14.7-15-g2988cd4) 13:54:26 INFO opendrift.readers:63: Opening file with xr.open_dataset 13:54:27 INFO opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'} 13:54:27 INFO opendrift.models.basemodel.environment:203: Adding a global landmask from GSHHG 13:54:31 INFO opendrift.models.basemodel.environment:227: Fallback values will be used for the following variables which have no readers: 13:54:31 INFO opendrift.models.basemodel.environment:230: sea_surface_height: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: x_wind: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: y_wind: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: upward_sea_water_velocity: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: ocean_vertical_diffusivity: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_significant_height: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_stokes_drift_x_velocity: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_stokes_drift_y_velocity: 0.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: ocean_mixed_layer_thickness: 50.000000 13:54:31 INFO opendrift.models.basemodel.environment:230: sea_floor_depth_below_sea_level: 10000.000000 13:54:31 INFO opendrift:1803: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False] 13:54:31 INFO opendrift:1803: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False] 13:54:31 INFO opendrift:1814: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous'])) 13:54:31 INFO opendrift:1814: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous'])) 13:54:31 INFO opendrift:1822: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True] 13:54:31 INFO opendrift:952: Using existing reader for land_binary_mask to move elements to ocean 13:54:31 INFO opendrift:982: All points are in ocean 13:54:31 INFO opendrift:2111: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 16:00:00 - step 17 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 17:00:00 - step 18 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 18:00:00 - step 19 of 20 - 1000 active elements (0 deactivated) 13:54:31 INFO opendrift:2111: 2015-11-16 19:00:00 - step 20 of 20 - 1000 active elements (0 deactivated) .. raw:: html
<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


.. GENERATED FROM PYTHON SOURCE LINES 28-29 Store final field of x-component of current .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python original_current = reader_norkyst.var_block_after[list(reader_norkyst.var_block_after.keys())[0]].data_dict['x_sea_water_velocity'].copy() .. GENERATED FROM PYTHON SOURCE LINES 32-35 For the second run, the NorKyst currents are convolved with a kernel, effectively lowering the spatial resolution. .set_convolution_kernel may also be given as an array (kernel) directly .. GENERATED FROM PYTHON SOURCE LINES 35-42 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 13:54:34 INFO opendrift:576: OpenDriftSimulation initialised (version 1.14.7 / v1.14.7-15-g2988cd4) 13:54:34 INFO opendrift.models.basemodel.environment:203: Adding a global landmask from GSHHG 13:54:34 INFO opendrift.models.basemodel.environment:227: Fallback values will be used for the following variables which have no readers: 13:54:34 INFO opendrift.models.basemodel.environment:230: sea_surface_height: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: x_wind: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: y_wind: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: upward_sea_water_velocity: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: ocean_vertical_diffusivity: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_significant_height: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_stokes_drift_x_velocity: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: sea_surface_wave_stokes_drift_y_velocity: 0.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: ocean_mixed_layer_thickness: 50.000000 13:54:34 INFO opendrift.models.basemodel.environment:230: sea_floor_depth_below_sea_level: 10000.000000 13:54:34 INFO opendrift:1803: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False] 13:54:34 INFO opendrift:1803: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False] 13:54:34 INFO opendrift:1814: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous'])) 13:54:34 INFO opendrift:1814: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous'])) 13:54:34 INFO opendrift:1822: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True] 13:54:34 INFO opendrift:952: Using existing reader for land_binary_mask to move elements to ocean 13:54:34 INFO opendrift:982: All points are in ocean 13:54:34 INFO opendrift:2111: 2015-11-16 00:00:00 - step 1 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 01:00:00 - step 2 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 02:00:00 - step 3 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 03:00:00 - step 4 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 04:00:00 - step 5 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 05:00:00 - step 6 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 06:00:00 - step 7 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 07:00:00 - step 8 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 08:00:00 - step 9 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 09:00:00 - step 10 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 10:00:00 - step 11 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 11:00:00 - step 12 of 20 - 1000 active elements (0 deactivated) 13:54:34 INFO opendrift:2111: 2015-11-16 12:00:00 - step 13 of 20 - 1000 active elements (0 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 13:00:00 - step 14 of 20 - 1000 active elements (0 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 14:00:00 - step 15 of 20 - 1000 active elements (0 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 15:00:00 - step 16 of 20 - 1000 active elements (0 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 16:00:00 - step 17 of 20 - 999 active elements (1 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 17:00:00 - step 18 of 20 - 999 active elements (1 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 18:00:00 - step 19 of 20 - 999 active elements (1 deactivated) 13:54:35 INFO opendrift:2111: 2015-11-16 19:00:00 - step 20 of 20 - 999 active elements (1 deactivated) .. raw:: html
<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


.. GENERATED FROM PYTHON SOURCE LINES 43-44 Store final field of x-component of (convolved) current .. GENERATED FROM PYTHON SOURCE LINES 44-59 .. code-block:: Python 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() .. image-sg:: /gallery/images/sphx_glr_example_convolve_input_001.png :alt: Original current field (x-component), Final, convolved current field (x-component) :srcset: /gallery/images/sphx_glr_example_convolve_input_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-61 Print and plot results, with convolved currents as background .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python 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']) .. rst-class:: sphx-glr-script-out .. code-block:: none =========================== -------------------- 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: 7.8 total time 4.3 configuration 0.0 preparing main loop 0.0 moving elements to ocean 2.0 main loop 0.0 updating elements 1.4 cleaning up -------------------- =========================== Model: OceanDrift (OpenDrift version 1.14.7) 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 =========================== 13:54:35 WARNING opendrift:2473: Plotting fast. This will make your plots less accurate. 13:54:43 INFO opendrift:4660: Saving animation to /root/project/docs/source/gallery/animations/example_convolve_input_0.gif... 13:55:40 INFO opendrift:3094: Time to make animation: 0:01:04.996404 .. GENERATED FROM PYTHON SOURCE LINES 67-68 .. image:: /gallery/animations/example_convolve_input_0.gif .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python o.plot(fast=True) .. image-sg:: /gallery/images/sphx_glr_example_convolve_input_002.png :alt: OpenDrift - OceanDrift 2015-11-16 00:00 to 2015-11-16 20:00 UTC (21 steps) :srcset: /gallery/images/sphx_glr_example_convolve_input_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 13:55:40 WARNING opendrift:2473: Plotting fast. This will make your plots less accurate. (,
) .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 21.917 seconds) .. _sphx_glr_download_gallery_example_convolve_input.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_convolve_input.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_convolve_input.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_convolve_input.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_