create_extensible_dataset.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 #include <highfive/H5DataSet.hpp>
14 #include <highfive/H5DataSpace.hpp>
15 #include <highfive/H5File.hpp>
16 
17 const std::string FILE_NAME("create_extensible_dataset_example.h5");
18 const std::string DATASET_NAME("dset");
19 
20 // Create a dataset name "dset" of double 4x6
21 //
22 int main(void) {
23  using namespace HighFive;
24  try {
25  // Create a new file using the default property lists.
27 
28  // Create a dataspace with initial shape and max shape
29  DataSpace dataspace = DataSpace({4, 5}, {17, DataSpace::UNLIMITED});
30 
31  // Use chunking
32  DataSetCreateProps props;
33  props.add(Chunking(std::vector<hsize_t>{2, 2}));
34 
35  // Create the dataset
36  DataSet dataset = file.createDataSet(DATASET_NAME, dataspace,
37  AtomicType<double>(), props);
38 
39  // Write into the initial part of the dataset
40  double t1[3][1] = {{2.0}, {2.0}, {4.0}};
41  dataset.select({0, 0}, {3, 1}).write(t1);
42 
43  // Resize the dataset to a larger size
44  dataset.resize({4, 6});
45 
46  // Write into the new part of the dataset
47  double t2[1][3] = {{4.0, 8.0, 6.0}};
48  dataset.select({3, 3}, {1, 3}).write(t2);
49 
50  // now we read it back
51  std::vector<std::vector<double>> result;
52  dataset.read(result);
53 
54  // we print it out and see:
55  // 2 0 0 0 0 0
56  // 2 0 0 0 0 0
57  // 4 0 0 0 0 0
58  // 0 0 0 4 8 6
59  for (auto row : result) {
60  for (auto col : row)
61  std::cout << " " << col;
62  std::cout << std::endl;
63  }
64 
65  } catch (const Exception& err) {
66  // catch and print any HDF5 error
67  std::cerr << err.what() << std::endl;
68  }
69 
70  return 0; // successfully terminated
71 }
DATASET_NAME
const std::string DATASET_NAME("dset")
HighFive::DataSpace::UNLIMITED
static const size_t UNLIMITED
Definition: H5DataSpace.hpp:33
H5File.hpp
HighFive::SliceTraits::read
void read(T &array) const
Definition: H5Slice_traits_misc.hpp:149
H5DataSet.hpp
H5DataSpace.hpp
HighFive::SliceTraits::select
Selection select(const std::vector< size_t > &offset, const std::vector< size_t > &count, const std::vector< size_t > &stride=std::vector< size_t >()) const
Definition: H5Slice_traits_misc.hpp:66
HighFive::DataSetCreateProps
Definition: H5PropertyList.hpp:67
HighFive::File::Create
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40
HighFive::Chunking
Definition: H5PropertyList.hpp:77
HighFive::Properties::add
void add(const Property &property)
Definition: H5PropertyList_misc.hpp:51
HighFive::File
File class.
Definition: H5File.hpp:25
HighFive::AtomicType
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
main
int main(void)
Definition: create_extensible_dataset.cpp:22
file
FILE * file
Definition: arithmeticencoder.cpp:77
HighFive::DataSet::resize
void resize(const std::vector< size_t > &dims)
Change the size of the dataset.
Definition: H5DataSet_misc.hpp:66
HighFive::DataSpace
Definition: H5DataSpace.hpp:30
HighFive::Exception::what
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
HighFive::Exception
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
HighFive::File::ReadWrite
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
FILE_NAME
const std::string FILE_NAME("create_extensible_dataset_example.h5")
HighFive::DataSet
Definition: H5DataSet.hpp:27
HighFive::File::Truncate
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
HighFive
Definition: H5Annotate_traits.hpp:14


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23