This repository was archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComtradeRecord.cpp
More file actions
229 lines (198 loc) · 6.18 KB
/
Copy pathComtradeRecord.cpp
File metadata and controls
229 lines (198 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "ComtradeRecord.h"
#include "ComtradeAnalogChannel.h"
#include "ComtradeDigitalChannel.h"
#include <QtCore/QDebug>
#include <QtCore/QDateTime>
ComtradeRecord::ComtradeRecord(QObject *parent)
: QObject(parent)
, m_timeMultiplier(0)
, m_triggerOffsetms(0)
{
}
ComtradeRecord::~ComtradeRecord()
{
}
int ComtradeRecord::channelsCount() const
{
return m_analogChannels.count() + m_digitalChannels.count();
}
int ComtradeRecord::analogChannelsCount() const
{
return m_analogChannels.count();
}
int ComtradeRecord::digitalChannelsCount() const
{
return m_digitalChannels.count();
}
const ComtradeAnalogChannel *ComtradeRecord::analogChannel(int index) const
{
return m_analogChannels.value(index);
}
const ComtradeDigitalChannel *ComtradeRecord::digitalChannel(int index) const
{
return m_digitalChannels.value(index);
}
int ComtradeRecord::ratesCount() const
{
return m_sampleRates.count();
}
double ComtradeRecord::sampleRate(int rateIndex) const
{
return m_sampleRates.value(rateIndex);
}
int ComtradeRecord::lastSample(int rateIndex) const
{
return m_lastSamples.value(rateIndex);
}
double ComtradeRecord::timeMultiplier() const
{
return m_timeMultiplier;
}
double ComtradeRecord::triggerOffsetms() const
{
return m_triggerOffsetms;
}
void ComtradeRecord::appendAnalogChannel(ComtradeAnalogChannel *analogChannel)
{
m_analogChannels.append(analogChannel);
}
void ComtradeRecord::appendDigitalChannel(ComtradeDigitalChannel *digitalChannels)
{
m_digitalChannels.append(digitalChannels);
}
void ComtradeRecord::appendSampling(double sampleRate, int lastSample)
{
m_sampleRates.append(sampleRate);
m_lastSamples.append(lastSample);
}
void ComtradeRecord::setTimeMultiplier(double timeMultiplier)
{
m_timeMultiplier = timeMultiplier;
}
void ComtradeRecord::setTriggerOffsetms(double triggerOffsetms)
{
m_triggerOffsetms = triggerOffsetms;
}
ComtradeRecord *ComtradeRecord::fromCfg(QIODevice *cfgFile, QObject *parent)
{
ComtradeRecord *ret = new ComtradeRecord(parent);
QList<QByteArray> tokens;
bool ok = true;
// station_name,rec_dev_id,rev_year, ignore for now
QByteArray line = cfgFile->readLine();
// TT,##A,##D
line = cfgFile->readLine();
tokens = line.trimmed().split(',');
if (tokens.count() != 3) {
qWarning() << "Invalid CFG line: " << line;
ret->deleteLater();
return nullptr;
}
int totalChannels = tokens.value(0).toInt(&ok);
if (!ok) {
qWarning() << "Invalid channels count: " << tokens.value(0);
}
QByteArray analogToken = tokens.value(1);
analogToken.chop(1);
int analogChannels = analogToken.toInt(&ok);
if (!ok) {
qWarning() << "Invalid analog count: " << tokens.value(1);
ret->deleteLater();
return nullptr;
}
QByteArray digitalToken = tokens.value(2);
digitalToken.chop(1);
int digitalChannels = digitalToken.toInt(&ok);
if (!ok) {
qWarning() << "Invalid digital count: " << tokens.value(2);
ret->deleteLater();
return nullptr;
}
if (totalChannels != digitalChannels + analogChannels) {
qWarning() << "Total count not equal to analog + digital (" << totalChannels << analogChannels << digitalChannels << ")";
ret->deleteLater();
return nullptr;
}
// Analog channels lines
for (int i = 0; i < analogChannels; ++i) {
line = cfgFile->readLine();
ComtradeAnalogChannel *analogChannel = ComtradeAnalogChannel::fromCfgLine(line, ret);
if (!analogChannel) {
ret->deleteLater();
return nullptr;
}
ret->appendAnalogChannel(analogChannel);
}
// Digital channels lines
for (int i = 0; i < digitalChannels; ++i) {
line = cfgFile->readLine();
ComtradeDigitalChannel *digitalChannel = ComtradeDigitalChannel::fromCfgLine(line, ret);
if (!digitalChannel) {
ret->deleteLater();
return nullptr;
}
ret->appendDigitalChannel(digitalChannel);
}
// lf, ignore for now
line = cfgFile->readLine();
// nrates
line = cfgFile->readLine();
int nRates = line.trimmed().toInt(&ok);
if (!ok) {
qWarning() << "Invalid nrates: " << line;
ret->deleteLater();
return nullptr;
}
// Rates lines
for (int i = 0; i < nRates; ++i) {
line = cfgFile->readLine();
tokens = line.trimmed().split(',');
if (tokens.count() != 2) {
qWarning() << "Invalid rate line: " << line;
ret->deleteLater();
return nullptr;
}
double sampleRate = tokens.value(0).toDouble(&ok);
if (!ok) {
qWarning() << "Invalid sample rate: " << tokens.value(0);
ret->deleteLater();
return nullptr;
}
int lastSample = tokens.value(1).toInt(&ok);
if (!ok) {
qWarning() << "Invalid last sample: " << tokens.value(1);
ret->deleteLater();
return nullptr;
}
ret->appendSampling(sampleRate, lastSample);
}
// initialTimestamp
line = cfgFile->readLine();
QDateTime initialTimestamp = QDateTime::fromString(QString::fromLatin1(line.trimmed()), QStringLiteral("dd/MM/yyyy,HH:mm:ss.zzz000"));
// triggerTimestamp
line = cfgFile->readLine();
QDateTime triggerTimestamp = QDateTime::fromString(QString::fromLatin1(line.trimmed()), QStringLiteral("dd/MM/yyyy,HH:mm:ss.zzz000"));
if (!initialTimestamp.isValid() || !triggerTimestamp.isValid()) {
qWarning() << "Invalid timestamps, timebase will have no offset";
} else {
qint64 offset = initialTimestamp.msecsTo(triggerTimestamp);
ret->setTriggerOffsetms(offset);
}
// ft
line = cfgFile->readLine();
if (line.trimmed() != "ASCII") {
qWarning() << "Unsupported file type: " << line;
ret->deleteLater();
return nullptr;
}
// timemultiplier
line = cfgFile->readLine();
double timeMultiplier = line.trimmed().toInt(&ok);
if (!ok) {
qWarning() << "Invalid time multiplier: " << line;
ret->deleteLater();
return nullptr;
}
ret->setTimeMultiplier(timeMultiplier);
return ret;
}