Browse Source

adding example codes

Patrik Kovacs 6 years ago
parent
commit
01287c4975
4 changed files with 135 additions and 0 deletions
  1. 3 0
      CMakeLists.txt
  2. 29 0
      examples/CMakeLists.txt
  3. 57 0
      examples/ReadingSData.cpp
  4. 46 0
      examples/ReadingVData.cpp

+ 3 - 0
CMakeLists.txt

@@ -66,6 +66,7 @@ target_link_libraries(hdf4cpp-tests
         hdf4cpp
         )
 
+
 if (NOT DEFINED TEST_DATA_PATH)
     set(TEST_DATA_PATH "${PROJECT_SOURCE_DIR}/tests/test_data/")
     message(STATUS "No path to test data defined, using ${TEST_DATA_PATH} as default")
@@ -78,3 +79,5 @@ add_test(
         COMMAND hdf4cpp-tests --gtest_output=xml:${PROJECT_BINARY_DIR}/test_details.xml
         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} # we need the serialized test data for the integration test
 )
+
+add_subdirectory(examples)

+ 29 - 0
examples/CMakeLists.txt

@@ -0,0 +1,29 @@
+project(examples)
+
+add_executable(example_sdata
+        ReadingSData.cpp
+        )
+
+target_sources(example_sdata PRIVATE
+        ReadingSData.cpp
+        )
+
+target_link_libraries(example_sdata PRIVATE
+        hdf4cpp)
+
+target_compile_definitions(example_sdata PRIVATE
+        "TEST_DATA_PATH=\"${TEST_DATA_PATH}\"")
+
+add_executable(example_vdata
+        ReadingVData.cpp
+        )
+
+target_sources(example_vdata PRIVATE
+        ReadingVData.cpp)
+
+target_link_libraries(example_vdata PRIVATE
+        hdf4cpp
+        )
+
+target_compile_definitions(example_vdata PRIVATE
+        "TEST_DATA_PATH=\"${TEST_DATA_PATH}\"")

+ 57 - 0
examples/ReadingSData.cpp

@@ -0,0 +1,57 @@
+/// \copyright Copyright (c) Catalysts GmbH
+/// \author Patrik Kovacs, Catalysts GmbH
+
+#include <hdf4cpp/hdf.h>
+#include <iostream>
+
+using namespace hdf4cpp;
+
+int main() {
+    // Open the file
+    HdfFile file(TEST_DATA_PATH "small_test.hdf");
+    // Get the item
+    HdfItem item = file.get("DataWithAttributes");
+    // Check the type
+    if (item.getType() == SDATA) {
+        std::cout << "Yes, the type is SData\n";
+    } else {
+        std::cerr << "Type missmatch\n";
+        return 1;
+    }
+
+    // Read the whole data
+    std::vector<float32> vec;
+    item.read(vec);
+    std::cout << "The whole data: ";
+    for (const auto &value : vec) {
+        std::cout << value << ' ';
+    }
+    std::cout << std::endl;
+
+    // Read in a range
+    std::vector<Range> ranges;
+    std::vector<int32> dims = item.getDims();
+    for (const auto &dim : dims) {
+        if (dim >= 1) {
+            ranges.push_back(Range(0, dim - 1));
+        } else {
+            ranges.push_back(Range(0, dim));
+        }
+    }
+    item.read(vec, ranges);
+    std::cout << "Data in the range [0, dim-1) if it's possible, else [0, dim): ";
+    for (const auto &value : vec) {
+        std::cout << value << ' ';
+    }
+    std::cout << std::endl;
+
+    // Read an attribute
+    HdfAttribute attribute = item.getAttribute("Integers");
+    std::vector<int32> integers;
+    attribute.get(integers);
+    std::cout << "The 'Integers' attribute data: ";
+    for (const auto &integer : integers) {
+        std::cout << integer << ' ';
+    }
+    std::cout << std::endl;
+}

+ 46 - 0
examples/ReadingVData.cpp

@@ -0,0 +1,46 @@
+/// \copyright Copyright (c) Catalysts GmbH
+/// \author Patrik Kovacs, Catalysts GmbH
+
+#include <hdf4cpp/hdf.h>
+#include <iostream>
+
+using namespace hdf4cpp;
+
+int main() {
+    // Open the file
+    HdfFile file(TEST_DATA_PATH "small_test.hdf");
+    // Get the item
+    HdfItem item = file.get("Vdata");
+    // Reading scalar field
+    {
+        std::vector<int32> vec;
+        item.read(vec, "age");
+        std::cout << "Age field values: ";
+        for (const auto &value : vec) {
+            std::cout << value << ' ';
+        }
+        std::cout << std::endl;
+    }
+    // Reading array field (string)
+    {
+        std::vector<std::vector<char> > vec;
+        item.read(vec, "name");
+        std::cout << "Name field values: ";
+        for(const auto& char_vec : vec) {
+            std::string string(char_vec.data());
+            std::cout << '\'' << string << "' ";
+        }
+        std::cout << std::endl;
+    }
+    // Reading attribute
+    {
+        HdfAttribute attribute = item.getAttribute("attribute");
+        std::vector<int32> vec;
+        attribute.get(vec);
+        std::cout << "Attribute values: ";
+        for(const auto& value : vec) {
+            std::cout << value << ' ';
+        }
+        std::cout << std::endl;
+    }
+}