-
Notifications
You must be signed in to change notification settings - Fork 6
/
computeTimeStepFromHourly.R
380 lines (324 loc) · 15.3 KB
/
computeTimeStepFromHourly.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
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
#' @title Computation function for rebuild mc-ind daily, monthly and annual from hourly data.
#'
#' @param hourlydata antaresData for hourly timestep
#' @param timeStep timestep of aggregation (daily, monthly and annual, NO weekly)
#' @param type type of data (areas, links, clusters, clustersRes)
#'
#' @import data.table
#'
#' @keywords internal
.hourlyToOther <- function(hourlydata, timeStep, type){
# Get proper time from timeStep
char_timeStep = switch(timeStep,
"daily" = 10, #XXXX-XX-XX
"monthly" = 7, #XXXX-XX
"annual" = 4) #XXXX
# Columns for aggregate based on timeStep
agg_columns = switch(timeStep,
"daily" = c("mcYear", "time", "month", "day"),
"monthly" = c("mcYear", "time", "month"),
"annual" = c("mcYear", "time"))
# Columns for aggregate based on type
agg_columns = switch(type,
"areas" = c("area", agg_columns),
"links" = c("link", agg_columns),
"clusters" = c("area", "cluster", agg_columns),
"clustersRes" = c("area", "cluster", agg_columns))
colForMean = switch(type,
"areas" = c("MRG. PRICE", "H. LEV"),
"links" = character(0),
"clusters" = character(0),
"clustersRes" = character(0))
colForMax = switch(type,
"areas" = "LOLP",
"links" = c("CONG. PROB +", "CONG. PROB -"),
"clusters" = character(0),
"clustersRes" = character(0))
colorder <- colnames(hourlydata)
idcols <- getIdCols(hourlydata)
res <- copy(hourlydata)[, time := substr(as.character(time),1,char_timeStep)][, hour := NULL]
if (timeStep == "annual") res[, time := "Annual"] #to groupby both years of horizon in one
resSum <- res[, lapply(.SD, sum),
.SDcols = colorder[!(colorder %in% idcols) &
!(colorder %in% colForMean) & !(colorder %in% colForMax)],
by = agg_columns]
if (length(colForMean) > 0){
resMean <- res[, lapply(.SD, function(x){round(mean(x),2)}), .SDcols = colForMean, by = agg_columns]
resFinal <- merge(resSum, resMean, by = agg_columns)
} else resFinal <- resSum
if (length(colForMax) > 0){
resMax <- res[, lapply(.SD, max), .SDcols = colForMax, by = agg_columns]
resFinal <- merge(resFinal, resMax, by = agg_columns)
}
if (timeStep == "annual") {
setnames(resFinal, "time", "annual")
colorder <- gsub("time", "annual", colorder)
}
setcolorder(resFinal, intersect(colorder, colnames(resFinal)))
}
#' @title Computation function for rebuild mc-ind weekly from daily data.
#'
#' @param dailydata antaresData for daily timestep
#' @param opts study opts
#' @param type type of data (areas, links, clusters, clustersRes)
#'
#' @keywords internal
.dailyToWeekly <- function(dailydata, opts = simOptions(), type){
colorder <- gsub("time", "timeId", colnames(dailydata))
colForMean = switch(type,
"areas" = c("MRG. PRICE", "H. LEV"),
"links" = character(0),
"clusters" = character(0),
"clustersRes" = character(0))
colForMax = switch(type,
"areas" = "LOLP",
"links" = c("CONG. PROB +", "CONG. PROB -"),
"clusters" = character(0),
"clustersRes" = character(0))
# Columns for aggregate based on type
agg_columns = switch(type,
"areas" = c("area", "mcYear", "year", "week"),
"links" = c("link", "mcYear", "year", "week"),
"clusters" = c("area", "cluster", "mcYear", "year", "week"),
"clustersRes" = c("area", "cluster", "mcYear", "year", "week"))
weekdays <- c("Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday")
weekdays <- c(weekdays, weekdays)
firstDayWeek <- opts$firstWeekday
firstDayYear <- opts$parameters$general$january.1st
year <- as.integer(substr(opts$parameters$general$horizon,6,10))
if (is.na(year)) year <- as.integer(opts$parameters$general$horizon) + 1
# Set up general week
firstidx <- which(weekdays %in% firstDayWeek)[1]
weekdays <- weekdays[firstidx:(firstidx+6)]
# Set up year two
alldaysYearTwo <- seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),by="1 day")
leapYear <- paste0(year,"-02-29") %in% as.character(alldaysYearTwo)
# Set up week/Year
firstidx <- which(weekdays %in% firstDayYear)
daysleftYearTwo <- daysleft <- length(weekdays[firstidx:length(weekdays)])
weekVector <- rep(1,daysleft)
weekVector <- c(weekVector, unlist(lapply(2:52, rep.int, times = 7)))
weekVector <- c(weekVector, rep(1, ifelse(leapYear, 366, 365) - length(weekVector)))
# First merge
alldaysYearTwo <- data.table(day = alldaysYearTwo, week2 = weekVector)
alldaysYearTwo[, year2 := year(day)]
res <- merge(copy(dailydata)[, time := as.Date(time)], alldaysYearTwo, by.x = "time", by.y = "day", all.x = T)
# Set up year One
alldaysYearOne <- seq(as.Date(paste0(year-1,"-01-01")),as.Date(paste0(year-1,"-12-31")),by="1 day")
leapYear <- paste0(year-1,"-02-29") %in% as.character(alldaysYearOne)
# Set up week/Year
firstidx <- (firstidx - ifelse(leapYear, 2, 1))%%7
daysleft <- length(weekdays[firstidx:length(weekdays)])
weekVector <- rep(1,daysleft)
weekVector <- c(weekVector, unlist(lapply(2:52, rep.int, times = 7)))
weekVector <- c(weekVector, rep(1, 7 - daysleftYearTwo))
# Second merge
alldaysYearOne <- data.table(day = alldaysYearOne, week1 = weekVector)
alldaysYearOne[, year1 := year(day)]
res <- merge(res, alldaysYearOne, by.x = "time", by.y = "day", all.x = T)
res[, `:=` (week = ifelse(is.na(week1),week2,week1),
year = ifelse(is.na(year1),year2,year1))][, c("week1", "week2", "year1", "year2") := NULL]
res <- res[week == 1, year := 0]
cols <- setdiff(colnames(res),c(getIdCols(dailydata),"week","year"))
resSum <- res[, lapply(.SD, sum), by= agg_columns,
.SDcols = cols[!(cols %in% colForMean | cols %in% colForMax)]]
if (length(colForMean) > 0){
resMean <- res[, lapply(.SD, function(x){round(mean(x),2)}),.SDcols = colForMean, by= agg_columns]
resFinal <- merge(resSum, resMean, by = agg_columns, sort = F)
} else resFinal <- resSum
if (length(colForMax) > 0){
resMean <- res[, lapply(.SD, max),.SDcols = colForMax, by= agg_columns]
resFinal <- merge(resFinal, resMean, by = agg_columns, sort = F)
}
resFinal[, timeId := week]
setcolorder(resFinal, intersect(colorder, colnames(resFinal)))[, year := NULL]
}
#' @title Compute daily, weekly, monthly and annual mc-ind from hourly data for one year. (new)
#'
#' @note Recommended only with studies spanning on two years.
#'
#' @param mcYear vector of years to compute
#' @param type type of data (areas, links, clusters, clustersRes)
#' @param areas vector of areas. links type will use getLinks() to get data.
#' @param opts study opts
#' @param timeStep timestep of aggregation (daily, monthly and annual, NO weekly)
#' @param writeOutput boolean to write data in mc-ind folder
#'
#' @seealso
#' \code{\link{computeOtherFromHourlyMulti}}
#'
#' @export
computeOtherFromHourlyYear <- function(mcYear,
type,
areas = "all",
opts = simOptions(),
timeStep = c("daily", "monthly", "annual", "weekly"),
writeOutput = F){
res <- list()
#for the eval(parse(text))
if (length(areas) == 1) selected <- ifelse(areas == "all", "areas", paste0("'",areas,"'"))
else if (type != "links") selected <- paste(list(areas), sep = ",")
else selected <- paste(list(getLinks(areas, internalOnly = T, opts = opts)),
sep = ",")
formula <- sprintf('readAntares(%s = %s, timeStep = "hourly",
mcYears = mcYear, showProgress = F, opts = opts)',
type, selected) #read any type data
hourlydata <- eval(parse(text = formula))
if (type == "clustersRes" && length(hourlydata) > 1) hourlydata <- hourlydata$clustersRes
# Multi timestep at once
# steps <- as.list(intersect(c("daily", "monthly", "annual"), timeStep))
# res <- llply(steps, .hourlyToOther, hourlydata = hourlydata, type = type,
# .parallel = T, .paropts = list(preschedule=TRUE))
# names(res) <- paste0(steps,"data")
# Separate timesteps
if ("daily" %in% timeStep) res$dailydata <- .hourlyToOther(hourlydata, timeStep = "daily", type = type)
if ("monthly" %in% timeStep) res$monthlydata <- .hourlyToOther(hourlydata, timeStep = "monthly", type = type)
if ("annual" %in% timeStep) res$annualdata <- .hourlyToOther(hourlydata, timeStep = "annual", type = type)
if ("weekly" %in% timeStep){
if (!"daily" %in% timeStep){
warning("Daily mc-ind needed to compute weekly. Computing daily mc-ind...")
res$weeklydata <- .dailyToWeekly(.hourlyToOther(hourlydata, timeStep = "daily", type = type), type = type, opts = opts)
}
else res$weeklydata <- .dailyToWeekly(res$dailydata, opts = opts, type = type)
}
rm(hourlydata)
for (nm in names(res)){ #only loop works for writing attributes
setattr(res[[nm]], "type", type)
setattr(res[[nm]], "synthesis", FALSE)
setattr(res[[nm]], "timeStep", gsub("data", "", nm))
}
if (writeOutput){
if (!is.null(res$dailydata)){
if (type == "links") res$dailydata <- res$dailydata[, timeId := rep(1:nrow(unique(.SD[, c("day", "month")])), length(unique(link)))]
else if (type == "areas") res$dailydata <- res$dailydata[, timeId := rep(1:nrow(unique(.SD[, c("day", "month")])), length(unique(area)))]
else res$dailydata <- res$dailydata[, timeId := rep(1:nrow(unique(.SD[, c("day", "month")])), nrow(unique(.SD[, c("area", "cluster")])))]
}
if (!is.null(res$monthlydata)){
if (type == "links") res$monthlydata <- res$monthlydata[, timeId := rep(1:12, length(unique(link)))]
else if (type == "areas") res$monthlydata <- res$monthlydata[, timeId := rep(1:12, length(unique(area)))]
else res$monthlydata <- res$monthlydata[, timeId := rep(1:12, nrow(unique(.SD[, c("area", "cluster")])))]
}
lapply(res, writeOutputValues, opts = opts)
# llply(res, writeOutputValues, opts = opts, .parallel = T, .paropts = list(preschedule=TRUE))
}
res
}
#' @title Compute daily, weekly, monthly and annual mc-ind from hourly data multiyear. (new)
#'
#' @note Recommended only with studies spanning on two years.
#'
#' @param opts study opts
#' @param areas vector of areas
#' @param type type of aggregation
#' @param timeStep timestep of aggregation (daily, monthly and annual, NO weekly)
#' @param mcYears vector of years to compute
#' @param writeOutput boolean to write data in mc-ind folder
#' @param nbcl number of cpu cores for parallelization
#' @param verbose logical for printing output
#'
#' @importFrom plyr llply ldply
#' @importFrom future plan
#' @importFrom doFuture registerDoFuture
#' @importFrom memuse Sys.meminfo
#'
#' @import progressr
#'
#' @seealso
#' \code{\link{computeOtherFromHourlyYear}}
#'
#' @export
computeOtherFromHourlyMulti <- function(opts = simOptions(),
areas = "all",
type = c("areas", "links", "clusters"),
timeStep = c("daily", "monthly", "annual", "weekly"),
mcYears = simOptions()$mcYears,
writeOutput = F,
nbcl = 8,
verbose = F){
if (verbose){
handlers(global = T)
handlers("progress")
}
res <- list()
parallel <- (nbcl > 1)
#Parallel config####
if (parallel){
closeAllConnections()
cl <- makeCluster(nbcl)
plan("multisession")
registerDoFuture()
paropts <- list(preschedule=TRUE)
clusterSetRNGStream(cl, 123)
clusterExport(cl=cl, "opts",
envir=environment())
clusterEvalQ(cl, library("antaresRead"))
}
#Dynamic batch value####
batch = floor(((as.numeric(Sys.meminfo()[[2]])/(1024*1024*1024)) * 0.7)/2)
if (batch > 1 & length(mcYears)%%batch == 1) batch <- batch + 1
if (verbose) cat("\nBatch :",batch,"\n")
for (j in 1:ceiling(length(mcYears)/batch)){
lst_idx = 0
left = (j-1)*batch + 1
right = min(length(mcYears), j*batch)
curr_years = setdiff(mcYears[left:right],NA)
if ("areas" %in% type){
gc()
exec_time <- Sys.time()
if (verbose) cat(c("\nComputing :", timeStep, "mc-ind (areas) from hourly...\n"))
resAreas <- llply(curr_years, computeOtherFromHourlyYear, opts = opts, writeOutput = writeOutput,
type = "areas", areas = areas, .parallel = parallel, .progress = "progressr",
.paropts = list(.options.snow = paropts))
res$areas <- resAreas
if (verbose){
cat(c("Areas : OK\n"))
print(Sys.time() - exec_time)
}
}
if ("links" %in% type){
gc()
exec_time <- Sys.time()
if (verbose) cat(c("\nComputing :", timeStep, "mc-ind (links) from hourly...\n"))
resLinks <- llply(curr_years, computeOtherFromHourlyYear, opts = opts, writeOutput = writeOutput,
type = "links", areas = areas, .parallel = parallel, .progress = "progressr",
.paropts = list(.options.snow = paropts))
res$links <- resLinks
if (verbose){
cat(c("Links : OK\n"))
print(Sys.time() - exec_time)
}
}
if ("clusters" %in% type){
gc()
exec_time <- Sys.time()
if (verbose) cat(c("\nComputing :", timeStep, "mc-ind (clusters) from hourly...\n"))
resClusters <- llply(curr_years, computeOtherFromHourlyYear, opts = opts, writeOutput = writeOutput,
type = "clusters", areas = areas, .parallel = parallel, .progress = "progressr",
.paropts = list(.options.snow = paropts))
res$clusters <- resClusters
if (verbose){
cat(c("Clusters : OK\n"))
print(Sys.time() - exec_time)
}
}
# Compute cluster RES disabled for now (TODO)
# gc()
# if (opts$antaresVersion >= 810 && opts$parameters$`other preferences`$`renewable-generation-modelling` == "clusters"){
# cat(c("\nComputing :", timeStep, "mc-ind (clusters Res) from hourly...\n"))
# resClustersRes <- llply(curr_years, computeOtherFromHourlyYear, opts = opts, writeOutput = writeOutput,
# type = "clustersRes", areas = areas, .parallel = parallel, .progress = "progressr",
# .paropts = list(.options.snow = paropts))
# cat(c("Clusters Res : OK\n"))
# res <- list(resAreas, resLinks, resClusters, resClustersRes)
# } else res <- list(resAreas, resLinks, resClusters)
}
closeAllConnections()
print("Success.")
if (!writeOutput) return (res)
}