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