Skip to content

Commit

Permalink
read groups from array name; #659
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jan 5, 2024
1 parent 99d6565 commit 0b8995e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ CPL_raw_to_hex <- function(raw) {
.Call(`_sf_CPL_raw_to_hex`, raw)
}

CPL_read_mdim <- function(file, array_names, groups, oo, offset, count, step, proxy = FALSE, debug = FALSE) {
.Call(`_sf_CPL_read_mdim`, file, array_names, groups, oo, offset, count, step, proxy, debug)
CPL_read_mdim <- function(file, array_names, oo, offset, count, step, proxy = FALSE, debug = FALSE) {
.Call(`_sf_CPL_read_mdim`, file, array_names, oo, offset, count, step, proxy, debug)
}

CPL_write_mdim <- function(name, driver, dimensions, variables, wkt, xy, RootGroupOptions, CreationOptions, as_float = TRUE) {
Expand Down
4 changes: 2 additions & 2 deletions R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ gdal_extract = function(f, pts, bilinear = FALSE) {
#' @param proxy logical; return proxy object?
#' @param debug logical; print debug messages?
#' @export
gdal_read_mdim = function(file, array_name = character(0), groups = character(0), options = character(0),
gdal_read_mdim = function(file, array_name = character(0), options = character(0),
offset = integer(0), count = integer(0), step = integer(0),
proxy = FALSE, debug = FALSE) {
CPL_read_mdim(file, array_name, groups, options, offset, count, step, proxy, debug)
CPL_read_mdim(file, array_name, options, offset, count, step, proxy, debug)
}

#' @rdname gdal
Expand Down
9 changes: 4 additions & 5 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,21 +923,20 @@ BEGIN_RCPP
END_RCPP
}
// CPL_read_mdim
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
RcppExport SEXP _sf_CPL_read_mdim(SEXP fileSEXP, SEXP array_namesSEXP, SEXP groupsSEXP, SEXP ooSEXP, SEXP offsetSEXP, SEXP countSEXP, SEXP stepSEXP, SEXP proxySEXP, SEXP debugSEXP) {
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
RcppExport SEXP _sf_CPL_read_mdim(SEXP fileSEXP, SEXP array_namesSEXP, SEXP ooSEXP, SEXP offsetSEXP, SEXP countSEXP, SEXP stepSEXP, SEXP proxySEXP, SEXP debugSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< CharacterVector >::type file(fileSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type array_names(array_namesSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type groups(groupsSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type oo(ooSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type offset(offsetSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type count(countSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type step(stepSEXP);
Rcpp::traits::input_parameter< bool >::type proxy(proxySEXP);
Rcpp::traits::input_parameter< bool >::type debug(debugSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, groups, oo, offset, count, step, proxy, debug));
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, oo, offset, count, step, proxy, debug));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -1524,7 +1523,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_line_interpolate", (DL_FUNC) &_sf_CPL_line_interpolate, 3},
{"_sf_CPL_hex_to_raw", (DL_FUNC) &_sf_CPL_hex_to_raw, 1},
{"_sf_CPL_raw_to_hex", (DL_FUNC) &_sf_CPL_raw_to_hex, 1},
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 9},
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 8},
{"_sf_CPL_write_mdim", (DL_FUNC) &_sf_CPL_write_mdim, 9},
{"_sf_opp_sfc", (DL_FUNC) &_sf_opp_sfc, 4},
{"_sf_normalize_sfc", (DL_FUNC) &_sf_normalize_sfc, 4},
Expand Down
96 changes: 45 additions & 51 deletions src/mdim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,28 @@ List get_all_arrays(std::shared_ptr<GDALGroup> curGroup, List ret, std::string n
return ret;
}

std::shared_ptr<GDALMDArray> get_array(std::shared_ptr<GDALGroup> grp, const std::string &osName) {
CPLStringList aosTokens(CSLTokenizeString2(osName.c_str(), "/", 0));
for (int i = 0; i < aosTokens.size() - 1; i++) {
auto curGroupNew = grp->OpenGroup(aosTokens[i]);
if (!curGroupNew) {
Rcout << "Cannot find group " << aosTokens[i] << std::endl;
stop("group not found");
}
grp = curGroupNew;
}
const char *pszArrayName = aosTokens[aosTokens.size() - 1];
auto array(grp->OpenMDArray(pszArrayName));
if (!array) {
Rcout << "Cannot open array" << pszArrayName << std::endl;
stop("array not found");
}
return array;
}

// [[Rcpp::export]]
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups,
CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step,
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo,
IntegerVector offset, IntegerVector count, IntegerVector step,
bool proxy = false, bool debug = false) {

std::vector <char *> oo_char = create_options(oo, true); // open options
Expand All @@ -202,35 +221,6 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
}

auto curGroup = poRootGroup;
if (groups.size() > 0) {
for (int i = 0; i < groups.size(); i++) {
curGroup = curGroup->OpenGroup(Rcpp::as<std::string>(groups[i]));
if (curGroup == nullptr) {
Rcout << "group: " << groups[i] << " ";
stop("cannot open group: does it exist?");
}
if (debug)
Rcout << groups[i] << " ";
}
Rcout << std::endl;
} else {
auto groupNames = poRootGroup->GetGroupNames();
if (groupNames.size() > 0) {
curGroup = curGroup->OpenGroup(groupNames[0]);
if (!curGroup) {
Rcout << "group: " << groupNames[0] << ";" << std::endl;
stop("Cannot find group");
}
if (debug && groupNames.size() > 1) {
Rcout << "ignored groups: ";
for (size_t i = 1; i < groupNames.size(); i++)
Rcout << groupNames[i] << " ";
Rcout << std::endl;
}
} else if (debug)
Rcout << "using root group" << std::endl;
}

// find possible vector geometry array, and construct
List geometry = get_geometry(curGroup);

Expand All @@ -257,7 +247,7 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV

const char *name = array_names[0];
std::shared_ptr<GDALMDArray> array;
array = curGroup->OpenMDArray(name);
array = get_array(curGroup, name);
if (!array)
stop("Cannot find array");
if (offset.size() != 0 && (size_t) offset.size() != array->GetDimensionCount())
Expand Down Expand Up @@ -312,7 +302,7 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
for (int i = 0; i < n; i++) {
name = array_names[i];
a_names[i] = array_names[i];
auto arr(curGroup->OpenMDArray(name));
auto arr(get_array(curGroup, name));
dims.attr("names") = dim_names;
dimensions.attr("names") = dim_names;
NumericVector vec;
Expand All @@ -326,24 +316,28 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
nullptr, /* stride: default to row-major convention */
GDALExtendedDataType::Create(GDT_Float64),
vec_.begin());
if (!ok)
Rcout << "Read failed for array " << name << std::endl;
bool has_offset = false;
double offst = arr->GetOffset(&has_offset);
if (!has_offset)
offst = 0.0;
bool has_scale = false;
double scale = arr->GetScale(&has_scale);
if (!has_scale)
scale = 1.0;
bool has_nodata = false;
double nodata_value = arr->GetNoDataValueAsDouble(&has_nodata);
if (has_offset || has_scale || has_nodata) {
for (size_t i = 0; i < nValues; i++) {
if (ISNAN(vec_[i]) || (has_nodata && vec_[i] == nodata_value))
vec_[i] = NA_REAL;
else
vec_[i] = vec_[i] * scale + offst;
if (!ok) {
Rcout << "Cannot read array into a Float64 buffer" << name << std::endl;
for (size_t i = 0; i < nValues; i++)
vec_[i] = NA_REAL;
} else {
bool has_offset = false;
double offst = arr->GetOffset(&has_offset);
if (!has_offset)
offst = 0.0;
bool has_scale = false;
double scale = arr->GetScale(&has_scale);
if (!has_scale)
scale = 1.0;
bool has_nodata = false;
double nodata_value = arr->GetNoDataValueAsDouble(&has_nodata);
if (has_offset || has_scale || has_nodata) {
for (size_t i = 0; i < nValues; i++) {
if (ISNAN(vec_[i]) || (has_nodata && vec_[i] == nodata_value))
vec_[i] = NA_REAL;
else
vec_[i] = vec_[i] * scale + offst;
}
}
}
vec_.attr("dim") = dims;
Expand Down

0 comments on commit 0b8995e

Please sign in to comment.