From f0d9bb19735ccf2b3dbe66aab8f4a1723380dd5a Mon Sep 17 00:00:00 2001 From: marcadella Date: Thu, 19 Dec 2024 14:01:28 +0100 Subject: [PATCH] Fix issue with runread_biomee not being able to deal with missing columns. --- R/run_biomee_f_bysite.R | 10 ++++++---- R/runread_biomee_f.R | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/R/run_biomee_f_bysite.R b/R/run_biomee_f_bysite.R index 81d52375..f0045ab5 100644 --- a/R/run_biomee_f_bysite.R +++ b/R/run_biomee_f_bysite.R @@ -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 @@ -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)) { diff --git a/R/runread_biomee_f.R b/R/runread_biomee_f.R index 9f7fcb32..5c7bf1c5 100644 --- a/R/runread_biomee_f.R +++ b/R/runread_biomee_f.R @@ -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", @@ -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)) @@ -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( @@ -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( .,