runtraj.particles.ParticlesAccessor.to_cf_trajectories#

ParticlesAccessor.to_cf_trajectories()[source]#

Return the particles as a CF-1.10 trajectory dataset, one trajectory per particle.

The dataset is what trajan, cf-xarray and other tools that read CF trajectories open. Write it to NetCDF with xarray.Dataset.to_netcdf(). Each particle is one trajectory along the trajectory dimension, and its rows, from its release onward, run along obs. A particle with fewer rows than the longest (one that left the meteorology domain early) is padded with missing values at the end. time is UTC, and age, in minutes since release, is kept as a variable. obs counts output steps from the release, so in a backward run time decreases along obs.

This is CF’s “incomplete multidimensional array” representation of trajectories (CF-1.10, appendix H.4.1).

Returns:

Dimensions (trajectory, obs). The coordinates are trajectory (the particle number), time, lon, lat and zagl. age and every other numeric column of the table are data variables, with units for the columns runtraj and HYSPLIT write.

Return type:

Dataset

Raises:

ValueError – If the table lacks one of particle, age, lon, lat zagl or time, if a particle has two rows at the same age, or if it holds more than one realization (select one: particles[particles.realization == k]).

Examples

>>> import pandas as pd
>>> particles = pd.DataFrame(
...     {
...         "particle": [1, 1, 1, 2, 2],
...         "age": [-1.0, -2.0, -3.0, -1.0, -2.0],
...         "lon": [-111.85, -111.86, -111.88, -111.84, -111.83],
...         "lat": [40.77, 40.78, 40.80, 40.76, 40.75],
...         "zagl": [5.0, 40.0, 120.0, 8.0, 60.0],
...     }
... )
>>> particles["time"] = pd.Timestamp("2024-01-15 18:00") + pd.to_timedelta(
...     particles["age"], unit="min"
... )
>>> ds = particles.runtraj.to_cf_trajectories()
>>> dict(ds.sizes)
{'trajectory': 2, 'obs': 3}
>>> ds.attrs["featureType"]
'trajectory'
>>> ds.age.sel(trajectory=2).values
array([-1., -2., nan])
>>> str(ds.time.sel(trajectory=1).values[-1])
'2024-01-15T17:57:00.000000000'