2021-04-11 01:51:32 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2023-01-03 07:52:33 -06:00
|
|
|
#include "RiaFilePathTools.h"
|
2021-04-13 06:04:00 -05:00
|
|
|
#include "RiaLogging.h"
|
2021-04-16 03:52:30 -05:00
|
|
|
#include "RiaPreferencesSummary.h"
|
2021-09-29 06:59:44 -05:00
|
|
|
#include "RiaStdStringTools.h"
|
2021-04-13 06:04:00 -05:00
|
|
|
|
2021-04-11 01:51:32 -05:00
|
|
|
#include "RifHdf5Exporter.h"
|
2022-07-07 09:42:06 -05:00
|
|
|
#include "RifOpmCommonSummary.h"
|
2021-04-11 01:51:32 -05:00
|
|
|
#include "RifSummaryReaderInterface.h"
|
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Disable warning from external library to make sure treat warnings as error works
|
|
|
|
#pragma warning( disable : 4267 )
|
|
|
|
#endif
|
2021-04-16 08:16:00 -05:00
|
|
|
#include "opm/common/utility/FileSystem.hpp"
|
2021-04-12 07:01:06 -05:00
|
|
|
#include "opm/common/utility/TimeService.hpp"
|
2021-04-11 01:51:32 -05:00
|
|
|
#include "opm/io/eclipse/ESmry.hpp"
|
|
|
|
|
2021-04-11 13:53:27 -05:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QString>
|
|
|
|
|
2021-04-11 01:51:32 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-04-13 12:19:55 -05:00
|
|
|
bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames,
|
|
|
|
const std::vector<std::string>& h5FileNames,
|
2024-04-19 08:09:15 -05:00
|
|
|
bool createHdfIfNotPresent,
|
2021-04-13 12:19:55 -05:00
|
|
|
int threadCount )
|
|
|
|
{
|
|
|
|
if ( smspecFileNames.empty() ) return true;
|
|
|
|
|
|
|
|
if ( smspecFileNames.size() != h5FileNames.size() ) return false;
|
|
|
|
|
|
|
|
size_t hdfFilesCreatedCount = 0;
|
|
|
|
|
2021-04-16 03:52:30 -05:00
|
|
|
bool useMultipleThreads = threadCount > 1;
|
|
|
|
|
|
|
|
#pragma omp parallel for schedule( dynamic ) if ( useMultipleThreads ) num_threads( threadCount )
|
2021-04-13 12:19:55 -05:00
|
|
|
for ( int cIdx = 0; cIdx < static_cast<int>( smspecFileNames.size() ); ++cIdx )
|
|
|
|
{
|
2021-04-16 03:52:30 -05:00
|
|
|
const auto& smspecFileName = smspecFileNames[cIdx];
|
|
|
|
const auto& h5FileName = h5FileNames[cIdx];
|
2021-04-13 12:19:55 -05:00
|
|
|
|
2024-04-19 08:09:15 -05:00
|
|
|
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( smspecFileName, h5FileName, createHdfIfNotPresent, hdfFilesCreatedCount );
|
2021-04-13 12:19:55 -05:00
|
|
|
}
|
|
|
|
|
2021-06-10 01:09:48 -05:00
|
|
|
if ( hdfFilesCreatedCount > 0 )
|
2021-04-13 12:19:55 -05:00
|
|
|
{
|
|
|
|
QString txt = QString( "Created [ %1 ] h5 files from a total of [ %2 ] summary files" )
|
|
|
|
.arg( static_cast<int>( hdfFilesCreatedCount ) )
|
|
|
|
.arg( static_cast<int>( smspecFileNames.size() ) );
|
|
|
|
RiaLogging::info( txt );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2024-04-19 08:09:15 -05:00
|
|
|
bool RifHdf5SummaryExporter::ensureHdf5FileIsCreated( const std::string& smspecFileName,
|
|
|
|
const std::string& h5FileName,
|
|
|
|
bool createHdfIfNotPresent,
|
|
|
|
size_t& hdfFilesCreatedCount )
|
2021-04-11 01:51:32 -05:00
|
|
|
{
|
2024-04-19 08:09:15 -05:00
|
|
|
// If an H5 file is present, and the SMSPEC file is newer than the H5 file, the H5 file will be recreated.
|
|
|
|
// If no H5 file is present, the H5 file will be created if the flag createHdfIfNotPresent is set to true.
|
|
|
|
//
|
|
|
|
// NB! Always make sure the logic is consistent with the logic in RifOpmCommonEclipseSummary::open
|
2021-04-16 03:52:30 -05:00
|
|
|
|
2024-04-19 08:09:15 -05:00
|
|
|
if ( !std::filesystem::exists( smspecFileName ) ) return false;
|
2021-04-16 03:52:30 -05:00
|
|
|
|
|
|
|
bool exportIsRequired = false;
|
2021-04-11 01:51:32 -05:00
|
|
|
|
2024-04-19 08:09:15 -05:00
|
|
|
bool h5FileExists = std::filesystem::exists( h5FileName );
|
|
|
|
if ( !h5FileExists )
|
2021-04-16 03:52:30 -05:00
|
|
|
{
|
2024-04-19 08:09:15 -05:00
|
|
|
if ( createHdfIfNotPresent )
|
2021-12-01 00:17:11 -06:00
|
|
|
{
|
|
|
|
exportIsRequired = true;
|
|
|
|
}
|
2024-04-19 08:09:15 -05:00
|
|
|
}
|
|
|
|
else if ( RiaFilePathTools::isFirstOlderThanSecond( h5FileName, smspecFileName ) )
|
|
|
|
{
|
|
|
|
// If both files are present, check if the SMSPEC file is newer than the H5 file. If the SMSPEC file is newer, we abort if it is not
|
|
|
|
// possible to write to the H5 file
|
|
|
|
|
|
|
|
// Check if we have write permission in the folder
|
|
|
|
QFileInfo info( QString::fromStdString( smspecFileName ) );
|
|
|
|
|
|
|
|
if ( !info.isWritable() )
|
2021-04-16 03:52:30 -05:00
|
|
|
{
|
2024-04-19 08:09:15 -05:00
|
|
|
QString txt =
|
|
|
|
QString( "HDF is older than SMSPEC, but export to file %1 failed due to missing write permissions. Aborting operation." )
|
|
|
|
.arg( QString::fromStdString( h5FileName ) );
|
|
|
|
RiaLogging::error( txt );
|
|
|
|
|
|
|
|
return false;
|
2021-04-16 03:52:30 -05:00
|
|
|
}
|
2024-04-19 08:09:15 -05:00
|
|
|
|
|
|
|
exportIsRequired = true;
|
2021-04-16 03:52:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( exportIsRequired )
|
2021-04-11 13:53:27 -05:00
|
|
|
{
|
2021-04-13 06:04:00 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Opm::EclIO::ESmry sourceSummaryData( smspecFileName );
|
|
|
|
|
|
|
|
// Read all data summary data before starting export to HDF. Loading one and one summary vector causes huge
|
|
|
|
// performance penalty
|
2022-07-07 09:42:06 -05:00
|
|
|
sourceSummaryData.loadData();
|
2021-04-13 06:04:00 -05:00
|
|
|
|
2021-04-13 12:19:55 -05:00
|
|
|
#pragma omp critical( critical_section_HDF5_export )
|
|
|
|
{
|
2021-04-16 03:52:30 -05:00
|
|
|
// HDF5 file access is not thread-safe, always make sure we use the HDF5 library from a single thread
|
|
|
|
|
2021-04-13 12:19:55 -05:00
|
|
|
RifHdf5Exporter exporter( h5FileName );
|
|
|
|
|
|
|
|
writeGeneralSection( exporter, sourceSummaryData );
|
|
|
|
writeSummaryVectors( exporter, sourceSummaryData );
|
2021-04-11 01:51:32 -05:00
|
|
|
|
2021-04-13 12:19:55 -05:00
|
|
|
hdfFilesCreatedCount++;
|
|
|
|
}
|
2021-04-13 06:04:00 -05:00
|
|
|
}
|
|
|
|
catch ( std::exception& e )
|
|
|
|
{
|
2024-04-19 08:09:15 -05:00
|
|
|
QString txt = QString( "HDF export to file %1 failed : %2" ).arg( QString::fromStdString( smspecFileName ), e.what() );
|
2021-04-13 02:26:58 -05:00
|
|
|
|
2021-04-13 06:04:00 -05:00
|
|
|
RiaLogging::error( txt );
|
2021-04-11 13:53:27 -05:00
|
|
|
|
2021-04-13 06:04:00 -05:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-11 13:53:27 -05:00
|
|
|
}
|
2021-04-11 01:51:32 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RifHdf5SummaryExporter::writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData )
|
|
|
|
{
|
|
|
|
auto timesteps = sourceSummaryData.dates();
|
|
|
|
|
2021-04-12 06:46:59 -05:00
|
|
|
auto group = exporter.createGroup( nullptr, "general" );
|
|
|
|
|
2021-04-11 01:51:32 -05:00
|
|
|
{
|
|
|
|
std::vector<int> values( 1 );
|
|
|
|
values[0] = -1;
|
|
|
|
|
2021-04-12 06:46:59 -05:00
|
|
|
exporter.writeDataset( group, "checksum", values );
|
2021-04-11 01:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto startDate = sourceSummaryData.startdate();
|
|
|
|
|
2021-04-12 07:01:06 -05:00
|
|
|
// Make sure we convert time identically as the inverse of make_date in ESmry.cpp
|
|
|
|
time_t firstTimeStep = std::chrono::system_clock::to_time_t( startDate );
|
|
|
|
Opm::TimeStampUTC opmTimeStamp( firstTimeStep );
|
2021-04-11 01:51:32 -05:00
|
|
|
|
2021-04-12 07:01:06 -05:00
|
|
|
int day = opmTimeStamp.day();
|
|
|
|
int month = opmTimeStamp.month();
|
|
|
|
int year = opmTimeStamp.year();
|
2021-04-11 01:51:32 -05:00
|
|
|
|
2021-04-12 07:01:06 -05:00
|
|
|
int hour = opmTimeStamp.hour();
|
|
|
|
int minute = opmTimeStamp.minutes();
|
|
|
|
int second = opmTimeStamp.seconds();
|
2021-04-11 01:51:32 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-04-12 06:46:59 -05:00
|
|
|
exporter.writeDataset( group, "start_date", timeValues );
|
2021-04-11 01:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<int> values( 2 );
|
|
|
|
values[0] = 1;
|
|
|
|
values[1] = 7;
|
|
|
|
|
2021-04-12 06:46:59 -05:00
|
|
|
exporter.writeDataset( group, "version", values );
|
2021-04-11 01:51:32 -05:00
|
|
|
}
|
2021-04-11 13:53:27 -05:00
|
|
|
|
|
|
|
return true;
|
2021-04-11 01:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RifHdf5SummaryExporter::writeSummaryVectors( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData )
|
|
|
|
{
|
|
|
|
size_t valueCount = sourceSummaryData.numberOfTimeSteps();
|
|
|
|
if ( valueCount == 0 ) return false;
|
|
|
|
|
|
|
|
const std::string datasetName( "values" );
|
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
std::map<std::string, std::vector<RifEclipseSummaryAddress>> mapVectorNameToSummaryAddresses;
|
2023-02-26 03:48:40 -06:00
|
|
|
auto [addresses, addressToKeywordMap] = RifOpmCommonSummaryTools::buildAddressesAndKeywordMap( sourceSummaryData.keywordList() );
|
2022-07-07 09:42:06 -05:00
|
|
|
for ( const auto& adr : addresses )
|
2021-04-11 01:51:32 -05:00
|
|
|
{
|
2022-07-07 09:42:06 -05:00
|
|
|
auto vectorName = adr.vectorName();
|
2021-04-12 06:46:59 -05:00
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
if ( mapVectorNameToSummaryAddresses.find( vectorName ) == mapVectorNameToSummaryAddresses.end() )
|
2021-04-12 06:46:59 -05:00
|
|
|
{
|
2022-07-07 09:42:06 -05:00
|
|
|
mapVectorNameToSummaryAddresses[vectorName] = {};
|
2021-04-12 06:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
auto it = mapVectorNameToSummaryAddresses.find( vectorName );
|
|
|
|
if ( it != mapVectorNameToSummaryAddresses.end() )
|
2021-04-12 06:46:59 -05:00
|
|
|
{
|
2022-07-07 09:42:06 -05:00
|
|
|
it->second.push_back( adr );
|
2021-04-12 06:46:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
auto summaryVectorsGroup = exporter.createGroup( nullptr, "summary_vectors" );
|
|
|
|
|
2021-09-29 06:59:44 -05:00
|
|
|
std::set<std::string> exportErrorKeywords;
|
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
for ( const auto& [vectorName, addresses] : mapVectorNameToSummaryAddresses )
|
2021-04-12 06:46:59 -05:00
|
|
|
{
|
2022-07-07 09:42:06 -05:00
|
|
|
auto keywordGroup = exporter.createGroup( &summaryVectorsGroup, vectorName );
|
2021-04-12 06:46:59 -05:00
|
|
|
|
2022-07-07 09:42:06 -05:00
|
|
|
for ( const auto& address : addresses )
|
2021-04-12 06:46:59 -05:00
|
|
|
{
|
2022-07-07 09:42:06 -05:00
|
|
|
auto keyword = addressToKeywordMap[address];
|
|
|
|
auto smspecKeywordIndex = sourceSummaryData.getSmspecIndexForKeyword( keyword );
|
2021-04-16 03:52:30 -05:00
|
|
|
const QString& smspecKeywordText = QString( "%1" ).arg( smspecKeywordIndex );
|
2021-04-12 06:46:59 -05:00
|
|
|
|
2021-09-29 06:59:44 -05:00
|
|
|
try
|
|
|
|
{
|
2023-02-26 03:48:40 -06:00
|
|
|
const std::vector<float>& values = sourceSummaryData.get( keyword );
|
|
|
|
auto dataValuesGroup = exporter.createGroup( &keywordGroup, smspecKeywordText.toStdString() );
|
2021-04-13 02:26:58 -05:00
|
|
|
|
2021-09-29 06:59:44 -05:00
|
|
|
exporter.writeDataset( dataValuesGroup, datasetName, values );
|
|
|
|
dataValuesGroup.close();
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
exportErrorKeywords.insert( keyword );
|
|
|
|
}
|
2021-04-12 06:46:59 -05:00
|
|
|
}
|
2021-04-13 02:26:58 -05:00
|
|
|
|
|
|
|
keywordGroup.close();
|
2021-04-11 01:51:32 -05:00
|
|
|
}
|
|
|
|
|
2021-09-29 06:59:44 -05:00
|
|
|
if ( !exportErrorKeywords.empty() )
|
|
|
|
{
|
|
|
|
std::vector<std::string> keywordVector;
|
|
|
|
for ( const auto& k : exportErrorKeywords )
|
|
|
|
{
|
|
|
|
keywordVector.push_back( k );
|
|
|
|
}
|
|
|
|
auto txt = RiaStdStringTools::joinStrings( keywordVector, ',' );
|
|
|
|
|
|
|
|
QString errorTxt = QString( "Failed to export keywords %1 " ).arg( QString::fromStdString( txt ) );
|
|
|
|
RiaLogging::error( errorTxt );
|
|
|
|
}
|
|
|
|
|
2021-04-11 01:51:32 -05:00
|
|
|
return true;
|
|
|
|
}
|