Skip to content

Commit

Permalink
Check if correct parameters are used.
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Oct 1, 2018
1 parent 66ea4e9 commit 86e1957
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
41 changes: 40 additions & 1 deletion R/RSAGA-core.R
Expand Up @@ -857,6 +857,9 @@ rsaga.html.help = function(lib, module=NULL, use.program.folder = TRUE, env=rsag
#' @param check.module.exists logical (default: `TRUE`): call [rsaga.module.exists()] to determine if the specified module can be called in the current SAGA installation
#' @param warn logical (default: `TRUE`): for internal purposes - can be used to suppress warning messages generated by failed SAGA_CMD calls; currently used by [rsaga.get.lib.modules()] and related functions; see [options()] argument `warn` for details
#' @param argsep character (default: `" "`; currently for internal use): defines the character symbol used as a separator between each argument name and argument value passed to `saga_cmd`. SAGA GIS 2.1.0 (RC1) seems to move toward `"="` as a separator, but `" "` still works and some modules (e.g. the used by `rsaga.pisr`) don't seem to work with `argsep="="`. Future releases of RSAGA may change the default `argsep` value and/or delete or ignore this argument and/or move it to [rsaga.env()].
#'
#' @param check.parameters logical(default: `TRUE`): Check if correct parameters are used.
#'
#' @param ... Additional arguments to be passed to [base::system()].
#'
#' @details This workhorse function establishes the interface between the SAGA command line program and R by submitting a system call. This is a low-level function that may be used for directly accessing SAGA; specific functions such as `rsaga.hillshade` are intended to be more user-friendly interfaces to the most frequently used SAGA modules. These higher-level interfaces support default values for the arguments and perform some error checking; they should therefore be preferred if available.
Expand Down Expand Up @@ -899,7 +902,7 @@ rsaga.geoprocessor = function(
prefix = NULL, flags = ifelse(show.output.on.console,"q","s"), cores,
env = rsaga.env(), display.command = FALSE, reduce.intern = TRUE,
check.module.exists = TRUE, warn = options("warn")$warn,
argsep = " ", ... )
argsep = " ", check.parameters = TRUE, ... )
{
# Issue warning if using SAGA GIS version that has not been tested with RSAGA:
if (!is.null(env$version)) {
Expand Down Expand Up @@ -988,6 +991,42 @@ rsaga.geoprocessor = function(
}
}

# Check parameters
if(check.parameters) {
# Some rsaga core calls need to suppress warnings
# related to non-zero exit codes of saga_cmd:
oldwarn = options("warn")$warn
on.exit(options(warn = oldwarn), add = TRUE)
options(warn = -1)

# Get console output
res <- system(paste0(command, " \"", module, "\""), intern = TRUE)
options(warn = oldwarn)

# Get parameters
i <- grep("Usage:", res)

# Extract parameter list
unlist(str_extract_all(res[i], "\\[(.*?)\\]")) %>%
str_remove("\\[-") %>%
str_remove("\\]") %>%
str_remove("<(.*?)>") %>%
str_remove("\\s") -> param_list

# Print error message
if(!all(names(params) %in% param_list)) {
#Get false parameter names
false_paramters <- names(params) %in% param_list
# Get all paramter names
paramters <- names(params)
stop(paste0("Wrong paramters used: ",
paste(paramters[!false_paramter], collapse = " "),
". ",
"Possible parameters:",
paste(param_list, collapse = " ")))
}
}

if (is.character(module)) module = shQuote(module)
command = paste(command, module)
if (length(param)>0) {
Expand Down
6 changes: 4 additions & 2 deletions man/rsaga.geoprocessor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 86e1957

Please sign in to comment.