-
Notifications
You must be signed in to change notification settings - Fork 45
/
calcEmber.R
61 lines (49 loc) · 2.13 KB
/
calcEmber.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
#' @title calc Ember
#' @description prepare the yearly Ember electricity data set
#' To use only a part of the Ember data, call calcOutput("Ember", subtype = "...")
#' and convert to TW if you want to use capacities as input data to REMIND.
#'
#' @param subtype data subtype. Either "capacity", "generation" or "all"
#'
#' @return A [`magpie`][magclass::magclass] object.
#'
#' @seealso [`calcOutput()`]
#'
#' @author Pascal Weigmann
#'
#'
#' @export
calcEmber <- function(subtype = "all") {
# get Ember data
x <- readSource("Ember")
if (subtype == "capacity") {
# choose only capacity variables
x <- x[, , "GW"]
description <- "Capacities from the yearly Ember electricity data set"
} else if (subtype == "generation") {
# choose only generation variables
x <- x[, , "TWh"]
description <- "Electricity generation from the yearly Ember electricity data set"
} else if (subtype == "all") {
# remove all variables with % as unit, as they are not used and make data preparation more complicated
x <- x[, , "%", invert = TRUE]
description <- "Capacities and electricity generation from the yearly Ember electricity data set"
} else {
stop("Not a valid subtype!")
}
# load and apply mapping to chosen variables
map <- toolGetMapping("Mapping_Ember.csv", type = "reportingVariables", where = "mrremind") %>%
filter(!is.na(!!sym("REMIND_Variable")), !!sym("REMIND_Variable") != "") # remove incomplete mapping rows
# combine variable and unit in mapping
map$REMIND_Variable_Unit <- paste0(map$REMIND_Variable, " (", map$REMIND_Unit, ")")
# drop unit dimension as unit is now part of the variable name
x <- collapseDim(x, dim = 3.2)
# apply mapping - careful: units are changed already, values not converted yet
x <- toolAggregate(x, dim = 3.1, rel = map, from = "Variable",
to = "REMIND_Variable_Unit", partrel = TRUE, verbosity = 2)
# convert values using factors from mapping
for (var in getNames(x, dim = 1)) {
x[, , var] <- x[, , var] * map[map$REMIND_Variable_Unit == var, "Factor"]
}
return(list(x = x, weight = NULL, unit = "various", description = description))
}