#7568 Summary HDF : Avoid exeptions when exporting to h5

This commit is contained in:
Magne Sjaastad 2021-04-12 13:46:59 +02:00
parent 3bc569e2a6
commit 8f00c527b1
3 changed files with 48 additions and 60 deletions

View File

@ -48,22 +48,6 @@ RifHdf5Exporter::~RifHdf5Exporter()
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifHdf5Exporter::exportSummaryVector( H5::Group& summaryRootGroup,
const std::string& vectorName,
const std::string& vectorSubNodeName,
const std::string& datasetName,
const std::vector<float>& values )
{
auto summaryGroup = findOrCreateGroup( &summaryRootGroup, vectorName );
auto subNodeGroup = summaryGroup.createGroup( vectorSubNodeName );
return writeDataset( subNodeGroup, datasetName, values );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -115,45 +99,26 @@ bool RifHdf5Exporter::writeDataset( const H5::Group& group, const std::string& d
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifHdf5Exporter::writeDataset( const std::string& groupName, const std::string& datasetName, const std::vector<int>& values )
{
if ( !m_hdfFile ) return false;
{
auto generalGroup = findOrCreateGroup( nullptr, groupName );
return writeDataset( generalGroup, datasetName, values );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
H5::Group RifHdf5Exporter::findOrCreateGroup( H5::Group* parentGroup, const std::string& groupName )
H5::Group RifHdf5Exporter::createGroup( H5::Group* parentGroup, const std::string& groupName )
{
if ( parentGroup )
{
try
{
auto group = parentGroup->openGroup( groupName );
return group;
return parentGroup->createGroup( groupName );
}
catch ( ... )
{
return parentGroup->createGroup( groupName );
}
}
else
{
try
{
auto group = m_hdfFile->openGroup( groupName );
return group;
return m_hdfFile->createGroup( groupName );
}
catch ( ... )
{
return m_hdfFile->createGroup( groupName );
}
}

View File

@ -33,19 +33,10 @@ public:
explicit RifHdf5Exporter( const std::string& fileName );
~RifHdf5Exporter();
H5::Group findOrCreateGroup( H5::Group* parentGroup, const std::string& groupName );
H5::Group createGroup( H5::Group* parentGroup, const std::string& groupName );
bool exportSummaryVector( H5::Group& summaryRootGroup,
const std::string& vectorName,
const std::string& vectorSubNodeName,
const std::string& datasetName,
const std::vector<float>& values );
bool writeDataset( const std::string& groupName, const std::string& datasetName, const std::vector<int>& values );
private:
bool writeDataset( const H5::Group& group, const std::string& datasetName, const std::vector<int>& values );
bool writeDataset( const H5::Group& group, const std::string& datasetName, const std::vector<float>& values );
bool writeDataset( const H5::Group& group, const std::string& datasetName, const std::vector<int>& values );
private:
std::string m_fileName;

View File

@ -56,11 +56,13 @@ bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm
{
auto timesteps = sourceSummaryData.dates();
auto group = exporter.createGroup( nullptr, "general" );
{
std::vector<int> values( 1 );
values[0] = -1;
exporter.writeDataset( "general", "checksum", values );
exporter.writeDataset( group, "checksum", values );
}
{
@ -87,7 +89,7 @@ bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm
timeValues[5] = second;
timeValues[6] = 0; // Unknown value, could be millisec
exporter.writeDataset( "general", "start_date", timeValues );
exporter.writeDataset( group, "start_date", timeValues );
}
{
@ -95,7 +97,7 @@ bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm
values[0] = 1;
values[1] = 7;
exporter.writeDataset( "general", "version", values );
exporter.writeDataset( group, "version", values );
}
return true;
@ -106,23 +108,53 @@ bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm
//--------------------------------------------------------------------------------------------------
bool RifHdf5SummaryExporter::writeSummaryVectors( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData )
{
using SumNodeVector = std::vector<Opm::EclIO::SummaryNode>;
size_t valueCount = sourceSummaryData.numberOfTimeSteps();
if ( valueCount == 0 ) return false;
const std::string datasetName( "values" );
const std::vector<Opm::EclIO::SummaryNode>& summaryNodeList = sourceSummaryData.summaryNodeList();
const SumNodeVector& summaryNodeList = sourceSummaryData.summaryNodeList();
auto summaryVectorsGroup = exporter.findOrCreateGroup( nullptr, "summary_vectors" );
auto summaryVectorsGroup = exporter.createGroup( nullptr, "summary_vectors" );
for ( const auto& summaryNode : summaryNodeList )
std::map<std::string, std::vector<size_t>> mapKeywordToSummaryNodeIndex;
for ( size_t i = 0; i < summaryNodeList.size(); i++ )
{
auto smspecKeywordIndex = summaryNode.smspecKeywordIndex;
QString smspecKeywordText = QString( "%1" ).arg( smspecKeywordIndex );
const auto& quantity = summaryNode.keyword;
const std::vector<float>& values = sourceSummaryData.get( summaryNode );
const auto summaryNode = summaryNodeList[i];
const std::string keyword = summaryNode.keyword;
exporter.exportSummaryVector( summaryVectorsGroup, quantity, smspecKeywordText.toStdString(), datasetName, values );
if ( mapKeywordToSummaryNodeIndex.find( keyword ) == mapKeywordToSummaryNodeIndex.end() )
{
mapKeywordToSummaryNodeIndex[keyword] = std::vector<size_t>();
}
auto it = mapKeywordToSummaryNodeIndex.find( keyword );
if ( it != mapKeywordToSummaryNodeIndex.end() )
{
it->second.push_back( i );
}
}
for ( const auto& nodesForKeyword : mapKeywordToSummaryNodeIndex )
{
std::string keyword = nodesForKeyword.first;
auto keywordGroup = exporter.createGroup( &summaryVectorsGroup, keyword );
for ( auto nodeIndex : nodesForKeyword.second )
{
auto summaryNode = summaryNodeList[nodeIndex];
auto smspecKeywordIndex = summaryNode.smspecKeywordIndex;
QString smspecKeywordText = QString( "%1" ).arg( smspecKeywordIndex );
auto dataValuesGroup = exporter.createGroup( &keywordGroup, smspecKeywordText.toStdString() );
const std::vector<float>& values = sourceSummaryData.get( summaryNode );
exporter.writeDataset( dataValuesGroup, datasetName, values );
}
}
return true;