Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahel Mandaroux authored and Rahel Mandaroux committed Feb 5, 2025
1 parent 148eb0e commit 1829cac
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions R/readNewClimate.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Reads NPI policy database with technology capacity target from the Policy data base (v4 August 2024)
#' by PBL that translate the high impact policies of https://climatepolicydatabase.org/.
#' @description Reads excel sheet with NPi (National Policies Implemented)
#' data on different policy targets (capacity) with different variations:
#' data on different policy targets (capacity) with different variations:
#' unconditional for minimal and conditionnal for maximum targets.
#' NPI targets only include targets that are based on implemented policy instruments.
#' @details Country name is ISO coded. Capacity/Additional Capacity targets are in GW.
Expand All @@ -11,10 +11,9 @@
#' @importFrom readxl read_xlsx
#' @importFrom dplyr select


readNewClimate <- function(subtype) {


if (grepl("2025", subtype, fixed = TRUE)) { # keep structure to compare when new versions (NPi target updates)
NPIfile <- "NPi_2025-02-03.xlsx"
} else {
Expand All @@ -23,17 +22,15 @@ readNewClimate <- function(subtype) {
warning("\nNo data for year in ", subtype, " available. Choose default: ", NPIfile)
}
}

if (grepl("Capacity", subtype, fixed = TRUE)) {
# Capacity/Additional Capacity targets are in GW.
NPI <- read_excel(NPIfile, sheet = "Capacity_target_PBL_2025",
col_types = c("text", "skip", "numeric", "text", "text", "numeric",
"numeric", "numeric", "numeric", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric", "numeric", "skip", "skip", "skip"))
x <- as.magpie(NPI, spatial = 1, temporal = 2, datacol = 3)
}

if (grepl("Emissions", subtype, fixed = TRUE)) {
} else if (grepl("Emissions", subtype, fixed = TRUE)) {
input <- read_xlsx(NPIfile, sheet = "EmissionTargets", skip = 3, na = c("?", ""))
# select the relevant columns to work upon
input2 <- select(input, 2, 7:14)
Expand All @@ -54,29 +51,29 @@ readNewClimate <- function(subtype) {
paste(unique(input2$Type)[!unique(input2$Type) %in% allowedType], collapse = ", "),
". Please use: ", paste(allowedType, collapse = " or "), ".")
}

# drop extra columns
input2[is.na(input2$Unconditional), ]$Unconditional <- input2[is.na(input2$Unconditional), ]$Uncond2
input2[is.na(input2$Conditional), ]$Conditional <- input2[is.na(input2$Conditional), ]$Cond2
input2$Uncond2 <- NULL
input2$Cond2 <- NULL

# if conditional is empty, fill with unconditional
input2[is.na(input2$Conditional), ]$Conditional <- input2[is.na(input2$Conditional), ]$Unconditional

# in case a country has two or more types of targets for same year, use GHG-Absolute targets
# note: the only remaining country in 2021 is MGD Madagascar based on its 2016 submission
input2 <- input2[!(input2$ISO_Code %in% input2[duplicated(input2[c(1, 4)]), ]$ISO_Code &
input2$Target_Year %in% input2[duplicated(input2[c(1, 4)]), ]$Target_Year &
input2$Type != "GHG-Absolute"), ]

input2$Target_Year %in% input2[duplicated(input2[c(1, 4)]), ]$Target_Year &
input2$Type != "GHG-Absolute"), ]
# check whether conditional is more stringent than unconditional
condTrumpsUncond <- (input2$Conditional <= input2$Unconditional) | is.na(input2$Unconditional)
if (any(!condTrumpsUncond)) {
warning("readNewClimate with subtype=", subtype, ": unconditional target more stringent than conditional in: ",
paste(input2$ISO_Code[!condTrumpsUncond], collapse = ", "))
}

# warning if emission changes have no reference year or BAU reference
ref4change <- input2$ISO_Code[input2$Reference_Year %in% c("no", NA) &
input2$Type %in% c("GHG", "CO2/GDP", "GHG/GDP", "GHG-Absolute")]
Expand All @@ -85,11 +82,13 @@ readNewClimate <- function(subtype) {
paste(ref4change, collapse = ", "))
}
# as magclass can only cover numerical values well, transform: in column 2 BAU into -1, and Type into number based on allowedType
input2[2] <- as.numeric(unlist(rapply(input2[2], function(x)
ifelse(x == "BAU", -1, ifelse(x == "no", -2, x)), how = "replace")))
input2[2] <- as.numeric(unlist(rapply(input2[2], function(x)
ifelse(x == "BAU", -1, ifelse(x == "no", -2, x)), how = "replace")))
input2[5] <- as.numeric(unlist(rapply(input2[5], function(x) match(x, allowedType), how = "replace")))
# sort with c(1,4,2,3,5,6,7) to get region into first and years into second column
x <- as.magpie(input2[c(1, 4, 2, 3, 5, 6, 7)], spatial = 1, temporal = 2, datacol = 3)
}
} else {
stop("Incorrect subtype, please use Capacity_YYYY_cond or Emissions_YYYY_cond (or uncond).")
}
return(x)
}

0 comments on commit 1829cac

Please sign in to comment.