Skip to content

Commit dbf66aa

Browse files
authored
Merge pull request #136 from gauteh/interp-obs
Example for interpolating longitude and latitude to IMU observations
2 parents a2d62ba + 10295f0 commit dbf66aa

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Finding the closeset or interpolated positions at given times (e.g. wave observations).
3+
=======================================================================================
4+
"""
5+
6+
from pathlib import Path
7+
from trajan.readers.omb import read_omb_csv
8+
import xarray as xr
9+
import coloredlogs
10+
11+
coloredlogs.install(level='debug')
12+
13+
#%%
14+
# Read the data
15+
data = Path.cwd().parent / "tests" / "test_data" / "csv" / "omb3.csv"
16+
ds = read_omb_csv(data)
17+
print(ds)
18+
19+
#%%
20+
# The wave data in the variable `pHs0` is given along a different observation dimension.
21+
# Because it is also a observation, i.e. in the style of 2D trajectory
22+
# datasets, we need to iterate over the trajectories:
23+
24+
25+
def gridwaves(tds):
26+
t = tds[['lat', 'lon',
27+
'time']].traj.gridtime(tds['time_waves_imu'].squeeze())
28+
return t.traj.to_2d(obsdim='obs_waves_imu')
29+
30+
31+
dsw = ds.groupby('trajectory').map(gridwaves)
32+
33+
print(dsw)
34+
35+
#%%
36+
# We now have the positions interpolated to the IMU (wave) observations. We
37+
# could also merge these together to one dataset again:
38+
39+
ds = xr.merge((ds, dsw.rename({
40+
'lon': 'lon_waves',
41+
'lat': 'lat_waves'
42+
}).drop('time')))
43+
print(ds)

tests/test_convert_datalayout.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from numpy.testing import assert_almost_equal
2+
import numpy as np
3+
import trajan as ta
4+
import xarray as xr
5+
import pandas as pd
6+
7+
def test_to2d(barents):
8+
# print(barents)
9+
gr = barents.traj.gridtime('1H')
10+
# print(gr)
11+
12+
assert gr.traj.is_1d()
13+
14+
b2d = gr.traj.to_2d()
15+
assert b2d.traj.is_2d()

trajan/ragged.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, ds, obsdim, timedim, trajectorycoord, rowsizevar):
1919
self.rowvar = rowsizevar
2020
super().__init__(ds, obsdim, timedim)
2121

22-
def to_2d(self):
22+
def to_2d(self, obsdim='obs'):
2323
"""This actually converts a contiguous ragged xarray Dataset into an xarray Dataset that follows the Traj2d conventions."""
2424
global_attrs = self.ds.attrs
2525

@@ -70,7 +70,7 @@ def to_2d(self):
7070

7171
# trajectory vars
7272
'time':
73-
xr.DataArray(dims=["trajectory", "obs"],
73+
xr.DataArray(dims=["trajectory", obsdim],
7474
data=array_time,
7575
attrs={
7676
"standard_name": "time",
@@ -119,7 +119,7 @@ def to_2d(self):
119119
crrt_data_var = "lat"
120120

121121
ds_converted_to_traj2d[crrt_data_var] = \
122-
xr.DataArray(dims=["trajectory", "obs"],
122+
xr.DataArray(dims=["trajectory", obsdim],
123123
data=crrt_var,
124124
attrs=attrs)
125125

trajan/traj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def condense_obs(self) -> xr.Dataset:
862862
"""
863863

864864
@abstractmethod
865-
def to_2d(self) -> xr.Dataset:
865+
def to_2d(self, obsdim='obs') -> xr.Dataset:
866866
"""
867867
Convert dataset into a 2D dataset from.
868868
"""

trajan/traj1d.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ def is_1d(self):
2828
def is_2d(self):
2929
return False
3030

31+
def to_2d(self, obsdim='obs'):
32+
ds = self.ds.copy()
33+
time = ds[self.timedim].rename({
34+
self.timedim: obsdim
35+
}).expand_dims(dim={'trajectory': ds.sizes['trajectory']})
36+
ds = ds.rename({self.timedim: obsdim})
37+
ds[self.timedim] = time
38+
ds[obsdim] = np.arange(0, ds.sizes[obsdim])
39+
40+
return ds
41+
3142
def time_to_next(self):
3243
time_step = self.ds.time[1] - self.ds.time[0]
3344
return time_step

0 commit comments

Comments
 (0)