##// END OF EJS Templates
Improves controls when reading results
Alexandre Leroux -
r363:4b7007448469
parent child
Show More
@@ -13,6 +13,9 namespace {
13 13 /// Format for dates in result files
14 14 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
15 15
16 /// Separator between values in a result line
17 const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")};
18
16 19 /// Regex to find unit in a line. Examples of valid lines:
17 20 /// ... - Units : nT - ...
18 21 /// ... -Units:nT- ...
@@ -20,11 +23,13 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
20 23 /// ... - Units : m/s - ...
21 24 const auto UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")};
22 25
23 /// @todo ALX
26 /// Converts a string date to a double date
27 /// @return a double that represents the date in seconds, NaN if the string date can't be converted
24 28 double doubleDate(const QString &stringDate) noexcept
25 29 {
26 30 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
27 return dateTime.toMSecsSinceEpoch() / 1000.;
31 return dateTime.isValid() ? (dateTime.toMSecsSinceEpoch() / 1000.)
32 : std::numeric_limits<double>::quiet_NaN();
28 33 }
29 34
30 35 /**
@@ -55,6 +60,48 Unit readXAxisUnit(QTextStream &stream)
55 60 return Unit{{}, true};
56 61 }
57 62
63 /**
64 * Reads stream to retrieve results
65 * @param stream the stream to read
66 * @return the pair of vectors x-axis data/values data that has been read in the stream
67 */
68 QPair<QVector<double>, QVector<double> > readResults(QTextStream &stream)
69 {
70 auto xData = QVector<double>{};
71 auto valuesData = QVector<double>{};
72
73 QString line{};
74 while (stream.readLineInto(&line)) {
75 auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts);
76 if (lineData.size() == 2) {
77 // X : the data is converted from date to double (in secs)
78 auto x = doubleDate(lineData.at(0));
79
80 // Value
81 bool valueOk;
82 auto value = lineData.at(1).toDouble(&valueOk);
83
84 // Adds result only if x and value are valid
85 if (!std::isnan(x) && !std::isnan(value) && valueOk) {
86 xData.push_back(x);
87 valuesData.push_back(value);
88 }
89 else {
90 qCWarning(LOG_AmdaResultParser())
91 << QObject::tr(
92 "Can't retrieve results from line %1: x and/or value are invalid")
93 .arg(line);
94 }
95 }
96 else {
97 qCWarning(LOG_AmdaResultParser())
98 << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line);
99 }
100 }
101
102 return qMakePair(std::move(xData), std::move(valuesData));
103 }
104
58 105 } // namespace
59 106
60 107 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath) noexcept
@@ -68,36 +115,18 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath)
68 115 return nullptr;
69 116 }
70 117
71 auto xData = QVector<double>{};
72 auto valuesData = QVector<double>{};
73
74 118 QTextStream stream{&file};
75 119
76 120 // Ignore first two lines (comments lines)
77 121 stream.readLine();
78 122 stream.readLine();
79 123
80 QString line{};
81
82 124 // Reads x-axis unit
83 125 auto xAxisUnit = readXAxisUnit(stream);
84 126
85 127 // Reads results
86 auto lineRegex = QRegExp{QStringLiteral("\\s+")};
87 while (stream.readLineInto(&line)) {
88 auto lineData = line.split(lineRegex, QString::SkipEmptyParts);
89 if (lineData.size() == 2) {
90 // X : the data is converted from date to double (in secs)
91 xData.push_back(doubleDate(lineData.at(0)));
92
93 // Value
94 valuesData.push_back(lineData.at(1).toDouble());
95 }
96 else {
97 /// @todo ALX : log
98 }
99 }
128 auto results = readResults(stream);
100 129
101 return std::make_shared<ScalarSeries>(std::move(xData), std::move(valuesData), xAxisUnit,
102 Unit{});
130 return std::make_shared<ScalarSeries>(std::move(results.first), std::move(results.second),
131 xAxisUnit, Unit{});
103 132 }
General Comments 0
You need to be logged in to leave comments. Login now