HdfItem.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #include <hdf4cpp/HdfItem.h>
  5. #include <hdf/mfhdf.h>
  6. #include <algorithm>
  7. #include <iostream>
  8. HdfDatasetItem::HdfDatasetItem(int32 id) : HdfItemBase(id) {
  9. if(id == FAIL) {
  10. _size = FAIL;
  11. } else {
  12. std::vector<int32> dims = getDims();
  13. _size = 1;
  14. std::for_each(dims.begin(), dims.end(), [this](const int32 &t) {
  15. _size *= t;
  16. });
  17. int32 dim[MAX_DIMENSION];
  18. int32 size, nrAtt;
  19. char _name[MAX_NAME_LENGTH];
  20. SDgetinfo(id, _name, &size, dim, &dataType, &nrAtt);
  21. name = std::string(_name);
  22. }
  23. }
  24. std::vector<int32> HdfDatasetItem::getDims() {
  25. int32 dims[MAX_DIMENSION];
  26. int32 size, dataType, nrAtt;
  27. char name[MAX_NAME_LENGTH];
  28. SDgetinfo(id, name, &size, dims, &dataType, &nrAtt);
  29. return std::vector<int32>(dims, dims + size);
  30. }
  31. bool HdfDatasetItem::read(void *dest, const std::vector<Range>& ranges) {
  32. std::vector<int32> start, quantity, stride;
  33. for(const auto& range : ranges) {
  34. start.push_back(range.begin);
  35. quantity.push_back(range.quantity);
  36. stride.push_back(range.stride);
  37. }
  38. return SDreaddata(id, start.data(), stride.data(), quantity.data(), dest) != FAIL;
  39. }
  40. HdfAttribute HdfDatasetItem::getAttribute(const std::string &name) {
  41. return HdfAttribute(new HdfDatasetAttribute(id, name));
  42. }
  43. Type HdfDatasetItem::getType() const {
  44. return DATASET;
  45. }
  46. std::string HdfDatasetItem::getName() const {
  47. return name;
  48. }
  49. int32 HdfDatasetItem::getId() const {
  50. return id;
  51. }
  52. int32 HdfDatasetItem::getDataType() const {
  53. return dataType;
  54. }
  55. HdfDatasetItem::~HdfDatasetItem() {
  56. if(isValid()) {
  57. SDendaccess(id);
  58. }
  59. }
  60. intn HdfDatasetItem::size() const {
  61. return _size;
  62. }
  63. HdfGroupItem::HdfGroupItem(int32 id) : HdfItemBase(id) {
  64. char _name[MAX_NAME_LENGTH];
  65. Vgetname(id, _name);
  66. name = std::string(_name);
  67. }
  68. std::vector<int32> HdfGroupItem::getDims() {
  69. throw std::runtime_error("HDF4CPP: getDims not defined for HdfGroupItem");
  70. }
  71. bool HdfGroupItem::read(void *, const std::vector<Range>&) {
  72. throw std::runtime_error("HDF4CPP: read not defined for HdfGroupItem");
  73. }
  74. HdfAttribute HdfGroupItem::getAttribute(const std::string &name) {
  75. return HdfAttribute(new HdfGroupAttribute(id, name));
  76. }
  77. Type HdfGroupItem::getType() const {
  78. return GROUP;
  79. }
  80. std::string HdfGroupItem::getName() const {
  81. return name;
  82. }
  83. int32 HdfGroupItem::getId() const {
  84. return id;
  85. }
  86. HdfGroupItem::~HdfGroupItem() {
  87. if(isValid()) {
  88. Vdetach(id);
  89. }
  90. }
  91. intn HdfGroupItem::size() const {
  92. throw std::runtime_error("HdfFile: read not defined for HdfGroupItem");
  93. }
  94. int32 HdfGroupItem::getDataType() const {
  95. throw std::runtime_error("HDF4CPP: no data type for HdfGroup");
  96. }
  97. HdfItem::HdfItem(HdfItem&& other) : item(std::move(other.item)), sId(other.sId), vId(other.vId) {
  98. }
  99. std::vector<int32> HdfItem::getDims() {
  100. if(isValid()) {
  101. return item->getDims();
  102. } else {
  103. return std::vector<int32>();
  104. }
  105. }
  106. HdfAttribute HdfItem::getAttribute(const std::string &name) {
  107. return item->getAttribute(name);
  108. }
  109. Type HdfItem::getType() const {
  110. if(isValid()) {
  111. return item->getType();
  112. } else {
  113. return NONE;
  114. }
  115. }
  116. std::string HdfItem::getName() const {
  117. if(isValid()) {
  118. return item->getName();
  119. } else {
  120. return std::string();
  121. }
  122. }
  123. bool HdfItem::isValid() const {
  124. return item && item->isValid();
  125. }
  126. intn HdfItem::size() const {
  127. if(isValid()) {
  128. return item->size();
  129. } else {
  130. return FAIL;
  131. }
  132. }
  133. HdfItem::Iterator HdfItem::begin() const {
  134. return Iterator(sId, vId, item->getId(), 0);
  135. }
  136. HdfItem::Iterator HdfItem::end() const {
  137. int32 size = Vntagrefs(item->getId());
  138. if(size == FAIL) {
  139. return Iterator(sId, vId, item->getId(), 0);
  140. } else {
  141. return Iterator(sId, vId, item->getId(), size);
  142. }
  143. }