-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
597 lines (421 loc) · 20.1 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
---
title: "SticsOnR"
output:
github_document:
html_preview: true
params:
javastics_dist: 'JavaSTICS-1.5.2-STICS-10.1.0'
#javastics_dist: 'JavaSTICS-1.41-stics-9.2'
#java_cmd: '/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java'
java_cmd: 'java'
root_path: '/home/plecharpent/Téléchargements'
#root_path: 'F:/TESTS/test_SticsOnR'
work_dir: 'example'
windows: !r Sys.info()[['sysname']]=='Windows'
---
```{r setup, echo = FALSE, warning=FALSE}
suppressPackageStartupMessages(library(SticsRFiles))
suppressPackageStartupMessages(library(SticsOnR))
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
options(tibble.print_min = 5, tibble.print_max = 5)
javastics_dist <- params$javastics_dist
root_path <- normalizePath(params$root_path, winslash = "/")
javastics_path <- file.path(root_path, params$javastics_dist)
workspace_path <- file.path(javastics_path, params$work_dir)
output_path <- file.path(root_path, "gen_usms_xml2txt")
windows <- params$windows
linux <- !windows
java_cmd <- "java"
if (linux) {
java_cmd <- normalizePath(params$java_cmd, winslash = "/")
}
```
```{r error=FALSE, echo=FALSE, results='hide'}
# For testing consistency betwen JavaSTICS and java version
# Adding a test to see if javastics is compatible with java version
res <- try(
run_javastics(javastics_path,
workspace_path,
usm = "wheat", verbose = FALSE,
java_cmd = java_cmd
),
silent = TRUE
)
try_err <- attr(res, "class")
if (!is.null(try_err) && attr(try_err, "class") == "try-error") {
ver <- system2(
command = "java", args = "-version",
stdout = TRUE, stderr = TRUE
)
stop(
javastics_dist, ": javastics not compatible with java\n",
ver[1],
paste0(
"\nA java path must be provided for a ",
"compatible java version (11) to run_javastics"
)
)
}
```
The R package for the [STICS](https://eng-stics.paca.hub.inrae.fr/) model <img src="man/figures/logo.png" alt="logo" width="150" align="right" />
<!-- badges: start -->
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R build status](https://github.com/SticsRPacks/SticsOnR/workflows/R-CMD-check/badge.svg)](https://github.com/SticsRPacks/SticsOnR/actions)
[![Codecov test coverage](https://codecov.io/gh/SticsRPacks/SticsOnR/branch/main/graph/badge.svg)](https://app.codecov.io/gh/SticsRPacks/SticsOnR?branch=main)
[![DOI](https://zenodo.org/badge/166790380.svg)](https://zenodo.org/badge/latestdoi/166790380)
<!-- badges: end -->
The goal of SticsOnR is to perform simulations of the Stics model, downloadable with its graphical user interface from https://eng-stics.paca.hub.inrae.fr/Download.
If you want to be notified when a new release of this package is made, you can tick the Releases box in the "Watch / Unwatch => Custom" menu at the top right of [this page](https://github.com/SticsRPacks/SticsOnR).
## Prerequisites and technical tips
### JavaStics software
JavaStics must be installed and the minimal version is version 1.41.
The latest distribution version for Stics is downloadable [here](https://eng-stics.paca.hub.inrae.fr/Download).
The installation process only consists of unzipping the JavaStics archive, except for MacOS system (see MacOS installation [specificities](#under-macos-systems))
### Under the Windows operating system
Be aware that the java virtual machine does not need to be installed to use the
JavaSTICS software, neither the graphical interface (`JavaStics.exe`) nor the command line interface (`JavaSticsCmd.exe`). Because, a Java machine is embedded in the JavaStics archive.
### Under linux operating systems
#### Java version
For using the JavaStics software (GUI and command line interface) under a `linux` operating system, the java version must be consistent with the JavaStics version
* for JavaStics 1.41, the java version must be at most a Java 8 version
* for JavaStics 1.5.0/1.5.1 versions, the java version must be at least a Java 11 version
* for JavaStics 1.5.2 version, the java version must be at least a Java 17 version
So, for adapting the java version to the JavaStics version some manipulations must be done either by switching between versions through system commands [see here](https://sticsrpacks.github.io/SticsOnR/articles/Changing_java_version_linux.html), using a specific java executable path ([see running JavaStics](#javastics-command-line-interface)).
#### System libraries
For the linux operating system, the SticsOnR package may require to install an `xslt` library.
If the SticsOnR installation fails, and the `xslt` library is missing, the error message indicates what is the name of the xslt library to be installed (according to the common linux distributions). For example, for the Ubuntu or Debian OS `libxslt1-dev` must be installed.
### Under MacOS systems
The STICS executable must be compiled and imported in the JavaStics directory. The procedure is fully described
in the JavaStics documentation (see JavaStics_documentation.html under the doc folder), in the prerequisites sub-section under the Software section. Java version installation management is also described in it.
### Files/directories paths syntax
Under all systems, file paths must not contain any special character or space.
Under unix like systems, using the `~` in files or directories paths may cause errors in SticsOnR functions. So, it is safer for the moment to use absolute paths. This will be fixed in the future versions.
#### Remote installation tools
For installing packages from the Github site an additional package must be installed. One can use either `devtools` or [`remotes`](https://github.com/r-lib/remotes#readme)
For `devtools`, installation requires system dependent tools. They must be installed first.
* For Windows
RTools must be installed using the latest installer from [here](https://cran.r-project.org/bin/windows/Rtools/)
* For linux
Development tools must be installed first, like the `build-essentials` package for a Debian like distribution for example.
Then the `devtools` package can be installed using:
```{r eval=FALSE}
install.packages("devtools")
```
For `remotes`, it can be directly installed using:
```{r eval=FALSE}
install.packages("remotes")
```
## Installation
### Recommended installation: `SticsRPacks`
The best way to install the packages from `SticsRPacks`, from which `SticsOnR` is part of, is by installing the `[SticsRPacks]` package:
```{r eval=FALSE}
devtools::install_github("SticsRPacks/SticsRPacks")
```
- With `remotes`
```{r eval=FALSE}
remotes::install_github("SticsRPacks/SticsRPacks")
```
The package will install the packages for you at the latest release version.
### Other way: install each package independently
#### SticsOnR
The package installation can be remotely done directly from [GitHub](https://github.com/) using either `devtools` or the lightweight `remote` one package
The latest release version can be installed using:
- With `devtools`
```{r eval=FALSE}
devtools::install_github("SticsRPacks/SticsOnR@*release")
```
- With `remotes`
```{r eval=FALSE}
remotes::install_github("SticsRPacks/SticsOnR@*release")
```
Normally, all the package dependencies will be installed for CRAN packages.
#### SticsRFiles
`SticsRFiles` must be installed manually using the above syntax, just replacing **SticsOnR** with **SticsRFiles**.
## Loading the packages library
```{r lib_load}
library(SticsOnR)
library(SticsRFiles)
```
## Running the model
Here are basic examples which show you how to run the model either from a R model interface or a JavaStics (command line) one. More complete examples will be detailed in a specific documentation later.
### JavaStics command line interface
The JavaStics installation folder (for example, `r javastics_dist`) contains an `example` workspace folder with a set of runnable usms.
For running simulations from it, we can use the `run_javastics()` function.
* Specifying the JavaStics folder
<pre>javastics_path <- `r paste0('/path/to/',javastics_dist)`</pre>
* Specifying a workspace
* as a sub-folder of JavaStics
<pre>workspace_path <- "example"</pre>
* or an absolute path to an external folder
<pre>workspace_path <- "/path/to/javastics/workspace"</pre>
```{r eval = windows, echo = TRUE, include = TRUE}
########## For Windows or linux with a compatible java version ################
# Running specific usms from the workspace
run_javastics(javastics_path, workspace_path,
usm = c("banana", "wheat"),
verbose = FALSE
)
# Running all usms contained in the workspace
run_javastics(javastics_path, workspace_path,
verbose = FALSE
)
# Getting information about execution:
runs_info <- run_javastics(javastics_path, workspace_path,
usm = c("banana", "wheat"), verbose = FALSE
)
runs_info
```
* For linux systems, if the java version is not compatible
with the JavaStics version a compatible java executable path must be used
<pre>java_cmd <- "/path/to/java/exe"</pre>
```{r eval = linux, echo = TRUE, include = TRUE}
################ Only for linux with a specific java executable################
# Running specific usms from the workspace
run_javastics(javastics_path, workspace_path,
usm = c("banana", "wheat"),
verbose = FALSE, java_cmd = java_cmd
)
# Running all usms contained in the workspace
run_javastics(javastics_path, workspace_path,
verbose = FALSE, java_cmd = java_cmd
)
# Getting information about execution:
runs_info <- run_javastics(javastics_path, workspace_path,
usm = c("banana", "wheat"),
verbose = FALSE, java_cmd = java_cmd
)
runs_info
```
In the returned information, the error field name gives a list of messages from the JavaStics command line interface. If any `Error` key word appears in a message, the corresponding simulation failed. But, at the moment it is impossible to identify what is the error's origin. Things must be checked manually in the workspace, after running again the faulty usm (because the model input files are overwritten at each usms simulation).
### Running the model using the stics executable directly
We need for that a JavaStics folder and a directory with text input files for Stics, or a folder containing individual sub-directories for usms.
These directories can be generated using the `[SticsRFiles::gen_usms_xml2txt()]` function from the **SticsRFiles** package, by converting automatically XML files to Stics input text files. See the documentation [here](https://sticsrpacks.github.io/SticsRFiles/articles/Generating_Stics_text_files.html).
Example of use:
```{r eval = windows, echo = TRUE}
# For Windows
# Generating files for all the usms contained in the workspace
SticsRFiles::gen_usms_xml2txt(javastics_path,
workspace = workspace_path,
out_dir = output_path, verbose = FALSE
)
```
```{r eval = linux, echo = TRUE}
# For linux, using a specific java executable
# Generating files for all the usms contained in the workspace
SticsRFiles::gen_usms_xml2txt(javastics_path,
workspace = workspace_path,
out_dir = output_path, verbose = FALSE,
java_cmd = java_cmd
)
```
```{r, echo=F, results='hide', eval = TRUE}
unlink(x = file.path(output_path, "intercrop_pea_barley"), recursive = TRUE)
```
The `run_stics()` function can be used as follows with one folder or multiple sub-folders.
The Stics executable path is set according to each operating system:
* for windows
<pre>stics_path <- file.path(javastics_path,"bin","stics_modulo.exe")</pre>
* for linux
<pre>stics_path <- file.path(javastics_path,"bin","stics_modulo")</pre>
* for Mac
<pre>stics_path <- file.path(javastics_path,"bin","stics_modulo_mac")</pre>
```{r run_stics_int, echo = FALSE, results='hide'}
# Specifying the Stics executable file path
stics_path <- file.path(javastics_path, "bin", "stics_modulo.exe")
# for linux
if (is_unix()) {
stics_path <- file.path(javastics_path, "bin", "stics_modulo")
}
```
```{r run_stics}
# Specifying a directory containing Stics input files
# For example reusing a generated sub-directory in the previous section
# of the document
# Running one usm
files_dir_path <- file.path(output_path, "banana")
run_stics(stics_path, files_dir_path)
# Specifying a root directory containing usms individual directories
# For example reusing a generated directory in the previous section
# of the document
# Running two usms
run_stics(stics_path, output_path, usm = c("banana", "wheat"))
# Running all the usms defined in the sub-directories of output_path
run_stics(stics_path, output_path, usm = "all")
# Getting returned information about stics runs
runs_info <- run_stics(stics_path, output_path, usm = c("banana", "wheat"))
runs_info
```
### Advanced simulations parameterization
A specific function `stics_wrapper()` is dedicated to manage simulations with a higher level
of parameterization than what `run_stics()` offers.
This `stics_wrapper()` function allows:
- Forcing the values of a set of parameters (common or specific values per USM)
- Returning simulated daily outputs for each usm with possible dates and variables filtering
- Parallelizing simulations, and displaying execution time
- Run Usms in successive mode
As the `run_stics()` function, the `stics_wrapper()` operates on directories
containing text stics input files.
#### Defining simulations options
Simulation options can be fixed using the `stics_wrapper_options()` function.
Both of them are mandatory: the model executable path and the directory path containing usms sub-directories with text input files.
A template is returned by the function when called with no arguments:
```{r}
stics_wrapper_options()
```
For the example, we will use the default stics model version shipping with JavaStics and the directory where individual usms input directories have been generated:
```{r eval=FALSE}
sim_options <- stics_wrapper_options(
javastics = javastics_path,
workspace = output_path, verbose = FALSE
)
```
By default, `stics_wrapper_options()` checks that `javastics`, `stics_exe` and `workspace` exists.
There are different solutions if you need to use a custom version of stics:
1. if it is already listed in the preference (e.g. added in JavaStics), simply provide its name (ID):
```{r, eval=FALSE}
sim_options <- stics_wrapper_options(
javastics = javastics_path,
stics_exe = "stics_custom",
workspace = output_path, verbose = FALSE
)
```
2. if it is located in the bin directory of the JavaStics installation directory, provide the executable name:
```{r, eval=FALSE}
sim_options <- stics_wrapper_options(
javastics = javastics_path,
stics_exe = "stics_custom.exe",
workspace = output_path, verbose = FALSE
)
```
3. if it is located in any other folder, provide the full path to the executable name, and no need to use `javastics_path`:
```{r, eval=FALSE}
sim_options <- stics_wrapper_options(
stics_exe = "path/to/stics_custom.exe",
workspace = output_path, verbose = FALSE
)
```
#### Simple simulations cases
- Without filtering usms or outputs
```{r, eval=FALSE}
results <- stics_wrapper(model_options = sim_options)
```
- Filtering on usms list
```{r, eval=FALSE}
usm <- c("wheat", "maize")
results <- stics_wrapper(model_options = sim_options, situation = usm)
```
- Filtering outputs on variables
```{r, eval=FALSE}
usm <- c("wheat", "maize")
stics_wrapper(
model_options = sim_options, situation = usm,
var = c("masec_n", "mafruit")
)
```
- Filtering outputs on variables and dates for several USMs
The argument `sit_var_dates_mask` must contain a named list (named by usms names) containing
data.frames, as the sim_list element of the list returned by stics\_wrapper (see here-after) or as observations data.
It defines a mask: stics\_wrapper will return a result for each USM, variable and date that contains at least a value (*i.e.* different from NA) in the mask.
The stics\_wrapper function returns a list that contains two elements:
- error, a boolean indicating if an error occurs during the simulations,
- sim\_list, a named list of data.frames containing the simulated values for the requested USMs, variables and dates.
```{r}
obs_list <- get_obs(
workspace = workspace_path, usm = c("wheat", "maize"),
verbose = FALSE
)
# Observations table for wheat
obs_list$wheat
sim_options <- stics_wrapper_options(
javastics = javastics_path,
workspace = output_path, verbose = TRUE
)
results <- stics_wrapper(
model_options = sim_options,
sit_var_dates_mask = obs_list
)
head(results)
```
Some warnings may occur, as in this case (that is why `results$error` is TRUE), indicating that observed variables and/or observations dates are missing in simulated data.
Concerning the dates, this may be due to the USMs simulation period that may not include observed dates.
For the variables, this may be due to an incorrect spelling of the variables in obs\_list.
#### Simulations with forcing parameters
- Applying a single parameter values vector for all the selected usms
Parameters values are prescribed using the `param_values` argument. It can be a named vector containing the values and names of the parameters to force.
In this case, the same values will be applied for all the simulated usms.
```{r, eval=FALSE}
param_values <- c(0.002, 50)
names(param_values) <- c("dlaimax", "durvieF")
results <- stics_wrapper(
model_options = sim_options, situation = usm,
param_values = param_values
)
```
- Defining different parameters values depending on the usms
`param_values` can also be a data.frame or a tibble having one named column per parameter and an optional column named `situation` containing the name of the situations (USMs for Stics) that allows to define different values of the parameters for different situations.
```{r}
# Let's run usm wheat with c(dlaimax=0.001, durvieF=50),
# usm pea with c(dlaimax=0.001, durvieF=60),
# and usm maize with c(dlaimax=0.001, durvieF=70)
param_values <- data.frame(
situation = c("wheat", "pea", "maize"),
dlaimax = c(0.001, 0.001, 0.001),
durvieF = c(50, 60, 70)
)
# Let's display it
param_values
results <- stics_wrapper(
model_options = sim_options,
param_values = param_values,
situation = c("wheat", "maize")
)
```
#### Simulations in successive mode
USMs can be run in successive mode, if they are adequately defined (*i.e.* if the beginning and end of simulations are consistent), using the option `successive_usms`.
`successive_usms` is a list of vectors containing the names of the UMSs to consider as successive (e.g. `list(c("usm1.1","usm1.2"),c("usm2.1","usm2.2"))` defines 2 successions usm1.1 -> usm1.2 and usm2.1 -> usm2.2).
#### Other Optional arguments
- Displaying execution time
```{r eval=FALSE}
sim_options <- stics_wrapper_options(
javastics = javastics_path,
workspace = output_path,
time_display = TRUE,
verbose = FALSE
)
results <- stics_wrapper(model_options = sim_options)
```
- Activating parallel execution
On may specify the number of cores to use with the cores argument.
```{r eval=FALSE}
sim_options <- stics_wrapper_options(
javastics = javastics_path,
workspace = output_path,
parallel = TRUE, time_display = TRUE,
cores = 2, verbose = FALSE
)
results <- stics_wrapper(model_options = sim_options)
```
If cores is not given, parallel execution is performed over machine total cores number minus 1.
```{r eval=FALSE}
library(parallel)
# Used cores number
detectCores() - 1
sim_options <- stics_wrapper_options(
javastics = javastics_path,
workspace = output_path, parallel = TRUE,
time_display = TRUE, verbose = FALSE
)
results <- stics_wrapper(model_options = sim_options)
```
## Getting help
If you have any question or suggestion or if you want to report a bug, please do it via the GitHub [issues](https://github.com/SticsRPacks/SticsOnR/issues).
Thanks for that, this would greatly help us to improve this package.
## Citation
If you have used this package for a study that led to a publication or report, please cite us. You can either use the citation tool from Github if you used the last version, or use `citation("SticsOnR")` from R otherwise.
## Code of conduct
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
The package is under intensive development, so you can fill an issue or request us a feature [here](https://github.com/SticsRPacks/SticsOnR/issues) at any time.