-
Notifications
You must be signed in to change notification settings - Fork 13
/
load.R
317 lines (292 loc) · 11.1 KB
/
load.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
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
if (getRversion() >= "3.1.0") {
utils::globalVariables(c("fun", "loadTime", "package"))
}
#' File extensions map
#'
#' How to load various types of files in R.
#'
#' @export
#' @rdname loadFiles
.fileExtensions <- function() {
.fE <- data.frame(matrix(ncol = 3, byrow = TRUE, c(
"Rdata", "load", "base",
"rdata", "load", "base",
"RData", "load", "base",
"rds", "readRDS", "base",
"RDS", "readRDS", "base",
"tif", "raster", "raster",
"png", "raster", "raster",
"csv", "read.csv", "utils",
"shp", "readOGR", "rgdal",
"txt", "read.table", "utils",
"asc", "raster", "raster")),
stringsAsFactors = FALSE)
colnames(.fE) <- c("exts", "fun", "package")
return(.fE)
}
#' Extract filename (without extension) of a file
#'
#' @param x List or character vector
#'
#' @return A character vector.
#'
#' @author Eliot McIntire
fileName <- function(x) {
return(unlist(strsplit(basename(unlist(x)), "\\..*$")))
}
# The load doEvent
doEvent.load <- function(sim, eventTime, eventType, debug = FALSE) { # nolint
if (eventType == "inputs") {
sim <- loadFiles(sim)
}
return(invisible(sim))
}
###############################################################################
#' Load simulation objects according to \code{filelist}
#'
#' This function has two roles: 1) to proceed with the loading of files that
#' are in a simList or 2) as a short cut to simInit(inputs = filelist). Generally
#' not to be used by a user.
#'
#' @seealso \code{\link{inputs}}
#'
#' @param sim \code{simList} object.
#'
#' @param filelist \code{list} or \code{data.frame} to call \code{loadFiles} directly from the
#' \code{filelist} as described in Details
#'
#' @param ... Additional arguments.
#'
#' @author Eliot McIntire and Alex Chubaty
#' @export
#' @importFrom data.table := data.table rbindlist
#' @importFrom stringi stri_detect_fixed
#' @importFrom raster inMemory
#' @importFrom utils getFromNamespace
#' @include simulation-simInit.R
#' @name loadFiles
#' @rdname loadFiles
#'
#' @examples
#' \dontrun{
#'
#' # Load random maps included with package
#' filelist <- data.frame(
#' files = dir(system.file("maps", package = "quickPlot"),
#' full.names = TRUE, pattern = "tif"),
#' functions = "rasterToMemory", package = "quickPlot"
#' )
#' sim1 <- loadFiles(filelist = filelist)
#' clearPlot()
#' if (interactive()) Plot(sim1$DEM)
#'
#' # Second, more sophisticated. All maps loaded at time = 0, and the last one is reloaded
#' # at time = 10 and 20 (via "intervals").
#' # Also, pass the single argument as a list to all functions...
#' # specifically, when add "native = TRUE" as an argument to the raster function
#' files = dir(system.file("maps", package = "quickPlot"),
#' full.names = TRUE, pattern = "tif")
#' arguments = I(rep(list(native = TRUE), length(files)))
#' filelist = data.frame(
#' files = files,
#' functions = "raster::raster",
#' objectName = NA,
#' arguments = arguments,
#' loadTime = 0,
#' intervals = c(rep(NA, length(files)-1), 10)
#' )
#'
#' sim2 <- loadFiles(filelist = filelist)
#'
#' # if we extend the end time and continue running, it will load an object scheduled
#' # at time = 10, and it will also schedule a new object loading at 20 because
#' # interval = 10
#' end(sim2) <- 20
#' sim2 <- spades(sim2) # loads the percentPine map 2 more times, once at 10, once at 20
#' }
setGeneric("loadFiles", function(sim, filelist, ...) {
standardGeneric("loadFiles")
})
#' @rdname loadFiles
setMethod(
"loadFiles",
signature(sim = "simList", filelist = "missing"),
definition = function(sim, ...) {
# Pull .fileExtensions() into function so that scoping is faster
.fileExts <- .fileExtensions()
if (NROW(inputs(sim)) != 0) {
inputs(sim) <- .fillInputRows(inputs(sim), start(sim))
filelist <- inputs(sim) # does not create a copy - because data.table ... this is a pointer
curTime <- time(sim, timeunit(sim))
arguments <- inputArgs(sim)
# Check if arguments is a named list; the name may be concatenated
# with the "arguments", separated by a ".". This will extract that.
if ((length(arguments) > 0) & !is.null(names(arguments))) {
if (grepl(".", fixed = TRUE, names(filelist)[pmatch("arguments", names(filelist))]))
names(arguments) <- sapply(strsplit(
names(filelist)[pmatch("arguments", names(filelist))], ".", fixed = TRUE),
function(x) x[-1]
)
}
# check if arguments should be, i.e,. recycled
if (!is.null(arguments)) {
if (length(arguments) < length(filelist$file)) {
arguments <- rep(arguments, length.out = length(filelist$file))
}
}
# only load those that are to be loaded at their loadTime
cur <- (filelist$loadTime == curTime) & !(sapply(filelist$loaded, isTRUE))
if (any(cur)) {
# load files
loadPackage <- filelist$package
loadFun <- filelist$fun
for (y in which(cur)) {
nam <- names(arguments[y])
if (is.na(filelist$file[y])) {
# i.e., only for objects
if (!is.na(loadFun[y])) {
if (is.na(loadPackage[y])) {
if (exists(loadFun[y])) {
objList <- list(do.call(get(loadFun[y]), arguments[[y]]))
} else {
stop("'inputs' often requires (like now) that package be specified",
" explicitly in the 'fun' column, e.g., base::load")
}
} else {
objList <- list(do.call(getFromNamespace(loadFun[y], loadPackage[y]), arguments[[y]])) # nolint
}
} else {
objListEnv <- quickPlot::whereInStack(filelist$objectName[y])
objList <- list(get(filelist$objectName[y], objListEnv))
}
names(objList) <- filelist$objectName[y]
if (length(objList) > 0) {
list2env(objList, envir = sim@.xData)
filelist[y, "loaded"] <- TRUE
message(filelist[y, "objectName"], " loaded into simList")
} else {
message("Can't find object '", filelist$objectName[y], "'. ",
"To correctly transfer it to the simList, it should be ",
"in the search path.")
}
} else {
# for files
if (is.na(loadPackage[y])) {
if (!exists(loadFun[y])) {
stop("'inputs' often requires (like now) that package be specified",
" explicitly in the 'fun' column, e.g., base::load")
}
}
if (!is.null(nam)) {
argument <- list(unname(unlist(arguments[y])), filelist[y, "file"])
if (is.na(loadPackage[y])) {
names(argument) <- c(nam, names(formals(get(loadFun[y])))[1])
} else {
names(argument) <- c(nam, names(formals(getFromNamespace(loadFun[y], loadPackage[y])))[1])
}
} else {
argument <- list(filelist[y, "file"])
if (is.na(loadPackage[y])) {
names(argument) <- names(formals(get(loadFun[y])))[1]
} else {
names(argument) <- names(formals(getFromNamespace(loadFun[y], loadPackage[y])))[1]
}
}
# The actual load call
if (identical(loadFun[y], "load")) {
do.call(getFromNamespace(loadFun[y], loadPackage[y]),
args = argument, envir = sim@.xData)
} else {
sim[[filelist[y, "objectName"]]] <-
if (is.na(loadPackage[y])) {
do.call(get(loadFun[y]), args = argument)
} else {
do.call(getFromNamespace(loadFun[y], loadPackage[y]),
args = argument)
}
}
filelist[y, "loaded"] <- TRUE
if (loadFun[y] == "raster") {
message(paste0(
filelist[y, "objectName"], " read from ", filelist[y, "file"], " using ", loadFun[y], # nolint
"(inMemory=", inMemory(sim[[filelist[y, "objectName"]]]), ")",
ifelse(filelist[y, "loadTime"] != sim@simtimes[["start"]],
paste("\n at time", filelist[y, "loadTime"]), "")
))
} else {
message(paste0(
filelist[y, "objectName"], " read from ", filelist[y, "file"], " using ", loadFun[y], # nolint
ifelse(filelist[y, "loadTime"] != sim@simtimes[["start"]],
paste("\n at time", filelist[y, "loadTime"]), "")
))
}
}
} # end y
# add new rows of files to load based on filelistDT$Interval
if (!is.na(match("intervals", names(filelist)))) {
if (any(!is.na(filelist[filelist$loaded, "intervals"]))) {
newFilelist <- filelist[(filelist$loaded & !is.na(filelist$intervals)), ]
newFilelist[, c("loadTime", "loaded", "intervals")] <-
data.frame(curTime + newFilelist$intervals, NA, NA_real_)
filelist <- rbind(filelist, newFilelist)
}
}
} # if there are no files to load at curTime, then nothing
if (is(filelist, "data.frame")) {
inputs(sim) <- filelist # this is required if intervals is used
} else if (is(filelist, "list")) {
inputs(sim) <- c(as.list(filelist), arguments = arguments)
} else {
stop("filelist must be either a list or data.frame")
}
}
return(invisible(sim))
})
#' @rdname loadFiles
setMethod("loadFiles",
signature(sim = "missing", filelist = "ANY"),
definition = function(filelist, ...) {
sim <- simInit(times = list(start = 0.0, end = 1),
params = list(),
inputs = filelist,
modules = list(), ...)
return(invisible(sim))
})
#' @rdname loadFiles
setMethod("loadFiles",
signature(sim = "missing", filelist = "missing"),
definition = function(...) {
message("no files loaded because sim and filelist are empty")
})
#######################################################
#' Read raster to memory
#'
#' Wrapper to the \code{raster} function, that creates the raster object in
#' memory, even if it was read in from file.
#'
#' @param x An object passed directly to the function raster (e.g., character string of a filename).
#'
#' @param ... Additional arguments to \code{raster}.
#'
#' @return A raster object whose values are stored in memory.
#'
#' @seealso \code{\link{raster}}.
#'
#' @name rasterToMemory
#' @importFrom raster getValues raster setValues
#' @export
#' @rdname rasterToMemory
#'
#' @author Eliot McIntire and Alex Chubaty
#'
setGeneric("rasterToMemory", function(x, ...) {
standardGeneric("rasterToMemory")
})
#' @rdname rasterToMemory
setMethod("rasterToMemory",
signature = c(x = "ANY"),
definition = function(x, ...) {
r <- raster(x, ...)
r <- setValues(r, getValues(r))
return(r)
})