Hdf5Util.cpp
Go to the documentation of this file.
2 
3 namespace lvr2
4 {
5 
6 namespace hdf5util
7 {
8 
9 std::vector<std::string> splitGroupNames(const std::string& groupName)
10 {
11  std::vector<std::string> ret;
12 
13  std::string remainder = groupName;
14  size_t delimiter_pos = 0;
15 
16  while ((delimiter_pos = remainder.find('/', delimiter_pos)) != std::string::npos)
17  {
18  if (delimiter_pos > 0)
19  {
20  ret.push_back(remainder.substr(0, delimiter_pos));
21  }
22 
23  remainder = remainder.substr(delimiter_pos + 1);
24 
25  delimiter_pos = 0;
26  }
27 
28  if (remainder.size() > 0)
29  {
30  ret.push_back(remainder);
31  }
32 
33  return ret;
34 }
35 
36 void writeBaseStructure(std::shared_ptr<HighFive::File> hdf5_file)
37 {
38  int version = 1;
39  hdf5_file->createDataSet<int>("version", HighFive::DataSpace::From(version)).write(version);
40  HighFive::Group raw_data_group = hdf5_file->createGroup("raw");
41 
42  // Create string with current time
43  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
44  std::time_t t_now = std::chrono::system_clock::to_time_t(now);
45  std::string time(ctime(&t_now));
46 
47  // Add current time to raw data group
48  raw_data_group.createDataSet<std::string>("created", HighFive::DataSpace::From(time))
49  .write(time);
50  raw_data_group.createDataSet<std::string>("changed", HighFive::DataSpace::From(time))
51  .write(time);
52 
53  // Create empty reference frame
54  std::vector<float> frame = Matrix4<BaseVector<float>>().getVector();
55  raw_data_group.createDataSet<float>("position", HighFive::DataSpace::From(frame)).write(frame);
56 }
57 
58 HighFive::Group getGroup(std::shared_ptr<HighFive::File> hdf5_file,
59  const std::string& groupName,
60  bool create)
61 {
62  std::vector<std::string> groupNames = hdf5util::splitGroupNames(groupName);
63  HighFive::Group cur_grp;
64 
65  try
66  {
67  cur_grp = hdf5_file->getGroup("/");
68 
69  for (size_t i = 0; i < groupNames.size(); i++)
70  {
71  if (cur_grp.exist(groupNames[i]))
72  {
73  cur_grp = cur_grp.getGroup(groupNames[i]);
74  }
75  else if (create)
76  {
77  cur_grp = cur_grp.createGroup(groupNames[i]);
78  }
79  else
80  {
81  // Throw exception because a group we searched
82  // for doesn't exist and create flag was false
83  throw std::runtime_error("HDF5IO - getGroup(): Groupname '" + groupNames[i] +
84  "' doesn't exist and create flag is false");
85  }
86  }
87  }
88  catch (HighFive::Exception& e)
89  {
90  std::cout << "Error in getGroup (with group name '" << groupName << "': " << std::endl;
91  std::cout << e.what() << std::endl;
92  throw e;
93  }
94 
95  return cur_grp;
96 }
97 
98 HighFive::Group getGroup(HighFive::Group& g, const std::string& groupName, bool create)
99 {
100  std::vector<std::string> groupNames = hdf5util::splitGroupNames(groupName);
101  HighFive::Group cur_grp = g;
102 
103  try
104  {
105 
106  for (size_t i = 0; i < groupNames.size(); i++)
107  {
108 
109  if (cur_grp.exist(groupNames[i]))
110  {
111  cur_grp = cur_grp.getGroup(groupNames[i]);
112  }
113  else if (create)
114  {
115  cur_grp = cur_grp.createGroup(groupNames[i]);
116  }
117  else
118  {
119  // Throw exception because a group we searched
120  // for doesn't exist and create flag was false
121  throw std::runtime_error("HDF5IO - getGroup(): Groupname '" + groupNames[i] +
122  "' doesn't exist and create flag is false");
123  }
124  }
125  }
126  catch (HighFive::Exception& e)
127  {
128  std::cout << timestamp << "Error in HDF5Util::getGroup '" << groupName << "': " << std::endl;
129  std::cout << timestamp << e.what() << std::endl;
130  throw e;
131  }
132 
133  return cur_grp;
134 }
135 
136 bool exist(std::shared_ptr<HighFive::File> hdf5_file, const std::string& groupName)
137 {
138  std::vector<std::string> groupNames = hdf5util::splitGroupNames(groupName);
139  HighFive::Group cur_grp;
140 
141  try
142  {
143  cur_grp = hdf5_file->getGroup("/");
144 
145  for (size_t i = 0; i < groupNames.size(); i++)
146  {
147  if (cur_grp.exist(groupNames[i]))
148  {
149  if (i < groupNames.size() - 1)
150  {
151  cur_grp = cur_grp.getGroup(groupNames[i]);
152  }
153  }
154  else
155  {
156  return false;
157  }
158  }
159  }
160  catch (HighFive::Exception& e)
161  {
162  std::cout << "Error in exist (with group name '" << groupName << "': " << std::endl;
163  std::cout << e.what() << std::endl;
164  throw e;
165  }
166 
167  return true;
168 }
169 
170 bool exist(HighFive::Group& group, const std::string& groupName)
171 {
172  std::vector<std::string> groupNames = hdf5util::splitGroupNames(groupName);
173  HighFive::Group cur_grp = group;
174 
175  try
176  {
177  for (size_t i = 0; i < groupNames.size(); i++)
178  {
179  if (cur_grp.exist(groupNames[i]))
180  {
181  if (i < groupNames.size() - 1)
182  {
183  cur_grp = cur_grp.getGroup(groupNames[i]);
184  }
185  }
186  else
187  {
188  return false;
189  }
190  }
191  }
192  catch (HighFive::Exception& e)
193  {
194  std::cout << "Error in exist (with group name '" << groupName << "': " << std::endl;
195  std::cout << e.what() << std::endl;
196  throw e;
197  }
198 
199  return true;
200 }
201 
202 std::shared_ptr<HighFive::File> open(const std::string& filename)
203 {
204  std::shared_ptr<HighFive::File> hdf5_file;
205  boost::filesystem::path path(filename);
206 
207  if (!boost::filesystem::exists(path))
208  {
209  hdf5_file.reset(
211  hdf5util::writeBaseStructure(hdf5_file);
212  }
213  else
214  {
215  hdf5_file.reset(new HighFive::File(filename, HighFive::File::ReadWrite));
216  }
217 
218  return hdf5_file;
219 }
220 
221 } // namespace hdf5util
222 
223 } // namespace lvr2
A 4x4 matrix class implementation for use with the provided vertex types.
Definition: Matrix4.hpp:64
static DataSpace From(const ScalarValue &scalar_value)
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
File class.
Definition: H5File.hpp:25
bool exist(const std::string &node_name) const
check a dataset or group exists in the current node / group
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
Group getGroup(const std::string &group_name) const
open an existing group with the name group_name
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
void writeBaseStructure(std::shared_ptr< HighFive::File > hdf5_file)
Definition: Hdf5Util.cpp:36
std::shared_ptr< HighFive::File > open(const std::string &filename)
Definition: Hdf5Util.cpp:202
Group createGroup(const std::string &group_name)
create a new group with the name group_name
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 ...
HighFive::Group getGroup(std::shared_ptr< HighFive::File > hdf5_file, const std::string &groupName, bool create=true)
Definition: Hdf5Util.cpp:58
bool exist(std::shared_ptr< HighFive::File > hdf5_file, const std::string &groupName)
Definition: Hdf5Util.cpp:136
std::vector< std::string > splitGroupNames(const std::string &groupName)
Definition: Hdf5Util.cpp:9
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