mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#9611 Support up to three header lines for CSV import
This commit is contained in:
parent
10960ea2fa
commit
4c0e7b2979
@ -313,19 +313,69 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStr
|
||||
QString line = dataStream->readLine();
|
||||
if ( line.trimmed().isEmpty() ) continue;
|
||||
|
||||
QStringList lineColumns = RifFileParseTools::splitLineAndTrim( line, parseOptions.cellSeparator );
|
||||
QStringList columnHeaders = RifFileParseTools::splitLineAndTrim( line, parseOptions.cellSeparator );
|
||||
|
||||
int colCount = lineColumns.size();
|
||||
// Optional support for unit text (second header line) and names (third header line)
|
||||
QStringList unitTexts;
|
||||
QStringList names;
|
||||
|
||||
auto startOfLineWithDataValues = dataStream->pos();
|
||||
bool hasDataValues = false;
|
||||
while ( !hasDataValues )
|
||||
{
|
||||
QString candidateLine = dataStream->readLine();
|
||||
|
||||
QStringList candidateColumnHeaders =
|
||||
RifFileParseTools::splitLineAndTrim( candidateLine, parseOptions.cellSeparator );
|
||||
for ( const auto& text : candidateColumnHeaders )
|
||||
{
|
||||
if ( RiaStdStringTools::isNumber( text.toStdString(), parseOptions.locale.decimalPoint().toLatin1() ) )
|
||||
hasDataValues = true;
|
||||
}
|
||||
|
||||
if ( !hasDataValues && candidateColumnHeaders.size() == columnHeaders.size() )
|
||||
{
|
||||
if ( unitTexts.empty() )
|
||||
{
|
||||
unitTexts = candidateColumnHeaders;
|
||||
}
|
||||
else if ( names.empty() )
|
||||
{
|
||||
names = candidateColumnHeaders;
|
||||
}
|
||||
|
||||
startOfLineWithDataValues = dataStream->pos();
|
||||
}
|
||||
}
|
||||
|
||||
dataStream->seek( startOfLineWithDataValues );
|
||||
|
||||
int colCount = columnHeaders.size();
|
||||
|
||||
for ( int iCol = 0; iCol < colCount; iCol++ )
|
||||
{
|
||||
QString colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( lineColumns[iCol] );
|
||||
QString colName = RiaTextStringTools::trimAndRemoveDoubleSpaces( columnHeaders[iCol] );
|
||||
|
||||
if ( iCol < names.size() )
|
||||
{
|
||||
QString name = RiaTextStringTools::trimAndRemoveDoubleSpaces( names[iCol] );
|
||||
if ( !name.isEmpty() )
|
||||
{
|
||||
// Create summary address in the form "WBHP:A-1", <vector name>:<well name>
|
||||
colName += ":" + name;
|
||||
}
|
||||
}
|
||||
|
||||
QString unit;
|
||||
if ( iCol < unitTexts.size() ) unit = unitTexts[iCol];
|
||||
|
||||
RifEclipseSummaryAddress addr =
|
||||
RifEclipseSummaryAddress::fromEclipseTextAddressParseErrorTokens( colName.toStdString() );
|
||||
Column col = Column::createColumnInfoFromCsvData( addr, "" );
|
||||
Column col = Column::createColumnInfoFromCsvData( addr, unit.toStdString() );
|
||||
|
||||
columnInfoList->push_back( col );
|
||||
}
|
||||
|
||||
headerFound = true;
|
||||
}
|
||||
|
||||
|
@ -401,6 +401,47 @@ TEST( RifColumnBasedAsciiParserTest, TestCellSeparatorComma )
|
||||
EXPECT_EQ( 4.44, pwValues[3] );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifColumnBasedAsciiParserTest, ThreeLinesHeader )
|
||||
{
|
||||
AsciiDataParseOptions parseOptions;
|
||||
parseOptions.dateFormat = "dd.MM.yyyy";
|
||||
parseOptions.cellSeparator = ";";
|
||||
parseOptions.locale = QLocale::c();
|
||||
parseOptions.timeSeriesColumnName = "TIME";
|
||||
|
||||
// The CSV header require at leas one line with header data.
|
||||
// Units and object names are optional header lines.
|
||||
|
||||
// Example data with three header lines:
|
||||
QString data = R"(
|
||||
TIME;WBHPH;WBHPH;WBHPH
|
||||
DAYS;BARS;BARS;BARS
|
||||
;A-3T2_old;A-3A_old;A-9T2_old
|
||||
15.07.1999;456.78;0;0
|
||||
16.07.1999;0;0;0
|
||||
)";
|
||||
|
||||
QTextStream out( &data );
|
||||
|
||||
RifCsvUserDataPastedTextParser parser = RifCsvUserDataPastedTextParser( data );
|
||||
parser.parse( parseOptions );
|
||||
|
||||
auto timeColumn = parser.columnInfo( 0 );
|
||||
ASSERT_TRUE( timeColumn != nullptr );
|
||||
|
||||
auto column1 = parser.columnInfo( 1 );
|
||||
ASSERT_TRUE( column1 != nullptr );
|
||||
auto adr = column1->summaryAddress;
|
||||
|
||||
ASSERT_STREQ( adr.wellName().data(), "A-3T2_old" );
|
||||
ASSERT_STREQ( adr.vectorName().data(), "WBHPH" );
|
||||
|
||||
ASSERT_EQ( column1->values.front(), 456.78 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user