HdfFile.cpp 2.9 KB

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