mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-26 15:10:22 -05:00
#14543 Use time from simulation start in Show Plot Data
When the summary plot time axis is configured to show time from simulation start, the text produced by Show Plot Data and the ASCII export now reports the same values instead of date and time. The time column header becomes "Time [<unit>]", and each row reports the elapsed time relative to the first time step of the first curve, scaled to the display unit selected on the time axis. Plots using the date based time axis are unchanged. Time in months and years is computed using calendar arithmetic instead of a fixed number of seconds per unit, so a time step exactly N calendar months or years after the simulation start reports exactly N. This makes resampled data report whole numbers also when a leap year is part of the interval. The calendar arithmetic is available as RiaQDateTimeTools::calendarYearsBetween() and calendarMonthsBetween(), and is used both when plotting the curves and when reporting the time column.
This commit is contained in:
1 parent
ce12272721
commit
9b268e6d98
12 files changed
+340
-32
No files matched your search
@@ -207,6 +207,60 @@ QDateTime RiaQDateTimeTools::createDateTime( const QDate& date, Qt::TimeSpec tim
|
||||
return date.startOfDay( timeSpec );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaQDateTimeTools::calendarYearsBetween( const QDateTime& startTime, const QDateTime& endTime )
|
||||
{
|
||||
return calendarUnitsBetween( startTime, endTime, false );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaQDateTimeTools::calendarMonthsBetween( const QDateTime& startTime, const QDateTime& endTime )
|
||||
{
|
||||
return calendarUnitsBetween( startTime, endTime, true );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// A calendar month or year has a varying number of seconds. Count the number of whole calendar units, and express the
|
||||
/// remainder as a fraction of the calendar unit it falls inside.
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaQDateTimeTools::calendarUnitsBetween( const QDateTime& startTime, const QDateTime& endTime, bool useMonths )
|
||||
{
|
||||
if ( startTime == endTime ) return 0.0;
|
||||
|
||||
auto addUnits = [&startTime, useMonths]( int unitCount )
|
||||
{ return useMonths ? startTime.addMonths( unitCount ) : startTime.addYears( unitCount ); };
|
||||
|
||||
const int sign = ( endTime > startTime ) ? 1 : -1;
|
||||
|
||||
auto isBeyondEnd = [&endTime, sign]( const QDateTime& dt ) { return sign > 0 ? dt > endTime : dt < endTime; };
|
||||
|
||||
// Start off with an estimate based on the average length of the calendar unit, then adjust to the exact count
|
||||
const double averageSecondsInUnit = useMonths ? 2629746.0 : 31556952.0;
|
||||
|
||||
int unitCount = static_cast<int>( std::abs( static_cast<double>( startTime.secsTo( endTime ) ) ) / averageSecondsInUnit );
|
||||
|
||||
while ( unitCount > 0 && isBeyondEnd( addUnits( unitCount * sign ) ) )
|
||||
{
|
||||
unitCount--;
|
||||
}
|
||||
while ( !isBeyondEnd( addUnits( ( unitCount + 1 ) * sign ) ) )
|
||||
{
|
||||
unitCount++;
|
||||
}
|
||||
|
||||
const QDateTime lower = addUnits( unitCount * sign );
|
||||
const QDateTime upper = addUnits( ( unitCount + 1 ) * sign );
|
||||
|
||||
const double secondsInUnit = std::abs( static_cast<double>( lower.secsTo( upper ) ) );
|
||||
const double fraction = secondsInUnit > 0.0 ? std::abs( static_cast<double>( lower.secsTo( endTime ) ) ) / secondsInUnit : 0.0;
|
||||
|
||||
return sign * ( unitCount + fraction );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -58,6 +60,12 @@ public:
|
||||
|
||||
static QDateTime createDateTime( const QDate& date, Qt::TimeSpec timeSpec = Qt::LocalTime );
|
||||
|
||||
// Number of calendar years/months between two time steps. The remainder is expressed as a fraction of the calendar
|
||||
// unit it falls inside, so a time step exactly N calendar years/months after the start reports exactly N. Time
|
||||
// steps before the start time are reported as negative values.
|
||||
static double calendarYearsBetween( const QDateTime& startTime, const QDateTime& endTime );
|
||||
static double calendarMonthsBetween( const QDateTime& startTime, const QDateTime& endTime );
|
||||
|
||||
static QDateTime epoch();
|
||||
|
||||
static QDateTime createUtcDateTime();
|
||||
@@ -99,6 +107,8 @@ public:
|
||||
getTimeStepsWithinSelectedRange( const std::vector<QDateTime>& timeSteps, const QDateTime& fromTimeStep, const QDateTime& toTimeStep );
|
||||
|
||||
private:
|
||||
static double calendarUnitsBetween( const QDateTime& startTime, const QDateTime& endTime, bool useMonths );
|
||||
|
||||
static const DateTimeSpan TIMESPAN_MINUTE;
|
||||
static const DateTimeSpan TIMESPAN_HOUR;
|
||||
static const DateTimeSpan TIMESPAN_DAY;
|
||||
|
||||
Reference in new issue
Block a user