HdfItem.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #include <hdf4cpp/HdfItem.h>
  5. #include <hdf/mfhdf.h>
  6. #include <sstream>
  7. hdf4cpp::HdfDatasetItem::HdfDatasetItem(int32 id) : HdfItemBase(id) {
  8. if(id == FAIL) {
  9. _size = FAIL;
  10. } else {
  11. std::vector<int32> dims = getDims();
  12. _size = 1;
  13. std::for_each(dims.begin(), dims.end(), [this](const int32 &t) {
  14. _size *= t;
  15. });
  16. int32 dim[MAX_DIMENSION];
  17. int32 size, nrAtt;
  18. char _name[MAX_NAME_LENGTH];
  19. SDgetinfo(id, _name, &size, dim, &dataType, &nrAtt);
  20. name = std::string(_name);
  21. }
  22. }
  23. std::vector<int32> hdf4cpp::HdfDatasetItem::getDims() {
  24. int32 dims[MAX_DIMENSION];
  25. int32 size, dataType, nrAtt;
  26. char name[MAX_NAME_LENGTH];
  27. SDgetinfo(id, name, &size, dims, &dataType, &nrAtt);
  28. return std::vector<int32>(dims, dims + size);
  29. }
  30. hdf4cpp::HdfAttribute hdf4cpp::HdfDatasetItem::getAttribute(const std::string &name) {
  31. return HdfAttribute(new HdfDatasetAttribute(id, name));
  32. }
  33. hdf4cpp::Type hdf4cpp::HdfDatasetItem::getType() const {
  34. return SDATA;
  35. }
  36. std::string hdf4cpp::HdfDatasetItem::getName() const {
  37. return name;
  38. }
  39. int32 hdf4cpp::HdfDatasetItem::getId() const {
  40. return id;
  41. }
  42. int32 hdf4cpp::HdfDatasetItem::getDataType() const {
  43. return dataType;
  44. }
  45. hdf4cpp::HdfDatasetItem::~HdfDatasetItem() {
  46. if(isValid()) {
  47. SDendaccess(id);
  48. }
  49. }
  50. intn hdf4cpp::HdfDatasetItem::size() const {
  51. return _size;
  52. }
  53. hdf4cpp::HdfGroupItem::HdfGroupItem(int32 id) : HdfItemBase(id) {
  54. char _name[MAX_NAME_LENGTH];
  55. Vgetname(id, _name);
  56. name = std::string(_name);
  57. }
  58. std::vector<int32> hdf4cpp::HdfGroupItem::getDims() {
  59. throw std::runtime_error("HDF4CPP: getDims not defined for HdfGroupItem");
  60. }
  61. hdf4cpp::HdfAttribute hdf4cpp::HdfGroupItem::getAttribute(const std::string &name) {
  62. return HdfAttribute(new HdfGroupAttribute(id, name));
  63. }
  64. hdf4cpp::Type hdf4cpp::HdfGroupItem::getType() const {
  65. return VGROUP;
  66. }
  67. std::string hdf4cpp::HdfGroupItem::getName() const {
  68. return name;
  69. }
  70. int32 hdf4cpp::HdfGroupItem::getId() const {
  71. return id;
  72. }
  73. hdf4cpp::HdfGroupItem::~HdfGroupItem() {
  74. if(isValid()) {
  75. Vdetach(id);
  76. }
  77. }
  78. intn hdf4cpp::HdfGroupItem::size() const {
  79. throw std::runtime_error("HdfFile: read not defined for HdfGroupItem");
  80. }
  81. int32 hdf4cpp::HdfGroupItem::getDataType() const {
  82. throw std::runtime_error("HDF4CPP: no data type for HdfGroup");
  83. }
  84. hdf4cpp::HdfDataItem::HdfDataItem(int32 id) : HdfItemBase(id) {
  85. char fieldNameList[MAX_NAME_LENGTH];
  86. char _name[MAX_NAME_LENGTH];
  87. VSinquire(id, &nrRecords, &interlace, fieldNameList, &recordSize, _name);
  88. name = std::string(_name);
  89. std::istringstream in(fieldNameList);
  90. std::string token;
  91. while(getline(in, token, ',')) {
  92. fieldNames.push_back(token);
  93. }
  94. }
  95. hdf4cpp::HdfDataItem::~HdfDataItem() {
  96. if(isValid()) {
  97. Vdetach(id);
  98. }
  99. }
  100. hdf4cpp::HdfAttribute hdf4cpp::HdfDataItem::getAttribute(const std::string &name) {
  101. return HdfAttribute(new HdfDataAttribute(id, name));
  102. }
  103. hdf4cpp::Type hdf4cpp::HdfDataItem::getType() const {
  104. return VDATA;
  105. }
  106. int32 hdf4cpp::HdfDataItem::getId() const {
  107. return id;
  108. }
  109. std::string hdf4cpp::HdfDataItem::getName() const {
  110. return name;
  111. }
  112. std::vector<int32> hdf4cpp::HdfDataItem::getDims() {
  113. std::vector<int32> dims;
  114. dims.push_back(nrRecords);
  115. dims.push_back((int32) fieldNames.size());
  116. return dims;
  117. }
  118. intn hdf4cpp::HdfDataItem::size() const {
  119. return nrRecords * (int32) fieldNames.size();
  120. }
  121. int32 hdf4cpp::HdfDataItem::getDataType() const {
  122. return 0;
  123. }
  124. hdf4cpp::HdfItem::HdfItem(HdfItem&& other) : item(std::move(other.item)), sId(other.sId), vId(other.vId) {
  125. }
  126. std::vector<int32> hdf4cpp::HdfItem::getDims() {
  127. if(isValid()) {
  128. return item->getDims();
  129. } else {
  130. return std::vector<int32>();
  131. }
  132. }
  133. hdf4cpp::HdfAttribute hdf4cpp::HdfItem::getAttribute(const std::string &name) {
  134. return item->getAttribute(name);
  135. }
  136. hdf4cpp::Type hdf4cpp::HdfItem::getType() const {
  137. if(isValid()) {
  138. return item->getType();
  139. } else {
  140. return NONE;
  141. }
  142. }
  143. std::string hdf4cpp::HdfItem::getName() const {
  144. if(isValid()) {
  145. return item->getName();
  146. } else {
  147. return std::string();
  148. }
  149. }
  150. bool hdf4cpp::HdfItem::isValid() const {
  151. return item && item->isValid();
  152. }
  153. intn hdf4cpp::HdfItem::size() const {
  154. if(isValid()) {
  155. return item->size();
  156. } else {
  157. return FAIL;
  158. }
  159. }
  160. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::begin() const {
  161. return Iterator(sId, vId, item->getId(), 0);
  162. }
  163. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::end() const {
  164. int32 size = Vntagrefs(item->getId());
  165. if(size == FAIL) {
  166. return Iterator(sId, vId, item->getId(), 0);
  167. } else {
  168. return Iterator(sId, vId, item->getId(), size);
  169. }
  170. }