view redelacSticsInputGenerator.R @ 14:812bb8097dac draft

"planemo upload for repository https://forgemia.inra.fr/redelac commit d16b9ecdb4dd51b69a6aaa4ab298b82455fe0267"
author siwaa
date Tue, 12 Dec 2023 10:38:20 +0000
parents f707c2fb725b
children 413cec4dfe5f
line wrap: on
line source

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

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)
}

beforeGenXmlTime <- Sys.time()

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

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

gen_usms_xml2txt(
  javastics = javastics_path,
  workspace = workspace,
  out_dir = txt_path,
  usms_file = "usms.xml",
  redelac = redelac,
  parallel = parallel,
  verbose = FALSE
)

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("===================================================================================")