Files
ResInsight/ApplicationLibCode/FileInterface/RifWellPathFormationReader.cpp
T

135 lines
4.6 KiB
C++
Raw Normal View History

2017-11-22 11:59:50 +01:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017- Statoil ASA
//
2017-11-22 11:59:50 +01:00
// 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.
//
2017-11-22 11:59:50 +01:00
// 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>
2017-11-22 11:59:50 +01:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RifWellPathFormationReader.h"
2017-11-22 11:59:50 +01:00
#include <QFile>
#include <QStringList>
#include <QTextStream>
2017-11-22 11:59:50 +01:00
#include <vector>
namespace
{
2017-11-22 11:59:50 +01:00
//--------------------------------------------------------------------------------------------------
///
2017-11-22 11:59:50 +01:00
//--------------------------------------------------------------------------------------------------
QStringList parseHeader( const QString& line )
2017-11-22 11:59:50 +01:00
{
QString header = line.toLower();
header.removeIf( []( QChar c ) { return c.isSpace(); } );
return header.split( ';' );
2017-11-22 11:59:50 +01:00
}
} // namespace
2017-11-22 11:59:50 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<RifWellPathFormationReader::WellFormations, QString> RifWellPathFormationReader::readWellFormations( const QString& filePath )
{
QFile file( filePath );
if ( !file.open( QFile::ReadOnly | QFile::Text ) )
{
return std::unexpected( QString( "Failed to open %1" ).arg( filePath ) );
}
return parseWellFormations( QString::fromUtf8( file.readAll() ), filePath );
}
2017-11-22 11:59:50 +01:00
//--------------------------------------------------------------------------------------------------
///
2017-11-22 11:59:50 +01:00
//--------------------------------------------------------------------------------------------------
std::expected<RifWellPathFormationReader::WellFormations, QString> RifWellPathFormationReader::parseWellFormations( const QString& content,
const QString& filePath )
2017-11-22 11:59:50 +01:00
{
const auto parseFailure = QString( "Failed to parse %1 as a well pick file" ).arg( filePath );
2017-11-22 11:59:50 +01:00
QString text = content;
QTextStream stream( &text );
2017-11-22 11:59:50 +01:00
QStringList header;
while ( header.size() < 3 )
2017-11-22 11:59:50 +01:00
{
if ( stream.atEnd() ) return std::unexpected( parseFailure );
2017-12-07 08:28:20 +01:00
header = parseHeader( stream.readLine() );
2017-11-22 11:59:50 +01:00
}
const int wellNameIndex = header.indexOf( "wellname" );
const int unitNameIndex = header.indexOf( "unitname" );
const int mdTopIndex = header.indexOf( "topmd" );
const int mdBaseIndex = header.indexOf( "basemd" );
const int tvdTopIndex = header.indexOf( "toptvdss" );
const int tvdBaseIndex = header.indexOf( "basetvdss" );
2017-11-22 11:59:50 +01:00
const bool hasMd = mdTopIndex != -1 && mdBaseIndex != -1;
const bool hasTvd = tvdTopIndex != -1 && tvdBaseIndex != -1;
2017-11-22 11:59:50 +01:00
if ( wellNameIndex == -1 || unitNameIndex == -1 )
2017-11-22 11:59:50 +01:00
{
return std::unexpected( parseFailure );
2017-11-22 11:59:50 +01:00
}
if ( !hasMd && !hasTvd )
{
return std::unexpected( parseFailure + ". Neither MD or TVD is present." );
}
std::map<QString, std::vector<RigWellPathFormation>> formationsPerWell;
while ( !stream.atEnd() )
2017-12-07 08:28:20 +01:00
{
const QStringList columns = stream.readLine().split( ';' );
if ( columns.size() != header.size() ) continue;
const QString wellName = columns[wellNameIndex];
const QString unitName = columns[unitNameIndex].trimmed();
if ( wellName.trimmed().isEmpty() && unitName.isEmpty() ) continue;
2020-12-10 10:44:23 +01:00
RigWellPathFormation formation;
formation.formationName = unitName;
if ( hasMd )
{
formation.mdTop = columns[mdTopIndex].toDouble();
formation.mdBase = columns[mdBaseIndex].toDouble();
}
if ( hasTvd )
{
formation.tvdTop = -columns[tvdTopIndex].toDouble();
formation.tvdBase = -columns[tvdBaseIndex].toDouble();
}
formationsPerWell[wellName].push_back( formation );
}
2017-11-22 11:59:50 +01:00
if ( formationsPerWell.empty() )
{
return std::unexpected( parseFailure );
}
2017-11-22 11:59:50 +01:00
WellFormations result;
for ( const auto& [wellName, formations] : formationsPerWell )
{
result.emplace( wellName, RigWellPathFormations( formations, filePath, wellName ) );
2017-12-07 08:28:20 +01:00
}
return result;
2017-11-22 11:59:50 +01:00
}