Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not execute package creation script #1

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b2790b5
Do not execute package creation script
daniel-wicke Nov 5, 2024
0886cef
config- und input-Daten für ersten Modellabschnitt
daniel-wicke Nov 7, 2024
94ec111
Fix to be in line with R package structure
mrustl Nov 8, 2024
ef7e8fa
Add @daniel-wicke as package author
mrustl Nov 8, 2024
4cb11c5
correction of 2 section_out_id
daniel-wicke Nov 8, 2024
d9dd158
Wuhle-EZG Regen für kwb.DWD
daniel-wicke Nov 12, 2024
8c03128
fisbroker.R added
daniel-wicke Nov 13, 2024
8711f91
Move to R scripts
mrustl Nov 13, 2024
467bb9d
Add overview of DWD stations for Wuhle EZG
mrustl Nov 13, 2024
6ab4a8c
Add step to get DWD daily precipitation data
mrustl Nov 13, 2024
1c45f1b
Import config and input (flow data)
mrustl Nov 14, 2024
6a0f04c
Add input WWTP flow data
mrustl Nov 14, 2024
4edad61
Merge branch 'dev' of https://github.com/KWB-R/kwb.BerlinWaterModel i…
mrustl Nov 14, 2024
860074f
Import WW abstraction and calculate m3/s
mrustl Nov 14, 2024
139d60d
Merge branch 'dev' of https://github.com/KWB-R/kwb.BerlinWaterModel i…
mrustl Nov 14, 2024
684578b
Add plot for flow network (inflows+flow sections
mrustl Nov 15, 2024
a3fa8e0
Add node scaling based on volume of section
mrustl Nov 15, 2024
ce5b152
Extend complex graph for all flows
mrustl Nov 15, 2024
92f5c7b
corrected ID (GoK_3.41 instead of GoK_26.5)
daniel-wicke Nov 15, 2024
d7e4bd7
:bug: fix
daniel-wicke Nov 15, 2024
55f9a86
Fix multiple outflow :bug: reported by @daniel-wicke
mrustl Nov 15, 2024
64fdc2c
Merge flows_in_out and WW by newly created ID
mrustl Nov 18, 2024
6fdb0f5
Add bachward flow path routing and dummy :book:
mrustl Nov 19, 2024
d84a5e7
Fix Rcmdcheck (data :book:, shorter file names)
mrustl Nov 20, 2024
cd0b959
Fix GitHub actions by switching to latest ubuntu
mrustl Nov 20, 2024
e4f6b2b
Do not run archived Linux dep part
mrustl Nov 20, 2024
56c6445
Add missing linux dependency manually
mrustl Nov 20, 2024
f2c83c3
Try to fix YAML syntax
mrustl Nov 20, 2024
6df2c83
Try to fix YAML syntax
mrustl Nov 20, 2024
e2e46e0
Merge branch 'dev' of https://github.com/KWB-R/kwb.BerlinWaterModel i…
mrustl Nov 20, 2024
248aedc
Switch to latest actions/cache version
mrustl Nov 20, 2024
c2f062e
Add dataset WW and move config_read() to functions
mrustl Nov 20, 2024
45b8d6b
Fix typo :bug:
mrustl Nov 20, 2024
dda4841
Fix error handling
mrustl Nov 20, 2024
ab34e04
Move to better place
mrustl Nov 20, 2024
af5788b
Fix flow routing :bug:
mrustl Nov 21, 2024
4ae527a
File now UTF-8 coded
daniel-wicke Nov 21, 2024
dc4ad5b
Converted from ANSI to UTF-8 with Notepad++
mrustl Nov 25, 2024
ea907fd
Use UTF-8 as default
mrustl Nov 25, 2024
a8e4430
Start preparing input with different time res
mrustl Nov 25, 2024
1f1bcf7
Fix GitHub actions
mrustl Nov 25, 2024
d0f36ab
Fix prepare input (BF share instead WW abstraction)
mrustl Nov 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
^BerlinWaterModel\.Rproj$
^kwb\.BerlinWaterModel\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^_pkgdown\.yml$
Expand All @@ -8,3 +8,4 @@
^codecov\.yml$
^index\.md$
^README\.md$
^data-raw$
23 changes: 10 additions & 13 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: windows-latest, r: 'devel'}
- {os: windows-latest, r: 'oldrel'}
- {os: windows-latest, r: 'release'}
- { os: macOS-latest, r: 'release' }
- { os: ubuntu-24.04, r: 'release', rspm: "https://packagemanager.posit.co/cran/__linux__/noble/latest" }
- { os: windows-latest, r: 'devel' }
- { os: windows-latest, r: 'oldrel' }
- { os: windows-latest, r: 'release' }

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -51,20 +51,16 @@ jobs:

- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-r-${{ matrix.config.r }}-3-${{ hashFiles('depends.Rds') }}
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-3-

- name: Install system dependencies
- name: Install linux system dependencies
if: runner.os == 'Linux'
env:
RHUB_PLATFORM: linux-x86_64-ubuntu-gcc
run: |
Rscript -e "remotes::install_github('r-hub/sysreqs')"
sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))")
sudo -s eval "$sysreqs"
run: sudo apt-get install -y libglpk-dev

- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
Expand All @@ -81,3 +77,4 @@ jobs:
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check

2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
24 changes: 20 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Package: kwb.BerlinWaterModel
Title: R Package of Berlin Water Model
Version: 0.0.0.9000
Authors@R: c(
person("Daniel", "Wicke", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-5722-5433")),
person("Michael", "Rustler", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0647-7726")),
person("IMPETUS", role = "fnd"),
Expand All @@ -11,11 +13,25 @@ Description: R Package of Berlin Water Model.
License: MIT + file LICENSE
URL: https://github.com/KWB-R/kwb.BerlinWaterModel
BugReports: https://github.com/KWB-R/kwb.BerlinWaterModel/issues
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
Imports:
dplyr,
igraph,
magrittr,
readr,
rlang,
stringr,
tibble,
tidyr,
tidyselect
Suggests:
covr,
knitr,
rmarkdown
VignetteBuilder: knitr
VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
27 changes: 27 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(calculate_flowpath)
export(config_read)
export(fill_timeseries)
export(find_node_orders)
importFrom(dplyr,arrange)
importFrom(dplyr,arrange_at)
importFrom(dplyr,desc)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,mutate)
importFrom(igraph,V)
importFrom(igraph,distances)
importFrom(igraph,reverse_edges)
importFrom(igraph,vcount)
importFrom(magrittr,"%>%")
importFrom(readr,locale)
importFrom(readr,read_csv2)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(stats,setNames)
importFrom(stringr,str_remove)
importFrom(tibble,tibble)
importFrom(tidyr,fill)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,separate)
importFrom(tidyselect,matches)
File renamed without changes.
83 changes: 83 additions & 0 deletions R/calculate_flowpath.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' Helper function: find node order
#'
#' @param links links
#' @param nodes nodes
#' @param start_node start node
#' @param reverse should network direction be reversed? (default: FALSE)
#' @param mode Character constant, gives whether the shortest paths to or from
#' the given vertices should be calculated for directed graphs. If out then the
#' shortest paths from the vertex, if ⁠in⁠ then to it will be considered. If all,
#' the default, then the graph is treated as undirected, i.e. edge directions
#' are not taken into account. This argument is ignored for undirected graphs
#' (default: "out")
#'
#' @return data frame with additional column "order"
#' @export
#' @importFrom igraph distances reverse_edges V vcount
#' @importFrom dplyr mutate
#' @importFrom rlang .data
find_node_orders <- function(links, nodes, start_node, reverse = FALSE, mode = "out") {


# Erstellen eines gerichteten Graphen aus den Links
graph <- igraph::graph_from_data_frame(d = links,
vertices = nodes,
directed = TRUE)


if(reverse) {
graph <- igraph::reverse_edges(graph)
}

# Berechnen der Kürzesten Wege vom Startknoten
distances <- igraph::distances(graph, v = start_node, mode = mode)

# Initialisieren der Ordnungen
orders <- rep(NA, igraph::vcount(graph))
names(orders) <- igraph::V(graph)$name

# Zuweisen der Knoten zu ihren jeweiligen Ordnungen
for (i in 1:igraph::vcount(graph)) {
node_name <- igraph::V(graph)[i]$name
if (!is.infinite(distances[start_node, node_name])) {
orders[node_name] <- distances[start_node, node_name]
} else {
orders[node_name] <- NA # Knoten, die vom Startknoten aus nicht erreichbar sind
}
}

# Hinzufügen der Ordnungen zu den Knoten
nodes <- nodes %>%
dplyr::mutate(order = orders[.data$name])

return(nodes)
}


#' Calculate Flowpath
#'
#' @param flow_name flow name (as named in nodes) where calculation should start
#' @param links links
#' @param nodes nodes
#' @param backward should flowpath be calculated backwards? (default: FALSE)
#'
#' @return data frame with column "order" indicating the flow path order
#' @export
#' @importFrom dplyr arrange desc filter
calculate_flowpath <- function(flow_name,
links,
nodes,
backward = FALSE) {

stopifnot(flow_name %in% nodes$name)
stopifnot(any(nodes$group == "Fliessabschnitt"))

find_node_orders(links = links,
nodes = nodes,
start_node = flow_name,
reverse = backward,
mode = "out") %>%
dplyr::arrange(dplyr::desc(.data$order)) %>%
dplyr::filter(.data$group == "Fliessabschnitt",
!is.na(.data$order))
}
31 changes: 31 additions & 0 deletions R/config_read.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' Configuration: Read
#'
#' @param config_dir directory with configuration files (default:
#' system.file("extdata/config", package = "kwb.BerlinWaterModel")). It is mandatory
#' that there are three files within this folder: "flows_in_out.csv", "outflows_multiple.csv"
#' and "sections.csv"
#' @param file_encoding encoding for reading the files (default: "UTF-8")
#'
#' @return list with three sublists "flows_in_out", "outflows_multiple" and
#' "sections
#' @export
#'
#' @examples
#' config <- kwb.BerlinWaterModel::config_read()
#' config
#' @importFrom stats setNames
#' @importFrom readr read_csv2 locale
#' @importFrom stringr str_remove
config_read <- function(config_dir = "inst/extdata/config", file_encoding = "UTF-8") {

files <- list.files(config_dir, full.names = TRUE)

filenames_without_extension <- basename(files) %>% stringr::str_remove("\\..*")

stopifnot(all(filenames_without_extension %in% c("flows_in_out", "outflows_multiple", "sections")))

stats::setNames(lapply(files, function(file) {

readr::read_csv2(file, locale = readr::locale(encoding = file_encoding))
}), nm = filenames_without_extension)
}
11 changes: 11 additions & 0 deletions R/dataset_inflows.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' Dataset: Inflows
#'
#' Dataset with inflows
#'
#' @format A tibble with 398,840 rows and three columns
#' \describe{
#' \item{date}{date}
#' \item{id}{id of inflow station}
#' \item{cbm_per_second}{measured flow in cubicmeter per second}
#' }
"inflows"
13 changes: 13 additions & 0 deletions R/dataset_ww.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' Dataset: Waterworks (WW) Abstractions per Gallery
#'
#' Dataset with waterworks abstractions per gallery
#'
#' @format A tibble with 17,141 rows and three columns
#' \describe{
#' \item{date}{date}
#' \item{id}{id of WW gallery}
#' \item{cbm_per_second}{measured flow in cubicmeter per second, backcalculated
#' from monthly abstraction sums by dividing through number of days in month}
#' }
"ww"

13 changes: 13 additions & 0 deletions R/dataset_wwtp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' Dataset: WWTP (Wastewater Treatment Plant) Flows
#'
#' Dataset with WWTP inflows
#'
#' @format A tibble with 1,152 rows and three columns
#' \describe{
#' \item{date}{date}
#' \item{id}{id of WWTP}
#' \item{cbm_per_second}{measured flow in cubicmeter per second, backcalculated
#' from monthly effluent sums by dividing through number of days in month}
#' }
"wwtp"

64 changes: 64 additions & 0 deletions R/fill_timeseries.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' Fill Timeseries based on User Defined Intervall (days, hours, minutes or seconds)
#'
#' @param df data frame with data
#' @param col_datetime column of date or datetime
#' @param temporal_resolution select one of ("days", "hours", "minutes", "seconds"),
#' (default: "days")
#' @param direction in which direction should the parameter be filled ("up" or
#' "down"), default: "up"
#' @return tibble with filled dates. Note that if min(daste) == "xxxx-xx-end-of-month-day"
#' the min(date) is set to "xxxx-xx-01", as monthly measured (originally summed)
#' values were set to the last day of the month, but are valid for the whole month
#' @export
#'
#' @importFrom tibble tibble
#' @importFrom dplyr arrange_at full_join
#' @importFrom tidyr fill pivot_wider separate
#' @importFrom tidyselect matches
#' @importFrom rlang :=
fill_timeseries <- function(df, col_datetime = "date", temporal_resolution = "days", direction = "up")
{
stopifnot(col_datetime %in% names(df))

temp_res <- c("seconds" = "sec", "minutes" = "min", "hours" = "hour", "days" = "days")
stopifnot(temporal_resolution %in% names(temp_res))

stopifnot(direction %in% c("up", "down"))

dates <- df[[col_datetime]]
date_min_monthly <- as.Date(paste0(stringr::str_sub(min(dates), 1, 8), "01"))

date_min <- if (min(dates) != date_min_monthly) {
date_min_monthly
} else {
min(dates)
}


date_sequence <- if(names(temp_res[temporal_resolution]) == "days") {
seq.Date(from = date_min, to = max(dates),
by = temp_res[temporal_resolution])
} else {
seq.POSIXt(from = as.POSIXct(date_min, tz = "UTC"),
to = as.POSIXct(max(dates), tz = "UTC"),
by = temp_res[temporal_resolution])
}

new_dates <- tibble::tibble(!!(col_datetime) := date_sequence)


df_new <- df %>%
dplyr::full_join(new_dates,
by = col_datetime) %>%
dplyr::arrange_at(col_datetime) %>%
tidyr::fill(!tidyselect::matches(col_datetime), .direction = direction)


if(temporal_resolution != "days") {
df_new <- df_new %>%
dplyr::rename(datetime = col_datetime)
}

df_new

}
14 changes: 14 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
4 changes: 3 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
url: https://kwb-r.github.io/kwb.BerlinWaterModel
authors:
Daniel Wicke:
href: https://www.kompetenz-wasser.de/en/ueber-uns/team/daniel-wicke
Michael Rustler:
href: https://mrustl.de
href: https://www.kompetenz-wasser.de/en/ueber-uns/team/michael-rustler
IMPETUS:
href: https://www.kompetenz-wasser.de/en/forschung/projekte/impetus
html: <img src='https://climate-impetus.eu/wp-content/uploads/2021/12/horizontal.png'
Expand Down
Loading
Loading