trajan

Trajan API

Subpackages

Submodules

Functions

read_csv(f, **kwargs)

Construct a CF-compliant trajectory dataset from a CSV file. A thin wrapper around: from_dataframe().

from_dataframe(df[, lon, lat, time, name, ...])

Construct a CF-compliant trajectory dataset from a pd.DataFrame of positions.

trajectory_dict_to_dataset(trajectory_dict[, ...])

Create a CF-compatible trajectory file from dictionary of drifter positions

Package Contents

trajan.read_csv(f, **kwargs)[source]

Construct a CF-compliant trajectory dataset from a CSV file. A thin wrapper around: from_dataframe().

trajan.from_dataframe(df, lon='lon', lat='lat', time='time', name=None, *, __test_condense__=False)[source]

Construct a CF-compliant trajectory dataset from a pd.DataFrame of positions.

Args:

lon: name of column containing longitudes.

lat: name of column containing latitudes.

time: name of column containing timestamps (parsable by pandas).

name: name of column to be used for drifter names.

Returns:

ds: a CF-compliant trajectory xarray.Dataset.

Constructing a dataset from arrays of postions and time:

import pandas as pd
import trajan as ta

# Construct some synthetic data
lon = np.linspace(5, 10, 50)
lat = np.linspace(60, 70, 50)
temp = np.linspace(10, 15, 50)
time = pd.date_range('2023-01-01', '2023-01-14', periods=50)

# Construct a pandas.DataFrame
ds = pd.DataFrame({'lon': lon, 'lat': lat, 'time': time, 'temp': temp, 'name': 'My drifter'})
ds = ta.from_dataframe(ds, name='name')
print(ds)
<xarray.Dataset>
Dimensions:     (trajectory: 1, obs: 50)
Coordinates:
  * trajectory  (trajectory) <U10 'My drifter'
  * obs         (obs) int64 0 1 2 3 4 5 6 7 8 9 ... 41 42 43 44 45 46 47 48 49
Data variables:
    lon         (trajectory, obs) float64 5.0 5.102 5.204 ... 9.796 9.898 10.0
    lat         (trajectory, obs) float64 60.0 60.2 60.41 ... 69.59 69.8 70.0
    time        (trajectory, obs) datetime64[ns] 2023-01-01 ... 2023-01-14
    temp        (trajectory, obs) float64 10.0 10.1 10.2 ... 14.8 14.9 15.0
Attributes:
    Conventions:          CF-1.10
    featureType:          trajectory
    geospatial_lat_min:   60.0
    geospatial_lat_max:   70.0
    geospatial_lon_min:   5.0
    geospatial_lon_max:   10.0
    time_coverage_start:  2023-01-01T00:00:00
    time_coverage_end:    2023-01-14T00:00:00

Often you might want to add some attributes:

ds = ds.assign_attrs({'Author': 'Albus Dumbledore'})
print(ds)
<xarray.Dataset>
Dimensions:     (trajectory: 1, obs: 50)
Coordinates:
  * trajectory  (trajectory) <U10 'My drifter'
  * obs         (obs) int64 0 1 2 3 4 5 6 7 8 9 ... 41 42 43 44 45 46 47 48 49
Data variables:
    lon         (trajectory, obs) float64 5.0 5.102 5.204 ... 9.796 9.898 10.0
    lat         (trajectory, obs) float64 60.0 60.2 60.41 ... 69.59 69.8 70.0
    time        (trajectory, obs) datetime64[ns] 2023-01-01 ... 2023-01-14
    temp        (trajectory, obs) float64 10.0 10.1 10.2 ... 14.8 14.9 15.0
Attributes:
    Conventions:          CF-1.10
    featureType:          trajectory
    geospatial_lat_min:   60.0
    geospatial_lat_max:   70.0
    geospatial_lon_min:   5.0
    geospatial_lon_max:   10.0
    time_coverage_start:  2023-01-01T00:00:00
    time_coverage_end:    2023-01-14T00:00:00
    Author:               Albus Dumbledore
Parameters:

df (pandas.DataFrame)

trajan.trajectory_dict_to_dataset(trajectory_dict, variable_attributes=None, global_attributes=None)[source]

Create a CF-compatible trajectory file from dictionary of drifter positions

Trajectory_dict shall have the following structure:
{‘buoy1_name’: {

time0: {‘lon’: lon0, ‘lat’: lat0, ‘variable1’: var1_0, … ‘variableM’: varM_0}, time1: {‘lon’: lon1, ‘lat’: lat1, ‘variable1’: var1_1, … ‘variableM’: varM_1},

timeN: {‘lon’: lonN, ‘lat’: latN, ‘variable1’: var1_N, … ‘variableM’: varM_N}},

{‘buoy2_name’: {