HdfFile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Created by patrik on 10.08.17.
  3. //
  4. #include <hdf/mfhdf.h>
  5. #include <stdexcept>
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfFile.h>
  8. hdf4cpp::HdfFile::HdfFile(const std::string& path) {
  9. sId = SDstart(path.c_str(), DFACC_READ);
  10. vId = Hopen(path.c_str(), DFACC_READ, 0);
  11. Vstart(vId);
  12. int32 loneSize = Vlone(vId, nullptr, 0);
  13. std::vector<int32> refs((size_t) loneSize);
  14. Vlone(vId, refs.data(), loneSize);
  15. for(const auto& ref : refs) {
  16. loneRefs.push_back(std::pair<int32, Type>(ref, VGROUP));
  17. }
  18. int32 loneVdata = VSlone(vId, nullptr, 0);
  19. refs.resize((size_t) loneVdata);
  20. VSlone(vId, refs.data(), loneVdata);
  21. for(const auto& ref : refs) {
  22. loneRefs.push_back(std::pair<int32, Type>(ref, VDATA));
  23. }
  24. }
  25. hdf4cpp::HdfFile::HdfFile(HdfFile&& file) {
  26. sId = file.sId;
  27. vId = file.vId;
  28. loneRefs = file.loneRefs;
  29. file.sId = file.vId = FAIL;
  30. loneRefs.clear();
  31. }
  32. hdf4cpp::HdfFile& hdf4cpp::HdfFile::operator=(HdfFile&& file) {
  33. destroy();
  34. sId = file.sId;
  35. vId = file.vId;
  36. loneRefs = file.loneRefs;
  37. file.sId = file.vId = FAIL;
  38. loneRefs.clear();
  39. return *this;
  40. }
  41. void hdf4cpp::HdfFile::destroy() {
  42. SDend(sId);
  43. Vend(vId);
  44. Hclose(vId);
  45. }
  46. hdf4cpp::HdfFile::~HdfFile() {
  47. destroy();
  48. }
  49. int32 hdf4cpp::HdfFile::getDatasetId(const std::string &name) const {
  50. int32 index = SDnametoindex(sId, name.c_str());
  51. return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
  52. }
  53. int32 hdf4cpp::HdfFile::getGroupId(const std::string &name) const {
  54. int32 ref = Vfind(vId, name.c_str());
  55. return (!ref) ? (FAIL) : (Vattach(vId, ref, "r"));
  56. }
  57. int32 hdf4cpp::HdfFile::getDataId(const std::string &name) const {
  58. int32 ref = VSfind(vId, name.c_str());
  59. return (!ref) ? (FAIL) : (VSattach(vId, ref, "r"));
  60. }
  61. hdf4cpp::HdfItem hdf4cpp::HdfFile::get(const std::string& name) const {
  62. int32 id = getDatasetId(name);
  63. if(id == FAIL) {
  64. id = getGroupId(name);
  65. if(id == FAIL) {
  66. id = getDataId(name);
  67. if(id == FAIL) {
  68. return HdfItem(nullptr, sId, vId);
  69. }
  70. return HdfItem(new HdfDataItem(id), sId, vId);
  71. }
  72. return HdfItem(new HdfGroupItem(id), sId, vId);
  73. }
  74. return HdfItem(new HdfDatasetItem(id), sId, vId);
  75. }
  76. std::vector<int32> hdf4cpp::HdfFile::getDatasetIds(const std::string &name) const {
  77. std::vector<int32> ids;
  78. char nameDataset[MAX_NAME_LENGTH];
  79. int32 datasets, attrs;
  80. SDfileinfo(sId, &datasets, &attrs);
  81. for(int32 i = 0; i < datasets; ++i) {
  82. int32 id = SDselect(sId, i);
  83. int32 rank, dimSize, nt, nAttr;
  84. SDgetinfo(id, nameDataset, &rank, &dimSize, &nt, &nAttr);
  85. if(name == std::string(nameDataset)) {
  86. ids.push_back(id);
  87. } else {
  88. SDendaccess(id);
  89. }
  90. }
  91. return ids;
  92. }
  93. std::vector<int32> hdf4cpp::HdfFile::getGroupIds(const std::string &name) const {
  94. std::vector<int32> ids;
  95. char nameGroup[MAX_NAME_LENGTH];
  96. int32 ref = Vgetid(vId, -1);
  97. while(ref != FAIL) {
  98. int32 id = Vattach(vId, ref, "r");
  99. Vgetname(id, nameGroup);
  100. if(Visvg(id, ref) && name == std::string(nameGroup)) {
  101. ids.push_back(id);
  102. } else {
  103. Vdetach(id);
  104. }
  105. ref = Vgetid(vId, ref);
  106. }
  107. return ids;
  108. }
  109. std::vector<hdf4cpp::HdfItem> hdf4cpp::HdfFile::getAll(const std::string& name) const {
  110. std::vector<HdfItem> items;
  111. std::vector<int32> ids;
  112. ids = getDatasetIds(name);
  113. for(const auto& id : ids) {
  114. items.push_back(HdfItem(new HdfDatasetItem(id), sId, vId));
  115. }
  116. ids = getGroupIds(name);
  117. for(const auto& id : ids) {
  118. items.push_back(HdfItem(new HdfGroupItem(id), sId, vId));
  119. }
  120. return std::move(items);
  121. }
  122. hdf4cpp::HdfAttribute hdf4cpp::HdfFile::getAttribute(const std::string &name) {
  123. return HdfAttribute(new HdfDatasetAttribute(sId, name));
  124. }
  125. bool hdf4cpp::HdfFile::isValid() const {
  126. return sId != FAIL || vId != FAIL;
  127. }
  128. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::begin() const {
  129. return Iterator(this, 0);
  130. }
  131. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::end() const {
  132. return Iterator(this, (int32) loneRefs.size());
  133. }