People-Places-Solutions / floodmodeller-api

The Flood Modeller Python API is a free and open-source python package which provides a bridge between Flood Modeller and the python programming language to extend the capabilities of Flood Modeller in automated workflows
https://api.floodmodeller.com/api/
Other
38 stars 15 forks source link

Issue reading default oriface shape #28

Closed calumpeden closed 2 years ago

calumpeden commented 2 years ago

When trying to read a .dat file with the oriface shape set to the default 'Rectangle' setting, as far as I can see there is no exception handling for that default. As the default does not write 'RECTANGLE' to the .dat file (see images 1 and 2 below) structures.py does raises an IndexError (see image 3 below) as there are only 5 items in the params1 list rather than 6 in this case.

image image 1

image image 2

image image 3

I have recoded the structures.py script to handle this exception and set self.shape to 'RECTANGLE' and print a warning noting the assumption of default shape. (see below image - changes/additions in lines 927 - 931 highlighted).

image image 4

Altered structures.py as a .txt file as couldn't upload a .py file = structures.txt

Obviously there may be a better way to handle this exception but if you want feel free to use this code change within the next release. There may be other exceptions of a similar nature for other units defaults settings if they are not explicitly written to the .dat file which could be handled similarly.

Regardless, really enjoying using it and it has revolutionised several projects. Kind Regards, Calum Peden

joe-pierce commented 2 years ago

Hi @calumpeden, thanks for raising this.

Can I check what version of the API you have installed? I believe this is an issue that should be resolved in more recent versions as the code currently looks like this:

# First parameter line
params1 = split_10_char(f"{block[3]:<60}")
self.invert = _to_float(params1[0])
self.soffit = _to_float(params1[1])
self.bore_area = _to_float(params1[2])
self.upstream_sill = _to_float(params1[3])
self.downstream_sill = _to_float(params1[4])
self.shape = _to_str(params1[5], "RECTANGLE")

as you can see on the last line we now have an additional helper function _to_str() to handle these cases with blank defaults.

To update your version simply run pip install floodmodeller-api --upgrade. There has been several other updates that will come with this so be sure to check out the release notes!

Thanks, Joe