Skip to content

Commit f34ea22

Browse files
committed
Added new methods num_timesteps, num_trajectories and __repr__ to print summary information about a dataset (print self.ds.traj). New commandline utility trajaninfo <filename> also uses this new repr.
1 parent dbf66aa commit f34ea22

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "GPLv2"
77

88
[tool.poetry.scripts]
99
trajanshow = "trajan.scripts.trajanshow:main"
10+
trajaninfo = "trajan.scripts.trajaninfo:main"
1011

1112
[tool.poetry.dependencies]
1213
python = "^3.8"

trajan/scripts/trajaninfo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Utility script to quickly print summary information about a drifter collection file
2+
3+
import xarray as xr
4+
import trajan as _
5+
import click
6+
from pathlib import Path
7+
import lzma
8+
9+
@click.command()
10+
@click.argument('tf')
11+
12+
def main(tf):
13+
tf = Path(tf)
14+
if tf.suffix == '.xz':
15+
with lzma.open(tf) as fd:
16+
ds = xr.open_dataset(fd)
17+
ds.load()
18+
else:
19+
ds = xr.open_dataset(tf)
20+
21+
if 'status' in ds: # hack for OpenDrift files
22+
ds = ds.where(ds.status>=0)
23+
24+
print(ds.traj)
25+
26+
if __name__ == '__main__':
27+
main()

trajan/traj.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from abc import abstractmethod
9+
from datetime import timedelta
910
import pyproj
1011
import numpy as np
1112
import xarray as xr
@@ -63,6 +64,44 @@ def __init__(self, ds, obsdim, timedim):
6364
self.obsdim = obsdim
6465
self.timedim = timedim
6566

67+
def num_timesteps(self):
68+
if self.timedim in self.ds.sizes:
69+
timedim = self.timedim
70+
else:
71+
logger.warning(f'self.timedim ({self.timedim}) is not an existing dimension! Using instead "obs".')
72+
timedim = 'obs'
73+
return self.ds.sizes[timedim]
74+
75+
def num_trajectories(self):
76+
return self.ds.sizes['trajectory']
77+
78+
def __repr__(self):
79+
output = '=======================\n'
80+
output += 'TrajAn info:\n'
81+
output += '------------\n'
82+
output += f'{self.num_trajectories()} trajectories\n'
83+
output += f'{self.num_timesteps()} timesteps\n'
84+
try:
85+
timestep = self.timestep()
86+
timestep = timedelta(seconds=int(timestep))
87+
except:
88+
timestep = '[self.timestep returns error]' # TODO
89+
output += f'Timestep: {timestep}\n'
90+
start_time = self.ds.time.min().data
91+
end_time = self.ds.time.max().data
92+
output += f'Time coverage: {start_time} - {end_time}\n'
93+
output += f'Longitude span: {self.tx.min().data} to {self.tx.max().data}\n'
94+
output += f'Latitude span: {self.ty.min().data} to {self.ty.max().data}\n'
95+
output += 'Variables:\n'
96+
for var in self.ds.variables:
97+
if var not in ['trajectory', 'obs']:
98+
output += f' {var}'
99+
if 'standard_name' in self.ds[var].attrs:
100+
output += f' [{self.ds[var].standard_name}]'
101+
output += '\n'
102+
output += '=======================\n'
103+
return output
104+
66105
@property
67106
def plot(self) -> Plot:
68107
"""

0 commit comments

Comments
 (0)