Tutorial 6: Work with Tables
This Tutorial demonstrates how
SIR 3S Installation
[4]:
SIR3S_SIRGRAF_DIR = r"C:\3S\SIR 3S\SirGraf-90-15-00-21_Quebec-Upd2" #change to local path
Imports
Note: The SIR 3S Toolkit requires the Sir3S_Toolkit.dll included in SIR 3S installations (version Quebec and higher).
[5]:
import sir3stoolkit
The core of sir3stoolkit is a Python wrapper around basic functionality of SIR 3S, offering a low-level access to the creation, modification and simulation of SIR 3S models. In the future pure python subpackages may be added.
[6]:
from sir3stoolkit.core import wrapper
[7]:
sir3stoolkit
[7]:
<module 'sir3stoolkit' from 'C:\\Users\\aUsername\\3S\\sir3stoolkit\\src\\sir3stoolkit\\__init__.py'>
The wrapper package has to be initialized with reference to a SIR 3S (SirGraf) installation.
[8]:
wrapper.Initialize_Toolkit(SIR3S_SIRGRAF_DIR)
Initialization
The SIR 3S Toolkit contains two classes: SIR3S_Model (model and data) and SIR3S_View (depiction in SIR Graf). All SIR 3S Toolkit functionality is accessed via the methods of these classes.
[9]:
s3s = wrapper.SIR3S_Model()
Initialization complete
[10]:
s3s_view = wrapper.SIR3S_View()
Initialization complete
Open Model
[11]:
dbFilePath=r"C:\Users\aUsername\3S\PT3S\PT3S\Examples\Example5.db3"
[12]:
dbFilePath=r"Toolkit_Tutorial6_Model.db3"
[13]:
s3s.OpenModel(dbName=dbFilePath,
providerType=s3s.ProviderTypes.SQLite,
Mid="M-1-0-1",
saveCurrentlyOpenModel=False,
namedInstance="",
userID="",
password="")
Model is open for further operation
[14]:
print(s3s.GetNetworkType()) # to check that the correct model is responsive, model we are trying to open was created with type Undefined
NetworkType.Water
GetTableRows()
We can use the GetTableRows() function to obtain the tks of rows belonging to the table with a given tk.
[15]:
for tk in s3s.GetTksofElementType(s3s.ObjectTypes.PipeTable):
print(tk + ": " + s3s.GetValue(tk, "Name")[0])
5464993648735182081: STDROHR
5084212600034677044: AZ_PN10
4805611686096369081: StB_PN20
5266199647394782751: GUSS_K8
5015394485292264252: GUSS_K8Z
4879264831531644476: DN2460G
4702669660282998254: DN2460GZ
5584209084918079490: ST-V4
4747010066358051293: GFKUPGFPN10
5497475942106574922: PE100SDR11
4947305676931088023: PE100SDR17
5544640728947443469: PP_PN10
4740109886480699434: PVCPN10A
4743437019149053639: PVCPN10T
Let’s focus on the STDROHR table
[16]:
tk_STDROHR_table = "5464993648735182081"
[17]:
rows = s3s.GetTableRows(tablePkTk=tk_STDROHR_table)
[18]:
rows[0]
[18]:
<System.String[] object at 0x000001F2FED0D800>
We get a .NET array of strings returned, we need to transform it into a python list.
[19]:
row_tks = list(rows[0])
Now, we have the tks of all rows of this table.
We can reconstruct the table via pandas as follows.
[20]:
import pandas as pd
[21]:
properties_rows=s3s.GetPropertiesofElementType(s3s.ObjectTypes.PipeTable_Row)
[22]:
data = {
tk: {prop: s3s.GetValue(tk, prop)[0] for prop in properties_rows}
for tk in row_tks
}
[23]:
df = pd.DataFrame.from_dict(data, orient='index')
[24]:
df.head(5)
[24]:
| Name | Fk | Dn | Di | Da | S | Wsteig | Wtiefe | Kt | Pn | Ausfallzeit | Reparatur | Rehabilitation | Tk | Pk | InVariant | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4763523255060525502 | DN 3 | 5464993648735182081 | 3 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4763523255060525502 | 4763523255060525502 | False |
| 4985118461854533500 | DN 4 | 5464993648735182081 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4985118461854533500 | 4985118461854533500 | False |
| 5240628392234716908 | DN 5 | 5464993648735182081 | 5 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5240628392234716908 | 5240628392234716908 | False |
| 5735560888842799373 | DN 6 | 5464993648735182081 | 6 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5735560888842799373 | 5735560888842799373 | False |
| 4891609214500630565 | DN 8 | 5464993648735182081 | 8 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4891609214500630565 | 4891609214500630565 | False |
AddTableRow()
We can use the AddTableRow() function to add a new row to the bottom of any table.
[25]:
new_table_row_tk = s3s.AddTableRow(tk_STDROHR_table)[0]
Row is added to the table with Tk: 5510330002633575350
Now we can use the already known SetValue() function to propagate the row with the necessary values.
[26]:
s3s.GetPropertiesofElementType(s3s.ObjectTypes.PipeTable_Row)
[26]:
['Name',
'Fk',
'Dn',
'Di',
'Da',
'S',
'Wsteig',
'Wtiefe',
'Kt',
'Pn',
'Ausfallzeit',
'Reparatur',
'Rehabilitation',
'Tk',
'Pk',
'InVariant']
[27]:
s3s.SetValue(new_table_row_tk, "Name", "DN 60")
Value is set
[28]:
s3s.SetValue(new_table_row_tk, "Fk", "5207173582970272729")
Value is set
[29]:
s3s.SetValue(new_table_row_tk, "Dn", "60")
Value is set
… and so on. Then save your changes.
[30]:
#s3s.SaveChanges()