Skip to content

Commit bdb65a0

Browse files
committed
allow creating tar files for predicted distribution + rename argument of PrepData/Prep4HPC
1 parent 16a8b30 commit bdb65a0

File tree

9 files changed

+80
-29
lines changed

9 files changed

+80
-29
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Package: IASDT.R
33
Title: Modelling the distribution of invasive alien plant species in
44
Europe
55
Version: 0.1.03
6-
Date: 2025-03-06
6+
Date: 2025-03-07
77
Authors@R:
88
person("Ahmed", "El-Gabbas", , "ahmed.el-gabbas@ufz.de", role = c("aut", "cre"),
99
comment = c(ORCID = "0000-0003-2225-088X"))

R/Mod_Postprocess.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ Mod_Prep_TF <- function(
741741
# Mod_Postprocess_2_CPU ----
742742
## |------------------------------------------------------------------------| #
743743

744-
745744
#' @export
746745
#' @rdname Mod_postprocessing
747746
#' @name Mod_postprocessing
@@ -758,7 +757,7 @@ Mod_Postprocess_2_CPU <- function(
758757
"MRI-ESM2-0", "UKESM1-0-LL"),
759758
CC_Scenario = c("ssp126", "ssp370", "ssp585"),
760759
RC_NCores = 8L, Pred_Clamp = TRUE, Fix_Efforts = "q90", Fix_Rivers = "q90",
761-
Pred_NewSites = TRUE) {
760+
Pred_NewSites = TRUE, Tar = TRUE) {
762761

763762
.StartTime <- lubridate::now(tzone = "CET")
764763

@@ -949,10 +948,11 @@ Mod_Postprocess_2_CPU <- function(
949948
Path_Model = Path_Model, Hab_Abb = Hab_Abb, EnvFile = EnvFile,
950949
NCores = NCores, Pred_Clamp = Pred_Clamp, Fix_Efforts = Fix_Efforts,
951950
Fix_Rivers = Fix_Rivers, Pred_NewSites = Pred_NewSites, UseTF = UseTF,
952-
TF_Environ = TF_Environ, Temp_Dir = Temp_Dir, Temp_Cleanup = Temp_Cleanup,
953-
TF_use_single = TF_use_single, LF_NCores = LF_NCores, LF_Check = LF_Check,
951+
TF_Environ = TF_Environ, TF_use_single = TF_use_single,
952+
LF_NCores = LF_NCores, LF_Check = LF_Check,
954953
LF_Temp_Cleanup = LF_Temp_Cleanup, LF_Only = FALSE,
955-
LF_Commands_Only = FALSE, CC_Models = CC_Models, CC_Scenario = CC_Scenario)
954+
LF_Commands_Only = FALSE, Temp_Dir = Temp_Dir, Temp_Cleanup = Temp_Cleanup,
955+
Tar = Tar, CC_Models = CC_Models, CC_Scenario = CC_Scenario)
956956

957957
invisible(gc())
958958

R/Mod_Predict_Maps.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#' @param CC_Scenario Character vector. Climate scenarios for future
5555
#' predictions. Available options are: `c("ssp126", "ssp370", "ssp585")`
5656
#' (default).
57+
#' @param Tar Logical. Whether to compress the add files into a single `*.tar`
58+
#' file (without compression). Default: `TRUE`.
5759
#' @export
5860
#' @name Predict_Maps
5961
#' @author Ahmed El-Gabbas
@@ -68,7 +70,7 @@ Predict_Maps <- function(
6870
Pred_NewSites = TRUE, UseTF = TRUE, TF_Environ = NULL,
6971
TF_use_single = FALSE, LF_NCores = NCores, LF_Check = FALSE,
7072
LF_Temp_Cleanup = TRUE, LF_Only = FALSE, LF_Commands_Only = FALSE,
71-
Temp_Dir = "TEMP_Pred", Temp_Cleanup = TRUE,
73+
Temp_Dir = "TEMP_Pred", Temp_Cleanup = TRUE, Tar = TRUE,
7274
CC_Models = c(
7375
"GFDL-ESM4", "IPSL-CM6A-LR", "MPI-ESM1-2-HR",
7476
"MRI-ESM2-0", "UKESM1-0-LL"),
@@ -1399,6 +1401,36 @@ Predict_Maps <- function(
13991401
col.names = TRUE, file = Path_Summary_txt_Shiny, quote = FALSE,
14001402
fileEncoding = "UTF-8")
14011403

1404+
if (Tar) {
1405+
1406+
IASDT.R::CatTime("Create tar file for prediction files", Level = 1)
1407+
1408+
# Directory to save the tar file
1409+
TarDir <- dirname(Path_Summary_RData_Shiny)
1410+
# Path to the tar file
1411+
TarFile <- IASDT.R::Path(TarDir, "Predictions.tar")
1412+
# List of directories in the prediction folder. All directories will be
1413+
# included in the tar file
1414+
TarFiles <- list.dirs(
1415+
path = TarDir, full.names = FALSE, recursive = FALSE) %>%
1416+
paste(collapse = " ") %>%
1417+
# Add the summary files to the list
1418+
paste(
1419+
"Prediction_Summary.RData", "Prediction_Summary_Shiny.txt",
1420+
collapse = " ")
1421+
1422+
# Command to create the tar file
1423+
Command <- stringr::str_glue(
1424+
'cd {fs::path_abs(TarDir)}; tar -cf {basename(TarFile)} -b 2048 \\
1425+
{TarFiles}')
1426+
1427+
# Create tar file
1428+
system(Command)
1429+
1430+
# Change the permission of the tar file
1431+
Sys.chmod(TarFile, "755", use_umask = FALSE)
1432+
}
1433+
14021434
# # ................................................................... ###
14031435
# # ..................................................................... ###
14041436

R/Mod_Prep4HPC.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
#' ([Tikhonov et al.](https://doi.org/10.1002/ecy.2929)), initializing models,
1212
#' and creating HPC execution commands. The function supports parallel
1313
#' processing and offers the option to include or exclude phylogenetic tree
14-
#' data.<br/><br/> The internal **`Mod_PrepData`** function is used to prepare
14+
#' data.<br/><br/> The `Mod_PrepData` function is used to prepare
1515
#' habitat-specific data for Hmsc models. This function processes environmental
1616
#' and species presence data, reads environment variables from a file, verifies
1717
#' paths, loads and filters species data based on habitat type and minimum
1818
#' presence grid cells per species, and merges various environmental layers
1919
#' (e.g., CHELSA Bioclimatic variables, habitat coverage, road and railway
2020
#' intensity, sampling efforts) into a single dataset. Processed data is saved
2121
#' to disk as an `*.RData` file.
22-
#' @param Path_Model Character. Directory path where all output files, including
23-
#' the models to be fitted, will be saved.
22+
#' @param DirName Character. Directory name, without its parents, where the
23+
#' models will be saved. This directory will be created.
2424
#' @param GPP Logical. Whether to fit spatial random effect using Gaussian
2525
#' Predictive Process. Defaults to `TRUE`. If `FALSE`, non-spatial models will
2626
#' be fitted.
@@ -183,7 +183,7 @@
183183
#' plant species
184184

185185
Mod_Prep4HPC <- function(
186-
Hab_Abb = NULL, Path_Model = NULL,
186+
Hab_Abb = NULL, DirName = NULL,
187187
MinEffortsSp = 100L, PresPerSpecies = 80L, EnvFile = ".env",
188188
GPP = TRUE, GPP_Dists = NULL, GPP_Save = TRUE,
189189
GPP_Plot = TRUE, MinLF = NULL, MaxLF = NULL,
@@ -212,7 +212,7 @@ Mod_Prep4HPC <- function(
212212
.StartTime <- lubridate::now(tzone = "CET")
213213

214214
CheckNULL <- c(
215-
"Path_Model", "PresPerSpecies", "thin", "samples",
215+
"DirName", "PresPerSpecies", "thin", "samples",
216216
"MemPerCpu", "Path_Hmsc", "Hab_Abb")
217217
IsNull <- purrr::map_lgl(CheckNULL, ~ is.null(get(.x)))
218218

@@ -264,7 +264,7 @@ Mod_Prep4HPC <- function(
264264
M_Name_Fit <- Chain <- Post_Missing <- Command_HPC <- Command_WS <-
265265
Post_Path <- Path_ModProg <- TaxaInfoFile <- Path_Grid <- EU_Bound <-
266266
Path_PA <- NAME_ENGL <- NSp <- Species_File <- File <- ias_id <-
267-
PA <- PA_model <- PA_file <- PA_model_file <- NULL
267+
PA <- PA_model <- PA_file <- PA_model_file <- Path_Model <- NULL
268268

269269
if (isFALSE(VerboseProgress)) {
270270
sink(file = nullfile())
@@ -286,6 +286,7 @@ Mod_Prep4HPC <- function(
286286
"TaxaInfoFile", "DP_R_Taxa_info", FALSE, TRUE,
287287
"EU_Bound", "DP_R_EUBound", FALSE, TRUE,
288288
"Path_Grid", "DP_R_Grid_processed", TRUE, FALSE,
289+
"Path_Model", "DP_R_Model_path", FALSE, FALSE,
289290
"Path_PA", "DP_R_PA", TRUE, FALSE)
290291

291292
# Check if Python executable exists
@@ -297,6 +298,11 @@ Mod_Prep4HPC <- function(
297298
IASDT.R::AssignEnvVars(EnvFile = EnvFile, EnvVarDT = EnvVars2Read)
298299
rm(EnvVars2Read, envir = environment())
299300

301+
Path_Model <- IASDT.R::Path(Path_Model, DirName)
302+
if (fs::dir_exists(Path_Model)) {
303+
stop("Model directory already exists: ", Path_Model, call. = FALSE)
304+
}
305+
fs::dir_create(Path_Model)
300306

301307
Path_GridR <- IASDT.R::Path(Path_Grid, "Grid_10_Land_Crop.RData")
302308
if (!file.exists(Path_GridR)) {
@@ -319,7 +325,7 @@ Mod_Prep4HPC <- function(
319325
function(x) get(x, envir = parent.env(env = environment()))) %>%
320326
stats::setNames(AllArgs)
321327

322-
CharArgs <- c("Hab_Abb", "Path_Model", "Path_Hmsc", "EnvFile", "BioVars")
328+
CharArgs <- c("Hab_Abb", "DirName", "Path_Hmsc", "EnvFile", "BioVars")
323329
IASDT.R::CheckArgs(AllArgs = AllArgs, Args = CharArgs, Type = "character")
324330

325331
LogicArgs <- c(
@@ -446,8 +452,7 @@ Mod_Prep4HPC <- function(
446452
DT_All <- IASDT.R::Mod_PrepData(
447453
Hab_Abb = Hab_Abb, MinEffortsSp = MinEffortsSp, ExcludeCult = ExcludeCult,
448454
ExcludeZeroHabitat = ExcludeZeroHabitat, PresPerSpecies = PresPerSpecies,
449-
EnvFile = EnvFile, Path_Model = Path_Model,
450-
VerboseProgress = VerboseProgress)
455+
EnvFile = EnvFile, DirName = DirName, VerboseProgress = VerboseProgress)
451456

452457
IASDT.R::CatSep(Rep = 1, Extra1 = 1, Extra2 = 2)
453458

R/Mod_PrepData.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
#' @author Ahmed El-Gabbas
1010

1111
Mod_PrepData <- function(
12-
Hab_Abb = NULL, MinEffortsSp = 100L, ExcludeCult = TRUE,
12+
Hab_Abb = NULL, DirName = NULL, MinEffortsSp = 100L, ExcludeCult = TRUE,
1313
ExcludeZeroHabitat = TRUE, PresPerSpecies = 80L, EnvFile = ".env",
14-
Path_Model = NULL, VerboseProgress = TRUE) {
14+
VerboseProgress = TRUE) {
1515

1616
# # ..................................................................... ###
1717

1818
# # |||||||||||||||||||||||||||||||||||
1919
# Check input parameters ----
2020
# # |||||||||||||||||||||||||||||||||||
2121

22-
CheckNULL <- c("Hab_Abb", "Path_Model", "EnvFile")
22+
CheckNULL <- c("Hab_Abb", "DirName", "EnvFile")
2323
IsNull <- purrr::map_lgl(CheckNULL, ~ is.null(get(.x)))
2424
if (any(IsNull)) {
2525
stop(
@@ -40,7 +40,7 @@ Mod_PrepData <- function(
4040
# https://www.r-bloggers.com/2019/08/no-visible-binding-for-global-variable/
4141
SpeciesID <- Species_name <- Species_File <- PA <- Path_Rivers <-
4242
cell <- Path_PA <- Path_Grid <- Path_Grid_Ref <- Path_CLC <-
43-
Path_Roads <- Path_Rail <- Path_Bias <- Path_CHELSA <-
43+
Path_Roads <- Path_Rail <- Path_Bias <- Path_CHELSA <- Path_Model <-
4444
EU_Bound <- SpPA <- NPres <- Grid_R <- IAS_ID <- NULL
4545

4646
# # ..................................................................... ###
@@ -51,7 +51,7 @@ Mod_PrepData <- function(
5151
AllArgs,
5252
function(x) get(x, envir = parent.env(env = environment()))) %>%
5353
stats::setNames(AllArgs)
54-
CharArgs <- c("EnvFile", "Hab_Abb", "Path_Model")
54+
CharArgs <- c("EnvFile", "Hab_Abb", "DirName")
5555
IASDT.R::CheckArgs(AllArgs = AllArgs, Args = CharArgs, Type = "character")
5656
IASDT.R::CheckArgs(
5757
AllArgs = AllArgs, Args = c("MinEffortsSp", "PresPerSpecies"),
@@ -68,7 +68,6 @@ Mod_PrepData <- function(
6868
call. = FALSE)
6969
}
7070

71-
fs::dir_create(Path_Model)
7271

7372
# # ..................................................................... ###
7473

@@ -91,11 +90,18 @@ Mod_PrepData <- function(
9190
"Path_Rail", "DP_R_Railways_processed", TRUE, FALSE,
9291
"Path_Bias", "DP_R_Efforts_processed", TRUE, FALSE,
9392
"Path_Rivers", "DP_R_Rivers_processed", FALSE, TRUE,
93+
"Path_Model", "DP_R_Model_path", FALSE, FALSE,
9494
"EU_Bound", "DP_R_EUBound", FALSE, TRUE)
9595
# Assign environment variables and check file and paths
9696
IASDT.R::AssignEnvVars(EnvFile = EnvFile, EnvVarDT = EnvVars2Read)
9797
rm(EnvVars2Read, envir = environment())
9898

99+
Path_Model <- IASDT.R::Path(Path_Model, DirName)
100+
if (fs::dir_exists(Path_Model)) {
101+
stop("Model directory already exists: ", Path_Model, call. = FALSE)
102+
}
103+
fs::dir_create(Path_Model)
104+
99105
# # ..................................................................... ###
100106

101107
# # |||||||||||||||||||||||||||||||||||

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ If you use the `IASDT.R` package, please cite it as:
7474
> <a href="https://biodt.eu" target="_blank">https://biodt.eu</a>.
7575
7676
<span style=" color: grey !important;">Last update:
77-
2025-03-06</span>
77+
2025-03-07</span>

man/Mod_inputs.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Mod_postprocessing.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Predict_Maps.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)