-
Notifications
You must be signed in to change notification settings - Fork 6
/
writeAntaresOutput.R
133 lines (113 loc) · 3.77 KB
/
writeAntaresOutput.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
#' @title Write output value for Antares
#'
#' @description
#' `r antaresEditObject:::badge_api_no()`
#'
#' This function write all output values for an Antares study.
#'
#' @param data obtain with readAntares
#' @param opts
#' List of simulation parameters returned by the function
#' [antaresRead::setSimulationPath()]
#'
#' @examples
#' \dontrun{
#'
#' library(antaresRead)
#' library(data.table)
#' opts <- setSimulationPath("my_study")
#' data <- readAntares(links = "all", areas = "all", clusters = "all")
#' writeOutputValues(data)
#'
#' }
#'
#' @export
writeOutputValues <- function(data, opts = NULL) {
if (!"antaresDataList" %in% class(data)) {
data <- list(data)
}
if (is.null(opts)) {
opts <- attributes(data[[1]])$opts
}
assertthat::assert_that(inherits(opts, "simOptions"))
api_not_implemented(opts)
lapply(data, function(data) {
typeS <- attributes(data)$type
if (typeS %in% c("areas", "links")) {
file = "values-"
}
if (typeS == c("clusters")) {
file = "details-"
}
if (typeS == c("clustersRes")) {
file = "details-res-"
}
type <- substr(typeS, 1, nchar(typeS) - 1)
if (type %in% c("cluster", "clustersRe")) {
type2 = "area"
} else{
type2 = type
}
if (type %in% c("cluster", "clustersRe")) {
typeS2 = "areas"
} else{
typeS2 = typeS
}
##Load output
sapply(unique(data[[type2]]), function(sel) {
if (is.null(data$mcYear))
data$mcYear <- 0
sapply(unique(data$mcYear), function(mcY) {
datatp <-
data[eval(parse(text = paste0(
type2, "=='", sel, "' & mcYear == mcY"
)))]
sinthesys <- attributes(datatp)$synthesis
ori_type <- attributes(datatp)$type
if (type %in% c("cluster", "clustersRe")) {
id <- antaresRead::getIdCols(datatp)
id <- id[id != "cluster"]
colValues <- setdiff(colnames(datatp), c("cluster",id))
ts <- attributes(datatp)$timeStep
if (ts == "weekly"){
weekOrder <- unique(datatp[, id, with = F]$week)
datatp[, year := as.integer(factor(cumsum(week == unique(week)[1]))), by = c("area", "cluster", "mcYear")] #handle double week (first and last)
id <- c(id, "year")
}
tmp_formula <-
as.formula(paste0(paste0(id, collapse = "+"), "~cluster"))
datatp <-
dcast(datatp,
tmp_formula,
value.var = colValues)
if (ts == "weekly") datatp <- datatp[order(year, factor(week, levels = weekOrder))][, year := NULL]
class(datatp) <- c(class(datatp), "antaresDataTable")
attributes(datatp)$synthesis <- sinthesys
attributes(datatp)$timeStep <- ts
attributes(datatp)$type <- ori_type
}
file_name <-
paste0(file, attributes(datatp)$timeStep, ".txt")
file_name_new <-
paste0(file, attributes(datatp)$timeStep, "_new.txt")
if (sinthesys) {
yd <- "mc-all"
file_path <- file.path(opts$simDataPath, yd, typeS2, sel)
} else{
yd <- "mc-ind"
year_for_write <-
formatC(mcY,
width = 5,
format = "d",
flag = "0")
file_path <-
file.path(opts$simDataPath, yd, year_for_write, typeS2, sel)
}
file_name_new <- file.path(file_path, file_name_new)
file_name <- file.path(file_path, file_name)
if (ori_type %in% c("areas", "links") || file.exists(file_name))
.writeAntaresOutput(file_name, file_name_new, datatp, opts = opts)
})
})
})
}