Skip to content

Commit

Permalink
Fix issue with runread_biomee not being able to deal with missing col…
Browse files Browse the repository at this point in the history
…umns.
  • Loading branch information
marcadella committed Dec 19, 2024
1 parent e170e8d commit f0d9bb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
10 changes: 6 additions & 4 deletions R/run_biomee_f_bysite.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ run_biomee_f_bysite <- function(
n_annual_trans = as.integer(n_annual_trans)
)

out <- build_out(biomeeout, init_lu$name)
out <- build_out(biomeeout, init_lu$name, sitename)

return(out)
}

build_out <- function(biomeeout, lu_names){
build_out <- function(biomeeout, lu_names, sitename){
# If simulation is very long, output gets massive.
# E.g., In a 3000 years-simulation 'biomeeout' is 11.5 GB.
# In such cases (here, more than 5 GB), ignore hourly and daily outputs at tile and cohort levels
Expand Down Expand Up @@ -332,8 +332,10 @@ build_params_siml <- function(params_siml, forcing_years, makecheck){
`%nin%` <- Negate(`%in%`)
if ("spinup" %nin% names(params_siml))
params_siml$spinup <- params_siml$spinupyears > 0
else if (params_siml$spinup != (params_siml$spinupyears > 0))
warning("Warning: spinup in driver is deprecated. Please set spinupyears to 0 to disable spinup.")
else if (params_siml$spinup != (params_siml$spinupyears > 0)) {
warning("Warning: spinup flag is deprecated. Please set spinupyears to 0 to disable spinup.")
params_siml$spinup <- (params_siml$spinupyears > 0)
}

# Default value for nyeartrend
if ('nyeartrend' %nin% names(params_siml)) {
Expand Down
23 changes: 15 additions & 8 deletions R/runread_biomee_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ runread_biomee_f <- function(
params_tile <- params_species <- init_cohort <- init_soil <-
init_lu <- luc_forcing <- . <- NULL

if (parallel != (ncores > 0))
warning("Warning: parallel in driver is deprecated. Please set ncores to 0 to disable parallel execution.")
if (parallel != (ncores > 0)) {
warning("Warning: parallel flag is deprecated. Please set ncores to 0 to disable parallel execution.")
parallel <- (ncores > 0)
}

parameters <- c(
"sitename",
Expand All @@ -59,6 +61,14 @@ runread_biomee_f <- function(
"luc_forcing"
)

# Add potentially missing columns using NULL
`%nin%` <- Negate(`%in%`)
empty_col <- vector("list", nrow(drivers))
if ('init_lu' %nin% colnames(drivers))
drivers <- dplyr::mutate(drivers, 'init_lu'=empty_col)
if ('luc_forcing' %nin% colnames(drivers))
drivers <- dplyr::mutate(drivers, 'luc_forcing'=empty_col)

df <- drivers %>%
dplyr::select(any_of(parameters))

Expand All @@ -70,8 +80,6 @@ runread_biomee_f <- function(
packages = c("dplyr", "purrr", "rsofun")
)

# TODO: init_lu and luc_forcing are commented out because otherwise an error
# is thrown when they are not provided by the user. Needs to be fixed somehow.
df <- df %>%
multidplyr::partition(cl) %>%
dplyr::mutate('data' = purrr::pmap(list(
Expand All @@ -82,14 +90,13 @@ runread_biomee_f <- function(
params_tile,
params_species,
init_cohort,
init_soil#,
#init_lu,
#luc_forcing
init_soil,
init_lu,
luc_forcing
), run_biomee_f_bysite)) %>%
dplyr::collect()

} else {

df <- df %>%
dplyr::mutate('data' = purrr::pmap(
.,
Expand Down

0 comments on commit f0d9bb1

Please sign in to comment.