.. 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() .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-10-31 20:50:12 fv-az1024-670 h5py._conv[2149] DEBUG Creating converter from 3 to 5 .. 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 46-50 .. 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 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detecting time-dimension for "obs".. 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: obs, detected time-dim: time. 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detected un-structured (2D) trajectory dataset Max speed 1.2873047098224348 m/s .. GENERATED FROM PYTHON SOURCE LINES 51-53 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 53-55 .. code-block:: Python ds = ds.traj.insert_nan_where(ds.traj.time_to_next()>np.timedelta64(3, 'h')) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Plotting trajectories colored by drifter speed .. GENERATED FROM PYTHON SOURCE LINES 57-65 .. 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_003.png :alt: Trajectories colored by drift speed :srcset: /gallery/images/sphx_glr_example_drifters_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detecting time-dimension for "obs".. 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: obs, detected time-dim: time. 2024-10-31 20:50:14 fv-az1024-670 trajan.accessor[2149] DEBUG Detected un-structured (2D) trajectory dataset 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG Setting up new plot object. 2024-10-31 20:50:14 fv-az1024-670 trajan.plot[2149] DEBUG Plotting lines 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:14 fv-az1024-670 trajan.plot[2149] DEBUG Creating new figure and axes.. 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:14 fv-az1024-670 trajan.plot[2149] DEBUG Plotting trajectory 0 of 2 with color 2024-10-31 20:50:14 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) 2024-10-31 20:50:15 fv-az1024-670 trajan.plot[2149] DEBUG Plotting trajectory 1 of 2 with color 2024-10-31 20:50:15 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:15 fv-az1024-670 matplotlib.colorbar[2149] DEBUG locator: .. GENERATED FROM PYTHON SOURCE LINES 66-67 Histogram of drifter speeds. .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. 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_004.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-76 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 76-82 .. 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_005.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-86 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 86-93 .. 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_006.png :alt: example drifters :srcset: /gallery/images/sphx_glr_example_drifters_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-10-31 20:50:17 fv-az1024-670 trajan.plot[2149] DEBUG Plotting lines 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.plot[2149] DEBUG Creating new figure and axes.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) 2024-10-31 20:50:17 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: time, detected time-dim: time. 2024-10-31 20:50:17 fv-az1024-670 trajan.accessor[2149] DEBUG Detected structured (1D) trajectory dataset 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG Setting up new plot object. 2024-10-31 20:50:17 fv-az1024-670 trajan.plot[2149] DEBUG Plotting lines 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.plot[2149] DEBUG Axes already exist on existing figure. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:17 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) .. GENERATED FROM PYTHON SOURCE LINES 94-96 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 96-102 .. 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_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 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: time, detected time-dim: time. 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected structured (1D) trajectory dataset 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG Setting up new plot object. 2024-10-31 20:50:18 fv-az1024-670 trajan.plot[2149] DEBUG Plotting lines 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: time, detected time-dim: time. 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected structured (1D) trajectory dataset 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.plot[2149] DEBUG Creating new figure and axes.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. /home/runner/micromamba/envs/trajan/lib/python3.11/site-packages/shapely/creation.py:119: RuntimeWarning: invalid value encountered in linestrings return lib.linestrings(coords, out=out, **kwargs) 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: time, detected time-dim: time. 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected structured (1D) trajectory dataset 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG Setting up new plot object. 2024-10-31 20:50:18 fv-az1024-670 trajan.plot[2149] DEBUG Plotting lines 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected obs-dim: time, detected time-dim: time. 2024-10-31 20:50:18 fv-az1024-670 trajan.accessor[2149] DEBUG Detected structured (1D) trajectory dataset 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.plot[2149] DEBUG Axes already exist on existing figure. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. 2024-10-31 20:50:18 fv-az1024-670 trajan.traj[2149] DEBUG No grid-mapping specified, checking if coordinates are lon/lat.. .. GENERATED FROM PYTHON SOURCE LINES 103-105 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 105-107 .. 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 `_