Note
Go to the end to download the full example code.
Concatenating drifter datasets#
import numpy as np
import lzma
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import xarray as xr
import pandas as pd
import trajan as ta
Importing a dataset with two drifters in the Barents Sea
2025-06-24 07:11:27 fv-az1279-468 h5py._conv[2392] DEBUG Creating converter from 7 to 5
2025-06-24 07:11:27 fv-az1279-468 h5py._conv[2392] DEBUG Creating converter from 5 to 7
2025-06-24 07:11:27 fv-az1279-468 h5py._conv[2392] DEBUG Creating converter from 7 to 5
2025-06-24 07:11:27 fv-az1279-468 h5py._conv[2392] DEBUG Creating converter from 5 to 7
2025-06-24 07:11:27 fv-az1279-468 h5py._conv[2392] DEBUG Creating converter from 3 to 5
Split into two observational datasets for this example
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensing 2287 observations.
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensed observations from: 2287 to 2287
<xarray.Dataset> Size: 128kB
Dimensions: (trajectory: 2, obs: 2287)
Coordinates:
trajectory (trajectory) <U16 128B 'UIB-2022-TILL-01' 'UIB-2022-TILL-02'
* obs (obs) int64 18kB 0 1 2 3 4 5 6 ... 2281 2282 2283 2284 2285 2286
Data variables:
lon (trajectory, obs) float64 37kB 29.85 29.83 29.82 ... 21.14 21.15
lat (trajectory, obs) float64 37kB 77.3 77.31 77.31 ... 74.58 74.58
time (trajectory, obs) datetime64[ns] 37kB 2022-10-07T00:00:38 ......
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Single trajectory, a trajectory dimension will be added
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Using trajectory_id variable name (trajectory) as trajectory dimension name
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: time, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected structured (1D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Single trajectory, a trajectory dimension will be added
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Using trajectory_id variable name (trajectory) as trajectory dimension name
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: time, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected structured (1D) trajectory dataset
d1= <xarray.Dataset> Size: 33kB
Dimensions: (trajectory: 1, obs: 1027)
Coordinates:
* trajectory (trajectory) <U16 64B 'UIB-2022-TILL-01'
* obs (obs) int64 8kB 0 1 2 3 4 5 6 ... 1021 1022 1023 1024 1025 1026
Data variables:
lon (trajectory, obs) float64 8kB 29.85 29.83 29.82 ... 25.11 25.11
lat (trajectory, obs) float64 8kB 77.3 77.31 77.31 ... 76.57 76.57
time (trajectory, obs) datetime64[ns] 8kB 2022-10-07T00:00:38 ... ...
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
d2= <xarray.Dataset> Size: 73kB
Dimensions: (trajectory: 1, obs: 2287)
Coordinates:
* trajectory (trajectory) <U16 64B 'UIB-2022-TILL-02'
* obs (obs) int64 18kB 0 1 2 3 4 5 6 ... 2281 2282 2283 2284 2285 2286
Data variables:
lon (trajectory, obs) float64 18kB 27.82 27.8 27.78 ... 21.14 21.15
lat (trajectory, obs) float64 18kB 77.11 77.11 77.11 ... 74.58 74.58
time (trajectory, obs) datetime64[ns] 18kB 2022-10-07T00:00:40 ......
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
Concatenate two 2D datasets (with observation dimension).
dc = xr.concat((d1, d2), dim='trajectory')
dc = dc.traj.condense_obs()
print(dc)
assert np.all(ds.lat.values[~np.isnan(ds.lat.values)] ==
dc.lat.values[~np.isnan(dc.lat.values)])
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensing 2287 observations.
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensed observations from: 2287 to 2287
<xarray.Dataset> Size: 128kB
Dimensions: (trajectory: 2, obs: 2287)
Coordinates:
* trajectory (trajectory) <U16 128B 'UIB-2022-TILL-01' 'UIB-2022-TILL-02'
* obs (obs) int64 18kB 0 1 2 3 4 5 6 ... 2281 2282 2283 2284 2285 2286
Data variables:
lon (trajectory, obs) float64 37kB 29.85 29.83 29.82 ... 21.14 21.15
lat (trajectory, obs) float64 37kB 77.3 77.31 77.31 ... 74.58 74.58
time (trajectory, obs) datetime64[ns] 37kB 2022-10-07T00:00:38 ......
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
Concatenating two 1D datasets, with observations at different times.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
<xarray.Dataset> Size: 25kB
Dimensions: (trajectory: 1, time: 1027)
Coordinates:
* time (time) datetime64[ns] 8kB 2022-10-07T00:00:38 ... 2022-11-17T...
* trajectory (trajectory) <U16 64B 'UIB-2022-TILL-01'
Data variables:
lon (trajectory, time) float64 8kB 29.85 29.83 29.82 ... 25.11 25.11
lat (trajectory, time) float64 8kB 77.3 77.31 77.31 ... 76.57 76.57
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
Concatenating two 1D datasets will cause a lot of NaNs to be inserted.
dc = xr.concat((d1, d2), dim='trajectory')
print(dc)
assert np.all(ds.lat.values[~np.isnan(ds.lat.values)] ==
dc.lat.values[~np.isnan(dc.lat.values)])
<xarray.Dataset> Size: 127kB
Dimensions: (trajectory: 2, time: 3163)
Coordinates:
* time (time) datetime64[ns] 25kB 2022-10-07T00:00:38 ... 2022-11-23...
* trajectory (trajectory) <U16 128B 'UIB-2022-TILL-01' 'UIB-2022-TILL-02'
Data variables:
lon (trajectory, time) float64 51kB 29.85 nan 29.83 ... 21.14 21.15
lat (trajectory, time) float64 51kB 77.3 nan 77.31 ... 74.58 74.58
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
Converting to 2D and condensing the dataset will give a cleaner result.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: time, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected structured (1D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: time, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected structured (1D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting trajectory dimension
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detecting time-variable for "obs"..
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected obs-dim: obs, detected time-variable: time.
2025-06-24 07:11:27 fv-az1279-468 trajan.accessor[2392] DEBUG Detected un-structured (2D) trajectory dataset
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensing 2287 observations.
2025-06-24 07:11:27 fv-az1279-468 trajan.traj2d[2392] DEBUG Condensed observations from: 2287 to 2287
<xarray.Dataset> Size: 128kB
Dimensions: (trajectory: 2, obs: 2287)
Coordinates:
* trajectory (trajectory) <U16 128B 'UIB-2022-TILL-01' 'UIB-2022-TILL-02'
* obs (obs) int64 18kB 0 1 2 3 4 5 6 ... 2281 2282 2283 2284 2285 2286
Data variables:
lon (trajectory, obs) float64 37kB 29.85 29.83 29.82 ... 21.14 21.15
lat (trajectory, obs) float64 37kB 77.3 77.31 77.31 ... 74.58 74.58
time (trajectory, obs) datetime64[ns] 37kB 2022-10-07T00:00:38 ......
Attributes: (12/13)
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 74.5454462
geospatial_lat_max: 77.4774768
geospatial_lon_min: 17.2058074
geospatial_lon_max: 29.8523485
... ...
time_coverage_end: 2022-11-23T13:30:28
creator_email: gauteh@met.no, knutfd@met.no
creator_name: Gaute Hope and Knut Frode Dagestad
creator_url: https://github.com/OpenDrift/opendrift
summary: Two drifters in the Barents Sea. One stranded at Ho...
title: Barents Sea drifters
Total running time of the script: (0 minutes 0.154 seconds)