From a7129c9090f417c067629bf5c3b9dbd764a34f9f Mon Sep 17 00:00:00 2001 From: hinder Date: Thu, 3 Mar 2011 15:42:47 +0000 Subject: Correct check for whether to truncate output HDF5 file Previously a "first_time" variable was used to determine if truncation should happen. When decomposing multiple variables, this logic is incorrect. We now store the first_time information per output file. git-svn-id: http://svn.einsteintoolkit.org/cactus/EinsteinAnalysis/Multipole/trunk@76 4f5cb9a8-4dd8-4c2d-9bbd-173fa4467843 --- src/utils.cc | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/utils.cc b/src/utils.cc index 7140718..74d9eef 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -152,6 +153,12 @@ void Multipole_OutputComplexToFile(CCTK_ARGUMENTS, const string &name, CCTK_REAL #ifdef HAVE_CAPABILITY_HDF5 +static bool file_exists(const string &name) +{ + struct stat sts; + return !(stat(name.c_str(), &sts) == -1 && errno == ENOENT); +} + bool dataset_exists(hid_t file, const string &dataset_name) { // To test whether a dataset exists, the recommended way in API 1.6 @@ -182,14 +189,22 @@ void Multipole_OutputComplexToH5File(CCTK_ARGUMENTS, const string &basename, con string output_name = out_dir + string("/") + basename; int status = 0; - static int first_time = true; + static map checked; // Has the given file been checked + // for truncation? map<*,bool> + // defaults to false - hid_t file = - (first_time && IO_TruncateOutputFiles(cctkGH)) ? - H5Fcreate(output_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT) : - H5Fopen(output_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + hid_t file; + + if (!file_exists(output_name) || (!checked[output_name] && IO_TruncateOutputFiles(cctkGH))) + { + file = H5Fcreate(output_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + } + else + { + file = H5Fopen(output_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + } - first_time = false; + checked[output_name] = true; hid_t dataset = -1; -- cgit v1.2.3