isce3  0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
IH5.h
1 #pragma once
2 
3 #include <H5Cpp.h>
4 #include <complex>
5 #include <iostream>
6 #include <isce3/core/Constants.h>
7 #include <isce3/except/Error.h>
8 #include <regex>
9 #include <string>
10 #include <type_traits>
11 #include <typeindex>
12 #include <valarray>
13 #include <array>
14 #include <vector>
15 
16 namespace isce3 {
17 namespace io {
18 
19 // Dataset chunking size
20 const hsize_t chunkSizeX = 128;
21 const hsize_t chunkSizeY = 128;
22 
23 // String length (fixed-length string by default in file)
24 const int STRLENGTH = 50;
25 
26 // Specific isce data type for HDF5
27 // May be stored elsewhere eventually
28 typedef struct float16 {
29 } float16;
30 typedef struct n1bit {
31 } n1bit;
32 typedef struct n2bit {
33 } n2bit;
34 
35 // Parameters containers for HDF5 searching capability
36 struct findMeta {
37  std::vector<std::string> outList;
38  std::string searchStr;
39  std::string searchType;
40  std::string basePath;
41 };
42 
44 class IDataSet : public H5::DataSet {
45 
46 public:
47  IDataSet() : H5::DataSet() {};
48  IDataSet(const H5::DataSet& dset) : H5::DataSet(dset) {};
49  IDataSet(const hid_t id) : H5::DataSet(id) {};
50 
51  // Metadata query
52 
54  std::vector<std::string> getAttrs();
55 
58  H5::DataSpace getDataSpace(const std::string& v = "");
59 
61  int getRank(const std::string& v = "");
62 
65  int getNumElements(const std::string& v = "");
66 
68  std::vector<int> getDimensions(const std::string& v = "");
69 
71  std::string getTypeClassStr(const std::string& v = "");
72 
74  std::vector<int> getChunkSize();
75 
77  int getNumBits(const std::string& v = "");
78 
80  std::string toGDAL() const;
81 
82  // Dataset reading queries
83 
85  template<typename T> inline void read(T& v, const std::string& att = "");
86 
88  void read(std::string& v, const std::string& att = "");
89 
91  template<typename T> inline void read(T* buf, const std::string& att);
92 
94  template<typename T>
95  inline void read(std::vector<T>& buf, const std::string& att);
96 
98  template<typename T>
99  inline void read(std::valarray<T>& buf, const std::string& att);
100 
101  // Function to read a dataset from file to memory variable. The input
102  // parameter is a raw point/vector/valarray that will store the (partial)
103  // multi-dimensional dataset. Input vector/valarray is initialized by
104  // caller, but resized (if needed) by the function. Raw pointer have to be
105  // allocated by caller. Partial extraction of data is possible using
106  // startIn, countIn, and strideIn. startIn: Array containing the position of
107  // the first element to read in all
108  // dimension. SizeIn size must be equal to the number of dimension
109  // of the dataset. If nullptr, startIn values are 0.
110  // countIn: Array containing the number of elements to read in all
111  // dimension.
112  // countIn size must be equal to the number of dimension of the
113  // dataset.
114  // strideIn:Array containing the stride between elements to read in all
115  // dimension.
116  // strideIn size must be equal to the number of dimension of the
117  // dataset. If nullptr, stridIn values are 1.
118  //
119  // Examples:
120  // - Dataset contains a 3-bands raster. Dimensions are (100,100,3).
121  // To retrieve the full second band:
122  // startIn=[0,0,1], countIn=[100,100,1], strideIn=nullptr or [1,1,1]
123  // To retrieve the first band, but only every other elements in X direction:
124  // startIn=[0,0,0], countIn=[50,100,1], strideIn=[2,1,1]
125 
127  template<typename T>
128  inline void read(T* buf, const int* startIn = nullptr,
129  const int* countIn = nullptr,
130  const int* strideIn = nullptr);
131 
134  template<typename T>
135  inline void read(T* buf, const std::vector<std::slice>* slicesIn);
136 
139  template<typename T> inline void read(T* buf, const std::gslice* gsliceIn);
140 
142  template<typename T> inline void read(std::vector<T>& buf);
143 
145  template<typename T>
146  inline void read(std::vector<T>& buf, const std::vector<int>* startIn,
147  const std::vector<int>* countIn,
148  const std::vector<int>* strideIn);
149 
151  template<typename T>
152  inline void read(std::vector<T>& buf,
153  const std::vector<std::slice>* slicesIn);
154 
157  template<typename T>
158  inline void read(std::vector<T>& buf, const std::gslice* gsliceIn);
159 
161  template<typename T> inline void read(std::valarray<T>& buf);
162 
164  template<typename T>
165  inline void read(std::valarray<T>& buf, const std::valarray<int>* startIn,
166  const std::valarray<int>* countIn,
167  const std::valarray<int>* strideIn);
168 
171  template<typename T>
172  inline void read(std::valarray<T>& buf,
173  const std::vector<std::slice>* slicesIn);
174 
177  template<typename T>
178  inline void read(std::valarray<T>& buf, const std::gslice* gsliceIn);
179 
180  // Dataset writing queries
181 
183  template<typename T> inline void write(const std::vector<T>& buf);
184 
187  template<typename T, size_t S>
188  inline void write(const std::vector<T>& buf,
189  const std::array<int, S>& startIn,
190  const std::array<int, S>& countIn,
191  const std::array<int, S>& strideIn);
192 
195  template<typename T>
196  inline void write(const std::vector<T>& buf,
197  const std::vector<std::slice>* slicesIn);
198 
201  template<typename T>
202  inline void write(const std::vector<T>& buf, const std::gslice* gsliceIn);
203 
205  template<typename T> inline void write(const std::valarray<T>& buf);
206 
209  template<typename T, size_t S>
210  inline void write(const std::valarray<T>& buf,
211  const std::array<int, S>& startIn,
212  const std::array<int, S>& countIn,
213  const std::array<int, S>& strideIn);
214 
217  template<typename T>
218  inline void write(const std::valarray<T>& buf,
219  const std::vector<std::slice>* slicesIn);
220 
223  template<typename T>
224  inline void write(const std::valarray<T>& buf, const std::gslice* gsliceIn);
225 
227  template<typename T> inline void write(const T* buf, const size_t sz);
228 
231  template<typename T, size_t S>
232  inline void write(const T* buf, const std::array<int, S>& startIn,
233  const std::array<int, S>& countIn,
234  const std::array<int, S>& strideIn);
235 
238  template<typename T>
239  inline void write(const T* buf, const std::vector<std::slice>* slicesIn);
240 
243  template<typename T>
244  inline void write(const T* buf, const std::gslice* gsliceIn);
245 
247  template<typename T>
248  inline void createAttribute(const std::string& name, const T& data);
249 
251  template<typename T>
252  inline void createAttribute(const std::string& name,
253  const std::vector<T>& values);
254 
256  template<typename T>
257  inline void createAttribute(const std::string& name,
258  const std::valarray<T>& values);
259 
262  template<typename T, typename T2, size_t S>
263  inline void createAttribute(const std::string& name,
264  const std::array<T2, S>& dims,
265  const std::vector<T>& values);
266 
269  template<typename T, typename T2, size_t S>
270  inline void createAttribute(const std::string& name,
271  const std::array<T2, S>& dims,
272  const std::valarray<T>& values);
273 
276  template<typename T, typename T2, size_t S>
277  inline void createAttribute(const std::string& name,
278  const std::array<T2, S>& dims, const T* buffer);
279 
282  H5::DataSpace getDataSpace(const int* startIn, const int* countIn,
283  const int* strideIn);
284 
286  H5::DataSpace getDataSpace(const std::vector<std::slice>* sliceIn);
287 
289  H5::DataSpace getDataSpace(const std::gslice* gsliceIn);
290 
292  H5::DataSpace getDataSpace(const size_t xidx, const size_t yidx,
293  const size_t iowidth, const size_t iolength,
294  const size_t band);
295 
296 private:
297  template<typename T> void read(T* buffer, const H5::DataSpace& dspace);
298 
299  void read(std::string* buffer, const H5::DataSpace& dspace);
300  void read(std::string* buf, const std::string& att);
301 
302  template<typename T>
303  void createAttribute(const std::string& name, const H5::DataType& datatype,
304  const H5::DataSpace& dataspace, const T* buffer);
305 
306  template<typename T>
307  void write(const T* buf, const H5::DataSpace& filespace);
308 };
309 
310 // Specialized instantiations
311 template<>
312 void IDataSet::write(const std::string* buf, const H5::DataSpace& dspace);
313 template<>
314 void IDataSet::createAttribute(const std::string& name,
315  const H5::DataType& datatype,
316  const H5::DataSpace& dataspace,
317  const std::string* buffer);
318 
319 class IGroup : public H5::Group {
320 
321 public:
322  IGroup() : H5::Group() {};
323  IGroup(const H5::Group& group) : H5::Group(group) {};
324  IGroup(hid_t group) : H5::Group(group) {};
325 
326  std::vector<std::string> getAttrs();
327 
329  std::vector<std::string> find(const std::string name,
330  const std::string start = ".",
331  const std::string type = "BOTH",
332  const std::string path = "FULL");
333 
335  std::string getPathname();
336 
338  H5::DataSpace getDataSpace(const std::string& name);
339 
341  int getNumElements(const std::string& name);
342 
344  template<typename T> inline void read(T& v, const std::string& att);
345 
347  void read(std::string& v, const std::string& att);
348 
350  template<typename T> inline void read(T* buf, const std::string& att);
351 
353  void read(std::string* buf, const std::string& att);
354 
356  template<typename T>
357  inline void read(std::vector<T>& buf, const std::string& att);
358 
360  template<typename T>
361  inline void read(std::valarray<T>& buf, const std::string& att);
362 
364  IDataSet openDataSet(const H5std_string& name);
365 
367  IGroup openGroup(const H5std_string& name);
368 
370  IGroup createGroup(const H5std_string& name);
371 
373  IDataSet createDataSet(const std::string& name, const std::string& data);
374 
376  template<typename T>
377  inline IDataSet createDataSet(const std::string& name, const T& data);
378 
380  template<typename T>
381  inline IDataSet createDataSet(const std::string& name,
382  const std::vector<T>& data);
383 
385  template<typename T>
386  inline IDataSet createDataSet(const std::string& name,
387  const std::valarray<T>& data);
388 
390  template<typename T>
391  inline IDataSet createDataSet(const std::string& name, const T* buffer,
392  const size_t size);
393 
395  template<typename T, typename T2, size_t S>
396  inline IDataSet createDataSet(const std::string& name,
397  const std::vector<T>& data,
398  const std::array<T2, S>& dims);
399 
401  template<typename T, typename T2, size_t S>
402  inline IDataSet createDataSet(const std::string& name,
403  const std::valarray<T>& data,
404  const std::array<T2, S>& dims);
405 
407  template<typename T, typename T2, size_t S>
408  inline IDataSet createDataSet(const std::string& name, const T* data,
409  const std::array<T2, S>& dims);
410 
412  template<typename T, typename T2, size_t S>
413  IDataSet createDataSet(const std::string& name,
414  const std::array<T2, S>& dims, const int chunk = 0,
415  const int shuffle = 0, const int deflate = 0);
416 
418  template<typename T>
419  inline void createAttribute(const std::string& name, const T& data);
420 
422  template<typename T>
423  inline void createAttribute(const std::string& name,
424  const std::vector<T>& values);
425 
427  template<typename T>
428  inline void createAttribute(const std::string& name,
429  const std::valarray<T>& values);
430 
433  template<typename T, typename T2, size_t S>
434  inline void createAttribute(const std::string& name,
435  const std::array<T2, S>& dims,
436  const std::vector<T>& values);
437 
440  template<typename T, typename T2, size_t S>
441  inline void createAttribute(const std::string& name,
442  const std::array<T2, S>& dims,
443  const std::valarray<T>& values);
444 
447  template<typename T, typename T2, size_t S>
448  inline void createAttribute(const std::string& name,
449  const std::array<T2, S>& dims, const T* buffer);
450 
451 private:
452  template<typename T>
453  void createAttribute(const std::string& name, const H5::DataType& datatype,
454  const H5::DataSpace& dataspace, const T* buffer);
455 };
456 
457 template<>
458 void IGroup::createAttribute(const std::string& name,
459  const H5::DataType& datatype,
460  const H5::DataSpace& dataspace,
461  const std::string* buffer);
462 
463 class IH5File : public H5::H5File {
464 public:
465  IH5File() : H5::H5File() {};
466 
474  IH5File(const H5std_string& name, const char mode = 'r');
475 
476  void openFile(const H5std_string& name);
477 
479  IDataSet openDataSet(const H5std_string& name);
480 
482  IGroup openGroup(const H5std_string& name);
483 
485  std::vector<std::string> find(const std::string name,
486  const std::string start = "/",
487  const std::string type = "BOTH",
488  const std::string path = "FULL");
489 
491  inline std::string filename() const { return getFileName(); }
492 
494  IGroup createGroup(const H5std_string& name);
495 };
496 } // namespace io
497 } // namespace isce3
498 
499 // Get inline implementations for IH5
500 #define ISCE_IO_IH5_ICC
501 #include "IH5.icc"
502 #undef ISCE_IO_IH5_ICC
H5::DataSpace getDataSpace(const std::string &name)
Return the H5::DataSpace of the given attribute.
Definition: IH5.cpp:831
IGroup openGroup(const H5std_string &name)
Open a given group.
Definition: IH5.cpp:1021
std::vector< std::string > find(const std::string name, const std::string start=".", const std::string type="BOTH", const std::string path="FULL")
Search function for given name in the group.
Definition: IH5.cpp:804
std::vector< int > getDimensions(const std::string &v="")
Get the size of each dimension of the dataset or given attribute.
Definition: IH5.cpp:162
IDataSet openDataSet(const H5std_string &name)
Open a given dataset.
Definition: IH5.cpp:854
void createAttribute(const std::string &name, const T &data)
Creating and writing a scalar as an attribute.
Definition: IH5.icc:1238
void read(T &v, const std::string &att="")
Reading scalar (non string) dataset or attributes.
Definition: IH5.icc:130
int getNumElements(const std::string &v="")
Get the total number of elements contained in dataset or given attribute.
Definition: IH5.cpp:150
std::string getPathname()
Return the path of the group from the file root.
Definition: IH5.cpp:814
void createAttribute(const std::string &name, const T &data)
Creating and writing a scalar as an attribute.
Definition: IH5.icc:824
int getRank(const std::string &v="")
Get the number of dimension of dataset or given attribute.
Definition: IH5.cpp:140
Definition: IH5.h:463
IGroup createGroup(const H5std_string &name)
Create a group.
Definition: IH5.cpp:1029
std::vector< std::string > getAttrs()
Get a list of attributes attached to dataset.
Definition: IH5.cpp:242
Definition: IH5.h:36
std::string toGDAL() const
Generate GDALDataset Representation.
Definition: IH5.cpp:340
void write(const std::vector< T > &buf)
Writing std::vector data into a dataset.
Definition: IH5.icc:512
Our derived dataset structure that includes utility functions.
Definition: IH5.h:44
int getNumElements(const std::string &name)
Return the number of elements in the given attribute.
Definition: IH5.cpp:843
Definition: IH5.h:319
Definition: IH5.h:30
IGroup createGroup(const H5std_string &name)
Create a group within this group.
Definition: IH5.cpp:932
IDataSet openDataSet(const H5std_string &name)
Open a given dataset.
Definition: IH5.cpp:1013
std::string getTypeClassStr(const std::string &v="")
Get the H5 data type of the dataset or given attribute.
Definition: IH5.cpp:198
std::vector< int > getChunkSize()
Get the storage chunk size of the dataset.
Definition: IH5.cpp:266
std::vector< std::string > find(const std::string name, const std::string start="/", const std::string type="BOTH", const std::string path="FULL")
Searching for given name in file.
Definition: IH5.cpp:1065
H5::DataSpace getDataSpace(const std::string &v="")
Get a HDF5 DataSpace object corresponding to dataset or given attribute.
Definition: IH5.cpp:117
Definition: IH5.h:32
Definition: IH5.h:28
int getNumBits(const std::string &v="")
Get the number of bit used to store each dataset element.
Definition: IH5.cpp:305
void read(T &v, const std::string &att)
Reading scalar attribute given by name.
Definition: IH5.icc:852
IGroup openGroup(const H5std_string &name)
Open a given group.
Definition: IH5.cpp:862
IDataSet createDataSet(const std::string &name, const std::string &data)
Create a string scalar dataset and simultaneously write the data.
Definition: IH5.cpp:957
std::string filename() const
Get filename of HDF5 file.
Definition: IH5.h:491

Generated for ISCE3.0 by doxygen 1.8.5.