From 8f6ab8d82d71cfe59a1429b80be393b6ffe2587c Mon Sep 17 00:00:00 2001 From: Alois Dirnaichner Date: Tue, 27 Sep 2022 23:42:38 +0200 Subject: [PATCH 1/5] Avoid duplication of scenarios due to imprecise filtering. --- R/readEDGETransport.R | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index 1c748934..a181dea4 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -51,18 +51,21 @@ readEDGETransport <- function(subtype = "logit_exponent") { } create_copy_demscens <- function(dt) { - setkey(dt, "DEM_scenario") + setkey(dt, "DEM_scenario", "GDP_scenario", "EDGE_scenario") ## Workaround for NAVIGATE: copy-create demand scenarios which we do not supply by EDGE-T dt <- rbind(dt, - dt["gdp_SSP2EU", nomatch=NULL][, DEM_scenario := "gdp_SSP2EU_NAV_ele"], - dt["gdp_SSP2EU", nomatch=NULL][, DEM_scenario := "gdp_SSP2EU_NAV_tec"], - dt["gdp_SSP2EU_lowdem", nomatch=NULL][, DEM_scenario := "gdp_SSP2EU_NAV_act"], - dt["gdp_SSP2EU_lowdem", nomatch=NULL][, DEM_scenario := "gdp_SSP2EU_NAV_all"], - dt["gdp_SSP2EU_lowdem", nomatch=NULL][, DEM_scenario := "gdp_SSP2_lowEn"] + dt[c("gdp_SSP2EU", "gdp_SSP2EU", "NAV_ele"), nomatch=NULL][ + , DEM_scenario := "gdp_SSP2EU_NAV_ele"], + dt[c("gdp_SSP2EU", "gdp_SSP2EU", "NAV_tec"), nomatch=NULL][ + , DEM_scenario := "gdp_SSP2EU_NAV_tec"], + dt[c("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_act"), nomatch=NULL][ + , DEM_scenario := "gdp_SSP2EU_NAV_act"], + dt[c("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_all"), nomatch=NULL][ + , DEM_scenario := "gdp_SSP2EU_NAV_all"] ) setkey(dt, "DEM_scenario") + dt["gdp_SSP2EU_lowdem", DEM_scenario := "gdp_SSP2_lowEn"] scens <- unique(dt$DEM_scenario) - ## this scenario is not supported by REMIND dt <- dt[scens["gdp_SSP2EU_lowdem" != scens]] return(dt) From 1d5bfdf875162ce0aeb017f9290bd6845e53567b Mon Sep 17 00:00:00 2001 From: Alois Dirnaichner Date: Wed, 28 Sep 2022 11:27:29 +0200 Subject: [PATCH 2/5] Use setkeyv instead of setkey because we like to quote cnames --- R/readEDGETransport.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index a181dea4..7e28ec04 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -12,7 +12,7 @@ #' \dontrun{ a <- readSource(type="EDGETransport") #' } #' @importFrom magclass read.magpie -#' @importFrom data.table rbindlist fread setcolorder := setnames setkey +#' @importFrom data.table rbindlist fread setcolorder := setnames setkeyv #' @importFrom rmndt approx_dt #' @@ -51,7 +51,7 @@ readEDGETransport <- function(subtype = "logit_exponent") { } create_copy_demscens <- function(dt) { - setkey(dt, "DEM_scenario", "GDP_scenario", "EDGE_scenario") + setkeyv(dt, "DEM_scenario", "GDP_scenario", "EDGE_scenario") ## Workaround for NAVIGATE: copy-create demand scenarios which we do not supply by EDGE-T dt <- rbind(dt, dt[c("gdp_SSP2EU", "gdp_SSP2EU", "NAV_ele"), nomatch=NULL][ @@ -63,7 +63,7 @@ readEDGETransport <- function(subtype = "logit_exponent") { dt[c("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_all"), nomatch=NULL][ , DEM_scenario := "gdp_SSP2EU_NAV_all"] ) - setkey(dt, "DEM_scenario") + setkeyv(dt, "DEM_scenario") dt["gdp_SSP2EU_lowdem", DEM_scenario := "gdp_SSP2_lowEn"] scens <- unique(dt$DEM_scenario) ## this scenario is not supported by REMIND @@ -73,7 +73,7 @@ readEDGETransport <- function(subtype = "logit_exponent") { compress_magpie <- function(dt, ...) { dt <- create_copy_demscens(dt) - setkey(dt, "EDGE_scenario", "DEM_scenario", "GDP_scenario") + setkeyv(dt, "EDGE_scenario", "DEM_scenario", "GDP_scenario") mdata <- NULL for (i in unique(dt$EDGE_scenario)) { for (j in unique(dt$DEM_scenario)) { From 38717b5d3312054cc05496da8442dec92214057e Mon Sep 17 00:00:00 2001 From: Alois Dirnaichner Date: Wed, 28 Sep 2022 11:32:17 +0200 Subject: [PATCH 3/5] Use the recommended syntax when doing multikey selects. --- R/readEDGETransport.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index 7e28ec04..dc372972 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -54,17 +54,17 @@ readEDGETransport <- function(subtype = "logit_exponent") { setkeyv(dt, "DEM_scenario", "GDP_scenario", "EDGE_scenario") ## Workaround for NAVIGATE: copy-create demand scenarios which we do not supply by EDGE-T dt <- rbind(dt, - dt[c("gdp_SSP2EU", "gdp_SSP2EU", "NAV_ele"), nomatch=NULL][ + dt[.("gdp_SSP2EU", "gdp_SSP2EU", "NAV_ele"), nomatch=NULL][ , DEM_scenario := "gdp_SSP2EU_NAV_ele"], - dt[c("gdp_SSP2EU", "gdp_SSP2EU", "NAV_tec"), nomatch=NULL][ + dt[.("gdp_SSP2EU", "gdp_SSP2EU", "NAV_tec"), nomatch=NULL][ , DEM_scenario := "gdp_SSP2EU_NAV_tec"], - dt[c("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_act"), nomatch=NULL][ + dt[.("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_act"), nomatch=NULL][ , DEM_scenario := "gdp_SSP2EU_NAV_act"], - dt[c("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_all"), nomatch=NULL][ + dt[.("gdp_SSP2EU_lowdem", "gdp_SSP2EU", "NAV_all"), nomatch=NULL][ , DEM_scenario := "gdp_SSP2EU_NAV_all"] ) setkeyv(dt, "DEM_scenario") - dt["gdp_SSP2EU_lowdem", DEM_scenario := "gdp_SSP2_lowEn"] + dt[.("gdp_SSP2EU_lowdem"), DEM_scenario := "gdp_SSP2_lowEn"] scens <- unique(dt$DEM_scenario) ## this scenario is not supported by REMIND dt <- dt[scens["gdp_SSP2EU_lowdem" != scens]] From 69d555ac8c52815f272d2209392d27c66ca794ae Mon Sep 17 00:00:00 2001 From: Alois Dirnaichner Date: Wed, 28 Sep 2022 11:42:03 +0200 Subject: [PATCH 4/5] correctly provide a vector to setkeyv --- R/readEDGETransport.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index dc372972..25bd238a 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -51,7 +51,7 @@ readEDGETransport <- function(subtype = "logit_exponent") { } create_copy_demscens <- function(dt) { - setkeyv(dt, "DEM_scenario", "GDP_scenario", "EDGE_scenario") + setkeyv(dt, c("DEM_scenario", "GDP_scenario", "EDGE_scenario")) ## Workaround for NAVIGATE: copy-create demand scenarios which we do not supply by EDGE-T dt <- rbind(dt, dt[.("gdp_SSP2EU", "gdp_SSP2EU", "NAV_ele"), nomatch=NULL][ @@ -73,7 +73,7 @@ readEDGETransport <- function(subtype = "logit_exponent") { compress_magpie <- function(dt, ...) { dt <- create_copy_demscens(dt) - setkeyv(dt, "EDGE_scenario", "DEM_scenario", "GDP_scenario") + setkeyv(dt, c("EDGE_scenario", "DEM_scenario", "GDP_scenario")) mdata <- NULL for (i in unique(dt$EDGE_scenario)) { for (j in unique(dt$DEM_scenario)) { From 94d5761bad7eedccfe4247ab562055728b8be8cc Mon Sep 17 00:00:00 2001 From: Alois Dirnaichner Date: Wed, 28 Sep 2022 11:47:24 +0200 Subject: [PATCH 5/5] Remove check that is not required anymore. --- R/readEDGETransport.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index 25bd238a..cea505a6 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -66,8 +66,6 @@ readEDGETransport <- function(subtype = "logit_exponent") { setkeyv(dt, "DEM_scenario") dt[.("gdp_SSP2EU_lowdem"), DEM_scenario := "gdp_SSP2_lowEn"] scens <- unique(dt$DEM_scenario) - ## this scenario is not supported by REMIND - dt <- dt[scens["gdp_SSP2EU_lowdem" != scens]] return(dt) }