Skip to content

Commit

Permalink
unique cmems bc files with multiple polyfiles in modelbuilder (#1071)
Browse files Browse the repository at this point in the history
* renamed keyword ext_bnd to ext_new

* cleanup import and ocmments

* add polyfile basename to cmems bc filenames

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Jan 17, 2025
1 parent 79995d6 commit d9773eb
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 127 deletions.
7 changes: 4 additions & 3 deletions dfm_tools/interpolate_grid2bnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def ext_add_boundary_object_per_polyline(ext_new:hcdfm.ExtModel, boundary_object

def interpolate_tide_to_bc(ext_new: hcdfm.ExtModel, tidemodel, file_pli, component_list=None, load=True):
# read polyfile as geodataframe
polyfile_object = hcdfm.PolyFile(file_pli)
gdf_points = PolyFile_to_geodataframe_points(polyfile_object)
polyfile_obj = hcdfm.PolyFile(file_pli)
gdf_points = PolyFile_to_geodataframe_points(polyfile_obj)

# interpolate tidal components to plipoints
data_interp = interpolate_tide_to_plipoints(tidemodel=tidemodel, gdf_points=gdf_points,
Expand All @@ -155,7 +155,8 @@ def interpolate_tide_to_bc(ext_new: hcdfm.ExtModel, tidemodel, file_pli, compone
# convert interpolated xarray.Dataset to hydrolib ForcingModel and save as bc file
ForcingModel_object = plipointsDataset_to_ForcingModel(plipointsDataset=data_interp)
dir_output = os.path.dirname(file_pli)
file_bc_out = os.path.join(dir_output,f'tide_{tidemodel}.bc')
file_pli_name = polyfile_obj.filepath.stem
file_bc_out = os.path.join(dir_output,f'tide_{tidemodel}_{file_pli_name}.bc')
ForcingModel_object.save(filepath=file_bc_out)

# generate hydrolib-core Boundary object to be appended to the ext file
Expand Down
19 changes: 9 additions & 10 deletions dfm_tools/modelbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import hydrolib.core.dflowfm as hcdfm
from hydrolib.core.dimr.models import DIMR, FMComponent, Start
from hydrolib.core.utils import get_path_style_for_current_operating_system
from dfm_tools.hydrolib_helpers import get_ncbnd_construct
from dfm_tools.interpolate_grid2bnd import (ext_add_boundary_object_per_polyline,
open_prepare_dataset,
ds_apply_conversion_dict,
)

__all__ = [
"cmems_nc_to_bc",
"cmems_nc_to_ini",
Expand Down Expand Up @@ -44,10 +43,8 @@ def get_ncvarname(quantity, conversion_dict):
ncvarname = conversion_dict[quantity]['ncvarname']
return ncvarname


def cmems_nc_to_bc(ext_bnd, list_quantities, tstart, tstop, file_pli, dir_pattern, dir_output, conversion_dict=None, refdate_str=None):
#input examples in https://github.com/Deltares/dfm_tools/blob/main/tests/examples/preprocess_interpolate_nc_to_bc.py
# TODO: rename ext_bnd to ext_new for consistency

def cmems_nc_to_bc(ext_new, list_quantities, tstart, tstop, file_pli, dir_pattern, dir_output, conversion_dict=None, refdate_str=None):
if conversion_dict is None:
conversion_dict = dfmt.get_conversion_dict()

Expand Down Expand Up @@ -83,16 +80,18 @@ def cmems_nc_to_bc(ext_bnd, list_quantities, tstart, tstop, file_pli, dir_patter
ForcingModel_object = dfmt.plipointsDataset_to_ForcingModel(plipointsDataset=data_interp)

# generate boundary object for the ext file (quantity, pli-filename, bc-filename)
file_bc_out = os.path.join(dir_output,f'{quantity}_CMEMS.bc')
file_pli_name = polyfile_obj.filepath.stem
file_bc_out = os.path.join(dir_output,f'{quantity}_CMEMS_{file_pli_name}.bc')
ForcingModel_object.save(filepath=file_bc_out)
boundary_object = hcdfm.Boundary(quantity=quantity,
locationfile=file_pli, #placeholder, will be replaced later on
# locationfile is updated if multiple polylines in polyfile
locationfile=file_pli,
forcingfile=ForcingModel_object)

# add the boundary object to the ext file for each polyline in the polyfile
ext_add_boundary_object_per_polyline(ext_new=ext_bnd, boundary_object=boundary_object)
ext_add_boundary_object_per_polyline(ext_new=ext_new, boundary_object=boundary_object)

return ext_bnd
return ext_new


def preprocess_ini_cmems_to_nc(**kwargs):
Expand Down
Loading

0 comments on commit d9773eb

Please sign in to comment.