Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect resolving of relative paths for DiskOnlyFileModel #435

Open
hboisgon opened this issue Dec 16, 2022 · 1 comment
Open

Incorrect resolving of relative paths for DiskOnlyFileModel #435

hboisgon opened this issue Dec 16, 2022 · 1 comment
Assignees
Milestone

Comments

@hboisgon
Copy link

hboisgon commented Dec 16, 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:

  • 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:

def test_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_export
    fmmodel.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
@arthurvd arthurvd changed the title Parsing of relative paths Incorrect saving of relative paths for DiskOnlyFileModel Dec 20, 2022
@arthurvd arthurvd added the type: bug Something isn't working label Dec 20, 2022
@arthurvd arthurvd added this to To do in HYDROLIB-core via automation Dec 20, 2022
@arthurvd arthurvd added this to the Release 0.5 milestone Dec 20, 2022
@arthurvd arthurvd changed the title Incorrect saving of relative paths for DiskOnlyFileModel Incorrect resolving of relative paths for DiskOnlyFileModel Dec 20, 2022
@priscavdsluis priscavdsluis moved this from To do to In progress in HYDROLIB-core Mar 30, 2023
@priscavdsluis priscavdsluis self-assigned this Apr 5, 2023
@priscavdsluis
Copy link
Contributor

PR #512 will fix the problem for the greater part of the users.
We need to fix the root problem in another PR.

@priscavdsluis priscavdsluis moved this from In progress to To do in HYDROLIB-core Apr 5, 2023
priscavdsluis added a commit that referenced this issue Apr 11, 2023
@arthurvd arthurvd modified the milestones: Release 0.5, Release 1.0 Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
HYDROLIB-core
  
To do
Development

No branches or pull requests

3 participants