Skip to content

Commit

Permalink
#9773 Extract unit from names, and default category if provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriben committed Mar 13, 2023
1 parent 363a88c commit 48c7074
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include "RifEclipseSummaryAddress.h"
#include "RimPlotCurve.h"

#include "RifCsvUserDataParser.h"
Expand All @@ -41,6 +42,7 @@ class AsciiDataParseOptions
: useCustomDateTimeFormat( false )
, assumeNumericDataColumns( false )
, curveSymbolSkipDistance( 0.0f )
, defaultCategory( RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_INVALID )
{
}

Expand All @@ -59,6 +61,8 @@ class AsciiDataParseOptions

bool assumeNumericDataColumns;

RifEclipseSummaryAddress::SummaryVarCategory defaultCategory;

RiuQwtPlotCurveDefines::LineStyleEnum curveLineStyle;
RiuPlotCurveSymbol::PointSymbolEnum curveSymbol;
float curveSymbolSkipDistance;
Expand Down
39 changes: 35 additions & 4 deletions ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RifCsvUserDataParser.h"

#include "RifEclipseSummaryAddress.h"
#include "RifEclipseUserDataKeywordTools.h"
#include "RifEclipseUserDataParserTools.h"
#include "RifFileParseTools.h"
Expand Down Expand Up @@ -329,8 +330,9 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStream, const Ascii
QStringList unitTexts;
QStringList names;

auto startOfLineWithDataValues = dataStream->pos();
bool hasDataValues = false;
auto startOfLineWithDataValues = dataStream->pos();
bool hasDataValues = false;
QString nameFromData;
while ( !hasDataValues )
{
QString candidateLine = dataStream->readLine();
Expand All @@ -339,7 +341,14 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStream, const Ascii
for ( const auto& text : candidateColumnHeaders )
{
if ( RiaStdStringTools::isNumber( text.toStdString(), parseOptions.locale.decimalPoint().toLatin1() ) )
{
hasDataValues = true;
}
else if ( nameFromData.isEmpty() )
{
// Keep the first non-number data field as a possible name.
nameFromData = text;
}
}

if ( !hasDataValues && candidateColumnHeaders.size() == columnHeaders.size() )
Expand All @@ -365,6 +374,23 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStream, const Ascii
{
QString colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( columnHeaders[iCol] );

QString unit;

// Check if unit is part of the column name in parentheses, e.g. "VECTOR (unit)".
QRegExp exp( "\\((.*)\\)" );
if ( exp.indexIn( colName ) >= 0 )
{
// "VECTOR (unit)" ==> "(unit)"
QString fullCapture = exp.cap( 0 );
// "VECTOR (unit)" ==> "unit"
QString unitCapture = exp.cap( 1 );

unit = unitCapture;

// Remove unit from name
colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( colName.remove( fullCapture ) );
}

if ( iCol < names.size() )
{
QString name = RiaTextStringTools::trimAndRemoveDoubleSpaces( names[iCol] );
Expand All @@ -375,12 +401,17 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStream, const Ascii
}
}

QString unit;
if ( iCol < unitTexts.size() ) unit = unitTexts[iCol];

RifEclipseSummaryAddress addr = RifEclipseSummaryAddress::fromEclipseTextAddressParseErrorTokens( colName.toStdString() );
Column col = Column::createColumnInfoFromCsvData( addr, unit.toStdString() );

// Create address of a give category if provided
if ( parseOptions.defaultCategory == RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_WELL )
addr = RifEclipseSummaryAddress::wellAddress( colName.toStdString(), nameFromData.toStdString() );
else if ( parseOptions.defaultCategory == RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_FIELD )
addr = RifEclipseSummaryAddress::fieldAddress( colName.toStdString() );

Column col = Column::createColumnInfoFromCsvData( addr, unit.toStdString() );
columnInfoList->push_back( col );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RifRevealCsvSectionSummaryReader::~RifRevealCsvSectionSummaryReader()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifRevealCsvSectionSummaryReader::parse( const QString& text, QString* errorText )
bool RifRevealCsvSectionSummaryReader::parse( const QString& text, RifEclipseSummaryAddress::SummaryVarCategory defaultCategory, QString* errorText )
{
m_allResultAddresses.clear();
m_mapFromAddressToResultIndex.clear();
Expand All @@ -61,6 +61,7 @@ bool RifRevealCsvSectionSummaryReader::parse( const QString& text, QString* erro
parseOptions.cellSeparator = ",";
parseOptions.decimalSeparator = ".";
parseOptions.timeSeriesColumnName = "Date";
parseOptions.defaultCategory = defaultCategory;

m_parser = std::unique_ptr<RifCsvUserDataPastedTextParser>( new RifCsvUserDataPastedTextParser( text, errorText ) );
if ( !m_parser->parse( parseOptions ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RifRevealCsvSectionSummaryReader : public RifSummaryReaderInterface
RifRevealCsvSectionSummaryReader();
~RifRevealCsvSectionSummaryReader() override;

bool parse( const QString& fileName, QString* errorText = nullptr );
bool parse( const QString& fileName, RifEclipseSummaryAddress::SummaryVarCategory defaultCategory, QString* errorText = nullptr );

std::vector<time_t> timeSteps( const RifEclipseSummaryAddress& resultAddress ) const override;

Expand Down
12 changes: 10 additions & 2 deletions ApplicationLibCode/FileInterface/RifRevealCsvSummaryReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,25 @@ bool RifRevealCsvSummaryReader::parse( const QString& fileName, QString* errorTe
QStringList parts = fileContents.split( ",Date", QString::SkipEmptyParts );

// Parse each section separately
bool isFirst = true;
for ( auto p : parts )
{
p.prepend( "Name,Date" );
cvf::ref<RifRevealCsvSectionSummaryReader> sectionReader = new RifRevealCsvSectionSummaryReader;

addReader( sectionReader.p() );
// The first part is field data, and the rest is well data
auto defaultCategory = isFirst ? RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_FIELD
: RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_WELL;

QString errorMessage;
if ( !sectionReader->parse( p, &errorMessage ) )
if ( !sectionReader->parse( p, defaultCategory, &errorMessage ) )
{
return false;
}

addReader( sectionReader.p() );

isFirst = false;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST( RifRevealCsvSectionSummaryReaderTest, ExpectedText )
QString errorMessage;
RifRevealCsvSectionSummaryReader reader;

bool isOk = reader.parse( fileContents, &errorMessage );
bool isOk = reader.parse( fileContents, RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_WELL, &errorMessage );
ASSERT_TRUE( isOk );

EXPECT_TRUE( errorMessage.isEmpty() );
Expand All @@ -46,6 +46,6 @@ TEST( RifRevealCsvSectionSummaryReaderTest, EmptyText )
QString fileContents = "";
QString errorMessage;

bool isOk = reader.parse( fileContents, &errorMessage );
bool isOk = reader.parse( fileContents, RifEclipseSummaryAddress::SummaryVarCategory::SUMMARY_MISC, &errorMessage );
ASSERT_FALSE( isOk );
}

0 comments on commit 48c7074

Please sign in to comment.