You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Parsing relative paths within hydrolib`-core does not seem to work properly.
This happens to InifielFieldModel and seems to be the case since the change to DiskOnlyFileModel.
Maybe related but when initialising a FMModel from a dictionnary with existing file links relative to the mdu file from a working directory that is different from the mdu file directory, hydrolib-core returns an error that files are not found.
To Reproduce
We see this with hydromt.
Example working dir is D: and files are written in:
D:/dflow_model
dflowfm
dflowfm.mdu
fieldfile.ini
maps
dem.tif
If the inifiledmodel.initial.datafile.filepath.name is then relative to mdu: ../maps/dem.tif, the .. will be wrongfully parsed and changed in the mdu file so the file is not found anymore.
Example code to reproduce outside of HydroMT, in a small test:
deftest_435_a():
workdir=Path(r"d:\dam_ar\dflowfm_models\hydrolib-core-435")
mdupath=workdir/r"dflow_model\dflowfm\dflowfm.mdu"mdupath_export=workdir/r"dflow_model_export\dflowfm\dflowfm.mdu"cwd=os.getcwd()
os.chdir(os.path.dirname(mdupath))
fmmodel=FMModel(mdupath)
print(fmmodel.geometry.inifieldfile.initial) # will reveal paths that start with "dflowfm_model/", so relative path is wrong.fmmodel.filepath=mdupath_exportfmmodel.save(recurse=True) # Will save MDU and inifield file, but with wrong references to non-existent tif and 1dfield ini file.
Work-around in hydromt is:
# Assign initial fields to model and write
inifield_model = IniFieldModel(initial=inilist, parameter=paramlist)
# FIXME: when initialising IniFieldModel hydrolib-core does not parse correclty the relative path
# For now re-update manually....
for i in range(len(inifield_model.initial)):
path = Path(f"../maps/{inifield_model.initial[i].datafile.filepath.name}")
inifield_model.initial[i].datafile.filepath = path
for i in range(len(inifield_model.parameter)):
path = Path(f"../maps/{inifield_model.parameter[i].datafile.filepath.name}")
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))
inifield_model.save(
join(fm_dir, inifield_model_filename),
recurse=False,
)
When initialising FMModel from D, with a dictionary that contains relative filepaths (eg "fieldfile.ini") to the mdu file in D/dlfow/dflowfm/dflowfm.mdu, then an error is returned that files are not found even if they do exist. Work-around is to temporary switch working dir to the mdu dir:
# Need to switch to dflowfm folder for files to be found and properly added
mdu_fn = cf_dict.pop("filepath", None)
cwd = os.getcwd()
os.chdir(dirname(mdu_fn))
mdu = FMModel(**cf_dict)
# add filepath
mdu.filepath = mdu_fn
# write
mdu.save(recurse=False)
# Go back to working dir
os.chdir(cwd)
Version info (please complete the following information):
Version 0.1.3 dev
The text was updated successfully, but these errors were encountered:
arthurvd
changed the title
Incorrect saving of relative paths for DiskOnlyFileModel
Incorrect resolving of relative paths for DiskOnlyFileModel
Dec 20, 2022
Describe the bug
Parsing relative paths within hydrolib`-core does not seem to work properly.
This happens to InifielFieldModel and seems to be the case since the change to DiskOnlyFileModel.
Maybe related but when initialising a FMModel from a dictionnary with existing file links relative to the mdu file from a working directory that is different from the mdu file directory, hydrolib-core returns an error that files are not found.
To Reproduce
We see this with hydromt.
Example working dir is D: and files are written in:
If the inifiledmodel.initial.datafile.filepath.name is then relative to mdu: ../maps/dem.tif, the .. will be wrongfully parsed and changed in the mdu file so the file is not found anymore.
Example code to reproduce outside of HydroMT, in a small test:
Work-around in hydromt is:
When initialising FMModel from D, with a dictionary that contains relative filepaths (eg "fieldfile.ini") to the mdu file in D/dlfow/dflowfm/dflowfm.mdu, then an error is returned that files are not found even if they do exist. Work-around is to temporary switch working dir to the mdu dir:
Version info (please complete the following information):
The text was updated successfully, but these errors were encountered: