mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-09 23:16:00 -06:00
#2659 Ensemble statistics. Add time history resampler class. Extended RiaQDateTimeTools class
This commit is contained in:
parent
d09df6807d
commit
1137a916e0
@ -26,6 +26,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaExtractionTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaFilePathTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveMerger.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaCurveDataTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveResampler.h
|
||||
)
|
||||
|
||||
set (SOURCE_GROUP_SOURCE_FILES
|
||||
@ -55,6 +56,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaExtractionTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaFilePathTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveMerger.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaCurveDataTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveResampler.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
@ -21,10 +21,21 @@
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
|
||||
#include <cvfAssert.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_DECADE = DateTimeSpan(10, 0, 0);
|
||||
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_YEAR = DateTimeSpan(1, 0, 0);
|
||||
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_MONTH = DateTimeSpan(0, 1, 0);
|
||||
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_DAY = DateTimeSpan(0, 0, 1);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -72,6 +83,16 @@ QDateTime RiaQDateTimeTools::fromYears(double years)
|
||||
return RiaQDateTimeTools::addYears(dt, yearsAfterEpoch);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaQDateTimeTools::fromTime_t(time_t t)
|
||||
{
|
||||
auto qdt = createUtcDateTime();
|
||||
qdt.setTime_t(t);
|
||||
return qdt;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -112,6 +133,25 @@ QDateTime RiaQDateTimeTools::addYears(const QDateTime& dt, double years)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaQDateTimeTools::addSpan(const QDateTime& dt, DateTimeSpan span)
|
||||
{
|
||||
return createUtcDateTime(dt)
|
||||
.addYears(span.years())
|
||||
.addMonths(span.months())
|
||||
.addDays(span.days());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaQDateTimeTools::addPeriod(const QDateTime& dt, DateTimePeriod period)
|
||||
{
|
||||
return addSpan(dt, timeSpan(period));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -155,3 +195,90 @@ QDateTime RiaQDateTimeTools::createUtcDateTime(const QDate& date, const QTime& t
|
||||
auto qdt = QDateTime(date, time, currentTimeSpec());
|
||||
return qdt;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaQDateTimeTools::createUtcDateTime(const QDateTime& dt)
|
||||
{
|
||||
auto qdt = QDateTime(dt);
|
||||
qdt.setTimeSpec(currentTimeSpec());
|
||||
return qdt;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaQDateTimeTools::equalTo(const QDateTime& dt1, const QDateTime& dt2)
|
||||
{
|
||||
return dt1.secsTo(dt2) == 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaQDateTimeTools::lessThan(const QDateTime& dt1, const QDateTime& dt2)
|
||||
{
|
||||
// dt1 < dt2
|
||||
auto i = dt1.secsTo(dt2);
|
||||
return dt1.secsTo(dt2) > 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaQDateTimeTools::lessThanOrEqualTo(const QDateTime& dt1, const QDateTime& dt2)
|
||||
{
|
||||
// dt1 <= dt2
|
||||
return dt1.secsTo(dt2) >= 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaQDateTimeTools::biggerThan(const QDateTime& dt1, const QDateTime& dt2)
|
||||
{
|
||||
// dt1 > dt2
|
||||
return dt1.secsTo(dt2) < 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaQDateTimeTools::biggerThanOrEqualTo(const QDateTime& dt1, const QDateTime& dt2)
|
||||
{
|
||||
// dt1 >= dt2
|
||||
return dt1.secsTo(dt2) <= 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const DateTimeSpan RiaQDateTimeTools::timeSpan(DateTimePeriod period)
|
||||
{
|
||||
switch (period)
|
||||
{
|
||||
case DateTimePeriod::DECADE: return TIMESPAN_DECADE;
|
||||
case DateTimePeriod::YEAR: return TIMESPAN_YEAR;
|
||||
case DateTimePeriod::MONTH: return TIMESPAN_MONTH;
|
||||
case DateTimePeriod::DAY: return TIMESPAN_DAY;
|
||||
}
|
||||
CVF_ASSERT(false);
|
||||
return DateTimeSpan();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaQDateTimeTools::truncateTime(const QDateTime& dt, DateTimePeriod period)
|
||||
{
|
||||
switch (period)
|
||||
{
|
||||
case DateTimePeriod::DECADE: return createUtcDateTime(QDate((dt.date().year() / 10) * 10, 1, 1));
|
||||
case DateTimePeriod::YEAR: return createUtcDateTime(QDate(dt.date().year(), 1, 1));
|
||||
case DateTimePeriod::MONTH: return createUtcDateTime(QDate(dt.date().year(), dt.date().month(), 1));
|
||||
case DateTimePeriod::DAY: return createUtcDateTime(QDate(dt.date().year(), dt.date().month(), dt.date().day()));
|
||||
}
|
||||
CVF_ASSERT(false);
|
||||
return createUtcDateTime();
|
||||
}
|
||||
|
@ -26,29 +26,80 @@
|
||||
class QDateTime;
|
||||
class QDate;
|
||||
class QTime;
|
||||
class DateTimeSpan;
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//==================================================================================================
|
||||
enum class DateTimePeriod
|
||||
{
|
||||
DECADE,
|
||||
YEAR,
|
||||
MONTH,
|
||||
DAY
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//==================================================================================================
|
||||
class RiaQDateTimeTools
|
||||
{
|
||||
static const DateTimeSpan TIMESPAN_DECADE;
|
||||
static const DateTimeSpan TIMESPAN_YEAR;
|
||||
static const DateTimeSpan TIMESPAN_MONTH;
|
||||
static const DateTimeSpan TIMESPAN_DAY;
|
||||
|
||||
public:
|
||||
static Qt::TimeSpec currentTimeSpec();
|
||||
|
||||
static QDateTime fromString(const QString& dateString, const QString& format);
|
||||
static QDateTime fromYears(double years);
|
||||
|
||||
static QDateTime fromTime_t(time_t t);
|
||||
|
||||
static QDateTime addMSecs(const QDateTime& dt, double msecs);
|
||||
static QDateTime addDays(const QDateTime& dt, double days);
|
||||
static QDateTime addYears(const QDateTime& dt, double years);
|
||||
static QDateTime addSpan(const QDateTime& dt, DateTimeSpan span);
|
||||
static QDateTime addPeriod(const QDateTime& dt, DateTimePeriod period);
|
||||
|
||||
static QDateTime epoch();
|
||||
|
||||
static QDateTime createUtcDateTime();
|
||||
static QDateTime createUtcDateTime(const QDate& date);
|
||||
static QDateTime createUtcDateTime(const QDate& date, const QTime& time);
|
||||
static QDateTime createUtcDateTime(const QDateTime& dt);
|
||||
|
||||
static bool equalTo(const QDateTime& dt1, const QDateTime& dt2);
|
||||
static bool lessThan(const QDateTime& dt1, const QDateTime& dt2);
|
||||
static bool lessThanOrEqualTo(const QDateTime& dt1, const QDateTime& dt2);
|
||||
static bool biggerThan(const QDateTime& dt1, const QDateTime& dt2);
|
||||
static bool biggerThanOrEqualTo(const QDateTime& dt1, const QDateTime& dt2);
|
||||
|
||||
static const DateTimeSpan timeSpan(DateTimePeriod period);
|
||||
static QDateTime truncateTime(const QDateTime& dt, DateTimePeriod period);
|
||||
|
||||
private:
|
||||
static quint64 secondsInDay();
|
||||
static quint64 secondsInYear();
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class DateTimeSpan
|
||||
{
|
||||
public:
|
||||
DateTimeSpan() : m_years(0), m_months(0), m_days(0) { }
|
||||
DateTimeSpan(int years, int months, int days) : m_years(years), m_months(months), m_days(days) { }
|
||||
|
||||
int years() const { return m_years; }
|
||||
int months() const { return m_months; }
|
||||
int days() const { return m_days; }
|
||||
|
||||
bool isEmpty() { return m_years == 0 && m_months == 0 && m_days; }
|
||||
|
||||
private:
|
||||
int m_years;
|
||||
int m_months;
|
||||
int m_days;
|
||||
};
|
||||
|
@ -0,0 +1,198 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017 Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RiaTimeHistoryCurveResampler.h"
|
||||
|
||||
//QString tostring(const QDateTime& dt)
|
||||
//{
|
||||
// int y = dt.date().year();
|
||||
// int m = dt.date().month();
|
||||
// int d = dt.date().day();
|
||||
//
|
||||
// int h = dt.time().hour();
|
||||
// int mm = dt.time().minute();
|
||||
// int s = dt.time().second();
|
||||
//
|
||||
// return QString("%1.%2.%3 %4:%5:%6").arg(y).arg(m).arg(d).arg(h).arg(mm).arg(s);
|
||||
//}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiaTimeHistoryCurveResampler::RiaTimeHistoryCurveResampler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::setCurveData(const std::vector<double>& values, const std::vector<time_t>& timeSteps)
|
||||
{
|
||||
CVF_ASSERT(values.size() == timeSteps.size());
|
||||
|
||||
m_originalValues = std::make_pair(values, timeSteps);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::resampleAndComputePeriodEndValues(DateTimePeriod period)
|
||||
{
|
||||
computePeriodEndValues(period);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::resampleAndComputeWeightedMeanValues(DateTimePeriod period)
|
||||
{
|
||||
computeWeightedMeanValues(period);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const std::vector<time_t>& RiaTimeHistoryCurveResampler::resampledTimeSteps() const
|
||||
{
|
||||
return m_timeSteps;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const std::vector<double>& RiaTimeHistoryCurveResampler::resampledValues() const
|
||||
{
|
||||
return m_values;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::computeWeightedMeanValues(DateTimePeriod period)
|
||||
{
|
||||
size_t origDataSize = m_originalValues.second.size();
|
||||
size_t oi = 0;
|
||||
auto& origTimeSteps = m_originalValues.second;
|
||||
auto& origValues = m_originalValues.first;
|
||||
|
||||
computeResampledTimeSteps(period);
|
||||
|
||||
m_values.reserve(m_timeSteps.size());
|
||||
for (size_t i = 0; i < m_timeSteps.size(); i++)
|
||||
{
|
||||
double wMean = 0.0;
|
||||
time_t periodStart = i > 0 ? m_timeSteps[i - 1] : origTimeSteps[0];
|
||||
time_t periodEnd = m_timeSteps[i];
|
||||
time_t periodLength = periodEnd - periodStart;
|
||||
|
||||
while(true)
|
||||
{
|
||||
if (oi == origDataSize) break;
|
||||
|
||||
if (oi == 0)
|
||||
{
|
||||
if (origTimeSteps[oi] == m_timeSteps[i])
|
||||
{
|
||||
wMean += origValues[0];
|
||||
oi++;
|
||||
break;
|
||||
}
|
||||
oi++;
|
||||
continue;
|
||||
}
|
||||
|
||||
time_t startTime = std::max(origTimeSteps[oi-1], periodStart);
|
||||
time_t endTime = std::min(origTimeSteps[oi], periodEnd);
|
||||
|
||||
wMean += origValues[oi] * (endTime - startTime) / periodLength;
|
||||
|
||||
if (origTimeSteps[oi] > m_timeSteps[i]) break;
|
||||
if (origTimeSteps[oi] == m_timeSteps[i])
|
||||
{
|
||||
oi++;
|
||||
break;
|
||||
}
|
||||
oi++;
|
||||
}
|
||||
|
||||
m_values.push_back(wMean);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::computePeriodEndValues(DateTimePeriod period)
|
||||
{
|
||||
size_t origDataSize = m_originalValues.second.size();
|
||||
size_t origIndex = 0;
|
||||
auto& origTimeSteps = m_originalValues.second;
|
||||
auto& origValues = m_originalValues.first;
|
||||
|
||||
computeResampledTimeSteps(period);
|
||||
|
||||
m_values.reserve(m_timeSteps.size());
|
||||
for (size_t i = 0; i < m_timeSteps.size(); i++)
|
||||
{
|
||||
while (origIndex < origDataSize && origTimeSteps[origIndex] < m_timeSteps[i]) origIndex++;
|
||||
m_values.push_back(origValues[origIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::clearData()
|
||||
{
|
||||
m_timeSteps.clear();
|
||||
m_values.clear();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaTimeHistoryCurveResampler::computeResampledTimeSteps(DateTimePeriod period)
|
||||
{
|
||||
CVF_ASSERT(m_originalValues.size() > 0);
|
||||
|
||||
auto firstOriginalTimeStep = QDT::fromTime_t(m_originalValues.second.front());
|
||||
auto lastOriginalTimeStep = QDT::fromTime_t(m_originalValues.second.back());
|
||||
|
||||
clearData();
|
||||
auto currTimeStep = firstResampledTimeStep(firstOriginalTimeStep, period);
|
||||
while (QDT::lessThanOrEqualTo(currTimeStep, lastOriginalTimeStep))
|
||||
{
|
||||
auto ss1 = currTimeStep.toString();
|
||||
|
||||
m_timeSteps.push_back(currTimeStep.toTime_t());
|
||||
currTimeStep = QDT::addPeriod(currTimeStep, period);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RiaTimeHistoryCurveResampler::firstResampledTimeStep(const QDateTime& firstTimeStep, DateTimePeriod period)
|
||||
{
|
||||
QDateTime truncatedTime = QDT::truncateTime(firstTimeStep, period);
|
||||
|
||||
if (QDT::lessThan(truncatedTime, firstTimeStep)) return QDT::addPeriod(truncatedTime, period);
|
||||
return truncatedTime;
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017 Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RiaCurveDataTools.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
using QDT = RiaQDateTimeTools;
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RiaTimeHistoryCurveResampler
|
||||
{
|
||||
public:
|
||||
RiaTimeHistoryCurveResampler();
|
||||
|
||||
void setCurveData(const std::vector<double>& values,
|
||||
const std::vector<time_t>& timeSteps);
|
||||
|
||||
void resampleAndComputePeriodEndValues(DateTimePeriod period);
|
||||
void resampleAndComputeWeightedMeanValues(DateTimePeriod period);
|
||||
|
||||
const std::vector<time_t>& resampledTimeSteps() const;
|
||||
const std::vector<double>& resampledValues() const;
|
||||
|
||||
private:
|
||||
void computeWeightedMeanValues(DateTimePeriod period);
|
||||
void computePeriodEndValues(DateTimePeriod period);
|
||||
|
||||
void clearData();
|
||||
void computeResampledTimeSteps(DateTimePeriod period);
|
||||
QDateTime firstResampledTimeStep(const QDateTime& firstTimestep, DateTimePeriod period);
|
||||
|
||||
private:
|
||||
std::pair<std::vector<double>, std::vector<time_t>> m_originalValues;
|
||||
|
||||
std::vector<time_t> m_timeSteps;
|
||||
std::vector<double> m_values;
|
||||
};
|
@ -43,6 +43,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaTextFileCompare-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifCaseRealizationParametersReader-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RigWellLogExtractor-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifEclipseSummaryAddress-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveTools-Test.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
413
ApplicationCode/UnitTests/RiaTimeHistoryCurveTools-Test.cpp
Normal file
413
ApplicationCode/UnitTests/RiaTimeHistoryCurveTools-Test.cpp
Normal file
@ -0,0 +1,413 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "RiaTimeHistoryCurveResampler.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Helpers
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
static time_t toTime_t(const QString& timeString)
|
||||
{
|
||||
return RiaQDateTimeTools::fromString(timeString, "yyyy-MM-dd").toTime_t();
|
||||
}
|
||||
|
||||
static std::vector<time_t> toTime_tVector(const std::vector<QString>& timeStrings)
|
||||
{
|
||||
std::vector<time_t> tv;
|
||||
for (auto& ts : timeStrings) tv.push_back(toTime_t(ts));
|
||||
return tv;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_NoPeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-03",
|
||||
"2018-02-27"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(0, resampler.resampledTimeSteps().size());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_Days)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-03",
|
||||
"2018-02-07"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::DAY);
|
||||
|
||||
EXPECT_EQ(5, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-03"), resampler.resampledTimeSteps()[0]);
|
||||
EXPECT_EQ(toTime_t("2018-02-04"), resampler.resampledTimeSteps()[1]);
|
||||
EXPECT_EQ(toTime_t("2018-02-05"), resampler.resampledTimeSteps()[2]);
|
||||
EXPECT_EQ(toTime_t("2018-02-06"), resampler.resampledTimeSteps()[3]);
|
||||
EXPECT_EQ(toTime_t("2018-02-07"), resampler.resampledTimeSteps()[4]);
|
||||
|
||||
EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(5.0, resampler.resampledValues()[1], 1e-12);
|
||||
EXPECT_NEAR(5.0, resampler.resampledValues()[2], 1e-12);
|
||||
EXPECT_NEAR(5.0, resampler.resampledValues()[3], 1e-12);
|
||||
EXPECT_NEAR(5.0, resampler.resampledValues()[4], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_Decade)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"1999-02-03",
|
||||
"2005-06-06",
|
||||
"2012-02-07"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::DECADE);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2000-01-01"), resampler.resampledTimeSteps()[0]);
|
||||
EXPECT_EQ(toTime_t("2010-01-01"), resampler.resampledTimeSteps()[1]);
|
||||
|
||||
time_t t1 = toTime_t("1999-02-03");
|
||||
time_t t2 = toTime_t("2005-06-06");
|
||||
time_t t3 = toTime_t("2012-02-07");
|
||||
time_t tp1 = toTime_t("2000-01-01");
|
||||
time_t tp2 = toTime_t("2010-01-01");
|
||||
|
||||
double value1 = 5.0;
|
||||
double value2 = (5.0 * (t2 - tp1) + 7.0 * (tp2 - t2)) / (tp2 - tp1);
|
||||
|
||||
EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartBeforePeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-01-20",
|
||||
"2018-01-29",
|
||||
"2018-02-03",
|
||||
"2018-02-27",
|
||||
"2018-03-02"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0,
|
||||
13.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
time_t timePeriod1 = toTime_t("2018-02-01") - toTime_t("2018-01-20");
|
||||
time_t timePeriod2 = toTime_t("2018-03-01") - toTime_t("2018-02-01");
|
||||
int timeDay = 60 * 60 * 24;
|
||||
|
||||
double value1 =
|
||||
(5.0 * 9 +
|
||||
7.0 * 3) * timeDay / timePeriod1;
|
||||
|
||||
double value2 =
|
||||
(7.0 * 2 +
|
||||
11.0 * 24 +
|
||||
13.0 * 2) * timeDay / timePeriod2;
|
||||
|
||||
EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartBeforePeriod_TimeStepsMatchPeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-01-20",
|
||||
"2018-02-01",
|
||||
"2018-02-03",
|
||||
"2018-03-01",
|
||||
"2018-03-02"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0,
|
||||
13.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
time_t timePeriod1 = toTime_t("2018-02-01") - toTime_t("2018-01-20");
|
||||
time_t timePeriod2 = toTime_t("2018-03-01") - toTime_t("2018-02-01");
|
||||
int timeDay = 60 * 60 * 24;
|
||||
|
||||
double value1 =
|
||||
(5.0 * 12) * timeDay / timePeriod1;
|
||||
|
||||
double value2 =
|
||||
(7.0 * 2 +
|
||||
11.0 * 26) * timeDay / timePeriod2;
|
||||
|
||||
EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartAndEndMatchPeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-01",
|
||||
"2018-02-10",
|
||||
"2018-03-01"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
time_t timePeriod = toTime_t("2018-03-01") - toTime_t("2018-02-01");
|
||||
int timeDay = 60 * 60 * 24;
|
||||
|
||||
double value1 = 3.0;
|
||||
double value2 =
|
||||
(5.0 * 9 +
|
||||
7.0 * 19) * timeDay / timePeriod;
|
||||
|
||||
EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartMatchPeriodStart)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-01",
|
||||
"2018-02-10",
|
||||
"2018-03-01",
|
||||
"2018-03-02"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
time_t timePeriod = toTime_t("2018-03-01") - toTime_t("2018-02-01");
|
||||
int timeDay = 60 * 60 * 24;
|
||||
|
||||
double value1 = 3.0;
|
||||
double value2 =
|
||||
(5.0 * 9 +
|
||||
7.0 * 19) * timeDay / timePeriod;
|
||||
|
||||
EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SamplesStartBeforePeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-01-30",
|
||||
"2018-02-10",
|
||||
"2018-03-05",
|
||||
"2018-03-02"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
EXPECT_NEAR(5.0, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(7.0, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SamplesStartMatchPeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-01",
|
||||
"2018-02-10",
|
||||
"2018-03-01",
|
||||
"2018-03-02"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(7.0, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SamplesStartAndEndMatchPeriod)
|
||||
{
|
||||
std::vector<QString> timeStrings(
|
||||
{
|
||||
"2018-02-01",
|
||||
"2018-02-10",
|
||||
"2018-03-01"
|
||||
}
|
||||
);
|
||||
|
||||
std::vector<double> dataValues(
|
||||
{
|
||||
3.0,
|
||||
5.0,
|
||||
7.0,
|
||||
11.0
|
||||
}
|
||||
);
|
||||
|
||||
RiaTimeHistoryCurveResampler resampler;
|
||||
resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
|
||||
resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);
|
||||
|
||||
EXPECT_EQ(2, resampler.resampledTimeSteps().size());
|
||||
EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
|
||||
EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());
|
||||
|
||||
EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
|
||||
EXPECT_NEAR(7.0, resampler.resampledValues()[1], 1e-12);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user