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,
const std::vector<std::string>& h5FileNames,
bool createHdfIfNotPresent,
int threadCount )
{
if ( smspecFileNames.empty() ) return true;
@ -61,7 +62,7 @@ bool RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( const std::ve
const auto& smspecFileName = smspecFileNames[cIdx];
const auto& h5FileName = h5FileNames[cIdx];
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( smspecFileName, h5FileName, hdfFilesCreatedCount );
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( smspecFileName, h5FileName, createHdfIfNotPresent, hdfFilesCreatedCount );
}
if ( hdfFilesCreatedCount > 0 )
@ -78,29 +79,47 @@ 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;
{
// Check if we have write permission in the folder
QFileInfo info( QString::fromStdString( smspecFileName ) );
if ( !info.isWritable() ) return true;
}
bool exportIsRequired = false;
bool h5FileExists = std::filesystem::exists( h5FileName );
if ( !h5FileExists )
{
bool h5FileExists = std::filesystem::exists( h5FileName );
if ( !h5FileExists )
if ( createHdfIfNotPresent )
{
exportIsRequired = true;
}
else if ( RiaFilePathTools::isFirstOlderThanSecond( h5FileName, smspecFileName ) )
}
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() )
{
exportIsRequired = true;
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 )
@ -127,7 +146,7 @@ bool RifHdf5SummaryExporter::ensureHdf5FileIsCreated( const std::string& smspecF
}
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 );

View File

@ -42,9 +42,13 @@ class RifHdf5SummaryExporter
public:
static bool ensureHdf5FileIsCreatedMultithreaded( const std::vector<std::string>& smspecFileNames,
const std::vector<std::string>& h5FileNames,
bool createHdfIfNotPresent,
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:
static bool writeGeneralSection( RifHdf5Exporter& exporter, Opm::EclIO::ESmry& sourceSummaryData );

View File

@ -95,18 +95,18 @@ bool RifReaderEclipseSummary::open( const QString& headerFileName, RiaThreadSafe
( h5FileFound || ( prefSummary->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON ) ) )
{
#ifdef USE_HDF5
if ( prefSummary->createH5SummaryDataFiles() )
{
size_t createdH5FileCount = 0;
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( headerFileName.toStdString(), h5FileName.toStdString(), createdH5FileCount );
size_t createdH5FileCount = 0;
RifHdf5SummaryExporter::ensureHdf5FileIsCreated( headerFileName.toStdString(),
h5FileName.toStdString(),
prefSummary->createH5SummaryDataFiles(),
createdH5FileCount );
if ( createdH5FileCount > 0 )
{
QString txt = QString( "Created %1 " ).arg( h5FileName );
if ( threadSafeLogger ) threadSafeLogger->info( txt );
}
h5FileFound = QFile::exists( h5FileName );
if ( createdH5FileCount > 0 )
{
QString txt = QString( "Created %1 " ).arg( h5FileName );
if ( threadSafeLogger ) threadSafeLogger->info( txt );
}
h5FileFound = QFile::exists( h5FileName );
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
#ifdef USE_HDF5
{
if ( prefs->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON && prefs->createH5SummaryDataFiles() )
if ( prefs->summaryDataReader() == RiaPreferencesSummary::SummaryReaderMode::HDF5_OPM_COMMON )
{
int threadCount = 1;
#ifdef USE_OPENMP
@ -433,7 +433,10 @@ void RimSummaryCaseMainCollection::loadFileSummaryCaseData( std::vector<RimFileS
h5FileNames.push_back( h5FilenameCandidate.toStdString() );
}
RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( headerFileNames, h5FileNames, threadCount );
RifHdf5SummaryExporter::ensureHdf5FileIsCreatedMultithreaded( headerFileNames,
h5FileNames,
prefs->createH5SummaryDataFiles(),
threadCount );
}
}
#endif

View File

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