HdfItem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/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. intn 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. 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
  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 intn 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. intn 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. intn 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. intn _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. intn 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. intn 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. dest.resize(records);
  248. VOIDP buffptrs[1];
  249. buffptrs[0] = dest.data();
  250. if (VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs) ==
  251. FAIL) {
  252. raiseException(STATUS_RETURN_FAIL);
  253. }
  254. }
  255. /// Reads a specific number of the data of a specific field
  256. /// The records are arrays itself
  257. /// \param dest The destination matrix (every record is a vector)
  258. /// \param field The specific field name
  259. /// \param records The number of records to be read
  260. template <class T> void read(std::vector<std::vector<T>> &dest, const std::string &field, int32 records) {
  261. if (!records) {
  262. records = nrRecords;
  263. }
  264. if (VSsetfields(id, field.c_str()) == FAIL) {
  265. raiseException(STATUS_RETURN_FAIL);
  266. }
  267. int32 fieldSize = VSsizeof(id, (char *)field.c_str());
  268. if (fieldSize % sizeof(T) != 0) {
  269. raiseException(BUFFER_SIZE_NOT_DIVISIBLE);
  270. }
  271. size_t size = records * fieldSize;
  272. std::vector<uint8> buff(size);
  273. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  274. raiseException(STATUS_RETURN_FAIL);
  275. }
  276. int32 divided = fieldSize / sizeof(T);
  277. dest.resize(records, std::vector<T>(divided));
  278. std::vector<T> linearDest(records * divided);
  279. VOIDP buffptrs[1];
  280. buffptrs[0] = linearDest.data();
  281. VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs);
  282. int i = 0;
  283. int j;
  284. while (i < records) {
  285. j = 0;
  286. while (j < divided) {
  287. dest[i][j] = linearDest[i * divided + j];
  288. ++j;
  289. }
  290. ++i;
  291. }
  292. }
  293. private:
  294. intn _size;
  295. std::string name;
  296. int32 nrRecords;
  297. int32 interlace;
  298. int32 recordSize;
  299. int32 getDataType() const;
  300. };
  301. HdfItem(HdfItemBase *item, int32 sId, int32 vId);
  302. /// Holds an item object address
  303. std::unique_ptr<HdfItemBase> item;
  304. /// The file handle ids (needed by the iterator)
  305. int32 sId;
  306. int32 vId;
  307. };
  308. /// HdfItem iterator, gives the possibility to iterate over the items from the
  309. /// item
  310. class HdfItem::Iterator : public HdfObject, public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  311. public:
  312. Iterator(int32 sId, int32 vId, int32 key, int32 index, Type type, const HdfDestroyerChain &chain)
  313. : HdfObject(type, ITERATOR, chain)
  314. , sId(sId)
  315. , vId(vId)
  316. , key(key)
  317. , index(index) {
  318. }
  319. bool operator!=(const Iterator &it) {
  320. return index != it.index;
  321. }
  322. bool operator==(const Iterator &it) {
  323. return index == it.index;
  324. }
  325. Iterator &operator++() {
  326. ++index;
  327. return *this;
  328. }
  329. Iterator operator++(int) {
  330. Iterator it(*this);
  331. ++index;
  332. return it;
  333. }
  334. Iterator &operator--() {
  335. --index;
  336. return *this;
  337. }
  338. Iterator operator--(int) {
  339. Iterator it(*this);
  340. --index;
  341. return it;
  342. }
  343. HdfItem operator*() {
  344. int32 tag, ref;
  345. if (Vgettagref(key, index, &tag, &ref) == FAIL) {
  346. raiseException(OUT_OF_RANGE);
  347. }
  348. if (Visvs(key, ref)) {
  349. int32 id = VSattach(vId, ref, "r");
  350. return HdfItem(new HdfDataItem(id, chain), sId, vId);
  351. } else if (Visvg(key, ref)) {
  352. int32 id = Vattach(vId, ref, "r");
  353. return HdfItem(new HdfGroupItem(id, chain), sId, vId);
  354. } else {
  355. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  356. return HdfItem(new HdfDatasetItem(id, chain), sId, vId);
  357. }
  358. }
  359. private:
  360. int32 sId;
  361. int32 vId;
  362. int32 key;
  363. int32 index;
  364. };
  365. }
  366. #endif // HDF4CPP_HDFITEM_H