123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- //
- // Created by patrik on 10.08.17.
- //
- #include <hdf/mfhdf.h>
- #include <stdexcept>
- #include <hdf4cpp/HdfDefines.h>
- #include <hdf4cpp/HdfFile.h>
- HdfFile::HdfFile(const std::string& path) {
- sId = SDstart(path.c_str(), DFACC_READ);
- vId = Hopen(path.c_str(), DFACC_READ, 0);
- Vstart(vId);
- }
- HdfFile::~HdfFile() {
- SDend(sId);
- Vend(vId);
- Hclose(vId);
- }
- int32 HdfFile::getDatasetId(const std::string &name) {
- int32 index = SDnametoindex(sId, name.c_str());
- return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
- }
- int32 HdfFile::getGroupId(const std::string &name) {
- int32 ref = Vfind(vId, name.c_str());
- return (!ref) ? (FAIL) : (Vattach(vId, ref, "r"));
- }
- HdfItem HdfFile::get(const std::string& name) {
- int32 id = getDatasetId(name);
- if(id == FAIL) {
- id = getGroupId(name);
- if(id == FAIL) {
- return HdfItem(nullptr, sId, vId);
- }
- return HdfItem(new HdfGroupItem(id), sId, vId);
- }
- return HdfItem(new HdfDatasetItem(id), sId, vId);
- }
- std::vector<int32> HdfFile::getDatasetIds(const std::string &name) {
- std::vector<int32> ids;
- char nameDataset[MAX_NAME_LENGTH];
- int32 datasets, attrs;
- SDfileinfo(sId, &datasets, &attrs);
- for(int32 i = 0; i < datasets; ++i) {
- int32 id = SDselect(sId, i);
- int32 rank, dimSize, nt, nAttr;
- SDgetinfo(id, nameDataset, &rank, &dimSize, &nt, &nAttr);
- if(name == std::string(nameDataset)) {
- ids.push_back(id);
- } else {
- SDendaccess(id);
- }
- }
- return ids;
- }
- std::vector<int32> HdfFile::getGroupIds(const std::string &name) {
- std::vector<int32> ids;
- char nameGroup[MAX_NAME_LENGTH];
- int32 ref = Vgetid(vId, -1);
- while(ref != FAIL) {
- int32 id = Vattach(vId, ref, "r");
- Vgetname(id, nameGroup);
- if(Visvg(id, ref) && name == std::string(nameGroup)) {
- ids.push_back(id);
- } else {
- Vdetach(id);
- }
- ref = Vgetid(vId, ref);
- }
- return ids;
- }
- std::vector<HdfItem> HdfFile::getAll(const std::string& name) {
- std::vector<HdfItem> items;
- std::vector<int32> ids;
- ids = getDatasetIds(name);
- for(const auto& id : ids) {
- items.push_back(HdfItem(new HdfDatasetItem(id), sId, vId));
- }
- ids = getGroupIds(name);
- for(const auto& id : ids) {
- items.push_back(HdfItem(new HdfGroupItem(id), sId, vId));
- }
- return std::move(items);
- }
- HdfAttribute HdfFile::getAttribute(const std::string &name) {
- return HdfAttribute(new HdfDatasetAttribute(sId, name));
- }
- bool HdfFile::isValid() {
- return sId != FAIL || vId != FAIL;
- }
- HdfItem::Iterator HdfFile::begin() const {
- return HdfItem::Iterator(sId, vId, vId, 0);
- }
- HdfItem::Iterator HdfFile::end() const {
- // FIXME get the number of items in the root
- int32 size = Vntagrefs(vId);
- if(size == FAIL) {
- size = 0;
- }
- return HdfItem::Iterator(sId, vId, vId, size);
- }
|