HdfItem.h 13 KB

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