Note
Go to the end to download the full example code.
Read variables from a model along the trajectories of drifters.#
import xarray as xr
import numpy as np
import cf_xarray as _
import pyproj
import trajan as ta
import matplotlib.pyplot as plt
print(ta.versions())
------------------------------------------------------
Software and hardware:
TrajAn version 0.0.0
Platform: Linux, 6.17.0-1022-azure
15.614940643310547 GB memory
4 processors (x86_64)
NumPy version 2.5.2
SciPy version 1.18.0
Matplotlib version 3.11.1
NetCDF4 version 1.7.4
Xarray version 2025.9.0
Pandas version 3.0.5
OpenDrift version 1.14.10
Python version 3.14.6 | packaged by conda-forge | (main, Aug 11 2026, 10:26:15) [GCC 14.4.0]
------------------------------------------------------
Open drifter dataset from a CSV file
ds = ta.read_csv(ta.DATA_DIR + 'omb/bug05_pos.csv',
lon='Longitude',
lat='Latitude',
time='Time',
name='Device')
print(ds)
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG No trajectory_id attribute/variable found, trying to identify by name.
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detecting time-variable for "obs"..
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] WARNING No time CF-variable with dimension obs
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] WARNING Found instead variable with time in name: time
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detected Ragged trajectory dataset
2026-08-24 11:11:03 runnervm76f27 trajan.traj.ragged[2752] DEBUG Condensing 844 observations.
2026-08-24 11:11:03 runnervm76f27 trajan.traj.ragged[2752] DEBUG Condensed observations from: 844 to 844
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG No trajectory_id attribute/variable found, trying to identify by name.
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detecting time-variable for "obs"..
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] WARNING No time CF-variable with dimension obs
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] WARNING Found instead variable with time in name: time
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-08-24 11:11:03 runnervm76f27 trajan.accessor[2752] DEBUG Detected Ragged trajectory dataset
<xarray.Dataset> Size: 20kB
Dimensions: (trajectory: 1, obs: 844)
Coordinates:
* trajectory (trajectory) <U18 72B 'dev864475040536665'
* obs (obs) int64 7kB 0 1 2 3 4 5 6 7 ... 837 838 839 840 841 842 843
Data variables:
time (trajectory, obs) datetime64[us] 7kB 2022-05-10T12:56:16.5000...
lon (trajectory, obs) float32 3kB 5.332 5.332 5.332 ... 5.25 5.25
lat (trajectory, obs) float32 3kB 60.38 60.38 60.38 ... 60.45 60.45
Attributes:
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 60.383804
geospatial_lat_max: 60.45141
geospatial_lon_min: 5.2444935
geospatial_lon_max: 5.33242
time_coverage_start: 2020-01-01T00:00:07.250000
time_coverage_end: 2022-05-20T11:03:46.120000
Open the Norkyst800 model
nk = xr.open_dataset(
'https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_be')
Use cf-xarray to get the CRS
2026-08-24 11:11:10 runnervm76f27 pyproj[2752] DEBUG PROJ_ERROR: proj_create: several objects matching this name: Krovak (Greenwich), Equal Earth Greenwich, Laborde Grid (Greenwich), Modified Krovak (Greenwich), Krovak East North (Greenwich), Modified Krovak East North (Greenwich), ...
{"$schema": "https://proj.org/schemas/v0.2/projjson.schema.json", "type": "ProjectedCRS", "name": "undefined", "base_crs": {"$schema": "https://proj.org/schemas/v0.7/projjson.schema.json", "type": "GeographicCRS", "name": "undefined", "datum": {"type": "GeodeticReferenceFrame", "name": "undefined", "ellipsoid": {"name": "undefined", "semi_major_axis": 6378137, "semi_minor_axis": 6356752.3142}}, "coordinate_system": {"subtype": "ellipsoidal", "axis": [{"name": "Longitude", "abbreviation": "lon", "direction": "east", "unit": "degree"}, {"name": "Latitude", "abbreviation": "lat", "direction": "north", "unit": "degree"}]}}, "conversion": {"$schema": "https://proj.org/schemas/v0.7/projjson.schema.json", "type": "Conversion", "name": "unknown", "method": {"name": "Polar Stereographic (variant B)", "id": {"authority": "EPSG", "code": 9829}}, "parameters": [{"name": "Latitude of standard parallel", "value": 60, "unit": "degree", "id": {"authority": "EPSG", "code": 8832}}, {"name": "Longitude of origin", "value": 70, "unit": "degree", "id": {"authority": "EPSG", "code": 8833}}, {"name": "False easting", "value": 3192800, "unit": "metre", "id": {"authority": "EPSG", "code": 8806}}, {"name": "False northing", "value": 1784000, "unit": "metre", "id": {"authority": "EPSG", "code": 8807}}]}, "coordinate_system": {"$schema": "https://proj.org/schemas/v0.7/projjson.schema.json", "type": "CoordinateSystem", "subtype": "Cartesian", "axis": [{"name": "Easting", "abbreviation": "E", "direction": "east", "unit": "metre"}, {"name": "Northing", "abbreviation": "N", "direction": "north", "unit": "metre"}]}}
Grid the drifter dataset to the timesteps of the model.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Found CF trajectory dimension "trajectory"
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting time-variable for "obs"..
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] WARNING No time CF-variable with dimension obs
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] WARNING Found instead variable with time in name: time
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected Ragged trajectory dataset
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Found CF trajectory dimension "trajectory"
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting time-variable for "obs"..
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] WARNING No time CF-variable with dimension obs
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] WARNING Found instead variable with time in name: time
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected Ragged trajectory dataset
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Found CF trajectory dimension "trajectory"
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected Orthogonal trajectory dataset
2026-08-24 11:11:10 runnervm76f27 trajan.traj.orthogonal[2752] WARNING non-unique time points, dropping time-duplicates
<xarray.Dataset> Size: 6kB
Dimensions: (trajectory: 1, time: 251)
Coordinates:
* time (time) datetime64[us] 2kB 2022-05-10T13:00:00 ... 2022-05-20T...
* trajectory (trajectory) <U18 72B 'dev864475040536665'
Data variables:
lon (trajectory, time) float64 2kB 5.332 5.332 5.332 ... 5.25 5.25
lat (trajectory, time) float64 2kB 60.38 60.38 60.38 ... 60.45 60.45
Attributes:
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 60.383804
geospatial_lat_max: 60.45141
geospatial_lon_min: 5.2444935
geospatial_lon_max: 5.33242
time_coverage_start: 2020-01-01T00:00:07.250000
time_coverage_end: 2022-05-20T11:03:46.120000
Transform the drifter dataset to the CRS of the model
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Found CF trajectory dimension "trajectory"
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected Orthogonal trajectory dataset
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detecting trajectory dimension
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Found CF trajectory dimension "trajectory"
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-08-24 11:11:10 runnervm76f27 trajan.accessor[2752] DEBUG Detected Orthogonal trajectory dataset
<xarray.DataArray 'x' (trajectory: 1, time: 251)> Size: 2kB
array([[341742.46508359, 341746.15575471, 341750.41008676,
341754.66443019, 341758.918785 , 341763.17315118,
341767.42752874, 341771.68191768, 341775.93631799,
341780.19072968, 341784.44515275, 341788.69958719,
341792.95403301, 341797.2084902 , 341801.46295877,
341805.71743872, 341809.97193004, 341814.22643274,
341818.48094681, 341822.73547226, 341826.99000909,
341831.24455729, 341835.49911687, 341839.75368782,
341844.00827015, 341848.26286386, 341852.51746894,
341856.7720854 , 341861.02671323, 341865.28135244,
341869.53600302, 341873.79066498, 341878.04533832,
341882.30002303, 341886.55471912, 341890.80942658,
341895.06414542, 341899.31887563, 341903.57361722,
341907.82837019, 341912.08313453, 341916.33791024,
341920.59269733, 341924.8474958 , 341929.10230564,
341933.35712686, 341937.61195945, 341941.86680341,
341946.12165876, 341950.37652547, 341954.63140356,
341958.88629303, 341963.14119387, 341967.39610609,
341971.65102968, 341975.90596465, 341980.16091099,
341984.41586871, 341988.6708378 , 341992.92581827,
...
342558.93956979, 342563.19607422, 342567.45259002,
342571.70911719, 342575.96565573, 342580.22220565,
342584.47876693, 342588.73533959, 342592.99192361,
342597.24851901, 342601.50512578, 342605.76174392,
342610.01837342, 342614.2750143 , 342618.53166655,
342622.78833017, 342627.04500516, 342631.30169152,
342635.55838926, 342639.81509836, 342644.07181883,
342648.32855067, 342652.58529389, 342656.84204847,
342661.09881442, 342665.35559175, 342669.61238044,
342673.8691805 , 342678.12599194, 342682.38281474,
342686.63964892, 342690.89649446, 342695.15335138,
342699.41021966, 342703.66709931, 342707.92399034,
342712.18089273, 342716.4378065 , 342720.69473163,
342724.95166813, 342729.20861601, 342733.46557525,
342744.01671286, 346362.97897869, 345859.93639209,
345775.25735014, 346050.7958609 , 346071.2964588 ,
346071.2964588 , 346071.2964588 , 346071.2964588 ,
346071.2964588 , 346071.2964588 , 346071.2964588 ,
346071.2964588 , 346071.2964588 , 346071.2964588 ,
346071.2964588 , 346071.2964588 ]])
Coordinates:
* time (time) datetime64[us] 2kB 2022-05-10T13:00:00 ... 2022-05-20T...
* trajectory (trajectory) <U18 72B 'dev864475040536665'
Attributes:
grid_mapping: polar_stereographic
Extract the values of a variable for the trajectory
<xarray.DataArray 'temperature' (time: 251, trajectory: 1)> Size: 1kB
[251 values with dtype=float32]
Coordinates:
X (trajectory, time) float64 2kB 3.416e+05 3.416e+05 ... 3.464e+05
Y (trajectory, time) float64 2kB 4.344e+05 4.344e+05 ... 4.416e+05
depth float64 8B 0.0
* time (time) datetime64[ns] 2kB 2022-05-10T13:00:00 ... 2022-05-20T...
lat (trajectory, time) float64 2kB ...
lon (trajectory, time) float64 2kB ...
* trajectory (trajectory) <U18 72B 'dev864475040536665'
Attributes:
units: Celsius
time: ocean_time
grid: grid
location: face
field: temperature, scalar, series
grid_mapping: projection_stere
long_name: Sea water potential temperature
standard_name: sea_water_potential_temperature
_ChunkSizes: [ 1 1 17 2602]
Notice that the lat and lon variables from Norkyst match the original lat and lon from the dataset.
plt.figure()
temp.plot()
plt.show()
![depth = 0.0 [m], trajectory = dev864475040536665](../_images/sphx_glr_example_model_along_trajectory_001.png)
Total running time of the script: (1 minutes 6.526 seconds)