Skip to content

Commit bf3838c

Browse files
committed
Renamed detect_time_tim->detect_time_variable and detect_tx_dim->detect_tx_variable
1 parent 657e40b commit bf3838c

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

trajan/accessor.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,20 @@
77

88
logger = logging.getLogger(__name__)
99

10-
from .traj import Traj
10+
from .traj import Traj, detect_tx_variable
1111
from .traj1d import Traj1d
1212
from .traj2d import Traj2d
1313
from .ragged import ContiguousRagged
1414

1515

16-
def detect_tx_dim(ds):
17-
if 'lon' in ds:
18-
return ds.lon
19-
elif 'longitude' in ds:
20-
return ds.longitude
21-
elif 'x' in ds:
22-
return ds.x
23-
elif 'X' in ds:
24-
return ds.X
25-
else:
26-
raise ValueError("Could not determine x / lon variable")
27-
28-
29-
def detect_time_dim(ds, obs_dim):
30-
logger.debug(f'Detecting time-dimension for "{obs_dim}"..')
16+
def detect_time_variable(ds, obs_dim):
17+
logger.debug(f'Detecting time-variable for "{obs_dim}"..')
18+
# TODO: should use cf-xarray here
3119
for v in ds.variables:
3220
if obs_dim in ds[v].dims and 'time' in v:
3321
return v
3422

35-
raise ValueError("no time dimension detected")
23+
raise ValueError("No time variable detected")
3624

3725

3826
@xr.register_dataset_accessor("traj")
@@ -50,7 +38,7 @@ def __new__(cls, ds):
5038
obs_dim = None
5139
time_varname = None
5240

53-
tx = detect_tx_dim(ds)
41+
tx = detect_tx_variable(ds)
5442

5543
# if we have a 1D dims, this is most likely some contiguous data
5644
# there may be a few exceptions though, so be ready to default to the classical 2D parser below
@@ -106,11 +94,11 @@ def __new__(cls, ds):
10694
# there may also be some slightly unusual cases where these Traj1d and Traj2d classes will be used on data with 1D arrays
10795
if 'obs' in tx.dims:
10896
obs_dim = 'obs'
109-
time_varname = detect_time_dim(ds, obs_dim)
97+
time_varname = detect_time_variable(ds, obs_dim)
11098

11199
elif 'index' in tx.dims:
112100
obs_dim = 'obs'
113-
time_varname = detect_time_dim(ds, obs_dim)
101+
time_varname = detect_time_variable(ds, obs_dim)
114102

115103
elif 'time' in tx.dims:
116104
obs_dim = 'time'
@@ -123,7 +111,7 @@ def __new__(cls, ds):
123111
None) == 'trajectory_id' and not 'traj' in d:
124112

125113
obs_dim = d
126-
time_varname = detect_time_dim(ds, obs_dim)
114+
time_varname = detect_time_variable(ds, obs_dim)
127115

128116
break
129117

trajan/traj.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
logger = logging.getLogger(__name__)
2121

2222

23-
def detect_tx_dim(ds):
23+
def detect_tx_variable(ds):
2424
if 'lon' in ds:
2525
return ds.lon
2626
elif 'longitude' in ds:
@@ -33,15 +33,6 @@ def detect_tx_dim(ds):
3333
raise ValueError("Could not determine x / lon variable")
3434

3535

36-
def detect_time_dim(ds, obs_dim):
37-
logger.debug(f'Detecting time-dimension for "{obs_dim}"..')
38-
for v in ds.variables:
39-
if obs_dim in ds[v].dims and 'time' in v:
40-
return v
41-
42-
raise ValueError("no time dimension detected")
43-
44-
4536
class Traj:
4637
ds: xr.Dataset
4738

@@ -123,7 +114,7 @@ def tx(self):
123114
tlon, ty
124115
"""
125116

126-
return detect_tx_dim(self.ds)
117+
return detect_tx_variable(self.ds)
127118

128119
@property
129120
def ty(self):

0 commit comments

Comments
 (0)