.. 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-11 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import xarray as xr import trajan as ta .. GENERATED FROM PYTHON SOURCE LINES 12-13 Importing a dataset with two drifters in the Barents Sea .. GENERATED FROM PYTHON SOURCE LINES 13-15 .. code-block:: Python ds = xr.open_dataset(ta.DATA_DIR + 'barents.nc') .. GENERATED FROM PYTHON SOURCE LINES 16-18 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 18-20 .. 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 ... lat (trajectory, obs) float64 37kB ... time (trajectory, obs) datetime64[ns] 37kB ... drifter_names (trajectory) Size: 55kB Dimensions: (drifter_names: 1, obs: 2287) Coordinates: drifter_names (drifter_names) Size: 55kB Dimensions: (drifter_names: 1, obs: 2287) Coordinates: drifter_names (drifter_names) 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 50-54 .. 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 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset Max speed 1.2873047098224348 m/s .. GENERATED FROM PYTHON SOURCE LINES 55-57 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 57-59 .. code-block:: Python ds = ds.traj.insert_nan_where(ds.traj.time_to_next()>np.timedelta64(3, 'h')) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Plotting trajectories colored by drifter speed .. GENERATED FROM PYTHON SOURCE LINES 61-69 .. 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 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:04:00 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:04:00 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object. 2026-07-28 18:04:00 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:04:00 runnervmvrwv9 trajan.plot[4232] DEBUG Creating new figure and axes.. 2026-07-28 18:04:00 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting trajectory 0 of 2 with color 2026-07-28 18:04:01 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting trajectory 1 of 2 with color 2026-07-28 18:04:02 runnervmvrwv9 matplotlib.colorbar[4232] DEBUG locator: .. GENERATED FROM PYTHON SOURCE LINES 70-71 Histogram of drifter speeds. .. GENERATED FROM PYTHON SOURCE LINES 71-77 .. 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 78-80 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 80-86 .. 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 87-90 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 90-97 .. 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 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:04:02 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:04:02 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:04:02 runnervmvrwv9 trajan.plot[4232] DEBUG Creating new figure and axes.. 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:04:03 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object. 2026-07-28 18:04:03 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:04:03 runnervmvrwv9 trajan.plot[4232] DEBUG Axes already exist on existing figure .. GENERATED FROM PYTHON SOURCE LINES 98-100 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 100-106 .. 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 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Single trajectory, a trajectory dimension will be added 2026-07-28 18:04:03 runnervmvrwv9 trajan.accessor[4232] DEBUG Using trajectory_id variable name (drifter_names) as trajectory dimension name 2026-07-28 18:04:03 runnervmvrwv9 root[4232] DEBUG Size: 28kB Dimensions: (drifter_names: 1, time: 1143) Coordinates: drifter_names (drifter_names) Size: 2kB Dimensions: (drifter_names: 1, time: 72) Coordinates: drifter_names (drifter_names) Size: 46kB Dimensions: (trajectory: 2, time: 1143) Coordinates: * time (time) datetime64[ns] 9kB 2022-10-07 ... 2022-11-23T14:00:00 * trajectory (trajectory) int64 16B 0 1 Data variables: lon (trajectory, time) float64 18kB 29.85 29.82 ... 21.14 21.15 lat (trajectory, time) float64 18kB 77.3 77.31 ... 74.58 74.58 drifter_names (trajectory) Size: 28kB Dimensions: (drifter_names: 1, time: 1143) Coordinates: drifter_names (drifter_names)


.. GENERATED FROM PYTHON SOURCE LINES 122-131 Filtering outlier positions --------------------------- The :meth:`filter` method provides a convenient interface for common outlier-removal strategies, setting suspect lat/lon positions to NaN while leaving the time axis intact. **Speed filter** — positions from which the speed to the next observation exceeds a given threshold (here 3 m/s) are masked. We reload the original (uncleaned) dataset to illustrate the effect. .. GENERATED FROM PYTHON SOURCE LINES 131-134 .. code-block:: Python ds_raw = xr.open_dataset(ta.DATA_DIR + 'barents.nc') ds_speed = ds_raw.traj.filter(method='speed', max_speed=3.) .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset .. GENERATED FROM PYTHON SOURCE LINES 135-139 **n-sigma sliding filter** — positions whose latitude or longitude deviates more than *nsigma* standard deviations from the local mean in a sliding window of half-width *side_half_width* are masked (applied independently to latitude and longitude). .. GENERATED FROM PYTHON SOURCE LINES 139-141 .. code-block:: Python ds_sigma = ds_raw.traj.filter(method='nsigma_sliding', nsigma=5., side_half_width=2) .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: time, detected time-variable: time. 2026-07-28 18:05:50 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Orthogonal trajectory dataset .. GENERATED FROM PYTHON SOURCE LINES 142-143 Comparing raw and filtered trajectories .. GENERATED FROM PYTHON SOURCE LINES 143-150 .. code-block:: Python _, ax = ds_raw.traj.plot(color='gray', alpha=0.4, label='raw', land='mask') ds_speed.traj.plot(ax=ax, color='steelblue', label='speed filter (3 m/s)') ds_sigma.traj.plot(ax=ax, color='firebrick', label='n-sigma filter (5σ)') plt.legend() plt.title('Effect of outlier filters') plt.show() .. image-sg:: /gallery/images/sphx_glr_example_drifters_011.png :alt: Effect of outlier filters :srcset: /gallery/images/sphx_glr_example_drifters_011.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-07-28 18:05:50 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object. 2026-07-28 18:05:50 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:05:50 runnervmvrwv9 trajan.plot[4232] DEBUG Creating new figure and axes.. 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:51 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object. 2026-07-28 18:05:51 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:05:51 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting to existing Cartopy axes 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "obs".. 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: obs, detected time-variable: time. 2026-07-28 18:05:51 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset 2026-07-28 18:05:51 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object. 2026-07-28 18:05:51 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines 2026-07-28 18:05:51 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting to existing Cartopy axes .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 53.301 seconds) .. _sphx_glr_download_gallery_example_drifters.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_drifters.ipynb ` .. 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 `_