ReadingSData.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /// \copyright Copyright (c) Catalysts GmbH
  2. /// \author Patrik Kovacs, Catalysts GmbH
  3. #include <hdf4cpp/hdf.h>
  4. #include <iostream>
  5. using namespace hdf4cpp;
  6. int main() {
  7. // Open the file
  8. HdfFile file(TEST_DATA_PATH "small_test.hdf");
  9. // Get the item
  10. HdfItem item = file.get("DataWithAttributes");
  11. // Check the type
  12. if (item.getType() == SDATA) {
  13. std::cout << "Yes, the type is SData\n";
  14. } else {
  15. std::cerr << "Type missmatch\n";
  16. return 1;
  17. }
  18. // Read the whole data
  19. std::vector<float32> vec;
  20. item.read(vec);
  21. std::cout << "The whole data: ";
  22. for (const auto &value : vec) {
  23. std::cout << value << ' ';
  24. }
  25. std::cout << std::endl;
  26. // Read in a range
  27. std::vector<Range> ranges;
  28. std::vector<int32> dims = item.getDims();
  29. for (const auto &dim : dims) {
  30. if (dim >= 1) {
  31. ranges.push_back(Range(0, dim - 1));
  32. } else {
  33. ranges.push_back(Range(0, dim));
  34. }
  35. }
  36. item.read(vec, ranges);
  37. std::cout << "Data in the range [0, dim-1) if it's possible, else [0, dim): ";
  38. for (const auto &value : vec) {
  39. std::cout << value << ' ';
  40. }
  41. std::cout << std::endl;
  42. // Read an attribute
  43. HdfAttribute attribute = item.getAttribute("Integers");
  44. std::vector<int32> integers;
  45. attribute.get(integers);
  46. std::cout << "The 'Integers' attribute data: ";
  47. for (const auto &integer : integers) {
  48. std::cout << integer << ' ';
  49. }
  50. std::cout << std::endl;
  51. }