HdfItem.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <hdf4cpp/HdfException.h>
  9. #include <hdf4cpp/HdfDestroyer.h>
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <map>
  13. #include <memory>
  14. #include <vector>
  15. #include <hdf/hdf.h>
  16. namespace hdf4cpp {
  17. struct Range {
  18. int32 begin;
  19. int32 quantity;
  20. int32 stride;
  21. Range(int32 begin = 0, int32 quantity = 0, int32 stride = 1) : begin(begin), quantity(quantity), stride(stride) {}
  22. intn size() const {
  23. if(!stride) {
  24. return FAIL;
  25. }
  26. return quantity / stride;
  27. }
  28. bool check(const int32& dim) const {
  29. return begin >= 0 ||
  30. begin < dim ||
  31. quantity >= 0 ||
  32. begin + quantity <= dim ||
  33. stride > 0;
  34. }
  35. static void fill(std::vector<Range>& ranges, const std::vector<int32>& dims) {
  36. for(size_t i = ranges.size(); i < dims.size(); ++i) {
  37. ranges.push_back(Range(0, dims[i]));
  38. }
  39. }
  40. };
  41. class HdfItemBase : public HdfObject {
  42. public:
  43. HdfItemBase(int32 id, const Type& type, const HdfDestroyerChain& chain) : HdfObject(type, ITEM, chain), id(id) {
  44. if(id == FAIL) {
  45. raiseException(INVALID_ID);
  46. }
  47. }
  48. virtual ~HdfItemBase() {}
  49. virtual int32 getId() const = 0;
  50. virtual std::string getName() const = 0;
  51. virtual std::vector<int32> getDims() = 0;
  52. virtual intn size() const = 0;
  53. virtual HdfAttribute getAttribute(const std::string& name) const = 0;
  54. protected:
  55. int32 id;
  56. virtual int32 getDataType() const = 0;
  57. };
  58. class HdfDatasetItem : public HdfItemBase {
  59. public:
  60. HdfDatasetItem(int32 id, const HdfDestroyerChain& chain);
  61. ~HdfDatasetItem();
  62. int32 getId() const;
  63. std::string getName() const;
  64. std::vector<int32> getDims();
  65. intn size() const;
  66. HdfAttribute getAttribute(const std::string& name) const;
  67. template <class T> void read(std::vector<T>& dest, std::vector<Range>& ranges) {
  68. std::vector<int32> dims = getDims();
  69. Range::fill(ranges, dims);
  70. intn length = 1;
  71. for(size_t i = 0; i < ranges.size(); ++i) {
  72. if(!ranges[i].check(dims[i])) {
  73. raiseException(INVALID_RANGES);
  74. }
  75. length *= ranges[i].size();
  76. }
  77. auto it = typeSizeMap.find(getDataType());
  78. if(it != typeSizeMap.end()) {
  79. if((size_t) it->second != sizeof(T)) {
  80. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  81. }
  82. } else {
  83. raiseException(INVALID_DATA_TYPE);
  84. }
  85. dest.resize(length);
  86. std::vector<int32> start, quantity, stride;
  87. for(const auto& range : ranges) {
  88. start.push_back(range.begin);
  89. quantity.push_back(range.quantity);
  90. stride.push_back(range.stride);
  91. }
  92. if(SDreaddata(id, start.data(), stride.data(), quantity.data(), dest.data()) == FAIL) {
  93. raiseException(STATUS_RETURN_FAIL);
  94. }
  95. }
  96. template <class T> void read(std::vector<T>& dest) {
  97. std::vector<int32> dims = getDims();
  98. std::vector<Range> ranges;
  99. for(const auto& dim : dims) {
  100. ranges.push_back(Range(0, dim));
  101. }
  102. read(dest, ranges);
  103. }
  104. private:
  105. intn _size;
  106. int32 dataType;
  107. std::string name;
  108. std::vector<int32> dims;
  109. int32 getDataType() const;
  110. };
  111. class HdfGroupItem : public HdfItemBase {
  112. public:
  113. HdfGroupItem(int32 id, const HdfDestroyerChain& chain);
  114. ~HdfGroupItem();
  115. int32 getId() const;
  116. std::string getName() const;
  117. std::vector<int32> getDims();
  118. intn size() const;
  119. HdfAttribute getAttribute(const std::string& name) const;
  120. private:
  121. std::string name;
  122. int32 getDataType() const;
  123. };
  124. class HdfDataItem : public HdfItemBase {
  125. public:
  126. HdfDataItem(int32 id, const HdfDestroyerChain& chain);
  127. ~HdfDataItem();
  128. int32 getId() const;
  129. std::string getName() const;
  130. std::vector<int32> getDims();
  131. intn size() const;
  132. HdfAttribute getAttribute(const std::string &name) const;
  133. template<class T>
  134. void read(std::vector<T> &dest, const std::string &field, int32 records) {
  135. if (!records) {
  136. records = nrRecords;
  137. }
  138. if (VSsetfields(id, field.c_str()) == FAIL) {
  139. raiseException(STATUS_RETURN_FAIL);
  140. }
  141. int32 fieldSize = VSsizeof(id, (char *) field.c_str());
  142. if (sizeof(T) < fieldSize) {
  143. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  144. }
  145. size_t size = records * fieldSize;
  146. std::vector<uint8> buff(size);
  147. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  148. raiseException(STATUS_RETURN_FAIL);
  149. }
  150. dest.resize(records);
  151. VOIDP buffptrs[1];
  152. buffptrs[0] = dest.data();
  153. if (VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs) == FAIL) {
  154. raiseException(STATUS_RETURN_FAIL);
  155. }
  156. }
  157. template <class T> void read(std::vector<std::vector<T> >& dest, const std::string& field, int32 records) {
  158. if(!records) {
  159. records = nrRecords;
  160. }
  161. if(VSsetfields(id, field.c_str()) == FAIL) {
  162. raiseException(STATUS_RETURN_FAIL);
  163. }
  164. int32 fieldSize = VSsizeof(id, (char *) field.c_str());
  165. if(fieldSize % sizeof(T) != 0) {
  166. raiseException(BUFFER_SIZE_NOT_DIVISIBLE);
  167. }
  168. size_t size = records * fieldSize;
  169. std::vector<uint8> buff(size);
  170. if(VSread(id, buff.data(), records, interlace) == FAIL) {
  171. raiseException(STATUS_RETURN_FAIL);
  172. }
  173. int32 divided = fieldSize / sizeof(T);
  174. dest.resize(records, std::vector<T>(divided));
  175. std::vector<T> linearDest(records * divided);
  176. VOIDP buffptrs[1];
  177. buffptrs[0] = linearDest.data();
  178. VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs);
  179. int i = 0;
  180. int j;
  181. while(i < records) {
  182. j = 0;
  183. while(j < divided) {
  184. dest[i][j] = linearDest[i * divided + j];
  185. ++j;
  186. }
  187. ++i;
  188. }
  189. }
  190. private:
  191. intn _size;
  192. std::string name;
  193. int32 nrRecords;
  194. int32 interlace;
  195. int32 recordSize;
  196. std::vector<std::string> fieldNames;
  197. int32 getDataType() const;
  198. };
  199. class HdfItem : public HdfObject {
  200. public:
  201. HdfItem(HdfItemBase *item, int32 sId, int32 vId);
  202. HdfItem(const HdfItem& item) = delete;
  203. HdfItem(HdfItem&& item);
  204. HdfItem& operator=(const HdfItem& item) = delete;
  205. HdfItem& operator=(HdfItem&& it);
  206. Type getType() const;
  207. std::string getName() const;
  208. std::vector<int32> getDims();
  209. intn size() const;
  210. HdfAttribute getAttribute(const std::string& name) const;
  211. template <class T> void read(std::vector<T>& dest) {
  212. switch(item->getType()) {
  213. case SDATA: {
  214. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  215. dItem->read(dest);
  216. break;
  217. }
  218. default:
  219. raiseException(INVALID_OPERATION);
  220. }
  221. }
  222. template <class T> void read(std::vector<T>& dest, std::vector<Range> ranges) {
  223. switch(item->getType()) {
  224. case SDATA: {
  225. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  226. dItem->read(dest, ranges);
  227. break;
  228. }
  229. default:
  230. raiseException(INVALID_OPERATION);
  231. }
  232. }
  233. template <class T> void read(std::vector<T>& dest, const std::string& field, int32 records = 0) {
  234. switch(item->getType()) {
  235. case VDATA: {
  236. HdfDataItem *vItem = dynamic_cast<HdfDataItem*>(item.get());
  237. vItem->read(dest, field, records);
  238. break;
  239. }
  240. default:
  241. raiseException(INVALID_OPERATION);
  242. }
  243. }
  244. class Iterator;
  245. Iterator begin() const;
  246. Iterator end() const;
  247. private:
  248. std::unique_ptr<HdfItemBase> item;
  249. int32 sId;
  250. int32 vId;
  251. };
  252. class HdfItem::Iterator : public HdfObject, public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  253. public:
  254. Iterator(int32 sId, int32 vId, int32 key, int32 index, Type type, const HdfDestroyerChain& chain) :
  255. HdfObject(type, ITERATOR, chain),
  256. sId(sId),
  257. vId(vId),
  258. key(key),
  259. index(index) {}
  260. bool operator!=(const Iterator& it) { return index != it.index; }
  261. bool operator==(const Iterator& it) { return index == it.index; }
  262. Iterator& operator++() {
  263. ++index;
  264. return *this;
  265. }
  266. Iterator operator++(int) {
  267. Iterator it(*this);
  268. ++index;
  269. return it;
  270. }
  271. Iterator& operator--() {
  272. --index;
  273. return *this;
  274. }
  275. Iterator operator--(int) {
  276. Iterator it(*this);
  277. --index;
  278. return it;
  279. }
  280. HdfItem operator*() {
  281. int32 tag, ref;
  282. if(Vgettagref(key, index, &tag, &ref) == FAIL) {
  283. raiseException(OUT_OF_RANGE);
  284. }
  285. if(Visvs(key, ref)) {
  286. int32 id = VSattach(vId, ref, "r");
  287. return HdfItem(new HdfDataItem(id, chain), sId, vId);
  288. } else if(Visvg(key, ref)) {
  289. int32 id = Vattach(vId, ref, "r");
  290. return HdfItem(new HdfGroupItem(id, chain), sId, vId);
  291. } else {
  292. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  293. return HdfItem(new HdfDatasetItem(id, chain), sId, vId);
  294. }
  295. }
  296. private:
  297. int32 sId;
  298. int32 vId;
  299. int32 key;
  300. int32 index;
  301. };
  302. }
  303. #endif //HDF4CPP_HDFITEM_H