#13889: Import zone colors from stratigraphy.json and zones from yaml

Import zone colors from an FMU stratigraphy.json file located next to the
formation files, and import formation zones from a yaml file.

- Add RifFormationNamesFileParser and move the layer zone table, yaml and
  stratigraphy.json parsers there from RifFormationNamesReader.
- Return std::expected from convertStringToColor.
- Prefer zonation_ecl_map yaml when several yaml files are present, and ignore
  other yaml files.
- Document the FMU location of the formation files.
- Add unit tests and test data for fmu, malformed and missing-color inputs.

Issues: #13889, #13888
This commit is contained in:
Magne Sjaastad committed 2026-10-20 13:36:02 +02:00
1 parent c04edc9d39
commit fd0ec10d6b
18 files changed
+742 -235

No files matched your search

@@ -92,8 +92,9 @@ void RicImportFormationNamesFeature::onActionTriggered( bool isChecked )
RiaApplication* app = RiaApplication::instance();
QString defaultDir = app->lastUsedDialogDirectory( "BINARY_GRID" );
QString filterText = QString( "Formation Names description File (*.lyr);;FMU Layer Zone Table(%1);;All Files (*.*)" )
.arg( RimFormationNames::layerZoneTableFileName() );
QString filterText =
QString( "Formation Names description File (*.lyr);;FMU Zone Ranges (*.yml *.yaml);;FMU Layer Zone Table(%1);;All Files (*.*)" )
.arg( RimFormationNames::layerZoneTableFileName() );
QStringList fileNames =
RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(), "Import Formation Names", defaultDir, filterText );
@@ -51,6 +51,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RifRoffReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifRoffWriter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFormationNamesReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFormationNamesFileParser.cpp
${CMAKE_CURRENT_LIST_DIR}/RifElasticPropertiesReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelGeologicalFrkExporter.cpp
@@ -0,0 +1,406 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 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 "RifFormationNamesFileParser.h"
#include "RiaLogging.h"
#include "RiaTextStringTools.h"
#include "RigFormationNames.h"
#include <QColor>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegularExpression>
#include <QTextStream>
#include "cvfColor3.h"
namespace
{
//--------------------------------------------------------------------------------------------------
/// Entries with subzones are parents, all other entries are leaves. Within each group the first
/// occurrence of a name wins. Invalid entries are skipped with a warning.
//--------------------------------------------------------------------------------------------------
void collectStratigraphyColors( const QJsonArray& entries,
const QString& fileName,
std::map<QString, cvf::Color3f>& leafColors,
std::map<QString, cvf::Color3f>& parentColors )
{
for ( const QJsonValue& value : entries )
{
if ( !value.isObject() ) continue;
const QJsonObject obj = value.toObject();
const QString name = obj.value( "name" ).toString().trimmed();
if ( name.isEmpty() )
{
RiaLogging::warning( fileName.toStdString() + ": skipping stratigraphy entry without name" );
continue;
}
const QJsonArray subzones = obj.value( "subzones" ).toArray();
const bool isParent = !subzones.isEmpty();
auto& targetMap = isParent ? parentColors : leafColors;
const QString colorText = obj.value( "color" ).toString().trimmed();
const QColor color( colorText );
if ( !color.isValid() )
{
RiaLogging::warning( fileName.toStdString() + ": invalid color '" + colorText.toStdString() + "' for '" + name.toStdString() + "'" );
}
else
{
targetMap.emplace( name, cvf::Color3f::fromByteColor( color.red(), color.green(), color.blue() ) );
}
if ( isParent ) collectStratigraphyColors( subzones, fileName, leafColors, parentColors );
}
}
} // namespace
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<std::map<QString, cvf::Color3f>, QString> RifFormationNamesFileParser::readStratigraphyColors( const QString& fileName )
{
QFile file( fileName );
if ( !file.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
QJsonParseError parseError;
const QJsonDocument doc = QJsonDocument::fromJson( file.readAll(), &parseError );
if ( doc.isNull() )
{
return std::unexpected( QString( "Invalid JSON in '%1': %2" ).arg( fileName, parseError.errorString() ) );
}
if ( !doc.isArray() )
{
return std::unexpected( QString( "Expected a JSON array at the root of '%1'" ).arg( fileName ) );
}
std::map<QString, cvf::Color3f> leafColors;
std::map<QString, cvf::Color3f> parentColors;
collectStratigraphyColors( doc.array(), fileName, leafColors, parentColors );
// Zone colors are used only as fallback when no subzone has the same name
leafColors.insert( parentColors.begin(), parentColors.end() );
return leafColors;
}
//--------------------------------------------------------------------------------------------------
/// Reads the "zranges" section of a YAML file with one-based layer ranges per zone:
///
/// zranges:
/// - Valysar: [1, 20]
/// - Therys: [21, 54]
//--------------------------------------------------------------------------------------------------
std::expected<RigFormationNames, QString>
RifFormationNamesFileParser::readYamlFormationNameFile( const QString& fileName, const std::map<QString, cvf::Color3f>& colors )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
static const QRegularExpression rangeExpression( R"re(^(?:-\s*)?(?:"([^"]+)"|'([^']+)'|([^:]+?))\s*:\s*\[\s*(\d+)\s*,\s*(\d+)\s*\]$)re" );
RigFormationNames formationNames;
QTextStream stream( &dataFile );
bool isInZranges = false;
bool foundZranges = false;
int lineNumber = 0;
while ( !stream.atEnd() )
{
QString line = stream.readLine();
++lineNumber;
const int commentPos = line.indexOf( '#' );
if ( commentPos >= 0 ) line.truncate( commentPos );
const bool isIndented = line.startsWith( ' ' ) || line.startsWith( '\t' );
line = line.trimmed();
if ( line.isEmpty() ) continue;
if ( !isIndented && !line.startsWith( '-' ) )
{
// Top level key
isInZranges = line.startsWith( "zranges:" );
if ( isInZranges ) foundZranges = true;
continue;
}
if ( !isInZranges ) continue;
const QRegularExpressionMatch match = rangeExpression.match( line );
if ( !match.hasMatch() )
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
QString name;
for ( int i = 1; i <= 3 && name.isEmpty(); i++ )
name = match.captured( i ).trimmed();
const int startK = match.captured( 4 ).toInt();
const int endK = match.captured( 5 ).toInt();
if ( name.isEmpty() || startK < 1 || endK < startK )
{
RiaLogging::warning( ( fileName + ": invalid zone range on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
if ( auto it = colors.find( name ); it != colors.end() )
formationNames.appendFormationRange( name, it->second, startK - 1, endK - 1 );
else
formationNames.appendFormationRange( name, startK - 1, endK - 1 );
}
if ( !foundZranges )
{
return std::unexpected( QString( "No 'zranges' section found in '%1'" ).arg( fileName ) );
}
if ( formationNames.formationNames().empty() )
{
return std::unexpected( QString( "No valid zone ranges found in '%1'" ).arg( fileName ) );
}
return formationNames;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<RigFormationNames, QString> RifFormationNamesFileParser::readLyrFormationNameFile( const QString& fileName )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
RigFormationNames formationNames;
QTextStream stream( &dataFile );
int lineNumber = 1;
while ( !stream.atEnd() )
{
QString line = stream.readLine();
QStringList lineSegs = line.split( "'" );
if ( lineSegs.empty() ) continue; // Empty line
if ( lineSegs.size() == 1 ) continue; // No name present. Comment line ?
if ( lineSegs.size() == 2 )
{
RiaLogging::warning( ( fileName + ": missing quote on line " + QString::number( lineNumber ) ).toStdString() );
continue; // One quote present
}
if ( lineSegs.size() == 3 ) // Normal case
{
if ( lineSegs[0].contains( "--" ) ) continue; // Comment line
QString formationName = lineSegs[1];
int commentMarkPos = lineSegs[2].indexOf( "--" );
QString numberString = lineSegs[2];
if ( commentMarkPos >= 0 ) numberString.truncate( commentMarkPos );
// extract last word which may contain formation color
QString colorWord = RiaTextStringTools::splitSkipEmptyParts( numberString ).last();
if ( QColor::isValidColorName( colorWord ) ) numberString.remove( colorWord ); // remove color if present as last word on line
// extract words containing formation number(s)
QStringList numberWords = RiaTextStringTools::splitSkipEmptyParts( numberString, QRegularExpression( "-" ) );
if ( numberWords.size() == 2 ) // formation range with or without color at end of line
{
bool isNumber1 = false;
bool isNumber2 = false;
int startK = numberWords[0].toInt( &isNumber1 );
int endK = numberWords[1].toInt( &isNumber2 );
if ( !( isNumber2 && isNumber1 ) )
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
int tmp = startK;
startK = tmp < endK ? tmp : endK;
endK = tmp > endK ? tmp : endK;
if ( QColor::isValidColorName( colorWord ) ) // formation color present at end of line
{
if ( auto formationColor = convertStringToColor( colorWord ) )
formationNames.appendFormationRange( formationName, *formationColor, startK - 1, endK - 1 );
else
formationNames.appendFormationRange( formationName, startK - 1, endK - 1 );
}
else // no color present
{
formationNames.appendFormationRange( formationName, startK - 1, endK - 1 );
}
}
else if ( numberWords.size() == 1 )
{
bool isNumber1 = false;
int kLayerCount = numberWords[0].toInt( &isNumber1 );
if ( !isNumber1 )
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
if ( QColor::isValidColorName( colorWord ) ) // formation color present at end of line
{
if ( auto formationColor = convertStringToColor( colorWord ) )
formationNames.appendFormationRangeHeight( formationName, *formationColor, kLayerCount );
else
formationNames.appendFormationRangeHeight( formationName, kLayerCount );
}
else // no color present
{
formationNames.appendFormationRangeHeight( formationName, kLayerCount );
}
}
else
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
}
}
++lineNumber;
}
return formationNames;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<RigFormationNames, QString>
RifFormationNamesFileParser::readFmuFormationNameFile( const QString& fileName, const std::map<QString, cvf::Color3f>& colors )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
RigFormationNames formationNames;
QTextStream stream( &dataFile );
auto appendRange = [&]( const QString& name, int start, int end )
{
if ( auto it = colors.find( name.trimmed() ); it != colors.end() )
formationNames.appendFormationRange( name, it->second, start, end );
else
formationNames.appendFormationRange( name, start, end );
};
int lineNumber = 1;
QString currentFormationName;
int startK = -1;
int endK = -1;
while ( !stream.atEnd() )
{
QString line = stream.readLine();
if ( line.isNull() )
{
// Make sure we append the last formation
if ( !currentFormationName.isEmpty() )
{
appendRange( currentFormationName, startK - 1, endK - 1 );
}
break;
}
else
{
QTextStream lineStream( &line );
double kLayer;
QString formationName;
lineStream >> kLayer >> formationName;
if ( lineStream.status() != QTextStream::Ok )
{
return std::unexpected( QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName ) );
}
if ( formationName != currentFormationName )
{
// Append previous formation
if ( !currentFormationName.isEmpty() )
{
appendRange( currentFormationName, startK - 1, endK - 1 );
}
// Start new formation
currentFormationName = formationName;
startK = kLayer;
endK = kLayer;
}
else
{
endK = kLayer;
}
}
}
// Append previous formation at the end of the stream
if ( !currentFormationName.isEmpty() )
{
appendRange( currentFormationName, startK - 1, endK - 1 );
}
return formationNames;
}
//--------------------------------------------------------------------------------------------------
/// Conversion of a color specification from string representation to Color3f.
/// String can be in various formats according to QColor capabilities, notably
/// SVG color keyword names, c.f. https://www.w3.org/TR/SVG11/types.html#ColorKeywords,
/// or #RRGGBB used on a LYR formation data file.
//--------------------------------------------------------------------------------------------------
std::expected<cvf::Color3f, QString> RifFormationNamesFileParser::convertStringToColor( const QString& word )
{
if ( word.isEmpty() ) return std::unexpected( "Empty color string" );
QColor colorQ( word );
if ( colorQ.isValid() )
{
return cvf::Color3f::fromByteColor( colorQ.red(), colorQ.green(), colorQ.blue() );
}
return std::unexpected( "Invalid color: " + word );
}
@@ -0,0 +1,46 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 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 "RigFormationNames.h"
#include "cvfColor3.h"
#include <expected>
#include <map>
#include <QString>
//==================================================================================================
/// Parsers for the individual formation name file formats. Use RifFormationNamesReader to read a
/// file based on its name.
//==================================================================================================
class RifFormationNamesFileParser
{
public:
// Reads zone/subzone colors from a stratigraphy.json file. Subzone colors take precedence over zone colors.
[[nodiscard]] static std::expected<std::map<QString, cvf::Color3f>, QString> readStratigraphyColors( const QString& fileName );
[[nodiscard]] static std::expected<RigFormationNames, QString> readLyrFormationNameFile( const QString& fileName );
[[nodiscard]] static std::expected<RigFormationNames, QString>
readFmuFormationNameFile( const QString& fileName, const std::map<QString, cvf::Color3f>& colors = {} );
[[nodiscard]] static std::expected<RigFormationNames, QString>
readYamlFormationNameFile( const QString& fileName, const std::map<QString, cvf::Color3f>& colors = {} );
[[nodiscard]] static std::expected<cvf::Color3f, QString> convertStringToColor( const QString& word );
};
@@ -18,17 +18,24 @@
#include "RifFormationNamesReader.h"
#include "RifFormationNamesFileParser.h"
#include "RiaLogging.h"
#include "RiaTextStringTools.h"
#include "RigFormationNames.h"
#include "cafAssert.h"
#include "cafPdmUiFilePathEditor.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include "cvfColor3.h"
namespace
{
// Typical FMU location, relative to the folder of the grid case file (e.g. realization-0/iter-0/eclipse/model):
// ../../rms/output/zone/layer_zone_table.txt
// ../../rms/output/zone/stratigraphy.json
// stratigraphy.json is read from the same folder as the layer_zone_table.txt that is imported.
const QString layerZoneTableFileName = "layer_zone_table.txt";
const QString stratigraphyFileName = "stratigraphy.json";
const QString zoneRangesYamlBaseName = "zonation_ecl_map";
} // namespace
//--------------------------------------------------------------------------------------------------
///
@@ -37,219 +44,39 @@ std::expected<RigFormationNames, QString> RifFormationNamesReader::readFormation
{
QFileInfo fileInfo( fileName );
if ( fileInfo.fileName() == "layer_zone_table.txt" )
const QString suffix = fileInfo.suffix().toLower();
if ( ( suffix == "yml" || suffix == "yaml" ) && fileInfo.completeBaseName() != zoneRangesYamlBaseName )
{
return RifFormationNamesReader::readFmuFormationNameFile( fileName );
// Other yaml files are ignored without reporting an error
return RigFormationNames();
}
if ( fileInfo.fileName() == layerZoneTableFileName || suffix == "yml" || suffix == "yaml" )
{
std::map<QString, cvf::Color3f> colors;
const QString colorFile = fileInfo.dir().absoluteFilePath( stratigraphyFileName );
if ( QFile::exists( colorFile ) )
{
if ( auto result = RifFormationNamesFileParser::readStratigraphyColors( colorFile ); result )
{
colors = std::move( *result );
}
else
{
RiaLogging::warning( "Ignoring zone colors: " + result.error().toStdString() );
}
}
if ( suffix == "yml" || suffix == "yaml" )
{
return RifFormationNamesFileParser::readYamlFormationNameFile( fileName, colors );
}
return RifFormationNamesFileParser::readFmuFormationNameFile( fileName, colors );
}
else
{
return RifFormationNamesReader::readLyrFormationNameFile( fileName );
return RifFormationNamesFileParser::readLyrFormationNameFile( fileName );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<RigFormationNames, QString> RifFormationNamesReader::readLyrFormationNameFile( const QString& fileName )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
RigFormationNames formationNames;
QTextStream stream( &dataFile );
int lineNumber = 1;
while ( !stream.atEnd() )
{
QString line = stream.readLine();
QStringList lineSegs = line.split( "'" );
if ( lineSegs.empty() ) continue; // Empty line
if ( lineSegs.size() == 1 ) continue; // No name present. Comment line ?
if ( lineSegs.size() == 2 )
{
RiaLogging::warning( ( fileName + ": missing quote on line " + QString::number( lineNumber ) ).toStdString() );
continue; // One quote present
}
if ( lineSegs.size() == 3 ) // Normal case
{
if ( lineSegs[0].contains( "--" ) ) continue; // Comment line
QString formationName = lineSegs[1];
int commentMarkPos = lineSegs[2].indexOf( "--" );
QString numberString = lineSegs[2];
if ( commentMarkPos >= 0 ) numberString.truncate( commentMarkPos );
// extract last word which may contain formation color
QString colorWord = RiaTextStringTools::splitSkipEmptyParts( numberString ).last();
if ( QColor::isValidColorName( colorWord ) ) numberString.remove( colorWord ); // remove color if present as last word on line
// extract words containing formation number(s)
QStringList numberWords = RiaTextStringTools::splitSkipEmptyParts( numberString, QRegularExpression( "-" ) );
if ( numberWords.size() == 2 ) // formation range with or without color at end of line
{
bool isNumber1 = false;
bool isNumber2 = false;
int startK = numberWords[0].toInt( &isNumber1 );
int endK = numberWords[1].toInt( &isNumber2 );
if ( !( isNumber2 && isNumber1 ) )
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
int tmp = startK;
startK = tmp < endK ? tmp : endK;
endK = tmp > endK ? tmp : endK;
if ( QColor::isValidColorName( colorWord ) ) // formation color present at end of line
{
cvf::Color3f formationColor;
convertStringToColor( colorWord, &formationColor );
formationNames.appendFormationRange( formationName, formationColor, startK - 1, endK - 1 );
}
else // no color present
{
formationNames.appendFormationRange( formationName, startK - 1, endK - 1 );
}
}
else if ( numberWords.size() == 1 )
{
bool isNumber1 = false;
int kLayerCount = numberWords[0].toInt( &isNumber1 );
if ( !isNumber1 )
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
if ( QColor::isValidColorName( colorWord ) ) // formation color present at end of line
{
cvf::Color3f formationColor;
convertStringToColor( colorWord, &formationColor );
formationNames.appendFormationRangeHeight( formationName, formationColor, kLayerCount );
}
else // no color present
{
formationNames.appendFormationRangeHeight( formationName, kLayerCount );
}
}
else
{
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
}
}
++lineNumber;
}
return formationNames;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<RigFormationNames, QString> RifFormationNamesReader::readFmuFormationNameFile( const QString& fileName )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
return std::unexpected( "Could not open file: " + fileName );
}
RigFormationNames formationNames;
QTextStream stream( &dataFile );
int lineNumber = 1;
QString currentFormationName;
int startK = -1;
int endK = -1;
while ( !stream.atEnd() )
{
QString line = stream.readLine();
if ( line.isNull() )
{
// Make sure we append the last formation
if ( !currentFormationName.isEmpty() )
{
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
break;
}
else
{
QTextStream lineStream( &line );
double kLayer;
QString formationName;
lineStream >> kLayer >> formationName;
if ( lineStream.status() != QTextStream::Ok )
{
return std::unexpected( QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName ) );
}
if ( formationName != currentFormationName )
{
// Append previous formation
if ( !currentFormationName.isEmpty() )
{
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
// Start new formation
currentFormationName = formationName;
startK = kLayer;
endK = kLayer;
}
else
{
endK = kLayer;
}
}
}
// Append previous formation at the end of the stream
if ( !currentFormationName.isEmpty() )
{
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
return formationNames;
}
//--------------------------------------------------------------------------------------------------
/// Conversion of a color specification from string representation to Color3f.
/// String can be in various formats according to QColor capabilities, notably
/// SVG color keyword names, c.f. https://www.w3.org/TR/SVG11/types.html#ColorKeywords,
/// or #RRGGBB used on a LYR formation data file.
//--------------------------------------------------------------------------------------------------
bool RifFormationNamesReader::convertStringToColor( const QString& word, cvf::Color3f* color )
{
if ( word.isEmpty() ) return false;
QColor colorQ( word );
if ( colorQ.isValid() )
{
*color = cvf::Color3f::fromByteColor( colorQ.red(), colorQ.green(), colorQ.blue() );
return true;
}
return false;
}
@@ -17,20 +17,12 @@
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "RigFormationNames.h"
#include <expected>
#include <QString>
namespace cvf
{
class Color3f;
}
//==================================================================================================
///
//==================================================================================================
@@ -38,10 +30,4 @@ class RifFormationNamesReader
{
public:
[[nodiscard]] static std::expected<RigFormationNames, QString> readFormationNamesFile( const QString& fileName );
private:
static std::expected<RigFormationNames, QString> readLyrFormationNameFile( const QString& fileName );
static std::expected<RigFormationNames, QString> readFmuFormationNameFile( const QString& fileName );
static bool convertStringToColor( const QString& word, cvf::Color3f* color );
};
@@ -52,14 +52,20 @@ QStringList RimFormationTools::formationFoldersFromCaseFileName( const QString c
//--------------------------------------------------------------------------------------------------
RimFormationNames* RimFormationTools::loadFormationNamesFromFolder( const QStringList& folderNames )
{
QStringList filters;
filters << "*.lyr";
// Priority: .lyr (may contain colors), then zone range yaml files, then FMU layer zone table
const std::vector<QStringList> filterPriority = { { "*.lyr" },
{ "zonation_ecl_map.yml", "zonation_ecl_map.yaml", "zone_layers.yml", "zone_layers.yaml" },
{ "layer_zone_table.txt" } };
QStringList fileList;
for ( auto& folder : folderNames )
{
fileList.append( caf::Utils::getFilesInDirectory( folder, filters, true /*absolute filename*/ ) );
for ( const QStringList& filters : filterPriority )
{
fileList = caf::Utils::getFilesInDirectory( folder, filters, true /*absolute filename*/ );
if ( !fileList.isEmpty() ) break;
}
if ( !fileList.isEmpty() ) break;
}
@@ -126,6 +126,7 @@ set(SOURCE_UNITTEST_FILES
${CMAKE_CURRENT_LIST_DIR}/RigHexGradientTools-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceImporter-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFormationNamesReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFormationNamesFileParser-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifWellPathFormationReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RigWellPathFormations-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvHtmlTableTools-Test.cpp
@@ -0,0 +1,104 @@
#include "gtest/gtest.h"
#include "RiaTestDataDirectory.h"
#include "RifFormationNamesFileParser.h"
#include "RigFormationNames.h"
#include "cvfColor3.h"
#include <QDir>
#include <QString>
namespace
{
QString testFile( const QString& relativePath )
{
return QDir( TEST_DATA_DIR ).absoluteFilePath( "RifFormationNamesReader/" + relativePath );
}
} // namespace
TEST( RifFormationNamesFileParser, ConvertStringToColor )
{
auto named = RifFormationNamesFileParser::convertStringToColor( "red" );
ASSERT_TRUE( named.has_value() );
EXPECT_EQ( 1.0f, named->r() );
EXPECT_EQ( 0.0f, named->g() );
auto hex = RifFormationNamesFileParser::convertStringToColor( "#0000FF" );
ASSERT_TRUE( hex.has_value() );
EXPECT_EQ( 1.0f, hex->b() );
EXPECT_FALSE( RifFormationNamesFileParser::convertStringToColor( "" ).has_value() );
EXPECT_FALSE( RifFormationNamesFileParser::convertStringToColor( "not-a-color" ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadLyrFile )
{
auto fm = RifFormationNamesFileParser::readLyrFormationNameFile( testFile( "Norne_ATW2013ColorName.lyr" ) );
ASSERT_TRUE( fm.has_value() );
EXPECT_EQ( "Garn 3", fm->formationNameFromKLayerIdx( 0 ) );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 1, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_FALSE( RifFormationNamesFileParser::readLyrFormationNameFile( testFile( "missing.lyr" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadFmuFileWithExplicitColors )
{
std::map<QString, cvf::Color3f> colors = { { "Therys", cvf::Color3f( 0.0f, 1.0f, 0.0f ) } };
auto fm = RifFormationNamesFileParser::readFmuFormationNameFile( testFile( "fmu_nocolors/layer_zone_table.txt" ), colors );
ASSERT_TRUE( fm.has_value() );
cvf::Color3f color;
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 0, &color ) );
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 1, &color ) );
EXPECT_EQ( 1.0f, color.g() );
EXPECT_FALSE( RifFormationNamesFileParser::readFmuFormationNameFile( testFile( "missing.txt" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadYamlFile )
{
auto fm = RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu/zonation_ecl_map.yml" ) );
ASSERT_TRUE( fm.has_value() );
// Invalid and unrelated lines are skipped
EXPECT_EQ( 3u, fm->formationNames().size() );
EXPECT_EQ( "Valysar", fm->formationNameFromKLayerIdx( 0 ) );
EXPECT_EQ( "Therys", fm->formationNameFromKLayerIdx( 20 ) );
EXPECT_EQ( "Volon", fm->formationNameFromKLayerIdx( 54 ) );
cvf::Color3f color;
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 0, &color ) );
}
TEST( RifFormationNamesFileParser, ReadYamlFileWithColorsAndErrors )
{
std::map<QString, cvf::Color3f> colors = { { "Valysar", cvf::Color3f( 1.0f, 0.0f, 0.0f ) } };
auto fm = RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu/zonation_ecl_map.yml" ), colors );
ASSERT_TRUE( fm.has_value() );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 5, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_FALSE( RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu_malformed/zonation_ecl_map.yaml" ) ).has_value() );
EXPECT_FALSE( RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "missing.yml" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadStratigraphyColorsSubzonePrecedence )
{
auto colors = RifFormationNamesFileParser::readStratigraphyColors( testFile( "fmu/stratigraphy.json" ) );
ASSERT_TRUE( colors.has_value() );
EXPECT_EQ( 1.0f, colors->at( "Valysar" ).r() );
EXPECT_EQ( 1.0f, colors->at( "Therys" ).b() );
EXPECT_EQ( 1u, colors->count( "Other" ) );
EXPECT_EQ( 0u, colors->count( "Volon" ) );
EXPECT_FALSE( RifFormationNamesFileParser::readStratigraphyColors( testFile( "missing.json" ) ).has_value() );
}
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"
#include "RiaTestDataDirectory.h"
#include "RifFormationNamesFileParser.h"
#include "RifFormationNamesReader.h"
#include "RigFormationNames.h"
@@ -77,3 +78,97 @@ TEST( RifFormationNamesReader, ReadLYRFileWithColorHTML )
EXPECT_EQ( 0.0f, formationColor.g() );
EXPECT_EQ( 0.0f, formationColor.b() );
}
TEST( RifFormationNamesReader, ReadFmuFormationNamesWithStratigraphyColors )
{
QDir baseFolder( TEST_DATA_DIR );
auto fm = RifFormationNamesReader::readFormationNamesFile(
baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu/layer_zone_table.txt" ) );
ASSERT_TRUE( fm.has_value() );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 0, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_EQ( 0.0f, color.g() );
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 2, &color ) );
EXPECT_EQ( 1.0f, color.b() );
// Invalid color in json: formation keeps default coloring
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 4, &color ) );
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 5, &color ) );
EXPECT_NEAR( 128.0f / 255.0f, color.r(), 1e-3 );
}
TEST( RifFormationNamesReader, ReadStratigraphyColorsParentFallback )
{
QDir baseFolder( TEST_DATA_DIR );
auto colors = RifFormationNamesFileParser::readStratigraphyColors(
baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu/stratigraphy.json" ) );
ASSERT_TRUE( colors.has_value() );
ASSERT_EQ( 1u, colors->count( "TopVolantis_BaseVolantis" ) );
EXPECT_EQ( 1.0f, colors->at( "TopVolantis_BaseVolantis" ).g() );
EXPECT_EQ( 0u, colors->count( "Volon" ) );
}
TEST( RifFormationNamesReader, ReadFmuFormationNamesIgnoresBadOrMissingStratigraphy )
{
QDir baseFolder( TEST_DATA_DIR );
for ( const QString folder : { "fmu_malformed", "fmu_nocolors" } )
{
auto fm = RifFormationNamesReader::readFormationNamesFile(
baseFolder.absoluteFilePath( "RifFormationNamesReader/" + folder + "/layer_zone_table.txt" ) );
ASSERT_TRUE( fm.has_value() );
EXPECT_EQ( "Therys", fm->formationNameFromKLayerIdx( 1 ) );
cvf::Color3f color;
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 0, &color ) );
}
auto bad = RifFormationNamesFileParser::readStratigraphyColors(
baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu_malformed/stratigraphy.json" ) );
EXPECT_FALSE( bad.has_value() );
}
TEST( RifFormationNamesReader, ReadYamlZoneRangesWithStratigraphyColors )
{
QDir baseFolder( TEST_DATA_DIR );
auto fm = RifFormationNamesReader::readFormationNamesFile(
baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu/zonation_ecl_map.yml" ) );
ASSERT_TRUE( fm.has_value() );
EXPECT_EQ( 3u, fm->formationNames().size() );
EXPECT_EQ( "Valysar", fm->formationNameFromKLayerIdx( 0 ) );
EXPECT_EQ( "Valysar", fm->formationNameFromKLayerIdx( 19 ) );
EXPECT_EQ( "Therys", fm->formationNameFromKLayerIdx( 20 ) );
EXPECT_EQ( "Volon", fm->formationNameFromKLayerIdx( 68 ) );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 0, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 60, &color ) );
}
TEST( RifFormationNamesReader, ReadYamlWithoutZrangesFails )
{
QDir baseFolder( TEST_DATA_DIR );
auto fm = RifFormationNamesReader::readFormationNamesFile(
baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu_malformed/zonation_ecl_map.yaml" ) );
EXPECT_FALSE( fm.has_value() );
}
TEST( RifFormationNamesReader, OtherYamlFilesAreNotRead )
{
QDir baseFolder( TEST_DATA_DIR );
auto fm = RifFormationNamesReader::readFormationNamesFile( baseFolder.absoluteFilePath( "RifFormationNamesReader/fmu/other.yml" ) );
ASSERT_TRUE( fm.has_value() );
EXPECT_TRUE( fm->formationNames().empty() );
}
@@ -0,0 +1,6 @@
1 Valysar
2 Valysar
3 Therys
4 Therys
5 Volon
6 Other
@@ -0,0 +1,2 @@
zranges:
- Valysar: [1, 20]
@@ -0,0 +1,12 @@
[
{
"name": "TopVolantis_BaseVolantis",
"color": "#00FF00",
"subzones": [
{ "name": "Valysar", "color": "#FF0000" },
{ "name": "Therys", "color": "#0000FF" },
{ "name": "Volon" , "color": "not-a-color" }
]
},
{ "name": "Other", "color": "#808080", "extra": 1 }
]
@@ -0,0 +1,7 @@
zranges:
- Valysar: [1, 20] # comment
- Therys: [21, 54]
- "Volon": [55, 69]
- broken
other:
- Ignored: [1, 2]
@@ -0,0 +1 @@
[ { "name": "Valysar", "color":
@@ -0,0 +1,2 @@
not_zranges:
- Valysar: [1, 20]