HdfFile.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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() {
  26. SDend(sId);
  27. Vend(vId);
  28. Hclose(vId);
  29. }
  30. int32 hdf4cpp::HdfFile::getDatasetId(const std::string &name) {
  31. int32 index = SDnametoindex(sId, name.c_str());
  32. return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
  33. }
  34. int32 hdf4cpp::HdfFile::getGroupId(const std::string &name) {
  35. int32 ref = Vfind(vId, name.c_str());
  36. return (!ref) ? (FAIL) : (Vattach(vId, ref, "r"));
  37. }
  38. int32 hdf4cpp::HdfFile::getDataId(const std::string &name) {
  39. int32 ref = VSfind(vId, name.c_str());
  40. return (!ref) ? (FAIL) : (VSattach(vId, ref, "r"));
  41. }
  42. hdf4cpp::HdfItem hdf4cpp::HdfFile::get(const std::string& name) {
  43. int32 id = getDatasetId(name);
  44. if(id == FAIL) {
  45. id = getGroupId(name);
  46. if(id == FAIL) {
  47. id = getDataId(name);
  48. if(id == FAIL) {
  49. return HdfItem(nullptr, sId, vId);
  50. }
  51. return HdfItem(new HdfDataItem(id), sId, vId);
  52. }
  53. return HdfItem(new HdfGroupItem(id), sId, vId);
  54. }
  55. return HdfItem(new HdfDatasetItem(id), sId, vId);
  56. }
  57. std::vector<int32> hdf4cpp::HdfFile::getDatasetIds(const std::string &name) {
  58. std::vector<int32> ids;
  59. char nameDataset[MAX_NAME_LENGTH];
  60. int32 datasets, attrs;
  61. SDfileinfo(sId, &datasets, &attrs);
  62. for(int32 i = 0; i < datasets; ++i) {
  63. int32 id = SDselect(sId, i);
  64. int32 rank, dimSize, nt, nAttr;
  65. SDgetinfo(id, nameDataset, &rank, &dimSize, &nt, &nAttr);
  66. if(name == std::string(nameDataset)) {
  67. ids.push_back(id);
  68. } else {
  69. SDendaccess(id);
  70. }
  71. }
  72. return ids;
  73. }
  74. std::vector<int32> hdf4cpp::HdfFile::getGroupIds(const std::string &name) {
  75. std::vector<int32> ids;
  76. char nameGroup[MAX_NAME_LENGTH];
  77. int32 ref = Vgetid(vId, -1);
  78. while(ref != FAIL) {
  79. int32 id = Vattach(vId, ref, "r");
  80. Vgetname(id, nameGroup);
  81. if(Visvg(id, ref) && name == std::string(nameGroup)) {
  82. ids.push_back(id);
  83. } else {
  84. Vdetach(id);
  85. }
  86. ref = Vgetid(vId, ref);
  87. }
  88. return ids;
  89. }
  90. std::vector<hdf4cpp::HdfItem> hdf4cpp::HdfFile::getAll(const std::string& name) {
  91. std::vector<HdfItem> items;
  92. std::vector<int32> ids;
  93. ids = getDatasetIds(name);
  94. for(const auto& id : ids) {
  95. items.push_back(HdfItem(new HdfDatasetItem(id), sId, vId));
  96. }
  97. ids = getGroupIds(name);
  98. for(const auto& id : ids) {
  99. items.push_back(HdfItem(new HdfGroupItem(id), sId, vId));
  100. }
  101. return std::move(items);
  102. }
  103. hdf4cpp::HdfAttribute hdf4cpp::HdfFile::getAttribute(const std::string &name) {
  104. return HdfAttribute(new HdfDatasetAttribute(sId, name));
  105. }
  106. bool hdf4cpp::HdfFile::isValid() {
  107. return sId != FAIL || vId != FAIL;
  108. }
  109. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::begin() const {
  110. return Iterator(this, 0);
  111. }
  112. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::end() const {
  113. return Iterator(this, (int32) loneRefs.size());
  114. }