view redelacSticsInputGenerator.R @ 17:4d0807472ad3 draft default tip

"planemo upload for repository https://forgemia.inra.fr/redelac commit 9c5802d9b947f1e59bd5df610ebe5dfc89745be6"
author siwaa
date Tue, 23 Jan 2024 18:51:34 +0000
parents f2fe11a20371
children
line wrap: on
line source

library(SticsRFiles)
library(dplyr)
library(lubridate)
library(parallel)
library(doParallel)
library(doFuture)
library(stringr)

sessionInfo()

startTime <- Sys.time()

workspace <- paste0(getwd(), "/WS")
javastics_path <- getwd()

txt_path <- paste0(getwd(), "/WS/txt_files")
dir.create(txt_path, recursive = T)

USMsFile <- "USMs.csv"
TecFile <- "Tec.csv"
IniFile <- "Ini.csv"
StationFile <- "Station.csv"

args = commandArgs(trailingOnly = TRUE)

redelac <- (args[1] == "optim")
parallel <- (args[2] == "parallel")
print(paste("parallel:", parallel, args[2]))
nbSlots <- as.integer(args[3])
print(paste("nbslots:", nbSlots))

# USMs file loading

usms_param <- read_params_table(file.path(workspace, USMsFile)) %>%
  select(
    usm_name,
    datedebut,
    datefin,
    finit,
    nomsol,
    fstation,
    fclim1,
    fclim2,
    culturean,
    nbplantes,
    codesimul,
    fplt_1,
    ftec_1,
    flai_1,
    fplt_2,
    ftec_2,
    flai_2
  )

# Tec files loading and generating xml

tec_param <- read_params_table(file.path(workspace, TecFile))
columnToRemove <-
  names(tec_param[grep("juleclair|nbinfloecl", names(tec_param))])
columnToKeep <-
  names(tec_param)[names(tec_param) %in% columnToRemove == FALSE]

tec_param <- subset(tec_param, select = columnToKeep)

gen_tec_xml(param_df = tec_param, out_dir = workspace)

# Ini files loading and generating xml

ini_param <- read_params_table(file.path(workspace, IniFile))

gen_ini_xml(param_df = ini_param, out_dir = workspace)

# Station files loading and generating xml

sta_param <- read_params_table(file.path(workspace, StationFile))

gen_sta_xml(param_df = sta_param, out_dir = workspace)

if (parallel) {
  doFuture::registerDoFuture()
  future::plan(multisession, workers = nbSlots)
  `%dordopar%` <- `%dofuture%`
} else {
  `%dordopar%` <- `%do%`
}

beforeGenXmlTime <- Sys.time()

#gen_usms_xml(file = file.path(workspace, "usms.xml"),
#             param_df = usms_param)

chunckSize <- 1000
nbUSMs <- nrow(usms_param)

ids <- seq(1, nbUSMs, chunckSize)

foreach (i = ids,
         .options.future = list(packages = c("foreach", "SticsRFiles"))) %dordopar% {
           gen_usms_xml(file = file.path(workspace, paste0("usms__", i, ".xml")),
                        param_df = usms_param[i:min(i + chunckSize - 1, nbUSMs),])
         }

afterGenXmlTime <- Sys.time()
durGenX <-
  lubridate::as.duration(lubridate::interval(beforeGenXmlTime,
                                             afterGenXmlTime))

beforeGenTxtTime <- Sys.time()

foreach (i = ids,
         .options.future = list(packages = c("foreach", "SticsRFiles"))) %dordopar% {
           gen_usms_xml2txt(
             javastics = javastics_path,
             workspace = workspace,
             out_dir = txt_path,
             usms_file = paste0("usms__", i, ".xml"),
             redelac = redelac,
             parallel = parallel,
             verbose = FALSE
           )
         }

afterGenTxtTime <- Sys.time()
durGenT <-
  lubridate::as.duration(lubridate::interval(beforeGenTxtTime, afterGenTxtTime))

afterGenTxtTime <- Sys.time()
durGenT <-
  lubridate::as.duration(lubridate::interval(beforeGenTxtTime, afterGenTxtTime))

endTime <- Sys.time()

dur <-
  lubridate::as.period(lubridate::as.duration(lubridate::interval(startTime, endTime)))
durGenXP <- lubridate::as.period(durGenX)
durGenTP <- lubridate::as.period(durGenT)

print("REDELACSticsInputGenerator Report")
print("=================================")
print(paste("number of usms: ", nrow(usms_param)))
print(paste("redelac optimization:", redelac))
print(paste("process in parallel:", parallel))
print(paste("nbslots:", nbSlots))

sprintf(
  'overall duration: %g jour(s) %g heure(s) %g minute(s) %g seconde(s)',
  lubridate::day(dur),
  lubridate::hour(dur),
  lubridate::minute(dur),
  lubridate::second(dur)
)

sprintf(
  'inside gen_usms_xml duration: %g jour(s) %g heure(s) %g minute(s) %g seconde(s)',
  lubridate::day(durGenXP),
  lubridate::hour(durGenXP),
  lubridate::minute(durGenXP),
  lubridate::second(durGenXP)
)

sprintf(
  'inside gen_usms_xml2txt duration: %g jour(s) %g heure(s) %g minute(s) %g seconde(s)',
  lubridate::day(durGenTP),
  lubridate::hour(durGenTP),
  lubridate::minute(durGenTP),
  lubridate::second(durGenTP)
)
print(
  "==================================================================================="
)