mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#5989 Add parser for facies properties csv file
This commit is contained in:
committed by
Magne Sjaastad
parent
ee20ec4107
commit
d443f97a1a
@@ -53,6 +53,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifEclipseInputPropertyLoader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifRoffReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifColorLegendData.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifFaciesPropertiesReader.h
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CMAKE_CURRENT_LIST_DIR}/RifHdf5Reader.h
|
||||
@@ -110,6 +111,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifEclipseInputPropertyLoader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifRoffReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifColorLegendData.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifFaciesPropertiesReader.cpp
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CMAKE_CURRENT_LIST_DIR}/RifHdf5Reader.cpp
|
||||
|
||||
171
ApplicationCode/FileInterface/RifFaciesPropertiesReader.cpp
Normal file
171
ApplicationCode/FileInterface/RifFaciesPropertiesReader.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2020- Equinor 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 "RifFaciesPropertiesReader.h"
|
||||
|
||||
#include "RifFileParseTools.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QTextStream>
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
void RifFaciesPropertiesReader::readFaciesProperties( std::vector<RifFaciesProperties>& faciesProperties,
|
||||
const QStringList& filePaths )
|
||||
{
|
||||
for ( const QString& filePath : filePaths )
|
||||
{
|
||||
try
|
||||
{
|
||||
readFaciesProperties( faciesProperties, filePath );
|
||||
}
|
||||
catch ( FileParseException& )
|
||||
{
|
||||
// Delete all facies properties and rethrow exception
|
||||
faciesProperties.clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifFaciesPropertiesReader::readFaciesProperties( std::vector<RifFaciesProperties>& faciesProperties,
|
||||
const QString& filePath )
|
||||
{
|
||||
QFile file( filePath );
|
||||
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
||||
{
|
||||
throw FileParseException( QString( "Unable to open file: %1" ).arg( filePath ) );
|
||||
}
|
||||
|
||||
QTextStream in( &file );
|
||||
int lineNumber = 1;
|
||||
while ( !in.atEnd() )
|
||||
{
|
||||
QString line = in.readLine();
|
||||
if ( !isEmptyLine( line ) && !isCommentLine( line ) )
|
||||
{
|
||||
RifFaciesProperties faciesProp = parseFaciesProperties( line, lineNumber, filePath );
|
||||
faciesProperties.push_back( faciesProp );
|
||||
}
|
||||
|
||||
lineNumber++;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifFaciesProperties
|
||||
RifFaciesPropertiesReader::parseFaciesProperties( const QString& line, int lineNumber, const QString& filePath )
|
||||
{
|
||||
QStringList tokens = tokenize( line, "," );
|
||||
|
||||
if ( tokens.size() != 8 )
|
||||
{
|
||||
throw FileParseException( QString( "Incomplete data on line %1: %2" ).arg( lineNumber ).arg( filePath ) );
|
||||
}
|
||||
|
||||
// Check for unexpected empty tokens
|
||||
QStringList nameOfNonEmptyTokens;
|
||||
nameOfNonEmptyTokens << "Field Name"
|
||||
<< "Formation Name"
|
||||
<< "Facies Name"
|
||||
<< "Porosity"
|
||||
<< "Young's Modulus"
|
||||
<< "Poisson's Ratio"
|
||||
<< "K-Ic"
|
||||
<< "Proppant Embedment";
|
||||
verifyNonEmptyTokens( tokens, nameOfNonEmptyTokens, lineNumber, filePath );
|
||||
|
||||
RifFaciesProperties faciesProperties;
|
||||
faciesProperties.fieldName = tokens[0];
|
||||
faciesProperties.formationName = tokens[1];
|
||||
faciesProperties.faciesName = tokens[2];
|
||||
faciesProperties.porosity = parseDouble( tokens[3], "Porosity", lineNumber, filePath );
|
||||
faciesProperties.youngsModulus = parseDouble( tokens[4], "Young's Modulus", lineNumber, filePath );
|
||||
faciesProperties.poissonsRatio = parseDouble( tokens[5], "Poisson's Ratio", lineNumber, filePath );
|
||||
faciesProperties.K_Ic = parseDouble( tokens[6], "K-Ic", lineNumber, filePath );
|
||||
faciesProperties.proppantEmbedment = parseDouble( tokens[7], "Proppant Embedment", lineNumber, filePath );
|
||||
|
||||
return faciesProperties;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList RifFaciesPropertiesReader::tokenize( const QString& line, const QString& separator )
|
||||
{
|
||||
return RifFileParseTools::splitLineAndTrim( line, separator );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RifFaciesPropertiesReader::parseDouble( const QString& token,
|
||||
const QString& propertyName,
|
||||
int lineNumber,
|
||||
const QString& filePath )
|
||||
{
|
||||
bool isOk = false;
|
||||
double value = token.toDouble( &isOk );
|
||||
if ( !isOk )
|
||||
{
|
||||
throw FileParseException(
|
||||
QString( "Invalid number for '%1' on line %2: %3" ).arg( propertyName ).arg( lineNumber ).arg( filePath ) );
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifFaciesPropertiesReader::isEmptyLine( const QString& line )
|
||||
{
|
||||
return line.trimmed().isEmpty();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifFaciesPropertiesReader::isCommentLine( const QString& line )
|
||||
{
|
||||
return line.trimmed().startsWith( "#" );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifFaciesPropertiesReader::verifyNonEmptyTokens( const QStringList& tokens,
|
||||
const QStringList& nameOfNonEmptyTokens,
|
||||
int lineNumber,
|
||||
const QString& filePath )
|
||||
{
|
||||
for ( int i = 0; i < nameOfNonEmptyTokens.size(); ++i )
|
||||
{
|
||||
if ( tokens[i].isEmpty() )
|
||||
{
|
||||
throw FileParseException(
|
||||
QString( "Unexpected empty '%1' on line %2: %3" ).arg( nameOfNonEmptyTokens[i] ).arg( lineNumber ).arg( filePath ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
58
ApplicationCode/FileInterface/RifFaciesPropertiesReader.h
Normal file
58
ApplicationCode/FileInterface/RifFaciesPropertiesReader.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2020- Equinor 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 <vector>
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct RifFaciesProperties
|
||||
{
|
||||
QString fieldName;
|
||||
QString formationName;
|
||||
QString faciesName;
|
||||
double porosity;
|
||||
double youngsModulus;
|
||||
double poissonsRatio;
|
||||
double K_Ic;
|
||||
double proppantEmbedment;
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RifFaciesPropertiesReader
|
||||
{
|
||||
public:
|
||||
static void readFaciesProperties( std::vector<RifFaciesProperties>& faciesProperties, const QStringList& filePaths );
|
||||
|
||||
private:
|
||||
static void readFaciesProperties( std::vector<RifFaciesProperties>& faciesProperties, const QString& filePath );
|
||||
static RifFaciesProperties parseFaciesProperties( const QString& line, int lineNumber, const QString& filePath );
|
||||
static QStringList tokenize( const QString& line, const QString& separator );
|
||||
static void verifyNonEmptyTokens( const QStringList& tokens,
|
||||
const QStringList& nameOfNonEmptyTokens,
|
||||
int lineNumber,
|
||||
const QString& filePath );
|
||||
|
||||
static double parseDouble( const QString& token, const QString& propertyName, int lineNumber, const QString& filePath );
|
||||
|
||||
static bool isEmptyLine( const QString& line );
|
||||
static bool isCommentLine( const QString& line );
|
||||
};
|
||||
@@ -68,6 +68,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RigHexGradientTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceReader-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifColorLegendData-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifRoffReader-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifFaciesPropertiesReader-Test.cpp
|
||||
)
|
||||
|
||||
if (RESINSIGHT_ENABLE_GRPC)
|
||||
|
||||
216
ApplicationCode/UnitTests/RifFaciesPropertiesReader-Test.cpp
Normal file
216
ApplicationCode/UnitTests/RifFaciesPropertiesReader-Test.cpp
Normal file
@@ -0,0 +1,216 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2019- Equinor 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 "gtest/gtest.h"
|
||||
|
||||
#include "RifFaciesPropertiesReader.h"
|
||||
#include "RifFileParseTools.h"
|
||||
|
||||
#include <QStringList>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, ReadCorrectInputFile )
|
||||
{
|
||||
QTemporaryFile file;
|
||||
EXPECT_TRUE( file.open() );
|
||||
|
||||
{
|
||||
QTextStream out( &file );
|
||||
out << "Norne,Not,Sand,0.00,25,0.25,2000,0.2\n"
|
||||
<< "Norne,Not,Sand,0.10,19,0.27,2099,0.3\n";
|
||||
}
|
||||
|
||||
QStringList filePaths;
|
||||
filePaths.append( file.fileName() );
|
||||
|
||||
std::vector<RifFaciesProperties> faciesProperties;
|
||||
RifFaciesPropertiesReader::readFaciesProperties( faciesProperties, filePaths );
|
||||
|
||||
ASSERT_EQ( 2u, faciesProperties.size() );
|
||||
|
||||
ASSERT_EQ( "Norne", faciesProperties[0].fieldName.toStdString() );
|
||||
ASSERT_EQ( "Norne", faciesProperties[1].fieldName.toStdString() );
|
||||
|
||||
ASSERT_EQ( "Not", faciesProperties[0].formationName.toStdString() );
|
||||
ASSERT_EQ( "Not", faciesProperties[1].formationName.toStdString() );
|
||||
|
||||
ASSERT_EQ( "Sand", faciesProperties[0].faciesName.toStdString() );
|
||||
ASSERT_EQ( "Sand", faciesProperties[1].faciesName.toStdString() );
|
||||
|
||||
ASSERT_DOUBLE_EQ( 0.0, faciesProperties[0].porosity );
|
||||
ASSERT_DOUBLE_EQ( 0.1, faciesProperties[1].porosity );
|
||||
|
||||
ASSERT_DOUBLE_EQ( 25.0, faciesProperties[0].youngsModulus );
|
||||
ASSERT_DOUBLE_EQ( 19.0, faciesProperties[1].youngsModulus );
|
||||
|
||||
ASSERT_DOUBLE_EQ( 0.25, faciesProperties[0].poissonsRatio );
|
||||
ASSERT_DOUBLE_EQ( 0.27, faciesProperties[1].poissonsRatio );
|
||||
|
||||
ASSERT_DOUBLE_EQ( 2000.0, faciesProperties[0].K_Ic );
|
||||
ASSERT_DOUBLE_EQ( 2099.0, faciesProperties[1].K_Ic );
|
||||
|
||||
ASSERT_DOUBLE_EQ( 0.2, faciesProperties[0].proppantEmbedment );
|
||||
ASSERT_DOUBLE_EQ( 0.3, faciesProperties[1].proppantEmbedment );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Helper to check exception messages when reading invalid files
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
::testing::AssertionResult readingFaciesPropertiesThrowsException( const QStringList& filePaths,
|
||||
const QString& expectedMessage )
|
||||
{
|
||||
std::vector<RifFaciesProperties> faciesProperties;
|
||||
try
|
||||
{
|
||||
RifFaciesPropertiesReader::readFaciesProperties( faciesProperties, filePaths );
|
||||
// No exception thrown: fail!
|
||||
return ::testing::AssertionFailure() << "readFaciesProperties did not throw exception";
|
||||
}
|
||||
catch ( FileParseException& error )
|
||||
{
|
||||
// Should always have cleaned up on failure
|
||||
EXPECT_EQ( 0u, faciesProperties.size() );
|
||||
// Check that we get the expected message
|
||||
EXPECT_EQ( expectedMessage.toStdString(), error.message.toStdString() );
|
||||
return ::testing::AssertionSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, ReadMissingFileThrows )
|
||||
{
|
||||
QStringList filePaths;
|
||||
QString nonExistingFile( "this/is/a/file/which/does/not/exist.csv" );
|
||||
filePaths.append( nonExistingFile );
|
||||
ASSERT_TRUE( readingFaciesPropertiesThrowsException( filePaths,
|
||||
QString( "Unable to open file: %1" ).arg( nonExistingFile ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, ReadShortLinesFileThrows )
|
||||
{
|
||||
QTemporaryFile file;
|
||||
EXPECT_TRUE( file.open() );
|
||||
|
||||
{
|
||||
QTextStream out( &file );
|
||||
out << "Norne,Not,Sand,0.00,25,0.25,2000,0.2\n"
|
||||
<< "Norne,Not,Sand,0.10,19,0.27\n";
|
||||
}
|
||||
|
||||
QStringList filePaths;
|
||||
filePaths.append( file.fileName() );
|
||||
ASSERT_TRUE(
|
||||
readingFaciesPropertiesThrowsException( filePaths,
|
||||
QString( "Incomplete data on line 2: %1" ).arg( file.fileName() ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, ReadEmptyFieldNameThrows )
|
||||
{
|
||||
QTemporaryFile file;
|
||||
EXPECT_TRUE( file.open() );
|
||||
|
||||
{
|
||||
QTextStream out( &file );
|
||||
out << "Norne,Not,Sand,0.00,25,0.25,2000,0.2\n"
|
||||
<< ",Not,Sand,0.10,19,0.27,2099,0.3\n";
|
||||
}
|
||||
|
||||
QStringList filePaths;
|
||||
filePaths.append( file.fileName() );
|
||||
ASSERT_TRUE( readingFaciesPropertiesThrowsException( filePaths,
|
||||
QString( "Unexpected empty 'Field Name' on line 2: %1" )
|
||||
.arg( file.fileName() ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, ReadInvalidMeasureDepthThrows )
|
||||
{
|
||||
QTemporaryFile file;
|
||||
EXPECT_TRUE( file.open() );
|
||||
|
||||
{
|
||||
QTextStream out( &file );
|
||||
out << "Norne,Not,Sand,0.00,25,0.25,2000,0.2\n"
|
||||
<< "Norne,Not,Sand, not a number,23.4,0.27,2099,0.3\n";
|
||||
}
|
||||
|
||||
QStringList filePaths;
|
||||
filePaths.append( file.fileName() );
|
||||
ASSERT_TRUE( readingFaciesPropertiesThrowsException( filePaths,
|
||||
QString( "Invalid number for 'Porosity' on line 2: %1" )
|
||||
.arg( file.fileName() ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RifFaciesPropertiesReaderTest, CommentsAndEmptyLinesAreIgnored )
|
||||
{
|
||||
QTemporaryFile file;
|
||||
EXPECT_TRUE( file.open() );
|
||||
|
||||
{
|
||||
QTextStream out( &file );
|
||||
// Comment should be ignored
|
||||
out << "# This is a comment.\n";
|
||||
out << "#This is also a comment.\n";
|
||||
out << " # This is also a comment which does not start on first character.\n";
|
||||
// Should skip empty lines
|
||||
out << "\n";
|
||||
out << "\t\n";
|
||||
out << " \n";
|
||||
// Then some data
|
||||
out << "Norne,Not,Sand,0.00,25,0.25,2000,0.2\n";
|
||||
// Comment in-between data should be ignored
|
||||
out << "# One more comment in-between the data\n";
|
||||
out << "Norne,Not,Silt,0.00,25,0.25,2000,0.2\n";
|
||||
// Empty line in-between data should be ignored
|
||||
out << "\n";
|
||||
// Data with comment sign inside it is not ignored
|
||||
out << "Norne,Not,Shale,0.00,25,0.25,2000,0.2\n";
|
||||
// Trailing empty lines should be ignored
|
||||
out << "\n\n\n";
|
||||
}
|
||||
|
||||
QStringList filePaths;
|
||||
filePaths.append( file.fileName() );
|
||||
std::vector<RifFaciesProperties> faciesProperties;
|
||||
RifFaciesPropertiesReader::readFaciesProperties( faciesProperties, filePaths );
|
||||
|
||||
ASSERT_EQ( 3u, faciesProperties.size() );
|
||||
|
||||
ASSERT_EQ( "Sand", faciesProperties[0].faciesName.toStdString() );
|
||||
ASSERT_EQ( "Silt", faciesProperties[1].faciesName.toStdString() );
|
||||
ASSERT_EQ( "Shale", faciesProperties[2].faciesName.toStdString() );
|
||||
}
|
||||
Reference in New Issue
Block a user