فهرست منبع

supporting vdata attributes, test

Patrik Kovacs 6 سال پیش
والد
کامیت
d45d4d7043
6فایلهای تغییر یافته به همراه53 افزوده شده و 10 حذف شده
  1. 14 0
      include/hdf4cpp/HdfAttribute.h
  2. 1 1
      include/hdf4cpp/HdfItem.h
  3. 22 0
      lib/HdfAttribute.cpp
  4. 3 0
      lib/HdfItem.cpp
  5. 13 9
      tests/HdfFileTest.cpp
  6. BIN
      tests/test_data/small_test.hdf

+ 14 - 0
include/hdf4cpp/HdfAttribute.h

@@ -79,6 +79,20 @@ private:
     int32 getDataType() const;
 };
 
+class HdfDataAttribute : public HdfAttributeBase {
+public:
+    HdfDataAttribute(int32 id, const std::string& name);
+    Type getType() const;
+
+    intn size() const;
+
+private:
+    intn _size;
+    int32 dataType;
+    bool get(void *dest);
+    int32 getDataType() const;
+};
+
 class HdfAttribute {
 public:
     HdfAttribute(HdfAttributeBase *attribute) : attribute(attribute) {}

+ 1 - 1
include/hdf4cpp/HdfItem.h

@@ -210,7 +210,7 @@ public:
 
     intn size() const;
 
-    HdfAttribute getAttribute(const std::string &name) {}
+    HdfAttribute getAttribute(const std::string &name);
 
     template<class T>
     bool read(std::vector<T> &dest, const std::string &field, int32 records) {

+ 22 - 0
lib/HdfAttribute.cpp

@@ -66,6 +66,28 @@ int32 hdf4cpp::HdfGroupAttribute::getDataType() const {
 bool hdf4cpp::HdfGroupAttribute::get(void *dest) {
     return Vgetattr2(id, index, dest) != FAIL;
 }
+hdf4cpp::HdfDataAttribute::HdfDataAttribute(int32 id, const std::string &name) : HdfAttributeBase(id, VSfindattr(id, _HDF_VDATA, name.c_str())) {
+    if(index != FAIL) {
+        int32 nrBytes;
+        char _name[MAX_NAME_LENGTH];
+        VSattrinfo(id, _HDF_VDATA, index, _name, &dataType, &_size, &nrBytes);
+    } else {
+        dataType = 0;
+        _size = FAIL;
+    }
+}
+hdf4cpp::Type hdf4cpp::HdfDataAttribute::getType() const {
+    return VDATA;
+}
+intn hdf4cpp::HdfDataAttribute::size() const {
+    return _size;
+}
+bool hdf4cpp::HdfDataAttribute::get(void *dest) {
+    return VSgetattr(id, _HDF_VDATA, index, dest) != FAIL;
+}
+int32 hdf4cpp::HdfDataAttribute::getDataType() const {
+    return dataType;
+}
 hdf4cpp::HdfAttribute::HdfAttribute(HdfAttribute &&other) : attribute(std::move(other.attribute)) {
 }
 bool hdf4cpp::HdfAttribute::isValid() const {

+ 3 - 0
lib/HdfItem.cpp

@@ -99,6 +99,9 @@ hdf4cpp::HdfDataItem::~HdfDataItem() {
         Vdetach(id);
     }
 }
+hdf4cpp::HdfAttribute hdf4cpp::HdfDataItem::getAttribute(const std::string &name) {
+    return HdfAttribute(new HdfDataAttribute(id, name));
+}
 hdf4cpp::Type hdf4cpp::HdfDataItem::getType() const {
     return VDATA;
 }

+ 13 - 9
tests/HdfFileTest.cpp

@@ -9,9 +9,7 @@ using namespace hdf4cpp;
 
 class HdfFileTest : public ::testing::Test {
 protected:
-    HdfFileTest() : file(std::string(TEST_DATA_PATH) + "small_test.hdf"),
-                    fileModis("/home/patrik/test_data/modis/MOD021KM.A2008002.0000.006.2014221053223.hdf"),
-                    fileNew("/home/patrik/CLionProjects/Python-scripts/scripts/small_test.hdf") {}
+    HdfFileTest() : file(std::string(TEST_DATA_PATH) + "small_test.hdf") {}
 
     void writeOut(const HdfItem& item, std::ostream& out, const std::string& tab = std::string()) {
         if(!item.isValid()) return;
@@ -28,8 +26,6 @@ protected:
     }
 
     HdfFile file;
-    HdfFile fileModis;
-    HdfFile fileNew;
 };
 
 TEST_F(HdfFileTest, FileValidity) {
@@ -218,8 +214,7 @@ TEST_F(HdfFileTest, HiddenGroup) {
     ASSERT_TRUE(item.isValid());
 }
 
-TEST_F(HdfFileTest, VDataRead) {
-    ASSERT_TRUE(file.isValid());
+TEST_F(HdfFileTest, VDataRead1) {
     HdfItem item = file.get("Vdata");
     ASSERT_TRUE(item.isValid());
     std::vector<int32> vec;
@@ -227,8 +222,7 @@ TEST_F(HdfFileTest, VDataRead) {
     ASSERT_EQ(vec, std::vector<int32>({39, 19, 55}));
 }
 
-TEST_F(HdfFileTest, VDataRead1) {
-    ASSERT_TRUE(file.isValid());
+TEST_F(HdfFileTest, VDataRead2) {
     HdfItem item = file.get("Vdata");
     ASSERT_TRUE(item.isValid());
     std::vector<std::vector<char> > vec;
@@ -238,4 +232,14 @@ TEST_F(HdfFileTest, VDataRead1) {
     for(int i = 0; i < 3; ++i) {
         ASSERT_EQ(std::string(vec[i].data()), exp[i]);
     }
+}
+
+TEST_F(HdfFileTest, VDataAttributes) {
+    HdfItem item = file.get("Vdata");
+    ASSERT_TRUE(item.isValid());
+    HdfAttribute attribute = item.getAttribute("attribute");
+    ASSERT_TRUE(attribute.isValid());
+    std::vector<int32> vec;
+    ASSERT_TRUE(attribute.get(vec));
+    ASSERT_EQ(vec, std::vector<int32>({1, 2, 3, 3, 2, 1}));
 }

BIN
tests/test_data/small_test.hdf