HdfItem.cpp 3.5 KB

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