Tutorial 54: Measured Variable Tables
This example demonstrates how view, create, delete and edit measured variable tables (Sollwerttabellen) with pandas dataframes as python representation.
SIR 3S Installation
[1]:
SIR3S_SIRGRAF_DIR = r"C:\3S\SIR 3S\SirGraf-90-15-00-24_Quebec-Upd2" #change to local path
Imports
[2]:
from sir3stoolkit.core import wrapper
[3]:
from sir3stoolkit.mantle import mantle
The wrapper package has to be initialized with reference to a SIR 3S (SirGraf) installation.
[4]:
wrapper.Initialize_Toolkit(SIR3S_SIRGRAF_DIR)
Additional imports
[5]:
import pandas as pd
import numpy as np
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
…
Initialization
[6]:
s3s = mantle.SIR3S_Model_Mantle()
Initialization complete
[7]:
from sir3stoolkit.mantle import dataframes
s3s = dataframes.SIR3S_Model_Dataframes()
Initialization complete
Open Model
[8]:
dbFilePath=r"Toolkit_Tutorial54_Model.db3"
[9]:
s3s.OpenModel(dbName=dbFilePath,
providerType=s3s.ProviderTypes.SQLite,
Mid="M-1-0-1",
saveCurrentlyOpenModel=False,
namedInstance="",
userID="",
password="")
Model is open for further operation
Create new measured variable tables
Let’s insert Variable 1 as a placeholder example.
[10]:
s3s.GetPropertiesofElementType(ElementType=s3s.ObjectTypes.MeasuredVariableTable)
[10]:
['Name',
'Beschreibung',
'Zeitoption',
'Intpol',
'Idreferenz',
'Pk',
'InVariant']
[11]:
measured_variable_table_1 = s3s.InsertElement(ElementType=s3s.ObjectTypes.MeasuredVariableTable, IdRef="1")
Element inserted successfully into the model with Tk: 5106786640761474739
[12]:
s3s.SetValue(Tk=measured_variable_table_1, propertyName="Name", Value="Variable 1")
Value is set
[13]:
s3s.SetValue(Tk=measured_variable_table_1, propertyName="Zeitoption", Value="0") # We do not want special time reference, see general SIR 3S documentation for details
Value is set
[14]:
s3s.SetValue(Tk=measured_variable_table_1, propertyName="Intpol", Value="0") # We choose linear interpolation scheme. Use 9 for step function
Value is set
Insert values
W is the value that is given as a function of time W = f(t)
The sample data we provide is in ‘measured’ in constant time intervals and the values are random between 0 and 100.
[15]:
# Create time vector: 0 to 600 seconds, step 20
time_seconds = np.arange(0, 601, 20)
# Create variable values between 0 and 100
np.random.seed(42)
werte = np.random.rand(len(time_seconds)) * 100
YEAR = 2026
DATE = datetime(YEAR, 1, 1) # constant date for all entries
rows = []
for t, w in zip(time_seconds, werte):
timestamp = DATE + timedelta(seconds=int(t))
tag = timestamp.strftime("%Y-%m-%d")
uhrzeit = timestamp.strftime("%H:%M:%S.%f")
rows.append({
"TAG": tag,
"UHRZEIT": uhrzeit,
"WERT": w
})
# Build DataFrame
df = pd.DataFrame(rows, columns=["TAG", "UHRZEIT", "WERT"])
[16]:
df.head(3)
[16]:
| TAG | UHRZEIT | WERT | |
|---|---|---|---|
| 0 | 2026-01-01 | 00:00:00.000000 | 37.454012 |
| 1 | 2026-01-01 | 00:00:20.000000 | 95.071431 |
| 2 | 2026-01-01 | 00:00:40.000000 | 73.199394 |
We can use the insert_dataframe_into_time_table() method to insert the dataframe into the model as a measured variable table.
[17]:
s3s.insert_dataframe_into_time_table(time_table_tk=measured_variable_table_1, dataframe=df)
[2026-03-15 23:21:45,909] INFO in sir3stoolkit.mantle.dataframes: [insert dataframe into time table] Validating input data ...
[2026-03-15 23:21:46,062] INFO in sir3stoolkit.mantle.dataframes: [insert dataframe into time table] Successfully validated input data.
[2026-03-15 23:21:46,064] INFO in sir3stoolkit.mantle.dataframes: [insert dataframe into time table] Deleting previous table rows ...
[2026-03-15 23:21:46,088] INFO in sir3stoolkit.mantle.dataframes: [insert dataframe into time table] Inserting value pairs ...
[2026-03-15 23:21:46,150] INFO in sir3stoolkit.mantle.dataframes: [insert dataframe into time table] Successfully inserted value pairs
Row is added to the table with Tk: 5511155235703596569
Value is set
Value is set
Row is added to the table with Tk: 4825162945970759077
Value is set
Value is set
Row is added to the table with Tk: 5521553334084544950
Value is set
Value is set
Row is added to the table with Tk: 5525861603483762389
Value is set
Value is set
Row is added to the table with Tk: 4781997243709936648
Value is set
Value is set
Row is added to the table with Tk: 5156056649568580716
Value is set
Value is set
Row is added to the table with Tk: 4879667976440342748
Value is set
Value is set
Row is added to the table with Tk: 5757735004081879745
Value is set
Value is set
Row is added to the table with Tk: 5270771206606675534
Value is set
Value is set
Row is added to the table with Tk: 5356156136750266123
Value is set
Value is set
Row is added to the table with Tk: 5274865376625650628
Value is set
Value is set
Row is added to the table with Tk: 5643180560610670472
Value is set
Value is set
Row is added to the table with Tk: 4735710860099421525
Value is set
Value is set
Row is added to the table with Tk: 5242751510991005820
Value is set
Value is set
Row is added to the table with Tk: 5059165236983414066
Value is set
Value is set
Row is added to the table with Tk: 5207350801417098632
Value is set
Value is set
Row is added to the table with Tk: 5620761669190564881
Value is set
Value is set
Row is added to the table with Tk: 5398755750928181747
Value is set
Value is set
Row is added to the table with Tk: 5468273030384953951
Value is set
Value is set
Row is added to the table with Tk: 5378449128804989150
Value is set
Value is set
Row is added to the table with Tk: 4892813647782580457
Value is set
Value is set
Row is added to the table with Tk: 5386302469724656505
Value is set
Value is set
Row is added to the table with Tk: 4835333517786276925
Value is set
Value is set
Row is added to the table with Tk: 4912022863985683158
Value is set
Value is set
Row is added to the table with Tk: 5216101434651515658
Value is set
Value is set
Row is added to the table with Tk: 5106335853962282844
Value is set
Value is set
Row is added to the table with Tk: 5496823346782345808
Value is set
Value is set
Row is added to the table with Tk: 5418828972356076760
Value is set
Value is set
Row is added to the table with Tk: 4956836853293904667
Value is set
Value is set
Row is added to the table with Tk: 4655372675412091100
Value is set
Value is set
Row is added to the table with Tk: 5582807784106464947
Value is set
Value is set
View measured value tables
[18]:
def view_non_value_data_of_measured_variable_table(measured_variable_table_tk):
name = s3s.GetValue(Tk=measured_variable_table_tk, propertyName="Name")[0]
intpol = s3s.GetValue(Tk=measured_variable_table_tk, propertyName="Intpol")[0]
if intpol == "0":
intpol_clear = "linear"
elif intpol == "9":
intpol_clear = "step function"
else:
intpol_clear = "invalid intpol"
print(f"Tk: {measured_variable_table_tk}, Name: {name}, Interpolation scheme: {intpol_clear}")
We can use the get_dataframes_from_time_table_type() method to obtain a df containing all dfs of a certain time table type (eg. MeasuredVariableTable), a dict with all individual dfs and their tk keys.
[19]:
df_h, table_dfs, table_tks = s3s.get_dataframes_from_time_table_type("MeasuredVariableTable")
[2026-03-15 23:21:46,191] INFO in sir3stoolkit.mantle.dataframes: [get dataframes from time table type] ...
[2026-03-15 23:21:46,195] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=4690496339163833971)...
[2026-03-15 23:21:46,201] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,203] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,223] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,224] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=4775965132419647289)...
[2026-03-15 23:21:46,227] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,228] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,232] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,233] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=5721839716933060230)...
[2026-03-15 23:21:46,234] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,234] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,242] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,250] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=5106786640761474739)...
[2026-03-15 23:21:46,255] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,256] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,264] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,273] INFO in sir3stoolkit.mantle.dataframes: [get dataframes from time table type] Successful
0
[20]:
table_dfs[table_tks[0]].head(3)
[20]:
| Variable 2 | |
|---|---|
| Zeit [s] | |
| 0.0 | 0.0 |
| 18.0 | 1.0 |
| 22.0 | 0.0 |
[21]:
view_non_value_data_of_measured_variable_table(table_tks[0])
Tk: 4690496339163833971, Name: Variable 2, Interpolation scheme: step function
1
[22]:
table_dfs[table_tks[1]].head(3)
[22]:
| Variable 2 | |
|---|---|
| Zeit [s] | |
| 0.0 | 99999.0 |
| 1.0 | 99999.0 |
As can be seen this is some invalid duplicate, that can be deleted.
[23]:
table_tk_to_delete = table_tks[1]
2
[24]:
table_dfs[table_tks[2]].head(3)
[24]:
| Variable 3 | |
|---|---|
| Zeit [s] | |
| 0.0 | 1.0 |
| 20.0 | 35.0 |
| 40.0 | 57.0 |
[25]:
view_non_value_data_of_measured_variable_table(table_tks[2])
Tk: 5721839716933060230, Name: Variable 3, Interpolation scheme: linear
3
[26]:
table_dfs[table_tks[3]].head(3)
[26]:
| Variable 1 | |
|---|---|
| Zeit [s] | |
| 0.0 | 37.45401 |
| 20.0 | 95.07143 |
| 40.0 | 73.19939 |
[27]:
view_non_value_data_of_measured_variable_table(table_tks[3])
Tk: 5106786640761474739, Name: Variable 1, Interpolation scheme: linear
This is the table we created earlier, as can be seen, it worked out fine.
Delete measured variable table
We can delete the faulty duplicate measured variable table as any other element. The rows depending on it will be deleted as well.
[28]:
s3s.DeleteElement(Tk=str(table_tk_to_delete))
Element deleted successfully
[29]:
df_h, table_dfs, table_tks = s3s.get_dataframes_from_time_table_type("MeasuredVariableTable")
[2026-03-15 23:21:46,555] INFO in sir3stoolkit.mantle.dataframes: [get dataframes from time table type] ...
[2026-03-15 23:21:46,560] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=4690496339163833971)...
[2026-03-15 23:21:46,563] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,565] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,583] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,584] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=5721839716933060230)...
[2026-03-15 23:21:46,591] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,593] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,606] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,608] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Validating input data (time_table_tk=5106786640761474739)...
[2026-03-15 23:21:46,612] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully validated input data.
[2026-03-15 23:21:46,613] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Building dataframe ...
[2026-03-15 23:21:46,624] INFO in sir3stoolkit.mantle.dataframes: [get dataframe from time table] Successfully built dataframe.
[2026-03-15 23:21:46,628] INFO in sir3stoolkit.mantle.dataframes: [get dataframes from time table type] Successful
[30]:
len(table_dfs)
[30]:
3
Joint view
[31]:
df_h.head(10)
[31]:
| Variable 2 | Variable 3 | Variable 1 | |
|---|---|---|---|
| Zeit [s] | |||
| 0.0 | 0.0 | 1.0 | 37.45401 |
| 18.0 | 1.0 | NaN | NaN |
| 20.0 | NaN | 35.0 | 95.07143 |
| 22.0 | 0.0 | NaN | NaN |
| 39.0 | 1.0 | NaN | NaN |
| 40.0 | NaN | 57.0 | 73.19939 |
| 52.0 | 0.0 | NaN | NaN |
| 60.0 | NaN | 40.0 | 59.86585 |
| 62.0 | 1.0 | NaN | NaN |
| 64.0 | 0.0 | NaN | NaN |
[32]:
s3s.CloseModel(False)
[32]:
True