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

5

6
def read_timeseries_Modelica(filename, timeseries_names=None):
7
    from modelicares import SimRes
8
    sim = SimRes(filename)
9
    if timeseries_names is None:
10
11
12
13
        # No trajectory names specified, thus read in all
        print('TBD')
    else:
        # Read in specified time series
14
15
16
17
18
19
20
        if not isinstance(timeseries_names, list):
            timeseries = TimeSeries(timeseries_names, sim(timeseries_names).times(), sim(timeseries_names).values())
        else:
            for name in timeseries_names:
                timeseries = []
                timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
    return timeseries
21

22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
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))
    return timeseries_list
37

38
39
40
41
42
43
44
45
def read_timeseries_dpsim_real(filename, header=None, 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.
    :param filename: name of the csv file that has the data
    :param header: specifies if the log file has a header
    :param timeseries_names: column names which should be read
    :return: list of Timeseries objects
    """
46
47
    timeseries_list = []

48
49
50
51
52
    if header is True:
        pd_df = pd.read_csv(filename)
    else:
        pd_df = pd.read_csv(filename, header=None)

53
    if timeseries_names is None:
54
        # No trajectory names specified, thus read in all
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        column_names = list(pd_df.columns.values)
        # Remove timestamps column name and store separately
        column_names.remove(0)
        timestamps = pd_df.iloc[:,0]

        if header is True:
            for name in column_names:
                timeseries_list.append(TimeSeries(name, timestamps, pd_df[name].values))
        else:
            node_number = int(len(column_names))
            node_index = 1
            for column in column_names:
                ts_name = 'node ' + str(node_index)
                timeseries_list.append(TimeSeries(ts_name, timestamps, pd_df.iloc[:, column]))
                node_index = node_index + 1
70
71
    else:
        # Read in specified time series
72
73
74
75
76
77
78
        print('no column names specified yet')

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

80
81
82
83
84
85
86
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
    """
87
    pd_df = pd.read_csv(filename, header=None)
88
    timeseries_list = []
89

90
    if timeseries_names is None:
91
92
        # No trajectory names specified, thus read in all
        column_names = list(pd_df.columns.values)
93
        # Remove timestamps column name and store separately
94
        column_names.remove(0)
95
96
97
        timestamps = pd_df.iloc[:,0]
        # Calculate number of network nodes since array is [real, imag]
        node_number = int(len(column_names) / 2)
98
        node_index = 1
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        for column in column_names:
            if node_index <= node_number:
                ts_name = 'node '+ str(node_index)
                timeseries_list.append(TimeSeries(ts_name, timestamps, np.vectorize(complex)(pd_df.iloc[:,column],pd_df.iloc[:,column + node_number])))
            else:
                break
            node_index = node_index + 1
    else:
        # Read in specified time series
        print('cannot read specified columns yet')

    print('DPsim results file length:')
    print(len(timeseries_list))
    for result in timeseries_list:
        print(result.name)
    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.
    :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]
133
        node_number = int(len(column_names) / 2)
134
        node_index = 1
135
136
        for column in column_names:
            if node_index <= node_number:
137
138
                node_name = 'node '+ str(node_index) +' Re'
                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:,column]))
139
            else:
140
141
                node_name = 'node '+ str(node_index - node_number) +' Im'
                timeseries_list.append(TimeSeries(node_name, timestamps, pd_df.iloc[:,column]))
142
143
144
145
146

            node_index = node_index + 1
    else:
        # Read in specified time series
        print('no column names specified yet')
147
148
149
150
151
152

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