Skip to content

Commit 735a62a

Browse files
committed
1d: convert to 2d
1 parent a2d62ba commit 735a62a

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

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)