From 9eb18412770ededa8851a28d91c6adb6e9a23f90 Mon Sep 17 00:00:00 2001
From: Markus Mirz <mmirz@eonerc.rwth-aachen.de>
Date: Mon, 13 Aug 2018 12:48:46 +0200
Subject: [PATCH] addded new dpsim read function for real and imaginary vars

---
 dataprocessing/readtools.py | 54 +++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/dataprocessing/readtools.py b/dataprocessing/readtools.py
index e33c462..6c17927 100644
--- a/dataprocessing/readtools.py
+++ b/dataprocessing/readtools.py
@@ -181,3 +181,57 @@ def read_timeseries_dpsim_cmpl_separate(filename, timeseries_names=None):
     for result in timeseries_list:
         print(result.name)
     return timeseries_list
+
+def read_timeseries_dpsim(filename, timeseries_names=None):
+    """Reads complex time series data from DPsim log file. Real and
+    imaginary part are stored in one complex variable.
+    :param filename: name of the csv file that has the data
+    :param timeseries_names: column name which should be read
+    :return: list of Timeseries objects
+    """
+    pd_df = pd.read_csv(filename)
+    timeseries_list = []
+    cmpl_result_columns = []
+    real_result_columns = []
+
+    if timeseries_names is None:
+        # No column names specified, thus read in all and strip off 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]
+
+        # Find real and complex variable names
+        real_string = '_re'
+        imaginary_string = '_im'
+        for column in column_names:
+            if real_string in column:
+                tmp = column.replace(real_string, '')
+                cmpl_result_columns.append(tmp)
+                #print("Found complex variable: " + tmp)
+            elif not imaginary_string in column:
+                real_result_columns.append(column)
+                #print("Found real variable: " + column)
+ 
+        for column in cmpl_result_columns:                
+            timeseries_list.append(
+                TimeSeries(column, timestamps, 
+                    np.vectorize(complex)(pd_df[column + real_string], 
+                    pd_df[column + imaginary_string])))
+        
+        for column in real_result_columns:                
+            timeseries_list.append(
+                TimeSeries(column, timestamps, pd_df[column]))
+           
+    else:
+        # Read in specified time series
+        print('cannot read specified columns yet')
+
+    print('DPsim results real column names: ' + str(real_result_columns))
+    print('DPsim results complex column names: ' + str(cmpl_result_columns))
+    print('DPsim results variable number: ' + str(len(timeseries_list)))
+    print('DPsim results length: ' + str(len(timestamps)))
+
+    return timeseries_list
-- 
GitLab