##// END OF EJS Templates
Reads x-axis unit in result file
Alexandre Leroux -
r393:dc7f981a773b
parent child
Show More
@@ -1,64 +1,103
1 1 #include "AmdaResultParser.h"
2 2
3 3 #include <Data/ScalarSeries.h>
4 4
5 5 #include <QDateTime>
6 6 #include <QFile>
7 #include <QRegularExpression>
7 8
8 9 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
9 10
10 11 namespace {
11 12
12 13 /// Format for dates in result files
13 14 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
14 15
16 /// Regex to find unit in a line. Examples of valid lines:
17 /// ... - Units : nT - ...
18 /// ... -Units:nT- ...
19 /// ... -Units: mΒ²- ...
20 /// ... - Units : m/s - ...
21 const auto UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")};
22
15 23 /// @todo ALX
16 24 double doubleDate(const QString &stringDate) noexcept
17 25 {
18 26 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
19 27 return dateTime.toMSecsSinceEpoch() / 1000.;
20 28 }
21 29
30 /**
31 * Reads stream to retrieve x-axis unit
32 * @param stream the stream to read
33 * @return the unit that has been read in the stream, a default unit (time unit with no label) if an
34 * error occured during reading
35 */
36 Unit readXAxisUnit(QTextStream &stream)
37 {
38 QString line{};
39
40 if (stream.readLineInto(&line)) {
41 auto match = UNIT_REGEX.match(line);
42 if (match.hasMatch()) {
43 return Unit{match.captured(1), true};
44 }
45 else {
46 qCWarning(LOG_AmdaResultParser())
47 << QObject::tr("Can't read unit: invalid line %1").arg(line);
48 }
49 }
50 else {
51 qCWarning(LOG_AmdaResultParser()) << QObject::tr("Can't read unit: end of file");
52 }
53
54 // Error cases
55 return Unit{{}, true};
56 }
57
22 58 } // namespace
23 59
24 60 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath) noexcept
25 61 {
26 62 QFile file{filePath};
27 63
28 64 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
29 65 qCCritical(LOG_AmdaResultParser())
30 66 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
31 67 .arg(filePath, file.errorString());
32 68 return nullptr;
33 69 }
34 70
35 71 auto xData = QVector<double>{};
36 72 auto valuesData = QVector<double>{};
37 73
38 74 QTextStream stream{&file};
39 75
40 // Ignore comment lines (3 lines)
41 stream.readLine();
76 // Ignore first two lines (comments lines)
42 77 stream.readLine();
43 78 stream.readLine();
44 79
45 80 QString line{};
81
82 // Reads x-axis unit
83 auto xAxisUnit = readXAxisUnit(stream);
84
85 // Reads results
46 86 auto lineRegex = QRegExp{QStringLiteral("\\s+")};
47 87 while (stream.readLineInto(&line)) {
48 88 auto lineData = line.split(lineRegex, QString::SkipEmptyParts);
49 89 if (lineData.size() == 2) {
50 90 // X : the data is converted from date to double (in secs)
51 91 xData.push_back(doubleDate(lineData.at(0)));
52 92
53 93 // Value
54 94 valuesData.push_back(lineData.at(1).toDouble());
55 95 }
56 96 else {
57 97 /// @todo ALX : log
58 98 }
59 99 }
60 100
61 /// @todo ALX : handle units
62 return std::make_shared<ScalarSeries>(std::move(xData), std::move(valuesData), Unit{"nT", true},
101 return std::make_shared<ScalarSeries>(std::move(xData), std::move(valuesData), xAxisUnit,
63 102 Unit{});
64 103 }
General Comments 0
You need to be logged in to leave comments. Login now