From 65789370e04c326eb6cb75f74d07f7d176d2af05 Mon Sep 17 00:00:00 2001
From: Jan Dinkelbach <jdinkelbach@eonerc.rwth-aachen.de>
Date: Thu, 7 Dec 2017 16:52:11 +0100
Subject: [PATCH] extended modelica read in (use of regex, all vars), added
 read in examples

---
 dataprocessing/readtools.py      | 19 +++++++++++---
 dataprocessing/timeseries.py     |  9 +++++--
 examples/ExamplesReadModelica.py | 45 ++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 6 deletions(-)
 create mode 100644 examples/ExamplesReadModelica.py

diff --git a/dataprocessing/readtools.py b/dataprocessing/readtools.py
index 9e0d6c2..aec32db 100644
--- a/dataprocessing/readtools.py
+++ b/dataprocessing/readtools.py
@@ -1,14 +1,25 @@
 import numpy as np
 import pandas as pd
 from .timeseries import *
+import re
 
 
-def read_timeseries_Modelica(filename, timeseries_names=None):
+def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False):
     from modelicares import SimRes
     sim = SimRes(filename)
-    if timeseries_names is None:
-        # No trajectory names specified, thus read in all
-        print('TBD')
+    if timeseries_names is None and is_regex is False:
+        # No trajectory names or regex specified, thus read in all
+        timeseries = []
+        for name in sim.names():
+            timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
+    elif is_regex is True:
+        # Read in variables which match with regex
+        timeseries = []
+        p = re.compile(timeseries_names)
+        timeseries_names = [name for name in sim.names() if p.search(name)]
+        timeseries_names.sort()
+        for name in timeseries_names:
+            timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
     else:
         # Read in specified time series
         if not isinstance(timeseries_names, list):
diff --git a/dataprocessing/timeseries.py b/dataprocessing/timeseries.py
index 7115f4c..83b979a 100644
--- a/dataprocessing/timeseries.py
+++ b/dataprocessing/timeseries.py
@@ -14,9 +14,14 @@ class TimeSeries:
     @staticmethod
     def diff(name, ts1, ts2):
         """Returns difference between values of two Timeseries objects.
-        Assumes the same time steps for both timeseries.
         """
-        ts_diff = TimeSeries(name, ts1.time, (ts1.values - ts2.values))
+        if ts1.time==ts2.time:
+            ts_diff = TimeSeries(name, ts1.time, (ts1.values - ts2.values))
+        else:  # different timestamps, common time vector and interpolation required before substraction
+            time = sorted(set(list(ts1.time) + list(ts2.time)))
+            interp_vals_ts1 = np.interp(time, ts1.time, ts1.values)
+            interp_vals_ts2 = np.interp(time, ts2.time, ts2.values)
+            ts_diff = TimeSeries(name, time, (interp_vals_ts2 - interp_vals_ts1))
         return ts_diff
 
 
diff --git a/examples/ExamplesReadModelica.py b/examples/ExamplesReadModelica.py
new file mode 100644
index 0000000..eab9a32
--- /dev/null
+++ b/examples/ExamplesReadModelica.py
@@ -0,0 +1,45 @@
+from dataprocessing.readtools import *
+from dataprocessing.plottools import *
+import matplotlib.pyplot as plt
+
+
+# Example 1: read in single variable included in the Modelica results file
+voltage_node126 = read_timeseries_Modelica(
+    r"\\tsclient\N\Research\German Public\ACS0049_SINERGIEN_bsc\Data\WorkData\SimulationResults\IEEE European\Single_scenario_fixed_PV\IEEEEuropean_60.mat",
+    timeseries_names="N126.Vrel")
+plt.figure(1, figsize=(12,8))
+set_timeseries_labels(voltage_node126, "voltage N126")
+plt.plot(voltage_node126.time/3600, voltage_node126.values, label=voltage_node126.label)
+plt.legend()
+plt.show(block=True)
+
+# Example 2: read in multiple variables defined in a list
+voltage_two_nodes = read_timeseries_Modelica(
+    r"\\tsclient\N\Research\German Public\ACS0049_SINERGIEN_bsc\Data\WorkData\SimulationResults\IEEE European\Single_scenario_fixed_PV\IEEEEuropean_60.mat",
+    timeseries_names=["N127.Vrel", "N128.Vrel"])
+plt.figure(2, figsize=(12,8))
+plt.plot(voltage_two_nodes[0].time/3600, voltage_two_nodes[0].values, label=voltage_two_nodes[0].label)
+plt.plot(voltage_two_nodes[1].time/3600, voltage_two_nodes[1].values, label=voltage_two_nodes[1].label)
+plt.legend()
+plt.show(block=True)
+
+# Example 3: read in all voltages using regular expressions
+voltages_all_nodes = read_timeseries_Modelica(
+    r"\\tsclient\N\Research\German Public\ACS0049_SINERGIEN_bsc\Data\WorkData\SimulationResults\IEEE European\Single_scenario_fixed_PV\IEEEEuropean_60.mat",
+    timeseries_names='^[^.]*.Vrel$', is_regex=True)
+plt.figure(3, figsize=(12, 8))
+for i in range(len(voltages_all_nodes)):
+    plt.plot(voltages_all_nodes[i].time / 3600, voltages_all_nodes[i].values, label=voltages_all_nodes[i].label)
+plt.legend()
+plt.show(block=True)
+
+# Example 4: read in all variables
+variables_all = read_timeseries_Modelica(
+    r"\\tsclient\N\Research\German Public\ACS0049_SINERGIEN_bsc\Data\WorkData\SimulationResults\IEEE European\Single_scenario_fixed_PV\IEEEEuropean_60.mat")
+dict_variables_all = {}
+for ts in variables_all:
+    dict_variables_all[ts.name] = ts
+plt.figure(4, figsize=(12, 8))
+plt.plot(dict_variables_all["L12.Irel"].time/3600, dict_variables_all["L12.Irel"].values, label=dict_variables_all["L12.Irel"].label)
+plt.legend()
+plt.show(block=True)
\ No newline at end of file
-- 
GitLab