Deltares / HYDROLIB-core

Core code around the I/O of the DHYDRO-suite
https://deltares.github.io/HYDROLIB-core/
MIT License
24 stars 4 forks source link

Relative paths not working in newly initialized FMModel #532

Open veenstrajelmer opened 1 year ago

veenstrajelmer commented 1 year ago

Describe the bug When initializing an FMModel and providing input (network, fixedweirfile, extfile, etc), one has to provide absolute paths of the files. When relative paths are used (the files are e.g. located in the folder where the mdu will also be saved), the files are not found. This might have to do with the mdu file not (yet) being saved, but even when doing that first, relative paths do not work.

To Reproduce

import os
import hydrolib.core.dflowfm as hcdfm

dir_output = r'c:\test_model_folder'

#create dummy file
if not os.path.exists(dir_output):
    os.mkdir(dir_output)
file_pli = os.path.join(dir_output,'test_model.pli')
with open(file_pli,'w') as f:
    f.write("""name
                2    2
                1.0    2.0
                3.0    4.0
                """)

## input
mdu_file = os.path.join(dir_output, 'test_model.mdu')
mdu = hcdfm.FMModel()

#absolute paths works
mdu.geometry.fixedweirfile = file_pli

#relative path does not work
mdu.geometry.fixedweirfile = os.path.basename(file_pli)

mdu.save(mdu_file)

Expected behavior Being able to provide relative paths to the mdu keywords. EDIT: It does work when setting os.chdir(dir_output), but changing the working directory is not always desireable. It might get a bit messy when using a nested model tree.

Version info (please complete the following information):

veenstrajelmer commented 1 year ago

A sort of workaround is to set os.chdir(dir_output) at the top of the script. However, this does not make the paths to the bc-files relative (not included in the minimal example above).