Skip to content

Commit

Permalink
Merge pull request #4 from geco-bern/streamline-parallel-code
Browse files Browse the repository at this point in the history
Update grsofun_tidy() for map2tidy v2.0
  • Loading branch information
stineb authored Aug 22, 2024
2 parents 46ce6d1 + 5fa1307 commit 084cbdc
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 183 deletions.
21 changes: 15 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
Package: grsofun
Title: Global rsofun
Version: 0.1
Authors@R:
Version: 0.2
Authors@R:c(
person(
given = "Benjamin",
family = "Stocker",
email = "[email protected]",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2697-9096")
comment = c(ORCID = "0000-0003-2697-9096")),
person(
given = "Fabian",
family = "Bernhard",
email = "[email protected]",
role = c("aut"),
comment = c(ORCID = "0000-0003-0338-0961"))
)
Description: A wrapper for global rsofun runs
Description: A wrapper for global rsofun runs.
License: AGPL-3
Encoding: UTF-8
Imports:
zoo,
dplyr,
readr,
tidyr,
magrittr,
lubridate,
purrr,
multidplyr,
parallel,
map2tidy,
map2tidy (>= 2.1.0),
rsofun,
rgeco
Suggests:
here,
cowplot,
rnaturalearth,
ggplot2,
Remotes:
geco-bern/map2tidy,
geco-bern/rgeco,
geco-bern/rsofun,
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export(grsofun)
export(grsofun_collect)
export(grsofun_collect_byilon)
export(grsofun_run)
export(grsofun_run_bychunk)
export(grsofun_run_byilon)
export(grsofun_tidy)
export(grsofun_tidy_byvar)
export(read_forcing_byvar_byilon)
2 changes: 1 addition & 1 deletion R/grsofun.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ grsofun <- function(par, settings){

# Convert forcing files to a tidy format
# Checks by file. If tidy already, skips automatically
settings <- grsofun_tidy(settings)
tidy_out <- grsofun_tidy(settings)

# Run rsofun
# Parallelizes runs to chunks of longitudinal bands
Expand Down
17 changes: 9 additions & 8 deletions R/grsofun_collect.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ grsofun_collect <- function(
if (settings$ncores_max == 1){

# un-parallel alternative
df <- tibble(ilon = seq(settings$grid$len_ilon)) |>
len_ilon <- settings$grid$len_ilon
df <- dplyr::tibble(ilon = seq(len_ilon)) |>
dplyr::mutate(out = purrr::map(
ilon,
~grsofun_collect_byilon(
Expand Down Expand Up @@ -47,16 +48,16 @@ grsofun_collect <- function(
"purrr",
"tidyr",
"readr",
"here",
"magrittr"
"here"
)) |>
multidplyr::cluster_assign(
grsofun_collect_byilon = grsofun_collect_byilon # make the function known for each core
)

# distribute computation across the cores, calculating for all longitudinal
# indices of this chunk
df <- tibble(ilon = seq(settings$grid$len_ilon)) |>
len_ilon <- settings$grid$len_ilon
df <- dplyr::tibble(ilon = seq(len_ilon)) |>
multidplyr::partition(cl) |>
dplyr::mutate(out = purrr::map(
ilon,
Expand All @@ -69,7 +70,7 @@ grsofun_collect <- function(

if (return_data){
df <- df |>
collect() |>
dplyr::collect() |>
dplyr::mutate(len = purrr::map_int(out, ~nrow(.))) |>
dplyr::filter(len > 0) |>
dplyr::select(-len) |>
Expand Down Expand Up @@ -114,10 +115,10 @@ grsofun_collect_byilon <- function(
month = lubridate::month(date)
) |>
dplyr::group_by(sitename, year, month) |>
summarise(across(all_of(vars), \(x) mean(x, na.rm = TRUE)), .groups = "drop") |>
dplyr::summarise(dplyr::across(dplyr::all_of(vars), \(x) mean(x, na.rm = TRUE)), .groups = "drop") |>

# add lon and lat to data frame
left_join(
dplyr::left_join(
ddf |>
dplyr::select(sitename, site_info) |>
tidyr::unnest(site_info) |>
Expand All @@ -126,7 +127,7 @@ grsofun_collect_byilon <- function(
)

} else {
mdf <- tibble()
mdf <- dplyr::tibble()
}

if (return_data){
Expand Down
Loading

0 comments on commit 084cbdc

Please sign in to comment.