isce3 0.25.0
Loading...
Searching...
No Matches
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
16namespace isce3 {
17namespace io {
18
19// Dataset chunking size
20const hsize_t chunkSizeX = 128;
21const hsize_t chunkSizeY = 128;
22
23// Specific isce data type for HDF5
24// May be stored elsewhere eventually
25typedef struct float16 {
26} float16;
27typedef struct n1bit {
28} n1bit;
29typedef struct n2bit {
30} n2bit;
31
32// Parameters containers for HDF5 searching capability
33struct findMeta {
34 std::vector<std::string> outList;
35 std::string searchStr;
36 std::string searchType;
37 std::string basePath;
38};
39
41class IDataSet : public H5::DataSet {
42
43public:
44 IDataSet() : H5::DataSet() {};
45 IDataSet(const H5::DataSet& dset) : H5::DataSet(dset) {};
46 IDataSet(const hid_t id) : H5::DataSet(id) {};
47
48 // Metadata query
49
51 std::vector<std::string> getAttrs();
52
55 H5::DataSpace getDataSpace(const std::string& v = "");
56
58 int getRank(const std::string& v = "");
59
62 int getNumElements(const std::string& v = "");
63
65 std::vector<int> getDimensions(const std::string& v = "");
66
68 std::string getTypeClassStr(const std::string& v = "");
69
71 std::vector<int> getChunkSize();
72
74 int getNumBits(const std::string& v = "");
75
77 std::string toGDAL() const;
78
79 // Dataset reading queries
80
82 template<typename T> inline void read(T& v, const std::string& att = "");
83
85 void read(std::string& v, const std::string& att = "");
86
88 template<typename T> inline void read(T* buf, const std::string& att);
89
91 template<typename T>
92 inline void read(std::vector<T>& buf, const std::string& att);
93
95 template<typename T>
96 inline void read(std::valarray<T>& buf, const std::string& att);
97
98 // Function to read a dataset from file to memory variable. The input
99 // parameter is a raw point/vector/valarray that will store the (partial)
100 // multi-dimensional dataset. Input vector/valarray is initialized by
101 // caller, but resized (if needed) by the function. Raw pointer have to be
102 // allocated by caller. Partial extraction of data is possible using
103 // startIn, countIn, and strideIn. startIn: Array containing the position of
104 // the first element to read in all
105 // dimension. SizeIn size must be equal to the number of dimension
106 // of the dataset. If nullptr, startIn values are 0.
107 // countIn: Array containing the number of elements to read in all
108 // dimension.
109 // countIn size must be equal to the number of dimension of the
110 // dataset.
111 // strideIn:Array containing the stride between elements to read in all
112 // dimension.
113 // strideIn size must be equal to the number of dimension of the
114 // dataset. If nullptr, stridIn values are 1.
115 //
116 // Examples:
117 // - Dataset contains a 3-bands raster. Dimensions are (100,100,3).
118 // To retrieve the full second band:
119 // startIn=[0,0,1], countIn=[100,100,1], strideIn=nullptr or [1,1,1]
120 // To retrieve the first band, but only every other elements in X direction:
121 // startIn=[0,0,0], countIn=[50,100,1], strideIn=[2,1,1]
122
124 template<typename T>
125 inline void read(T* buf, const int* startIn = nullptr,
126 const int* countIn = nullptr,
127 const int* strideIn = nullptr);
128
131 template<typename T>
132 inline void read(T* buf, const std::vector<std::slice>* slicesIn);
133
136 template<typename T> inline void read(T* buf, const std::gslice* gsliceIn);
137
139 template<typename T> inline void read(std::vector<T>& buf);
140
142 template<typename T>
143 inline void read(std::vector<T>& buf, const std::vector<int>* startIn,
144 const std::vector<int>* countIn,
145 const std::vector<int>* strideIn);
146
148 template<typename T>
149 inline void read(std::vector<T>& buf,
150 const std::vector<std::slice>* slicesIn);
151
154 template<typename T>
155 inline void read(std::vector<T>& buf, const std::gslice* gsliceIn);
156
158 template<typename T> inline void read(std::valarray<T>& buf);
159
161 template<typename T>
162 inline void read(std::valarray<T>& buf, const std::valarray<int>* startIn,
163 const std::valarray<int>* countIn,
164 const std::valarray<int>* strideIn);
165
168 template<typename T>
169 inline void read(std::valarray<T>& buf,
170 const std::vector<std::slice>* slicesIn);
171
174 template<typename T>
175 inline void read(std::valarray<T>& buf, const std::gslice* gsliceIn);
176
177 // Dataset writing queries
178
180 template<typename T> inline void write(const std::vector<T>& buf);
181
184 template<typename T, size_t S>
185 inline void write(const std::vector<T>& buf,
186 const std::array<int, S>& startIn,
187 const std::array<int, S>& countIn,
188 const std::array<int, S>& strideIn);
189
192 template<typename T>
193 inline void write(const std::vector<T>& buf,
194 const std::vector<std::slice>* slicesIn);
195
198 template<typename T>
199 inline void write(const std::vector<T>& buf, const std::gslice* gsliceIn);
200
202 template<typename T> inline void write(const std::valarray<T>& buf);
203
206 template<typename T, size_t S>
207 inline void write(const std::valarray<T>& buf,
208 const std::array<int, S>& startIn,
209 const std::array<int, S>& countIn,
210 const std::array<int, S>& strideIn);
211
214 template<typename T>
215 inline void write(const std::valarray<T>& buf,
216 const std::vector<std::slice>* slicesIn);
217
220 template<typename T>
221 inline void write(const std::valarray<T>& buf, const std::gslice* gsliceIn);
222
224 template<typename T> inline void write(const T* buf, const size_t sz);
225
228 template<typename T, size_t S>
229 inline void write(const T* buf, const std::array<int, S>& startIn,
230 const std::array<int, S>& countIn,
231 const std::array<int, S>& strideIn);
232
235 template<typename T>
236 inline void write(const T* buf, const std::vector<std::slice>* slicesIn);
237
240 template<typename T>
241 inline void write(const T* buf, const std::gslice* gsliceIn);
242
244 template<typename T>
245 inline void createAttribute(const std::string& name, const T& data);
246
248 template<typename T>
249 inline void createAttribute(const std::string& name,
250 const std::vector<T>& values);
251
253 template<typename T>
254 inline void createAttribute(const std::string& name,
255 const std::valarray<T>& values);
256
259 template<typename T, typename T2, size_t S>
260 inline void createAttribute(const std::string& name,
261 const std::array<T2, S>& dims,
262 const std::vector<T>& values);
263
266 template<typename T, typename T2, size_t S>
267 inline void createAttribute(const std::string& name,
268 const std::array<T2, S>& dims,
269 const std::valarray<T>& values);
270
273 template<typename T, typename T2, size_t S>
274 inline void createAttribute(const std::string& name,
275 const std::array<T2, S>& dims, const T* buffer);
276
279 H5::DataSpace getDataSpace(const int* startIn, const int* countIn,
280 const int* strideIn);
281
283 H5::DataSpace getDataSpace(const std::vector<std::slice>* sliceIn);
284
286 H5::DataSpace getDataSpace(const std::gslice* gsliceIn);
287
289 H5::DataSpace getDataSpace(const size_t xidx, const size_t yidx,
290 const size_t iowidth, const size_t iolength,
291 const size_t band);
292
293private:
294 template<typename T> void read(T* buffer, const H5::DataSpace& dspace);
295
296 template<typename T>
297 void createAttribute(const std::string& name, const H5::DataType& datatype,
298 const H5::DataSpace& dataspace, const T* buffer);
299
300 template<typename T>
301 void write(const T* buf, const H5::DataSpace& filespace);
302};
303
304class IGroup : public H5::Group {
305
306public:
307 IGroup() : H5::Group() {};
308 IGroup(const H5::Group& group) : H5::Group(group) {};
309 IGroup(hid_t group) : H5::Group(group) {};
310
311 std::vector<std::string> getAttrs();
312
314 std::vector<std::string> find(const std::string name,
315 const std::string start = ".",
316 const std::string type = "BOTH",
317 const std::string path = "FULL");
318
320 std::string getPathname();
321
323 H5::DataSpace getDataSpace(const std::string& name);
324
326 int getNumElements(const std::string& name);
327
329 template<typename T> inline void read(T& v, const std::string& att);
330
332 void read(std::string& v, const std::string& att);
333
335 template<typename T> inline void read(T* buf, const std::string& att);
336
338 template<typename T>
339 inline void read(std::vector<T>& buf, const std::string& att);
340
342 template<typename T>
343 inline void read(std::valarray<T>& buf, const std::string& att);
344
346 IDataSet openDataSet(const H5std_string& name);
347
349 IGroup openGroup(const H5std_string& name);
350
352 IGroup createGroup(const H5std_string& name);
353
355 IDataSet createDataSet(const std::string& name, const std::string& data);
356
358 template<typename T>
359 inline IDataSet createDataSet(const std::string& name, const T& data);
360
362 template<typename T>
363 inline IDataSet createDataSet(const std::string& name,
364 const std::vector<T>& data);
365
367 template<typename T>
368 inline IDataSet createDataSet(const std::string& name,
369 const std::valarray<T>& data);
370
372 template<typename T>
373 inline IDataSet createDataSet(const std::string& name, const T* buffer,
374 const size_t size);
375
377 template<typename T, typename T2, size_t S>
378 inline IDataSet createDataSet(const std::string& name,
379 const std::vector<T>& data,
380 const std::array<T2, S>& dims);
381
383 template<typename T, typename T2, size_t S>
384 inline IDataSet createDataSet(const std::string& name,
385 const std::valarray<T>& data,
386 const std::array<T2, S>& dims);
387
389 template<typename T, typename T2, size_t S>
390 inline IDataSet createDataSet(const std::string& name, const T* data,
391 const std::array<T2, S>& dims);
392
394 template<typename T, typename T2, size_t S>
395 IDataSet createDataSet(const std::string& name,
396 const std::array<T2, S>& dims, const int chunk = 0,
397 const int shuffle = 0, const int deflate = 0);
398
400 template<typename T>
401 inline void createAttribute(const std::string& name, const T& data);
402
404 template<typename T>
405 inline void createAttribute(const std::string& name,
406 const std::vector<T>& values);
407
409 template<typename T>
410 inline void createAttribute(const std::string& name,
411 const std::valarray<T>& values);
412
415 template<typename T, typename T2, size_t S>
416 inline void createAttribute(const std::string& name,
417 const std::array<T2, S>& dims,
418 const std::vector<T>& values);
419
422 template<typename T, typename T2, size_t S>
423 inline void createAttribute(const std::string& name,
424 const std::array<T2, S>& dims,
425 const std::valarray<T>& values);
426
429 template<typename T, typename T2, size_t S>
430 inline void createAttribute(const std::string& name,
431 const std::array<T2, S>& dims, const T* buffer);
432
433private:
434 template<typename T>
435 void createAttribute(const std::string& name, const H5::DataType& datatype,
436 const H5::DataSpace& dataspace, const T* buffer);
437};
438
439class IH5File : public H5::H5File {
440public:
441 IH5File() : H5::H5File() {};
442
450 IH5File(const H5std_string& name, const char mode = 'r');
451
452 void openFile(const H5std_string& name);
453
455 IDataSet openDataSet(const H5std_string& name);
456
458 IGroup openGroup(const H5std_string& name);
459
461 std::vector<std::string> find(const std::string name,
462 const std::string start = "/",
463 const std::string type = "BOTH",
464 const std::string path = "FULL");
465
467 inline std::string filename() const { return getFileName(); }
468
470 IGroup createGroup(const H5std_string& name);
471};
472} // namespace io
473} // namespace isce3
474
475// Get inline implementations for IH5
476#define ISCE_IO_IH5_ICC
477#include "IH5.icc"
478#undef ISCE_IO_IH5_ICC
Our derived dataset structure that includes utility functions.
Definition IH5.h:41
std::string getTypeClassStr(const std::string &v="")
Get the H5 data type of the dataset or given attribute.
Definition IH5.cpp:208
void write(const std::vector< T > &buf)
Writing std::vector data into a dataset.
Definition IH5.icc:525
std::string toGDAL() const
Generate GDALDataset Representation.
Definition IH5.cpp:350
H5::DataSpace getDataSpace(const std::string &v="")
Get a HDF5 DataSpace object corresponding to dataset or given attribute.
Definition IH5.cpp:127
std::vector< int > getDimensions(const std::string &v="")
Get the size of each dimension of the dataset or given attribute.
Definition IH5.cpp:172
std::vector< int > getChunkSize()
Get the storage chunk size of the dataset.
Definition IH5.cpp:276
void read(T &v, const std::string &att="")
Reading scalar (non string) dataset or attributes.
Definition IH5.icc:143
int getNumElements(const std::string &v="")
Get the total number of elements contained in dataset or given attribute.
Definition IH5.cpp:160
int getRank(const std::string &v="")
Get the number of dimension of dataset or given attribute.
Definition IH5.cpp:150
std::vector< std::string > getAttrs()
Get a list of attributes attached to dataset.
Definition IH5.cpp:252
void createAttribute(const std::string &name, const T &data)
Creating and writing a scalar as an attribute.
Definition IH5.icc:837
int getNumBits(const std::string &v="")
Get the number of bit used to store each dataset element.
Definition IH5.cpp:315
Definition IH5.h:304
IGroup createGroup(const H5std_string &name)
Create a group within this group.
Definition IH5.cpp:757
std::string getPathname()
Return the path of the group from the file root.
Definition IH5.cpp:672
IGroup openGroup(const H5std_string &name)
Open a given group.
Definition IH5.cpp:720
H5::DataSpace getDataSpace(const std::string &name)
Return the H5::DataSpace of the given attribute.
Definition IH5.cpp:689
IDataSet openDataSet(const H5std_string &name)
Open a given dataset.
Definition IH5.cpp:712
IDataSet createDataSet(const std::string &name, const std::vector< T > &data, const std::array< T2, S > &dims)
Create a dataset (nD) and write the data from a vector container.
IDataSet createDataSet(const std::string &name, const std::vector< T > &data)
Create a dataset (1D) and write the data from a vector container.
IDataSet createDataSet(const std::string &name, const T *buffer, const size_t size)
Create a dataset (1D) and write a buffer's data.
IDataSet createDataSet(const std::string &name, const T &data)
Create a (non-string) scalar dataset and simultaneously write the data.
IDataSet createDataSet(const std::string &name, const T *data, const std::array< T2, S > &dims)
Create a dataset (nD) and write the data from a buffer.
IDataSet createDataSet(const std::string &name, const std::string &data)
Create a string scalar dataset and simultaneously write the data.
Definition IH5.cpp:782
int getNumElements(const std::string &name)
Return the number of elements in the given attribute.
Definition IH5.cpp:701
void createAttribute(const std::string &name, const T &data)
Creating and writing a scalar as an attribute.
Definition IH5.icc:1251
void read(T &v, const std::string &att)
Reading scalar attribute given by name.
Definition IH5.icc:865
IDataSet createDataSet(const std::string &name, const std::valarray< T > &data)
Create a dataset (1D) and write the data from a valarray container.
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:662
IDataSet createDataSet(const std::string &name, const std::valarray< T > &data, const std::array< T2, S > &dims)
Create a dataset (nD) and write the data from a valarray container.
IDataSet createDataSet(const std::string &name, const std::array< T2, S > &dims, const int chunk=0, const int shuffle=0, const int deflate=0)
Create a datatset with compression options.
IDataSet openDataSet(const H5std_string &name)
Open a given dataset.
Definition IH5.cpp:794
std::string filename() const
Get filename of HDF5 file.
Definition IH5.h:467
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:846
IGroup openGroup(const H5std_string &name)
Open a given group.
Definition IH5.cpp:802
IGroup createGroup(const H5std_string &name)
Create a group.
Definition IH5.cpp:810
The isce3::io namespace.
Definition Constants.h:14
base interpolator is an abstract base class
Definition BinarySearchFunc.cpp:5
Definition IH5.h:33
Definition IH5.h:25
Definition IH5.h:27
Definition IH5.h:29

Generated for ISCE3.0 by doxygen 1.13.2.