readtools.py 6.12 KB
Newer Older
1
2
import numpy as np
import pandas as pd
3
from .timeseries import *
4
import re
5

6

7
def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False):
8
    from modelicares import SimRes
9
    sim = SimRes(filename)
10
11
12
13
14
15
16
17
18
19
20
21
22
    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()))
23
24
    else:
        # Read in specified time series
25
26
27
        if not isinstance(timeseries_names, list):
            timeseries = TimeSeries(timeseries_names, sim(timeseries_names).times(), sim(timeseries_names).values())
        else:
Markus Mirz's avatar
Markus Mirz committed
28
            timeseries = []
29
30
            for name in timeseries_names:
                timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
31
32

    print('Modelica results column names: ' + str(timeseries_names))
33
    print('Modelica results number: ' + str(len(timeseries_names)))
34

35
    return timeseries
36

37

38
39
40
41
42
43
44
45
46
47
48
49
50
def read_timeseries_PLECS(filename, timeseries_names=None):
    pd_df = pd.read_csv(filename)
    timeseries_list = []
    if timeseries_names is None:
        # No trajectory names specified, thus read in all
        timeseries_names = list(pd_df.columns.values)
        timeseries_names.remove('Time')
        for name in timeseries_names:
            timeseries_list.append(TimeSeries(name, pd_df['Time'].values, pd_df[name].values))
    else:
        # Read in specified time series
        for name in timeseries_names:
            timeseries_list.append(TimeSeries(name, pd_df['Time'].values, pd_df[name].values))
51
52
53
54

    print('PLECS results column names: ' + str(timeseries_names))
    print('PLECS results number: ' + str(len(timeseries_list)))

55
    return timeseries_list
56

Markus Mirz's avatar
Markus Mirz committed
57
58
def read_timeseries_simulink(filename, timeseries_names=None):
    pd_df = pd.read_csv(filename)
59
60
61
62
    timeseries_list = {}
    cmpl_result_columns = []
    real_result_columns = []

Markus Mirz's avatar
Markus Mirz committed
63
    if timeseries_names is None:
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        # 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 = '.real'
        imaginary_string = '.imag'
        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)
82
83
84
                #print("Found real variable: " + column)

        for column in real_result_columns:
85
86
            timeseries_list[column] = TimeSeries(column, timestamps, pd_df[column])

87
88
89
        for column in cmpl_result_columns:
            timeseries_list[column] = TimeSeries(column, timestamps,
                np.vectorize(complex)(pd_df[column + real_string],
90
                pd_df[column + imaginary_string]))
91

Markus Mirz's avatar
Markus Mirz committed
92
93
    else:
        # Read in specified time series
94
        print('cannot read specified columns yet')
Markus Mirz's avatar
Markus Mirz committed
95

96
97
98
99
    print('Simulink results real column names: ' + str(real_result_columns))
    print('Simulink results complex column names: ' + str(cmpl_result_columns))
    print('Simulink results variable number: ' + str(len(timeseries_list)))
    print('Simulink results length: ' + str(len(timestamps)))
Markus Mirz's avatar
Markus Mirz committed
100
101

    return timeseries_list
102

103
104
105
106
107
108
109
110
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)
Markus Mirz's avatar
Markus Mirz committed
111
    timeseries_list = {}
112
113
114
115
116
117
118
119
120
121
122
123
124
    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
125
126
        real_string = '.real'
        imaginary_string = '.imag'
127
128
129
130
131
132
133
        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)
134
135
136
                #print("Found real variable: " + column)

        for column in real_result_columns:
Markus Mirz's avatar
Markus Mirz committed
137
            timeseries_list[column] = TimeSeries(column, timestamps, pd_df[column])
138

139
140
141
142
143
144
145
146
147
148
149
        for column in cmpl_result_columns:
            try:
                timeseries_list[column] = TimeSeries(column, timestamps,
                    np.vectorize(complex)(
                        pd_df[column + real_string],
                        pd_df[column + imaginary_string]
                    )
                )
            except:
                pass

150
151
152
153
154
155
156
157
158
159
    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