Skip to content

Commit

Permalink
resolved some simple bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 24, 2024
1 parent 83ea0cf commit f067867
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 23 additions & 13 deletions hydromt_delft3dfm/dflowfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ def __init__(
self._crs = CRS.from_user_input(crs) if crs else None
self._check_crs()

# TODO: three class functions that were deprecated in hydromt v1
def _read(self):
return self.root.is_reading_mode()

def _assert_read_mode(self):
assert self.root.is_reading_mode()

def _assert_write_mode(self):
assert self.root.is_writing_mode()

def setup_region(self, region):
"""HYDROMT CORE METHOD NOT USED FOR DFlowFMModel."""
raise ValueError(
Expand Down Expand Up @@ -2927,7 +2937,7 @@ def write_config(self) -> None:
cf_dict = self._config.copy()
# Need to switch to dflowfm folder for files to be found and properly added
mdu_fn = cf_dict.pop("filepath", None)
mdu_fn = Path(join(self.root, self._config_fn))
mdu_fn = Path(join(self.root.path, self._config_fn))
cwd = os.getcwd()
os.chdir(dirname(mdu_fn))
mdu = FMModel(**cf_dict)
Expand Down Expand Up @@ -2972,7 +2982,7 @@ def read_maps(self) -> Dict[str, Union[xr.Dataset, xr.DataArray]]:
# does not parse correclty the relative path
# For now re-update manually....
if not isfile(_fn):
_fn = join(self.root, "maps", _fn.name)
_fn = join(self.root.path, "maps", _fn.name)
inimap = hydromt.io.open_raster(_fn)
name = inidict.quantity
# Need to get branchid from config
Expand Down Expand Up @@ -3009,7 +3019,7 @@ def write_maps(self) -> None:
return
self._assert_write_mode()
# Global parameters
mapsroot = join(self.root, "maps")
mapsroot = join(self.root.path, "maps")
inilist = []
paramlist = []
logger.info(f"Writing maps files to {mapsroot}")
Expand Down Expand Up @@ -3091,7 +3101,7 @@ def _prepare_inifields(da_dict, da):
inifield_model.parameter[i].datafile.filepath = path
# Write inifield file
inifield_model_filename = inifield_model._filename() + ".ini"
fm_dir = dirname(join(self.root, self._config_fn))
fm_dir = dirname(join(self.root.path, self._config_fn))
inifield_model.save(
join(fm_dir, inifield_model_filename),
recurse=False,
Expand Down Expand Up @@ -3153,7 +3163,7 @@ def write_geoms(self, write_mesh_gdf=True) -> None:
super().write_geoms(fn="geoms/{name}.geojson")

# Write dfm files
savedir = dirname(join(self.root, self._config_fn))
savedir = dirname(join(self.root.path, self._config_fn))

# Write cross-sections (inc. friction)
if "crosssections" in self._geoms:
Expand Down Expand Up @@ -3288,7 +3298,7 @@ def write_forcing(self) -> None:
else:
self._assert_write_mode()
logger.info("Writting forcing files.")
savedir = dirname(join(self.root, self._config_fn))
savedir = dirname(join(self.root.path, self._config_fn))
# create new external forcing file
ext_fn = "bnd.ext"
Path(join(savedir, ext_fn)).unlink(missing_ok=True)
Expand All @@ -3309,7 +3319,7 @@ def read_mesh(self):
# FIXME: crs info is not available in dfmmodel, so get it from region.geojson
# Cannot use read_geoms yet because for some some geoms
# (crosssections, manholes) mesh needs to be read first...
region_fn = join(self.root, "geoms", "region.geojson")
region_fn = join(self.root.path, "geoms", "region.geojson")
if (not self._crs) and isfile(region_fn):
crs = gpd.read_file(region_fn).crs
self._crs = crs
Expand Down Expand Up @@ -3346,7 +3356,7 @@ def read_mesh(self):
def write_mesh(self, write_gui=True):
"""Write 1D branches and 2D mesh at <root/dflowfm/fm_net.nc>."""
self._assert_write_mode()
savedir = join(self.root, "dflowfm")
savedir = join(self.root.path, "dflowfm")
mesh_filename = "fm_net.nc"

# write mesh
Expand Down Expand Up @@ -3448,8 +3458,8 @@ def dfmmodel(self):
def init_dfmmodel(self):
"""Initialise the hydrolib-core FMModel object."""
# create a new MDU-Model
mdu_fn = Path(join(self.root, self._config_fn))
if isfile(mdu_fn) and self._read:
mdu_fn = Path(join(self.root.path, self._config_fn))
if isfile(mdu_fn) and self._read():
logger.info(f"Reading mdu file at {mdu_fn}")
self._dfmmodel = FMModel(filepath=mdu_fn)
else: # use hydrolib template
Expand All @@ -3468,7 +3478,7 @@ def dimr(self):
def read_dimr(self, dimr_fn: Optional[str] = None) -> None:
"""Read DIMR from file and else create from hydrolib-core."""
if dimr_fn is None:
dimr_fn = join(self.root, self._dimr_fn)
dimr_fn = join(self.root.path, self._dimr_fn)
# if file exist, read
if isfile(dimr_fn) and self._read:
logger.info(f"Reading dimr file at {dimr_fn}")
Expand All @@ -3488,9 +3498,9 @@ def write_dimr(self, dimr_fn: Optional[str] = None):
# force read
self.dimr
if dimr_fn is not None:
self._dimr.filepath = join(self.root, dimr_fn)
self._dimr.filepath = join(self.root.path, dimr_fn)
else:
self._dimr.filepath = join(self.root, self._dimr_fn)
self._dimr.filepath = join(self.root.path, self._dimr_fn)

if not self._read:
# Updates the dimr file first before writing
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dflowfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_read_write_config_empty_paths(tmpdir):
model.read_config()
# Check whether the path is an emtpy string
# TODO: we temporarly put . in the example mdu, so this is now also here
assert model.config["output"]["outputdir"] == Path(".")
assert model._config["output"]["outputdir"] == Path(".")

# write the mdu to read again
model.write_config()
Expand All @@ -29,7 +29,7 @@ def test_read_write_config_empty_paths(tmpdir):
# Check whether the path is an emtpy string
# TODO: should be an empty string: https://github.com/Deltares/HYDROLIB-core/issues/703
# then update this test: https://github.com/Deltares/hydromt_delft3dfm/issues/148
assert model2.config["output"]["outputdir"] == Path(".")
assert model2._config["output"]["outputdir"] == Path(".")


def test_setup_mesh2d_refine(tmpdir):
Expand Down

0 comments on commit f067867

Please sign in to comment.