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