Skip to content

Commit

Permalink
Improve reading of intermediates
Browse files Browse the repository at this point in the history
and add argument "read_intermediates" to
run_abimo()
  • Loading branch information
hsonne committed Oct 28, 2023
1 parent d1b0213 commit efc9080
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export(default_config)
export(extdata_file)
export(get_bagrov_curves_from_abimo)
export(get_xpaths)
export(install_abimo)
export(replace_value)
export(run_abimo)
export(run_abimo_command_line)
Expand All @@ -37,7 +36,6 @@ importFrom(kwb.utils,preparePdf)
importFrom(kwb.utils,printIf)
importFrom(kwb.utils,replaceFileExtension)
importFrom(kwb.utils,selectElements)
importFrom(kwb.utils,stringList)
importFrom(remotes,available_packages)
importFrom(utils,download.file)
importFrom(utils,getFromNamespace)
Expand Down
49 changes: 34 additions & 15 deletions R/read_abimo_intermediate_results_from_log.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,49 @@ read_abimo_intermediate_results_from_log <- function(
file = file.path(tempdir(), "abimo_input_result.log")
)
{
text <- readLines(file)
raw_text <- readLines(file)

# Remove informative lines
text <- grep("unknown", text, value = TRUE, invert = TRUE)
# Remove informative lines or empty lines
# e.g. "Nutzungstyp nicht definiert fuer Element"
pattern_remove <- "Start|unknown|unbekannt|Nutzungstyp|angenommen|^$"
pattern_code <- "^\\*\\*\\* Code: "

pattern <- "^\\*\\*\\* Code: "
text <- grep(pattern_remove, raw_text, value = TRUE, invert = TRUE)

starts <- grep(pattern, text)
ends <- kwb.utils::startsToEnds(starts, lastStop = length(text))
if (length(text) == 0L) {
message("No intermediates found in log file. Returning NULL.")
return(NULL)
}

textblocks <- lapply(
X = mapply(seq.int, starts, ends),
FUN = function(indices) text[indices]
textblocks <- kwb.utils::extractRowRanges(
text,
pattern = pattern_code,
startOffset = 0L
)

result <- lapply(textblocks, function(x) {
as.data.frame(matrix(
# Function to get the number of elements matching a pattern
n_matching <- function(x, pattern) length(grep(pattern, x))

# In each block all but the first line must contain the equal sign
stopifnot(all(lengths(textblocks) == sapply(textblocks, n_matching, "=")))

# Read the codes from the first lines of the text blocks
code_lines <- sapply(textblocks, "[", 1L)

# Name the list elements according to the codes
names(textblocks) <- gsub(pattern, "", code_lines)

# Convert the text blocks with first (= code) line excluded to matrices
matrices <- lapply(textblocks, function(x) {
matrix(
data = unlist(strsplit(x[-1L], "=")),
ncol = 2,
byrow = TRUE,
unlist(strsplit(grep("=", x, value = TRUE), "="))
))
dimnames = list(NULL, c("variable", "value"))
)
})

names(result) <- gsub(pattern, "code_", text[starts])
result <- kwb.utils::rbindAll(matrices, nameColumn = "code")

result
kwb.utils::moveColumnsToFront(result, "code")
}
20 changes: 13 additions & 7 deletions R/run_abimo.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
#' ignored.
#' @param tag version tag of Abimo release to be used, see
#' \url{https://github.com/KWB-R/abimo/releases}.
#' @param read_intermediates if \code{TRUE} the values of intermediate variables
#' are read from the log file (if applicable). The default is \code{FALSE}.
#' @return data frame, read from dbf file that was created by Abimo.exe.
#' Intermediate results are returned in the attribute "intermediates"
#' If \code{read_intermediates} is \code{TRUE}, intermediate results are
#' returned in the attribute "intermediates"
#' @export
run_abimo <- function(
input_file = NULL,
input_data = NULL,
output_file = NULL,
config_file = NULL,
config = NULL,
tag = latest_abimo_version()
tag = latest_abimo_version(),
read_intermediates = FALSE
)
{
if (is.null(input_file) && is.null(input_data)) {
Expand Down Expand Up @@ -69,12 +73,14 @@ run_abimo <- function(
result <- foreign::read.dbf(output_file)

# Read intermediate results from the log file
intermediates <- catAndRun(
"Reading intermediate results from ABIMO log file",
read_abimo_intermediate_results_from_log(
file = kwb.utils::replaceFileExtension(output_file, ".log")
intermediates <- if (read_intermediates) {
catAndRun(
"Reading intermediate results from ABIMO log file",
read_abimo_intermediate_results_from_log(
file = kwb.utils::replaceFileExtension(output_file, ".log")
)
)
)
} # else NULL

# Return the intermediate results as an attribute
structure(result, intermediates = intermediates)
Expand Down
22 changes: 0 additions & 22 deletions man/install_abimo.Rd

This file was deleted.

12 changes: 9 additions & 3 deletions man/run_abimo.Rd

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

0 comments on commit efc9080

Please sign in to comment.