Skip to content

Commit 6c5d9d8

Browse files
committed
updated convert_time to take xarray
1 parent f875e5f commit 6c5d9d8

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

mhkit/river/io/d3d.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def get_all_time(data: (netCDF4.Dataset, xr.Dataset)) -> NDArray:
2323
2424
Parameters
2525
----------
26-
data: NetCDF4 object or xarray Dataset
27-
A NetCDF4 object or xarray Dataset that contains spatial data, e.g. velocity or shear
26+
data: NetCDF4 object
27+
A NetCDF4 object that contains spatial data, e.g. velocity or shear
2828
stress generated by running a Delft3D model.
2929
3030
Returns
@@ -36,9 +36,13 @@ def get_all_time(data: (netCDF4.Dataset, xr.Dataset)) -> NDArray:
3636
"""
3737

3838
if not isinstance(data, (netCDF4.Dataset, xr.Dataset)):
39-
raise TypeError("data must be a NetCDF4 object or xarray Dataset")
39+
raise TypeError("data must be a NetCDF4 object Dataset")
4040

41-
seconds_run = np.ma.getdata(data.variables["time"][:], False)
41+
if isinstance(data, netCDF4.Dataset):
42+
seconds_run = np.ma.getdata(data.variables["time"][:], False)
43+
44+
elif isinstance(data, xr.Dataset):
45+
seconds_run = (data.time.values - data.time.values[0]).astype('timedelta64[s]').astype(int)
4246

4347
return seconds_run
4448

@@ -88,7 +92,7 @@ def seconds_to_index(data: netCDF4.Dataset, seconds_run: Union[int, float]) -> i
8892

8993

9094
def _convert_time(
91-
data: netCDF4.Dataset,
95+
data: netCDF4.Dataset or xr.Dataset,
9296
time_index: Optional[Union[int, float]] = None,
9397
seconds_run: Optional[Union[int, float]] = None,
9498
) -> Union[int, float]:

0 commit comments

Comments
 (0)