##// END OF EJS Templates
Creates constructor for ScalarSeries that directly takes vectors...
Alexandre Leroux -
r392:cae900f78dff
parent child
Show More
@@ -1,105 +1,116
1 #ifndef SCIQLOP_ARRAYDATA_H
1 #ifndef SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
3
3
4 #include <QReadLocker>
4 #include <QReadLocker>
5 #include <QReadWriteLock>
5 #include <QReadWriteLock>
6 #include <QVector>
6 #include <QVector>
7 /**
7 /**
8 * @brief The ArrayData class represents a dataset for a data series.
8 * @brief The ArrayData class represents a dataset for a data series.
9 *
9 *
10 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
10 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
11 * template-parameter.
11 * template-parameter.
12 *
12 *
13 * @tparam Dim the dimension of the ArrayData (one or two)
13 * @tparam Dim the dimension of the ArrayData (one or two)
14 * @sa IDataSeries
14 * @sa IDataSeries
15 */
15 */
16 template <int Dim>
16 template <int Dim>
17 class ArrayData {
17 class ArrayData {
18 public:
18 public:
19 /**
19 /**
20 * Ctor for a unidimensional ArrayData
20 * Ctor for a unidimensional ArrayData
21 * @param nbColumns the number of values the ArrayData will hold
21 * @param nbColumns the number of values the ArrayData will hold
22 */
22 */
23 template <int D = Dim, typename = std::enable_if_t<D == 1> >
23 template <int D = Dim, typename = std::enable_if_t<D == 1> >
24 explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}}
24 explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}}
25 {
25 {
26 QWriteLocker locker{&m_Lock};
26 QWriteLocker locker{&m_Lock};
27 m_Data[0].resize(nbColumns);
27 m_Data[0].resize(nbColumns);
28 }
28 }
29
29
30 /**
31 * Ctor for a unidimensional ArrayData
32 * @param data the data the ArrayData will hold
33 */
34 template <int D = Dim, typename = std::enable_if_t<D == 1> >
35 explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
36 {
37 QWriteLocker locker{&m_Lock};
38 m_Data[0] = std::move(data);
39 }
40
30 /// Copy ctor
41 /// Copy ctor
31 explicit ArrayData(const ArrayData &other)
42 explicit ArrayData(const ArrayData &other)
32 {
43 {
33 QReadLocker otherLocker{&other.m_Lock};
44 QReadLocker otherLocker{&other.m_Lock};
34 QWriteLocker locker{&m_Lock};
45 QWriteLocker locker{&m_Lock};
35 m_Data = other.m_Data;
46 m_Data = other.m_Data;
36 }
47 }
37
48
38 /**
49 /**
39 * Sets a data at a specified index. The index has to be valid to be effective
50 * Sets a data at a specified index. The index has to be valid to be effective
40 * @param index the index to which the data will be set
51 * @param index the index to which the data will be set
41 * @param data the data to set
52 * @param data the data to set
42 * @remarks this method is only available for a unidimensional ArrayData
53 * @remarks this method is only available for a unidimensional ArrayData
43 */
54 */
44 template <int D = Dim, typename = std::enable_if_t<D == 1> >
55 template <int D = Dim, typename = std::enable_if_t<D == 1> >
45 void setData(int index, double data) noexcept
56 void setData(int index, double data) noexcept
46 {
57 {
47 QWriteLocker locker{&m_Lock};
58 QWriteLocker locker{&m_Lock};
48 if (index >= 0 && index < m_Data.at(0).size()) {
59 if (index >= 0 && index < m_Data.at(0).size()) {
49 m_Data[0].replace(index, data);
60 m_Data[0].replace(index, data);
50 }
61 }
51 }
62 }
52
63
53 /**
64 /**
54 * @return the data as a vector
65 * @return the data as a vector
55 * @remarks this method is only available for a unidimensional ArrayData
66 * @remarks this method is only available for a unidimensional ArrayData
56 */
67 */
57 template <int D = Dim, typename = std::enable_if_t<D == 1> >
68 template <int D = Dim, typename = std::enable_if_t<D == 1> >
58 QVector<double> data() const noexcept
69 QVector<double> data() const noexcept
59 {
70 {
60 QReadLocker locker{&m_Lock};
71 QReadLocker locker{&m_Lock};
61 return m_Data[0];
72 return m_Data[0];
62 }
73 }
63
74
64 /**
75 /**
65 * @return the data as a vector
76 * @return the data as a vector
66 * @remarks this method is only available for a unidimensional ArrayData
77 * @remarks this method is only available for a unidimensional ArrayData
67 */
78 */
68 template <int D = Dim, typename = std::enable_if_t<D == 1> >
79 template <int D = Dim, typename = std::enable_if_t<D == 1> >
69 QVector<double> data(double tStart, double tEnd) const noexcept
80 QVector<double> data(double tStart, double tEnd) const noexcept
70 {
81 {
71 QReadLocker locker{&m_Lock};
82 QReadLocker locker{&m_Lock};
72 return m_Data.at(tStart);
83 return m_Data.at(tStart);
73 }
84 }
74
85
75 // TODO Comment
86 // TODO Comment
76 template <int D = Dim, typename = std::enable_if_t<D == 1> >
87 template <int D = Dim, typename = std::enable_if_t<D == 1> >
77 void merge(const ArrayData<1> &arrayData)
88 void merge(const ArrayData<1> &arrayData)
78 {
89 {
79 QWriteLocker locker{&m_Lock};
90 QWriteLocker locker{&m_Lock};
80 if (!m_Data.empty()) {
91 if (!m_Data.empty()) {
81 QReadLocker otherLocker{&arrayData.m_Lock};
92 QReadLocker otherLocker{&arrayData.m_Lock};
82 m_Data[0] += arrayData.data();
93 m_Data[0] += arrayData.data();
83 }
94 }
84 }
95 }
85
96
86 template <int D = Dim, typename = std::enable_if_t<D == 1> >
97 template <int D = Dim, typename = std::enable_if_t<D == 1> >
87 int size() const
98 int size() const
88 {
99 {
89 QReadLocker locker{&m_Lock};
100 QReadLocker locker{&m_Lock};
90 return m_Data[0].size();
101 return m_Data[0].size();
91 }
102 }
92
103
93 void clear()
104 void clear()
94 {
105 {
95 QWriteLocker locker{&m_Lock};
106 QWriteLocker locker{&m_Lock};
96 m_Data.clear();
107 m_Data.clear();
97 }
108 }
98
109
99
110
100 private:
111 private:
101 QVector<QVector<double> > m_Data;
112 QVector<QVector<double> > m_Data;
102 mutable QReadWriteLock m_Lock;
113 mutable QReadWriteLock m_Lock;
103 };
114 };
104
115
105 #endif // SCIQLOP_ARRAYDATA_H
116 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,30 +1,39
1 #ifndef SCIQLOP_SCALARSERIES_H
1 #ifndef SCIQLOP_SCALARSERIES_H
2 #define SCIQLOP_SCALARSERIES_H
2 #define SCIQLOP_SCALARSERIES_H
3
3
4 #include <Data/DataSeries.h>
4 #include <Data/DataSeries.h>
5
5
6 /**
6 /**
7 * @brief The ScalarSeries class is the implementation for a data series representing a scalar.
7 * @brief The ScalarSeries class is the implementation for a data series representing a scalar.
8 */
8 */
9 class ScalarSeries : public DataSeries<1> {
9 class ScalarSeries : public DataSeries<1> {
10 public:
10 public:
11 /**
11 /**
12 * Ctor
12 * Ctor
13 * @param size the number of data the series will hold
13 * @param size the number of data the series will hold
14 * @param xAxisUnit x-axis unit
14 * @param xAxisUnit x-axis unit
15 * @param valuesUnit values unit
15 * @param valuesUnit values unit
16 */
16 */
17 explicit ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit);
17 explicit ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit);
18
18
19 /**
19 /**
20 * Ctor with two vectors. The vectors must have the same size, otherwise a ScalarSeries with no
21 * values will be created.
22 * @param xAxisData x-axis data
23 * @param valuesData values data
24 */
25 explicit ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
26 const Unit &xAxisUnit, const Unit &valuesUnit);
27
28 /**
20 * Sets data for a specific index. The index has to be valid to be effective
29 * Sets data for a specific index. The index has to be valid to be effective
21 * @param index the index to which the data will be set
30 * @param index the index to which the data will be set
22 * @param x the x-axis data
31 * @param x the x-axis data
23 * @param value the value data
32 * @param value the value data
24 */
33 */
25 void setData(int index, double x, double value) noexcept;
34 void setData(int index, double x, double value) noexcept;
26
35
27 std::unique_ptr<IDataSeries> clone() const;
36 std::unique_ptr<IDataSeries> clone() const;
28 };
37 };
29
38
30 #endif // SCIQLOP_SCALARSERIES_H
39 #endif // SCIQLOP_SCALARSERIES_H
@@ -1,18 +1,25
1 #include <Data/ScalarSeries.h>
1 #include <Data/ScalarSeries.h>
2
2
3 ScalarSeries::ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit)
3 ScalarSeries::ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit)
4 : DataSeries{std::make_shared<ArrayData<1> >(size), xAxisUnit,
4 : DataSeries{std::make_shared<ArrayData<1> >(size), xAxisUnit,
5 std::make_shared<ArrayData<1> >(size), valuesUnit}
5 std::make_shared<ArrayData<1> >(size), valuesUnit}
6 {
6 {
7 }
7 }
8
8
9 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
10 const Unit &xAxisUnit, const Unit &valuesUnit)
11 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
12 std::make_shared<ArrayData<1> >(std::move(valuesData)), valuesUnit}
13 {
14 }
15
9 void ScalarSeries::setData(int index, double x, double value) noexcept
16 void ScalarSeries::setData(int index, double x, double value) noexcept
10 {
17 {
11 xAxisData()->setData(index, x);
18 xAxisData()->setData(index, x);
12 valuesData()->setData(index, value);
19 valuesData()->setData(index, value);
13 }
20 }
14
21
15 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
22 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
16 {
23 {
17 return std::make_unique<ScalarSeries>(*this);
24 return std::make_unique<ScalarSeries>(*this);
18 }
25 }
@@ -1,70 +1,64
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
7
8 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
8 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
9
9
10 namespace {
10 namespace {
11
11
12 /// Format for dates in result files
12 /// Format for dates in result files
13 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
13 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
14
14
15 /// @todo ALX
15 /// @todo ALX
16 double doubleDate(const QString &stringDate) noexcept
16 double doubleDate(const QString &stringDate) noexcept
17 {
17 {
18 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
18 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
19 return dateTime.toMSecsSinceEpoch() / 1000.;
19 return dateTime.toMSecsSinceEpoch() / 1000.;
20 }
20 }
21
21
22 } // namespace
22 } // namespace
23
23
24 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath) noexcept
24 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath) noexcept
25 {
25 {
26 QFile file{filePath};
26 QFile file{filePath};
27
27
28 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
28 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
29 qCCritical(LOG_AmdaResultParser())
29 qCCritical(LOG_AmdaResultParser())
30 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
30 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
31 .arg(filePath, file.errorString());
31 .arg(filePath, file.errorString());
32 return nullptr;
32 return nullptr;
33 }
33 }
34
34
35 auto xData = QVector<double>{};
35 auto xData = QVector<double>{};
36 auto valuesData = QVector<double>{};
36 auto valuesData = QVector<double>{};
37
37
38 QTextStream stream{&file};
38 QTextStream stream{&file};
39
39
40 // Ignore comment lines (3 lines)
40 // Ignore comment lines (3 lines)
41 stream.readLine();
41 stream.readLine();
42 stream.readLine();
42 stream.readLine();
43 stream.readLine();
43 stream.readLine();
44
44
45 QString line{};
45 QString line{};
46 auto lineRegex = QRegExp{QStringLiteral("\\s+")};
46 auto lineRegex = QRegExp{QStringLiteral("\\s+")};
47 while (stream.readLineInto(&line)) {
47 while (stream.readLineInto(&line)) {
48 auto lineData = line.split(lineRegex, QString::SkipEmptyParts);
48 auto lineData = line.split(lineRegex, QString::SkipEmptyParts);
49 if (lineData.size() == 2) {
49 if (lineData.size() == 2) {
50 // X : the data is converted from date to double (in secs)
50 // X : the data is converted from date to double (in secs)
51 xData.push_back(doubleDate(lineData.at(0)));
51 xData.push_back(doubleDate(lineData.at(0)));
52
52
53 // Value
53 // Value
54 valuesData.push_back(lineData.at(1).toDouble());
54 valuesData.push_back(lineData.at(1).toDouble());
55 }
55 }
56 else {
56 else {
57 /// @todo ALX : log
57 /// @todo ALX : log
58 }
58 }
59 }
59 }
60
60
61 /// @todo ALX : handle units
61 /// @todo ALX : handle units
62 auto scalarSeries = std::make_shared<ScalarSeries>(xData.size(), Unit{"nT", true}, Unit{});
62 return std::make_shared<ScalarSeries>(std::move(xData), std::move(valuesData), Unit{"nT", true},
63
63 Unit{});
64 const auto count = xData.size();
65 for (auto i = 0; i < count; ++i) {
66 scalarSeries->setData(i, xData.at(i), valuesData.at(i));
67 }
68
69 return scalarSeries;
70 }
64 }
General Comments 0
You need to be logged in to leave comments. Login now