Always check date for H5 files, and recreate if required

This commit is contained in:
Magne Sjaastad
2024-04-14 08:44:19 +02:00
parent 274d4fc0db
commit de4a5bba27
5 changed files with 59 additions and 29 deletions

View File

@@ -45,6 +45,7 @@
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames, bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames,
const std::vector<std::string>& h5FileNames, const std::vector<std::string>& h5FileNames,
bool createHdfIfNotPresent,
int threadCount ) int threadCount )
{ {
if ( smspecFileNames.empty() ) return true; if ( smspecFileNames.empty() ) return true;
@@ -61,7 +62,7 @@ bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::ve
const auto& smspecFileName = smspecFileNames[cIdx]; const auto& smspecFileName = smspecFileNames[cIdx];
const auto& h5FileName = h5FileNames[cIdx]; const auto& h5FileName = h5FileNames[cIdx];
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( smspecFileName, h5FileName, hdfFilesCreatedCount ); RifHdf5SummaryExporter::ensureHdf5FileIsCreated( smspecFileName, h5FileName, createHdfIfNotPresent, hdfFilesCreatedCount );
} }
if ( hdfFilesCreatedCount > 0 ) if ( hdfFilesCreatedCount > 0 )
@@ -78,30 +79,48 @@ bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::ve
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
bool RifHdf5SummaryExporter::ensureHdf5FileIsCreated( const std::string& smspecFileName, const std::string& h5FileName, size_t& hdfFilesCreatedCount ) bool RifHdf5SummaryExporter::ensureHdf5FileIsCreated( const std::string& smspecFileName,
const std::string& h5FileName,
bool createHdfIfNotPresent,
size_t& hdfFilesCreatedCount )
{ {
// 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
if ( !std::filesystem::exists( smspecFileName ) ) return false; if ( !std::filesystem::exists( smspecFileName ) ) return false;
{
// Check if we have write permission in the folder
QFileInfo info( QString::fromStdString( smspecFileName ) );
if ( !info.isWritable() ) return true;
}
bool exportIsRequired = false; bool exportIsRequired = false;
{
bool h5FileExists = std::filesystem::exists( h5FileName ); bool h5FileExists = std::filesystem::exists( h5FileName );
if ( !h5FileExists ) if ( !h5FileExists )
{ {
exportIsRequired = true; if ( createHdfIfNotPresent )
}
else if ( RiaFilePathTools::isFirstOlderThanSecond( h5FileName, smspecFileName ) )
{ {
exportIsRequired = true; exportIsRequired = true;
} }
} }
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() )
{
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;
}
exportIsRequired = true;
}
if ( exportIsRequired ) if ( exportIsRequired )
{ {
@@ -127,7 +146,7 @@ bool RifHdf5SummaryExporter::ensureHdf5FileIsCreated( const std::string& smspecF
} }
catch ( std::exception& e ) catch ( std::exception& e )
{ {
QString txt = QString( "HDF export to file %1 failed : %3" ).arg( QString::fromStdString( smspecFileName ), e.what() ); QString txt = QString( "HDF export to file %1 failed : %2" ).arg( QString::fromStdString( smspecFileName ), e.what() );
RiaLogging::error( txt ); RiaLogging::error( txt );

View File

@@ -42,9 +42,13 @@ class RifHdf5SummaryExporter
public: public:
static bool ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames, static bool ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames,
const std::vector<std::string>& h5FileNames, const std::vector<std::string>& h5FileNames,
bool createHdfIfNotPresent,
int threadCount ); int threadCount );
static bool ensureHdf5FileIsCreated( const std::string& smspecFileName, const std::string& h5FileName, size_t& hdfFilesCreatedCount ); static bool ensureHdf5FileIsCreated( const std::string& smspecFileName,
const std::string& h5FileName,
bool createHdfIfNotPresent,
size_t& hdfFilesCreatedCount );
private: private:
static bool writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData ); static bool writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData );

View File

@@ -95,10 +95,11 @@ bool RifReaderEclipseSummary::open( const QString& headerFileName, RiaThreadSafe
( h5FileFound || ( prefSummary->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON ) ) ) ( h5FileFound || ( prefSummary->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON ) ) )
{ {
#ifdef USE_HDF5 #ifdef USE_HDF5
if ( prefSummary->createH5SummaryDataFiles() )
{
size_t createdH5FileCount = 0; size_t createdH5FileCount = 0;
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( headerFileName.toStdString(), h5FileName.toStdString(), createdH5FileCount ); RifHdf5SummaryExporter::ensureHdf5FileIsCreated( headerFileName.toStdString(),
h5FileName.toStdString(),
prefSummary->createH5SummaryDataFiles(),
createdH5FileCount );
if ( createdH5FileCount > 0 ) if ( createdH5FileCount > 0 )
{ {
@@ -106,7 +107,6 @@ bool RifReaderEclipseSummary::open( const QString& headerFileName, RiaThreadSafe
if ( threadSafeLogger ) threadSafeLogger->info( txt ); if ( threadSafeLogger ) threadSafeLogger->info( txt );
} }
h5FileFound = QFile::exists( h5FileName ); h5FileFound = QFile::exists( h5FileName );
}
if ( h5FileFound ) if ( h5FileFound )
{ {

View File

@@ -411,7 +411,7 @@ void RimSummaryCaseMainCollection::loadFileSummaryCaseData( std::vector<RimFileS
// If h5 mode, check all summary files and create or recreate h5 files if required // If h5 mode, check all summary files and create or recreate h5 files if required
#ifdef USE_HDF5 #ifdef USE_HDF5
{ {
if ( prefs->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON && prefs->createH5SummaryDataFiles() ) if ( prefs->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON )
{ {
int threadCount = 1; int threadCount = 1;
#ifdef USE_OPENMP #ifdef USE_OPENMP
@@ -433,7 +433,10 @@ void RimSummaryCaseMainCollection::loadFileSummaryCaseData( std::vector<RimFileS
h5FileNames.push_back( h5FilenameCandidate.toStdString() ); h5FileNames.push_back( h5FilenameCandidate.toStdString() );
} }
RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( headerFileNames, h5FileNames, threadCount ); RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( headerFileNames,
h5FileNames,
prefs->createH5SummaryDataFiles(),
threadCount );
} }
} }
#endif #endif

View File

@@ -36,7 +36,11 @@ TEST( DISABLED_HDFTests, WriteToHdf5SummaryExporter )
std::string exportFileName = "e:/project/scratch_export/hdf_complete.h5"; std::string exportFileName = "e:/project/scratch_export/hdf_complete.h5";
int threadCount = 1; int threadCount = 1;
RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( { file_path.toStdString() }, { exportFileName }, threadCount ); bool createEnhancedSummaryFiles = true;
RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( { file_path.toStdString() },
{ exportFileName },
createEnhancedSummaryFiles,
threadCount );
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------