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

Status change > Approved

You need to be logged in to leave comments. Login now