HdfItem.h 13 KB

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