@@ -147,9 +147,41 @@ List get_geometry(std::shared_ptr<GDALGroup> curGroup) {
147
147
return (lst);
148
148
}
149
149
150
+ List get_all_arrays (std::shared_ptr<GDALGroup> curGroup, List ret, std::string name) {
151
+ auto array_names (curGroup->GetMDArrayNames ());
152
+ // for (size_t i = 0 i < array_names
153
+ CharacterVector a (array_names.size ());
154
+ // ret needs to be a _named_ list, so that na is never null
155
+ CharacterVector na = ret.attr (" names" );
156
+ if (a.size () > 0 ) { // group with array(s):
157
+ for (int i = 0 ; i < a.size (); i++)
158
+ a[i] = array_names[i];
159
+ ret.push_back (a);
160
+ CharacterVector gn;
161
+ // gn.push_back("");
162
+ std::string group_name;
163
+ if (name == " /" )
164
+ group_name = name;
165
+ else
166
+ group_name = name + " /" ;
167
+ na.push_back (group_name);
168
+ }
169
+ ret.attr (" names" ) = na;
170
+ auto gn (curGroup->GetGroupNames ());
171
+ for (const auto &gn: curGroup->GetGroupNames ()) { // iterate over groups:
172
+ std::string slash;
173
+ if (name == " /" )
174
+ slash = " " ;
175
+ else
176
+ slash = " /" ;
177
+ ret = get_all_arrays (curGroup->OpenGroup (gn), ret, name + slash + gn);
178
+ }
179
+ return ret;
180
+ }
181
+
150
182
// [[Rcpp::export]]
151
- List CPL_read_mdim (CharacterVector file, CharacterVector array_names, CharacterVector oo ,
152
- IntegerVector offset, IntegerVector count, IntegerVector step,
183
+ List CPL_read_mdim (CharacterVector file, CharacterVector array_names, CharacterVector groups ,
184
+ CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step,
153
185
bool proxy = false , bool debug = false ) {
154
186
155
187
std::vector <char *> oo_char = create_options (oo, true ); // open options
@@ -163,22 +195,41 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
163
195
if ( !poRootGroup )
164
196
stop (" cannot open root group" );
165
197
198
+ if (array_names.size () == 1 && array_names[0 ] == " ?" ) {
199
+ List l;
200
+ l.attr (" names" ) = CharacterVector::create ();
201
+ return get_all_arrays (poRootGroup, l, poRootGroup->GetName ());
202
+ }
203
+
166
204
auto curGroup = poRootGroup;
167
- auto groupNames = poRootGroup->GetGroupNames ();
168
- if (groupNames.size () > 0 ) {
169
- curGroup = curGroup->OpenGroup (groupNames[0 ]);
170
- if (!curGroup) {
171
- Rcout << " group: " << groupNames[0 ] << " ;" << std::endl;
172
- stop (" Cannot find group" );
173
- }
174
- if (debug && groupNames.size () > 1 ) {
175
- Rcout << " ignored groups: " ;
176
- for (size_t i = 1 ; i < groupNames.size (); i++)
177
- Rcout << groupNames[i] << " " ;
178
- Rcout << std::endl;
205
+ if (groups.size () > 0 ) {
206
+ for (int i = 0 ; i < groups.size (); i++) {
207
+ curGroup = curGroup->OpenGroup (Rcpp::as<std::string>(groups[i]));
208
+ if (curGroup == nullptr ) {
209
+ Rcout << " group: " << groups[i] << " " ;
210
+ stop (" cannot open group: does it exist?" );
211
+ }
212
+ if (debug)
213
+ Rcout << groups[i] << " " ;
179
214
}
180
- } else if (debug)
181
- Rcout << " using root group" << std::endl;
215
+ Rcout << std::endl;
216
+ } else {
217
+ auto groupNames = poRootGroup->GetGroupNames ();
218
+ if (groupNames.size () > 0 ) {
219
+ curGroup = curGroup->OpenGroup (groupNames[0 ]);
220
+ if (!curGroup) {
221
+ Rcout << " group: " << groupNames[0 ] << " ;" << std::endl;
222
+ stop (" Cannot find group" );
223
+ }
224
+ if (debug && groupNames.size () > 1 ) {
225
+ Rcout << " ignored groups: " ;
226
+ for (size_t i = 1 ; i < groupNames.size (); i++)
227
+ Rcout << groupNames[i] << " " ;
228
+ Rcout << std::endl;
229
+ }
230
+ } else if (debug)
231
+ Rcout << " using root group" << std::endl;
232
+ }
182
233
183
234
// find possible vector geometry array, and construct
184
235
List geometry = get_geometry (curGroup);
@@ -199,11 +250,14 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
199
250
if (ndim == largest_size)
200
251
array_names.push_back (an);
201
252
}
253
+ if (array_names.size () == 0 )
254
+ stop (" no array names found" );
202
255
}
203
256
int n = array_names.size ();
204
257
205
258
const char *name = array_names[0 ];
206
- auto array (curGroup->OpenMDArray (name));
259
+ std::shared_ptr<GDALMDArray> array;
260
+ array = curGroup->OpenMDArray (name);
207
261
if (!array)
208
262
stop (" Cannot find array" );
209
263
if (offset.size () != 0 && (size_t ) offset.size () != array->GetDimensionCount ())
0 commit comments