Retrieving pressure at the node using owa-epanet

I am using owa-epanet to retrieve pressure at the node for each timestamp.
I wrote the code shown below:

But finally, it returned the IndexError shown below:

Traceback (most recent call last):
File “d:/Vs code files/.py”, line 19, in
pres[i].append §
IndexError: list index out of range

Could you please tell what’s problem with the code I wrote? And if this IndexError is fixed, can I use those code to retrieve pressure at the node for each timestamp?
Thank you in advance!

Hello Ziyuan,

In Python, elements can be added dynamically, so you shouldn’t indicate the index on line 16:


Also, your code is missing to initialize the network to run a hydraulic analysis. After the openH it should goes:

en.initH(ph=epanet_proj, initFlag=en.NOSAVE)

Finally, values for initFlag can be found in the following link.

Hello Sergio,
Thanks for replying me.
I followed your advices to modify my code and an new TypeError was returned.
The modified code is shown below:

The new TypeError is:
I have no ideas to fix it. Could you please give me some clues? Thank you in advance!

The error simply says that some object is not iterable (e.g. not a list, dictionary, array, etc.)
In your case, I believe that the pres[ind] is causing the problem

You can either append the list before the print function by:
pres.append(ind) and than use print([round(x,2) for x in pres])
or just print the ind variable by print(round(ind,2))

Note that lines 22-23 are outside the while loop and therefore they are executed only once when the loop breaks.

1 Like

Could you please explain what does “pres.append(ind)” mean?
Here I want to retrieve pressure at the node, and I believe that using “pres.append(ind)” is to add index of the node to the list of pres. If I choose to print “pres”, maybe the results it returnd is not the pressure at the node with index of “ind”. And I changed the code based on your advices, the error was fixed, but the pressure at the node I retrieve using the code did not match the pressure at the node in rpt file.

Now I understand what you are trying to do. Appending pres was not the right solution. Sorry for I may have misled you.

In line 23 you are using list comprehension:
[x for x in pres[ind]]
This is actually a small loop iterating on the object pres[ind]
But as the error says, pres[ind] is not a list (not iterable), but a single item inside a list.
It is the item with the index ind inside the list pres

If you wish to retrieve time series results more efficiently, you can check out the module that allows access to the results straight from a binary output file created by EPANET

Thanks for your help.
Now I have rewritten my code shown below:

Finally, it returned the pressure results at the node at around 2000 timestamps, but actually, there are far more 1 million timestamps, could you please give me some advices based on my code? I am new to epanet-python, so I can’t figure out which step I did something wrong leading to not retrieve of the pressure results at the node at all timestamps.
Thank you in advance!