.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/example_drifters.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_drifters.py: Analysing a drifter dataset ============================ .. GENERATED FROM PYTHON SOURCE LINES 5-12 .. code-block:: Python import numpy as np import lzma import matplotlib.pyplot as plt import cartopy.crs as ccrs import xarray as xr import trajan as ta .. GENERATED FROM PYTHON SOURCE LINES 13-14 Importing a dataset with two drifters in the Barents Sea .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: Python with lzma.open('barents.nc.xz') as barents: ds = xr.open_dataset(barents) ds.load() .. GENERATED FROM PYTHON SOURCE LINES 19-21 This follows the CF convention for trajectories https://cfconventions.org/Data/cf-conventions/cf-conventions-1.10/cf-conventions.html#_multidimensional_array_representation_of_trajectories .. GENERATED FROM PYTHON SOURCE LINES 21-23 .. code-block:: Python print(ds) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 110kB Dimensions: (trajectory: 2, obs: 2287) Dimensions without coordinates: trajectory, obs Data variables: lon (trajectory, obs) float64 37kB 29.85 29.83 ... 21.14 21.15 lat (trajectory, obs) float64 37kB 77.3 77.31 ... 74.58 74.58 time (trajectory, obs) datetime64[ns] 37kB 2022-10-07T00:00:38 ... drifter_names (trajectory) 1000 m/s, which is a numerical error due to some cases with GPS positions reported with very small time interval. By removing all positions where time interval < 5 min, we avoid this problem. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python ds = ds.traj.drop_where(ds.traj.time_to_next() < np.timedelta64(5, 'm')) speed = ds.traj.speed() print(f'Max speed {speed.max().values} m/s') .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting time-variable for "obs".. 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: obs, detected time-variable: time. 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detected un-structured (2D) trajectory dataset Max speed 1.2873047098224348 m/s .. GENERATED FROM PYTHON SOURCE LINES 58-60 Likewise, one can insert breaks (NaN) in the trajectories whenever the time between points exceed a desired threshold, e.g. 3 hours .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python ds = ds.traj.insert_nan_where(ds.traj.time_to_next()>np.timedelta64(3, 'h')) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Plotting trajectories colored by drifter speed .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: Python mappable = ds.traj.plot(color=speed) cb = plt.gcf().colorbar(mappable, orientation='horizontal', pad=.05, aspect=30, shrink=.8, drawedges=False) cb.set_label('Speed [m/s]') plt.title('Trajectories colored by drift speed') plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_004.png :alt: Trajectories colored by drift speed :srcset: /gallery/images/sphx_glr_example_drifters_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting time-variable for "obs".. 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: obs, detected time-variable: time. 2025-06-25 09:02:11 fv-az1326-4 trajan.accessor[2399] DEBUG Detected un-structured (2D) trajectory dataset 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG Setting up new plot object. 2025-06-25 09:02:11 fv-az1326-4 trajan.plot[2399] DEBUG Plotting lines 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.plot[2399] DEBUG Creating new figure and axes.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:11 fv-az1326-4 trajan.plot[2399] DEBUG Plotting trajectory 0 of 2 with color /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) 2025-06-25 09:02:12 fv-az1326-4 trajan.plot[2399] DEBUG Plotting trajectory 1 of 2 with color 2025-06-25 09:02:13 fv-az1326-4 matplotlib.colorbar[2399] DEBUG locator: .. GENERATED FROM PYTHON SOURCE LINES 73-74 Histogram of drifter speeds. .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python speed = ds.traj.speed() plt.hist(speed.values[~np.isnan(speed.values)], 100) plt.xlabel('Drifter speed [m/s]') plt.ylabel('Number') plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_005.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-83 The peak at speed=0m/s is from the period where one of the drifters are on land (Hopen island). This can be removed simply: .. GENERATED FROM PYTHON SOURCE LINES 83-89 .. code-block:: Python speed = speed.where(speed>0.01) plt.hist(speed.values[~np.isnan(speed.values)], 100) plt.xlabel('Drifter speed [m/s]') plt.ylabel('Number') plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_006.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-93 The positions of GPS based drifters are normally given at slightly irregular time intervals, as drifters may be without GPS coverage for periods, and may get GPX fixes irregularly. TrajAn contains the method `gridtime` to interpolate positions to a regular time intervel, e.g. hourly: .. GENERATED FROM PYTHON SOURCE LINES 93-100 .. code-block:: Python dh = ds.traj.gridtime('1h') ds.traj.plot(color='r', label='raw data', land='mask') dh.traj.plot(color='b', label='hourly') plt.gca().set_extent([23.8, 25.0, 76.8, 77], crs=ccrs.PlateCarree()) # Zooming in to highliht differences plt.legend() plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_007.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-06-25 09:02:14 fv-az1326-4 trajan.plot[2399] DEBUG Plotting lines 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.plot[2399] DEBUG Creating new figure and axes.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:14 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: time, detected time-variable: time. 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected structured (1D) trajectory dataset 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG Setting up new plot object. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Plotting lines 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Axes already exist on existing figure. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) .. GENERATED FROM PYTHON SOURCE LINES 101-103 Having the dataset on a regular time interval makes it simpler to compare with e.g. drift models, and also makes some analyses simpler .. GENERATED FROM PYTHON SOURCE LINES 103-109 .. code-block:: Python dh.isel(trajectory=0).traj.plot(color='r', label='Full trajectory') dh.isel(trajectory=0).sel(time=slice('2022-10-10', '2022-10-12')).traj.plot( color='k', linewidth=2, label='10-12 Oct 2022') plt.legend() plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_008.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Single trajectory, a trajectory dimension will be added 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Using trajectory_id variable name (drifter_names) as trajectory dimension name 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: time, detected time-variable: time. 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected structured (1D) trajectory dataset 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG Setting up new plot object. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Plotting lines 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: time, detected time-variable: time. 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected structured (1D) trajectory dataset 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Creating new figure and axes.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.13/site-packages/shapely/creation.py:218: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, np.intc(handle_nan), out=out, **kwargs) 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Single trajectory, a trajectory dimension will be added 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Using trajectory_id variable name (drifter_names) as trajectory dimension name 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: time, detected time-variable: time. 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected structured (1D) trajectory dataset 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG Setting up new plot object. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Plotting lines 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detecting trajectory dimension 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected obs-dim: time, detected time-variable: time. 2025-06-25 09:02:15 fv-az1326-4 trajan.accessor[2399] DEBUG Detected structured (1D) trajectory dataset 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.plot[2399] DEBUG Axes already exist on existing figure. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2025-06-25 09:02:15 fv-az1326-4 trajan.traj[2399] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. .. GENERATED FROM PYTHON SOURCE LINES 110-112 The original dataset had two dimensions `(trajectory, obs)` (see top of page), and time is a 2D variable. For the gridded dataset (as with datasets imported from some trajectory models), dimensions are `(trajectory, time)` and time is a 1D Xarray dimension coordinate .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: Python print(dh) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 46kB Dimensions: (trajectory: 2, time: 1142) Coordinates: * time (time) datetime64[ns] 9kB 2022-10-07 ... 2022-11-23T13:00:00 * trajectory (trajectory) int64 16B 0 1 Data variables: lon (trajectory, time) float64 18kB nan 29.82 29.77 ... 21.13 nan lat (trajectory, time) float64 18kB nan 77.31 77.31 ... 74.58 nan drifter_names (trajectory) ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_drifters.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_drifters.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_