trajan.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: str

Name of column containing longitudes.

lat: str

Name of column containing latitudes.

time: str

Name of column containing timestamps (parsable by pandas).

name: str

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