Skip to content

Commit f9e4791

Browse files
authored
Merge pull request #22 from KWB-R/dev
Prepare next release
2 parents fd8a249 + 7e7f990 commit f9e4791

16 files changed

+198
-95
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.Ruserdata
55
docs
66
inst/doc
7+
inst/extdata/config_tmp.xml

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: kwb.abimo
22
Title: R Package with Functions for Working with Water Balance
33
Model ABIMO
4-
Version: 0.3.0
4+
Version: 0.4.0
55
Authors@R:
66
c(person(given = "Andreas",
77
family = "Matzinger",
@@ -45,7 +45,7 @@ Remotes:
4545
Encoding: UTF-8
4646
LazyData: true
4747
Roxygen: list(markdown = TRUE)
48-
RoxygenNote: 7.2.3
48+
RoxygenNote: 7.3.1
4949
Depends:
5050
R (>= 2.10)
5151
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(extdata_file)
1717
export(get_bagrov_curves_from_abimo)
1818
export(get_xpaths)
1919
export(install_abimo)
20+
export(read_config)
2021
export(replace_value)
2122
export(run_abimo)
2223
export(run_abimo_command_line)

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Latest developments
22

3+
# kwb.abimo 0.4.0 (2024-02-09)
4+
5+
* export read_config()
6+
* deprecate private function default_config_file(), use default_config() instead
7+
* add private function github_pat()
8+
* document and export install_abimo()
9+
* add argument "token" to private functions download_assets(), get_asset_info()
10+
* add arguments "tag", "read_intermediates" to run_abimo()
11+
312
# kwb.abimo 0.3.0 (2023-10-26)
413

514
* Fix GitHub Actions workflow files (master -> v2)

R/configure.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @export
1010
create_configurator <- function(xml_file = NULL)
1111
{
12-
xml_file <- kwb.utils::defaultIfNULL(xml_file, default_config_file())
12+
xml_file <- kwb.utils::defaultIfNULL(xml_file, default_config())
1313

1414
x <- xml2::read_xml(xml_file)
1515

@@ -74,7 +74,22 @@ create_configurator <- function(xml_file = NULL)
7474
# default_config_file ----------------------------------------------------------
7575
default_config_file <- function()
7676
{
77-
system.file("extdata/config.xml", package = "kwb.abimo")
77+
kwb.utils::warningDeprecated(
78+
"default_config_file",
79+
"default_config"
80+
)
81+
}
82+
83+
# default_config ---------------------------------------------------------------
84+
85+
#' Default ABIMO config.xml path
86+
#'
87+
#' @export
88+
#' @examples
89+
#' kwb.abimo::default_config()
90+
default_config <- function()
91+
{
92+
extdata_file("config.xml")
7893
}
7994

8095
# get_xpaths -------------------------------------------------------------------

R/configure_2.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# read_config ------------------------------------------------------------------
2-
read_config <- function(file = default_config_file())
2+
3+
#' Read Abimo Configuration from XML File
4+
#'
5+
#' @param file path to XML file
6+
#' @importFrom xml2 read_xml
7+
#' @export
8+
read_config <- function(file = default_config())
39
{
410
dive_into(x = xml2::read_xml(file), file)
511
}

R/helpers.R

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ check_abimo_binary <- function(tag = latest_abimo_version())
7171
file
7272
}
7373

74-
# default_config -----------------------------------------------------------------
75-
76-
#' Default ABIMO config.xml path
77-
#'
78-
#' @export
79-
#' @examples
80-
#' kwb.abimo::default_config()
81-
default_config <- function()
82-
{
83-
extdata_file("config.xml")
84-
}
85-
8674
# get_bagrov_curves_from_abimo -------------------------------------------------
8775

8876
#' Get Bagrov curves from Abimo software
@@ -109,6 +97,11 @@ get_bagrov_curves_from_abimo <- function()
10997
))
11098
}
11199

100+
# github_pat -------------------------------------------------------------------
101+
# Provide non-exported function github_pat() from package remotes
102+
#' @importFrom utils getFromNamespace
103+
github_pat <- utils::getFromNamespace("github_pat", "remotes")
104+
112105
# latest_abimo_version ---------------------------------------------------------
113106
latest_abimo_version <- function()
114107
{

R/install_abimo.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# install_abimo ----------------------------------------------------------------
22

3+
#' Install ABIMO
4+
#'
5+
#' @param tag tag of ABIMO version to be installed, e.g. "v3.2.2"
6+
#' @param arch target system architecture, one of "windows", "linux", "macos"
7+
#' @param \dots further arguments passed to \code{kwb.abimo:::download_assets}
38
#' @importFrom archive archive_extract
4-
#' @importFrom kwb.utils catAndRun createDirectory
9+
#' @importFrom kwb.utils catAndRun createDirectory stringList
10+
#' @export
511
install_abimo <- function(
612
tag = latest_abimo_version(),
713
arch = get_architecture_suffix(),
@@ -16,7 +22,6 @@ install_abimo <- function(
1622
"'architectures': ",
1723
kwb.utils::stringList(expected_architectures),
1824
call. = FALSE
19-
2025
)
2126
}
2227

@@ -48,40 +53,35 @@ install_abimo <- function(
4853

4954
# download_assets --------------------------------------------------------------
5055

51-
#' @importFrom utils download.file getFromNamespace
56+
#' @importFrom utils download.file
5257
download_assets <- function(
5358
repo,
5459
tag,
5560
destdir = tempdir(),
5661
pattern = NULL,
5762
accept = "application/octet-stream",
58-
timeout = getOption("timeout")
63+
timeout = getOption("timeout"),
64+
token = github_pat()
5965
)
6066
{
6167
old_options <- options(timeout = timeout)
6268
on.exit(options(old_options))
6369

64-
asset_info <- get_asset_info(repo, tag)
70+
asset_info <- get_asset_info(repo, tag, token = token)
6571

6672
if (!is.null(pattern)) {
6773
asset_info <- asset_info[grepl(pattern, asset_info$name), ]
6874
}
6975

70-
# Provide non-exported function github_pat() from package remotes
71-
token <- utils::getFromNamespace("github_pat", "remotes")()
72-
73-
# Compose HTTP header (with or without token)
74-
headers <- c(
75-
if (!is.null(token)) c(Authorization = paste("token", token)),
76-
Accept = accept
77-
)
78-
7976
for (i in seq_len(nrow(asset_info))) {
8077

8178
utils::download.file(
8279
url = asset_info$url[i],
8380
destfile = file.path(destdir, asset_info$name[i]),
84-
headers = headers,
81+
headers = c(
82+
Authorization = paste("token", token),
83+
Accept = accept
84+
),
8585
mode = "wb"
8686
)
8787
}
@@ -93,11 +93,11 @@ download_assets <- function(
9393

9494
#' @importFrom gh gh
9595
#' @importFrom kwb.utils asNoFactorDataFrame selectElements
96-
get_asset_info <- function(repo, tag)
96+
get_asset_info <- function(repo, tag, token = github_pat())
9797
{
9898
url_releases <- sprintf("https://api.github.com/repos/%s/releases", repo)
9999

100-
release_info <- gh::gh(url_releases)
100+
release_info <- gh::gh(url_releases, .token = token)
101101

102102
tag_names <- sapply(release_info, kwb.utils::selectElements, "tag_name")
103103

@@ -109,7 +109,7 @@ get_asset_info <- function(repo, tag)
109109
)
110110

111111
if (!length(assets)) {
112-
stop("There are no assets for release ", tag)
112+
stop("There are no assets for release ", tag, call. = FALSE)
113113
}
114114

115115
do.call(rbind, lapply(assets, function(asset) {

R/read_abimo_intermediate_results_from_log.R

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# read_intermediate_results_from_log -------------------------------------------
2+
3+
#' Read Intermediate Results from Log File
4+
#'
5+
#' @param file path to log file
6+
#' @param pattern_remove regular expression matching lines to remove from the
7+
#' log file before looking for "variable=expression" assignments
8+
read_intermediate_results_from_log <- function(
9+
file = file.path(tempdir(), "abimo_input_result.log"),
10+
pattern_remove = "Start|unknown|angenommen|nicht definiert|std::"
11+
)
12+
{
13+
#file <- file.path(tempdir(), "abimo_input_result.log")
14+
raw_text <- readLines(file)
15+
16+
# Remove irrelevant lines
17+
text <- grep(pattern_remove, raw_text, value = TRUE, invert = TRUE)
18+
19+
# Pattern matching lines with code
20+
pattern_code <- "^\\*\\*\\* Code: "
21+
22+
# Keep only lines with code or with equal sign (variable=value)
23+
text <- grep(paste0(pattern_code, "|="), text, value = TRUE)
24+
25+
if (length(text) == 0L) {
26+
message("No intermediates found in log file. Returning NULL.")
27+
return(NULL)
28+
}
29+
30+
textblocks <- kwb.utils::extractRowRanges(
31+
text,
32+
pattern = pattern_code,
33+
startOffset = 0L
34+
)
35+
36+
# Function to get the number of elements matching a pattern
37+
n_matching <- function(x, pattern) length(grep(pattern, x))
38+
39+
# In each block all but the first line must contain the equal sign
40+
stopifnot(all(
41+
lengths(textblocks) - 1L == sapply(textblocks, n_matching, "=")
42+
))
43+
44+
# Read the codes from the first lines of the text blocks
45+
code_lines <- sapply(textblocks, "[", 1L)
46+
47+
# Name the list elements according to the codes
48+
names(textblocks) <- gsub(pattern_code, "", code_lines)
49+
50+
# Convert the text blocks with first (= code) line excluded to matrices
51+
matrices <- lapply(textblocks, function(x) {
52+
matrix(
53+
data = unlist(strsplit(x[-1L], "=")),
54+
ncol = 2,
55+
byrow = TRUE,
56+
dimnames = list(NULL, c("variable", "value"))
57+
)
58+
})
59+
60+
result <- kwb.utils::rbindAll(
61+
x = matrices,
62+
nameColumn = "code",
63+
namesAsFactor = FALSE
64+
)
65+
66+
# Convert usage string to numeric (number of letter in alphabet)
67+
is_usage <- result$variable == "ut.usage"
68+
result$value[is_usage] <- match(result$value[is_usage], LETTERS)
69+
70+
# Let "code" be the first column
71+
kwb.utils::moveColumnsToFront(result, "code")
72+
}

0 commit comments

Comments
 (0)