comparison 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
comparison
equal deleted inserted replaced
13:3db1f57029a7 14:812bb8097dac
1 library(SticsRFiles) 1 library(SticsRFiles)
2 library(dplyr) 2 library(dplyr)
3 library(lubridate) 3 library(lubridate)
4 library(parallel) 4 library(parallel)
5 library(doParallel) 5 library(doParallel)
6 library(doFuture)
6 7
7 sessionInfo() 8 sessionInfo()
8 9
9 startTime <- Sys.time() 10 startTime <- Sys.time()
10 11
19 IniFile <- "Ini.csv" 20 IniFile <- "Ini.csv"
20 StationFile <- "Station.csv" 21 StationFile <- "Station.csv"
21 22
22 args = commandArgs(trailingOnly = TRUE) 23 args = commandArgs(trailingOnly = TRUE)
23 24
24 genUSMsXmlOneAfterOther <- (args[1] == "usmx_one_after_other") 25 redelac <- (args[1] == "optim")
25 parallel <- (args[2] == "parallel") 26 parallel <- (args[2] == "parallel")
27 print(paste("parallel:", parallel, args[2]))
26 nbSlots <- as.integer(args[3]) 28 nbSlots <- as.integer(args[3])
29 print(paste("nbslots:", nbSlots))
27 30
28 # USMs file loading 31 # USMs file loading
29 32
30 usms_param <- read_params_table(file.path(workspace, USMsFile)) %>% 33 usms_param <- read_params_table(file.path(workspace, USMsFile)) %>%
31 select( 34 select(
70 73
71 sta_param <- read_params_table(file.path(workspace, StationFile)) 74 sta_param <- read_params_table(file.path(workspace, StationFile))
72 75
73 gen_sta_xml(param_df = sta_param, out_dir = workspace) 76 gen_sta_xml(param_df = sta_param, out_dir = workspace)
74 77
75 if (genUSMsXmlOneAfterOther) { 78 if (parallel) {
76 79 doFuture::registerDoFuture()
77 durGenX <- lubridate::as.duration(0) 80 future::plan(multisession, workers = nbSlots)
78 durGenT <- lubridate::as.duration(0)
79
80 if (parallel) {
81 cl <- makeCluster(nbSlots)
82 print(cl)
83 registerDoParallel(cl)
84 `%dordopar%` <- `%dopar%`
85 } else {
86 `%dordopar%` <- `%do%`
87 }
88
89 # looping on USMs
90 loopRes <- foreach::foreach(i = 1:nrow(usms_param),
91 .packages = c("SticsRFiles", "lubridate")) %dordopar% {
92
93 beforeGenXmlTimeI <- Sys.time()
94
95 usmxFileName = paste0("usms", as.character(i) , ".xml")
96
97 usmxFilePath = file.path(workspace, usmxFileName)
98
99 gen_usms_xml(file = usmxFilePath,
100 param_df = usms_param[i, ])
101
102 afterGenXmlTimeI <- Sys.time()
103 durGenXI <-
104 lubridate::as.duration(lubridate::interval(beforeGenXmlTimeI,
105 afterGenXmlTimeI))
106
107 # one after the other generating txt folder
108
109 beforeGenTxtTimeI <- Sys.time()
110
111 gen_usms_xml2txt(
112 usms_file = usmxFileName,
113 javastics = javastics_path,
114 workspace = workspace,
115 out_dir = txt_path,
116 verbose = TRUE
117 )
118
119 afterGenTxtTimeI <- Sys.time()
120 durGenTI <-
121 lubridate::as.duration(lubridate::interval(beforeGenTxtTimeI,
122 afterGenTxtTimeI))
123
124 return(c(durGenXI, durGenTI))
125 }
126
127 if (parallel) stopCluster(cl)
128
129 durDS <- as.data.frame(do.call(rbind, loopRes))
130 durGenX <- sum(durDS[1])
131 durGenT <- sum(durDS[2])
132
133 } else {
134
135 beforeGenXmlTime <- Sys.time()
136
137 gen_usms_xml(file = file.path(workspace, "usms.xml"),
138 param_df = usms_param)
139
140 afterGenXmlTime <- Sys.time()
141 durGenX <-
142 lubridate::as.duration(lubridate::interval(beforeGenXmlTime,
143 afterGenXmlTime))
144
145 beforeGenTxtTime <- Sys.time()
146
147 gen_usms_xml2txt(
148 javastics = javastics_path,
149 workspace = workspace,
150 out_dir = txt_path,
151 verbose = TRUE
152 )
153
154 afterGenTxtTime <- Sys.time()
155 durGenT <-
156 lubridate::as.duration(lubridate::interval(beforeGenTxtTime,
157 afterGenTxtTime))
158
159 } 81 }
160 82
83 beforeGenXmlTime <- Sys.time()
84
85 gen_usms_xml(file = file.path(workspace, "usms.xml"),
86 param_df = usms_param)
87
88 afterGenXmlTime <- Sys.time()
89 durGenX <-
90 lubridate::as.duration(lubridate::interval(beforeGenXmlTime,
91 afterGenXmlTime))
92 beforeGenTxtTime <- Sys.time()
93
94 gen_usms_xml2txt(
95 javastics = javastics_path,
96 workspace = workspace,
97 out_dir = txt_path,
98 usms_file = "usms.xml",
99 redelac = redelac,
100 parallel = parallel,
101 verbose = FALSE
102 )
103
104 afterGenTxtTime <- Sys.time()
105 durGenT <-
106 lubridate::as.duration(lubridate::interval(beforeGenTxtTime,
107 afterGenTxtTime))
108
161 endTime <- Sys.time() 109 endTime <- Sys.time()
162 110
163 dur <- 111 dur <-
164 lubridate::as.period(lubridate::as.duration(lubridate::interval(startTime, endTime))) 112 lubridate::as.period(lubridate::as.duration(lubridate::interval(startTime, endTime)))
165 durGenXP <- lubridate::as.period(durGenX) 113 durGenXP <- lubridate::as.period(durGenX)
166 durGenTP <- lubridate::as.period(durGenT) 114 durGenTP <- lubridate::as.period(durGenT)
167 115
168 print("REDELACSticsInputGenerator Report") 116 print("REDELACSticsInputGenerator Report")
169 print("=================================") 117 print("=================================")
170 print(paste0("number of usms: ", nrow(usms_param))) 118 print(paste("number of usms: ", nrow(usms_param)))
171 print(paste("usms.xml one after the other:", genUSMsXmlOneAfterOther)) 119 print(paste("redelac optimization:", redelac))
120 print(paste("process in parallel:", parallel))
121 print(paste("nbslots:", nbSlots))
172 122
173 sprintf( 123 sprintf(
174 'overall duration: %g jour(s) %g heure(s) %g minute(s) %g seconde(s)', 124 'overall duration: %g jour(s) %g heure(s) %g minute(s) %g seconde(s)',
175 lubridate::day(dur), 125 lubridate::day(dur),
176 lubridate::hour(dur), 126 lubridate::hour(dur),