-
Notifications
You must be signed in to change notification settings - Fork 6
/
load_output.R
executable file
·139 lines (129 loc) · 3.32 KB
/
load_output.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
#' @include trollsim.R
#' @importFrom readr read_tsv cols read_file
#' @importFrom dplyr bind_rows n filter
#' @importFrom reshape2 melt dcast
#' @importFrom lidR readLAS LAS
NULL
#' Load outputs from simulation
#'
#' `load_output` load outputs from `TROLL` simulation files using `TROLL`
#' simulation `name` and `path`.
#'
#' @param name char. Name given to the model output.
#' @param path char. Path where the model is saved.
#' @param thin int. Vector of integers corresponding to the iterations to be
#' kept to reduce output size, default is NULL and corresponds to no thinning.
#'
#' @return An S4 [trollsim()] class object.
#'
#' @seealso [trollsim()], [trollstack()], [load_sim()], [load_stack()]
#'
#' @examples
#' \dontrun{
#' load_output("test", "./")
#' }
#'
#' @export
load_output <- function(name,
path,
thin = NULL) {
# tidyverse
iter <- NULL
# Check inputs
if (!all(unlist(lapply(list(name, path), class)) %in% c("character"))) {
stop("name and path should be character.")
}
# @inputs
inputs <- lapply(
list(
global = "global",
species = "species",
climate = "climate",
daily = "daily"
),
function(x) {
read_tsv(file.path(path, paste0(name, paste0("_input_", x, ".txt"))),
col_types = cols()
)
}
)
lidar_file <- file.path(path, paste0(name, paste0("_input_lidar.txt")))
inputs$lidar <- data.frame()
if (file.exists(lidar_file)) {
inputs$lidar <- read_tsv(lidar_file, col_types = cols())
}
forest_file <- file.path(path, paste0(name, paste0("_input_forest.txt")))
inputs$forest <- data.frame()
if (file.exists(forest_file)) {
inputs$forest <- read_tsv(forest_file, col_types = cols())
}
# @parameters
parameters <- inputs$global$value
names(parameters) <- inputs$global$param
# @log
log <- read_file(file.path(path, paste0(name, "_log.txt")))
# @forest
initial_pattern <- read_tsv(
file.path(path, paste0(name, paste0("_0_initial_pattern.txt"))),
col_types = cols()
)
final_pattern <- read_tsv(
file.path(path, paste0(name, paste0("_0_final_pattern.txt"))),
col_types = cols()
)
if (nrow(initial_pattern) > 0) {
forest <- bind_rows(initial_pattern, final_pattern)
} else {
forest <- final_pattern
}
# @ecosystem
ecosystem <- read_tsv(
file.path(
path,
paste0(name, "_0_", "sumstats", ".txt")
),
col_types = cols()
)
if (!is.null(thin)) {
ecosystem <- ecosystem %>%
filter(iter %in% thin)
}
# @species
species_file <- file.path(
path,
paste0(name, "_0_", "sumstats_species", ".txt")
)
if (file.exists(species_file)) {
species <- read_tsv(species_file,
col_types = cols()
)
if (!is.null(thin)) {
species <- species %>%
filter(iter %in% thin)
}
} else {
species <- data.frame()
}
# @las
las_file <- file.path(path, paste0(name, "_0", "", ".las"))
if (file.exists(las_file)) {
file.copy(las_file, paste0(las_file, ".save.las"))
las <- list(readLAS(file.path(las_file)))
} else {
las <- list()
}
return(
trollsim(
name = name,
path = path,
mem = TRUE,
parameters = parameters,
inputs = inputs,
log = log,
forest = forest,
ecosystem = ecosystem,
species = species,
las = las
)
)
}