Summary Calculation: add import and export to toml

This commit is contained in:
Kristian Bendiksen authored and Magne Sjaastad committed 2023-12-04 17:26:44 +01:00
1 parent 232706a4e9
commit ca87cee79b
25 files changed
+866 -73

No files matched your search

@@ -92,6 +92,9 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculation.h
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationImporter.h
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCalculation.h
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCalculationImporter.h
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCalculationExporter.h
)
set(SOURCE_GROUP_SOURCE_FILES
@@ -184,6 +187,8 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RifEclipseSummaryAddressDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationImporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCalculationImporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCalculationExporter.cpp
)
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
@@ -727,6 +727,18 @@ std::string RifEclipseSummaryAddress::itemUiText() const
return text;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string RifEclipseSummaryAddress::toEclipseTextAddress() const
{
std::string noVectorName = itemUiText();
if ( noVectorName.empty() )
return m_vectorName;
else
return m_vectorName + ":" + noVectorName;
}
//--------------------------------------------------------------------------------------------------
/// Returns the stringified address component requested
//--------------------------------------------------------------------------------------------------
@@ -120,6 +120,8 @@ public:
std::string ensembleStatisticsVectorName() const;
std::string toEclipseTextAddress() const;
// Derived properties
std::string uiText() const;
@@ -0,0 +1,40 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
struct RifSummaryCalculationVariable
{
std::string name;
std::string address;
};
struct RifSummaryCalculation
{
std::string expression;
std::string unit;
bool distributeToAllCases;
bool distributeToOther;
std::vector<RifSummaryCalculationVariable> variables;
};
@@ -0,0 +1,72 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifSummaryCalculationExporter.h"
#include <fstream>
#include <tomlplusplus/toml.hpp>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<bool, std::string> RifSummaryCalculationExporter::writeToFile( const std::vector<RifSummaryCalculation>& calculations,
const std::string& filePath )
{
std::ofstream stream( filePath );
if ( !stream.good() ) return { false, "Unable to open file: " + filePath };
return writeToStream( calculations, stream );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<bool, std::string> RifSummaryCalculationExporter::writeToStream( const std::vector<RifSummaryCalculation>& calculations,
std::ostream& stream )
{
auto calculationsVector = toml::array();
for ( auto calculation : calculations )
{
auto variablesVector = toml::array{};
for ( auto variable : calculation.variables )
{
variablesVector.push_back( toml::table{
{ "name", variable.name },
{ "address", variable.address },
} );
}
calculationsVector.push_back( toml::table{
{ "expression", calculation.expression },
{ "unit", calculation.unit },
{ "distribute-to-other-items", calculation.distributeToOther },
{ "distribute-to-all-cases", calculation.distributeToAllCases },
{ "variables", variablesVector },
} );
}
auto tbl = toml::table{
{ "summary-calculation", calculationsVector },
};
stream << tbl;
return { stream.good(), "" };
}
@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifSummaryCalculation.h"
#include <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
class RifSummaryCalculationExporter
{
public:
static std::pair<bool, std::string> writeToFile( const std::vector<RifSummaryCalculation>& calculations, const std::string& filePath );
static std::pair<bool, std::string> writeToStream( const std::vector<RifSummaryCalculation>& calculations, std::ostream& stream );
};
@@ -0,0 +1,98 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifSummaryCalculationImporter.h"
#include <fstream>
#include <tomlplusplus/toml.hpp>
#include <utility>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::vector<RifSummaryCalculation>, std::string> RifSummaryCalculationImporter::readFromFile( const std::string& filePath )
{
std::ifstream stream( filePath );
if ( !stream.good() ) return { {}, "Unable to open file: " + filePath };
return readFromStream( stream );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::vector<RifSummaryCalculation>, std::string> RifSummaryCalculationImporter::readFromStream( std::istream& stream )
{
try
{
toml::table tbl = toml::parse( stream );
auto calculationsVector = tbl["summary-calculation"];
std::vector<RifSummaryCalculation> calculations;
if ( toml::array* arr = calculationsVector.as_array() )
{
for ( auto&& a : *arr )
{
RifSummaryCalculation calculation;
if ( toml::table* calc = a.as_table() )
{
calculation.expression = calc->at_path( "expression" ).value_or<std::string>( "" );
if ( calculation.expression.empty() ) throw std::runtime_error( "Missing expression." );
calculation.distributeToOther = calc->at_path( "distribute-to-other-items" ).value_or<bool>( true );
calculation.distributeToAllCases = calc->at_path( "distribute-to-all-cases" ).value_or<bool>( true );
calculation.unit = calc->at_path( "unit" ).value_or<std::string>( "" );
if ( toml::array* vars = calc->at_path( "variables" ).as_array() )
{
std::vector<RifSummaryCalculationVariable> variables;
for ( auto&& v : *vars )
{
if ( toml::table* var = v.as_table() )
{
RifSummaryCalculationVariable variable;
variable.name = var->at_path( "name" ).value_or<std::string>( "" );
variable.address = var->at_path( "address" ).value_or<std::string>( "" );
if ( variable.name.empty() || variable.address.empty() )
throw std::runtime_error( "Incomplete variable: Missing either name or address." );
variables.push_back( variable );
}
}
calculation.variables = variables;
}
calculations.push_back( calculation );
}
}
}
if ( calculations.empty() )
{
return { calculations, "No calculations imported." };
}
return { calculations, "" };
}
catch ( const std::runtime_error& error )
{
return { {}, error.what() };
}
}
@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifSummaryCalculation.h"
#include <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
class RifSummaryCalculationImporter
{
public:
static std::pair<std::vector<RifSummaryCalculation>, std::string> readFromFile( const std::string& filePath );
static std::pair<std::vector<RifSummaryCalculation>, std::string> readFromStream( std::istream& stream );
};