HdfItem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFITEM_H
  5. #define HDF4CPP_HDFITEM_H
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfAttribute.h>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <map>
  11. #include <memory>
  12. #include <vector>
  13. #include <hdf/hdf.h>
  14. namespace hdf4cpp {
  15. struct Range {
  16. int32 begin;
  17. int32 quantity;
  18. int32 stride;
  19. Range(int32 begin = 0, int32 quantity = 0, int32 stride = 1) : begin(begin), quantity(quantity), stride(stride) {}
  20. intn size() const {
  21. if(!stride) {
  22. return FAIL;
  23. }
  24. return quantity / stride;
  25. }
  26. bool check(const int32& dim) const {
  27. return begin >= 0 ||
  28. begin < dim ||
  29. quantity >= 0 ||
  30. begin + quantity <= dim ||
  31. stride > 0;
  32. }
  33. static void fill(std::vector<Range>& ranges, const std::vector<int32>& dims) {
  34. for(int i = ranges.size(); i < dims.size(); ++i) {
  35. ranges.push_back(Range(0, dims[i]));
  36. }
  37. }
  38. };
  39. class HdfItemBase {
  40. public:
  41. HdfItemBase(int32 id) : id(id) {}
  42. virtual ~HdfItemBase() {}
  43. virtual bool isValid() const { return id != FAIL; }
  44. virtual Type getType() const = 0;
  45. virtual int32 getId() const = 0;
  46. virtual std::string getName() const = 0;
  47. virtual std::vector<int32> getDims() = 0;
  48. virtual intn size() const = 0;
  49. template <class T> bool read(std::vector<T> &dest, const std::vector<Range>& ranges) {
  50. if(size() == FAIL) {
  51. return false;
  52. }
  53. std::vector<int32> dims = getDims();
  54. if(ranges.size() != dims.size()) {
  55. throw std::runtime_error("HDF4CPP: incorrect number of ranges");
  56. }
  57. intn length = 1;
  58. for(int i = 0; i < dims.size(); ++i) {
  59. if(ranges[i].begin < 0 || ranges[i].begin >= dims[i] || ranges[i].quantity < 0 || ranges[i].begin + ranges[i].quantity > dims[i] || ranges[i].stride <= 0) {
  60. throw std::runtime_error("HDF4CPP: incorrect range");
  61. }
  62. length *= ranges[i].size();
  63. }
  64. if(length > 0) {
  65. auto it = typeSizeMap.find(getDataType());
  66. if(it != typeSizeMap.end()) {
  67. if(it->second != sizeof(T)) {
  68. throw std::runtime_error("HDF4CPP: type size missmatch");
  69. }
  70. } else {
  71. throw std::runtime_error("HDF4CPP: hdf data set type not supported");
  72. }
  73. dest.resize(length);
  74. return read(dest, ranges);
  75. } else {
  76. return false;
  77. }
  78. }
  79. template <class T> bool read(std::vector<T> &dest) {
  80. std::vector<int32> dims = getDims();
  81. std::vector<Range> ranges(dims.size());
  82. std::transform(dims.begin(), dims.end(), ranges.begin(), [](const int32& t) {
  83. return Range(0, t);
  84. });
  85. return read(dest, ranges);
  86. }
  87. virtual HdfAttribute getAttribute(const std::string& name) = 0;
  88. protected:
  89. int32 id;
  90. virtual int32 getDataType() const = 0;
  91. };
  92. class HdfDatasetItem : public HdfItemBase {
  93. public:
  94. HdfDatasetItem(int32 id);
  95. ~HdfDatasetItem();
  96. Type getType() const;
  97. int32 getId() const;
  98. std::string getName() const;
  99. std::vector<int32> getDims();
  100. intn size() const;
  101. HdfAttribute getAttribute(const std::string& name);
  102. template <class T> bool read(std::vector<T>& dest, std::vector<Range>& ranges) {
  103. if(_size == FAIL) {
  104. return false;
  105. }
  106. if(!isValid()) {
  107. return false;
  108. }
  109. std::vector<int32> dims = getDims();
  110. Range::fill(ranges, dims);
  111. intn length = 1;
  112. for(int i = 0; i < ranges.size(); ++i) {
  113. if(!ranges[i].check(dims[i])) {
  114. return false;
  115. // TODO or throw exception (ask Moritz)
  116. }
  117. length *= ranges[i].size();
  118. }
  119. if(length > 0) {
  120. // TODO try with the new size getter hdf function
  121. auto it = typeSizeMap.find(getDataType());
  122. if(it != typeSizeMap.end()) {
  123. if(it->second != sizeof(T)) {
  124. throw std::runtime_error("HDF4CPP: type size missmatch");
  125. }
  126. } else {
  127. throw std::runtime_error("HDF4CPP: hdf data type not supported");
  128. }
  129. dest.resize(length);
  130. std::vector<int32> start, quantity, stride;
  131. for(const auto& range : ranges) {
  132. start.push_back(range.begin);
  133. quantity.push_back(range.quantity);
  134. stride.push_back(range.stride);
  135. }
  136. return SDreaddata(id, start.data(), stride.data(), quantity.data(), dest.data()) != FAIL;
  137. } else {
  138. return false;
  139. }
  140. }
  141. template <class T> bool read(std::vector<T>& dest) {
  142. std::vector<int32> dims = getDims();
  143. std::vector<Range> ranges;
  144. for(const auto& dim : dims) {
  145. ranges.push_back(Range(0, dim));
  146. }
  147. return read(dest, ranges);
  148. }
  149. private:
  150. intn _size;
  151. int32 dataType;
  152. std::string name;
  153. int32 getDataType() const;
  154. };
  155. class HdfGroupItem : public HdfItemBase {
  156. public:
  157. HdfGroupItem(int32 id);
  158. ~HdfGroupItem();
  159. Type getType() const;
  160. int32 getId() const;
  161. std::string getName() const;
  162. std::vector<int32> getDims();
  163. intn size() const;
  164. HdfAttribute getAttribute(const std::string& name);
  165. private:
  166. std::string name;
  167. int32 getDataType() const;
  168. };
  169. class HdfDataItem : public HdfItemBase {
  170. public:
  171. HdfDataItem(int32 id);
  172. ~HdfDataItem();
  173. Type getType() const;
  174. int32 getId() const;
  175. std::string getName() const;
  176. std::vector<int32> getDims();
  177. intn size() const;
  178. HdfAttribute getAttribute(const std::string &name);
  179. template<class T>
  180. bool read(std::vector<T> &dest, const std::string &field, int32 records) {
  181. if (!records) {
  182. records = nrRecords;
  183. }
  184. if (VSsetfields(id, field.c_str()) == FAIL) {
  185. return false;
  186. }
  187. int32 fieldSize = VSsizeof(id, (char *) field.c_str());
  188. if (sizeof(T) < fieldSize) {
  189. throw std::runtime_error(
  190. "HDF4CPP: the size of the destination type is less than the size of the field type");
  191. }
  192. size_t size = records * fieldSize;
  193. std::vector<uint8> buff(size);
  194. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  195. return false;
  196. }
  197. dest.resize(records);
  198. VOIDP buffptrs[1];
  199. buffptrs[0] = dest.data();
  200. if (VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs) == FAIL) {
  201. return false;
  202. }
  203. return true;
  204. }
  205. template <class T> bool read(std::vector<std::vector<T> >& dest, const std::string& field, int32 records) {
  206. if(!records) {
  207. records = nrRecords;
  208. }
  209. if(VSsetfields(id, field.c_str()) == FAIL) {
  210. return false;
  211. }
  212. int32 fieldSize = VSsizeof(id, (char *) field.c_str());
  213. if(fieldSize % sizeof(T) != 0) {
  214. throw std::runtime_error("HDF4CPP: the size of the destination type is less than the size of the field type");
  215. }
  216. size_t size = records * fieldSize;
  217. std::vector<uint8> buff(size);
  218. if(VSread(id, buff.data(), records, interlace) == FAIL) {
  219. return false;
  220. }
  221. int32 divided = fieldSize / sizeof(T);
  222. dest.resize(records, std::vector<T>(divided));
  223. std::vector<T> linearDest(records * divided);
  224. VOIDP buffptrs[1];
  225. buffptrs[0] = linearDest.data();
  226. VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs);
  227. int i = 0;
  228. int j;
  229. while(i < records) {
  230. j = 0;
  231. while(j < divided) {
  232. dest[i][j] = linearDest[i * divided + j];
  233. ++j;
  234. }
  235. ++i;
  236. }
  237. return true;
  238. }
  239. private:
  240. intn _size;
  241. std::string name;
  242. int32 nrRecords;
  243. int32 interlace;
  244. int32 recordSize;
  245. std::vector<std::string> fieldNames;
  246. int32 getDataType() const;
  247. };
  248. class HdfItem {
  249. public:
  250. HdfItem(HdfItemBase *item, int32 sId, int32 vId) : item(item), sId(sId), vId(vId) {}
  251. HdfItem(const HdfItem& item) = delete;
  252. HdfItem(HdfItem&& item);
  253. bool isValid() const;
  254. Type getType() const;
  255. std::string getName() const;
  256. std::vector<int32> getDims();
  257. intn size() const;
  258. HdfAttribute getAttribute(const std::string& name);
  259. template <class T> bool read(std::vector<T>& dest) {
  260. if(!isValid()) {
  261. return false;
  262. }
  263. switch(item->getType()) {
  264. case SDATA: {
  265. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  266. return dItem->read(dest);
  267. }
  268. case VGROUP: {
  269. HdfGroupItem *gItem = dynamic_cast<HdfGroupItem *>(item.get());
  270. return gItem->read(dest);
  271. }
  272. default:
  273. return false;
  274. }
  275. }
  276. template <class T> bool read(std::vector<T>& dest, std::vector<Range> ranges) {
  277. if(!isValid()) {
  278. return false;
  279. }
  280. switch(item->getType()) {
  281. case SDATA: {
  282. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  283. return dItem->read(dest, ranges);
  284. }
  285. default:
  286. return false;
  287. }
  288. }
  289. template <class T> bool read(std::vector<T>& dest, const std::string& field, int32 records = 0) {
  290. if(!isValid()) {
  291. return false;
  292. }
  293. switch(item->getType()) {
  294. case VDATA: {
  295. HdfDataItem *vItem = dynamic_cast<HdfDataItem*>(item.get());
  296. return vItem->read(dest, field, records);
  297. }
  298. default:
  299. return false;
  300. }
  301. }
  302. class Iterator;
  303. Iterator begin() const;
  304. Iterator end() const;
  305. private:
  306. std::unique_ptr<HdfItemBase> item;
  307. int32 sId;
  308. int32 vId;
  309. };
  310. class HdfItem::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  311. public:
  312. Iterator(int32 sId, int32 vId, int32 key, int32 index) : sId(sId), vId(vId), key(key), index(index) {}
  313. bool operator!=(const Iterator& it) { return index != it.index; }
  314. bool operator==(const Iterator& it) { return index == it.index; }
  315. // bool operator<(const Iterator& it) { return index < it.index; }
  316. // bool operator>(const Iterator& it) { return index > it.index; }
  317. // bool operator>=(const Iterator& it) { return index >= it.index; }
  318. // bool operator<=(const Iterator& it) { return index <= it.index; }
  319. Iterator& operator++() {
  320. ++index;
  321. return *this;
  322. }
  323. Iterator operator++(int) {
  324. Iterator it(*this);
  325. ++index;
  326. return it;
  327. }
  328. Iterator& operator--() {
  329. --index;
  330. return *this;
  331. }
  332. Iterator operator--(int) {
  333. Iterator it(*this);
  334. --index;
  335. return it;
  336. }
  337. HdfItem operator*() {
  338. int32 tag, ref;
  339. if(Vgettagref(key, index, &tag, &ref) == FAIL) {
  340. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  341. }
  342. if(Visvs(key, ref)) {
  343. throw std::runtime_error("HDF4CPP: vdata not supported yet");
  344. } else if(Visvg(key, ref)) {
  345. int32 id = Vattach(vId, ref, "r");
  346. return HdfItem(new HdfGroupItem(id), sId, vId);
  347. } else {
  348. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  349. return HdfItem(new HdfDatasetItem(id), sId, vId);
  350. }
  351. }
  352. private:
  353. int32 sId;
  354. int32 vId;
  355. int32 key;
  356. int32 index;
  357. };
  358. }
  359. #endif //HDF4CPP_HDFITEM_H