From 7ff7615687bf17f7a86020db6cea981509de5b67 2017-07-10 08:09:24 From: Alexandre Leroux Date: 2017-07-10 08:09:24 Subject: [PATCH] Amda provider (3) Parses results file --- diff --git a/plugins/amda/include/AmdaResultParser.h b/plugins/amda/include/AmdaResultParser.h new file mode 100644 index 0000000..8c4bc61 --- /dev/null +++ b/plugins/amda/include/AmdaResultParser.h @@ -0,0 +1,19 @@ +#ifndef SCIQLOP_AMDARESULTPARSER_H +#define SCIQLOP_AMDARESULTPARSER_H + +#include "AmdaGlobal.h" + +#include + +#include + +class IDataSeries; + +Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaResultParser) + +struct SCIQLOP_AMDA_EXPORT AmdaResultParser { + + static std::shared_ptr readTxt(const QString &filePath) noexcept; +}; + +#endif // SCIQLOP_AMDARESULTPARSER_H diff --git a/plugins/amda/src/AmdaProvider.cpp b/plugins/amda/src/AmdaProvider.cpp index 550d3dd..88777fc 100644 --- a/plugins/amda/src/AmdaProvider.cpp +++ b/plugins/amda/src/AmdaProvider.cpp @@ -1,4 +1,6 @@ #include "AmdaProvider.h" +#include "AmdaResultParser.h" + #include #include @@ -107,6 +109,15 @@ void AmdaProvider::httpDownloadFinished() noexcept { if (impl->m_File) { impl->m_File->close(); + + // Parse results file + if (auto dataSeries = AmdaResultParser::readTxt(impl->m_File->fileName())) { + emit dataProvided(impl->m_Token, dataSeries, impl->m_Params.m_Time); + } + else { + /// @todo ALX : debug + } + impl->m_File = nullptr; } diff --git a/plugins/amda/src/AmdaResultParser.cpp b/plugins/amda/src/AmdaResultParser.cpp new file mode 100644 index 0000000..b929c7b --- /dev/null +++ b/plugins/amda/src/AmdaResultParser.cpp @@ -0,0 +1,70 @@ +#include "AmdaResultParser.h" + +#include + +#include +#include + +Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser") + +namespace { + +/// Format for dates in result files +const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"); + +/// @todo ALX +double doubleDate(const QString &stringDate) noexcept +{ + auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT); + return dateTime.toMSecsSinceEpoch() / 1000.; +} + +} // namespace + +std::shared_ptr AmdaResultParser::readTxt(const QString &filePath) noexcept +{ + QFile file{filePath}; + + if (!file.open(QFile::ReadOnly | QIODevice::Text)) { + qCCritical(LOG_AmdaResultParser()) + << QObject::tr("Can't retrieve AMDA data from file %1: %2") + .arg(filePath, file.errorString()); + return nullptr; + } + + auto xData = QVector{}; + auto valuesData = QVector{}; + + QTextStream stream{&file}; + + // Ignore comment lines (3 lines) + stream.readLine(); + stream.readLine(); + stream.readLine(); + + QString line{}; + auto lineRegex = QRegExp{QStringLiteral("\\s+")}; + while (stream.readLineInto(&line)) { + auto lineData = line.split(lineRegex, QString::SkipEmptyParts); + if (lineData.size() == 2) { + // X : the data is converted from date to double (in secs) + xData.push_back(doubleDate(lineData.at(0))); + + // Value + valuesData.push_back(lineData.at(1).toDouble()); + } + else { + /// @todo ALX : log + } + } + + /// @todo ALX : handle units + auto scalarSeries = std::make_shared(xData.size(), Unit{"nT", true}, Unit{}); + + const auto count = xData.size(); + for (auto i = 0; i < count; ++i) { + scalarSeries->setData(i, xData.at(i), valuesData.at(i)); + } + + return scalarSeries; +}