@@ -1,97 +1,100 | |||||
1 | #ifndef SCIQLOP_AMDARESULTPARSERHELPER_H |
|
1 | #ifndef SCIQLOP_AMDARESULTPARSERHELPER_H | |
2 | #define SCIQLOP_AMDARESULTPARSERHELPER_H |
|
2 | #define SCIQLOP_AMDARESULTPARSERHELPER_H | |
3 |
|
3 | |||
4 | #include "AmdaResultParserDefs.h" |
|
4 | #include "AmdaResultParserDefs.h" | |
5 |
|
5 | |||
6 | #include <QtCore/QLoggingCategory> |
|
6 | #include <QtCore/QLoggingCategory> | |
7 | #include <QtCore/QString> |
|
7 | #include <QtCore/QString> | |
8 |
|
8 | |||
9 | #include <memory> |
|
9 | #include <memory> | |
10 |
|
10 | |||
11 | class IDataSeries; |
|
11 | class IDataSeries; | |
12 |
|
12 | |||
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaResultParserHelper) |
|
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaResultParserHelper) | |
14 |
|
14 | |||
15 | /** |
|
15 | /** | |
16 | * Helper used to interpret the data of an AMDA result file and generate the corresponding data |
|
16 | * Helper used to interpret the data of an AMDA result file and generate the corresponding data | |
17 | * series. |
|
17 | * series. | |
18 | * |
|
18 | * | |
19 | * It proposes methods allowing to read line by line an AMDA file and to extract the properties |
|
19 | * It proposes methods allowing to read line by line an AMDA file and to extract the properties | |
20 | * (from the header) and the values corresponding to the data series |
|
20 | * (from the header) and the values corresponding to the data series | |
21 | * |
|
21 | * | |
22 | * @sa DataSeries |
|
22 | * @sa DataSeries | |
23 | */ |
|
23 | */ | |
24 | struct IAmdaResultParserHelper { |
|
24 | struct IAmdaResultParserHelper { | |
25 | virtual ~IAmdaResultParserHelper() noexcept = default; |
|
25 | virtual ~IAmdaResultParserHelper() noexcept = default; | |
26 |
|
26 | |||
27 | /// Verifies that the extracted properties are well formed and possibly applies other treatments |
|
27 | /// Verifies that the extracted properties are well formed and possibly applies other treatments | |
28 | /// on them |
|
28 | /// on them | |
29 | /// @return true if the properties are well formed, false otherwise |
|
29 | /// @return true if the properties are well formed, false otherwise | |
30 | virtual bool checkProperties() = 0; |
|
30 | virtual bool checkProperties() = 0; | |
31 |
|
31 | |||
32 | /// Creates the data series from the properties and values extracted from the AMDA file. |
|
32 | /// Creates the data series from the properties and values extracted from the AMDA file. | |
33 | /// @warning as the data are moved in the data series, the helper shouldn't be used after |
|
33 | /// @warning as the data are moved in the data series, the helper shouldn't be used after | |
34 | /// calling this method |
|
34 | /// calling this method | |
35 | /// @return the data series created |
|
35 | /// @return the data series created | |
36 | virtual std::shared_ptr<IDataSeries> createSeries() = 0; |
|
36 | virtual std::shared_ptr<IDataSeries> createSeries() = 0; | |
37 |
|
37 | |||
38 | /// Reads a line from the AMDA file to extract a property that will be used to generate the data |
|
38 | /// Reads a line from the AMDA file to extract a property that will be used to generate the data | |
39 | /// series |
|
39 | /// series | |
40 | /// @param line tahe line to interpret |
|
40 | /// @param line tahe line to interpret | |
41 | virtual void readPropertyLine(const QString &line) = 0; |
|
41 | virtual void readPropertyLine(const QString &line) = 0; | |
42 |
|
42 | |||
43 | /// Reads a line from the AMDA file to extract a value that will be set in the data series |
|
43 | /// Reads a line from the AMDA file to extract a value that will be set in the data series | |
44 | /// @param line the line to interpret |
|
44 | /// @param line the line to interpret | |
45 | virtual void readResultLine(const QString &line) = 0; |
|
45 | virtual void readResultLine(const QString &line) = 0; | |
46 | }; |
|
46 | }; | |
47 |
|
47 | |||
48 | /** |
|
48 | /** | |
49 | * Implementation of @sa IAmdaResultParserHelper for scalars |
|
49 | * Implementation of @sa IAmdaResultParserHelper for scalars | |
50 | */ |
|
50 | */ | |
51 | class ScalarParserHelper : public IAmdaResultParserHelper { |
|
51 | class ScalarParserHelper : public IAmdaResultParserHelper { | |
52 | public: |
|
52 | public: | |
53 | bool checkProperties() override; |
|
53 | bool checkProperties() override; | |
54 | std::shared_ptr<IDataSeries> createSeries() override; |
|
54 | std::shared_ptr<IDataSeries> createSeries() override; | |
55 | void readPropertyLine(const QString &line) override; |
|
55 | void readPropertyLine(const QString &line) override; | |
56 | void readResultLine(const QString &line) override; |
|
56 | void readResultLine(const QString &line) override; | |
57 |
|
57 | |||
58 | private: |
|
58 | private: | |
59 | /// @return the reading order of the "value" columns for a result line of the AMDA file |
|
59 | /// @return the reading order of the "value" columns for a result line of the AMDA file | |
60 | std::vector<int> valuesIndexes() const; |
|
60 | std::vector<int> valuesIndexes() const; | |
61 |
|
61 | |||
62 | Properties m_Properties{}; |
|
62 | Properties m_Properties{}; | |
63 | std::vector<double> m_XAxisData{}; |
|
63 | std::vector<double> m_XAxisData{}; | |
64 | std::vector<double> m_ValuesData{}; |
|
64 | std::vector<double> m_ValuesData{}; | |
65 | }; |
|
65 | }; | |
66 |
|
66 | |||
67 | /** |
|
67 | /** | |
68 | * Implementation of @sa IAmdaResultParserHelper for spectrograms |
|
68 | * Implementation of @sa IAmdaResultParserHelper for spectrograms | |
69 | */ |
|
69 | */ | |
70 | class SpectrogramParserHelper : public IAmdaResultParserHelper { |
|
70 | class SpectrogramParserHelper : public IAmdaResultParserHelper { | |
71 | public: |
|
71 | public: | |
72 | bool checkProperties() override; |
|
72 | bool checkProperties() override; | |
73 | std::shared_ptr<IDataSeries> createSeries() override; |
|
73 | std::shared_ptr<IDataSeries> createSeries() override; | |
74 | void readPropertyLine(const QString &line) override; |
|
74 | void readPropertyLine(const QString &line) override; | |
75 | void readResultLine(const QString &line) override; |
|
75 | void readResultLine(const QString &line) override; | |
|
76 | ||||
|
77 | private: | |||
|
78 | Properties m_Properties{}; | |||
76 | }; |
|
79 | }; | |
77 |
|
80 | |||
78 | /** |
|
81 | /** | |
79 | * Implementation of @sa IAmdaResultParserHelper for vectors |
|
82 | * Implementation of @sa IAmdaResultParserHelper for vectors | |
80 | */ |
|
83 | */ | |
81 | class VectorParserHelper : public IAmdaResultParserHelper { |
|
84 | class VectorParserHelper : public IAmdaResultParserHelper { | |
82 | public: |
|
85 | public: | |
83 | bool checkProperties() override; |
|
86 | bool checkProperties() override; | |
84 | std::shared_ptr<IDataSeries> createSeries() override; |
|
87 | std::shared_ptr<IDataSeries> createSeries() override; | |
85 | void readPropertyLine(const QString &line) override; |
|
88 | void readPropertyLine(const QString &line) override; | |
86 | void readResultLine(const QString &line) override; |
|
89 | void readResultLine(const QString &line) override; | |
87 |
|
90 | |||
88 | private: |
|
91 | private: | |
89 | /// @return the reading order of the "value" columns for a result line of the AMDA file |
|
92 | /// @return the reading order of the "value" columns for a result line of the AMDA file | |
90 | std::vector<int> valuesIndexes() const; |
|
93 | std::vector<int> valuesIndexes() const; | |
91 |
|
94 | |||
92 | Properties m_Properties{}; |
|
95 | Properties m_Properties{}; | |
93 | std::vector<double> m_XAxisData{}; |
|
96 | std::vector<double> m_XAxisData{}; | |
94 | std::vector<double> m_ValuesData{}; |
|
97 | std::vector<double> m_ValuesData{}; | |
95 | }; |
|
98 | }; | |
96 |
|
99 | |||
97 | #endif // SCIQLOP_AMDARESULTPARSERHELPER_H |
|
100 | #endif // SCIQLOP_AMDARESULTPARSERHELPER_H |
@@ -1,260 +1,363 | |||||
1 | #include "AmdaResultParserHelper.h" |
|
1 | #include "AmdaResultParserHelper.h" | |
2 |
|
2 | |||
3 | #include <Common/DateUtils.h> |
|
3 | #include <Common/DateUtils.h> | |
4 |
|
4 | |||
5 | #include <Data/ScalarSeries.h> |
|
5 | #include <Data/ScalarSeries.h> | |
6 | #include <Data/Unit.h> |
|
6 | #include <Data/Unit.h> | |
7 | #include <Data/VectorSeries.h> |
|
7 | #include <Data/VectorSeries.h> | |
8 |
|
8 | |||
9 | #include <QtCore/QDateTime> |
|
9 | #include <QtCore/QDateTime> | |
10 | #include <QtCore/QRegularExpression> |
|
10 | #include <QtCore/QRegularExpression> | |
11 |
|
11 | |||
12 | Q_LOGGING_CATEGORY(LOG_AmdaResultParserHelper, "AmdaResultParserHelper") |
|
12 | Q_LOGGING_CATEGORY(LOG_AmdaResultParserHelper, "AmdaResultParserHelper") | |
13 |
|
13 | |||
14 | namespace { |
|
14 | namespace { | |
15 |
|
15 | |||
16 | // ///////// // |
|
16 | // ///////// // | |
17 | // Constants // |
|
17 | // Constants // | |
18 | // ///////// // |
|
18 | // ///////// // | |
19 |
|
19 | |||
20 | /// Separator between values in a result line |
|
20 | /// Separator between values in a result line | |
21 | const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")}; |
|
21 | const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")}; | |
22 |
|
22 | |||
23 | /// Format for dates in result files |
|
23 | /// Format for dates in result files | |
24 | const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"); |
|
24 | const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"); | |
25 |
|
25 | |||
26 | // /////// // |
|
26 | // /////// // | |
27 | // Methods // |
|
27 | // Methods // | |
28 | // /////// // |
|
28 | // /////// // | |
29 |
|
29 | |||
30 | /** |
|
30 | /** | |
31 | * Checks that the properties contain a specific unit and that this unit is valid |
|
31 | * Checks that the properties contain a specific unit and that this unit is valid | |
32 | * @param properties the properties map in which to search unit |
|
32 | * @param properties the properties map in which to search unit | |
33 | * @param key the key to search for the unit in the properties |
|
33 | * @param key the key to search for the unit in the properties | |
34 | * @param errorMessage the error message to log in case the unit is invalid |
|
34 | * @param errorMessage the error message to log in case the unit is invalid | |
35 | * @return true if the unit is valid, false it it's invalid or was not found in the properties |
|
35 | * @return true if the unit is valid, false it it's invalid or was not found in the properties | |
36 | */ |
|
36 | */ | |
37 | bool checkUnit(const Properties &properties, const QString &key, const QString &errorMessage) |
|
37 | bool checkUnit(const Properties &properties, const QString &key, const QString &errorMessage) | |
38 | { |
|
38 | { | |
39 | auto unit = properties.value(key).value<Unit>(); |
|
39 | auto unit = properties.value(key).value<Unit>(); | |
40 | if (unit.m_Name.isEmpty()) { |
|
40 | if (unit.m_Name.isEmpty()) { | |
41 | qCWarning(LOG_AmdaResultParserHelper()) << errorMessage; |
|
41 | qCWarning(LOG_AmdaResultParserHelper()) << errorMessage; | |
42 | return false; |
|
42 | return false; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | return true; |
|
45 | return true; | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | QDateTime dateTimeFromString(const QString &stringDate) noexcept |
|
48 | QDateTime dateTimeFromString(const QString &stringDate) noexcept | |
49 | { |
|
49 | { | |
50 | #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) |
|
50 | #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) | |
51 | return QDateTime::fromString(stringDate, Qt::ISODateWithMs); |
|
51 | return QDateTime::fromString(stringDate, Qt::ISODateWithMs); | |
52 | #else |
|
52 | #else | |
53 | return QDateTime::fromString(stringDate, DATE_FORMAT); |
|
53 | return QDateTime::fromString(stringDate, DATE_FORMAT); | |
54 | #endif |
|
54 | #endif | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | /// Converts a string date to a double date |
|
57 | /// Converts a string date to a double date | |
58 | /// @return a double that represents the date in seconds, NaN if the string date can't be converted |
|
58 | /// @return a double that represents the date in seconds, NaN if the string date can't be converted | |
59 | double doubleDate(const QString &stringDate) noexcept |
|
59 | double doubleDate(const QString &stringDate) noexcept | |
60 | { |
|
60 | { | |
61 | // Format: yyyy-MM-ddThh:mm:ss.zzz |
|
61 | // Format: yyyy-MM-ddThh:mm:ss.zzz | |
62 | auto dateTime = dateTimeFromString(stringDate); |
|
62 | auto dateTime = dateTimeFromString(stringDate); | |
63 | dateTime.setTimeSpec(Qt::UTC); |
|
63 | dateTime.setTimeSpec(Qt::UTC); | |
64 | return dateTime.isValid() ? DateUtils::secondsSinceEpoch(dateTime) |
|
64 | return dateTime.isValid() ? DateUtils::secondsSinceEpoch(dateTime) | |
65 | : std::numeric_limits<double>::quiet_NaN(); |
|
65 | : std::numeric_limits<double>::quiet_NaN(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | /** |
|
68 | /** | |
69 | * Reads a line from the AMDA file and tries to extract a x-axis data and value data from it |
|
69 | * Reads a line from the AMDA file and tries to extract a x-axis data and value data from it | |
70 | * @param xAxisData the vector in which to store the x-axis data extracted |
|
70 | * @param xAxisData the vector in which to store the x-axis data extracted | |
71 | * @param valuesData the vector in which to store the value extracted |
|
71 | * @param valuesData the vector in which to store the value extracted | |
72 | * @param line the line to read to extract the property |
|
72 | * @param line the line to read to extract the property | |
73 | * @param valuesIndexes indexes of insertion of read values. For example, if the line contains three |
|
73 | * @param valuesIndexes indexes of insertion of read values. For example, if the line contains three | |
74 | * columns of values, and valuesIndexes are {2, 0, 1}, the value of the third column will be read |
|
74 | * columns of values, and valuesIndexes are {2, 0, 1}, the value of the third column will be read | |
75 | * and inserted first, then the value of the first column, and finally the value of the second |
|
75 | * and inserted first, then the value of the first column, and finally the value of the second | |
76 | * column. |
|
76 | * column. | |
77 | * @param fillValue value that tags an invalid data. For example, if fillValue is -1 and a read |
|
77 | * @param fillValue value that tags an invalid data. For example, if fillValue is -1 and a read | |
78 | * value is -1, then this value is considered as invalid and converted to NaN |
|
78 | * value is -1, then this value is considered as invalid and converted to NaN | |
79 | */ |
|
79 | */ | |
80 | void tryReadResult(std::vector<double> &xAxisData, std::vector<double> &valuesData, |
|
80 | void tryReadResult(std::vector<double> &xAxisData, std::vector<double> &valuesData, | |
81 | const QString &line, const std::vector<int> &valuesIndexes, |
|
81 | const QString &line, const std::vector<int> &valuesIndexes, | |
82 | double fillValue = std::numeric_limits<double>::quiet_NaN()) |
|
82 | double fillValue = std::numeric_limits<double>::quiet_NaN()) | |
83 | { |
|
83 | { | |
84 | auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts); |
|
84 | auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts); | |
85 |
|
85 | |||
86 | // Checks that the line contains expected number of values + x-axis value |
|
86 | // Checks that the line contains expected number of values + x-axis value | |
87 | if (lineData.size() == valuesIndexes.size() + 1) { |
|
87 | if (lineData.size() == valuesIndexes.size() + 1) { | |
88 | // X : the data is converted from date to double (in secs) |
|
88 | // X : the data is converted from date to double (in secs) | |
89 | auto x = doubleDate(lineData.at(0)); |
|
89 | auto x = doubleDate(lineData.at(0)); | |
90 |
|
90 | |||
91 | // Adds result only if x is valid. Then, if value is invalid, it is set to NaN |
|
91 | // Adds result only if x is valid. Then, if value is invalid, it is set to NaN | |
92 | if (!std::isnan(x)) { |
|
92 | if (!std::isnan(x)) { | |
93 | xAxisData.push_back(x); |
|
93 | xAxisData.push_back(x); | |
94 |
|
94 | |||
95 | // Values |
|
95 | // Values | |
96 | for (auto valueIndex : valuesIndexes) { |
|
96 | for (auto valueIndex : valuesIndexes) { | |
97 | bool valueOk; |
|
97 | bool valueOk; | |
98 | // we use valueIndex + 1 to skip column 0 (x-axis value) |
|
98 | // we use valueIndex + 1 to skip column 0 (x-axis value) | |
99 | auto value = lineData.at(valueIndex + 1).toDouble(&valueOk); |
|
99 | auto value = lineData.at(valueIndex + 1).toDouble(&valueOk); | |
100 |
|
100 | |||
101 | if (!valueOk) { |
|
101 | if (!valueOk) { | |
102 | qCWarning(LOG_AmdaResultParserHelper()) |
|
102 | qCWarning(LOG_AmdaResultParserHelper()) | |
103 | << QObject::tr( |
|
103 | << QObject::tr( | |
104 | "Value from (line %1, column %2) is invalid and will be " |
|
104 | "Value from (line %1, column %2) is invalid and will be " | |
105 | "converted to NaN") |
|
105 | "converted to NaN") | |
106 | .arg(line, valueIndex); |
|
106 | .arg(line, valueIndex); | |
107 | value = std::numeric_limits<double>::quiet_NaN(); |
|
107 | value = std::numeric_limits<double>::quiet_NaN(); | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | // Handles fill value |
|
110 | // Handles fill value | |
111 | if (!std::isnan(fillValue) && !std::isnan(value) && fillValue == value) { |
|
111 | if (!std::isnan(fillValue) && !std::isnan(value) && fillValue == value) { | |
112 | value = std::numeric_limits<double>::quiet_NaN(); |
|
112 | value = std::numeric_limits<double>::quiet_NaN(); | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 | valuesData.push_back(value); |
|
115 | valuesData.push_back(value); | |
116 | } |
|
116 | } | |
117 | } |
|
117 | } | |
118 | else { |
|
118 | else { | |
119 | qCWarning(LOG_AmdaResultParserHelper()) |
|
119 | qCWarning(LOG_AmdaResultParserHelper()) | |
120 | << QObject::tr("Can't retrieve results from line %1: x is invalid").arg(line); |
|
120 | << QObject::tr("Can't retrieve results from line %1: x is invalid").arg(line); | |
121 | } |
|
121 | } | |
122 | } |
|
122 | } | |
123 | else { |
|
123 | else { | |
124 | qCWarning(LOG_AmdaResultParserHelper()) |
|
124 | qCWarning(LOG_AmdaResultParserHelper()) | |
125 | << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line); |
|
125 | << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line); | |
126 | } |
|
126 | } | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | /** |
|
129 | /** | |
130 | * Reads a line from the AMDA file and tries to extract a property from it |
|
130 | * Reads a line from the AMDA file and tries to extract a property from it | |
131 | * @param properties the properties map in which to put the property extracted from the line |
|
131 | * @param properties the properties map in which to put the property extracted from the line | |
132 | * @param key the key to which the property is added in the properties map |
|
132 | * @param key the key to which the property is added in the properties map | |
133 | * @param line the line to read to extract the property |
|
133 | * @param line the line to read to extract the property | |
134 | * @param regex the expected regex to extract the property. If the line matches this regex, the |
|
134 | * @param regex the expected regex to extract the property. If the line matches this regex, the | |
135 | * property is generated |
|
135 | * property is generated | |
136 | * @param fun the function used to generate the property |
|
136 | * @param fun the function used to generate the property | |
137 | * @return true if the property could be generated, false if the line does not match the regex, or |
|
137 | * @return true if the property could be generated, false if the line does not match the regex, or | |
138 | * if a property has already been generated for the key |
|
138 | * if a property has already been generated for the key | |
139 | */ |
|
139 | */ | |
140 | template <typename GeneratePropertyFun> |
|
140 | template <typename GeneratePropertyFun> | |
141 | bool tryReadProperty(Properties &properties, const QString &key, const QString &line, |
|
141 | bool tryReadProperty(Properties &properties, const QString &key, const QString &line, | |
142 | const QRegularExpression ®ex, GeneratePropertyFun fun) |
|
142 | const QRegularExpression ®ex, GeneratePropertyFun fun) | |
143 | { |
|
143 | { | |
144 | if (properties.contains(key)) { |
|
144 | if (properties.contains(key)) { | |
145 | return false; |
|
145 | return false; | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | auto match = regex.match(line); |
|
148 | auto match = regex.match(line); | |
149 | if (match.hasMatch()) { |
|
149 | if (match.hasMatch()) { | |
150 | properties.insert(key, fun(match)); |
|
150 | properties.insert(key, fun(match)); | |
151 | } |
|
151 | } | |
152 |
|
152 | |||
153 | return match.hasMatch(); |
|
153 | return match.hasMatch(); | |
154 | } |
|
154 | } | |
155 |
|
155 | |||
156 | /** |
|
156 | /** | |
|
157 | * Reads a line from the AMDA file and tries to extract a double from it | |||
|
158 | * @sa tryReadProperty() | |||
|
159 | */ | |||
|
160 | bool tryReadDouble(Properties &properties, const QString &key, const QString &line, | |||
|
161 | const QRegularExpression ®ex) | |||
|
162 | { | |||
|
163 | return tryReadProperty(properties, key, line, regex, [](const auto &match) { | |||
|
164 | bool ok; | |||
|
165 | ||||
|
166 | // If the value can't be converted to double, it is set to NaN | |||
|
167 | auto doubleValue = match.captured(1).toDouble(&ok); | |||
|
168 | if (!ok) { | |||
|
169 | doubleValue = std::numeric_limits<double>::quiet_NaN(); | |||
|
170 | } | |||
|
171 | ||||
|
172 | return QVariant::fromValue(doubleValue); | |||
|
173 | }); | |||
|
174 | } | |||
|
175 | ||||
|
176 | /** | |||
|
177 | * Reads a line from the AMDA file and tries to extract a vector of doubles from it | |||
|
178 | * @param sep the separator of double values in the line | |||
|
179 | * @sa tryReadProperty() | |||
|
180 | */ | |||
|
181 | bool tryReadDoubles(Properties &properties, const QString &key, const QString &line, | |||
|
182 | const QRegularExpression ®ex, const QString &sep = QStringLiteral(",")) | |||
|
183 | { | |||
|
184 | return tryReadProperty(properties, key, line, regex, [sep](const auto &match) { | |||
|
185 | std::vector<double> doubleValues{}; | |||
|
186 | ||||
|
187 | // If the value can't be converted to double, it is set to NaN | |||
|
188 | auto values = match.captured(1).split(sep); | |||
|
189 | for (auto value : values) { | |||
|
190 | bool ok; | |||
|
191 | ||||
|
192 | auto doubleValue = value.toDouble(&ok); | |||
|
193 | if (!ok) { | |||
|
194 | doubleValue = std::numeric_limits<double>::quiet_NaN(); | |||
|
195 | } | |||
|
196 | ||||
|
197 | doubleValues.push_back(doubleValue); | |||
|
198 | } | |||
|
199 | ||||
|
200 | return QVariant::fromValue(doubleValues); | |||
|
201 | }); | |||
|
202 | } | |||
|
203 | ||||
|
204 | /** | |||
157 | * Reads a line from the AMDA file and tries to extract a unit from it |
|
205 | * Reads a line from the AMDA file and tries to extract a unit from it | |
158 | * @sa tryReadProperty() |
|
206 | * @sa tryReadProperty() | |
159 | */ |
|
207 | */ | |
160 | bool tryReadUnit(Properties &properties, const QString &key, const QString &line, |
|
208 | bool tryReadUnit(Properties &properties, const QString &key, const QString &line, | |
161 | const QRegularExpression ®ex, bool timeUnit = false) |
|
209 | const QRegularExpression ®ex, bool timeUnit = false) | |
162 | { |
|
210 | { | |
163 | return tryReadProperty(properties, key, line, regex, [timeUnit](const auto &match) { |
|
211 | return tryReadProperty(properties, key, line, regex, [timeUnit](const auto &match) { | |
164 | return QVariant::fromValue(Unit{match.captured(1), timeUnit}); |
|
212 | return QVariant::fromValue(Unit{match.captured(1), timeUnit}); | |
165 | }); |
|
213 | }); | |
166 | } |
|
214 | } | |
167 |
|
215 | |||
168 | } // namespace |
|
216 | } // namespace | |
169 |
|
217 | |||
170 | // ////////////////// // |
|
218 | // ////////////////// // | |
171 | // ScalarParserHelper // |
|
219 | // ScalarParserHelper // | |
172 | // ////////////////// // |
|
220 | // ////////////////// // | |
173 |
|
221 | |||
174 | bool ScalarParserHelper::checkProperties() |
|
222 | bool ScalarParserHelper::checkProperties() | |
175 | { |
|
223 | { | |
176 | return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY, |
|
224 | return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY, | |
177 | QObject::tr("The x-axis unit could not be found in the file")); |
|
225 | QObject::tr("The x-axis unit could not be found in the file")); | |
178 | } |
|
226 | } | |
179 |
|
227 | |||
180 | std::shared_ptr<IDataSeries> ScalarParserHelper::createSeries() |
|
228 | std::shared_ptr<IDataSeries> ScalarParserHelper::createSeries() | |
181 | { |
|
229 | { | |
182 | return std::make_shared<ScalarSeries>(std::move(m_XAxisData), std::move(m_ValuesData), |
|
230 | return std::make_shared<ScalarSeries>(std::move(m_XAxisData), std::move(m_ValuesData), | |
183 | m_Properties.value(X_AXIS_UNIT_PROPERTY).value<Unit>(), |
|
231 | m_Properties.value(X_AXIS_UNIT_PROPERTY).value<Unit>(), | |
184 | m_Properties.value(VALUES_UNIT_PROPERTY).value<Unit>()); |
|
232 | m_Properties.value(VALUES_UNIT_PROPERTY).value<Unit>()); | |
185 | } |
|
233 | } | |
186 |
|
234 | |||
187 | void ScalarParserHelper::readPropertyLine(const QString &line) |
|
235 | void ScalarParserHelper::readPropertyLine(const QString &line) | |
188 | { |
|
236 | { | |
189 | tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); |
|
237 | tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); | |
190 | } |
|
238 | } | |
191 |
|
239 | |||
192 | void ScalarParserHelper::readResultLine(const QString &line) |
|
240 | void ScalarParserHelper::readResultLine(const QString &line) | |
193 | { |
|
241 | { | |
194 | tryReadResult(m_XAxisData, m_ValuesData, line, valuesIndexes()); |
|
242 | tryReadResult(m_XAxisData, m_ValuesData, line, valuesIndexes()); | |
195 | } |
|
243 | } | |
196 |
|
244 | |||
197 | std::vector<int> ScalarParserHelper::valuesIndexes() const |
|
245 | std::vector<int> ScalarParserHelper::valuesIndexes() const | |
198 | { |
|
246 | { | |
199 | // Only one value to read |
|
247 | // Only one value to read | |
200 | static auto result = std::vector<int>{0}; |
|
248 | static auto result = std::vector<int>{0}; | |
201 | return result; |
|
249 | return result; | |
202 | } |
|
250 | } | |
203 |
|
251 | |||
204 | // /////////////////////// // |
|
252 | // /////////////////////// // | |
205 | // SpectrogramParserHelper // |
|
253 | // SpectrogramParserHelper // | |
206 | // /////////////////////// // |
|
254 | // /////////////////////// // | |
207 |
|
255 | |||
208 | bool SpectrogramParserHelper::checkProperties() |
|
256 | bool SpectrogramParserHelper::checkProperties() | |
209 | { |
|
257 | { | |
210 | /// @todo ALX |
|
258 | // Generates y-axis data from bands extracted (take the middle of the intervals) | |
|
259 | auto minBands = m_Properties.value(MIN_BANDS_PROPERTY).value<std::vector<double> >(); | |||
|
260 | auto maxBands = m_Properties.value(MAX_BANDS_PROPERTY).value<std::vector<double> >(); | |||
|
261 | ||||
|
262 | if (minBands.size() != maxBands.size()) { | |||
|
263 | qCWarning(LOG_AmdaResultParserHelper()) << QObject::tr( | |||
|
264 | "Can't generate y-axis data from bands extracted: bands intervals are invalid"); | |||
|
265 | return false; | |||
|
266 | } | |||
|
267 | ||||
|
268 | return true; | |||
211 | } |
|
269 | } | |
212 |
|
270 | |||
213 | std::shared_ptr<IDataSeries> SpectrogramParserHelper::createSeries() |
|
271 | std::shared_ptr<IDataSeries> SpectrogramParserHelper::createSeries() | |
214 | { |
|
272 | { | |
215 | /// @todo ALX |
|
273 | /// @todo ALX | |
216 | } |
|
274 | } | |
217 |
|
275 | |||
218 | void SpectrogramParserHelper::readPropertyLine(const QString &line) |
|
276 | void SpectrogramParserHelper::readPropertyLine(const QString &line) | |
219 | { |
|
277 | { | |
220 | /// @todo ALX |
|
278 | // Set of functions to test on the line to generate a property. If a function is valid (i.e. a | |
|
279 | // property has been generated for the line), the line is treated as processed and the other | |||
|
280 | // functions are not called | |||
|
281 | std::vector<std::function<bool()> > functions{ | |||
|
282 | // values unit | |||
|
283 | [&] { | |||
|
284 | return tryReadUnit(m_Properties, VALUES_UNIT_PROPERTY, line, | |||
|
285 | SPECTROGRAM_VALUES_UNIT_REGEX); | |||
|
286 | }, | |||
|
287 | // y-axis unit | |||
|
288 | [&] { | |||
|
289 | return tryReadUnit(m_Properties, Y_AXIS_UNIT_PROPERTY, line, | |||
|
290 | SPECTROGRAM_Y_AXIS_UNIT_REGEX); | |||
|
291 | }, | |||
|
292 | // min sampling | |||
|
293 | [&] { | |||
|
294 | return tryReadDouble(m_Properties, MIN_SAMPLING_PROPERTY, line, | |||
|
295 | SPECTROGRAM_MIN_SAMPLING_REGEX); | |||
|
296 | }, | |||
|
297 | // max sampling | |||
|
298 | [&] { | |||
|
299 | return tryReadDouble(m_Properties, MAX_SAMPLING_PROPERTY, line, | |||
|
300 | SPECTROGRAM_MAX_SAMPLING_REGEX); | |||
|
301 | }, | |||
|
302 | // fill value | |||
|
303 | [&] { | |||
|
304 | return tryReadDouble(m_Properties, FILL_VALUE_PROPERTY, line, | |||
|
305 | SPECTROGRAM_FILL_VALUE_REGEX); | |||
|
306 | }, | |||
|
307 | // min bounds of each band | |||
|
308 | [&] { | |||
|
309 | return tryReadDoubles(m_Properties, MIN_BANDS_PROPERTY, line, | |||
|
310 | SPECTROGRAM_MIN_BANDS_REGEX); | |||
|
311 | }, | |||
|
312 | // max bounds of each band | |||
|
313 | [&] { | |||
|
314 | return tryReadDoubles(m_Properties, MAX_BANDS_PROPERTY, line, | |||
|
315 | SPECTROGRAM_MAX_BANDS_REGEX); | |||
|
316 | }}; | |||
|
317 | ||||
|
318 | for (auto function : functions) { | |||
|
319 | // Stops at the first function that is valid | |||
|
320 | if (function()) { | |||
|
321 | return; | |||
|
322 | } | |||
|
323 | } | |||
221 | } |
|
324 | } | |
222 |
|
325 | |||
223 | void SpectrogramParserHelper::readResultLine(const QString &line) |
|
326 | void SpectrogramParserHelper::readResultLine(const QString &line) | |
224 | { |
|
327 | { | |
225 | /// @todo ALX |
|
328 | /// @todo ALX | |
226 | } |
|
329 | } | |
227 |
|
330 | |||
228 | // ////////////////// // |
|
331 | // ////////////////// // | |
229 | // VectorParserHelper // |
|
332 | // VectorParserHelper // | |
230 | // ////////////////// // |
|
333 | // ////////////////// // | |
231 |
|
334 | |||
232 | bool VectorParserHelper::checkProperties() |
|
335 | bool VectorParserHelper::checkProperties() | |
233 | { |
|
336 | { | |
234 | return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY, |
|
337 | return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY, | |
235 | QObject::tr("The x-axis unit could not be found in the file")); |
|
338 | QObject::tr("The x-axis unit could not be found in the file")); | |
236 | } |
|
339 | } | |
237 |
|
340 | |||
238 | std::shared_ptr<IDataSeries> VectorParserHelper::createSeries() |
|
341 | std::shared_ptr<IDataSeries> VectorParserHelper::createSeries() | |
239 | { |
|
342 | { | |
240 | return std::make_shared<VectorSeries>(std::move(m_XAxisData), std::move(m_ValuesData), |
|
343 | return std::make_shared<VectorSeries>(std::move(m_XAxisData), std::move(m_ValuesData), | |
241 | m_Properties.value(X_AXIS_UNIT_PROPERTY).value<Unit>(), |
|
344 | m_Properties.value(X_AXIS_UNIT_PROPERTY).value<Unit>(), | |
242 | m_Properties.value(VALUES_UNIT_PROPERTY).value<Unit>()); |
|
345 | m_Properties.value(VALUES_UNIT_PROPERTY).value<Unit>()); | |
243 | } |
|
346 | } | |
244 |
|
347 | |||
245 | void VectorParserHelper::readPropertyLine(const QString &line) |
|
348 | void VectorParserHelper::readPropertyLine(const QString &line) | |
246 | { |
|
349 | { | |
247 | tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); |
|
350 | tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); | |
248 | } |
|
351 | } | |
249 |
|
352 | |||
250 | void VectorParserHelper::readResultLine(const QString &line) |
|
353 | void VectorParserHelper::readResultLine(const QString &line) | |
251 | { |
|
354 | { | |
252 | tryReadResult(m_XAxisData, m_ValuesData, line, valuesIndexes()); |
|
355 | tryReadResult(m_XAxisData, m_ValuesData, line, valuesIndexes()); | |
253 | } |
|
356 | } | |
254 |
|
357 | |||
255 | std::vector<int> VectorParserHelper::valuesIndexes() const |
|
358 | std::vector<int> VectorParserHelper::valuesIndexes() const | |
256 | { |
|
359 | { | |
257 | // 3 values to read, in order in the file (x, y, z) |
|
360 | // 3 values to read, in order in the file (x, y, z) | |
258 | static auto result = std::vector<int>{0, 1, 2}; |
|
361 | static auto result = std::vector<int>{0, 1, 2}; | |
259 | return result; |
|
362 | return result; | |
260 | } |
|
363 | } |
General Comments 0
You need to be logged in to leave comments.
Login now