Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:55:35

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include "../H5Easy.hpp"
0012 
0013 namespace H5Easy {
0014 
0015 namespace detail {
0016 
0017 // Generate error-stream and return "Exception" (not yet thrown).
0018 inline Exception error(const File& file, const std::string& path, const std::string& message) {
0019     std::ostringstream ss;
0020     ss << message << std::endl
0021        << "Path: " << path << std::endl
0022        << "Filename: " << file.getName() << std::endl;
0023     return Exception(ss.str());
0024 }
0025 
0026 // Generate specific dump error
0027 inline Exception dump_error(File& file, const std::string& path) {
0028     if (file.getObjectType(path) == ObjectType::Dataset) {
0029         return error(file,
0030                      path,
0031                      "H5Easy: Dataset already exists, dump with H5Easy::DumpMode::Overwrite "
0032                      "to overwrite (with an array of the same shape).");
0033     } else {
0034         return error(
0035             file,
0036             path,
0037             "H5Easy: path exists, but does not correspond to a Dataset. Dump not possible.");
0038     }
0039 }
0040 
0041 // get a opened DataSet: nd-array
0042 template <class T>
0043 inline DataSet initDataset(File& file,
0044                            const std::string& path,
0045                            const std::vector<size_t>& shape,
0046                            const DumpOptions& options) {
0047     if (!file.exist(path)) {
0048         if (!options.compress() && !options.isChunked()) {
0049             return file.createDataSet<T>(path, DataSpace(shape), {}, {}, true);
0050         } else {
0051             std::vector<hsize_t> chunks(shape.begin(), shape.end());
0052             if (options.isChunked()) {
0053                 chunks = options.getChunkSize();
0054                 if (chunks.size() != shape.size()) {
0055                     throw error(file, path, "H5Easy::dump: Incorrect rank ChunkSize");
0056                 }
0057             }
0058             DataSetCreateProps props;
0059             props.add(Chunking(chunks));
0060             if (options.compress()) {
0061                 props.add(Shuffle());
0062                 props.add(Deflate(options.getCompressionLevel()));
0063             }
0064             return file.createDataSet<T>(path, DataSpace(shape), props, {}, true);
0065         }
0066     } else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
0067         DataSet dataset = file.getDataSet(path);
0068         if (dataset.getDimensions() != shape) {
0069             throw error(file, path, "H5Easy::dump: Inconsistent dimensions");
0070         }
0071         return dataset;
0072     }
0073     throw dump_error(file, path);
0074 }
0075 
0076 // get a opened DataSet: scalar
0077 template <class T>
0078 inline DataSet initScalarDataset(File& file,
0079                                  const std::string& path,
0080                                  const T& data,
0081                                  const DumpOptions& options) {
0082     if (!file.exist(path)) {
0083         return file.createDataSet<T>(path, DataSpace::From(data), {}, {}, true);
0084     } else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
0085         DataSet dataset = file.getDataSet(path);
0086         if (dataset.getElementCount() != 1) {
0087             throw error(file, path, "H5Easy::dump: Existing field not a scalar");
0088         }
0089         return dataset;
0090     }
0091     throw dump_error(file, path);
0092 }
0093 
0094 // get a opened Attribute: nd-array
0095 template <class T>
0096 inline Attribute initAttribute(File& file,
0097                                const std::string& path,
0098                                const std::string& key,
0099                                const std::vector<size_t>& shape,
0100                                const DumpOptions& options) {
0101     if (!file.exist(path)) {
0102         throw error(file, path, "H5Easy::dumpAttribute: DataSet does not exist");
0103     }
0104     if (file.getObjectType(path) != ObjectType::Dataset) {
0105         throw error(file, path, "H5Easy::dumpAttribute: path not a DataSet");
0106     }
0107     DataSet dataset = file.getDataSet(path);
0108     if (!dataset.hasAttribute(key)) {
0109         return dataset.createAttribute<T>(key, DataSpace(shape));
0110     } else if (options.overwrite()) {
0111         Attribute attribute = dataset.getAttribute(key);
0112         DataSpace dataspace = attribute.getSpace();
0113         if (dataspace.getDimensions() != shape) {
0114             throw error(file, path, "H5Easy::dumpAttribute: Inconsistent dimensions");
0115         }
0116         return attribute;
0117     }
0118     throw error(file,
0119                 path,
0120                 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
0121 }
0122 
0123 // get a opened Attribute: scalar
0124 template <class T>
0125 inline Attribute initScalarAttribute(File& file,
0126                                      const std::string& path,
0127                                      const std::string& key,
0128                                      const T& data,
0129                                      const DumpOptions& options) {
0130     if (!file.exist(path)) {
0131         throw error(file, path, "H5Easy::dumpAttribute: DataSet does not exist");
0132     }
0133     if (file.getObjectType(path) != ObjectType::Dataset) {
0134         throw error(file, path, "H5Easy::dumpAttribute: path not a DataSet");
0135     }
0136     DataSet dataset = file.getDataSet(path);
0137     if (!dataset.hasAttribute(key)) {
0138         return dataset.createAttribute<T>(key, DataSpace::From(data));
0139     } else if (options.overwrite()) {
0140         Attribute attribute = dataset.getAttribute(key);
0141         DataSpace dataspace = attribute.getSpace();
0142         if (dataspace.getElementCount() != 1) {
0143             throw error(file, path, "H5Easy::dumpAttribute: Existing field not a scalar");
0144         }
0145         return attribute;
0146     }
0147     throw error(file,
0148                 path,
0149                 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
0150 }
0151 
0152 }  // namespace detail
0153 }  // namespace H5Easy