From 8e110c930b99482773ae3b7c5e552641bb91d138 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Fri, 22 Sep 2023 18:58:14 +0200 Subject: [PATCH 1/3] Explicit flag for when subsectors not available --- R/reportEmi.R | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 5374abab..b4f846ba 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -138,9 +138,16 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_demFeSector <- readGDX(gdx, "vm_demFeSector", field = "l", restore_zeros = F)[, t, ] # set NA values to 0, vm_demFeSector[is.na(vm_demFeSector)] <- 0 - # FE demand per industry subsector - o37_demFeIndSub <- readGDX(gdx, "o37_demFeIndSub", restore_zeros = F)[, t, ] - if (!is.null(o37_demFeIndSub)) { + # FE demand per industry subsector. First read without subsetting to check if + # is was actually written + # o37_demFeIndSub <- readGDX(gdx, "o37_demFeIndSub", restore_zeros = F)[, t, ] + o37_demFeIndSub <- readGDX(gdx, "o37_demFeIndSub", restore_zeros = F) + # Does subsector information exist? + if (length(o37_demFeIndSub) != 0) { + hassubsectors <- TRUE + o37_demFeIndSub <- o37_demFeIndSub[,t,] + } else { + hassubsectors <- FALSE o37_demFeIndSub[is.na(o37_demFeIndSub)] <- 0 } @@ -555,7 +562,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # reporting if FE per industry subsector o37_demFeIndSub exists - if (!is.null(o37_demFeIndSub)) { + if (hassubsectors) { # relabel industry energy CC from CCS sectors to industry sectors vm_emiIndCCS_Mapped <- toolAggregate( @@ -1231,7 +1238,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 mbind()) } else { - if (!is.null(o37_demFeIndSub)) { + if (hassubsectors) { # calculate bioenergy shares in industry solids, liquids and gases per subsector @@ -2392,7 +2399,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Emi|CO2|Gross|Energy|Demand|+|Transport (Mt CO2/yr)") # TODO: remove this if clause once the below variable are there for industry subsectors - if ((module2realisation["industry", 2] == "fixed_shares") | (!is.null(o37_demFeIndSub))) { + if ((module2realisation["industry", 2] == "fixed_shares") | hassubsectors) { # Note: this assumes that all bunker fuels are liquids emi.vars.wBunkers <- c(emi.vars.wBunkers, "Emi|CO2|Energy|Demand|Transport|+|Liquids (Mt CO2/yr)", "Emi|CO2|Energy|Demand|++|Liquids (Mt CO2/yr)") From e3ad1e942a0dc028ccdae395c7224a07568b5f87 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Fri, 22 Sep 2023 19:49:40 +0200 Subject: [PATCH 2/3] Fix logic and extend handling to CCS variables --- R/reportEmi.R | 166 +++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index b4f846ba..5f7398c5 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -146,9 +146,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (length(o37_demFeIndSub) != 0) { hassubsectors <- TRUE o37_demFeIndSub <- o37_demFeIndSub[,t,] + o37_demFeIndSub[is.na(o37_demFeIndSub)] <- 0 } else { hassubsectors <- FALSE - o37_demFeIndSub[is.na(o37_demFeIndSub)] <- 0 } # secondary energy production @@ -181,95 +181,97 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_co2CCS <- readGDX(gdx, "vm_co2CCS", field = "l", restore_zeros = F)[, t, ] - # CO2 captured by industry sectors - vm_emiIndCCS <- readGDX(gdx, "vm_emiIndCCS", field = "l", restore_zeros = T)[, t, ] - getSets(vm_emiIndCCS)[3] <- "secInd37" # relabel subsector dimension to match with other parameters - - # CO2 released by CCU - vm_co2CCUshort <- readGDX(gdx, "vm_co2CCUshort", field = "l", restore_zeros = F)[, t, ] - # helper variable to release captured CO2 in no CCU capacities are standing anymore to take the captured CO2 - v_co2capturevalve <- readGDX(gdx, "v_co2capturevalve", field = "l", restore_zeros = F)[, t, ] - - # CO2 captured per industry subsector - # NOTE: The parameter pm_IndstCO2Captured was calculated without taking into - # account the different emission factors of energy carriers, so we recalculate - # it here. Might load it again when we are reasonably sure we won't process - # any gdxes with the faulty parameter any more. - vm_emiIndCCS_tibble <- vm_emiIndCCS[,,emiInd37_fuel] %>% - as_tibble() %>% - select(t = 'ttot', regi = 'all_regi', emiInd37 = 'secInd37', - vm_emiIndCCS = 'value') %>% - left_join(secInd37_2_emiInd37, 'emiInd37') %>% - select(-'emiInd37') - - o37_demFeIndSub_tibble <- o37_demFeIndSub %>% - as_tibble() %>% - select(t = 'ttot', regi = 'all_regi', sety = 'all_enty', - fety = 'all_enty1', 'secInd37', 'emiMkt' = 'all_emiMkt', - o37_demFEindsub = 'value') - - pm_emifac_tibble <- pm_emifac %>% - mselect(mutate(se2fe, all_enty2 = 'co2')) %>% - as_tibble() %>% - select(t = 'tall', regi = 'all_regi', fety = 'all_enty1', - pm_emifac = 'value') %>% - # extend fossil emission factors to biomass and synfuels - left_join( - se2fe %>% - distinct(sety = .data$all_enty, fety = .data$all_enty1), - - by = 'fety', - - relationship = 'many-to-many' - ) + # CO2 captured by industry sectors + vm_emiIndCCS <- readGDX(gdx, "vm_emiIndCCS", field = "l", restore_zeros = T)[, t, ] + getSets(vm_emiIndCCS)[3] <- "secInd37" # relabel subsector dimension to match with other parameters - subsector_total_emissions <- inner_join( - o37_demFeIndSub_tibble, - pm_emifac_tibble, + # CO2 released by CCU + vm_co2CCUshort <- readGDX(gdx, "vm_co2CCUshort", field = "l", restore_zeros = F)[, t, ] + # helper variable to release captured CO2 in no CCU capacities are standing anymore to take the captured CO2 + v_co2capturevalve <- readGDX(gdx, "v_co2capturevalve", field = "l", restore_zeros = F)[, t, ] - c('t', 'regi', 'sety', 'fety') - ) %>% - group_by(.data$t, .data$regi, .data$secInd37) %>% - summarise( - subsector_total_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), - .groups = 'drop') + if (hassubsectors) { + # CO2 captured per industry subsector + # NOTE: The parameter pm_IndstCO2Captured was calculated without taking into + # account the different emission factors of energy carriers, so we recalculate + # it here. Might load it again when we are reasonably sure we won't process + # any gdxes with the faulty parameter any more. + vm_emiIndCCS_tibble <- vm_emiIndCCS[,,emiInd37_fuel] %>% + as_tibble() %>% + select(t = 'ttot', regi = 'all_regi', emiInd37 = 'secInd37', + vm_emiIndCCS = 'value') %>% + left_join(secInd37_2_emiInd37, 'emiInd37') %>% + select(-'emiInd37') + + o37_demFeIndSub_tibble <- o37_demFeIndSub %>% + as_tibble() %>% + select(t = 'ttot', regi = 'all_regi', sety = 'all_enty', + fety = 'all_enty1', 'secInd37', 'emiMkt' = 'all_emiMkt', + o37_demFEindsub = 'value') + + pm_emifac_tibble <- pm_emifac %>% + mselect(mutate(se2fe, all_enty2 = 'co2')) %>% + as_tibble() %>% + select(t = 'tall', regi = 'all_regi', fety = 'all_enty1', + pm_emifac = 'value') %>% + # extend fossil emission factors to biomass and synfuels + left_join( + se2fe %>% + distinct(sety = .data$all_enty, fety = .data$all_enty1), + + by = 'fety', + + relationship = 'many-to-many' + ) - subsector_emissions <- inner_join( - o37_demFeIndSub_tibble, - pm_emifac_tibble, + subsector_total_emissions <- inner_join( + o37_demFeIndSub_tibble, + pm_emifac_tibble, - c('t', 'regi', 'sety', 'fety') - ) %>% - group_by(.data$t, .data$regi, .data$secInd37, .data$sety, .data$fety, - .data$emiMkt) %>% - summarise( - subsector_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), - .groups = 'drop') + c('t', 'regi', 'sety', 'fety') + ) %>% + group_by(.data$t, .data$regi, .data$secInd37) %>% + summarise( + subsector_total_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), + .groups = 'drop') - pm_IndstCO2Captured <- subsector_emissions %>% - full_join( - inner_join( - vm_emiIndCCS_tibble, + subsector_emissions <- inner_join( + o37_demFeIndSub_tibble, + pm_emifac_tibble, - subsector_total_emissions, + c('t', 'regi', 'sety', 'fety') + ) %>% + group_by(.data$t, .data$regi, .data$secInd37, .data$sety, .data$fety, + .data$emiMkt) %>% + summarise( + subsector_emissions = sum(.data$o37_demFEindsub * .data$pm_emifac), + .groups = 'drop') - c('t', 'regi', 'secInd37') - ) %>% - mutate(subsector_capture_share = .data$vm_emiIndCCS - / .data$subsector_total_emissions) %>% - select(-'vm_emiIndCCS', -'subsector_total_emissions'), + pm_IndstCO2Captured <- subsector_emissions %>% + full_join( + inner_join( + vm_emiIndCCS_tibble, - c('t', 'regi', 'secInd37') - ) %>% - mutate( - value = .data$subsector_emissions * .data$subsector_capture_share) %>% - select(-'subsector_emissions', -'subsector_capture_share') %>% - select(ttot = 't', all_regi = 'regi', all_enty = 'sety', all_enty1 = 'fety', - 'secInd37', all_emiMkt = 'emiMkt', 'value') %>% - as.magpie(spatial = 2, temporal = 1, data = ncol(.)) %>% - ifelse(is.finite(.), ., 0) # replace NaN by 0 + subsector_total_emissions, - rm(vm_emiIndCCS_tibble, subsector_total_emissions, pm_emifac_tibble) + c('t', 'regi', 'secInd37') + ) %>% + mutate(subsector_capture_share = .data$vm_emiIndCCS + / .data$subsector_total_emissions) %>% + select(-'vm_emiIndCCS', -'subsector_total_emissions'), + + c('t', 'regi', 'secInd37') + ) %>% + mutate( + value = .data$subsector_emissions * .data$subsector_capture_share) %>% + select(-'subsector_emissions', -'subsector_capture_share') %>% + select(ttot = 't', all_regi = 'regi', all_enty = 'sety', all_enty1 = 'fety', + 'secInd37', all_emiMkt = 'emiMkt', 'value') %>% + as.magpie(spatial = 2, temporal = 1, data = ncol(.)) %>% + ifelse(is.finite(.), ., 0) # replace NaN by 0 + + rm(vm_emiIndCCS_tibble, subsector_total_emissions, pm_emifac_tibble) + } # utility functions ---- # Convert a mixer table into a list that can be passed to mselect() to @@ -1161,7 +1163,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ) ### report industry captured CO2 ---- - if (!is.null(pm_IndstCO2Captured)) { + if (ifelse(exists("pm_IndstCO2Captured"),!is.null(pm_IndstCO2Captured),FALSE) & hassubsectors) { variable_prefix <- 'Carbon Management|Carbon Capture|Industry Energy|' variable_postfix <- ' (Mt CO2/yr)' From 455615107eb314332318a94926db888fbc75fdf5 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Fri, 22 Sep 2023 19:53:37 +0200 Subject: [PATCH 3/3] Added name --- CITATION.cff | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CITATION.cff b/CITATION.cff index 4f2a5962..1415f0a6 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -65,6 +65,8 @@ authors: given-names: Jessica - family-names: Verpoort given-names: Philipp +- family-names: Abrahao + given-names: Gabriel license: LGPL-3.0 repository-code: https://github.com/pik-piam/remind2