From 0f7bc841f0cfb3661f529fee6cc398b46055fff8 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Tue, 16 Jan 2018 15:55:56 +0100 Subject: [PATCH] Add format writer --- src/H5ChunkedWriter.cpp | 39 ++++++++++++++++++++++++++++++++++++++- src/H5ChunkedWriter.hpp | 4 +++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/H5ChunkedWriter.cpp b/src/H5ChunkedWriter.cpp index 0da29f5..86f386f 100644 --- a/src/H5ChunkedWriter.cpp +++ b/src/H5ChunkedWriter.cpp @@ -227,4 +227,41 @@ hsize_t HDF5ChunkedWriter::prepare_storage_for_frame(size_t frame_index, size_t* return relative_frame_index; } -void HDF5ChunkedWriter::write_format(h5_base& format_root, std::map& values) {} \ No newline at end of file +void HDF5ChunkedWriter::write_format_data(H5::CommonFG& file_node, h5_parent& format_node, std::map& values){ + auto node_group = h5_utils::create_group(file_node, format_node.name); + + for (auto item : format_node.items) { + + if (item->node_type == GROUP) { + auto sub_group = dynamic_cast(item); + + write_format_data(node_group, *sub_group, values); + } else if (item->node_type == ATTRIBUTE) { + auto sub_attribute = dynamic_cast(item); + + h5_utils::write_attribute(node_group, *sub_attribute, values); + } else if (item->node_type == DATASET) { + auto sub_dataset = dynamic_cast(item); + + auto current_dataset = h5_utils::write_dataset(node_group, *sub_dataset, values); + + for (auto dataset_attr : sub_dataset->items) { + // You can specify only attributes inside a dataset. + if (dataset_attr->node_type != ATTRIBUTE) { + stringstream error_message; + error_message << "Invalid element " << dataset_attr->name << " on dataset " << sub_dataset->name << ". Only attributes allowd."; + + throw invalid_argument( error_message.str() ); + } + + auto sub_attribute = dynamic_cast(item); + + h5_utils::write_attribute(current_dataset, *sub_attribute, values); + } + } + } +} + +void HDF5ChunkedWriter::write_format(h5_group& format_root, std::map& values) { + write_format_data(file, format_root, values); +} \ No newline at end of file diff --git a/src/H5ChunkedWriter.hpp b/src/H5ChunkedWriter.hpp index 516fce7..c856cba 100644 --- a/src/H5ChunkedWriter.hpp +++ b/src/H5ChunkedWriter.hpp @@ -31,12 +31,14 @@ class HDF5ChunkedWriter hsize_t prepare_storage_for_frame(size_t frame_index, size_t* frame_shape); void create_file(size_t* frame_shape, hsize_t frame_chunk=0); + void write_format_data(H5::CommonFG& file_node, h5_parent& format_node, std::map& values); + public: HDF5ChunkedWriter(const std::string filename, const std::string dataset_name, hsize_t frames_per_file=0, hsize_t initial_dataset_size=config::initial_dataset_size); ~HDF5ChunkedWriter(); void close_file(); void write_data(size_t frame_index, size_t* frame_shape, size_t data_bytes_size, char* data); - void write_format(h5_base& format_root, std::map& values); + void write_format(h5_group& format_root, std::map& values); }; #endif \ No newline at end of file