Skip to content

Commit

Permalink
D3D: Add delft_3d_convert_time
Browse files Browse the repository at this point in the history
  • Loading branch information
simmsa committed Mar 6, 2024
1 parent fe7e1a4 commit c9ad7e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mhkit/river/IO/delft_3d/delft_3d_convert_time.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function result = delft_3d_convert_time(delft_3d_py_object, seconds_run)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Returns
%
% Parameters
% ------------
% delft_3d_py_object: py.netCDF4._netCDF4.Dataset
% A netCDF python object.
% seconds_run: number
% Time index
%
% Returns
% ---------
% result: number
% The closest time index
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~isa(delft_3d_py_object, 'py.netCDF4._netCDF4.Dataset')
error('MATLAB:get_delft_3d_keys:InvalidInput', 'Input must be a py.netCDF4._netCDF4.Dataset object.');
end

python_result = py.mhkit_python_utils.delft_3d_helper.call_convert_time(delft_3d_py_object, seconds_run);

% Return type can be an numpy int64 or a double. Converting both of these types into MATLAB is
% more consistent using double conversion
result = double(python_result) + 1;
end
22 changes: 22 additions & 0 deletions mhkit_python_utils/delft_3d_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from mhkit.river.io import d3d
import netCDF4


Expand Down Expand Up @@ -45,3 +46,24 @@ def get_d3d_keys(netcdf_root):
result[var] = ""

return [list(result.keys()), list(result.values())]


def call_convert_time(netcdf_root, seconds_run):
"""
Calls the _convert_time function from the mhkit d3d module to find the closest time index to the specified number
of seconds, using information from the provided netCDF4 Dataset object.
Args:
netcdf_root (netCDF4.Dataset): The root object of a Delft3D NetCDF dataset.
seconds_run (int): The number of seconds to find the closest time index to.
Returns:
int: The index of the closest time index to the specified number of seconds.
Raises:
ValueError: If the input netCDF4 Dataset object is not provided.
"""
if not isinstance(netcdf_root, netCDF4.Dataset):
raise ValueError("Input must be a netCDF4 Dataset object")

return d3d._convert_time(netcdf_root, seconds_run=seconds_run)

0 comments on commit c9ad7e3

Please sign in to comment.