HdfItem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/HdfFile.h>
  8. #include <hdf4cpp/HdfException.h>
  9. #include <algorithm>
  10. #include <map>
  11. #include <memory>
  12. #include <vector>
  13. #include <hdf/hdf.h>
  14. namespace hdf4cpp {
  15. /// Structure to define in which range we want to read the data from the SData
  16. /// It must be defined for every dimension
  17. struct Range {
  18. /// The index in which begins the reading of the data
  19. int32 begin;
  20. /// The number of data to be read
  21. int32 quantity;
  22. /// The increasing number (stride = 1 : every data, stride = 2 : every second data, ...)
  23. int32 stride;
  24. Range(int32 begin = 0, int32 quantity = 0, int32 stride = 1) : begin(begin), quantity(quantity), stride(stride) {}
  25. /// What would be the number of data to read, if we read in this range
  26. intn size() const {
  27. if (!stride) {
  28. return 0;
  29. }
  30. return quantity / stride;
  31. }
  32. /// Checks if the range is correct for a specific dimension
  33. bool check(const int32& dim) const {
  34. return begin >= 0 ||
  35. begin < dim ||
  36. quantity >= 0 ||
  37. begin + quantity <= dim ||
  38. stride > 0;
  39. }
  40. /// Fills a range vector with a dimension array
  41. static void fill(std::vector<Range>& ranges, const std::vector<int32>& dims) {
  42. for (size_t i = ranges.size(); i < dims.size(); ++i) {
  43. ranges.push_back(Range(0, dims[i]));
  44. }
  45. }
  46. };
  47. class HdfAttribute;
  48. /// Represents an hdf item
  49. class HdfItem : public HdfObject {
  50. public:
  51. HdfItem(const HdfItem& item) = delete;
  52. HdfItem(HdfItem&& item);
  53. HdfItem& operator=(const HdfItem& item) = delete;
  54. HdfItem& operator=(HdfItem&& it);
  55. /// \returns The name of the item
  56. std::string getName() const;
  57. /// \returns The dimensions of the item
  58. /// \note This operation is not supported for every item type
  59. std::vector<int32> getDims();
  60. /// \returns The number of data being in the item
  61. intn size() const;
  62. /// \returns the attribute of the item with the given name
  63. /// \param name the name of the attribute
  64. /// \note If there are multiple attributes with the same name then the first will be returned
  65. HdfAttribute getAttribute(const std::string& name) const;
  66. /// Reads the entire data from the item
  67. /// \param dest the destination vector in which the data will be stored
  68. template <class T> void read(std::vector<T>& dest) {
  69. switch(item->getType()) {
  70. case SDATA: {
  71. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  72. dItem->read(dest);
  73. break;
  74. }
  75. default:
  76. raiseException(INVALID_OPERATION);
  77. }
  78. }
  79. /// Reads the data from the item in a specified range
  80. /// \param dest the destination vector in which the data will be stored
  81. /// \param ranges specifies the range in which the data will be read
  82. template <class T> void read(std::vector<T>& dest, std::vector<Range> ranges) {
  83. switch(item->getType()) {
  84. case SDATA: {
  85. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  86. dItem->read(dest, ranges);
  87. break;
  88. }
  89. default:
  90. raiseException(INVALID_OPERATION);
  91. }
  92. }
  93. /// Reads the given field from the item
  94. /// \param dest the destination vector in which the data will be stored
  95. /// \param field the name of the field
  96. /// \param records the number of records to be read
  97. template <class T> void read(std::vector<T>& dest, const std::string& field, int32 records = 0) {
  98. switch(item->getType()) {
  99. case VDATA: {
  100. HdfDataItem *vItem = dynamic_cast<HdfDataItem*>(item.get());
  101. vItem->read(dest, field, records);
  102. break;
  103. }
  104. default:
  105. raiseException(INVALID_OPERATION);
  106. }
  107. }
  108. class Iterator;
  109. Iterator begin() const;
  110. Iterator end() const;
  111. friend HdfItem HdfFile::get(const std::string& name) const;
  112. friend std::vector<HdfItem> HdfFile::getAll(const std::string& name) const;
  113. friend HdfItem HdfFile::Iterator::operator*();
  114. friend class HdfAttribute;
  115. private:
  116. /// The base class of the item classes
  117. class HdfItemBase : public HdfObject {
  118. public:
  119. HdfItemBase(int32 id, const Type& type, const HdfDestroyerChain& chain) : HdfObject(type, ITEM, chain), id(id) {
  120. if (id == FAIL) {
  121. raiseException(INVALID_ID);
  122. }
  123. }
  124. virtual ~HdfItemBase() {}
  125. /// Get the id which is held by this object
  126. virtual int32 getId() const = 0;
  127. /// Get the name of the item
  128. virtual std::string getName() const = 0;
  129. /// Get the dimensions of the item
  130. virtual std::vector<int32> getDims() = 0;
  131. /// Get the number of data from the item
  132. virtual intn size() const = 0;
  133. /// Get the attribute from the item given by its name
  134. virtual HdfAttribute getAttribute(const std::string& name) const = 0;
  135. protected:
  136. int32 id;
  137. virtual int32 getDataType() const = 0;
  138. };
  139. /// Item class for the SData items
  140. class HdfDatasetItem : public HdfItemBase {
  141. public:
  142. HdfDatasetItem(int32 id, const HdfDestroyerChain& chain);
  143. ~HdfDatasetItem();
  144. int32 getId() const;
  145. std::string getName() const;
  146. std::vector<int32> getDims();
  147. intn size() const;
  148. HdfAttribute getAttribute(const std::string& name) const;
  149. /// Reads the data in a specific range. See Range
  150. /// \param dest The destination vector
  151. /// \param ranges The vector of ranges
  152. template <class T>
  153. void read(std::vector<T>& dest, std::vector<Range>& ranges) {
  154. std::vector<int32> dims = getDims();
  155. Range::fill(ranges, dims);
  156. intn length = 1;
  157. for (size_t i = 0; i < ranges.size(); ++i) {
  158. if (!ranges[i].check(dims[i])) {
  159. raiseException(INVALID_RANGES);
  160. }
  161. length *= ranges[i].size();
  162. }
  163. auto it = typeSizeMap.find(getDataType());
  164. if (it != typeSizeMap.end()) {
  165. if ((size_t)it->second != sizeof(T)) {
  166. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  167. }
  168. } else {
  169. raiseException(INVALID_DATA_TYPE);
  170. }
  171. dest.resize(length);
  172. std::vector<int32> start, quantity, stride;
  173. for (const auto& range : ranges) {
  174. start.push_back(range.begin);
  175. quantity.push_back(range.quantity);
  176. stride.push_back(range.stride);
  177. }
  178. if (SDreaddata(id, start.data(), stride.data(), quantity.data(), dest.data()) == FAIL) {
  179. raiseException(STATUS_RETURN_FAIL);
  180. }
  181. }
  182. /// Reads the whole data
  183. /// \param dest The destination vector
  184. template <class T>
  185. void read(std::vector<T>& dest) {
  186. std::vector<int32> dims = getDims();
  187. std::vector<Range> ranges;
  188. for (const auto& dim : dims) {
  189. ranges.push_back(Range(0, dim));
  190. }
  191. read(dest, ranges);
  192. }
  193. private:
  194. intn _size;
  195. int32 dataType;
  196. std::string name;
  197. std::vector<int32> dims;
  198. int32 getDataType() const;
  199. };
  200. /// Item class for VGroup items
  201. class HdfGroupItem : public HdfItemBase {
  202. public:
  203. HdfGroupItem(int32 id, const HdfDestroyerChain& chain);
  204. ~HdfGroupItem();
  205. int32 getId() const;
  206. std::string getName() const;
  207. std::vector<int32> getDims();
  208. intn size() const;
  209. HdfAttribute getAttribute(const std::string& name) const;
  210. private:
  211. std::string name;
  212. int32 getDataType() const;
  213. };
  214. /// Item class for VData items
  215. class HdfDataItem : public HdfItemBase {
  216. public:
  217. HdfDataItem(int32 id, const HdfDestroyerChain& chain);
  218. ~HdfDataItem();
  219. int32 getId() const;
  220. std::string getName() const;
  221. std::vector<int32> getDims();
  222. intn size() const;
  223. HdfAttribute getAttribute(const std::string& name) const;
  224. /// Reads a specific number of the data of a specific field
  225. /// The records are simple values
  226. /// \param dest The destination vector
  227. /// \param field The specific field name
  228. /// \param records The number of records to be read
  229. template <class T>
  230. void read(std::vector<T>& dest, const std::string& field, int32 records) {
  231. if (!records) {
  232. records = nrRecords;
  233. }
  234. if (VSsetfields(id, field.c_str()) == FAIL) {
  235. raiseException(STATUS_RETURN_FAIL);
  236. }
  237. int32 fieldSize = VSsizeof(id, (char*)field.c_str());
  238. if (sizeof(T) < (size_t) fieldSize) {
  239. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  240. }
  241. size_t size = records * fieldSize;
  242. std::vector<uint8> buff(size);
  243. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  244. raiseException(STATUS_RETURN_FAIL);
  245. }
  246. dest.resize(records);
  247. VOIDP buffptrs[1];
  248. buffptrs[0] = dest.data();
  249. if (VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs) == FAIL) {
  250. raiseException(STATUS_RETURN_FAIL);
  251. }
  252. }
  253. /// Reads a specific number of the data of a specific field
  254. /// The records are arrays itself
  255. /// \param dest The destination matrix (every record is a vector)
  256. /// \param field The specific field name
  257. /// \param records The number of records to be read
  258. template <class T>
  259. void read(std::vector<std::vector<T> >& dest, const std::string& field, int32 records) {
  260. if (!records) {
  261. records = nrRecords;
  262. }
  263. if (VSsetfields(id, field.c_str()) == FAIL) {
  264. raiseException(STATUS_RETURN_FAIL);
  265. }
  266. int32 fieldSize = VSsizeof(id, (char*)field.c_str());
  267. if (fieldSize % sizeof(T) != 0) {
  268. raiseException(BUFFER_SIZE_NOT_DIVISIBLE);
  269. }
  270. size_t size = records * fieldSize;
  271. std::vector<uint8> buff(size);
  272. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  273. raiseException(STATUS_RETURN_FAIL);
  274. }
  275. int32 divided = fieldSize / sizeof(T);
  276. dest.resize(records, std::vector<T>(divided));
  277. std::vector<T> linearDest(records * divided);
  278. VOIDP buffptrs[1];
  279. buffptrs[0] = linearDest.data();
  280. VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs);
  281. int i = 0;
  282. int j;
  283. while (i < records) {
  284. j = 0;
  285. while (j < divided) {
  286. dest[i][j] = linearDest[i * divided + j];
  287. ++j;
  288. }
  289. ++i;
  290. }
  291. }
  292. private:
  293. intn _size;
  294. std::string name;
  295. int32 nrRecords;
  296. int32 interlace;
  297. int32 recordSize;
  298. int32 getDataType() const;
  299. };
  300. HdfItem(HdfItemBase *item, int32 sId, int32 vId);
  301. /// Holds an item object address
  302. std::unique_ptr<HdfItemBase> item;
  303. /// The file handle ids (needed by the iterator)
  304. int32 sId;
  305. int32 vId;
  306. };
  307. /// HdfItem iterator, gives the possibility to iterate over the items from the item
  308. class HdfItem::Iterator : public HdfObject, public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  309. public:
  310. Iterator(int32 sId, int32 vId, int32 key, int32 index, Type type, const HdfDestroyerChain& chain) :
  311. HdfObject(type, ITERATOR, chain),
  312. sId(sId),
  313. vId(vId),
  314. key(key),
  315. index(index) {}
  316. bool operator!=(const Iterator& it) { return index != it.index; }
  317. bool operator==(const Iterator& it) { return index == it.index; }
  318. Iterator& operator++() {
  319. ++index;
  320. return *this;
  321. }
  322. Iterator operator++(int) {
  323. Iterator it(*this);
  324. ++index;
  325. return it;
  326. }
  327. Iterator& operator--() {
  328. --index;
  329. return *this;
  330. }
  331. Iterator operator--(int) {
  332. Iterator it(*this);
  333. --index;
  334. return it;
  335. }
  336. HdfItem operator*() {
  337. int32 tag, ref;
  338. if(Vgettagref(key, index, &tag, &ref) == FAIL) {
  339. raiseException(OUT_OF_RANGE);
  340. }
  341. if(Visvs(key, ref)) {
  342. int32 id = VSattach(vId, ref, "r");
  343. return HdfItem(new HdfDataItem(id, chain), sId, vId);
  344. } else if(Visvg(key, ref)) {
  345. int32 id = Vattach(vId, ref, "r");
  346. return HdfItem(new HdfGroupItem(id, chain), sId, vId);
  347. } else {
  348. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  349. return HdfItem(new HdfDatasetItem(id, chain), 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