readtools.py 7.29 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:
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

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
def read_timeseries_simulink(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))

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

    return timeseries_list
75

76
def read_timeseries_dpsim_real(filename, timeseries_names=None):
77 78 79 80 81 82
    """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.
    :param filename: name of the csv file that has the data
    :param timeseries_names: column names which should be read
    :return: list of Timeseries objects
    """
83
    timeseries_list = []
84
    pd_df = pd.read_csv(filename)
85

86
    if timeseries_names is None:
87 88
        # No column names specified, thus read in all and strip spaces
        pd_df.rename(columns=lambda x: x.strip(), inplace=True)
89 90 91 92 93 94 95 96 97 98 99 100 101
        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))
102
    print('DPsim results number: ' + str(len(timeseries_list)))
103
    print('DPsim results timestamps number: ' + str(len(timestamps)))
104

105
    return timeseries_list
106

107

108 109 110 111 112 113 114
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.
    :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
    """
Markus Mirz's avatar
Markus Mirz committed
115
    pd_df = pd.read_csv(filename)
116
    timeseries_list = []
117

118
    if timeseries_names is None:
119
        # No column names specified, thus read in all and strip off spaces
Markus Mirz's avatar
Markus Mirz committed
120
        pd_df.rename(columns=lambda x: x.strip(), inplace=True)
121
        column_names = list(pd_df.columns.values)
122

123
        # Remove timestamps column name and store separately
Markus Mirz's avatar
Markus Mirz committed
124
        column_names.remove('time')
125
        timestamps = pd_df.iloc[:, 0]
126 127
        # Calculate number of network nodes since array is [real, imag]
        node_number = int(len(column_names) / 2)
128
        node_index = 1
129 130
        for column in column_names:
            if node_index <= node_number:
131
                ts_name = 'n' + str(node_index)
132
                timeseries_list.append(
133
                    TimeSeries(ts_name, timestamps, np.vectorize(complex)(pd_df.iloc[:, node_index], pd_df.iloc[:, node_index + node_number])))
134 135 136 137 138 139 140
            else:
                break
            node_index = node_index + 1
    else:
        # Read in specified time series
        print('cannot read specified columns yet')

141 142 143
    print('DPsim results column names: ' + str(column_names))
    print('DPsim results number: ' + str(len(timeseries_list)))

144 145
    return timeseries_list

146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
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.
    :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, header=None)
    timeseries_list = []

    if timeseries_names is None:
        # No trajectory names specified, thus read in all
        column_names = list(pd_df.columns.values)
        # Remove timestamps column name and store separately
        column_names.remove(0)
        timestamps = pd_df.iloc[:, 0]
        # Calculate number of network nodes since array is [real, imag]
164
        node_number = int(len(column_names) / 2)
165
        node_index = 1
166 167
        for column in column_names:
            if node_index <= node_number:
168 169
                node_name = 'node ' + str(node_index) + ' Re'
                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:, column]))
170
            else:
171 172
                node_name = 'node ' + str(node_index - node_number) + ' Im'
                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:, column]))
173 174 175 176 177

            node_index = node_index + 1
    else:
        # Read in specified time series
        print('no column names specified yet')
178 179 180 181 182 183

    print('DPsim results file length:')
    print(len(timeseries_list))
    for result in timeseries_list:
        print(result.name)
    return timeseries_list