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 }
void read(T &array) const
void add(const Property &property)
void resize(const std::vector< size_t > &dims)
Change the size of the dataset.
const std::string DATASET_NAME("dset")
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
const std::string FILE_NAME("create_extensible_dataset_example.h5")
File class.
Definition: H5File.hpp:25
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
static const size_t UNLIMITED
Definition: H5DataSpace.hpp:33
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
FILE * file
int main(void)
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
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space ...
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40


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 Mon Feb 28 2022 22:46:06