Mercurial > repos > siwaa > redelac_stics_s
view redelacSticsSimulator.R @ 7:2586f7dbcde1 draft
"planemo upload for repository https://forgemia.inra.fr/redelac/redelac-toolbox/-/tree/af2d1fcb63a2ae897e02f11e82cc76c4f6e8ca5e/tools/REDELACSticsSimulator commit af2d1fcb63a2ae897e02f11e82cc76c4f6e8ca5e-dirty"
author | siwaa |
---|---|
date | Mon, 02 Oct 2023 13:51:01 +0000 |
parents | 41f652e6616f |
children | 65c00476f417 |
line wrap: on
line source
library(SticsRFiles) library(SticsOnR) library(parallel) library(doParallel) library(readxl) library(tidyr) library(data.table) library(readr) library(dplyr) library(stringr) library(readr) sessionInfo() args = commandArgs(trailingOnly = TRUE) parallel <- (args[1] == "parallel") nbSlots <- as.integer(args[2]) scenario <- args[3] workspace <- paste0(getwd(), "/WS") javastics_path <- getwd() default_config_path <- paste0(javastics_path, "/config") txt_path <- paste0(getwd(), "/WS/txt_files") # List of successive USMs successive_usms_file <- ("successionPlan.csv") successive_usms_tab <- read.csv(file = file.path(workspace, successive_usms_file), sep = ";") # from succesions to simulate retrieving upcSol Cases successRef <- successive_usms_tab["Treatment"] successRef <- successRef %>% separate( col = Treatment, into = c("upc", "sol", "systeme", "rotation", "tete_rotation", "horizon"), sep = "-", remove = FALSE ) successRef$upcSol <- paste0(successRef$upc, "-", successRef$sol) casesUpcSol <- unique(successRef$upcSol) systeme <- successRef$systeme[1] rotation <- successRef$rotation[1] if (parallel) { cl <- makeCluster(nbSlots) print("Inside R, info on the cluster:") print(cl) registerDoParallel(cl) `%dordopar%` <- `%dopar%` } else { `%dordopar%` <- `%do%` } successive_usms_tab_split <- list() for (j in 1:length(casesUpcSol)) { splited <- strsplit(casesUpcSol[j], split = "-")[[1]] upc <- splited[1] sol <- splited[2] successive_usms_tab_split[[j]] <- successive_usms_tab[successRef$upcSol == casesUpcSol[j],] } result <- foreach(j = 1:length(casesUpcSol)) %:% foreach( i = 1:nrow(successive_usms_tab_split[[j]]), .packages = c("foreach", "SticsRFiles", "SticsOnR") ) %dordopar% { if (parallel) sink("./logPaRedelac.txt", append = TRUE) nusms <- as.numeric(successive_usms_tab_split[[j]][i, 2]) vec <- as.character(successive_usms_tab_split[[j]][i, c(3:(2 + nusms))]) list_successive_usms <- list(vec) sim_options <- stics_wrapper_options( javastics = javastics_path, stics_exe = paste0(javastics_path, "/bin/stics_modulo"), workspace = txt_path, verbose = TRUE, successive = list_successive_usms ) list_usms <- vec cat(paste0("-->", successive_usms_tab_split[[j]][i, 1], "launched\n")) return(stics_wrapper(model_options = sim_options, situation = list_usms)) } if (parallel) { stopCluster(cl) sink() cat(read_file("./logPaRedelac.txt")) } for (j in 1:length(casesUpcSol)) { splited <- strsplit(casesUpcSol[j], split = "-")[[1]] upc <- splited[1] sol <- splited[2] for (i in 1:nrow(successive_usms_tab_split[[j]])) { #i=1 if (i == 1) { simPCP <- result[[j]][[i]] } else { simPCP$sim_list <- c(simPCP$sim_list, result[[j]][[i]]$sim_list) } } #agregation des sorties dans un dataframe sim_df <- rbindlist(simPCP$sim_list, use.names = TRUE, idcol = "usm") #ajout des colonnes explicatives + de l'horizon temporel sim_df <- sim_df %>% separate( col = usm, into = c( "upc", "sol", "systeme", "rotation", "tete_rotation", "horizon", "annee", "culture" ), sep = "-", remove = FALSE ) #sauvegarde sous la forme d'un fichier rds if (dir.exists(paste0( workspace, "/analyse_simulations_STICS/", systeme, "/", rotation )) == FALSE) { dir.create( paste0( workspace, "/analyse_simulations_STICS/", systeme, "/", rotation ), recursive = TRUE ) } saveRDS( sim_df, paste0( workspace, "/analyse_simulations_STICS/", systeme, "/", rotation, "/outputs-", rotation, "-", upc, "-", sol, "-", scenario, ".rds" ) ) successive_usms_tab_split[[j]]$Treatment <- NULL successive_usms_tab_split[[j]]$Number_USM <- NULL list_usms <- unlist(unname(as.list( transpose(successive_usms_tab_split[[j]]) ))) list_usms <- list_usms[!is.na(list_usms)] list_usms <- list_usms[list_usms != ''] list_usms_yr1 <- successive_usms_tab_split[[j]]$USM_1 for (i in 1:length(list_usms)) { if (i == 1) { #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation #en attendant que toutes les simulations tournent correctement #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ rap_1 <- read_delim(file.path(txt_path, list_usms[i], "mod_rapport.sti"), col_names = TRUE) names_col <- colnames(rap_1) rap_string <- read_lines(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"), skip = 1) rap <- as_tibble(rap_string) %>% separate(col = value, sep = ";", into = names_col) %>% mutate(USM = list_usms[i]) # } } else { #if (list_usms[i] %in% list_usms_yr1) { #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation #en attendant que toutes les simulations tournent correctement #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ rap_string <- read_lines(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"), skip = 1,)#} # } else { #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation #en attendant que toutes les simulations tournent correctement #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ # rap_string <- # read_file(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"))#} # } rap_i <- as_tibble(rap_string) %>% separate(col = value, sep = ";", into = names_col) %>% mutate(USM = list_usms[i]) rap <- bind_rows(rap, rap_i) } } rm(rap_1, names_col, rap_string, rap_i) rap <- rap %>% select(USM, everything()) %>% mutate(P_usm = str_replace_all(P_usm, " ", "")) %>% mutate(wlieu = str_replace_all(wlieu, " ", "")) %>% mutate(stade = str_replace_all(stade, " ", "")) %>% mutate(nomversion = str_replace_all(nomversion, " ", "")) %>% mutate(across(3:9, as.numeric)) %>% mutate(across(13:ncol(rap), as.numeric)) %>% #separate(col=P_usm,into=c("upc","sol","systeme","rotation","tete_rotation","horizon","annee","culture"),sep="-",remove = FALSE) separate( col = USM, into = c( "upc", "sol", "systeme", "rotation", "tete_rotation", "horizon", "annee", "culture" ), sep = "-", remove = FALSE ) write_delim( x = rap, file = paste0( workspace, "/analyse_simulations_STICS/", systeme, "/", rotation, "/report-", rotation, "-", upc, "-", sol, "-", scenario, ".csv" ), delim = ";" ) }