Skip to content

Commit

Permalink
format scenario data for p4b input
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobvjk committed May 24, 2024
1 parent 727086c commit 7fe2d63
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions main.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,120 @@ for (scenario in scenarios_to_include) {
}

logger::log_info("Finished processing scenarios.")


# format and save scenarios for use in P4B -------------------------------------

logger::log_info("Determining scenarios to include.")

scenarios_to_include <- config$inherits

logger::log_info("Scenarios to be included: {scenarios_to_include}")

logger::log_info("Processing scenarios for P4B input.")

library(dplyr)

market_share_sectors <- c(
"Automotive",
"Coal",
"Oil&Gas",
"Power"
)

emission_intensity_sectors <- c(
"Aviation",
"Cement",
"Steel"
)

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()
}

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)
}

interpolation_groups <- c(
"source",
"scenario",
"sector",
"technology",
"scenario_geography",
"indicator",
"units"
)

scenario_input_p4b <- scenarios_p4b %>%
pacta.scenario.data.preparation::interpolate_yearly(!!!rlang::syms(interpolation_groups)) %>%
filter(
.data$year >= .env$reference_year,
.data$sector %in% .env$market_share_sectors
) %>%
pacta.scenario.data.preparation::add_market_share_columns(reference_year = reference_year) %>%
pacta.scenario.data.preparation::format_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()
}

scenario_input_p4b_ei <- scenarios_p4b %>%
pacta.scenario.data.preparation::interpolate_yearly(!!!rlang::syms(interpolation_groups)) %>%
filter(
.data$year >= .env$reference_year,
.data$sector %in% .env$emission_intensity_sectors
) %>%
pacta.scenario.data.preparation::format_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()
}

0 comments on commit 7fe2d63

Please sign in to comment.