From 55bc3703eabe2a5bd94abeadcda98090892938d1 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Tue, 19 Mar 2024 07:38:55 +0100 Subject: [PATCH] Avoid hardcoded requirements for headers of RFT pressure files Parsing "PSIA FEET" did not work, as "BARSA METRES" was the only supported unit. Keep special handling of header and date, and skip all other lines containing text or comment. --- .../RifPressureDepthTextFileReader.cpp | 36 +++++++++---------- .../RifPressureDepthTextFileReader.h | 5 ++- .../RifPressureDepthTextFileReader-Test.cpp | 30 ++++++++++++++++ 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.cpp b/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.cpp index cfeb6c8b70..f1d068205f 100644 --- a/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.cpp +++ b/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.cpp @@ -19,15 +19,13 @@ #include "RifPressureDepthTextFileReader.h" #include "RiaDateStringParser.h" -#include "RiaDefines.h" #include "RigPressureDepthData.h" #include "RifFileParseTools.h" -#include "cafAssert.h" - #include +#include #include //-------------------------------------------------------------------------------------------------- @@ -43,9 +41,20 @@ std::pair, QString> RifPressureDepthTextFileRe return std::make_pair( items, QString( "Unable to open file: %1" ).arg( filePath ) ); } + return parse( file.readAll() ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::pair, QString> RifPressureDepthTextFileReader::parse( const QString& content ) +{ + std::vector items; + QString separator = " "; - QTextStream in( &file ); + QString streamContent( content ); + QTextStream in( &streamContent ); while ( !in.atEnd() ) { QString line = in.readLine(); @@ -64,7 +73,7 @@ std::pair, QString> RifPressureDepthTextFileRe items.back().setTimeStep( date.value() ); } } - else if ( isPropertiesLine( line ) || isUnitsLine( line ) || isCommentLine( line ) ) + else if ( containsLetters( line ) || isCommentLine( line ) ) { // Ignored. } @@ -105,19 +114,10 @@ bool RifPressureDepthTextFileReader::isDateLine( const QString& line ) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RifPressureDepthTextFileReader::isPropertiesLine( const QString& line ) -{ - // TODO: this might be to strict.. - return line.startsWith( "PRESSURE DEPTH" ); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RifPressureDepthTextFileReader::isUnitsLine( const QString& line ) +bool RifPressureDepthTextFileReader::containsLetters( const QString& line ) { - // TODO: this might be to strict.. - return line.startsWith( "BARSA METRES" ); + QRegularExpression regex( "[a-zA-Z]" ); + return regex.match( line ).hasMatch(); } //-------------------------------------------------------------------------------------------------- @@ -152,8 +152,6 @@ std::optional RifPressureDepthTextFileReader::parseDateLine( const QS QStringList values = RifFileParseTools::splitLineAndTrim( line, " ", skipEmptyParts ); if ( values.size() != 2 ) return {}; - CAF_ASSERT( values[0] == "DATE" ); - // Second value is depth QDateTime dateTime = RiaDateStringParser::parseDateString( values[1], RiaDateStringParser::OrderPreference::DAY_FIRST ); if ( !dateTime.isValid() ) return {}; diff --git a/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.h b/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.h index 030cec4b4c..69f58bd996 100644 --- a/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.h +++ b/ApplicationLibCode/FileInterface/RifPressureDepthTextFileReader.h @@ -21,7 +21,6 @@ #include #include -#include #include #include @@ -34,13 +33,13 @@ class RifPressureDepthTextFileReader { public: static std::pair, QString> readFile( const QString& fileName ); + static std::pair, QString> parse( const QString& content ); private: static bool isHeaderLine( const QString& line ); static bool isCommentLine( const QString& line ); static bool isDateLine( const QString& line ); - static bool isPropertiesLine( const QString& line ); - static bool isUnitsLine( const QString& line ); + static bool containsLetters( const QString& line ); static std::optional> parseDataLine( const QString& line ); static std::optional parseDateLine( const QString& line ); diff --git a/ApplicationLibCode/UnitTests/RifPressureDepthTextFileReader-Test.cpp b/ApplicationLibCode/UnitTests/RifPressureDepthTextFileReader-Test.cpp index b202978972..f06b2acd84 100644 --- a/ApplicationLibCode/UnitTests/RifPressureDepthTextFileReader-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifPressureDepthTextFileReader-Test.cpp @@ -62,3 +62,33 @@ TEST( RifPressureDepthTextFileReaderTest, LoadFileNonExistingFiles ) EXPECT_FALSE( errorMessage.isEmpty() ); EXPECT_EQ( 0u, items.size() ); } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST( RifPressureDepthTextFileReaderTest, FieldUnitData ) +{ + auto content = R"( +--TVDMSL +RFT +-- +WELLNAME 'A1' +DATE 18-NOV-2018 +PRESSURE DEPTH +PSIA FEET +12008.00 22640.66 +12020.40 22674.44 +\n)"; + + auto [items, errorMessage] = RifPressureDepthTextFileReader::parse( content ); + + EXPECT_TRUE( errorMessage.isEmpty() ); + ASSERT_EQ( 1u, items.size() ); + + EXPECT_EQ( "A1", items[0].wellName().toStdString() ); + std::vector> values0 = items[0].getPressureDepthValues(); + EXPECT_EQ( 2u, values0.size() ); + double delta = 0.001; + EXPECT_NEAR( 12008.0, values0[0].first, delta ); + EXPECT_NEAR( 22640.66, values0[0].second, delta ); +}