From 92a695c1a5b7476c4c433a31a6f1581c707dd0da Mon Sep 17 00:00:00 2001 From: MayaGueguen Date: Fri, 29 Oct 2021 10:24:16 +0200 Subject: [PATCH 01/20] Trying to couple RangeShiftR with RFate --- DESCRIPTION | 6 +++--- R/UTILS.loadPackage.R | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b9a9d5c..b1bfe9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: RFate Type: Package Title: FATE with R -Version: 1.0.1 -Date: 2021-06-14 +Version: 1.0.2 +Date: 2021-09-27 Authors@R: c(person("Wilfried", "Thuiller", role = "aut" , email = "wilfried.thuiller@univ-grenoble-alpes.fr"), person("Damien", "Georges", role = "aut"), @@ -59,6 +59,6 @@ Suggests: testthat, covr, knitr, markdown, rmarkdown -LinkingTo: Rcpp, RcppThread, BH (>= 1.75.0-0) +LinkingTo: Rcpp, RcppThread, BH (>= 1.75.0-0), RangeShiftR VignetteBuilder: knitr, rmarkdown Language: en-US diff --git a/R/UTILS.loadPackage.R b/R/UTILS.loadPackage.R index 6359c4b..914cc99 100644 --- a/R/UTILS.loadPackage.R +++ b/R/UTILS.loadPackage.R @@ -80,5 +80,6 @@ .onUnload = function (libpath) { + library.dynam.unload("RangeShiftR", libpath) library.dynam.unload("RFate", libpath) } From 7e4bafebbc25c40bfc8467e9d818b5bb006cd7b2 Mon Sep 17 00:00:00 2001 From: MayaGueguen Date: Tue, 23 Nov 2021 14:28:14 +0100 Subject: [PATCH 02/20] Add first version of FATE_RS function --- R/FATE_RS.R | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 R/FATE_RS.R diff --git a/R/FATE_RS.R b/R/FATE_RS.R new file mode 100644 index 0000000..6415368 --- /dev/null +++ b/R/FATE_RS.R @@ -0,0 +1,120 @@ +### HEADER ##################################################################### +##' @title S +##' +##' @name FATE_RS +##' +##' @author Maya Guéguen +##' +##' +##' @description ... +##' +##' +##' @keywords RangeShifter +##' +##' @export +##' +##' @importFrom RangeShiftR RunRS readPop +##' @importFrom raster stack rasterFromXYZ writeRaster +##' +## END OF HEADER ############################################################### + + +FATE_RS = function(name.simulation, file.simulParam, opt.no_CPU = 1, verbose.level = 2 + , name.simulation.RS, params.RS + , year.start, year.end, year.step) +{ + ## + argument pour savoir si on commence par FATE ou RS + + ## LOOP over simulation years + for (ye in seq(year.start, year.end, 1)) + { + .setParam(params.lines = abs.simulParam + , flag = "SAVING_DIR" + , flag.split = "^--.*--$" + , value = paste0(name.simulation, "/RESULTS/SIMUL_YEAR_", ye, "")) + + ## Run FATE ################################################################################### + FATE(simulParam = abs.simulParam, no_CPU = opt.no_CPU, verboseLevel = verbose.level) + + ## Get resulting FATE vegetation maps ######################################################### + ## Get results directories ------------------------------------------------ + GLOB_DIR = .getGraphics_results(name.simulation = name.simulation + , abs.simulParam = abs.simulParam) + + ## Get number of PFGs ----------------------------------------------------- + ## Get PFG names ---------------------------------------------------------- + GLOB_SIM = .getGraphics_PFG(name.simulation = name.simulation + , abs.simulParam = abs.simulParam) + + ## Get raster mask -------------------------------------------------------- + GLOB_MASK = .getGraphics_mask(name.simulation = name.simulation + , abs.simulParam = abs.simulParam) + + ## UNZIP the raster saved ------------------------------------------------- + raster.perPFG.allStrata = .getRasterNames(year.step, "allStrata", "ABUND", GLOB_DIR) + .unzip(folder_name = GLOB_DIR$dir.output.perPFG.allStrata + , list_files = raster.perPFG.allStrata + , no_cores = opt.no_CPU) + + ## GET PFG abundance maps (all strata) ------------------------------ + file_name = paste0(GLOB_DIR$dir.output.perPFG.allStrata + , "Abund_YEAR_" + , year.step + , "_" + , GLOB_SIM$PFG + , "_STRATA_all.tif") + gp = GLOB_SIM$PFG[which(file.exists(file_name))] + file_name = file_name[which(file.exists(file_name))] + + if (length(file_name) > 0) + { + ras.PFG = stack(file_name) * GLOB_MASK$ras.mask + names(ras.PFG) = gp + } else + { + stop(paste0("Missing data!\n The folder " + , GLOB_DIR$dir.output.perPFG.allStrata + , " does not contain adequate files")) + } + + ## Transform them into habitat map ############################################################ + + ## Transform them into RS species distribution map ############################################ + + ## Update RS disp-cost and patch maps ######################################################### + + ## Run RangeShifter ########################################################################### + RunRS(RSparams = params.RS, dirpath = name.simulation.RS) + + ## Get resulting RS species density ########################################################### + ## Code from RangeShiftR help + # read population output file into a dataframe + pop_df <- readPop(s = params.RS, dirpath = name.simulation.RS) + + # Make stack of different raster layers for each year and for only one repetition (Rep==0): + pop_wide_rep0 <- reshape(subset(pop_df, Rep == 0)[, c('Year', 'x', 'y', 'NInd')] + , timevar = 'Year' + , v.names = c('NInd') + , idvar = c('x', 'y') + , direction = 'wide') + + # use raster package to make a raster from the data frame + stack_years_rep0 <- rasterFromXYZ(pop_wide_rep0) + name.dist = paste0(name.simulation, "/DATA/MASK/MASK_DIST_YEAR_", ye, ".tif") + writeRaster(stack_years_rep0, filename = name.dist) + + ## Update FATE disturbance maps ############################################################### + .setParam(params.lines = abs.simulParam + , flag = "DIST_MASK" + , flag.split = "^--.*--$" + , value = paste0(name.simulation, "/DATA/MASK/MASK_DIST_YEAR_", ye, ".tif")) + + ## Update FATE simulation to load ############################################################# + .setParam(params.lines = abs.simulParam + , flag = "SAVED_STATE" + , flag.split = "^--.*--$" + , value = "XXX") + + } +} + From adb0c2e576e3dcb84882b7829e8dbd05a6b515df Mon Sep 17 00:00:00 2001 From: MayaGueguen Date: Tue, 30 Nov 2021 12:52:37 +0100 Subject: [PATCH 03/20] Remove ALIEN.no parameter --- R/PRE_FATE.params_globalParameters.R | 12 +---- .../PRE_FATE.params_globalParameters.html | 20 +++------ man/PRE_FATE.params_globalParameters.Rd | 6 --- .../test-PRE_FATE.params_globalParameters.R | 44 ++----------------- .../test-PRE_FATE.params_simulParameters.R | 1 - 5 files changed, 11 insertions(+), 72 deletions(-) diff --git a/R/PRE_FATE.params_globalParameters.R b/R/PRE_FATE.params_globalParameters.R index bd1e786..c09abd0 100644 --- a/R/PRE_FATE.params_globalParameters.R +++ b/R/PRE_FATE.params_globalParameters.R @@ -124,8 +124,6 @@ ##' @param doAliens default \code{FALSE}. \cr If \code{TRUE}, invasive plant ##' introduction is activated in the \code{FATE} simulation, and associated ##' parameters are required -##' @param ALIEN.no (\emph{optional}) \cr an \code{integer} corresponding to the -##' number of introductions ##' @param ALIEN.freq (\emph{optional}) \cr a \code{vector} of \code{integer} ##' corresponding to the frequency of each introduction (\emph{in years}) ##' @param doFire default \code{FALSE}. \cr If \code{TRUE}, fire @@ -435,7 +433,6 @@ ##' introduction areas. \cr If the habitat suitability filter is on, ##' suitability maps will also be needed for these new groups. ##' \describe{ -##' \item{ALIEN.no}{the number of different introductions} ##' \item{ALIEN.freq}{the frequency of each introduction (\emph{in years})} ##' } ##' } @@ -642,7 +639,6 @@ ##' ##' \itemize{ ##' \item DO_ALIENS_INTRODUCTION -##' \item ALIENS_NO ##' \item ALIENS_FREQ ##' } ##' @@ -768,7 +764,6 @@ PRE_FATE.params_globalParameters = function( , doDrought = FALSE , DROUGHT.no_sub = 4 , doAliens = FALSE - , ALIEN.no , ALIEN.freq = 1 , doFire = FALSE , FIRE.no @@ -862,11 +857,10 @@ PRE_FATE.params_globalParameters = function( } if (doAliens) { - .testParam_notInteger.m("ALIEN.no", ALIEN.no) .testParam_notInteger.m("ALIEN.freq", ALIEN.freq) - if (length(ALIEN.freq) != ALIEN.no){ + if (length(ALIEN.freq) != required.no_PFG){ stop(paste0("Wrong type of data!\n `ALIEN.freq` must contain as many " - , "values as the number of introductions (`ALIEN.no`)")) + , "values as the number of PFG (`required.no_PFG`)")) } } if (doFire) @@ -1012,10 +1006,8 @@ PRE_FATE.params_globalParameters = function( if (doAliens) { params.ALIEN = list(as.numeric(doAliens) - , ALIEN.no , ALIEN.freq) names.params.list.ALIEN = c("DO_ALIENS_INTRODUCTION" - , "ALIENS_NO" , "ALIENS_FREQ") } else { diff --git a/docs/reference/PRE_FATE.params_globalParameters.html b/docs/reference/PRE_FATE.params_globalParameters.html index d41c784..8c5dc4f 100644 --- a/docs/reference/PRE_FATE.params_globalParameters.html +++ b/docs/reference/PRE_FATE.params_globalParameters.html @@ -75,7 +75,7 @@ RFate - 1.0.1 + 1.0.2 @@ -83,7 +83,7 @@