mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#7563 HDF5 Export : Add export of summary data to HDF5
This commit is contained in:
159
ApplicationLibCode/FileInterface/RifHdf5Exporter.cpp
Normal file
159
ApplicationLibCode/FileInterface/RifHdf5Exporter.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2021 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 "RifHdf5Exporter.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifHdf5Exporter::RifHdf5Exporter( const std::string& fileName )
|
||||
: m_fileName( fileName )
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create new or overwrite existing
|
||||
m_hdfFile = new H5::H5File( m_fileName, H5F_ACC_TRUNC );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
delete m_hdfFile;
|
||||
m_hdfFile = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifHdf5Exporter::~RifHdf5Exporter()
|
||||
{
|
||||
if ( m_hdfFile )
|
||||
{
|
||||
delete m_hdfFile;
|
||||
m_hdfFile = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifHdf5Exporter::writeDataset( const H5::Group& group, const std::string& datasetName, const std::vector<float>& values )
|
||||
{
|
||||
try
|
||||
{
|
||||
hsize_t dimsf[1];
|
||||
dimsf[0] = values.size();
|
||||
H5::DataSpace dataspace( 1, dimsf );
|
||||
|
||||
H5::DataType datatype( H5::PredType::NATIVE_FLOAT );
|
||||
H5::DataSet dataset = group.createDataSet( datasetName, datatype, dataspace );
|
||||
dataset.write( values.data(), H5::PredType::NATIVE_FLOAT );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifHdf5Exporter::writeDataset( const H5::Group& group, const std::string& datasetName, const std::vector<int>& values )
|
||||
{
|
||||
try
|
||||
{
|
||||
hsize_t dimsf[1];
|
||||
dimsf[0] = values.size();
|
||||
H5::DataSpace dataspace( 1, dimsf );
|
||||
|
||||
H5::DataType datatype( H5::PredType::NATIVE_INT );
|
||||
H5::DataSet dataset = group.createDataSet( datasetName, datatype, dataspace );
|
||||
dataset.write( values.data(), H5::PredType::NATIVE_INT );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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 group;
|
||||
if ( parentGroup )
|
||||
{
|
||||
try
|
||||
{
|
||||
group = parentGroup->openGroup( groupName );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
group = parentGroup->createGroup( groupName );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
group = m_hdfFile->openGroup( groupName );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
group = m_hdfFile->createGroup( groupName );
|
||||
}
|
||||
}
|
||||
|
||||
return group;
|
||||
}
|
||||
53
ApplicationLibCode/FileInterface/RifHdf5Exporter.h
Normal file
53
ApplicationLibCode/FileInterface/RifHdf5Exporter.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2021 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 "H5Cpp.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifHdf5Exporter
|
||||
{
|
||||
public:
|
||||
explicit RifHdf5Exporter( const std::string& fileName );
|
||||
~RifHdf5Exporter();
|
||||
|
||||
H5::Group findOrCreateGroup( 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 );
|
||||
|
||||
private:
|
||||
std::string m_fileName;
|
||||
H5::H5File* m_hdfFile;
|
||||
};
|
||||
119
ApplicationLibCode/FileInterface/RifHdf5SummaryExporter.cpp
Normal file
119
ApplicationLibCode/FileInterface/RifHdf5SummaryExporter.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2021 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 "RifHdf5SummaryExporter.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
#include "RifHdf5Exporter.h"
|
||||
#include "RifSummaryReaderInterface.h"
|
||||
|
||||
#include "opm/io/eclipse/ESmry.hpp"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifHdf5SummaryExporter::writeSummaryDataToHdf( const std::string& hdfFileName, Opm::EclIO::ESmry& sourceSummaryData )
|
||||
{
|
||||
auto timesteps = sourceSummaryData.numberOfTimeSteps();
|
||||
if ( timesteps == 0 ) return false;
|
||||
|
||||
RifHdf5Exporter exporter( hdfFileName );
|
||||
|
||||
writeGeneralSection( exporter, sourceSummaryData );
|
||||
writeSummaryVectors( exporter, sourceSummaryData );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData )
|
||||
{
|
||||
auto timesteps = sourceSummaryData.dates();
|
||||
|
||||
{
|
||||
std::vector<int> values( 1 );
|
||||
values[0] = -1;
|
||||
|
||||
exporter.writeDataset( "general", "checksum", values );
|
||||
}
|
||||
|
||||
{
|
||||
auto startDate = sourceSummaryData.startdate();
|
||||
|
||||
time_t firstTimeStep = std::chrono::system_clock::to_time_t( startDate );
|
||||
|
||||
QDateTime dt = QDateTime::fromTime_t( firstTimeStep );
|
||||
|
||||
int day = dt.date().day();
|
||||
int month = dt.date().month();
|
||||
int year = dt.date().year();
|
||||
|
||||
int hour = dt.time().hour();
|
||||
int minute = dt.time().minute();
|
||||
int second = dt.time().second();
|
||||
|
||||
std::vector<int> timeValues( 7 );
|
||||
timeValues[0] = day;
|
||||
timeValues[1] = month;
|
||||
timeValues[2] = year;
|
||||
timeValues[3] = hour;
|
||||
timeValues[4] = minute;
|
||||
timeValues[5] = second;
|
||||
timeValues[6] = 0; // Unknown value, could be millisec
|
||||
|
||||
exporter.writeDataset( "general", "start_date", timeValues );
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> values( 2 );
|
||||
values[0] = 1;
|
||||
values[1] = 7;
|
||||
|
||||
exporter.writeDataset( "general", "version", values );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifHdf5SummaryExporter::writeSummaryVectors( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData )
|
||||
{
|
||||
size_t valueCount = sourceSummaryData.numberOfTimeSteps();
|
||||
if ( valueCount == 0 ) return false;
|
||||
|
||||
const std::string datasetName( "values" );
|
||||
|
||||
const std::vector<Opm::EclIO::SummaryNode>& summaryNodeList = sourceSummaryData.summaryNodeList();
|
||||
|
||||
auto summaryVectorsGroup = exporter.findOrCreateGroup( nullptr, "summary_vectors" );
|
||||
|
||||
for ( const auto& summaryNode : summaryNodeList )
|
||||
{
|
||||
auto smspecKeywordIndex = summaryNode.smspecKeywordIndex;
|
||||
QString smspecKeywordText = QString( "%1" ).arg( smspecKeywordIndex );
|
||||
const auto& quantity = summaryNode.keyword;
|
||||
const std::vector<float>& values = sourceSummaryData.get( summaryNode );
|
||||
|
||||
exporter.exportSummaryVector( summaryVectorsGroup, quantity, smspecKeywordText.toStdString(), datasetName, values );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
48
ApplicationLibCode/FileInterface/RifHdf5SummaryExporter.h
Normal file
48
ApplicationLibCode/FileInterface/RifHdf5SummaryExporter.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2021 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>
|
||||
|
||||
class RifSummaryReaderInterface;
|
||||
class RifHdf5Exporter;
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
namespace EclIO
|
||||
{
|
||||
class ESmry;
|
||||
struct SummaryNode;
|
||||
} // namespace EclIO
|
||||
} // namespace Opm
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifHdf5SummaryExporter
|
||||
{
|
||||
public:
|
||||
static bool writeSummaryDataToHdf( const std::string& hdfFileName, Opm::EclIO::ESmry& sourceSummaryData );
|
||||
|
||||
private:
|
||||
static bool writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData );
|
||||
static bool writeSummaryVectors( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData );
|
||||
};
|
||||
Reference in New Issue
Block a user