HdfFile.cpp 3.4 KB

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