Skip to content
Snippets Groups Projects
Commit f5b0686d authored by Johannes Koch's avatar Johannes Koch
Browse files

:sparkles: Write policy costs to a reporting file

The computed policy costs are added to a new mif, created in the folder of the policy run. This file is a copy of the existing generic mif, but with the updated policy costs. Should be used for the AR6 submission.
parent 77676106
No related branches found
No related tags found
1 merge request!186:sparkles: Write policy costs to a reporting file
......@@ -38,7 +38,7 @@ rm_timestamp <- function(strings,
policy_costs_pdf <- function(policy_costs, fileName="PolicyCost.pdf") {
cat(paste0("A pdf with the name ",crayon::green(fileName)," is being created.\n"))
cat(paste0("A pdf with the name ",crayon::green(fileName)," is being created in the main remind folder.\n"))
template <- c("\\documentclass[a4paper,landscape,twocolumn]{article}",
"\\setlength{\\oddsidemargin}{-0.8in}",
......@@ -112,6 +112,34 @@ policy_costs_pdf <- function(policy_costs, fileName="PolicyCost.pdf") {
}
write_new_reporting <- function(mif_path, scen_name, new_polCost_data) {
new_mif_path <- paste0(substr(mif_path,1,nchar(mif_path)-4),"_adjustedPolicyCosts.mif")
cat(paste0("A mif file with the name ",crayon::green(paste0("REMIND_generic_",scen_name,"_adjustedPolicyCosts.mif"))," is being created in the ",scen_name," outputfolder.\n"))
my_data <- magclass::read.report(mif_path)
#my_variables <- grep("Policy Cost", magclass::getNames(my_data[[1]][[1]]), value = TRUE)
#my_data[[1]][[1]][,,my_variables] <- new_polCost_data[,,my_variables]
my_variables <- grep("Policy Cost", magclass::getNames(my_data[[1]][[1]]), value = TRUE, invert = T)
magclass::getSets(new_polCost_data)[1] <- "region"
magclass::getSets(new_polCost_data)[2] <- "year"
magclass::getSets(new_polCost_data)[3] <- "variable"
my_data <- magclass::mbind(my_data[[1]][[1]][,,my_variables], new_polCost_data)
my_data <- magclass::add_dimension(my_data,dim=3.1,add = "model",nm = "REMIND")
my_data <- magclass::add_dimension(my_data,dim=3.1,add = "scenario",nm = scen_name)
magclass::write.report(my_data, file = new_mif_path, ndigit = 7, skipempty = FALSE)
#magclass::write.report2(my_data, file=new_mif_path, ndigit=7, skipempty = FALSE)
}
# Check for an object called "source_include". If found, that means, this script
# is being called from another (output.R most likely), and the input variable
# "outputdirs" is already in the environment. If not found, "outputdirs" is given
......@@ -120,8 +148,8 @@ if(!exists("source_include")) {
# Set default value
outputdirs <- c("../../../output/default_2020-03-03_09.38.01",
"../../../output/default_slim_2020-03-03_11.37.51",
"../../../output/sa_emu_3_2_2020-03-13_16.41.08",
"../../../output/sa_emu_1_posYIntercept_posSlope_2020-04-14_11.08.07")
"../../../output/default_slim_2020-03-03_11.37.51",
"../../../output/default_2020-03-03_09.38.01")
# Make over-writtable from command line
lucode::readArgs("outputdirs")
......@@ -151,7 +179,7 @@ pc_pairs <- paste0(pol_names, "_w.r.t_",ref_names)
# are the ones she wanted.
if(exists("source_include")) {
cat(crayon::blue("\nPlease confirm the set-up:\n"))
cat("From the order with which you selected the directories, the following policy-cost curves will be created:\n")
cat("From the order with which you selected the directories, the following policy costs will be computed:\n")
cat(crayon::green(paste0("\t", pc_pairs ,"\n")))
cat("Is that what you intended?\n")
cat(paste0("Type '",crayon::green("y"),"' to continue, '",crayon::red("n"),"' to abort: "))
......@@ -169,7 +197,9 @@ if(exists("source_include")) {
cat(crayon::blue("\nPolicy cost computations:\n"))
# Get Policy costs for every policy-reference pair
tmp_policy_costs <- mapply(remind::reportPolicyCosts, pol_gdxs, ref_gdxs, SIMPLIFY = FALSE) %>%
tmp_policy_costs_magpie <- mapply(remind::reportPolicyCosts, pol_gdxs, ref_gdxs, SIMPLIFY = FALSE)
tmp_policy_costs <- tmp_policy_costs_magpie %>%
lapply(quitte::as.quitte) %>%
lapply(select, region, period, data, value)
......@@ -187,11 +217,17 @@ policy_costs <- policy_costs %>%
pivot_longer(cols = matches(".*w\\.r\\.t.*"), names_to = "Model Output") %>%
pivot_wider(names_from = data)
# Tell the user what is going on
cat(crayon::green("Done!\n"))
# Create Pdf
cat(crayon::blue("\nPdf creation:\n"))
time_stamp <- format(Sys.time(), "_%Y-%m-%d_%H.%M.%S")
policy_costs_pdf(policy_costs, fileName = paste0("PolicyCost",time_stamp,".pdf"))
cat(crayon::green("Done!\n"))
# Create new reporting file
cat(crayon::blue("\nMif creation:\n"))
pol_mifs <- paste0(dirname(pol_gdxs), "/REMIND_generic_", pol_names, ".mif")
return_check <- mapply(write_new_reporting, pol_mifs, pol_names, tmp_policy_costs_magpie)
cat(crayon::green("Done!\n\n"))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment