#5001 Add Well Measurement reader.

This commit is contained in:
Kristian Bendiksen 2019-11-11 23:07:53 +01:00
parent ff15ff939d
commit fd862b2eda
5 changed files with 530 additions and 0 deletions

View File

@ -35,6 +35,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifDataSourceForRftPltQMetaType.h
${CMAKE_CURRENT_LIST_DIR}/RifEclipseUserDataKeywordTools.h
${CMAKE_CURRENT_LIST_DIR}/RifCsvUserData.h
${CMAKE_CURRENT_LIST_DIR}/RifCsvUserDataParser.h
${CMAKE_CURRENT_LIST_DIR}/RifWellMeasurementReader.h
${CMAKE_CURRENT_LIST_DIR}/RifWellPathFormationReader.h
${CMAKE_CURRENT_LIST_DIR}/RifWellPathFormationsImporter.h
${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyTableReader.h
@ -88,6 +89,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifDataSourceForRftPlt.cpp
${CMAKE_CURRENT_LIST_DIR}/RifEclipseUserDataKeywordTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvUserData.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvUserDataParser.cpp
${CMAKE_CURRENT_LIST_DIR}/RifWellMeasurementReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifWellPathFormationReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifWellPathFormationsImporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyTableReader.cpp

View File

@ -0,0 +1,209 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RifWellMeasurementReader.h"
#include "RifFileParseTools.h"
#include <QDate>
#include <QFileInfo>
#include <QTextStream>
//==================================================================================================
///
//==================================================================================================
void RifWellMeasurementReader::readWellMeasurements( std::vector<RifWellMeasurement>& wellMeasurements,
const QStringList& filePaths )
{
for ( const QString& filePath : filePaths )
{
try
{
readWellMeasurements( wellMeasurements, filePath );
}
catch ( FileParseException& )
{
// Delete all well measurements and rethrow exception
wellMeasurements.clear();
throw;
}
}
}
//==================================================================================================
///
//==================================================================================================
void RifWellMeasurementReader::readWellMeasurements( std::vector<RifWellMeasurement>& wellMeasurements,
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 ) )
{
RifWellMeasurement wellMeasurement = parseWellMeasurement( line, lineNumber, filePath );
wellMeasurements.push_back( wellMeasurement );
}
lineNumber++;
}
}
//==================================================================================================
///
//==================================================================================================
RifWellMeasurement
RifWellMeasurementReader::parseWellMeasurement( const QString& line, int lineNumber, const QString& filePath )
{
QStringList tokens = tokenize( line, "," );
if ( tokens.size() != 7 )
{
throw FileParseException( QString( "Incomplete data on line %1: %2" ).arg( lineNumber ).arg( filePath ) );
}
// Check for unexpected empty tokens
QStringList nameOfNonEmptyTokens;
nameOfNonEmptyTokens << "Well Name"
<< "Measured Depth"
<< "Date"
<< "Value"
<< "Kind";
verifyNonEmptyTokens( tokens, nameOfNonEmptyTokens, lineNumber, filePath );
RifWellMeasurement wellMeasurement;
wellMeasurement.wellName = tokens[0];
wellMeasurement.MD = parseDouble( tokens[1], "Measured Depth", lineNumber, filePath );
wellMeasurement.date = parseDate( tokens[2], "Date", lineNumber, filePath );
wellMeasurement.value = parseDouble( tokens[3], "Value", lineNumber, filePath );
wellMeasurement.kind = tokens[4];
wellMeasurement.quality = parseInt( tokens[5], "Quality", lineNumber, filePath );
wellMeasurement.remark = tokens[6];
wellMeasurement.filePath = filePath;
return wellMeasurement;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RifWellMeasurementReader::tokenize( const QString& line, const QString& separator )
{
return RifFileParseTools::splitLineAndTrim( line, separator );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QDate RifWellMeasurementReader::parseDate( const QString& token,
const QString& propertyName,
int lineNumber,
const QString& filePath )
{
QDate date = QDate::fromString( token, Qt::ISODate );
if ( !date.isValid() )
{
throw FileParseException( QString( "Invalid date format (must be ISO 8601) for '%1' on line %2: %3" )
.arg( propertyName )
.arg( lineNumber )
.arg( filePath ) );
}
return date;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RifWellMeasurementReader::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;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RifWellMeasurementReader::parseInt( const QString& token,
const QString& propertyName,
int lineNumber,
const QString& filePath )
{
bool isOk = false;
int value = token.toInt( &isOk );
if ( !isOk )
{
throw FileParseException(
QString( "Invalid number for '%1' on line %2: %3" ).arg( propertyName ).arg( lineNumber ).arg( filePath ) );
}
return value;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifWellMeasurementReader::isEmptyLine( const QString& line )
{
return line.trimmed().isEmpty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifWellMeasurementReader::isCommentLine( const QString& line )
{
return line.startsWith( "#" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifWellMeasurementReader::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 ) );
}
}
}

View File

@ -0,0 +1,61 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 <vector>
#include <QDate>
#include <QString>
struct RifWellMeasurement
{
QString wellName;
double MD;
double value;
QDate date;
int quality;
QString kind;
QString remark;
QString filePath;
};
//==================================================================================================
///
//==================================================================================================
class RifWellMeasurementReader
{
public:
static void readWellMeasurements( std::vector<RifWellMeasurement>& wellMeasurements, const QStringList& filePaths );
private:
static void readWellMeasurements( std::vector<RifWellMeasurement>& wellMeasurements, const QString& filePath );
static RifWellMeasurement parseWellMeasurement( 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 QDate parseDate( const QString& token, const QString& propertyName, int lineNumber, const QString& filePath );
static double parseDouble( const QString& token, const QString& propertyName, int lineNumber, const QString& filePath );
static int parseInt( const QString& token, const QString& propertyName, int lineNumber, const QString& filePath );
static bool isEmptyLine( const QString& line );
static bool isCommentLine( const QString& line );
};

View File

@ -61,6 +61,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifActiveCellsReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvDataTableFormatter-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSummaryCurveAnalyzer-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaStdStringTools-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifWellMeasurementReader-Test.cpp
)
if (RESINSIGHT_ENABLE_GRPC)

View File

@ -0,0 +1,257 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RifFileParseTools.h"
#include "RifWellMeasurementReader.h"
#include <QStringList>
#include <QTemporaryFile>
#include <QTextStream>
#include <vector>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadCorrectInputFile )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "NO 1234/5-6, 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n"
<< "NO 4321/7-8, 4321.79, 2024-12-24, 88.8, DP, 1, Poor test\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
std::vector<RifWellMeasurement> wellMeasurements;
RifWellMeasurementReader::readWellMeasurements( wellMeasurements, filePaths );
ASSERT_EQ( 2u, wellMeasurements.size() );
ASSERT_EQ( "NO 1234/5-6", wellMeasurements[0].wellName.toStdString() );
ASSERT_EQ( "NO 4321/7-8", wellMeasurements[1].wellName.toStdString() );
ASSERT_DOUBLE_EQ( 1234.56, wellMeasurements[0].MD );
ASSERT_DOUBLE_EQ( 4321.79, wellMeasurements[1].MD );
QDate date0( 2019, 11, 13 );
ASSERT_EQ( date0.year(), wellMeasurements[0].date.year() );
ASSERT_EQ( date0.month(), wellMeasurements[0].date.month() );
ASSERT_EQ( date0.day(), wellMeasurements[0].date.day() );
QDate date1( 2024, 12, 24 );
ASSERT_EQ( date1.year(), wellMeasurements[1].date.year() );
ASSERT_EQ( date1.month(), wellMeasurements[1].date.month() );
ASSERT_EQ( date1.day(), wellMeasurements[1].date.day() );
ASSERT_DOUBLE_EQ( 99.9, wellMeasurements[0].value );
ASSERT_DOUBLE_EQ( 88.8, wellMeasurements[1].value );
ASSERT_EQ( "XLOT", wellMeasurements[0].kind.toStdString() );
ASSERT_EQ( "DP", wellMeasurements[1].kind.toStdString() );
ASSERT_EQ( 3, wellMeasurements[0].quality );
ASSERT_EQ( 1, wellMeasurements[1].quality );
ASSERT_EQ( "Good test", wellMeasurements[0].remark.toStdString() );
ASSERT_EQ( "Poor test", wellMeasurements[1].remark.toStdString() );
}
//--------------------------------------------------------------------------------------------------
/// Helper to check exception messages when reading invalid files
//--------------------------------------------------------------------------------------------------
::testing::AssertionResult readingThrowsException( const QStringList& filePaths, const QString& expectedMessage )
{
std::vector<RifWellMeasurement> wellMeasurements;
try
{
RifWellMeasurementReader::readWellMeasurements( wellMeasurements, filePaths );
// No exception thrown: fail!
return ::testing::AssertionFailure() << "readWellMeasurements did not throw exception";
}
catch ( FileParseException& error )
{
// Should always have cleaned up the well measurements on failure
EXPECT_EQ( 0u, wellMeasurements.size() );
// Check that we get the expected message
EXPECT_EQ( expectedMessage.toStdString(), error.message.toStdString() );
return ::testing::AssertionSuccess();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadMissingFileThrows )
{
QStringList filePaths;
QString nonExistingFile( "this/is/a/file/which/does/not/exist.csv" );
filePaths.append( nonExistingFile );
ASSERT_TRUE( readingThrowsException( filePaths, QString( "Unable to open file: %1" ).arg( nonExistingFile ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadShortLinesFileThrows )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "NO 1234/5-6, 1234.56\n"
<< "NO 4321/7-8, 4321.79, 2024-12-24\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
ASSERT_TRUE( readingThrowsException( filePaths, QString( "Incomplete data on line 1: %1" ).arg( file.fileName() ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadEmptyWellNameThrows )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "NO 1234/5-6, 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n";
out << ", 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
ASSERT_TRUE(
readingThrowsException( filePaths,
QString( "Unexpected empty 'Well Name' on line 2: %1" ).arg( file.fileName() ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadInvalidMeasureDepthThrows )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "well 1, 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n";
out << "well 2, this is should be a number, 2019-11-13, 99.9, XLOT, 3, Good test\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
ASSERT_TRUE(
readingThrowsException( filePaths,
QString( "Invalid number for 'Measured Depth' on line 2: %1" ).arg( file.fileName() ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadInvalidQualityThrows )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "well 1, 1234.56, 2019-11-13, 99.9, XLOT, this is not an int, Good test\n";
out << "well 2, 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
ASSERT_TRUE(
readingThrowsException( filePaths,
QString( "Invalid number for 'Quality' on line 1: %1" ).arg( file.fileName() ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, ReadInvalidDateFormateThrows )
{
QTemporaryFile file;
EXPECT_TRUE( file.open() );
{
QTextStream out( &file );
out << "well 1, 1234.56, 17th May 1814, 99.9, XLOT, 1, Good test\n";
out << "well 2, 1234.56, 2019-11-13, 99.9, XLOT, 3, Good test\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
ASSERT_TRUE( readingThrowsException( filePaths,
QString( "Invalid date format (must be ISO 8601) for 'Date' on line 1: %1" )
.arg( file.fileName() ) ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifWellMeasurementReaderTest, 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";
// Should skip empty lines
out << "\n";
out << "\t\n";
out << " \n";
// Then some data
out << "well 1, 1234.56, 2019-11-11, 199.9, XLOT, 1, Good test\n";
// Comment in-between data should be ignored
out << "# One more comment in-between the data\n";
out << "well 2, 2234.56, 2019-11-12, 299.9, XLOT, 2, Good test\n";
// Empty line in-between data should be ignored
out << "\n";
// Data with comment sign inside it is not ignored
out << "well #3, 3234.56, 2019-11-13, 399.9, XLOT, 3, Good test\n";
// Trailing empty lines should be ignored
out << "\n\n\n";
}
QStringList filePaths;
filePaths.append( file.fileName() );
std::vector<RifWellMeasurement> wellMeasurements;
RifWellMeasurementReader::readWellMeasurements( wellMeasurements, filePaths );
ASSERT_EQ( 3u, wellMeasurements.size() );
ASSERT_EQ( "well 1", wellMeasurements[0].wellName.toStdString() );
ASSERT_EQ( "well 2", wellMeasurements[1].wellName.toStdString() );
ASSERT_EQ( "well #3", wellMeasurements[2].wellName.toStdString() );
}