Merge pull request #3202 from tskille/fix_esmry_rename

Cleanup when ESMRY renamle fails
This commit is contained in:
Bård Skaflestad 2022-11-05 14:17:15 +01:00 committed by GitHub
commit 5dc4f7301f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View File

@ -63,6 +63,7 @@ private:
std::array<int, 3> ijk_from_global_index(const GridDims& dims, int globInd) const;
std::vector<std::string> make_modified_keys(const std::vector<std::string>& valueKeys, const GridDims& dims);
bool rename_tmpfile(const std::string& tmp_fname);
};

View File

@ -20,6 +20,7 @@
#include <opm/io/eclipse/EclFile.hpp>
#include <opm/io/eclipse/ExtSmryOutput.hpp>
#include <opm/io/eclipse/EclOutput.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
@ -29,7 +30,6 @@
#include <string>
#include <filesystem>
namespace Opm { namespace EclIO {
@ -99,8 +99,12 @@ void ExtSmryOutput::write(const std::vector<float>& ts_data, int report_step, bo
{
const auto tp = std::chrono::system_clock::now();
auto sec_since_epoch = std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
std::string tmp_file_name = "TMP_" + std::to_string(sec_since_epoch) + ".ESMRY";
std::filesystem::path esmry_file(m_outputFileName);
std::filesystem::path rootName = esmry_file.parent_path() / esmry_file.stem();
std::string tmp_file_name = rootName.string() + "_TMP_" + std::to_string(sec_since_epoch) + ".ESMRY";
{
Opm::EclIO::EclOutput outFile(tmp_file_name, m_fmt, std::ios::out);
@ -123,16 +127,31 @@ void ExtSmryOutput::write(const std::vector<float>& ts_data, int report_step, bo
}
}
const std::filesystem::path from_file = tmp_file_name;
const std::filesystem::path to_file = m_outputFileName;
std::filesystem::rename(from_file, to_file);
m_last_write = std::chrono::system_clock::now();
if (rename_tmpfile(tmp_file_name)){
m_last_write = std::chrono::system_clock::now();
} else {
Opm::OpmLog::warning("Not able to rename temporary ESMRY file " + tmp_file_name);
std::filesystem::path tmp_file(tmp_file_name);
std::filesystem::remove(tmp_file);
}
}
m_nTimeSteps++;
}
bool ExtSmryOutput::rename_tmpfile(const std::string& tmp_fname)
{
try {
std::filesystem::path from_file(tmp_fname);
std::filesystem::path to_file(m_outputFileName);
std::filesystem::rename(from_file, to_file);
} catch (...){
return false;
}
return true;
}
std::vector<std::string> ExtSmryOutput::make_modified_keys(const std::vector<std::string>& valueKeys, const GridDims& dims)
{