HdfItem.h 9.8 KB

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