Note
Go to the end to download the full example code.
Reading and using a contiguous ragged dataset#
import matplotlib.pyplot as plt
import xarray as xr
import trajan as ta
import coloredlogs
# the test data: a small extract from the Sofar dataset https://sofar-spotter-archive.s3.amazonaws.com/index.html
xr_data = xr.load_dataset(ta.DATA_DIR + 'xr_spotter_bulk_test_data.nc')
# this is a contiguous ragged dataset; the data variables are 1D arrays
xr_data

2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Found CF trajectory dimension "trajectory"
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG 1D storage dataset; detected: obs_dim = 'index', timecoord = 'time', trajectory_dim = 'trajectory', rowsize
2026-07-28 17:56:49 runnervmvrwv9 trajan.traj[4232] DEBUG Setting up new plot object.
2026-07-28 17:56:49 runnervmvrwv9 trajan.plot[4232] DEBUG Plotting lines
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting trajectory dimension
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Found CF trajectory dimension "trajectory"
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Detecting time-variable for "index"..
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected obs-dim: index, detected time-variable: time.
2026-07-28 17:56:49 runnervmvrwv9 trajan.accessor[4232] DEBUG Detected Ragged trajectory dataset
2026-07-28 17:56:49 runnervmvrwv9 trajan.plot[4232] DEBUG Creating new figure and axes..
# Convert ContiguousRaggedArray to RaggedArray (non-contiguous) dataset:
# compare:
print(f"{xr_data = }")
# with:
xr_data_as_ragged = xr_data.traj.to_ragged()
print(f"{xr_data_as_ragged = }")
# Dumping the Ragged dataset to disk:
#xr_data_as_ragged.to_netcdf("./xr_spotter_bulk_test_data_as_ragged.nc")
xr_data = <xarray.Dataset> Size: 5kB
Dimensions: (index: 65, trajectory: 2)
Coordinates:
time (index) datetime64[ns] 520B 2022-03-15T04:07:25 .....
* trajectory (trajectory) <U11 88B 'SPOT-010102' 'SPOT-010103'
Dimensions without coordinates: index
Data variables:
significantWaveHeight (index) float64 520B 1.736 1.817 ... 1.263 1.136
peakPeriod (index) float64 520B 7.877 8.533 ... 7.314 7.314
meanPeriod (index) float64 520B 6.804 6.652 ... 6.629 6.497
peakDirection (index) float64 520B 147.9 155.5 ... 105.2 102.2
peakDirectionalSpread (index) float64 520B 30.37 25.48 26.04 ... 37.2 38.09
meanDirection (index) float64 520B 146.7 144.5 ... 127.3 120.8
meanDirectionalSpread (index) float64 520B 40.34 40.47 ... 50.39 51.12
latitude (index) float64 520B -16.41 -16.43 ... -12.64 -12.65
longitude (index) float64 520B 65.03 65.03 ... 70.15 70.14
rowsize (trajectory) int64 16B 20 45
Attributes:
title: Sofar Spotter Data Archive - Bulk Wave Parameters
institution: Sofar Ocean
source: Spotter wave buoy
creation_date: 2023-10-18 00:43:55.333537
author: Isabel A. Houghton
email: isabel.houghton@sofarocean.com
references: https://content.sofarocean.com/hubfs/Spotter%20product%20...
modified: modified to only keep a couple of buoys, to be used as a ...
xr_data_as_ragged = <xarray.Dataset> Size: 7kB
Dimensions: (trajectory: 2, obs: 45)
Coordinates:
* trajectory (trajectory) <U11 88B 'SPOT-010102' 'SPOT-010103'
Dimensions without coordinates: obs
Data variables:
time (trajectory, obs) datetime64[ns] 720B 2022-03-15T0...
significantWaveHeight (trajectory, obs) float64 720B 1.736 1.817 ... 1.136
peakPeriod (trajectory, obs) float64 720B 7.877 8.533 ... 7.314
meanPeriod (trajectory, obs) float64 720B 6.804 6.652 ... 6.497
peakDirection (trajectory, obs) float64 720B 147.9 155.5 ... 102.2
peakDirectionalSpread (trajectory, obs) float64 720B 30.37 25.48 ... 38.09
meanDirection (trajectory, obs) float64 720B 146.7 144.5 ... 120.8
meanDirectionalSpread (trajectory, obs) float64 720B 40.34 40.47 ... 51.12
lat (trajectory, obs) float64 720B -16.41 ... -12.65
lon (trajectory, obs) float64 720B 65.03 65.03 ... 70.14
Attributes:
title: Sofar Spotter Data Archive - Bulk Wave Parameters
institution: Sofar Ocean
source: Spotter wave buoy
creation_date: 2023-10-18 00:43:55.333537
author: Isabel A. Houghton
email: isabel.houghton@sofarocean.com
references: https://content.sofarocean.com/hubfs/Spotter%20pr...
modified: modified to only keep a couple of buoys, to be us...
trajan_converted_from: contiguous
Total running time of the script: (0 minutes 6.110 seconds)