##// 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
@@ -17,24 +17,38 namespace {
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();
@@ -75,8 +89,8 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};
@@ -97,18 +111,21 Unit readXAxisUnit(QTextStream &stream)
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
@@ -117,8 +134,8 readResults(QTextStream &stream, AmdaResultParser::ValueType valueType)
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);
@@ -144,7 +161,7 readResults(QTextStream &stream, AmdaResultParser::ValueType valueType)
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));
@@ -186,7 +203,6 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath,
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
General Comments 0
You need to be logged in to leave comments. Login now