Skip to content

Commit 1844aff

Browse files
committed
added ctypesgen automated wrapping
1 parent 88e85c9 commit 1844aff

3 files changed

Lines changed: 2045 additions & 136 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# epanet-python
22
python wrapper for epanet library
33

4+
## epamodule
45
A Python module that permits Epanet toolkit library calls from pure Python programs.
6+
epanet2.py is generated from epanet2.h by ctypesgen
57

68
Example2.py is example2.c from toolkit help rewritten in Python as example of epamodule use.
9+
10+
## outbin
11+
Python module for reading EpanetBinaryOutputFiles in OO mode
12+
See help of class EpanetOutBin.
13+
At present it is a self contained pure Py module: a partial rewriting is planned
14+
in a near future for use of outputapi C library.

epamodule.py

Lines changed: 63 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import platform
88
import datetime
99

10-
_plat= platform.system()
10+
"""_plat= platform.system()
1111
if _plat=='Linux':
1212
_lib = ctypes.CDLL("libepanet.so.2")
1313
elif _plat=='Windows':
@@ -24,14 +24,17 @@
2424
raise Exception("epanet2.dll not suitable")
2525
2626
else:
27-
Exception('Platform '+ _plat +' unsupported (not yet)')
27+
Exception('Platform '+ _plat +' unsupported (not yet)')"""
28+
import epanet2 as _lib
2829

2930

3031
_current_simulation_time= ctypes.c_long()
3132

3233
_max_label_len= 32
34+
label = ctypes.create_string_buffer(_max_label_len)
35+
3336
_err_max_char= 80
34-
37+
errmsg= _lib.String(_err_max_char*"\0")
3538

3639

3740

@@ -87,7 +90,6 @@ def ENgetnodeid(index):
8790
8891
Arguments:
8992
index: node index"""
90-
label = ctypes.create_string_buffer(_max_label_len)
9193
ierr= _lib.ENgetnodeid(index, ctypes.byref(label))
9294
if ierr!=0: raise ENtoolkitError(ierr)
9395
return label.value
@@ -161,10 +163,9 @@ def ENgetlinkid(index):
161163
162164
Arguments:
163165
index: link index"""
164-
label = ctypes.create_string_buffer(_max_label_len)
165-
ierr= _lib.ENgetlinkid(index, ctypes.byref(label))
166+
ierr= _lib.ENgetlinkid(index, label)
166167
if ierr!=0: raise ENtoolkitError(ierr)
167-
return label.value
168+
return str(label)
168169

169170

170171
def ENgetlinktype(index):
@@ -221,10 +222,9 @@ def ENgetpatternid(index):
221222
222223
Arguments:
223224
index: pattern index"""
224-
label = ctypes.create_string_buffer(_max_label_len)
225-
ierr= _lib.ENgetpatternid(index, ctypes.byref(label))
225+
ierr= _lib.ENgetpatternid(index, label)
226226
if ierr!=0: raise ENtoolkitError(ierr)
227-
return label.value
227+
return str(label)
228228

229229
def ENgetpatternindex(patternid):
230230
"""Retrieves the index of a particular time pattern.
@@ -706,9 +706,8 @@ def ENsetstatusreport(statuslevel):
706706

707707
def ENgeterror(errcode):
708708
"""Retrieves the text of the message associated with a particular error or warning code."""
709-
errmsg= ctypes.create_string_buffer(_err_max_char)
710-
_lib.ENgeterror( errcode,ctypes.byref(errmsg), _err_max_char )
711-
return errmsg.value
709+
_lib.ENgeterror( errcode, errmsg , _err_max_char )
710+
return str(errmsg)
712711

713712
def ENwriteline(line ):
714713
"""Writes a line of text to the EPANET report file."""
@@ -733,12 +732,11 @@ def __str__(self):
733732
#----------------------------------------------------------------------------------
734733
if hasattr(_lib,"ENgetcurve"):
735734
def ENgetcurve(curveIndex):
736-
curveid = ctypes.create_string_buffer(_max_label_len)
737735
nValues = ctypes.c_int()
738736
xValues= ctypes.POINTER(ctypes.c_float)()
739737
yValues= ctypes.POINTER(ctypes.c_float)()
740738
ierr= _lib.ENgetcurve(curveIndex,
741-
ctypes.byref(curveid),
739+
label,
742740
ctypes.byref(nValues),
743741
ctypes.byref(xValues),
744742
ctypes.byref(yValues)
@@ -752,142 +750,71 @@ def ENgetcurve(curveIndex):
752750
return curve
753751

754752
def ENgetcurveid(curveIndex):
755-
curveid = ctypes.create_string_buffer(_max_label_len)
756753
nValues = ctypes.c_int()
757754
xValues= ctypes.POINTER(ctypes.c_float)()
758755
yValues= ctypes.POINTER(ctypes.c_float)()
759756
ierr= _lib.ENgetcurve(curveIndex,
760-
ctypes.byref(curveid),
757+
label,
761758
ctypes.byref(nValues),
762759
ctypes.byref(xValues),
763760
ctypes.byref(yValues)
764761
)
765762
# strange behavior of ENgetcurve: it returns also curveID
766763
# better split in two distinct functions ....
767764
if ierr!=0: raise ENtoolkitError(ierr)
768-
return curveid.value
765+
return str(label)
769766

770767
#-----end of functions added from OpenWaterAnalytics ----------------------------------
771768

769+
# /* Node parameters */
770+
from epanet2 import EN_ELEVATION, EN_BASEDEMAND, EN_PATTERN, EN_EMITTER, EN_INITQUAL, EN_SOURCEQUAL
771+
from epanet2 import EN_SOURCEPAT,EN_SOURCETYPE,EN_TANKLEVEL, EN_DEMAND, EN_HEAD, EN_PRESSURE
772+
from epanet2 import EN_QUALITY, EN_SOURCEMASS, EN_INITVOLUME, EN_MIXMODEL, EN_MIXZONEVOL
773+
774+
from epanet2 import EN_TANKDIAM,EN_MINVOLUME,EN_VOLCURVE,EN_MINLEVEL,EN_MAXLEVEL,EN_MIXFRACTION,EN_TANK_KBULK
775+
776+
# /* Link parameters */
777+
from epanet2 import EN_DIAMETER,EN_LENGTH,EN_ROUGHNESS,EN_MINORLOSS,EN_INITSTATUS,EN_INITSETTING
778+
from epanet2 import EN_KBULK,EN_KWALL,EN_FLOW,EN_VELOCITY,EN_HEADLOSS,EN_STATUS,EN_SETTING,EN_ENERGY
779+
780+
# /* Time parameters */
781+
from epanet2 import EN_DURATION,EN_HYDSTEP,EN_QUALSTEP,EN_PATTERNSTEP,EN_PATTERNSTART
782+
from epanet2 import EN_REPORTSTEP,EN_REPORTSTART,EN_RULESTEP,EN_STATISTIC,EN_PERIODS
783+
784+
# /* Component counts */
785+
from epanet2 import EN_NODECOUNT,EN_TANKCOUNT,EN_LINKCOUNT,EN_PATCOUNT,EN_CURVECOUNT,EN_CONTROLCOUNT
786+
787+
# /* Node types */
788+
from epanet2 import EN_JUNCTION,EN_RESERVOIR,EN_TANK
789+
790+
# /* Link types */
791+
from epanet2 import EN_CVPIPE,EN_PIPE,EN_PUMP,EN_PRV,EN_PSV,EN_PBV,EN_FCV,EN_TCV,EN_GPV
792+
793+
# /* Quality analysis types */
794+
from epanet2 import EN_NONE,EN_CHEM,EN_AGE,EN_TRACE
795+
796+
# /* Source quality types */
797+
from epanet2 import EN_CONCEN,EN_MASS,EN_SETPOINT,EN_FLOWPACED
798+
799+
# /* Flow units types */
800+
from epanet2 import EN_CFS,EN_GPM,EN_MGD,EN_IMGD,EN_AFD,EN_LPS,EN_LPM,EN_MLD,EN_CMH,EN_CMD
801+
802+
# /* Misc. options */
803+
from epanet2 import EN_TRIALS,EN_ACCURACY,EN_TOLERANCE,EN_EMITEXPON,EN_DEMANDMULT
804+
805+
# /* Control types */
806+
from epanet2 import EN_LOWLEVEL,EN_HILEVEL,EN_TIMER,EN_TIMEOFDAY
807+
808+
# /* Time statistic types. */
809+
from epanet2 import EN_AVERAGE,EN_MINIMUM,EN_MAXIMUM,EN_RANGE
810+
811+
# /* Tank mixing models */
812+
from epanet2 import EN_MIX1,EN_MIX2,EN_FIFO,EN_LIFO
772813

773-
EN_ELEVATION = 0 # /* Node parameters */
774-
EN_BASEDEMAND = 1
775-
EN_PATTERN = 2
776-
EN_EMITTER = 3
777-
EN_INITQUAL = 4
778-
EN_SOURCEQUAL = 5
779-
EN_SOURCEPAT = 6
780-
EN_SOURCETYPE = 7
781-
EN_TANKLEVEL = 8
782-
EN_DEMAND = 9
783-
EN_HEAD = 10
784-
EN_PRESSURE = 11
785-
EN_QUALITY = 12
786-
EN_SOURCEMASS = 13
787-
EN_INITVOLUME = 14
788-
EN_MIXMODEL = 15
789-
EN_MIXZONEVOL = 16
790-
791-
EN_TANKDIAM = 17
792-
EN_MINVOLUME = 18
793-
EN_VOLCURVE = 19
794-
EN_MINLEVEL = 20
795-
EN_MAXLEVEL = 21
796-
EN_MIXFRACTION = 22
797-
EN_TANK_KBULK = 23
798-
799-
EN_DIAMETER = 0 # /* Link parameters */
800-
EN_LENGTH = 1
801-
EN_ROUGHNESS = 2
802-
EN_MINORLOSS = 3
803-
EN_INITSTATUS = 4
804-
EN_INITSETTING = 5
805-
EN_KBULK = 6
806-
EN_KWALL = 7
807-
EN_FLOW = 8
808-
EN_VELOCITY = 9
809-
EN_HEADLOSS = 10
810-
EN_STATUS = 11
811-
EN_SETTING = 12
812-
EN_ENERGY = 13
813-
814-
EN_DURATION = 0 # /* Time parameters */
815-
EN_HYDSTEP = 1
816-
EN_QUALSTEP = 2
817-
EN_PATTERNSTEP = 3
818-
EN_PATTERNSTART = 4
819-
EN_REPORTSTEP = 5
820-
EN_REPORTSTART = 6
821-
EN_RULESTEP = 7
822-
EN_STATISTIC = 8
823-
EN_PERIODS = 9
824-
825-
EN_NODECOUNT = 0 # /* Component counts */
826-
EN_TANKCOUNT = 1
827-
EN_LINKCOUNT = 2
828-
EN_PATCOUNT = 3
829-
EN_CURVECOUNT = 4
830-
EN_CONTROLCOUNT = 5
831-
832-
EN_JUNCTION = 0 # /* Node types */
833-
EN_RESERVOIR = 1
834-
EN_TANK = 2
835-
836-
EN_CVPIPE = 0 # /* Link types */
837-
EN_PIPE = 1
838-
EN_PUMP = 2
839-
EN_PRV = 3
840-
EN_PSV = 4
841-
EN_PBV = 5
842-
EN_FCV = 6
843-
EN_TCV = 7
844-
EN_GPV = 8
845-
846-
EN_NONE = 0 # /* Quality analysis types */
847-
EN_CHEM = 1
848-
EN_AGE = 2
849-
EN_TRACE = 3
850-
851-
EN_CONCEN = 0 # /* Source quality types */
852-
EN_MASS = 1
853-
EN_SETPOINT = 2
854-
EN_FLOWPACED = 3
855-
856-
EN_CFS = 0 # /* Flow units types */
857-
EN_GPM = 1
858-
EN_MGD = 2
859-
EN_IMGD = 3
860-
EN_AFD = 4
861-
EN_LPS = 5
862-
EN_LPM = 6
863-
EN_MLD = 7
864-
EN_CMH = 8
865-
EN_CMD = 9
866-
867-
EN_TRIALS = 0 # /* Misc. options */
868-
EN_ACCURACY = 1
869-
EN_TOLERANCE = 2
870-
EN_EMITEXPON = 3
871-
EN_DEMANDMULT = 4
872-
873-
EN_LOWLEVEL = 0 # /* Control types */
874-
EN_HILEVEL = 1
875-
EN_TIMER = 2
876-
EN_TIMEOFDAY = 3
877-
878-
EN_AVERAGE = 1 # /* Time statistic types. */
879-
EN_MINIMUM = 2
880-
EN_MAXIMUM = 3
881-
EN_RANGE = 4
882-
883-
EN_MIX1 = 0 # /* Tank mixing models */
884-
EN_MIX2 = 1
885-
EN_FIFO = 2
886-
EN_LIFO = 3
887-
888-
EN_NOSAVE = 0 # /* Save-results-to-file flag */
889-
EN_SAVE = 1
890-
EN_INITFLOW = 10 # /* Re-initialize flow flag */
814+
# /* Save-results-to-file flag */
815+
from epanet2 import EN_NOSAVE,EN_SAVE
816+
# /* Re-initialize flow flag */
817+
from epanet2 import EN_INITFLOW
891818

892819

893820

0 commit comments

Comments
 (0)