ReadingSData.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // NOLINTNEXTLINE(bugprone-exception-escape)
  7. int main() {
  8. // Open the file
  9. HdfFile file(TEST_DATA_PATH "small_test.hdf");
  10. // Get the item
  11. HdfItem item = file.get("DataWithAttributes");
  12. // Check the type
  13. if (item.getType() == SDATA) {
  14. std::cout << "Yes, the type is SData\n";
  15. } else {
  16. std::cerr << "Type missmatch\n";
  17. return 1;
  18. }
  19. // Read the whole data
  20. std::vector<float32> vec;
  21. item.read(vec);
  22. std::cout << "The whole data: ";
  23. for (const auto &value : vec) {
  24. std::cout << value << ' ';
  25. }
  26. std::cout << std::endl;
  27. // Read in a range
  28. std::vector<Range> ranges;
  29. std::vector<int32> dims = item.getDims();
  30. for (const auto &dim : dims) {
  31. if (dim >= 1) {
  32. ranges.emplace_back(0, dim - 1);
  33. } else {
  34. ranges.emplace_back(0, dim);
  35. }
  36. }
  37. item.read(vec, ranges);
  38. std::cout << "Data in the range [0, dim-1) if it's possible, else [0, dim): ";
  39. for (const auto &value : vec) {
  40. std::cout << value << ' ';
  41. }
  42. std::cout << std::endl;
  43. // Read an attribute
  44. HdfAttribute attribute = item.getAttribute("Integers");
  45. std::vector<int32> integers;
  46. attribute.get(integers);
  47. std::cout << "The 'Integers' attribute data: ";
  48. for (const auto &integer : integers) {
  49. std::cout << integer << ' ';
  50. }
  51. std::cout << std::endl;
  52. }