-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_sim.R
163 lines (153 loc) · 5.7 KB
/
get_sim.R
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
#' Load and format STICS daily output file(s)
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function was deprecated because we realised that it's
#' a special case of the [get_sim()]
#'
#' @examples
#' \dontrun{
#' get_daily_results(path, "banana")
#' # ->
#' get_sim(path, "banana")
#' }
#'
#' @keywords internal
#'
#' @noRd
#'
get_daily_results <- function(...) {
lifecycle::deprecate_warn(
"1.0.0",
"get_daily_results()",
"get_sim()"
) # ,
# details = "This function is a special case of get_sim(); use it instead.")
get_sim(...)
}
#' Load and format STICS daily output file(s)
#'
#' @description Reads and format daily output file(s) (mod_s*.sti) for usm(s)
#' with possible selection on variable names, cumulative DOY and dates
#'
#' @param workspace Vector of path(s) of directory(ies) containing the STICS
#' output files to read (mod_s*.sti file) or path of a single directory
#' containing one sub-folder per USM (named as the USM names),
#' each of them containing the corresponding STICS output file to read.
#' In the second case, the argument `usm` must also be provided.
#' @param usm Vector of USM names. Optional, if not provided, the function
#' returns the results for all USMs.
#' @param var Vector of variable names for which results have to be provided.
#' Optional, all variables considered by default. See `get_var_info()`
#' to get the list of STICS variables names.
#' @param dates list of dates to filter (POSIX date)
#' @param usms_file Path of a USM xml file. Optional, if provided, the plant
#' names are added in the Plant column (see details).
#' @param javastics Path of JavaSTICS Optional, should be provided in addition
#' to usms_file to get the plant codes if the plant files used are not
#' in the workspace but in the JavaSTICS distribution (see Details).
#' @param verbose Logical value for displaying or not information while running
#'
#'
#' @param usm_name `r lifecycle::badge("deprecated")` `usm_name` is no
#' longer supported, use `usm` instead.
#' @param var_list `r lifecycle::badge("deprecated")` `var_list` is no
#' longer supported, use `var` instead.
#' @param dates_list `r lifecycle::badge("deprecated")` `dates_list` is no
#' longer supported, use `dates` instead.
#' @param usms_filepath `r lifecycle::badge("deprecated")` `usms_filepath`
#' is no longer supported, use `usms_file` instead.
#' @param javastics_path `r lifecycle::badge("deprecated")` `javastics_path`
#' is no longer supported, use `javastics` instead.
#'
#' @details
#' If `usm` is not specified (or equal to `NULL`), the
#' function reads the files from all usms in the `workspace`(s).
#'
#' If `usms_file` is provided and if the associated plant file is found,
#' the plant names in the "Plant" column of the generated `data.frame`
#' are either the plant code (as specified in the plant file) or
#' the name of the plant file, if the plant file is not found.
#'
#' If `usms_file` is not specified, the plants are named "plant_1" by default
#' (+ "plant_2" for intercrops).
#'
#' @return A list, where each element is a `data.frame` of simulation results
#' for the given usm. The list is named after the USM name.
#'
#' Intercrops are returned in a single `data.frame`, and are identified
#' using either the "Plant" or "Dominance" columns.
#'
#' See Details section for more information about the "Plant" column.
#'
#' @examples
#' path <- get_examples_path(file_type = "sti")
#' sim_data <- get_sim(path, "banana")
#'
#' @export
#'
get_sim <- function(workspace,
usm = NULL,
var = NULL,
dates = NULL,
usms_file = NULL,
javastics = NULL,
verbose = TRUE,
usm_name = lifecycle::deprecated(),
var_list = lifecycle::deprecated(),
dates_list = lifecycle::deprecated(),
usms_filepath = lifecycle::deprecated(),
javastics_path = lifecycle::deprecated()) {
# Managing deprecated arguments
# usm_name
if (lifecycle::is_present(usm_name)) {
lifecycle::deprecate_warn("1.0.0",
"get_sim(usm_name)",
"get_sim(usm)")
} else {
usm_name <- usm # to remove when we update inside the function
}
# var_list
if (lifecycle::is_present(var_list)) {
lifecycle::deprecate_warn("1.0.0",
"get_sim(var_list)",
"get_sim(var)")
} else {
var_list <- var # to remove when we update inside the function
}
# dates_list
if (lifecycle::is_present(dates_list)) {
lifecycle::deprecate_warn("1.0.0",
"get_sim(dates_list)",
"get_sim(dates)")
} else {
dates_list <- dates # to remove when we update inside the function
}
# usms_filepath
if (lifecycle::is_present(usms_filepath)) {
lifecycle::deprecate_warn("1.0.0",
"get_sim(usms_filepath)",
"get_sim(usms_file)")
} else {
usms_filepath <- usms_file # to remove when we update inside the function
}
# javastics_path
if (lifecycle::is_present(javastics_path)) {
lifecycle::deprecate_warn("1.0.0",
"get_sim(javastics_path)",
"get_sim(javastics)")
} else {
javastics_path <- javastics # to remove when we update inside the function
}
res <- get_file(
workspace, usm_name, var_list, dates_list, usms_filepath,
javastics_path, verbose, "sim"
)
# Testing if results list is not empty
# otherwise, setting "cropr_simulation" class attribute will fail
if (length(res) > 0) {
attr(res, "class") <- "cropr_simulation"
}
res
}