From 3081e56e570106512c8bdb865e1ed9b02dfe81d5 Mon Sep 17 00:00:00 2001
From: Jan Dinkelbach <jdinkelbach@eonerc.rwth-aachen.de>
Date: Thu, 1 Feb 2018 15:14:36 +0100
Subject: [PATCH] allow for read in of specific columns in csv data

---
 dataprocessing/readtools.py                   | 46 ++++++++++---------
 .../compare_modelica_distaix.py               | 27 +++++++++++
 examples/Distaix/read_distaix_examples.py     | 13 ++++++
 3 files changed, 65 insertions(+), 21 deletions(-)
 create mode 100644 examples/CompareResults/compare_modelica_distaix.py
 create mode 100644 examples/Distaix/read_distaix_examples.py

diff --git a/dataprocessing/readtools.py b/dataprocessing/readtools.py
index b7d5c64..1175903 100644
--- a/dataprocessing/readtools.py
+++ b/dataprocessing/readtools.py
@@ -30,7 +30,7 @@ def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False):
                 timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
 
     print('Modelica results column names: ' + str(timeseries_names))
-    print('Modelica results number: ' + str(len(timeseries_list)))
+    print('Modelica results number: ' + str(len(timeseries_names)))
 
     return timeseries
 
@@ -54,6 +54,7 @@ def read_timeseries_PLECS(filename, timeseries_names=None):
 
     return timeseries_list
 
+
 def read_timeseries_dpsim_real(filename, timeseries_names=None):
     """Reads real time series data from DPsim log file which may have a header.
     Timeseries names are assigned according to the header names if available.
@@ -67,23 +68,25 @@ def read_timeseries_dpsim_real(filename, timeseries_names=None):
     if timeseries_names is None:
         # No column names specified, thus read in all and strip spaces
         pd_df.rename(columns=lambda x: x.strip(), inplace=True)
-        column_names = list(pd_df.columns.values)
-
-        # Remove timestamps column name and store separately
-        column_names.remove('time')
-        timestamps = pd_df.iloc[:,0]
-
-        for name in column_names:
-            timeseries_list.append(TimeSeries(name, timestamps, pd_df[name].values))
-    else:
-        # Read in specified time series
-        print('no column names specified yet')
-
-    print('DPsim results column names: ' + str(column_names))
+        timeseries_names = list(pd_df.columns.values)
+        timeseries_names.remove('time')
+    #else:
+    #    # Read in specified column names
+    #    pd_df = pd.read_csv(filename, names=timeseries_names)
+
+    # store columns of interest in list of timeseries
+    # note: timestamps must be given in first column of csv file
+    timestamps = pd_df.iloc[:, 0]
+    for name in timeseries_names:
+        timeseries_list.append(TimeSeries(name, timestamps, pd_df[name].values))
+
+    print('DPsim results column names: ' + str(timeseries_names))
     print('DPsim results number: ' + str(len(timeseries_list)))
+    print('DPsim results timestamps number: ' + str(len(timestamps)))
 
     return timeseries_list
 
+
 def read_timeseries_dpsim_cmpl(filename, timeseries_names=None):
     """Reads complex time series data from DPsim log file. Real and
     imaginary part are stored in one complex variable.
@@ -101,15 +104,15 @@ def read_timeseries_dpsim_cmpl(filename, timeseries_names=None):
 
         # Remove timestamps column name and store separately
         column_names.remove('time')
-        timestamps = pd_df.iloc[:,0]
+        timestamps = pd_df.iloc[:, 0]
         # Calculate number of network nodes since array is [real, imag]
         node_number = int(len(column_names) / 2)
         node_index = 1
         for column in column_names:
             if node_index <= node_number:
-                ts_name = 'n'+ str(node_index)
+                ts_name = 'n' + str(node_index)
                 timeseries_list.append(
-                TimeSeries(ts_name, timestamps, np.vectorize(complex)(pd_df.iloc[:,node_index],pd_df.iloc[:,node_index + node_number])))
+                    TimeSeries(ts_name, timestamps, np.vectorize(complex)(pd_df.iloc[:, node_index], pd_df.iloc[:, node_index + node_number])))
             else:
                 break
             node_index = node_index + 1
@@ -122,6 +125,7 @@ def read_timeseries_dpsim_cmpl(filename, timeseries_names=None):
 
     return timeseries_list
 
+
 def read_timeseries_dpsim_cmpl_separate(filename, timeseries_names=None):
     """Deprecated - Reads complex time series data from DPsim log file. Real and
     imaginary part are stored separately.
@@ -143,11 +147,11 @@ def read_timeseries_dpsim_cmpl_separate(filename, timeseries_names=None):
         node_index = 1
         for column in column_names:
             if node_index <= node_number:
-                node_name = 'node '+ str(node_index) +' Re'
-                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:,column]))
+                node_name = 'node ' + str(node_index) + ' Re'
+                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:, column]))
             else:
-                node_name = 'node '+ str(node_index - node_number) +' Im'
-                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:,column]))
+                node_name = 'node ' + str(node_index - node_number) + ' Im'
+                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:, column]))
 
             node_index = node_index + 1
     else:
diff --git a/examples/CompareResults/compare_modelica_distaix.py b/examples/CompareResults/compare_modelica_distaix.py
new file mode 100644
index 0000000..ead439a
--- /dev/null
+++ b/examples/CompareResults/compare_modelica_distaix.py
@@ -0,0 +1,27 @@
+from dataprocessing.readtools import *
+from dataprocessing.plottools import *
+import matplotlib.pyplot as plt
+from plottingtools.config import *
+import numpy as np
+
+# Comparison of P, Q and delta for 3rd order Synchronous Generator
+syngen_modelica_1us = read_timeseries_Modelica(
+    r"\\tsclient\N\Research\German Public\ACS0049_SINERGIEN_bsc\Data\WorkData\SimulationResults\SynchronousGenerator\DP\Modelica\SinglePhase\SMIB_3rdOrderModel_PmStep_ThetaVolt0_Euler_1us.mat",
+    timeseries_names=["synchronousGenerator_Park.P", "synchronousGenerator_Park.Q", "synchronousGenerator_Park.delta", "synchronousGenerator_Park.i.re", "synchronousGenerator_Park.i.im"])
+syngen_distaix = read_timeseries_dpsim_real(
+    r"\\tsclient\N\Research\German Public\ACS0050_Swarmgrid_tis\Data\WorkData\AP5\simulation-results\distaix_syngen_power_step\10ms\agent_3.csv",
+    timeseries_names=["P [W]", "Q [var]", "delta [rad]", "i.re [A]", "i.im [A]"])
+
+num_vars = 5
+subplot_title_list = ["P [W]\n", "Q [var]\n", "delta [rad]\n", "i.re [A]", "i.im [A]"]
+plt.figure(1, figsize=(12, 8))
+for i in range(num_vars):
+    plt.subplot(num_vars, 1, i + 1)
+    set_timeseries_labels(syngen_modelica_1us, ["Modelica 1us", "Modelica 1us", "Modelica 1us", "Modelica 1us", "Modelica 1us"])
+    plt.plot(syngen_modelica_1us[i].time, syngen_modelica_1us[i].values, label=syngen_modelica_1us[i].label)
+    set_timeseries_labels(syngen_distaix, ["DistAIX 10ms", "DistAIX 10ms", "DistAIX 10ms", "DistAIX 10ms", "DistAIX 10ms"])
+    plt.plot(syngen_distaix[i].time, syngen_distaix[i].values, label=syngen_distaix[i].label, linestyle=':')
+    plt.legend()
+    plt.xlim([0, 30])
+    plt.ylabel(subplot_title_list[i])
+plt.show(block=True)
diff --git a/examples/Distaix/read_distaix_examples.py b/examples/Distaix/read_distaix_examples.py
new file mode 100644
index 0000000..f10131e
--- /dev/null
+++ b/examples/Distaix/read_distaix_examples.py
@@ -0,0 +1,13 @@
+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
+agent3_i_re = read_timeseries_dpsim_real(
+    r"\\tsclient\N\Research\German Public\ACS0050_Swarmgrid_tis\Data\WorkData\AP5\simulation-results\distaix_syngen_power_const\agent_3.csv",
+    timeseries_names=["i.re [A]"])
+plt.figure(1, figsize=(12, 8))
+set_timeseries_labels(agent3_i_re[0], "Agent 1 Ireal")
+plt.plot(agent3_i_re[0].time, agent3_i_re[0].values, label=agent3_i_re[0].label)
+plt.legend()
+plt.show(block=True)
-- 
GitLab