diff redelacSticsInputGenerator.R @ 7:fb6f7d60508d draft

"planemo upload for repository https://forgemia.inra.fr/redelac commit d1c69d78e9ccfe06ceb7609dce59b438cd94d9d8"
author siwaa
date Thu, 20 Jul 2023 16:18:59 +0000
parents bb1ac57f8545
children 2a6d7d053ba7
line wrap: on
line diff
--- a/redelacSticsInputGenerator.R	Fri Jul 07 10:19:22 2023 +0000
+++ b/redelacSticsInputGenerator.R	Thu Jul 20 16:18:59 2023 +0000
@@ -1,48 +1,164 @@
 library(SticsRFiles)
 library(dplyr)
+library(lubridate)
+
+
+startTime <- Sys.time()
 
 workspace <- paste0(getwd(), "/WS")
 javastics_path <- getwd()
 
 txt_path <- paste0(getwd(), "/WS/txt_files")
-dir.create(txt_path,recursive = T)
+dir.create(txt_path, recursive = T)
 
 USMsFile <- "USMs.csv"
 TecFile <- "Tec.csv"
 IniFile <- "Ini.csv"
 StationFile <- "Station.csv"
 
+args = commandArgs(trailingOnly = TRUE)
+
+genUSMsXmlOneAfterOther <- (args[1] == "usmx_one_after_other")
+
 # 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)
+  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)
+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)
 
-
-# looping on USMs
-for(i in 1:nrow(usms_param)) {
-   row <- usms_param[i,]
-
-   # generating xml
-   gen_usms_xml(file = file.path(workspace, "usms.xml"), param_df = row)
+if (genUSMsXmlOneAfterOther) {
+  durGenX <- lubridate::as.duration(0)
+  durGenT <- lubridate::as.duration(0)
+  
+  # looping on USMs
+  for (i in 1:nrow(usms_param)) {
+    row <- usms_param[i, ]
+    
+    beforeGenXmlTimeI <- Sys.time()
+    
+    gen_usms_xml(file = file.path(workspace, "usms.xml"),
+                 param_df = row)
+    
+    afterGenXmlTimeI <- Sys.time()
+    durGenXI <-
+      lubridate::as.duration(lubridate::interval(beforeGenXmlTimeI,
+                                                 afterGenXmlTimeI))
+    
+    # one after the other generating txt folder
+    
+    beforeGenTxtTimeI <- Sys.time()
+    
+    gen_usms_xml2txt(
+      javastics = javastics_path,
+      workspace = workspace,
+      out_dir = txt_path,
+      verbose = TRUE
+    )
+    
+    afterGenTxtTimeI <- Sys.time()
+    durGenTI <-
+      lubridate::as.duration(lubridate::interval(beforeGenTxtTimeI,
+                                                 afterGenTxtTimeI))
+    
+    durGenX <- durGenX + durGenXI
+    durGenT <- durGenT + durGenTI
+  }
+} else {
+  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,
+    verbose = TRUE
+  )
+  
+  afterGenTxtTime <- Sys.time()
+  durGenT <-
+    lubridate::as.duration(lubridate::interval(beforeGenTxtTime,
+                                               afterGenTxtTime))
+  
+}
 
-   # generating txt folder
-   gen_usms_xml2txt(javastics = javastics_path,
-	            workspace = workspace, 
-                    out_dir = txt_path,
-                    verbose = TRUE)
-}
-    
+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("REDELAC Report")
+print("==============")
+print(paste0("number of usms: ", nrow(usms_param)))
+print(paste("usms.xml one after the other:", genUSMsXmlOneAfterOther))
+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)
+)