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