-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_scenarios_for_p4b.R
165 lines (126 loc) · 4.49 KB
/
format_scenarios_for_p4b.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
logger::log_info("Determine sectors to include in P4B scenarios for target market share calculation.")
market_share_sectors <- c(
"Automotive",
"Coal",
"Oil&Gas",
"Power"
)
logger::log_info("Determine sectors to include in P4B scenarios for emission intensity/SDA calculation.")
emission_intensity_sectors <- c(
"Aviation",
"Cement",
"Steel"
)
logger::log_info("Ingerit reference year from config_name.")
if (nchar(gsub("Q.$", "", config_name)) == 4 & as.integer(gsub("Q.$", "", config_name)) >= 2020) {
reference_year <- as.integer(gsub("Q.$", "", config_name))
logger::log_info("config_name valid as a reference year. Preparing P4B scenarios with reference year {reference_year}.")
} else {
logger::log_error("{config_name} cannot be used as a reference year.")
stop()
}
logger::log_info("Read processed scenarios: {scenarios_to_include}.")
scenarios_p4b <- NULL
for (scenario in scenarios_to_include) {
scenario_i <- readr::read_csv(
file.path(scenario_preparation_outputs_path, paste0(scenario, ".csv")),
col_types = readr::cols_only(
source = "c",
scenario = "c",
scenario_geography = "c",
sector = "c",
indicator = "c",
units = "c",
year = "i",
technology = "c",
value = "d"
)
)
scenarios_p4b <- dplyr::bind_rows(scenarios_p4b, scenario_i)
}
logger::log_info("Derive common final year across scenarios used.")
final_year <- dplyr::summarise(
scenarios_p4b,
final_year_source = max(year, na.rm = TRUE),
.by = "source"
)
logger::log_info("Define interpolation groups for interpolation of yearly values.")
interpolation_groups <- c(
"source",
"scenario",
"sector",
"technology",
"scenario_geography",
"indicator",
"units"
)
logger::log_info("Prepare processed scenarios for use in P4B market share calculation.")
scenario_input_p4b <- pacta.scenario.data.preparation::interpolate_yearly(
data = scenarios_p4b,
!!!rlang::syms(interpolation_groups)
)
scenario_input_p4b <- dplyr::filter(
scenario_input_p4b,
.data$year >= .env$reference_year,
.data$sector %in% .env$market_share_sectors
)
final_year_by_sector_market_share <- dplyr::summarise(
scenario_input_p4b,
final_year = max(.data$year, na.rm = TRUE),
.by = "sector"
)
final_year_market_share <- min(final_year_by_sector_market_share$final_year, na.rm = TRUE)
scenario_input_p4b <- dplyr::filter(
scenario_input_p4b,
.data$year <= .env$final_year_market_share
)
scenario_input_p4b <- pacta.scenario.data.preparation::add_market_share_columns(
data = scenario_input_p4b,
reference_year = reference_year
)
scenario_input_p4b <- pacta.scenario.data.preparation::format_p4b(scenario_input_p4b)
if (pacta.data.validation::validate_intermediate_scenario_output(scenarios_p4b)) {
logger::log_info("{reference_year} scenarios for P4B input are valid.")
output_path <- fs::path(scenario_preparation_outputs_path, paste0("p4b_scenarios_", reference_year, ".csv"))
readr::write_csv(
x = scenario_input_p4b,
file = output_path
)
logger::log_info("P4B {reference_year}: P4B {reference_year} scenario data saved to {output_path}.")
} else {
logger::log_error("P4B {reference_year} scenario data is not valid.")
stop()
}
logger::log_info("Prepare processed scenarios for use in P4B EI/SDA calculation.")
scenario_input_p4b_ei <- pacta.scenario.data.preparation::interpolate_yearly(
data = scenarios_p4b,
!!!rlang::syms(interpolation_groups)
)
scenario_input_p4b_ei <- dplyr::filter(
scenario_input_p4b_ei,
.data$year >= .env$reference_year,
.data$sector %in% .env$emission_intensity_sectors
)
final_year_by_sector_ei <- dplyr::summarise(
scenario_input_p4b,
final_year = max(.data$year, na.rm = TRUE),
.by = "sector"
)
final_year_ei <- min(final_year_by_sector_ei$final_year, na.rm = TRUE)
scenario_input_p4b <- dplyr::filter(
scenario_input_p4b,
.data$year <= .env$final_year_ei
)
scenario_input_p4b_ei <- pacta.scenario.data.preparation::format_p4b_ei(scenario_input_p4b_ei)
if (pacta.data.validation::validate_intermediate_scenario_output(scenarios_p4b)) {
logger::log_info("{reference_year} scenarios for P4B EI input are valid.")
output_path <- fs::path(scenario_preparation_outputs_path, paste0("p4b_ei_scenarios_", reference_year, ".csv"))
readr::write_csv(
x = scenario_input_p4b_ei,
file = output_path
)
logger::log_info("P4B EI {reference_year}: P4B EI {reference_year} scenario data saved to {output_path}.")
} else {
logger::log_error("P4B EI {reference_year} scenario data is not valid.")
stop()
}