From 886a3d2162845797041ff988f9c389ab0a9630f3 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 3 Mar 2021 11:49:21 +0100 Subject: [PATCH] working append mode for hdf5 --- src/trexio_hdf5.org | 64 +++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index 06e8df2..97d2b91 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -159,26 +159,59 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) { trexio_hdf5_t* f = (trexio_hdf5_t*) file; /* If file doesn't exist, create it */ - int f_ishere = 0; + int f_exists = 0; struct stat st; - if (stat(file->file_name, &st) == 0) { - printf("%s \n","HDF5 file already exists"); - // RDWR OR RDONLY ??? - f->file_id = H5Fopen(file->file_name, H5F_ACC_RDWR, H5P_DEFAULT); - f_ishere = 1; + if (stat(file->file_name, &st) == 0) f_exists = 1; + + if (f_exists == 1) { + + switch (file->mode) { + case 'r': + // reading the existing file -> open as RDONLY + f->file_id = H5Fopen(file->file_name, H5F_ACC_RDONLY, H5P_DEFAULT); + break; + case 'a': + // appending the existing file -> open as RDWR + f->file_id = H5Fopen(file->file_name, H5F_ACC_RDWR, H5P_DEFAULT); + break; + case 'w': + // writing the existing file -> overwrite it (_TRUNC) [_EXCL | H5F_ACC_DEBUG as an alternative] + f->file_id = H5Fcreate(file->file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + break; + } + } else { - f->file_id = H5Fcreate(file->file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); - f_ishere = 0; + + switch (file->mode) { + case 'r': + // reading non-existing file -> error + return TREXIO_FAILURE; + case 'a': + case 'w': + // appending or writing non-existing file -> create it + f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT); + break; + } + } - /* Create groups in the hdf5 file */ - if (f_ishere == 0){ - f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - //f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - } else { - f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); - //f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT); + /* Create or open groups in the hdf5 file assuming that they exist if file exists */ + switch (file->mode) { + case 'r': + case 'a': + if (f_exists == 1) { + f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); + //f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT); + } else { + f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + //f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + } + break; + case 'w': + f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + //f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + break; } assert (f->nucleus_group > 0L); //assert (f->electron_group > 0L); @@ -578,7 +611,6 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou } #+end_src -** TODO Read/Write the charge attribute * File suffixes :noxport: