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.8.0
Platform: Linux, 6.11.0-1018-azure
15.620014190673828 GB memory
4 processors (x86_64)
NumPy version 2.4.1
SciPy version 1.17.0
Matplotlib version 3.10.8
NetCDF4 version 1.7.4
Xarray version 2025.9.0
Pandas version 3.0.0
OpenDrift version 1.14.8
Python version 3.14.2 | packaged by conda-forge | (main, Jan 26 2026, 19:56:00) [GCC 14.3.0]
------------------------------------------------------
Open drifter dataset from a CSV file
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG No trajectory_id attribute/variable found, trying to identify by name.
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detecting time-variable for "obs"..
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detected un-structured (2D) trajectory dataset
2026-02-05 14:45:20 runnervmkj6or trajan.traj2d[2639] DEBUG Condensing 844 observations.
2026-02-05 14:45:20 runnervmkj6or trajan.traj2d[2639] DEBUG Condensed observations from: 844 to 844
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG No trajectory_id attribute/variable found, trying to identify by name.
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detecting time-variable for "obs"..
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-02-05 14:45:20 runnervmkj6or trajan.accessor[2639] DEBUG Detected un-structured (2D) trajectory dataset
2026-02-05 14:45:20 runnervmkj6or trajan.traj[2639] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-02-05 14:45:20 runnervmkj6or trajan.traj[2639] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-02-05 14:45:20 runnervmkj6or trajan.traj[2639] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-02-05 14:45:20 runnervmkj6or trajan.traj[2639] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
<xarray.Dataset> Size: 27kB
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) float64 7kB 5.332 5.332 5.332 ... 5.25 5.25
lat (trajectory, obs) float64 7kB 60.38 60.38 60.38 ... 60.45 60.45
Attributes:
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 60.38380600000001
geospatial_lat_max: 60.45141000000001
geospatial_lon_min: 5.244493666666667
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-02-05 14:45:47 runnervmkj6or pyproj[2639] 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-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting time-variable for "obs"..
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected un-structured (2D) trajectory dataset
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting time-variable for "obs"..
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected un-structured (2D) trajectory dataset
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected structured (1D) trajectory dataset
2026-02-05 14:45:47 runnervmkj6or trajan.traj1d[2639] WARNING non-unique time points, dropping time-duplicates
<xarray.Dataset> Size: 6kB
Dimensions: (trajectory: 1, time: 239)
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.249 5.25
lat (trajectory, time) float64 2kB 60.38 60.38 60.38 ... 60.44 60.45
Attributes:
Conventions: CF-1.10
featureType: trajectory
geospatial_lat_min: 60.38380600000001
geospatial_lat_max: 60.45141000000001
geospatial_lon_min: 5.244493666666667
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-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected structured (1D) trajectory dataset
2026-02-05 14:45:47 runnervmkj6or trajan.traj[2639] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detecting trajectory dimension
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-02-05 14:45:47 runnervmkj6or trajan.accessor[2639] DEBUG Detected structured (1D) trajectory dataset
<xarray.DataArray 'x' (trajectory: 1, time: 239)> Size: 2kB
array([[341742.63684573, 341746.32710342, 341750.58095892,
341754.83482578, 341759.08870403, 341763.34259365,
341767.59649465, 341771.85040703, 341776.10433078,
341780.35826591, 341784.61221241, 341788.86617029,
341793.12013955, 341797.37412018, 341801.62811219,
341805.88211558, 341810.13613034, 341814.39015647,
341818.64419399, 341822.89824288, 341827.15230314,
341831.40637478, 341835.6604578 , 341839.91455219,
341844.16865796, 341848.42277511, 341852.67690363,
341856.93104353, 341861.1851948 , 341865.43935745,
341869.69353147, 341873.94771687, 341878.20191364,
341882.45612179, 341886.71034132, 341890.96457222,
341895.2188145 , 341899.47306815, 341903.72733318,
341907.98160959, 341912.23589736, 341916.49019652,
341920.74450705, 341924.99882895, 341929.25316223,
341933.50750689, 341937.76186292, 341942.01623033,
341946.27060911, 341950.52499926, 341954.7794008 ,
341959.0338137 , 341963.28823798, 341967.54267364,
341971.79712067, 341976.05157908, 341980.30604886,
341984.56053002, 341988.81502255, 341993.06952645,
...
342507.94844798, 342512.2043394 , 342516.46024219,
342520.71615635, 342524.97208188, 342529.22801879,
342533.48396706, 342537.7399267 , 342541.99589772,
342546.25188011, 342550.50787387, 342554.763879 ,
342559.0198955 , 342563.27592337, 342567.53196261,
342571.78801322, 342576.0440752 , 342580.30014856,
342584.55623328, 342588.81232938, 342593.06843685,
342597.32455568, 342601.58068589, 342605.83682747,
342610.09298042, 342614.34914474, 342618.60532043,
342622.86150749, 342627.11770592, 342631.37391573,
342635.6301369 , 342639.88636944, 342644.14261335,
342648.39886864, 342652.65513529, 342656.91141331,
342661.16770271, 342665.42400347, 342669.68031561,
342673.93663911, 342678.19297399, 342682.44932023,
342686.70567785, 342690.96204683, 342695.21842719,
342699.47481891, 342703.73122201, 342707.98763647,
342712.24406231, 342716.50049951, 342720.75694809,
342725.01340803, 342729.26987935, 342733.52636203,
342743.94356181, 346363.11391299, 345860.07469316,
345775.39990869, 346050.77654721]])
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: 239, trajectory: 1)> Size: 956B
[239 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: (0 minutes 52.008 seconds)