##// END OF EJS Templates
- Changes the way to retrieve unit in AMDA result file: The generated header in the result file differs from the first version...
Alexandre Leroux -
r775:c9e284499782
parent child
Show More
@@ -1,209 +1,225
1 #include "AmdaResultParser.h"
1 #include "AmdaResultParser.h"
2
2
3 #include <Common/DateUtils.h>
3 #include <Common/DateUtils.h>
4 #include <Data/ScalarSeries.h>
4 #include <Data/ScalarSeries.h>
5 #include <Data/VectorSeries.h>
5 #include <Data/VectorSeries.h>
6
6
7 #include <QDateTime>
7 #include <QDateTime>
8 #include <QFile>
8 #include <QFile>
9 #include <QRegularExpression>
9 #include <QRegularExpression>
10
10
11 #include <cmath>
11 #include <cmath>
12
12
13 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
13 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
14
14
15 namespace {
15 namespace {
16
16
17 /// Message in result file when the file was not found on server
17 /// Message in result file when the file was not found on server
18 const auto FILE_NOT_FOUND_MESSAGE = QStringLiteral("Not Found");
18 const auto FILE_NOT_FOUND_MESSAGE = QStringLiteral("Not Found");
19
19
20 /// Format for dates in result files
21 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
22
23 /// Separator between values in a result line
20 /// Separator between values in a result line
24 const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")};
21 const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")};
25
22
23 /// Regex to find the header of the data in the file. This header indicates the end of comments in
24 /// the file
25 const auto DATA_HEADER_REGEX = QRegularExpression{QStringLiteral("#\\s*DATA\\s*:")};
26
27 /// Format for dates in result files
28 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
29
26 /// Regex to find unit in a line. Examples of valid lines:
30 /// Regex to find unit in a line. Examples of valid lines:
27 /// ... - Units : nT - ...
31 /// ... PARAMETER_UNITS : nT ...
28 /// ... -Units:nT- ...
32 /// ... PARAMETER_UNITS:nT ...
29 /// ... -Units: mΒ²- ...
33 /// ... PARAMETER_UNITS: mΒ² ...
30 /// ... - Units : m/s - ...
34 /// ... PARAMETER_UNITS : m/s ...
31 const auto UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")};
35 const auto UNIT_REGEX = QRegularExpression{QStringLiteral("\\s*PARAMETER_UNITS\\s*:\\s*(.+)")};
36
37 QDateTime dateTimeFromString(const QString &stringDate) noexcept
38 {
39 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
40 return QDateTime::fromString(stringDate, Qt::ISODateWithMs);
41 #else
42 return QDateTime::fromString(stringDate, DATE_FORMAT);
43 #endif
44 }
32
45
33 /// Converts a string date to a double date
46 /// Converts a string date to a double date
34 /// @return a double that represents the date in seconds, NaN if the string date can't be converted
47 /// @return a double that represents the date in seconds, NaN if the string date can't be converted
35 double doubleDate(const QString &stringDate) noexcept
48 double doubleDate(const QString &stringDate) noexcept
36 {
49 {
37 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
50 // Format: yyyy-MM-ddThh:mm:ss.zzz
51 auto dateTime = dateTimeFromString(stringDate);
38 dateTime.setTimeSpec(Qt::UTC);
52 dateTime.setTimeSpec(Qt::UTC);
39 return dateTime.isValid() ? DateUtils::secondsSinceEpoch(dateTime)
53 return dateTime.isValid() ? DateUtils::secondsSinceEpoch(dateTime)
40 : std::numeric_limits<double>::quiet_NaN();
54 : std::numeric_limits<double>::quiet_NaN();
41 }
55 }
42
56
43 /// Checks if a line is a comment line
57 /// Checks if a line is a comment line
44 bool isCommentLine(const QString &line)
58 bool isCommentLine(const QString &line)
45 {
59 {
46 return line.startsWith("#");
60 return line.startsWith("#");
47 }
61 }
48
62
49 /// @return the number of lines to be read depending on the type of value passed in parameter
63 /// @return the number of lines to be read depending on the type of value passed in parameter
50 int nbValues(AmdaResultParser::ValueType valueType) noexcept
64 int nbValues(AmdaResultParser::ValueType valueType) noexcept
51 {
65 {
52 switch (valueType) {
66 switch (valueType) {
53 case AmdaResultParser::ValueType::SCALAR:
67 case AmdaResultParser::ValueType::SCALAR:
54 return 1;
68 return 1;
55 case AmdaResultParser::ValueType::VECTOR:
69 case AmdaResultParser::ValueType::VECTOR:
56 return 3;
70 return 3;
57 case AmdaResultParser::ValueType::UNKNOWN:
71 case AmdaResultParser::ValueType::UNKNOWN:
58 // Invalid case
72 // Invalid case
59 break;
73 break;
60 }
74 }
61
75
62 // Invalid cases
76 // Invalid cases
63 qCCritical(LOG_AmdaResultParser())
77 qCCritical(LOG_AmdaResultParser())
64 << QObject::tr("Can't get the number of values to read: unsupported type");
78 << QObject::tr("Can't get the number of values to read: unsupported type");
65 return 0;
79 return 0;
66 }
80 }
67
81
68 /**
82 /**
69 * Reads stream to retrieve x-axis unit
83 * Reads stream to retrieve x-axis unit
70 * @param stream the stream to read
84 * @param stream the stream to read
71 * @return the unit that has been read in the stream, a default unit (time unit with no label) if an
85 * @return the unit that has been read in the stream, a default unit (time unit with no label) if an
72 * error occured during reading
86 * error occured during reading
73 */
87 */
74 Unit readXAxisUnit(QTextStream &stream)
88 Unit readXAxisUnit(QTextStream &stream)
75 {
89 {
76 QString line{};
90 QString line{};
77
91
78 // Searches unit in the comment lines
92 // Searches unit in the comment lines (as long as the reading has not reached the data header)
79 while (stream.readLineInto(&line) && isCommentLine(line)) {
93 while (stream.readLineInto(&line) && !line.contains(DATA_HEADER_REGEX)) {
80 auto match = UNIT_REGEX.match(line);
94 auto match = UNIT_REGEX.match(line);
81 if (match.hasMatch()) {
95 if (match.hasMatch()) {
82 return Unit{match.captured(1), true};
96 return Unit{match.captured(1), true};
83 }
97 }
84 }
98 }
85
99
86 qCWarning(LOG_AmdaResultParser()) << QObject::tr("The unit could not be found in the file");
100 qCWarning(LOG_AmdaResultParser()) << QObject::tr("The unit could not be found in the file");
87
101
88 // Error cases
102 // Error cases
89 return Unit{{}, true};
103 return Unit{{}, true};
90 }
104 }
91
105
92 /**
106 /**
93 * Reads stream to retrieve results
107 * Reads stream to retrieve results
94 * @param stream the stream to read
108 * @param stream the stream to read
95 * @return the pair of vectors x-axis data/values data that has been read in the stream
109 * @return the pair of vectors x-axis data/values data that has been read in the stream
96 */
110 */
97 std::pair<std::vector<double>, std::vector<double> >
111 std::pair<std::vector<double>, std::vector<double> >
98 readResults(QTextStream &stream, AmdaResultParser::ValueType valueType)
112 readResults(QTextStream &stream, AmdaResultParser::ValueType valueType)
99 {
113 {
100 auto expectedNbValues = nbValues(valueType);
114 auto expectedNbValues = nbValues(valueType) + 1;
101
115
102 auto xData = std::vector<double>{};
116 auto xData = std::vector<double>{};
103 auto valuesData = std::vector<double>{};
117 auto valuesData = std::vector<double>{};
104
118
105 QString line{};
119 QString line{};
106
120
107 while (stream.readLineInto(&line)) {
121 // Skip comment lines
108 // Ignore comment lines
122 while (stream.readLineInto(&line) && isCommentLine(line)) {
109 if (!isCommentLine(line)) {
123 }
124
125 if (!stream.atEnd()) {
126 do {
110 auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts);
127 auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts);
111 if (lineData.size() == expectedNbValues + 1) {
128 if (lineData.size() == expectedNbValues) {
112 // X : the data is converted from date to double (in secs)
129 // X : the data is converted from date to double (in secs)
113 auto x = doubleDate(lineData.at(0));
130 auto x = doubleDate(lineData.at(0));
114
131
115 // Adds result only if x is valid. Then, if value is invalid, it is set to NaN
132 // Adds result only if x is valid. Then, if value is invalid, it is set to NaN
116 if (!std::isnan(x)) {
133 if (!std::isnan(x)) {
117 xData.push_back(x);
134 xData.push_back(x);
118
135
119 // Values
136 // Values
120 for (auto valueIndex = 0; valueIndex < expectedNbValues; ++valueIndex) {
137 for (auto valueIndex = 1; valueIndex < expectedNbValues; ++valueIndex) {
121 auto column = valueIndex + 1;
138 auto column = valueIndex;
122
139
123 bool valueOk;
140 bool valueOk;
124 auto value = lineData.at(column).toDouble(&valueOk);
141 auto value = lineData.at(column).toDouble(&valueOk);
125
142
126 if (!valueOk) {
143 if (!valueOk) {
127 qCWarning(LOG_AmdaResultParser())
144 qCWarning(LOG_AmdaResultParser())
128 << QObject::tr(
145 << QObject::tr(
129 "Value from (line %1, column %2) is invalid and will be "
146 "Value from (line %1, column %2) is invalid and will be "
130 "converted to NaN")
147 "converted to NaN")
131 .arg(line, column);
148 .arg(line, column);
132 value = std::numeric_limits<double>::quiet_NaN();
149 value = std::numeric_limits<double>::quiet_NaN();
133 }
150 }
134 valuesData.push_back(value);
151 valuesData.push_back(value);
135 }
152 }
136 }
153 }
137 else {
154 else {
138 qCWarning(LOG_AmdaResultParser())
155 qCWarning(LOG_AmdaResultParser())
139 << QObject::tr("Can't retrieve results from line %1: x is invalid")
156 << QObject::tr("Can't retrieve results from line %1: x is invalid")
140 .arg(line);
157 .arg(line);
141 }
158 }
142 }
159 }
143 else {
160 else {
144 qCWarning(LOG_AmdaResultParser())
161 qCWarning(LOG_AmdaResultParser())
145 << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line);
162 << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line);
146 }
163 }
147 }
164 } while (stream.readLineInto(&line));
148 }
165 }
149
166
150 return std::make_pair(std::move(xData), std::move(valuesData));
167 return std::make_pair(std::move(xData), std::move(valuesData));
151 }
168 }
152
169
153 } // namespace
170 } // namespace
154
171
155 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath,
172 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath,
156 ValueType valueType) noexcept
173 ValueType valueType) noexcept
157 {
174 {
158 if (valueType == ValueType::UNKNOWN) {
175 if (valueType == ValueType::UNKNOWN) {
159 qCCritical(LOG_AmdaResultParser())
176 qCCritical(LOG_AmdaResultParser())
160 << QObject::tr("Can't retrieve AMDA data: the type of values to be read is unknown");
177 << QObject::tr("Can't retrieve AMDA data: the type of values to be read is unknown");
161 return nullptr;
178 return nullptr;
162 }
179 }
163
180
164 QFile file{filePath};
181 QFile file{filePath};
165
182
166 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
183 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
167 qCCritical(LOG_AmdaResultParser())
184 qCCritical(LOG_AmdaResultParser())
168 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
185 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
169 .arg(filePath, file.errorString());
186 .arg(filePath, file.errorString());
170 return nullptr;
187 return nullptr;
171 }
188 }
172
189
173 QTextStream stream{&file};
190 QTextStream stream{&file};
174
191
175 // Checks if the file was found on the server
192 // Checks if the file was found on the server
176 auto firstLine = stream.readLine();
193 auto firstLine = stream.readLine();
177 if (firstLine.compare(FILE_NOT_FOUND_MESSAGE) == 0) {
194 if (firstLine.compare(FILE_NOT_FOUND_MESSAGE) == 0) {
178 qCCritical(LOG_AmdaResultParser())
195 qCCritical(LOG_AmdaResultParser())
179 << QObject::tr("Can't retrieve AMDA data from file %1: file was not found on server")
196 << QObject::tr("Can't retrieve AMDA data from file %1: file was not found on server")
180 .arg(filePath);
197 .arg(filePath);
181 return nullptr;
198 return nullptr;
182 }
199 }
183
200
184 // Reads x-axis unit
201 // Reads x-axis unit
185 stream.seek(0); // returns to the beginning of the file
202 stream.seek(0); // returns to the beginning of the file
186 auto xAxisUnit = readXAxisUnit(stream);
203 auto xAxisUnit = readXAxisUnit(stream);
187
204
188 // Reads results
205 // Reads results
189 stream.seek(0); // returns to the beginning of the file
190 auto results = readResults(stream, valueType);
206 auto results = readResults(stream, valueType);
191
207
192 // Creates data series
208 // Creates data series
193 switch (valueType) {
209 switch (valueType) {
194 case ValueType::SCALAR:
210 case ValueType::SCALAR:
195 return std::make_shared<ScalarSeries>(std::move(results.first),
211 return std::make_shared<ScalarSeries>(std::move(results.first),
196 std::move(results.second), xAxisUnit, Unit{});
212 std::move(results.second), xAxisUnit, Unit{});
197 case ValueType::VECTOR:
213 case ValueType::VECTOR:
198 return std::make_shared<VectorSeries>(std::move(results.first),
214 return std::make_shared<VectorSeries>(std::move(results.first),
199 std::move(results.second), xAxisUnit, Unit{});
215 std::move(results.second), xAxisUnit, Unit{});
200 case ValueType::UNKNOWN:
216 case ValueType::UNKNOWN:
201 // Invalid case
217 // Invalid case
202 break;
218 break;
203 }
219 }
204
220
205 // Invalid cases
221 // Invalid cases
206 qCCritical(LOG_AmdaResultParser())
222 qCCritical(LOG_AmdaResultParser())
207 << QObject::tr("Can't create data series: unsupported value type");
223 << QObject::tr("Can't create data series: unsupported value type");
208 return nullptr;
224 return nullptr;
209 }
225 }
General Comments 0
You need to be logged in to leave comments. Login now