|
1 | 1 | #include "Product.h"
|
2 | 2 | #include <isce3/product/Serialization.h>
|
3 | 3 |
|
| 4 | +/** Find unique group path excluding repeated occurrences |
| 5 | +*/ |
| 6 | +std::vector<std::string> _findGroupPath(isce3::io::IGroup& group, |
| 7 | + const std::string group_name) { |
| 8 | + |
| 9 | + auto group_vector = group.find(group_name, ".", "GROUP"); |
| 10 | + |
| 11 | + std::vector<std::string> filtered_group_vector; |
| 12 | + |
| 13 | + // Filter unique group paths |
| 14 | + for (const auto& full_group_path: group_vector) { |
| 15 | + std::size_t group_name_position = full_group_path.find(group_name); |
| 16 | + |
| 17 | + // Get base group path (remove sub-groups) |
| 18 | + std::string base_group = full_group_path.substr( |
| 19 | + 0, group_name_position + group_name.length()); |
| 20 | + |
| 21 | + /* If base group is not in filtered_group_vector, |
| 22 | + append it to the vector |
| 23 | + */ |
| 24 | + if (find(filtered_group_vector.begin(), |
| 25 | + filtered_group_vector.end(), |
| 26 | + base_group) == filtered_group_vector.end()) { |
| 27 | + filtered_group_vector.push_back(base_group); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return filtered_group_vector; |
| 32 | +} |
| 33 | + |
| 34 | + |
4 | 35 | /** @param[in] file IH5File object for product. */
|
5 | 36 | isce3::product::Product::
|
6 | 37 | Product(isce3::io::IH5File & file) {
|
7 |
| - // Get swaths group |
8 |
| - isce3::io::IGroup imGroup = file.openGroup("/science/LSAR/SLC/swaths"); |
| 38 | + |
| 39 | + std::string base_dir = "/science/"; |
| 40 | + |
| 41 | + isce3::io::IGroup base_group = file.openGroup(base_dir); |
| 42 | + |
| 43 | + bool flag_has_swaths = false; |
| 44 | + |
| 45 | + /* |
| 46 | + In this section, we look for grids or swaths group within base_group. |
| 47 | + We start by assigning an empty string to image_group_str in case |
| 48 | + grids and swaths group are not found. |
| 49 | + */ |
| 50 | + std::vector<std::string> key_vector = {"grids", "swaths"}; |
| 51 | + std::string image_group_str = "", metadata_group_str; |
| 52 | + for (const auto& key: key_vector) { |
| 53 | + |
| 54 | + // Look for HDF5 groups that match key (i.e., "grids" or "swaths") |
| 55 | + auto group_vector = _findGroupPath(base_group, key); |
| 56 | + |
| 57 | + if (group_vector.size() > 1) { |
| 58 | + /* |
| 59 | + NISAR products should have only one grids or swaths group |
| 60 | + within base_group: |
| 61 | + */ |
| 62 | + std::string error_msg = ("ERROR there should be at most one " + |
| 63 | + key + " group in " +file.getFileName()); |
| 64 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_msg); |
| 65 | + } |
| 66 | + else if (group_vector.size() > 0) { |
| 67 | + |
| 68 | + // Group swaths or grid was found |
| 69 | + |
| 70 | + /* |
| 71 | + Assign the group (grids or swaths) to image_group_str, |
| 72 | + locate the group metadata by substituting "grids"/"swaths" with |
| 73 | + "metadata", and save it on metadata_group_str |
| 74 | + */ |
| 75 | + image_group_str = base_dir + group_vector[0]; |
| 76 | + metadata_group_str = image_group_str; |
| 77 | + std::size_t key_position = metadata_group_str.rfind(key); |
| 78 | + metadata_group_str.replace(key_position, key.length(), "metadata"); |
| 79 | + if (key == "swaths") { |
| 80 | + /* |
| 81 | + flag_has_swaths indicates if image_group_str reffers to swaths or |
| 82 | + grids |
| 83 | + */ |
| 84 | + flag_has_swaths = true; |
| 85 | + } |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // If did not find HDF5 groups swaths or grids |
| 91 | + if (image_group_str.size() == 0) { |
| 92 | + std::string error_msg = ("ERROR swaths and grids groups" |
| 93 | + " not found in " + file.getFileName()); |
| 94 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_msg); |
| 95 | + } |
| 96 | + |
| 97 | + // Get swaths/grids group |
| 98 | + isce3::io::IGroup imGroup = file.openGroup(image_group_str); |
| 99 | + |
9 | 100 | // Configure swaths
|
10 |
| - loadFromH5(imGroup, _swaths); |
| 101 | + if (flag_has_swaths) { |
| 102 | + loadFromH5(imGroup, _swaths); |
| 103 | + } |
| 104 | + /* |
| 105 | + Not implemented yet: |
| 106 | + else { |
| 107 | + loadFromH5(imGroup, _grids); |
| 108 | + } |
| 109 | + */ |
| 110 | + |
11 | 111 | // Get metadata group
|
12 |
| - isce3::io::IGroup metaGroup = file.openGroup("/science/LSAR/SLC/metadata"); |
| 112 | + isce3::io::IGroup metaGroup = file.openGroup(metadata_group_str); |
13 | 113 | // Configure metadata
|
| 114 | + |
14 | 115 | loadFromH5(metaGroup, _metadata);
|
| 116 | + |
15 | 117 | // Get look direction
|
| 118 | + auto identification_vector = _findGroupPath( |
| 119 | + base_group, "identification"); |
| 120 | + if (identification_vector.size() == 0) { |
| 121 | + std::string error_msg = ("ERROR identification group not found in " + |
| 122 | + file.getFileName()); |
| 123 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_msg); |
| 124 | + } |
| 125 | + else if (identification_vector.size() > 1) { |
| 126 | + std::string error_msg = ("ERROR there should be only one identification" |
| 127 | + " group in " + file.getFileName()); |
| 128 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_msg); |
| 129 | + } |
| 130 | + |
| 131 | + std::string identification_group_str = base_dir + identification_vector[0]; |
16 | 132 | std::string lookDir;
|
17 |
| - isce3::io::loadFromH5(file, "/science/LSAR/identification/lookDirection", lookDir); |
| 133 | + isce3::io::loadFromH5(file, identification_group_str + "/lookDirection", |
| 134 | + lookDir); |
18 | 135 | lookSide(lookDir);
|
| 136 | + |
19 | 137 | // Save the filename
|
20 | 138 | _filename = file.filename();
|
| 139 | + |
21 | 140 | }
|
0 commit comments