HdfItem.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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::Type hdf4cpp::HdfDataItem::getType() const {
  101. return VDATA;
  102. }
  103. int32 hdf4cpp::HdfDataItem::getId() const {
  104. return id;
  105. }
  106. std::string hdf4cpp::HdfDataItem::getName() const {
  107. return name;
  108. }
  109. std::vector<int32> hdf4cpp::HdfDataItem::getDims() {
  110. std::vector<int32> dims;
  111. dims.push_back(nrRecords);
  112. dims.push_back((int32) fieldNames.size());
  113. return dims;
  114. }
  115. intn hdf4cpp::HdfDataItem::size() const {
  116. return nrRecords * (int32) fieldNames.size();
  117. }
  118. int32 hdf4cpp::HdfDataItem::getDataType() const {
  119. return 0;
  120. }
  121. hdf4cpp::HdfItem::HdfItem(HdfItem&& other) : item(std::move(other.item)), sId(other.sId), vId(other.vId) {
  122. }
  123. std::vector<int32> hdf4cpp::HdfItem::getDims() {
  124. if(isValid()) {
  125. return item->getDims();
  126. } else {
  127. return std::vector<int32>();
  128. }
  129. }
  130. hdf4cpp::HdfAttribute hdf4cpp::HdfItem::getAttribute(const std::string &name) {
  131. return item->getAttribute(name);
  132. }
  133. hdf4cpp::Type hdf4cpp::HdfItem::getType() const {
  134. if(isValid()) {
  135. return item->getType();
  136. } else {
  137. return NONE;
  138. }
  139. }
  140. std::string hdf4cpp::HdfItem::getName() const {
  141. if(isValid()) {
  142. return item->getName();
  143. } else {
  144. return std::string();
  145. }
  146. }
  147. bool hdf4cpp::HdfItem::isValid() const {
  148. return item && item->isValid();
  149. }
  150. intn hdf4cpp::HdfItem::size() const {
  151. if(isValid()) {
  152. return item->size();
  153. } else {
  154. return FAIL;
  155. }
  156. }
  157. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::begin() const {
  158. return Iterator(sId, vId, item->getId(), 0);
  159. }
  160. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::end() const {
  161. int32 size = Vntagrefs(item->getId());
  162. if(size == FAIL) {
  163. return Iterator(sId, vId, item->getId(), 0);
  164. } else {
  165. return Iterator(sId, vId, item->getId(), size);
  166. }
  167. }