HdfFile.cpp 3.0 KB

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