From 790093af2784990602346f041635fdd97ba7002f Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Mon, 21 Feb 2022 11:36:40 +0100 Subject: [PATCH 01/31] adapt emissions reporting for feedstocks implementation: apply emissions factors for energy-related emissions to FE witout non-energy use and add feedstocks emissions with lowered emissions factors to industrial process emissions --- R/reportEmi.R | 102 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 023807fe..cdc748d0 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -73,12 +73,13 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 rename(all_enty1 = all_enty) # mapping of combination of FE to sectors which are actually used sector2emiMkt <- readGDX(gdx, "sector2emiMkt") # mapping from sectors to markets secInd37_2_emiInd37 <- readGDX(gdx, "secInd37_2_emiInd37") # mapping from industry subsectors to industry CCS sectors - + entyFe2sector2emiMkt_NonEn <- readGDX(gdx, "entyFe2sector2emiMkt_NonEn") # combinations of FE type, sector, market for which non-energy use exists # technology sets teCCS <- readGDX(gdx, "teCCS") teBio <- readGDX(gdx, "teBio") teFosCCS <- readGDX(gdx, "teFosCCS") + # additional sets needed @@ -122,6 +123,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ### for emissions of energy system technologies # emission factors of technologies pm_emifac <- readGDX(gdx, "pm_emifac", restore_zeros = F)[, t, ] + # emissions factors of non-energy use + pm_emifacNonEnergy <- readGDX(gdx, "pm_emifacNonEnergy", restore_zeros = F)[,t,] # primary energy demand (pe2se emissions factors applied to) vm_demPE <- readGDX(gdx, "vm_demPE", field = "l", restore_zeros = F)[, t, ] # final energy demand (se2fe emissions factors applied to) @@ -146,9 +149,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (!is.null(pm_IndstCO2Captured)) { pm_IndstCO2Captured <- pm_IndstCO2Captured[se2fe[,c(1,2)]] } - - - + # FE non-energy use + vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T)[,t,] + # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) # parameter to calculate coupled production @@ -163,8 +166,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 v33_emiEW <- readGDX(gdx, "v33_emiEW", field = "l", restore_zeros = F)[, t, ] # stored CO2 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 @@ -173,8 +174,31 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 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, ] + + + # Preliminary Calculations ---- + + # get combinations of SE,FE,sector,emiMkt that exist in vm_demFeSector + FE.map <- se2fe %>% + left_join(entyFe2Sector) %>% + left_join(sector2emiMkt) %>% + select( -all_te) %>% + mutate( name = paste(all_enty,all_enty1,emi_sectors,all_emiMkt, sep = ".")) + - + # calculate FE without non-energy use + if (!is.null(vm_demFENonEnergySector)) { + vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[, , FE.map$name], + all_enty1 = entyFe2sector2emiMkt_NonEn$all_enty, + emi_sectors = entyFe2sector2emiMkt_NonEn$emi_sectors, + all_emiMkt = entyFe2sector2emiMkt_NonEn$all_emiMkt) + + # calculate FE without non-energy use + vm_demFeSector_woNonEn <- vm_demFeSector + vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector + } + + # Calculate Variables ---- @@ -280,7 +304,13 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 EmiPe2Se[,,getNames(CCO2Pe2Se, dim=3)] <- EmiPe2Se[,,getNames(CCO2Pe2Se, dim=3)] + CCO2Pe2Se * (1 - p_share_CCS) # emissions from fe carriers (demand-side emissions) - EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector[, , emi.map.fe$name] + # if FE non-energy use available, subtract it before calculating emissions + if (!is.null(vm_demFENonEnergySector)) { + EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector_woNonEn[, , emi.map.fe$name] + } else { + EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector[, , emi.map.fe$name] + } + @@ -686,20 +716,54 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Emi|CO2|+|Energy (Mt CO2/yr)")) ### 2.2 Non-energy CO2 emissions ---- - + + # (following q_emiAllMkt) + + + ### 2.2.1 Industrial Process Emissions ---- + + + + + # if FE non-energy use available, subtract it before calculating emissions + if (!is.null(vm_demFENonEnergySector)) { + + # calculate chemical process emissions from feedstocks treatment + EmiFac_NonEn.co2 <- collapseDim(pm_emifacNonEnergy[,,"co2"]) + EmiProcess_Feedstocks <- pm_emifacNonEnergy[,,"co2"] * vm_demFENonEnergySector[,,getNames(EmiFac_NonEn.co2)] + } else { + # otherwise chemical process emissions are 0 + EmiProcess_Feedstocks <- vm_co2eq * 0 + } + + + + + out <- mbind(out, + # industrial process CO2 (so far only cement process emissions in REMIND) + setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) + # add captured CO2 from cement process which is not stored but used for CCU + + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS) + # add chemical process emissions + + dimSums(EmiProcess_Feedstocks, dim = 3)) * GtC_2_MtCO2, + "Emi|CO2|+|Industrial Processes (Mt CO2/yr)"), + # process industry cement CO2 + setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) + # add captured CO2 from cement process which is not stored + + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2, + "Emi|CO2|Industrial Processes|+|Cement (Mt CO2/yr)"), + # chemical process emissions from feedstocks treatment + setNames(dimSums(EmiProcess_Feedstocks*GtC_2_MtCO2, dim = 3), + "Emi|CO2|Industrial Processes|+|Chemicals (Mt CO2/yr)") + ) + + ### 2.2.2 Other Non-energy emissions ---- + + + out <- mbind(out, - # industrial process CO2 (so far only cement process emissions in REMIND) - setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) - # add captured CO2 from cement process which is not stored but used for CCU - + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2, - "Emi|CO2|+|Industrial Processes (Mt CO2/yr)"), - # process industry cement CO2 - setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) - # add captured CO2 from cement process which is not stored - + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2, - "Emi|CO2|Industrial Processes|+|Cement (Mt CO2/yr)"), # land-use change CO2 setNames(dimSums(vm_emiMacSector[, , "co2luc"], dim = 3) * GtC_2_MtCO2, "Emi|CO2|+|Land-Use Change (Mt CO2/yr)"), From 833b0d4abad65dd5ce895192e4161984c57c88ed Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 22 Feb 2022 14:21:30 +0100 Subject: [PATCH 02/31] make feedstock-related changes in emissions reporting backwards compatible with earlier GDXs --- R/reportEmi.R | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index cdc748d0..ea29e402 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -35,6 +35,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # intialize varibles used in dplyr operations all_enty2 <- all_te <- all_enty <- all_enty1 <- emi_sectors <- all_emiMkt <- all_in <- NULL + + + # Read Data from GDX ---- @@ -73,7 +76,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 rename(all_enty1 = all_enty) # mapping of combination of FE to sectors which are actually used sector2emiMkt <- readGDX(gdx, "sector2emiMkt") # mapping from sectors to markets secInd37_2_emiInd37 <- readGDX(gdx, "secInd37_2_emiInd37") # mapping from industry subsectors to industry CCS sectors - entyFe2sector2emiMkt_NonEn <- readGDX(gdx, "entyFe2sector2emiMkt_NonEn") # combinations of FE type, sector, market for which non-energy use exists + entyFe2sector2emiMkt_NonEn <- readGDX(gdx, "entyFe2sector2emiMkt_NonEn", react = "silent") # combinations of FE type, sector, market for which non-energy use exists # technology sets teCCS <- readGDX(gdx, "teCCS") teBio <- readGDX(gdx, "teBio") @@ -124,7 +127,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # emission factors of technologies pm_emifac <- readGDX(gdx, "pm_emifac", restore_zeros = F)[, t, ] # emissions factors of non-energy use - pm_emifacNonEnergy <- readGDX(gdx, "pm_emifacNonEnergy", restore_zeros = F)[,t,] + pm_emifacNonEnergy <- readGDX(gdx, "pm_emifacNonEnergy", restore_zeros = F, react = "silent")[,t,] # primary energy demand (pe2se emissions factors applied to) vm_demPE <- readGDX(gdx, "vm_demPE", field = "l", restore_zeros = F)[, t, ] # final energy demand (se2fe emissions factors applied to) @@ -132,7 +135,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # 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, ] + o37_demFeIndSub <- readGDX(gdx, "o37_demFeIndSub", restore_zeros = F, react = "silent")[, t, ] if (!is.null(o37_demFeIndSub)) { o37_demFeIndSub[is.na(o37_demFeIndSub)] <- 0 } @@ -150,7 +153,11 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 pm_IndstCO2Captured <- pm_IndstCO2Captured[se2fe[,c(1,2)]] } # FE non-energy use - vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T)[,t,] + vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T, react = "silent")[,t,] + if (length(vm_demFENonEnergySector) == 0) { + vm_demFENonEnergySector <- NULL + } + # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) From 9099bf6d94e12f92c0e8f1bda523842cf889353c Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 22 Feb 2022 14:22:28 +0100 Subject: [PATCH 03/31] solve summations issues in emissions reporting after feedstock-related changes --- R/reportEmi.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index ea29e402..59fc977b 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -479,6 +479,11 @@ 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)) { + + ### calculate FE per industry subsector w/o Non-energy Use + o37_demFeIndSub_woNonEn <- o37_demFeIndSub + sefe.chem.feedstock <- intersect(getNames(collapseDim(o37_demFeIndSub_woNonEn[,,"chemicals.ETS"])), getNames(collapseDim(vm_demFENonEnergySector[,,"indst"]))) + o37_demFeIndSub_woNonEn[,,"chemicals"][,,sefe.chem.feedstock] <- o37_demFeIndSub_woNonEn[,,"chemicals"][,,sefe.chem.feedstock] - collapseDim(vm_demFENonEnergySector[,,"indst"][,,sefe.chem.feedstock]) # relabel industry energy CC from CCS sectors to industry sectors emiInd37 <- NULL @@ -487,7 +492,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # calculate captured CO2 per subsector and FE carrier by multiplying subsectoral share of fesos, fehos, fegas in total FE from fesos, fehos and fegas with the captured CO2 by subsector - vm_emiIndCCS_Sub <- vm_emiIndCCS_Mapped * dimSums(mselect(o37_demFeIndSub, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.4)) / dimSums(mselect(o37_demFeIndSub, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.2, 3.4)) + vm_emiIndCCS_Sub <- vm_emiIndCCS_Mapped * dimSums(mselect(o37_demFeIndSub_woNonEn, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.4)) / dimSums(mselect(o37_demFeIndSub_woNonEn, all_enty1 = c("fesos", "fehos", "fegas")), dim = c(3.1, 3.2, 3.4)) vm_emiIndCCS_Sub[is.na(vm_emiIndCCS_Sub)] <- 0 getSets(vm_emiIndCCS_Sub)[c(3, 4)] <- c("secInd37", "all_enty1") @@ -496,12 +501,12 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # calculate industry emissions by subsector (before industry CO2 Capture) sel_pm_emifac_co2 <- if(getSets(pm_emifac)[[6]] == "emiAll"){ - mselect(pm_emifac, all_enty1 = getNames(o37_demFeIndSub, dim = 2), emiAll = "co2") + mselect(pm_emifac, all_enty1 = getNames(o37_demFeIndSub_woNonEn, dim = 2), emiAll = "co2") } else { - mselect(pm_emifac, all_enty1 = getNames(o37_demFeIndSub, dim = 2), all_enty2 = "co2") + mselect(pm_emifac, all_enty1 = getNames(o37_demFeIndSub_woNonEn, dim = 2), all_enty2 = "co2") } pm_emifac.fe.indst <- dimSums(sel_pm_emifac_co2, dim = c(3.3, 3.4)) - EmiIndSubSec <- pm_emifac.fe.indst * o37_demFeIndSub[, , getNames(pm_emifac.fe.indst)] + EmiIndSubSec <- pm_emifac.fe.indst * o37_demFeIndSub_woNonEn[, , getNames(pm_emifac.fe.indst)] @@ -1638,7 +1643,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # add captured CO2 from cement process which is not stored # (EmiMACEq for co2cement_process contains cement process emissions - captured cement co2 process emissions) + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2 - + dimSums(mselect(EmiMACEq[, , "ETS"], sector = "indst"), dim = 3), + + dimSums(mselect(EmiMACEq[, , "ETS"], sector = "indst"), dim = 3) + # add chemical process emissions to ETS + + dimSums(EmiProcess_Feedstocks, dim = 3) * GtC_2_MtCO2, "Emi|GHG|ETS|+|Industry (Mt CO2eq/yr)"), setNames( # demand-side co2 emissions (before industry CCS) From d85b87acb2235d09a5b0d56bd758ab89a2587bdb Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 22 Feb 2022 14:23:16 +0100 Subject: [PATCH 04/31] remove deprecated reporting of emissions without non-energy use --- R/reportEmi.R | 145 ++------------------------------------------------ 1 file changed, 3 insertions(+), 142 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 59fc977b..ddf4c2b7 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -1806,131 +1806,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 - ## 8. Emissions w/o non-energy use ---- - # (Note: The non-energy use variables are so far only available for REMIND-EU runs and industry fixed_shares) - # TODO: add non-energy use variables for all regionmappings and sector realizations - - # Note: Non-energy use emissions should not be confused with process emissions. Non-energy use emissions are emissions/carbon flow of FE carriers which are used as feedstocks in industry. - if ("FE|Non-energy Use|Industry (EJ/yr)" %in% getNames(output)) { - - - # calculate non-energy use emissions (= feedstock carbon content) as industry emissions before CCS per energy carrier * share of feedstocks in final energy - # take industry emissions before CCS as feedstocks cannot be used for CCS - # this is a temporary approximation in the reporting, but should eventually be adapted in REMIND by having a seperate feedstock FE of which the carbon cannot be captured - out <- mbind(out, - # liquids - setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Liquids (EJ/yr)"], - "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Liquids (Mt CO2/yr)"), - # gases - setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Gases (EJ/yr)"], - "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Gases (Mt CO2/yr)"), - # solids - setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fesos"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Solids (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Solids (EJ/yr)"], - "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Solids (Mt CO2/yr)")) - - # total non-energy use emissions - out <- mbind(out, - setNames(out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Solids (Mt CO2/yr)"] + - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Liquids (Mt CO2/yr)"] + - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Gases (Mt CO2/yr)"], - "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)")) - - - - - # calculate emissions variables w/o non-energy use - # TODO: once proper accounting of non-energy use/feedstocks has been done for all cases, consider making the standard "Emi|CO2 (Mt CO2/yr)" etc. variables - # the ones without non-energy use and add an extra set "w/ Non-energy use" as this is likely a more sensible default - - - ### reattribution of a fraction of non-energy use carbon as waste emissions (plastic products that get combusted in waste incineration plants within the region) - # fraction of non-energy use emissions that gets reattributed as waste CO2 emissions (reporting assumption) - # as this analysis only used for Germany, take 50% which is current fraction of plastic waste to be incinerated - # https://www.umweltbundesamt.de/daten/ressourcen-abfall/verwertung-entsorgung-ausgewaehlter-abfallarten/kunststoffabfaelle#kunststoffe-produktion-verwendung-und-verwertung - WasteFraction <- 0.5 - - out <- mbind(out, - setNames(WasteFraction * out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"], - "Emi|CO2|w/o Non-energy Use|Waste Incineration (Mt CO2/yr)")) - - ### variables for which non-energy emissions should be substracted but waste fraction added - emi.vars.wNonEn.inclWaste <- c( - # GHG emissions - "Emi|GHG (Mt CO2eq/yr)", - "Emi|GHG|+|CO2 (Mt CO2eq/yr)", - "Emi|GHG|w/o Land-Use Change (Mt CO2eq/yr)", - - # CO2 Emissions - "Emi|CO2 (Mt CO2/yr)") - - - # add emissions variables with LULUCF national accounting - if (!is.null(p47_LULUCFEmi_GrassiShift)) { - - emi.vars.wNonEn.inclWaste <- c(emi.vars.wNonEn.inclWaste, - "Emi|GHG|LULUCF national accounting (Mt CO2eq/yr)", - "Emi|CO2|LULUCF national accounting (Mt CO2/yr)" ) - } - - ### variables for which non-energy emissions should be substracted (and no waste fraction added) - emi.vars.wNonEn.exclWaste <- c( - # GHG emissions - "Emi|GHG|+++|Energy (Mt CO2eq/yr)", - "Emi|GHG|Energy|+|Demand (Mt CO2eq/yr)", - "Emi|GHG|Energy|Demand|+|Industry (Mt CO2eq/yr)", - "Emi|GHG|Industry (Mt CO2eq/yr)", - - # Gross GHG Emissions - "Emi|GHG|Gross|Energy (Mt CO2eq/yr)", - "Emi|GHG|Gross|Energy|+|Demand (Mt CO2eq/yr)", - "Emi|GHG|Gross|Energy|Demand|+|Industry (Mt CO2eq/yr)", - - # CO2 Emissions - "Emi|CO2|+|Energy (Mt CO2/yr)", - "Emi|CO2|Energy and Industrial Processes (Mt CO2/yr)", - "Emi|CO2|Energy|+|Demand (Mt CO2/yr)", - "Emi|CO2|Energy|Demand|+|Industry (Mt CO2/yr)", - - - # Gross CO2 Emissions - "Emi|CO2|Gross|Energy|+|Demand (Mt CO2/yr)", - "Emi|CO2|Gross|Energy (Mt CO2/yr)", - "Emi|CO2|Gross|Energy and Industrial Processes (Mt CO2/yr)", - "Emi|CO2|Gross|Energy|Demand|+|Industry (Mt CO2/yr)") - - - - - # variable names, insert w/o non-energy use - emi.vars.wNonEn <- c(emi.vars.wNonEn.inclWaste, emi.vars.wNonEn.exclWaste) - names.wNonEn <- emi.vars.wNonEn - names.wNonEn <- gsub("Emi\\|CO2", "Emi|CO2|w/o Non-energy Use", names.wNonEn) - names.wNonEn <- gsub("Emi\\|GHG", "Emi|GHG|w/o Non-energy Use", names.wNonEn) - - # remove all pluses from the "Emi w/o Non-energy Use" variables as they do not cover sectors in which non-energy use not relevant and checking aggregation does not make sense - names.wNonEn <- gsub("\\|\\+\\|", "\\|", names.wNonEn) - names.wNonEn <- gsub("\\|\\++\\|", "\\|", names.wNonEn) - - # calulate emissions variables with non-energy use - out.wNonEn <- out[, , emi.vars.wNonEn] - # for aggregate emissions: substract non-energy use carbon and add waste incineration emissions - out.wNonEn[, , emi.vars.wNonEn.inclWaste] <- out.wNonEn[, , emi.vars.wNonEn.inclWaste] - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"] + out[, , "Emi|CO2|w/o Non-energy Use|Waste Incineration (Mt CO2/yr)"] - # for energy related emissions: substract non-energy use carbon - # (waste incineration emissions are treated as a separate category here outside of energy emissions to avoid attribution compliations down the energy emissions tree) - out.wNonEn[, , emi.vars.wNonEn.exclWaste] <- out.wNonEn[, , emi.vars.wNonEn.exclWaste] - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"] - - - # insert "w/o Non-energy Use" label in variable names - out.wNonEn <- setNames(out.wNonEn[, , emi.vars.wNonEn], names.wNonEn) - - - out <- mbind(out, out.wNonEn) - - - } - - - # Aggregation to global and regional values ---- + # 8. Aggregation to global and regional values ---- # add global values out <- mbind(out, dimSums(out, dim = 1)) @@ -1939,7 +1815,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList)) } - # Bunker Correction ---- + # 9. Bunker Correction ---- ### variables for which bunker emissions should be substracted @@ -1985,22 +1861,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 - # if non-energy use variables exist, also do bunker correction for variables w/o non-energy use - if ("FE|Non-energy Use|Industry (EJ/yr)" %in% getNames(output)) { - emi.vars.wBunkers.wNonEn <- intersect(emi.vars.wBunkers, emi.vars.wNonEn) - - # remove all pluses from the "Emi w/o Non-energy Use" variables as they do not cover sectors in which non-energy use not relevant and checking aggregation does not make sense - emi.vars.wBunkers.wNonEn <- gsub("\\|\\+\\|", "\\|", emi.vars.wBunkers.wNonEn) - emi.vars.wBunkers.wNonEn <- gsub("\\|\\++\\|", "\\|", emi.vars.wBunkers.wNonEn) - emi.vars.wBunkers.wNonEn <- gsub("Emi\\|CO2", "Emi|CO2|w/o Non-energy Use", emi.vars.wBunkers.wNonEn) - emi.vars.wBunkers.wNonEn <- gsub("Emi\\|GHG", "Emi|GHG|w/o Non-energy Use", emi.vars.wBunkers.wNonEn) - - emi.vars.wBunkers <- c(emi.vars.wBunkers, emi.vars.wBunkers.wNonEn) - - } - - # add emissions variables with LULUCF national accounting if (!is.null(p47_LULUCFEmi_GrassiShift)) { @@ -2027,7 +1888,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, out.wBunkers) - # Cumulative Emissions ---- + # 10. Cumulative Emissions ---- # function to calculate cumulated values cumulatedValue <- function(var, i_pm_ts = pm_ts) { From c6c6a21ff5229259e4a7b5b60b874ea0f4e6c035 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 22 Feb 2022 14:26:40 +0100 Subject: [PATCH 05/31] add FE reporting for FE without non-energy use for relevant sectors and energy carriers and remove deprecated non-energy use reporting --- R/reportFE.R | 371 ++++++++++++++++++++++----------------------------- 1 file changed, 157 insertions(+), 214 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 4bf40514..1693cb72 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -49,13 +49,15 @@ reportFE <- function(gdx, regionSubsetList = NULL, entyFe2Sector <- readGDX(gdx, "entyFe2Sector") sector2emiMkt <- readGDX(gdx, "sector2emiMkt") + entyFe2sector2emiMkt_NonEn <- readGDX(gdx, "entyFe2sector2emiMkt_NonEn", react = "silent") + demFemapping <- entyFe2Sector %>% full_join(sector2emiMkt, by = 'emi_sectors') %>% # rename such that all_enty1 always signifies the FE carrier like in # vm_demFeSector rename(all_enty1 = all_enty) %>% left_join(se2fe, by = 'all_enty1') %>% - select(-all_te) + select(-all_te) #sety <- readGDX(gdx,c("entySe","sety"),format="first_found") @@ -69,6 +71,11 @@ reportFE <- function(gdx, regionSubsetList = NULL, #vm_prodFe <- vm_prodFe[se2fe] vm_demFeSector <- readGDX(gdx,name=c("vm_demFeSector"),field="l",format="first_found",restore_zeros=FALSE)[,t,]*TWa_2_EJ vm_demFeSector[is.na(vm_demFeSector)] <- 0 + # FE non-energy use + vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T, react = "silent")[,t,]*TWa_2_EJ + if (length(vm_demFENonEnergySector) == 0) { + vm_demFENonEnergySector <- NULL + } # only retain combinations of SE, FE, sector, and emiMkt which actually exist in the model (see qm_balFe) vm_demFeSector <- vm_demFeSector[demFemapping] @@ -116,7 +123,26 @@ reportFE <- function(gdx, regionSubsetList = NULL, cdr_mod = find_real_module(module2realisation,"CDR") CCU_mod = find_real_module(module2realisation,"CCU") - # ---- FE total production ------ + + + # Preliminary Calculations ---- + + + # calculate FE non-energy use and FE without non-energy use + if (!is.null(vm_demFENonEnergySector)) { + vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[demFemapping], + all_enty1 = entyFe2sector2emiMkt_NonEn$all_enty, + emi_sectors = entyFe2sector2emiMkt_NonEn$emi_sectors, + all_emiMkt = entyFe2sector2emiMkt_NonEn$all_emiMkt) + + # calculate FE without non-energy use + vm_demFeSector_woNonEn <- vm_demFeSector + vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector + } + + + + # ---- FE total production (incl. non-energy use) ------ out <- mbind(out, #total @@ -1333,227 +1359,144 @@ reportFE <- function(gdx, regionSubsetList = NULL, ) - ### temporary (!) industry non-energy use reporting - # note: only for REMIND-EU SSP2 + #### Non-energy Use Reporting ---- + - if (module2realisation["industry",2]=="fixed_shares") { + if (!is.null(vm_demFENonEnergySector)) { - if ("DEU" %in% getRegions(vm_prodFe)) { - - # some initializations required for building library with dplyr operations below - encar <- data <- value <- value_subsectors <- SSP <- Value_NonEn <- encar <- region <- period <- NULL - - - - # read in FE industry non-energy use trajectories from industry subsectors run - df.fe_nechem <- read.csv(system.file("extdata","pm_fe_nechem.cs4r",package = "remind2"), - sep = ",", skip = 4, header = F) - - # df.fe_nechem <- read.csv("./inst/extdata/pm_fe_nechem.cs4r", - # sep = ",", skip = 4, header = F) - - colnames(df.fe_nechem) <- c("period", "region","SSP","encar","value_subsectors") - vars.nechem <- c("FE|Industry|+|Liquids (EJ/yr)", - "FE|Industry|+|Gases (EJ/yr)", - "FE|Industry|+|Solids (EJ/yr)") - - map.vars.nechem <- c("FE|Industry|+|Liquids (EJ/yr)" = "fehoi_nechem", - "FE|Industry|+|Gases (EJ/yr)" = "fegai_nechem", - "FE|Industry|+|Solids (EJ/yr)" = "fesoi_nechem") - - map.nonen.vars <- c("fehoi_nechem" = "FE|Non-energy Use|Industry|+|Liquids (EJ/yr)", - "fegai_nechem" = "FE|Non-energy Use|Industry|+|Gases (EJ/yr)", - "fesoi_nechem" = "FE|Non-energy Use|Industry|+|Solids (EJ/yr)") - - - # non-energy use of solids/liquids/gases: min(fehoi,fehoi_nechem), - # where fehoi would be the liquids of the current run and - # fehoi_nechem the non-energy use liquids of the reference industry subsectors run - df.out.nechem <- suppressWarnings(as.quitte(out[,,vars.nechem])) %>% - rename( encar = data) %>% - # join current FE|Industry|Liquids etc. with non-energy use subsectors data - revalue.levels(encar = map.vars.nechem) %>% - left_join(df.fe_nechem) %>% - mutate( Value_NonEn = ifelse(value >= value_subsectors, value_subsectors, value)) %>% - filter( SSP == "SSP2") %>% - # map to non-energy use variable names - revalue.levels(encar= map.nonen.vars) %>% - select(region, period, encar, Value_NonEn) - - out.nechem <- suppressWarnings(as.magpie(df.out.nechem, spatial=1, temporal=2, datacol=4)) - out.nechem <- out.nechem[getRegions(out), getYears(out),] - - # set NAs to zero - out.nechem[is.na( out.nechem)] <- 0 - - - # bind FE non-energy use to output object - out <- mbind(out, out.nechem) - - - - # add further FE variables needed in ARIADNE - out <- mbind(out, - setNames(out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] - + out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] - + out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"], - "FE|Non-energy Use|Industry (EJ/yr)")) - - out <- mbind(out, - setNames(out[,,"FE (EJ/yr)"] - - out[,,"FE|Transport|Bunkers (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry (EJ/yr)"], - "FE|w/o Non-energy Use w/o Bunkers (EJ/yr)"), - setNames(out[,,"FE|++|Industry (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry (EJ/yr)"], - "FE|w/o Non-energy Use|Industry (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Liquids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Gases (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Solids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids (EJ/yr)") ) - - - # energy carrier split in FE energy use variables - out <- mbind(out, - # FE industry (without feedstocks) liquids: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), - # FE industry (without feedstocks) gases: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)")) - - - # energy carrier split in FE non-energy use variables - out <- mbind(out, - # split of non-energy use variables to fossil, bio, synfuels - # liquids - setNames(out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), + + # FE non-energy use variables + out <- mbind(out, + setNames(dimSums(vm_demFENonEnergySector, dim=3), + "FE|Non-energy Use (EJ/yr)"), + + + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst"), dim=3), + "FE|Non-energy Use|+|Industry (EJ/yr)"), - # gases - setNames(out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos"), dim=3), + "FE|Non-energy Use|Industry|+|Solids (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos"), dim=3), + "FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas"), dim=3), + "FE|Non-energy Use|Industry|+|Gases (EJ/yr)") + ) + + + # FE non-energy use per SE origin + out <- mbind(out, + - # solids - setNames(out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos", all_enty = "sesofos"), dim=3), + "FE|Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos", all_enty = "sesobio"), dim=3), "FE|Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], - "FE|Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)")) - - - # total FE variables per energy carrier without bunkers and without non-energy use - - out <- mbind(out, - # liquids - setNames(out[,,"FE|+|Liquids (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|+|Liquids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Liquids (EJ/yr)"), - # biomass liquids - setNames(out[,,"FE|Liquids|+|Biomass (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Liquids|+|Biomass (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Liquids|Biomass (EJ/yr)"), - # fossil liquids - setNames(out[,,"FE|Liquids|+|Fossil (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Liquids|+|Fossil (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Liquids|Fossil (EJ/yr)"), - # synthetic liquids - setNames(out[,,"FE|Liquids|+|Hydrogen (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Liquids|+|Hydrogen (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Liquids|Hydrogen (EJ/yr)"), - - # gases - setNames(out[,,"FE|+|Gases (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|+|Gases (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Gases (EJ/yr)"), - # biomass Gases - setNames(out[,,"FE|Gases|+|Biomass (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Gases|+|Biomass (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Gases|Biomass (EJ/yr)"), - # fossil Gases - setNames(out[,,"FE|Gases|+|Fossil (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Gases|+|Fossil (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Gases|Fossil (EJ/yr)"), - # synthetic Gases - setNames(out[,,"FE|Gases|+|Hydrogen (EJ/yr)"] - - out[,,"FE|Transport|Bunkers|Gases|+|Hydrogen (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Gases|Hydrogen (EJ/yr)"), - - # solids - setNames(out[,,"FE|+|Solids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Solids (EJ/yr)"), - # biomass Solids - setNames(out[,,"FE|Solids|+|Biomass (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Solids|Biomass (EJ/yr)"), - # fossil Solids - setNames(out[,,"FE|Solids|+|Fossil (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"], - "FE|w/o Bunkers|w/o Non-energy Use|Solids|Fossil (EJ/yr)")) + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqfos"), dim=3), + "FE|Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqbio"), dim=3), + "FE|Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqsyn"), dim=3), + "FE|Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), + + + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segafos"), dim=3), + "FE|Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segabio"), dim=3), + "FE|Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segasyn"), dim=3), + "FE|Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)") - - + ) + + ### FE without non-energy use + out <- mbind(out, + + #total + setNames(dimSums(vm_demFeSector_woNonEn,dim=3), + "FE|w/o Non-energy Use (EJ/yr)"), + + #Liquids + setNames(dimSums(vm_demFeSector_woNonEn[,,c("fepet","fedie","fehos")],dim=3), "FE|w/o Non-energy Use|Liquids (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqbio"],dim=3), "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqfos"],dim=3), "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqsyn"] ,dim=3), "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)"), + + # Gases + setNames(dimSums(vm_demFeSector_woNonEn[,,c("fegas","fegat")],dim=3), "FE|w/o Non-energy Use|Gases (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"segabio"],dim=3), "FE|w/o Non-energy Use|Gases|+|Biomass (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"segafos"],dim=3), "FE|w/o Non-energy Use|Gases|+|Fossil (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"segasyn"] ,dim=3), "FE|w/o Non-energy Use|Gases|+|Hydrogen (EJ/yr)"), - } + # Solids + setNames(dimSums(vm_demFeSector_woNonEn[,,"fesos"],dim=3), "FE|w/o Non-energy Use|Solids (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"sesobio"],dim=3), "FE|w/o Non-energy Use|Solids|+|Biomass (EJ/yr)"), + setNames(dimSums(vm_demFeSector_woNonEn[,,"sesofos"],dim=3), "FE|w/o Non-energy Use|Solids|+|Fossil (EJ/yr)") + ) + + + + + #FE per sector and per emission market (ETS and ESR) + out <- mbind(out, + + #industry + setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS (EJ/yr)"), + + # industry liquids + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Hydrogen (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Hydrogen (EJ/yr)"), + + # industry solids + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids|+|Fossil (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids|+|Fossil (EJ/yr)"), + + # industry gases + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst"),dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Hydrogen (EJ/yr)"), + + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Fossil (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Hydrogen (EJ/yr)") + + + ) + + + } From 1f80c2a4d6f9e00433588348fe5ddcc102a30e79 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 22 Feb 2022 14:27:59 +0100 Subject: [PATCH 06/31] refactor and extend FE w/o Bunkers reporting for relevant variables --- R/reportFE.R | 54 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 1693cb72..5a4401aa 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -525,9 +525,7 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|Transport|++|ESR (EJ/yr)"], "FE|Transport|w/o Bunkers (EJ/yr)"), setNames(out[,,"FE|Transport|++|Outside ETS and ESR (EJ/yr)"], "FE|Transport|Bunkers (EJ/yr)") ) - out <- mbind(out, - setNames(out[,,"FE (EJ/yr)"] - out[,,"FE|Transport|Bunkers (EJ/yr)"], "FE|w/o Bunkers (EJ/yr)") - ) + @@ -1500,9 +1498,53 @@ reportFE <- function(gdx, regionSubsetList = NULL, } + ### FE variables without bunkers ---- + + + + + + + ### variables for which version without bunkers should be calculated + + fe.vars.woBunkers <- c( + + "FE (EJ/yr)", + "FE|++|Transport (EJ/yr)", + "FE|Transport|+|Liquids (EJ/yr)") + + + + # add FE w/o non-energy use variables if available + if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { + + + fe.vars.woBunkers <- c(fe.vars.woBunkers, + "FE|w/o Non-energy Use (EJ/yr)", + "FE|w/o Non-energy Use|Liquids (EJ/yr)") + + } + + + # variable names for FE variables without bunkers + names.woBunkers <- fe.vars.woBunkers + names.woBunkers <- gsub("FE", "FE|w/o Bunkers", names.woBunkers) + + + # calculate FE w/o Bunkers + out.woBunkers <- setNames(out[, , fe.vars.woBunkers], names.woBunkers) + out.woBunkers[, , names.woBunkers] <- out.woBunkers[,,names.woBunkers] - out[, , "FE|Transport|Bunkers (EJ/yr)"] + + # remove all pluses from variables of bunker correction + getNames(out.woBunkers) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers)) + getNames(out.woBunkers) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers)) + out <- mbind(out, out.woBunkers) + + + ### Aggregation to global values ---- # add global values out <- mbind(out,dimSums(out,dim=1)) @@ -1511,6 +1553,12 @@ reportFE <- function(gdx, regionSubsetList = NULL, out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList)) + + + ### Further Variable Calculations ---- + + + # add per sector electricity share (for SDG targets) out <- mbind(out, setNames(out[,,'FE|Buildings|+|Electricity (EJ/yr)'] / out[,,'FE|++|Buildings (EJ/yr)'] * 100, 'FE|Buildings|Electricity|Share (%)'), From bb764332aff993e4ae3a9302a79bff6c827089b8 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Wed, 23 Feb 2022 09:36:22 +0100 Subject: [PATCH 07/31] add non-energy use related plots to compare scenario 2 script --- .../compareScenarios2/cs2_01_summary.Rmd | 15 +++++++++++++-- .../compareScenarios2/cs2_03_emissions.Rmd | 18 ++++++++++++++++++ .../cs2_05_energy_demand.Rmd | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/inst/markdown/compareScenarios2/cs2_01_summary.Rmd b/inst/markdown/compareScenarios2/cs2_01_summary.Rmd index 82ef5423..495d7b00 100644 --- a/inst/markdown/compareScenarios2/cs2_01_summary.Rmd +++ b/inst/markdown/compareScenarios2/cs2_01_summary.Rmd @@ -138,7 +138,7 @@ items <- c( showAreaAndBarPlots(data, items) ``` -## FE Industry by carrier +## FE Industry by carrier (incl. non-energy use) ```{r FE Industry by carrier} items <- c( @@ -164,7 +164,7 @@ items <- c( showAreaAndBarPlots(data, items) ``` -## FE Transport by carrier +## FE Transport by carrier (incl. international bunkers) ```{r FE Transport by carrier} items <- c( @@ -187,6 +187,17 @@ items <- c( showAreaAndBarPlots(data, items) ``` + +## FE Non-energy Use by carrier + +```{r FE Non-energy Use by carrier} +items <- c( + "FE|Non-energy Use|Industry|Solids", + "FE|Non-energy Use|Industry|Liquids", + "FE|Non-energy Use|Industry|Gases") +showAreaAndBarPlots(data, items) +``` + ## SE Electricity by carrier ```{r SE Electricity by carrier} diff --git a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd index 9ac5b2d6..3a413d5e 100644 --- a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd +++ b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd @@ -234,6 +234,24 @@ items <- c( showLinePlots(data, items) ``` + +### Process Emissions Cement + +```{r} +items <- c( + "Emi|CO2|Industrial Processes|Cement") +showLinePlots(data, items) +``` + + +### Process Emissions Chemicals + +```{r} +items <- c( + "Emi|CO2|Industrial Processes|Chemicals") +showLinePlots(data, items) +``` + ### Land-Use Change ```{r} diff --git a/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd b/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd index 760fb706..634da275 100644 --- a/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd +++ b/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd @@ -130,6 +130,25 @@ showLinePlots(data, "FE|Industry|Chemicals") showLinePlots(data, c("FE|Industry|Other Industry", "FE|Industry|other")) ``` + +### FE|Non-energy Use|Industry per carrier and SE origin + +```{r} +tot <- "FE|Non-energy Use|Industry" +items <- c( + "FE|Non-energy Use|Industry|Solids|Fossil", + "FE|Non-energy Use|Industry|Solids|Biomass", + "FE|Non-energy Use|Industry|Liquids|Fossil", + "FE|Non-energy Use|Industry|Liquids|Biomass", + "FE|Non-energy Use|Industry|Liquids|Hydrogen", + "FE|Non-energy Use|Industry|Gases|Fossil", + "FE|Non-energy Use|Industry|Gases|Biomass", + "FE|Non-energy Use|Industry|Gases|Hydrogen") +showAreaAndBarPlots(data, items, tot) +``` + + + ### Specific Energy Consumption ```{r} From 72253e8663dd364222fd7bb9bbcfc28bc63279cb Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Mon, 7 Mar 2022 13:16:53 +0100 Subject: [PATCH 08/31] add further FE liquids variables to bunker correction part --- R/reportFE.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/reportFE.R b/R/reportFE.R index 5a4401aa..f70f4a28 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1521,7 +1521,10 @@ reportFE <- function(gdx, regionSubsetList = NULL, fe.vars.woBunkers <- c(fe.vars.woBunkers, "FE|w/o Non-energy Use (EJ/yr)", - "FE|w/o Non-energy Use|Liquids (EJ/yr)") + "FE|w/o Non-energy Use|Liquids (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)") } From 38337006fad42f2262bf7727995bcd1dda54d4d0 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Tue, 8 Mar 2022 15:29:32 +0100 Subject: [PATCH 09/31] correct bunker substraction for fossil, biomass, synthetic liquids --- R/reportFE.R | 57 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index f70f4a28..24b254ef 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -525,6 +525,8 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|Transport|++|ESR (EJ/yr)"], "FE|Transport|w/o Bunkers (EJ/yr)"), setNames(out[,,"FE|Transport|++|Outside ETS and ESR (EJ/yr)"], "FE|Transport|Bunkers (EJ/yr)") ) + + @@ -537,8 +539,9 @@ reportFE <- function(gdx, regionSubsetList = NULL, pm_cesdata <- readGDX(gdx,"pm_cesdata")[,t,] # ---- variables - if((buil_mod %in% c("services_putty", "services_with_capital"))||(tran_mod == "edge_esm") ) + if((buil_mod %in% c("services_putty", "services_with_capital"))||(tran_mod == "edge_esm") ) { vm_demFeForEs <- readGDX(gdx,name = c("vm_demFeForEs"), field="l", restore_zeros=FALSE,format= "first_found",react = "silent")[,t,]*TWa_2_EJ + } #vm_demFeForEs = vm_demFeForEs[fe2es] # CES nodes, convert from TWa to EJ vm_cesIO <- readGDX(gdx, name=c("vm_cesIO"), field="l", restore_zeros=FALSE,format= "first_found")[,t,]*TWa_2_EJ @@ -1514,6 +1517,8 @@ reportFE <- function(gdx, regionSubsetList = NULL, "FE|Transport|+|Liquids (EJ/yr)") + + # add FE w/o non-energy use variables if available if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { @@ -1521,30 +1526,68 @@ reportFE <- function(gdx, regionSubsetList = NULL, fe.vars.woBunkers <- c(fe.vars.woBunkers, "FE|w/o Non-energy Use (EJ/yr)", - "FE|w/o Non-energy Use|Liquids (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)") + "FE|w/o Non-energy Use|Liquids (EJ/yr)") } + # bunker correction for distinction of fossil, biomass, hydrogen-based liquids + fe.vars.woBunkers.fos <- c( "FE|Liquids|+|Fossil (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)", + "FE|Transport|Liquids|+|Fossil (EJ/yr)") + + fe.vars.woBunkers.bio <- c( "FE|Liquids|+|Biomass (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)", + "FE|Transport|Liquids|+|Biomass (EJ/yr)") + + fe.vars.woBunkers.syn <- c( "FE|Liquids|+|Hydrogen (EJ/yr)", + "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)", + "FE|Transport|Liquids|+|Hydrogen (EJ/yr)") + + # variable names for FE variables without bunkers names.woBunkers <- fe.vars.woBunkers names.woBunkers <- gsub("FE", "FE|w/o Bunkers", names.woBunkers) + names.woBunkers.fos <- fe.vars.woBunkers.fos + names.woBunkers.fos <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.fos) - # calculate FE w/o Bunkers + names.woBunkers.bio <- fe.vars.woBunkers.bio + names.woBunkers.bio <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.bio) + + names.woBunkers.syn <- fe.vars.woBunkers.syn + names.woBunkers.syn <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.syn) + + + # calculate FE w/o Bunkers (for now we only have liquids energy use in bunkers) out.woBunkers <- setNames(out[, , fe.vars.woBunkers], names.woBunkers) out.woBunkers[, , names.woBunkers] <- out.woBunkers[,,names.woBunkers] - out[, , "FE|Transport|Bunkers (EJ/yr)"] + out.woBunkers.fos <- setNames(out[, , fe.vars.woBunkers.fos], names.woBunkers.fos) + out.woBunkers.fos[, , names.woBunkers.fos] <- out.woBunkers.fos[,,names.woBunkers.fos] - out[, , "FE|Transport|Bunkers|Liquids|+|Fossil (EJ/yr)"] + + out.woBunkers.bio <- setNames(out[, , fe.vars.woBunkers.bio], names.woBunkers.bio) + out.woBunkers.bio[, , names.woBunkers.bio] <- out.woBunkers.bio[,,names.woBunkers.bio] - out[, , "FE|Transport|Bunkers|Liquids|+|Biomass (EJ/yr)"] + + out.woBunkers.syn <- setNames(out[, , fe.vars.woBunkers.syn], names.woBunkers.syn) + out.woBunkers.syn[, , names.woBunkers.syn] <- out.woBunkers.syn[,,names.woBunkers.syn] - out[, , "FE|Transport|Bunkers|Liquids|+|Hydrogen (EJ/yr)"] + # remove all pluses from variables of bunker correction getNames(out.woBunkers) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers)) getNames(out.woBunkers) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers)) + getNames(out.woBunkers.fos) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.fos)) + getNames(out.woBunkers.fos) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.fos)) + + getNames(out.woBunkers.bio) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.bio)) + getNames(out.woBunkers.bio) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.bio)) + + getNames(out.woBunkers.syn) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.syn)) + getNames(out.woBunkers.syn) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.syn)) + - out <- mbind(out, out.woBunkers) + out <- mbind(out, out.woBunkers, out.woBunkers.fos, out.woBunkers.bio, out.woBunkers.syn) ### Aggregation to global values ---- From 432ae5fa977260ce2fdb8ef93e236844beab04d8 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Fri, 11 Mar 2022 16:32:06 +0100 Subject: [PATCH 10/31] make bunkers correction backwards compatible with version without non-energy use FE variables --- R/reportFE.R | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index c1746bde..34192222 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -12,7 +12,7 @@ #' @param t temporal resolution of the reporting, default: #' t=c(seq(2005,2060,5),seq(2070,2110,10),2130,2150) #' -#' @author Renato Rodrigues, Christoph Bertram, Antoine Levesque +#' @author Renato Rodrigues, Felix Schreyer #' @examples #' #' \dontrun{reportFE(gdx)} @@ -1532,18 +1532,34 @@ reportFE <- function(gdx, regionSubsetList = NULL, # bunker correction for distinction of fossil, biomass, hydrogen-based liquids fe.vars.woBunkers.fos <- c( "FE|Liquids|+|Fossil (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)", "FE|Transport|Liquids|+|Fossil (EJ/yr)") fe.vars.woBunkers.bio <- c( "FE|Liquids|+|Biomass (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)", "FE|Transport|Liquids|+|Biomass (EJ/yr)") fe.vars.woBunkers.syn <- c( "FE|Liquids|+|Hydrogen (EJ/yr)", - "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)", "FE|Transport|Liquids|+|Hydrogen (EJ/yr)") + # add FE w/o non-energy use variables if available + if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { + + + # bunker correction for distinction of fossil, biomass, hydrogen-based liquids + fe.vars.woBunkers.fos <- c( fe.vars.woBunkers.fos, + "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)") + + fe.vars.woBunkers.bio <- c( fe.vars.woBunkers.bio, + "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)") + + fe.vars.woBunkers.syn <- c( fe.vars.woBunkers.syn, + "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)") + + + + } + + # variable names for FE variables without bunkers names.woBunkers <- fe.vars.woBunkers From f78238d136d6bf2f2a6587d3b38a347c0440b1b4 Mon Sep 17 00:00:00 2001 From: Felix Schreyer Date: Wed, 23 Mar 2022 16:53:53 +0100 Subject: [PATCH 11/31] adapt reporting to include feedstock CDR, that is, carbon stored in products from non-fossil origin --- R/reportEmi.R | 73 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index edfbf22f..69bfefbd 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -95,6 +95,11 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 se_gas <- c("segafos", "segabio") } se_sol <- c("sesofos", "sesobio") + + # sets of hydrocarbon SE carriers + entySeFos <- readGDX(gdx, "entySeFos") + entySeBio <- readGDX(gdx, "entySeBio") + entySeSyn <- readGDX(gdx, "entySeSyn") ### emissions variables from REMIND (see definitions in core/equations.gms) @@ -158,6 +163,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_demFENonEnergySector <- NULL } + vm_FeedstocksCarbon <- readGDX(gdx, "vm_FeedstocksCarbon", field = "l", restore_zeros = T, react = "silent")[,t,] + # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) @@ -196,8 +203,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 left_join(entyFe2Sector) %>% left_join(sector2emiMkt) %>% select( -all_te) %>% - mutate( name = paste(all_enty,all_enty1,emi_sectors,all_emiMkt, sep = ".")) - + mutate( name = paste(all_enty,all_enty1,emi_sectors,all_emiMkt, sep = ".")) + # calculate FE without non-energy use if (!is.null(vm_demFENonEnergySector)) { @@ -211,6 +218,24 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector } + # get CDR by feedstock carbon from non-fossil sources (biogenic or synthetic) + if (!is.null(vm_FeedstocksCarbon)) { + + # get combinations of SE,FE,sector,emiMkt that exist in vm_FeedstocksCarbon + FE.feed.map <- se2fe %>% + left_join(entyFe2Sector) %>% + left_join(sector2emiMkt) %>% + right_join(entyFe2sector2emiMkt_NonEn %>% + rename(all_enty1 = all_enty)) %>% + select( -all_te) %>% + mutate( name = paste(all_enty,all_enty1,all_emiMkt, sep = ".")) + Feedstock_CDR <- mselect(vm_FeedstocksCarbon[,,FE.feed.map$name], + all_enty = unique(entySeBio, entySeSyn)) + + + } else { + Feedstock_CDR <- collapseDim(vm_co2eq)*0 + } @@ -341,7 +366,10 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # fe emissions setNames((dimSums(EmiFeCarrier, dim = 3) # substract industry CCS - - dimSums(vm_emiIndCCS[, , emiInd37_fuel], dim = 3)*p_share_CCS) * GtC_2_MtCO2, + - dimSums(vm_emiIndCCS[, , emiInd37_fuel], dim = 3)*p_share_CCS + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(Feedstock_CDR, dim=3) + )*GtC_2_MtCO2, "Emi|CO2|Energy|+|Demand (Mt CO2/yr)") ) @@ -468,8 +496,12 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames(dimSums(EmiFeCarrier[, , "build"], dim = 3) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|+|Buildings (Mt CO2/yr)"), # industry emissions: fe carrier emissions - industry CCS - # (carbon captured by industry used for CCU is still accounted as industry emissions as only account negative emissions for the CO2 provider if carbon gets stored) - setNames((dimSums(EmiFeCarrier[, , "indst"], dim = 3) - dimSums(vm_emiIndCCS[, , emiInd37_fuel]* p_share_CCS, dim = 3) ) * GtC_2_MtCO2, + # (carbon captured by industry used for CCU is still accounted as industry emissions + # as only account negative emissions for the CO2 provider if carbon gets stored) + setNames((dimSums(EmiFeCarrier[, , "indst"], dim = 3) + - dimSums(vm_emiIndCCS[, , emiInd37_fuel]* p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(Feedstock_CDR, dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|+|Industry (Mt CO2/yr)"), # transport emissions: fe carrier emissions setNames(dimSums(EmiFeCarrier[, , "trans"], dim = 3) * GtC_2_MtCO2, @@ -523,17 +555,23 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # solids emissions: direct solids emissions of all sectors - industry CCS of solids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Solids (Mt CO2/yr)"), # liquids emissions: direct liquids emissions of all sectors - industry CCS of liquids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos", "fepet", "fedie")), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Liquids (Mt CO2/yr)"), # gases emissions: direct gases emissions of all sectors - industry CCS of gases setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas", "fegat")), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Gases (Mt CO2/yr)") ) @@ -544,17 +582,23 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # industry solids emissions: direct solids emissions - industry CCS of solids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|+|Solids (Mt CO2/yr)"), # industry liquids emissions: direct liquids emissions - industry CCS of liquids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|+|Liquids (Mt CO2/yr)"), # industry gases emissions: direct gases emissions - industry CCS of gases setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) - - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(mselect(Feedstock_CDR, all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|+|Gases (Mt CO2/yr)"), # buildings @@ -582,7 +626,10 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Emi|CO2|Energy|Demand|Industry|++|Steel (Mt CO2/yr)"), setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "cement"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "cement")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|++|Cement (Mt CO2/yr)"), - setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "chemicals"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "chemicals")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "chemicals"), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "chemicals")*p_share_CCS, dim = 3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(Feedstock_CDR, dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|++|Chemicals (Mt CO2/yr)"), setNames((dimSums(mselect(EmiIndSubSec, secInd37 = "otherInd"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, secInd37 = "otherInd")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|++|Other Industry (Mt CO2/yr)")) @@ -1646,6 +1693,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # industry CCS # TODO: adapt to industry ETS/ESR split - dimSums(vm_emiIndCCS[, , emiInd37_fuel]*p_share_CCS, dim=3) + # substract synthetic and biogenic carbon contained in industrial feedstocks + - dimSums(Feedstock_CDR, dim=3) # add captured CO2 from cement process which is not stored # (EmiMACEq for co2cement_process contains cement process emissions - captured cement co2 process emissions) + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2 From 286805b8f29231cbacd0e2d4c314b8bfe8fa052b Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 22 Jul 2022 17:50:40 +0200 Subject: [PATCH 12/31] fix bugs introduced when fetching --- R/reportFE.R | 248 +++++++++++++++++++++++++-------------------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 7e56bdbb..32c48468 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -11,7 +11,7 @@ #' be created. #' @param t temporal resolution of the reporting, default: #' t=c(seq(2005,2060,5),seq(2070,2110,10),2130,2150) -#' +#' #' @author Renato Rodrigues, Felix Schreyer #' @examples #' @@ -48,17 +48,17 @@ reportFE <- function(gdx, regionSubsetList = NULL, se2fe <- readGDX(gdx,"se2fe") entyFe2Sector <- readGDX(gdx, "entyFe2Sector") sector2emiMkt <- readGDX(gdx, "sector2emiMkt") - + entyFe2sector2emiMkt_NonEn <- readGDX(gdx, "entyFe2sector2emiMkt_NonEn", react = "silent") - - demFemapping <- entyFe2Sector %>% - full_join(sector2emiMkt, by = 'emi_sectors') %>% - # rename such that all_enty1 always signifies the FE carrier like in + + demFemapping <- entyFe2Sector %>% + full_join(sector2emiMkt, by = 'emi_sectors') %>% + # rename such that all_enty1 always signifies the FE carrier like in # vm_demFeSector - rename(all_enty1 = all_enty) %>% - left_join(se2fe, by = 'all_enty1') %>% - select(-all_te) - + rename(all_enty1 = all_enty) %>% + left_join(se2fe, by = 'all_enty1') %>% + select(-all_te) + #sety <- readGDX(gdx,c("entySe","sety"),format="first_found") # ---- parameter @@ -71,12 +71,12 @@ reportFE <- function(gdx, regionSubsetList = NULL, #vm_prodFe <- vm_prodFe[se2fe] vm_demFeSector <- readGDX(gdx,name=c("vm_demFeSector"),field="l",format="first_found",restore_zeros=FALSE)[,t,]*TWa_2_EJ vm_demFeSector[is.na(vm_demFeSector)] <- 0 - # FE non-energy use + # FE non-energy use vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T, react = "silent")[,t,]*TWa_2_EJ if (length(vm_demFENonEnergySector) == 0) { vm_demFENonEnergySector <- NULL } - + # only retain combinations of SE, FE, sector, and emiMkt which actually exist in the model (see qm_balFe) vm_demFeSector <- vm_demFeSector[demFemapping] @@ -129,26 +129,26 @@ reportFE <- function(gdx, regionSubsetList = NULL, buil_mod = find_real_module(module2realisation,"buildings") cdr_mod = find_real_module(module2realisation,"CDR") - - - + + + # Preliminary Calculations ---- - + # calculate FE non-energy use and FE without non-energy use if (!is.null(vm_demFENonEnergySector)) { - vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[demFemapping], + vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[demFemapping], all_enty1 = entyFe2sector2emiMkt_NonEn$all_enty, emi_sectors = entyFe2sector2emiMkt_NonEn$emi_sectors, all_emiMkt = entyFe2sector2emiMkt_NonEn$all_emiMkt) - + # calculate FE without non-energy use vm_demFeSector_woNonEn <- vm_demFeSector - vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector + vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector } - - - + + + # ---- FE total production (incl. non-energy use) ------ out <- mbind(out, @@ -532,9 +532,9 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|Transport|++|ESR (EJ/yr)"], "FE|Transport|w/o Bunkers (EJ/yr)"), setNames(out[,,"FE|Transport|++|Outside ETS and ESR (EJ/yr)"], "FE|Transport|Bunkers (EJ/yr)") ) - out <- mbind(out, - setNames(out[,,"FE (EJ/yr)"] - out[,,"FE|Transport|Bunkers (EJ/yr)"], "FE|w/o Bunkers (EJ/yr)") - ) + # out <- mbind(out, + # setNames(out[,,"FE (EJ/yr)"] - out[,,"FE|Transport|Bunkers (EJ/yr)"], "FE|w/o Bunkers (EJ/yr)") +# ) @@ -1364,23 +1364,23 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|Solids|Biomass|+|Modern (EJ/yr)"] * share_sol_noTrad_indu, "FE|Industry|Solids|Biomass|Modern (EJ/yr)"), setNames(out[,,"FE|Solids|Fossil|+|Coal (EJ/yr)"] * share_sol_noTrad_indu, "FE|Industry|Solids|Coal (EJ/yr)") ) - - + + #### Non-energy Use Reporting ---- - - + + if (!is.null(vm_demFENonEnergySector)) { - - - # FE non-energy use variables + + + # FE non-energy use variables out <- mbind(out, setNames(dimSums(vm_demFENonEnergySector, dim=3), "FE|Non-energy Use (EJ/yr)"), - - + + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst"), dim=3), "FE|Non-energy Use|+|Industry (EJ/yr)"), - + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos"), dim=3), "FE|Non-energy Use|Industry|+|Solids (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos"), dim=3), @@ -1388,202 +1388,202 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas"), dim=3), "FE|Non-energy Use|Industry|+|Gases (EJ/yr)") ) - - + + # FE non-energy use per SE origin out <- mbind(out, - + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos", all_enty = "sesofos"), dim=3), "FE|Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fesos", all_enty = "sesobio"), dim=3), "FE|Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)"), - + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqfos"), dim=3), "FE|Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqbio"), dim=3), "FE|Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fehos", all_enty = "seliqsyn"), dim=3), "FE|Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - - + + setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segafos"), dim=3), "FE|Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segabio"), dim=3), "FE|Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segasyn"), dim=3), "FE|Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)") - + ) - - + + ### FE without non-energy use out <- mbind(out, - + #total - setNames(dimSums(vm_demFeSector_woNonEn,dim=3), + setNames(dimSums(vm_demFeSector_woNonEn,dim=3), "FE|w/o Non-energy Use (EJ/yr)"), - - #Liquids + + #Liquids setNames(dimSums(vm_demFeSector_woNonEn[,,c("fepet","fedie","fehos")],dim=3), "FE|w/o Non-energy Use|Liquids (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqbio"],dim=3), "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqfos"],dim=3), "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"seliqsyn"] ,dim=3), "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)"), - - # Gases + + # Gases setNames(dimSums(vm_demFeSector_woNonEn[,,c("fegas","fegat")],dim=3), "FE|w/o Non-energy Use|Gases (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"segabio"],dim=3), "FE|w/o Non-energy Use|Gases|+|Biomass (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"segafos"],dim=3), "FE|w/o Non-energy Use|Gases|+|Fossil (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"segasyn"] ,dim=3), "FE|w/o Non-energy Use|Gases|+|Hydrogen (EJ/yr)"), - # Solids + # Solids setNames(dimSums(vm_demFeSector_woNonEn[,,"fesos"],dim=3), "FE|w/o Non-energy Use|Solids (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"sesobio"],dim=3), "FE|w/o Non-energy Use|Solids|+|Biomass (EJ/yr)"), setNames(dimSums(vm_demFeSector_woNonEn[,,"sesofos"],dim=3), "FE|w/o Non-energy Use|Solids|+|Fossil (EJ/yr)") ) - - - + + + #FE per sector and per emission market (ETS and ESR) out <- mbind(out, - + #industry setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry (EJ/yr)"), - setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR (EJ/yr)"), - setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS (EJ/yr)"), + # industry liquids setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst") ,dim=3,na.rm=T)),"FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Liquids|+|Hydrogen (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids (EJ/yr)"), - setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Biomass (EJ/yr)"), + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqbio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqfos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fehos",all_enty="seliqsyn",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Liquids|+|Hydrogen (EJ/yr)"), - + # industry solids setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Solids|+|Fossil (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesobio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fesos",all_enty="sesofos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Solids|+|Fossil (EJ/yr)"), - + # industry gases setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst"),dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst", all_emiMkt="ES") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ESR|Gases|+|Hydrogen (EJ/yr)"), - + setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segabio",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Biomass (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segafos",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Fossil (EJ/yr)"), setNames((dimSums(mselect(vm_demFeSector_woNonEn,all_enty1="fegas",all_enty="segasyn",emi_sectors="indst", all_emiMkt="ETS") ,dim=3,na.rm=T)), "FE|w/o Non-energy Use|Industry|ETS|Gases|+|Hydrogen (EJ/yr)") - - + + ) - - - + + + } ### FE variables without bunkers ---- - - - - - - + + + + + + ### variables for which version without bunkers should be calculated fe.vars.woBunkers <- c( - + "FE (EJ/yr)", "FE|++|Transport (EJ/yr)", "FE|Transport|+|Liquids (EJ/yr)") - - - - + + + + # add FE w/o non-energy use variables if available if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { - - + + fe.vars.woBunkers <- c(fe.vars.woBunkers, "FE|w/o Non-energy Use (EJ/yr)", "FE|w/o Non-energy Use|Liquids (EJ/yr)") - + } - + # bunker correction for distinction of fossil, biomass, hydrogen-based liquids fe.vars.woBunkers.fos <- c( "FE|Liquids|+|Fossil (EJ/yr)", "FE|Transport|Liquids|+|Fossil (EJ/yr)") - + fe.vars.woBunkers.bio <- c( "FE|Liquids|+|Biomass (EJ/yr)", "FE|Transport|Liquids|+|Biomass (EJ/yr)") - + fe.vars.woBunkers.syn <- c( "FE|Liquids|+|Hydrogen (EJ/yr)", "FE|Transport|Liquids|+|Hydrogen (EJ/yr)") - - + + # add FE w/o non-energy use variables if available if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { - - + + # bunker correction for distinction of fossil, biomass, hydrogen-based liquids fe.vars.woBunkers.fos <- c( fe.vars.woBunkers.fos, "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)") - + fe.vars.woBunkers.bio <- c( fe.vars.woBunkers.bio, "FE|w/o Non-energy Use|Liquids|+|Biomass (EJ/yr)") - + fe.vars.woBunkers.syn <- c( fe.vars.woBunkers.syn, "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)") - - + + } - - - + + + # variable names for FE variables without bunkers names.woBunkers <- fe.vars.woBunkers names.woBunkers <- gsub("FE", "FE|w/o Bunkers", names.woBunkers) - + names.woBunkers.fos <- fe.vars.woBunkers.fos names.woBunkers.fos <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.fos) - + names.woBunkers.bio <- fe.vars.woBunkers.bio names.woBunkers.bio <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.bio) - + names.woBunkers.syn <- fe.vars.woBunkers.syn names.woBunkers.syn <- gsub("FE", "FE|w/o Bunkers", names.woBunkers.syn) - - + + # calculate FE w/o Bunkers (for now we only have liquids energy use in bunkers) out.woBunkers <- setNames(out[, , fe.vars.woBunkers], names.woBunkers) out.woBunkers[, , names.woBunkers] <- out.woBunkers[,,names.woBunkers] - out[, , "FE|Transport|Bunkers (EJ/yr)"] - + out.woBunkers.fos <- setNames(out[, , fe.vars.woBunkers.fos], names.woBunkers.fos) out.woBunkers.fos[, , names.woBunkers.fos] <- out.woBunkers.fos[,,names.woBunkers.fos] - out[, , "FE|Transport|Bunkers|Liquids|+|Fossil (EJ/yr)"] @@ -1592,40 +1592,40 @@ reportFE <- function(gdx, regionSubsetList = NULL, out.woBunkers.syn <- setNames(out[, , fe.vars.woBunkers.syn], names.woBunkers.syn) out.woBunkers.syn[, , names.woBunkers.syn] <- out.woBunkers.syn[,,names.woBunkers.syn] - out[, , "FE|Transport|Bunkers|Liquids|+|Hydrogen (EJ/yr)"] - + # remove all pluses from variables of bunker correction getNames(out.woBunkers) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers)) getNames(out.woBunkers) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers)) - + getNames(out.woBunkers.fos) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.fos)) getNames(out.woBunkers.fos) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.fos)) - + getNames(out.woBunkers.bio) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.bio)) getNames(out.woBunkers.bio) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.bio)) - + getNames(out.woBunkers.syn) <- gsub("\\|\\+\\|", "\\|", getNames(out.woBunkers.syn)) getNames(out.woBunkers.syn) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.syn)) - - - + + + out <- mbind(out, out.woBunkers, out.woBunkers.fos, out.woBunkers.bio, out.woBunkers.syn) - - + + ### Aggregation to global values ---- - + # add global values out <- mbind(out,dimSums(out,dim=1)) # add other region aggregations if (!is.null(regionSubsetList)) out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList)) - - - - + + + + ### Further Variable Calculations ---- - - - + + + # add per sector electricity share (for SDG targets) out <- mbind(out, setNames(out[,,'FE|Buildings|+|Electricity (EJ/yr)'] / out[,,'FE|++|Buildings (EJ/yr)'] * 100, 'FE|Buildings|Electricity|Share (%)'), From 53c625edb96817d6adffc620b147a148790f05c9 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 29 Jul 2022 17:13:33 +0200 Subject: [PATCH 13/31] fix compatibility of reportEmi.R for feedstocks implementation --- R/reportEmi.R | 65 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 2cd9b862..c5c75cd7 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -156,7 +156,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 pm_IndstCO2Captured <- pm_IndstCO2Captured[se2fe[,c(1,2)]] } # FE non-energy use - vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", restore_zeros = T, react = "silent")[,t,] + vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", react = "silent")[,t,] if (length(vm_demFENonEnergySector) == 0) { vm_demFENonEnergySector <- NULL } @@ -549,32 +549,54 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # calculate demand-side Solids, Liquids and gases emissions (after industry CO2 Capture) + if (!is.null(vm_FeedstocksCarbon)){ out <- mbind(out, # solids emissions: direct solids emissions of all sectors - industry CCS of solids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("sesobio","sesosyn"), all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Solids (Mt CO2/yr)"), # liquids emissions: direct liquids emissions of all sectors - industry CCS of liquids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos", "fepet", "fedie")), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("seliqbio","seliqsyn"), all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Liquids (Mt CO2/yr)"), # gases emissions: direct gases emissions of all sectors - industry CCS of gases setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas", "fegat")), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("segabio","segasyn"), all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|++|Gases (Mt CO2/yr)") + ) + } else { + out <- mbind(out, + + # solids emissions: direct solids emissions of all sectors - industry CCS of solids + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos"), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|++|Solids (Mt CO2/yr)"), + + # liquids emissions: direct liquids emissions of all sectors - industry CCS of liquids + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos", "fepet", "fedie")), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|++|Liquids (Mt CO2/yr)"), + + # gases emissions: direct gases emissions of all sectors - industry CCS of gases + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas", "fegat")), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Gases (Mt CO2/yr)") ) + } + # sectoral demand-side emissions across energy carriers + if (!is.null(vm_FeedstocksCarbon)){ out <- mbind(out, @@ -582,23 +604,50 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "indst"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("sesobio","sesosyn"), all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|+|Solids (Mt CO2/yr)"), # industry liquids emissions: direct liquids emissions - industry CCS of liquids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("seliqbio","seliqsyn"), all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Industry|+|Liquids (Mt CO2/yr)"), # industry gases emissions: direct gases emissions - industry CCS of gases setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(Feedstock_CDR, all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, - "Emi|CO2|Energy|Demand|Industry|+|Gases (Mt CO2/yr)"), + - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("segabio","segasyn"), all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|Industry|+|Gases (Mt CO2/yr)") + ) + } else { + + out <- mbind(out, + # industry solids emissions: direct solids emissions - industry CCS of solids + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "indst"), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) + ) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|Industry|+|Solids (Mt CO2/yr)"), + + # industry liquids emissions: direct liquids emissions - industry CCS of liquids + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) + ) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|Industry|+|Liquids (Mt CO2/yr)"), + + # industry gases emissions: direct gases emissions - industry CCS of gases + setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) + - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) + ) * GtC_2_MtCO2, + "Emi|CO2|Energy|Demand|Industry|+|Gases (Mt CO2/yr)") + + ) + + } + + out <- mbind(out, # buildings setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos", emi_sectors = "build"), dim = 3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|Buildings|+|Solids (Mt CO2/yr)"), From dcfe4f3682992fa0c6b9ce08ae0135c4eb700ab4 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Wed, 31 Aug 2022 16:43:13 +0200 Subject: [PATCH 14/31] fix reportEmi.R and another attempt to make this dev branch compatible with main --- .buildlibrary | 2 +- .pre-commit-config.yaml | 2 +- .zenodo.json | 2 +- DESCRIPTION | 4 +- NAMESPACE | 5 + R/createVarListHtml.R | 107 +++++++++++ R/getPath.R | 44 +++++ R/loadModeltest.R | 61 +++++-- R/reportEmi.R | 138 +++++++++++++- R/reportFE.R | 13 +- R/variablesAsList.R | 159 +++++++++------- README.md | 6 +- inst/extdata/variablesAsListTemplate.html | 20 ++ inst/markdown/compareScenarios2/cs2_main.Rmd | 31 +++- man/createVarListHtml.Rd | 61 +++++++ man/getPath.Rd | 30 +++ man/variablesAsList.Rd | 28 ++- vignettes/compareScenarios2.Rmd | 183 +++++++++++-------- 18 files changed, 711 insertions(+), 185 deletions(-) create mode 100644 R/createVarListHtml.R create mode 100644 R/getPath.R create mode 100644 inst/extdata/variablesAsListTemplate.html create mode 100644 man/createVarListHtml.Rd create mode 100644 man/getPath.Rd diff --git a/.buildlibrary b/.buildlibrary index 8121d76d..fa67fc1b 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '38312716' +ValidationKey: '211730519' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4040f2b2..7c742e74 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - id: mixed-line-ending - repo: https://github.com/lorenzwalthert/precommit - rev: v0.3.2 + rev: v0.3.2.9001 hooks: - id: parsable-R - id: deps-in-desc diff --git a/.zenodo.json b/.zenodo.json index 7928ec6c..4caa0be9 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "remind2: The REMIND R package (2nd generation)", - "version": "1.99.4", + "version": "1.101.1", "description": "

Contains the REMIND-specific routines for data and model\n output manipulation.<\/p>", "creators": [ { diff --git a/DESCRIPTION b/DESCRIPTION index 65eb6718..fbbe601c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: remind2 Title: The REMIND R package (2nd generation) -Version: 1.99.4 -Date: 2022-08-10 +Version: 1.101.1 +Date: 2022-08-25 Authors@R: person("Renato", "Rodrigues", , "renato.rodrigues@pik-potsdam.de", role = c("aut", "cre")) Description: Contains the REMIND-specific routines for data and model diff --git a/NAMESPACE b/NAMESPACE index 78536662..f568e2e8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,10 +16,15 @@ export(convGDX2MIF) export(convGDX2MIF_LCOE) export(convGDX2MIF_fallback_for_coupling) export(convergenceCheck) +export(createVarListHtml) export(dashboard) export(deletePlus) export(gdx.copy) +export(getCfgDefaultPath) +export(getCfgScenPath) export(getCs2Profiles) +export(getMifHistPath) +export(getMifScenPath) export(getRunsMIFGDX) export(get_total_efficiencies) export(loadCs2Data) diff --git a/R/createVarListHtml.R b/R/createVarListHtml.R new file mode 100644 index 00000000..e4c5e57e --- /dev/null +++ b/R/createVarListHtml.R @@ -0,0 +1,107 @@ +node2Html <- function(name, item) { + if (name == "INFO") { + html <- info2Html(item) + } else if (is.list(item)) { + html <- innerNode2Html(name, item) + } else { + html <- paste0( + "

", name, ": ", + paste0(as.character(item), collapse = ", "), "
") + } + return(html) +} + +innerNode2Html <- function(name, item) { + content <- namedList2Html(item) + if ("INFO" %in% names(item)) name <- paste0("", name, "") + html <- c( + "
", + paste0(" ", name, ""), + paste0(" ", content), + "
") + return(html) +} + +info2Html <- function(item) { + name <- "INFO" + content <- paste0( + "", names(item), ": ", + vapply(item, paste0, character(1), collapse = ", ")) + html <- c( + "
", + paste0(" ", name, ""), + "
    ", + paste0("
  • ", content, "
  • "), + "
", + "
") + return(html) +} + +namedList2Html <- function(v) { + lines <- lapply(seq_along(v), \(i) node2Html(names(v)[i], v[[i]])) + html <- unlist(lines) + return(html) +} + +renderVarListAsHtml <- function(varList, outFileName, title, htmlBefore) { + htmlLines <- namedList2Html(varList) + htmlTemplateFile <- system.file("extdata/variablesAsListTemplate.html", package = "remind2") + htmlTemplate <- readLines(htmlTemplateFile) + html <- + htmlTemplate %>% + sub("[:TITLE:]", title, ., fixed = TRUE) %>% + sub("[:BEFORE:]", htmlBefore, ., fixed = TRUE) %>% + sub("[:CONTENT:]", paste0(htmlLines, collapse = "\n"), ., fixed = TRUE) + write(html, outFileName) + return(invisible(NULL)) +} + +#' Create an HTML Document of a Hierarchical List of Variables +#' +#' Creates a hierarchical list from mif data using \code{\link{variablesAsList}} +#' and writes it as an HTML document that displays the hierarchy via the +#' \code{
} HTML5-tag. +#' +#' @seealso \code{\link{variablesAsList}} +#' @param outFileName A single string. The path of the output file, preferably +#' ending in \code{.html} +#' @param title The title displayed at the top of the created HTML. +#' @param htmlBefore \code{character(1)}. HTML to be put between title and list of variables. +#' @inheritParams variablesAsList +#' @examples +#' \dontrun{ +#' loadModeltest() +#' createVarListHtml(data, "variables.html") +#' detailsAR6 <- +#' readr::read_delim( +#' paste0( +#' "https://raw.githubusercontent.com/pik-piam/", +#' "project_interfaces/master/ar6/mapping_template_AR6.csv"), +#' delim = ";", +#' col_select = c(r21m42, Definition) +#' ) %>% +#' rename(name = r21m42) +#' createVarListHtml( +#' data, +#' "variablesWithDescription.html", +#' title = "Reported REMIND Variables with AR6 Description", +#' usePlus = TRUE, +#' details = detailsAR6) +#' } +#' @export +createVarListHtml <- function( + x, + outFileName, + title = "List of Variables", + htmlBefore = "", + usePlus = FALSE, + details = NULL + ) { + message("Creating the hierarchical list structure...") + varList <- variablesAsList(x, entry = "INFO", usePlus = usePlus, details = details) + outFileName <- normalizePath(outFileName, mustWork = FALSE) + message("Creating HTML and writing it to ", outFileName, "...") + renderVarListAsHtml(varList, outFileName, title, htmlBefore) + message("Done.") + return(invisible(NULL)) +} diff --git a/R/getPath.R b/R/getPath.R new file mode 100644 index 00000000..522a1097 --- /dev/null +++ b/R/getPath.R @@ -0,0 +1,44 @@ +getScenNamesFast <- function(outputDirs) { + folder <- basename(outputDirs) + substr(folder, start = 0, stop = nchar(folder) - 20) +} + +#' Get Paths to Certain Files in the REMIND Directory +#' +#' \code{getMifScenPath}: get path to the scenarios' reporting mifs. +#' \code{getMifHistPath}: get path to the scenarios' historical.mif. +#' \code{getCfgScenPath}: get path to the scenarios' config.Rdata. +#' \code{getCfgDefaultPath}: get path to REMIND's default.cfg. +#' +#' +#' @param outputDirs A character vector of paths to output folders of REMIND runs. +#' @param mustWork \code{logical(1)}. Throw error if file not available? +#' @rdname getPath +#' @export +getMifScenPath <- function(outputDirs, mustWork = FALSE) { + names <- getScenNamesFast(outputDirs) + path <- file.path(outputDirs, paste0("REMIND_generic_", names, ".mif")) + normalizePath(path, mustWork = mustWork) +} + +#' @rdname getPath +#' @export +getMifHistPath <- function(outputDirs, mustWork = FALSE) { + path <- file.path(outputDirs, "historical.mif") + normalizePath(path, mustWork = mustWork) +} + +#' @rdname getPath +#' @export +getCfgScenPath <- function(outputDirs, mustWork = FALSE) { + path <- file.path(outputDirs, "config.Rdata") + normalizePath(path, mustWork = mustWork) +} + +#' @param remindDir A single string. The path to the remind directory. +#' @rdname getPath +#' @export +getCfgDefaultPath <- function(remindDir = ".", mustWork = FALSE) { + path <- file.path(remindDir, "config/default.cfg") + normalizePath(path, mustWork = mustWork) +} diff --git a/R/loadModeltest.R b/R/loadModeltest.R index 8b00c063..0e064154 100644 --- a/R/loadModeltest.R +++ b/R/loadModeltest.R @@ -1,6 +1,7 @@ #' @importFrom utils globalVariables globalVariables(".") + getProjectPath <- function(project = "remind") { possibleProjectLocations <- file.path(c("//clusterfs.pik-potsdam.de", "/p/projects"), project) sel <- which(file.exists(possibleProjectLocations))[1] @@ -8,8 +9,9 @@ getProjectPath <- function(project = "remind") { possibleProjectLocations[sel] } + #' @importFrom dplyr bind_cols -getNewsestModeltests <- function(namePattern = "^SSP2EU-AMT-") { +getNewsestModeltests <- function(namePattern, requireMif) { modeltestOutPath <- file.path(getProjectPath(), "modeltests/output") entries <- dir(modeltestOutPath) @@ -19,7 +21,7 @@ getNewsestModeltests <- function(namePattern = "^SSP2EU-AMT-") { x = ., pattern = "^[a-zA-Z0-9-]+_\\d{4}-\\d{2}-\\d{2}_\\d{2}\\.\\d{2}\\.\\d{2}$", value = TRUE) - newest <- + allRuns <- allRunNames %>% strsplit("_", fixed = TRUE) %>% unlist() %>% @@ -28,27 +30,17 @@ getNewsestModeltests <- function(namePattern = "^SSP2EU-AMT-") { as_tibble() %>% bind_cols(runName = allRunNames) %>% mutate(date = as.Date(.data$date)) %>% - filter(date >= max(.data$date) - 3) # day of newest run until 3 days before + mutate(path = file.path(.env$modeltestOutPath, .data$runName)) %>% + mutate(mifScen = getMifScenPath(.data$path)) selectedRuns <- - newest %>% + allRuns %>% filter(grepl(x = .data$name, pattern = .env$namePattern)) %>% - mutate(path = file.path(.env$modeltestOutPath, .data$runName)) + filter(!.env$requireMif | file.exists(.data$mifScen)) %>% + filter(date >= max(.data$date) - 3) # day of newest run until 3 days before return(selectedRuns) } -cs2InputPaths <- function(outputDirs) { - names <- lucode2::getScenNames(outputDirs) - # conditional for _adjustedPolicyCosts.mif not needed anymore since REMIND PR #881. - path <- list( - run = outputDirs, - mifScen = file.path(outputDirs, paste0("REMIND_generic_", names, ".mif")), - mifHist = file.path(outputDirs[1], "historical.mif"), - cfgScen = file.path(outputDirs, "config.Rdata"), - cfgDefault = file.path(outputDirs[1], "../../config/default.cfg") - ) - lapply(path, normalizePath) -} #' Load compareScenarios2 Data #' @@ -89,6 +81,19 @@ loadCs2Data <- function( return(invisible(NULL)) } + +cs2InputPaths <- function(outputDirs) { + path <- list( + run = outputDirs, + mifScen = getMifScenPath(outputDirs), + mifHist = getMifHistPath(outputDirs[1]), + cfgScen = getCfgScenPath(outputDirs), + cfgDefault = getCfgDefaultPath(file.path(outputDirs[1], "../..")) + ) + lapply(path, normalizePath) +} + + #' Load Modeltest Results #' #' The newest model tests are collected from the cluster and copied into a @@ -124,8 +129,16 @@ loadModeltest <- function( cat("Obtaining modeltest paths.\n") - modeltests <- getNewsestModeltests(namePattern) + modeltests <- getNewsestModeltests(namePattern, requireMif = TRUE) + if (NROW(modeltests) == 0) stop("Did not find model tests.") path <- cs2InputPaths(modeltests$path) + + if (!dir.exists(folder)) { + cat(folder, "does not exist -> creating it.\n") + dir.create(folder, recursive = TRUE) + } + folder <- normalizePath(folder, mustWork = TRUE) + tmpPath <- list( mifScen = file.path(folder, paste0(modeltests$name, ".mif")), mifHist = file.path(folder, "historical.mif"), @@ -137,7 +150,12 @@ loadModeltest <- function( "\nto\n ", paste(to, collapse = "\n "), "\n", sep = "") - file.copy(from, to, overwrite = TRUE) + success <- file.copy(from, to, overwrite = TRUE) + if (!all(success)) + warning( + "failed copying following files\n", + paste(from[!success], collapse = "\n"), + immediate. = TRUE) } copyFile(path$mifScen, tmpPath$mifScen) @@ -147,7 +165,10 @@ loadModeltest <- function( cat( "Loading cs2 data into ", - if (environmentName(envir) == "") "user specified environment", environmentName(envir), + if (environmentName(envir) == "") + "user specified environment" + else + environmentName(envir), ".\n", sep = "") loadCs2Data( diff --git a/R/reportEmi.R b/R/reportEmi.R index f62edec6..818127d5 100755 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -11,7 +11,7 @@ #' @param t temporal resolution of the reporting, default: #' t=c(seq(2005,2060,5),seq(2070,2110,10),2130,2150) #' -#' @author Felix Schreyer +#' @author Felix Schreyer... #' @examples #' \dontrun{ #' reportEmi(gdx) @@ -78,8 +78,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 teCCS <- readGDX(gdx, "teCCS") teBio <- readGDX(gdx, "teBio") teFosCCS <- readGDX(gdx, "teFosCCS") - - + # additional sets needed @@ -347,7 +346,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (!is.null(vm_demFENonEnergySector)) { EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector_woNonEn[, , emi.map.fe$name] } else { - EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector[, , emi.map.fe$name] + EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector[, , emi.map.fe$name] } @@ -2091,8 +2090,137 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 - # 8. Aggregation to global and regional values ---- + # 8. Ad-hoc fix for emissions w/o non-energy use and Aggregation to global and regional values ---- + +if (is.null(vm_demFENonEnergySector) && (module2realisation["industry", 2] == "fixed_shares")) { + # (Note: The non-energy use variables are so far only available for REMIND-EU runs and industry fixed_shares) + # TODO: add non-energy use variables for all regionmappings and sector realizations + #Note (SM): I'm not sure if I got these notes so I created the condition above to tra to make sure that this will work anyways + + # Note: Non-energy use emissions should not be confused with process emissions. Non-energy use emissions are emissions/carbon flow of FE carriers which are used as feedstocks in industry. + if ("FE|Non-energy Use|Industry (EJ/yr)" %in% getNames(output) && + "FE|Non-energy Use|Industry|+|Liquids (EJ/yr)" %in% getNames(output) && + "FE|Non-energy Use|Industry|+|Gases (EJ/yr)" %in% getNames(output) && + "FE|Non-energy Use|Industry|+|Solids (EJ/yr)" %in% getNames(output)) { + + + # calculate non-energy use emissions (= feedstock carbon content) as industry emissions before CCS per energy carrier * share of feedstocks in final energy + # take industry emissions before CCS as feedstocks cannot be used for CCS + # this is a temporary approximation in the reporting, but should eventually be adapted in REMIND by having a seperate feedstock FE of which the carbon cannot be captured + out <- mbind(out, + # liquids + setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Liquids (EJ/yr)"], + "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Liquids (Mt CO2/yr)"), + # gases + setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Gases (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Gases (EJ/yr)"], + "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Gases (Mt CO2/yr)"), + # solids + setNames(dimSums(mselect(EmiFeCarrier, all_enty1 = c("fesos"), emi_sectors = "indst"), dim = 3) * GtC_2_MtCO2 * output[getRegions(out), , "FE|Non-energy Use|Industry|+|Solids (EJ/yr)"] / output[getRegions(out), , "FE|Industry|+|Solids (EJ/yr)"], + "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Solids (Mt CO2/yr)")) + + # total non-energy use emissions + out <- mbind(out, + setNames(out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Solids (Mt CO2/yr)"] + + out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Liquids (Mt CO2/yr)"] + + out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry|Gases (Mt CO2/yr)"], + "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)")) + + + + + # calculate emissions variables w/o non-energy use + # TODO: once proper accounting of non-energy use/feedstocks has been done for all cases, consider making the standard "Emi|CO2 (Mt CO2/yr)" etc. variables + # the ones without non-energy use and add an extra set "w/ Non-energy use" as this is likely a more sensible default + + + ### reattribution of a fraction of non-energy use carbon as waste emissions (plastic products that get combusted in waste incineration plants within the region) + # set to zero for now to avoid inconsistencies with historic data sources on industry and power emissions + WasteFraction <- 0 + + out <- mbind(out, + setNames(WasteFraction * out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"], + "Emi|CO2|w/o Non-energy Use|Waste Incineration (Mt CO2/yr)")) + + ### variables for which non-energy emissions should be substracted but waste fraction added + emi.vars.wNonEn.inclWaste <- c( + # GHG emissions + "Emi|GHG (Mt CO2eq/yr)", + "Emi|GHG|+|CO2 (Mt CO2eq/yr)", + "Emi|GHG|w/o Land-Use Change (Mt CO2eq/yr)", + "Emi|GHG|++|ETS (Mt CO2eq/yr)", + + # CO2 Emissions + "Emi|CO2 (Mt CO2/yr)", + "Emi|CO2|++|ETS (Mt CO2/yr)") + + + # add emissions variables with LULUCF national accounting + if (!is.null(p47_LULUCFEmi_GrassiShift)) { + emi.vars.wNonEn.inclWaste <- c(emi.vars.wNonEn.inclWaste, + "Emi|GHG|LULUCF national accounting (Mt CO2eq/yr)", + "Emi|CO2|LULUCF national accounting (Mt CO2/yr)" ) + } + + ### variables for which non-energy emissions should be substracted (and no waste fraction added) + emi.vars.wNonEn.exclWaste <- c( + # GHG emissions + "Emi|GHG|+++|Energy (Mt CO2eq/yr)", + "Emi|GHG|Energy|+|Demand (Mt CO2eq/yr)", + "Emi|GHG|Energy|Demand|+|Industry (Mt CO2eq/yr)", + "Emi|GHG|Industry (Mt CO2eq/yr)", + "Emi|GHG|ETS|+|Industry (Mt CO2eq/yr)", + + # Gross GHG Emissions + "Emi|GHG|Gross|Energy (Mt CO2eq/yr)", + "Emi|GHG|Gross|Energy|+|Demand (Mt CO2eq/yr)", + "Emi|GHG|Gross|Energy|Demand|+|Industry (Mt CO2eq/yr)", + + # CO2 Emissions + "Emi|CO2|+|Energy (Mt CO2/yr)", + "Emi|CO2|Energy and Industrial Processes (Mt CO2/yr)", + "Emi|CO2|Energy|+|Demand (Mt CO2/yr)", + "Emi|CO2|Energy|Demand|+|Industry (Mt CO2/yr)", + "Emi|CO2|Energy|Demand|Industry|++|Chemicals (Mt CO2/yr)", + + + # Gross CO2 Emissions + "Emi|CO2|Gross|Energy|+|Demand (Mt CO2/yr)", + "Emi|CO2|Gross|Energy (Mt CO2/yr)", + "Emi|CO2|Gross|Energy and Industrial Processes (Mt CO2/yr)", + "Emi|CO2|Gross|Energy|Demand|+|Industry (Mt CO2/yr)") + + + + + # variable names, insert w/o non-energy use + emi.vars.wNonEn <- c(emi.vars.wNonEn.inclWaste, emi.vars.wNonEn.exclWaste) + names.wNonEn <- emi.vars.wNonEn + names.wNonEn <- gsub("Emi\\|CO2", "Emi|CO2|w/o Non-energy Use", names.wNonEn) + names.wNonEn <- gsub("Emi\\|GHG", "Emi|GHG|w/o Non-energy Use", names.wNonEn) + + # remove all pluses from the "Emi w/o Non-energy Use" variables as they do not cover sectors in which non-energy use not relevant and checking aggregation does not make sense + names.wNonEn <- gsub("\\|\\+\\|", "\\|", names.wNonEn) + names.wNonEn <- gsub("\\|\\++\\|", "\\|", names.wNonEn) + + # calulate emissions variables with non-energy use + out.wNonEn <- out[, , emi.vars.wNonEn] + # for aggregate emissions: substract non-energy use carbon and add waste incineration emissions + out.wNonEn[, , emi.vars.wNonEn.inclWaste] <- out.wNonEn[, , emi.vars.wNonEn.inclWaste] - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"] + out[, , "Emi|CO2|w/o Non-energy Use|Waste Incineration (Mt CO2/yr)"] + # for energy related emissions: substract non-energy use carbon + # (waste incineration emissions are treated as a separate category here outside of energy emissions to avoid attribution compliations down the energy emissions tree) + out.wNonEn[, , emi.vars.wNonEn.exclWaste] <- out.wNonEn[, , emi.vars.wNonEn.exclWaste] - out[, , "Emi|CO2|Non-energy Use|Energy|Demand|Industry (Mt CO2/yr)"] + + + # insert "w/o Non-energy Use" label in variable names + out.wNonEn <- setNames(out.wNonEn[, , emi.vars.wNonEn], names.wNonEn) + + + out <- mbind(out, out.wNonEn) + + + } +} # add global values out <- mbind(out, dimSums(out, dim = 1)) # add other region aggregations diff --git a/R/reportFE.R b/R/reportFE.R index 44b80d10..8916fc78 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1544,7 +1544,18 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)")) + "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), + # FE industry (without feedstocks) solids: from fossils, biomass + setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * + out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / + out[,,"FE|Industry|+|Solids (EJ/yr)"], + "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), + setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * + out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / + out[,,"FE|Industry|+|Solids (EJ/yr)"], + "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)")) + + tryCatch( expr = { diff --git a/R/variablesAsList.R b/R/variablesAsList.R index 3cff43cd..32e1854a 100644 --- a/R/variablesAsList.R +++ b/R/variablesAsList.R @@ -4,15 +4,19 @@ #' indicated by \code{|} in the name and convert it into a hierarchical #' structure of named lists. #' -#' @param x A character vector of variable names, a quitte object, or a path to -#' a mif file. +#' @seealso \code{\link{createVarListHtml}} for creating an HTML-file of such a list. +#' @param x A character vector of variable names, a quitte object, or a +#' character vectors of paths to mif files. #' @param entry A string determining the entries of all leafs and the nodes #' which represent variables in \code{vars}. \code{"NULL"} puts \code{NULL} #' into the leafs of the resulting list. \code{"name"} places the full name of #' an existing variable as entry \code{nm} in the respective node. #' \code{"INFO"} puts a list of further information about this node in each -#' node with existing variables, which requires \code{x} to be a mif file or a -#' quitte object. +#' node with existing variables, including \code{details} if specified. +#' @param usePlus \code{logical(1)}. If \code{FALSE}, removes \code{|+|, |++|, +#' ...} from variable names. +#' @param details \code{NULL} or a data frame with a column \code{name}. The +#' entries of further columns of \code{details} are put into the INFO nodes. #' @return A hierarchical named list. #' @author Christof Schoetz #' @examples @@ -23,94 +27,127 @@ #' \dontrun{View(v)} #' v <- variablesAsList(vars, entry = "name") #' \dontrun{mip::showLinePlots(data, v$Emi$GHG$nm)} +#' details <- data.frame(name = vars, nr = seq_along(vars)) +#' v <- variablesAsList(vars, entry = "INFO", details = details) +#' \dontrun{View(v)} #' #' \dontrun{ #' loadModeltest() #' v <- variablesAsList(data, entry = "INFO") #' View(v) #' # Include structure induced by |+|,|++|, ...: -#' vp <- variablesAsList(data$varplus, entry = "INFO") +#' vp <- variablesAsList(data, entry = "INFO", usePlus = TRUE) #' View(vp) #' } #' #' \dontrun{View(variablesAsList("path/to/scenario.mif"))} #' @export -variablesAsList <- function(x, entry = c("NULL", "name", "INFO")) { +variablesAsList <- function( + x, + entry = c("NULL", "name", "INFO"), + usePlus = FALSE, + details = NULL +) { + + stopifnot(identical(usePlus, TRUE) || identical(usePlus, FALSE)) + stopifnot(is.character(entry)) + stopifnot(is.null(details) || (is.data.frame(details)) && "name" %in% names(details)) entry <- match.arg(entry) # Read x to define data and vars. + data <- NULL + vars <- NULL if (is.factor(x)) x <- as.character(x) - if (is.character(x)) { - if (length(x) == 1 && file.exists(x)) { - data <- quitte::read.quitte(x, na.strings = c("UNDF", "NA", "N/A", "n_a", "Inf", "-Inf")) - vars <- data$variable - } else { - vars <- x - data <- NULL - } + if (is.character(x) && length(x) < 100 && all(file.exists(x))) { # x are files + data <- quitte::read.quitte(x) + } else if (is.character(x)) { + vars <- x } else { - data <- quitte::as.quitte(data) - vars <- data$variable + data <- quitte::as.quitte(x) } - - if (entry == "INFO" && is.null(data)) { - stop("For entry=\"INFO\", full data must be provided. Pass a quitte object or the path to a mif file.") + if (is.null(vars)) { + if ("varplus" %in% names(data)) + vars <- data$varplus + else if ("variable" %in% names(data)) + vars <- data$variable + else + stop("Object data does not contain a variable column.") + } + if (!usePlus) { + vars <- deletePlus(vars) } + vars <- as.character(vars) + stopifnot(is.character(vars)) + stopifnot(is.null(data) || quitte::is.quitte(data)) + # Create a data frame summary from vars and data containing all relevant info for further processing. if (entry == "INFO") { message("This might take a minute or two...") - } - # convert factors to character vectors - if (!is.null(data)) { - data$model <- as.character(data$model) - data$scenario <- as.character(data$scenario) - data$region <- as.character(data$region) - data$unit <- as.character(data$unit) + if (is.null(data)) { + df <- data.frame(name = vars) + } else { + df <- data + df$name <- vars + df$variable <- NULL + df$varplus <- NULL + df$value <- NULL + df$period <- NULL + } + counts <- dplyr::count(df, .data$name) + df <- distinct(df) + summary <- + df %>% + group_by(.data$name) %>% + summarize(across(everything(), ~ list(as.character(unique(.x))))) %>% + left_join(counts, by = "name") + if (!is.null(details)) { + summary <- left_join(summary, details, by = "name") + } + } else { + summary <- data.frame(name = vars) } - varsTable <- table(vars) - uniqueVars <- names(varsTable) - splitList <- strsplit(uniqueVars, "|", fixed = TRUE) + splitList <- strsplit(summary$name, "|", fixed = TRUE) maxLen <- max(vapply(splitList, length, integer(1))) varsMatrix <- t(vapply(splitList, function(x) x[seq_len(maxLen)], character(maxLen))) - # Function to recursively parse variable names and create node value. - .splitMatrixAsList <- function(mat, prefix) { - - # Set the value of the node. - if (prefix %in% uniqueVars) { - nodeValue <- switch( - entry, - "NULL" = NULL, - "name" = list(nm = prefix), - "INFO" = { - sel <- data$variable == prefix - list(INFO = list( - nm = prefix, - count = unname(varsTable[prefix]), - model = unique(data$model[sel]), - scenario = unique(data$scenario[sel]), - region = unique(data$region[sel]), - unit = unique(data$unit[sel]))) - }) - } else { - nodeValue <- NULL - } + res <- .splitMatrixAsList(varsMatrix, prefix = "", entry = entry, summary = summary) + return(res) +} - # Termination condition. - if (length(mat) == 0 || all(is.na(mat))) return(nodeValue) - # Process subcategories. - lst <- split(mat[, -1, drop = FALSE], mat[, 1]) - lst <- lapply(lst, matrix, ncol = NCOL(mat) - 1) - newPrefixes <- paste0(prefix, if (nchar(prefix) > 0) "|", names(lst)) - resSubCategories <- Map(.splitMatrixAsList, lst, newPrefixes) +# Function to recursively parse variable names and create node value. +.splitMatrixAsList <- function(mat, prefix, entry, summary) { - return(c(nodeValue, resSubCategories)) + # Set the value of the node. + if (prefix %in% summary$name) { + nodeValue <- switch( + entry, + "NULL" = NULL, + "name" = list(nm = prefix), + "INFO" = { + info <- filter(summary, .data$name == .env$prefix) # Exactly one row. + info <- lapply(info, function(x) if (is.list(x) && length(x) == 1) x[[1]] else x) + names(info) <- names(summary) + list(INFO = info) + } + ) + } else { + nodeValue <- NULL } - res <- .splitMatrixAsList(varsMatrix, prefix = "") - return(res) + # Termination condition. + if (length(mat) == 0 || all(is.na(mat))) return(nodeValue) + + # Process subcategories. + lst <- split(mat[, -1, drop = FALSE], mat[, 1]) + lst <- lapply(lst, matrix, ncol = NCOL(mat) - 1) + newPrefixes <- paste0(prefix, if (nchar(prefix) > 0) "|", names(lst)) + resSubCategories <- Map( + \(x, y) .splitMatrixAsList(x, y, entry = entry, summary = summary), + lst, newPrefixes) + + return(c(nodeValue, resSubCategories)) } diff --git a/README.md b/README.md index f8b8b26e..1c0b08f5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # The REMIND R package (2nd generation) -R package **remind2**, version **1.99.4** +R package **remind2**, version **1.101.1** [![CRAN status](https://www.r-pkg.org/badges/version/remind2)](https://cran.r-project.org/package=remind2) [![R build status](https://github.com/pik-piam/remind2/workflows/check/badge.svg)](https://github.com/pik-piam/remind2/actions) [![codecov](https://codecov.io/gh/pik-piam/remind2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/remind2) [![r-universe](https://pik-piam.r-universe.dev/badges/remind2)](https://pik-piam.r-universe.dev/ui#builds) @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues . +Rodrigues R (2022). _remind2: The REMIND R package (2nd generation)_. R package version 1.101.1, . A BibTeX entry for LaTeX users is @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is title = {remind2: The REMIND R package (2nd generation)}, author = {Renato Rodrigues}, year = {2022}, - note = {R package version 1.99.4}, + note = {R package version 1.101.1}, url = {https://github.com/pik-piam/remind2}, } ``` diff --git a/inst/extdata/variablesAsListTemplate.html b/inst/extdata/variablesAsListTemplate.html new file mode 100644 index 00000000..5d819f2b --- /dev/null +++ b/inst/extdata/variablesAsListTemplate.html @@ -0,0 +1,20 @@ + + + + +

[:TITLE:]

+[:BEFORE:] +[:CONTENT:] + + diff --git a/inst/markdown/compareScenarios2/cs2_main.Rmd b/inst/markdown/compareScenarios2/cs2_main.Rmd index 148bf4cc..abcf1822 100644 --- a/inst/markdown/compareScenarios2/cs2_main.Rmd +++ b/inst/markdown/compareScenarios2/cs2_main.Rmd @@ -82,10 +82,10 @@ if (interactive()) { # may cause "Undefined control sequence" errors in LaTeX. try(unloadNamespace("kableExtra"), silent = TRUE) -library(gridExtra) # nolint +suppressMessages(library(gridExtra)) # nolint options(tidyverse.quiet = TRUE) # nolint library(tidyverse) # nolint -library(kableExtra) # nolint +suppressMessages(library(kableExtra)) # nolint library(quitte) # nolint library(mip) # nolint library(remind2) # nolint @@ -131,14 +131,10 @@ if (!is.null(params$cfgDefault)) { dataScenNested <- tibble(path = unname(params$mifScen)) %>% rowid_to_column("fileid") %>% - mutate(data = map( - path, - read.quitte, - factors = TRUE, - # read.quitte() default NA-strings and Inf, -Inf - na.strings = c("UNDF", "NA", "N/A", "n_a", "Inf", "-Inf"))) %>% + mutate(data = map(path, read.quitte)) %>% unnest(data) %>% nest(data = -c(fileid, path, scenario)) + # Add column character column "newScenarioName", # either with contents of params$mifScenNames or copy names from column scenario. if (is.null(params$mifScenNames)) { @@ -154,6 +150,7 @@ if (is.null(params$mifScenNames)) { newScenarioName = params$mifScenNames), by = "fileid") } + # Check for duplicated scenario names. if (anyDuplicated(dataScenNested$newScenarioName)) { warning("There are duplicated scenario names. They will be renamed.") @@ -161,6 +158,7 @@ if (anyDuplicated(dataScenNested$newScenarioName)) { dataScenNested %>% mutate(newScenarioName = make.unique(newScenarioName)) } + # Retrieve data for reference table to be shown at the beginning of the document. fileReference <- dataScenNested %>% @@ -203,9 +201,8 @@ dataHist <- filter(period %in% params$yearsHist, !(model %in% params$modelsHistExclude), !is.na(value)) %>% droplevels() -# Combine into one data frame and remove old. +# Combine into one data frame. data <- bind_rows(dataScen, dataHist) -rm(dataScen, dataScenNested, dataHist) data <- data %>% @@ -422,6 +419,20 @@ if (length(params$sections) == 1 && params$sections == "all") { } ``` +```{r remove objects not to be used anymore} +varNames <- c( + "availableSections", "cfgTopLevel", "dataGDP", "dataPCap", + "dataPGdp", "dataPop", "dataScenNested", "env", "envToolsRstudio", + "fn", "histRefModel", "insertExprAtStartOfFun", "lightenColor", + "loadCfg", "matches", "oldRsSetNotebookGraphicsOption", "pCapVariables", + "pGdpVariables") +for (vn in varNames) if (exists(vn)) rm(list = vn) +rm(varNames) +rm(vn) +gc() +``` + + ```{r prepare mark} # CLICK "RUN ALL CHUNKS ABOVE" HERE TO PREPARE THE ENVIRONMENT diff --git a/man/createVarListHtml.Rd b/man/createVarListHtml.Rd new file mode 100644 index 00000000..a85fed26 --- /dev/null +++ b/man/createVarListHtml.Rd @@ -0,0 +1,61 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/createVarListHtml.R +\name{createVarListHtml} +\alias{createVarListHtml} +\title{Create an HTML Document of a Hierarchical List of Variables} +\usage{ +createVarListHtml( + x, + outFileName, + title = "List of Variables", + htmlBefore = "", + usePlus = FALSE, + details = NULL +) +} +\arguments{ +\item{x}{A character vector of variable names, a quitte object, or a +character vectors of paths to mif files.} + +\item{outFileName}{A single string. The path of the output file, preferably +ending in \code{.html}} + +\item{title}{The title displayed at the top of the created HTML.} + +\item{htmlBefore}{\code{character(1)}. HTML to be put between title and list of variables.} + +\item{usePlus}{\code{logical(1)}. If \code{FALSE}, removes \code{|+|, |++|, +...} from variable names.} + +\item{details}{\code{NULL} or a data frame with a column \code{name}. The +entries of further columns of \code{details} are put into the INFO nodes.} +} +\description{ +Creates a hierarchical list from mif data using \code{\link{variablesAsList}} +and writes it as an HTML document that displays the hierarchy via the +\code{
} HTML5-tag. +} +\examples{ +\dontrun{ +loadModeltest() +createVarListHtml(data, "variables.html") +detailsAR6 <- + readr::read_delim( + paste0( + "https://raw.githubusercontent.com/pik-piam/", + "project_interfaces/master/ar6/mapping_template_AR6.csv"), + delim = ";", + col_select = c(r21m42, Definition) + ) \%>\% + rename(name = r21m42) +createVarListHtml( + data, + "variablesWithDescription.html", + title = "Reported REMIND Variables with AR6 Description", + usePlus = TRUE, + details = detailsAR6) +} +} +\seealso{ +\code{\link{variablesAsList}} +} diff --git a/man/getPath.Rd b/man/getPath.Rd new file mode 100644 index 00000000..b8a6d4db --- /dev/null +++ b/man/getPath.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/getPath.R +\name{getMifScenPath} +\alias{getMifScenPath} +\alias{getMifHistPath} +\alias{getCfgScenPath} +\alias{getCfgDefaultPath} +\title{Get Paths to Certain Files in the REMIND Directory} +\usage{ +getMifScenPath(outputDirs, mustWork = FALSE) + +getMifHistPath(outputDirs, mustWork = FALSE) + +getCfgScenPath(outputDirs, mustWork = FALSE) + +getCfgDefaultPath(remindDir = ".", mustWork = FALSE) +} +\arguments{ +\item{outputDirs}{A character vector of paths to output folders of REMIND runs.} + +\item{mustWork}{\code{logical(1)}. Throw error if file not available?} + +\item{remindDir}{A single string. The path to the remind directory.} +} +\description{ +\code{getMifScenPath}: get path to the scenarios' reporting mifs. +\code{getMifHistPath}: get path to the scenarios' historical.mif. +\code{getCfgScenPath}: get path to the scenarios' config.Rdata. +\code{getCfgDefaultPath}: get path to REMIND's default.cfg. +} diff --git a/man/variablesAsList.Rd b/man/variablesAsList.Rd index 019d216a..9e5b4a99 100644 --- a/man/variablesAsList.Rd +++ b/man/variablesAsList.Rd @@ -4,19 +4,29 @@ \alias{variablesAsList} \title{Variable Names as Hierarchical List} \usage{ -variablesAsList(x, entry = c("NULL", "name", "INFO")) +variablesAsList( + x, + entry = c("NULL", "name", "INFO"), + usePlus = FALSE, + details = NULL +) } \arguments{ -\item{x}{A character vector of variable names, a quitte object, or a path to -a mif file.} +\item{x}{A character vector of variable names, a quitte object, or a +character vectors of paths to mif files.} \item{entry}{A string determining the entries of all leafs and the nodes which represent variables in \code{vars}. \code{"NULL"} puts \code{NULL} into the leafs of the resulting list. \code{"name"} places the full name of an existing variable as entry \code{nm} in the respective node. \code{"INFO"} puts a list of further information about this node in each -node with existing variables, which requires \code{x} to be a mif file or a -quitte object.} +node with existing variables, including \code{details} if specified.} + +\item{usePlus}{\code{logical(1)}. If \code{FALSE}, removes \code{|+|, |++|, +...} from variable names.} + +\item{details}{\code{NULL} or a data frame with a column \code{name}. The +entries of further columns of \code{details} are put into the INFO nodes.} } \value{ A hierarchical named list. @@ -34,18 +44,24 @@ v <- variablesAsList(vars) \dontrun{View(v)} v <- variablesAsList(vars, entry = "name") \dontrun{mip::showLinePlots(data, v$Emi$GHG$nm)} +details <- data.frame(name = vars, nr = seq_along(vars)) +v <- variablesAsList(vars, entry = "INFO", details = details) +\dontrun{View(v)} \dontrun{ loadModeltest() v <- variablesAsList(data, entry = "INFO") View(v) # Include structure induced by |+|,|++|, ...: -vp <- variablesAsList(data$varplus, entry = "INFO") +vp <- variablesAsList(data, entry = "INFO", usePlus = TRUE) View(vp) } \dontrun{View(variablesAsList("path/to/scenario.mif"))} } +\seealso{ +\code{\link{createVarListHtml}} for creating an HTML-file of such a list. +} \author{ Christof Schoetz } diff --git a/vignettes/compareScenarios2.Rmd b/vignettes/compareScenarios2.Rmd index 5545e794..9479abd5 100644 --- a/vignettes/compareScenarios2.Rmd +++ b/vignettes/compareScenarios2.Rmd @@ -16,26 +16,49 @@ vignette: > knitr::opts_chunk$set( collapse = TRUE, comment = "#>", - eval=FALSE + eval = FALSE ) ``` +# Overview -The function `compareScenarios2()` of the `remind2` package can be used to visually compare the results of multiple runs of the IAM [REMIND](https://github.com/remindmodel/remind) +The function `compareScenarios2()` (*cs2* for short) of the `remind2` package can be used to visually compare the results of multiple runs of the IAM [REMIND](https://github.com/remindmodel/remind) -It can be called via the [output.R](https://github.com/remindmodel/remind/blob/develop/output.R) script in the [REMIND repository](https://github.com/remindmodel/remind): Execute `Rscript output.R` in the REMIND folder, select `Comparison across runs` and then `compareScenarios2`. +It can be called via the [`remind/output.R`](https://github.com/remindmodel/remind/blob/develop/output.R) script: Execute `Rscript output.R` in the REMIND folder, select `Comparison across runs` and then `compareScenarios2`. -The function reads the results from the output-mif-files (in the remind folder after runs are completed, i.e., the files `output//REMIND_generic_.mif`). Additionally it reads historical data from a `historical.mif` in one of the scenario output folders. Using this data, a document containing many plots is created and written as a PDF- or HTML-file. See `?remind2::compareScenarios2` for information on how to call the function with the appropriate arguments. +The function reads the results from the output-mif-files (in the remind folder after runs are completed, i.e., the files `./output//REMIND_generic_.mif`). Additionally it reads historical data from a `historical.mif` in one of the run output folders. Using this data, a document containing many plots is created and written as a PDF- or HTML-file. See `?remind2::compareScenarios2` for information on how to call the function directly with the appropriate arguments. -Internally, `compareScenarios2()` calls `rmarkdown::render()` on certain Rmarkdown-files (Rmd-files). Rmd-files may contain R-code (e.g., for creating plots) as well as descriptive text (in this case, mostly section titles). The Rmd-files to be rendered are part of the remind2-package. In the development state of the package, they can be found in the folder `inst/markdown/compareScenarios2/`. +Internally, `compareScenarios2()` calls `rmarkdown::render()` on certain Rmarkdown-files (Rmd-files). Rmd-files may contain R-code (e.g., for creating plots, creating tables) as well as descriptive text (in this case, mostly section titles). The main Rmd-files to be rendered are part of the remind2-package. Some optional section may be placed in the REMIND repository at [`remind/scripts/cs2/`](https://github.com/remindmodel/remind/tree/develop/scripts/cs2). In the development state of the package, the main Rmd-files can be found in the folder [`remind2/inst/markdown/compareScenarios2/`](https://github.com/pik-piam/remind2/tree/master/inst/markdown/compareScenarios2). To be more precises, `rmarkdown::render()` is called on `cs2_main.Rmd` in this folder, which includes other Rmd-files - one for each section of the output document. The loading and preprocessing of data happens in `cs2_main.Rmd`; the section Rmd-files mostly call plot functions. Aside from HTML- and PDF-documents as output, `compareScenarios2()` also allows to obtain a copy of the Rmd-files needed to create these outputs (by setting the argument `outputFormat = "Rmd"`). Rendering the resulting Rmd-files to PDF or HTML yields the same documents as calls to `compareScenarios2()` with `outputFormat = "PDF"` or `"HTML"`. The advantage of being able to access these Rmd-files is the possibility to change the plotting code without changing the code of the `remind2`-package. Moreover, Rmd-files can be used interactively in RStudio. -# Local Use and mif-Files +# Usage -`compareScenarios2()` with default settings can be called automatically after a bundle of runs on the cluster has finished or manually via `Rscript output.R`. It can also be called directly as a function of the R-package `remind2`, e.g., +`compareScenarios2()` with different predefined settings can be called via `Rscript output.R`. It can also be called directly as a function of the R-package `remind2`. + +## output.R and Profiles + +### output.R + +Typically we want to compare the results of some REMIND runs which were created in the `remind/output/` folder of a clone of the REMIND repository. Assume such runs are available and, in particular, the `remind/output//REMIND_generic_.mif` of the runs of interest exist. Go to the REMIND directory `remind/` and call `Rscript output.R`. Then choose `Comparison across runs` and `compareScenarios2`. Select the runs of interest. We then can choose a filename prefix for the output file if we want. Next, choose a slurm mode, e.g., `short` (the job will run for less than 1 hour, usually something like 20min). Finally we can choose a cs2-profile. Standard profiles for the selected runs are colored. After choosing one or more profiles, the respective slurm jobs will be started. An HTML- or PDF-file will be created. If something goes wrong, take a look at the `*.log`-file with the same name as the cs2-output file. + +### Profiles + +A cs2-profile is a set of arguments for `compareScenarios2()`. There are different profiles for a comparison on the 12 main REMIND regions (`H12`, `H12-short`), or on the European regions (`EU27`, `EU27-short`) for 21-region-runs. The suffix `-short` indicates that only periods up to 2050 are considered. The `default`-profiles just call `compareScenarios2()` with the default arguments as described in `?remind2::compareScenarios2`. +The available profiles are stored in [`remind/scripts/cs2/profiles.json`](https://github.com/remindmodel/remind/blob/develop/scripts/cs2/profiles.json). Take a look at this file (open in text editor) to see the definition of each profile. + +### Changing or Adding Profiles + +To change a profile or add a new profile, just edit [`remind/scripts/cs2/profiles.json`](https://github.com/remindmodel/remind/blob/develop/scripts/cs2/profiles.json). Read the `_DESCRIPTION_` in that file. Every argument (including YAML-arguments) to `compareScenarios2()` can be set or changed. + +We can create our own cs2-report: +First we create an "section"-Rmd-file, similar to `cs2_NN_XXXX.Rmd` in [`remind2/inst/markdown/compareScenarios2`](https://github.com/pik-piam/remind2/tree/master/inst/markdown/compareScenarios2). It typically contains calls to plot-function using an R-object called `data`. This objects stores all the information read from the mif-files. We place our "section"-Rmd-file in [`remind/scripts/cs2/`](https://github.com/remindmodel/remind/tree/develop/scripts/cs2). Then we add a new profile in [`remind/scripts/cs2/profiles.json`](https://github.com/remindmodel/remind/blob/develop/scripts/cs2/profiles.json) with options `"sections": "0"` and `"userSectionPath": "normalizePath('./scripts/cs2/ourSection.Rmd')"`. The new profile should appear as a choice in the `output.R`-dialog. Choosing it will create a cs2-report consisiting only of the Info-section and our own section. See the profile `mySection` and [`remind/scripts/cs2/mySection.Rmd`](https://github.com/remindmodel/remind/blob/develop/scripts/cs2/mySection.Rmd) for an example. + +## Direct Call and mif-Files + +A simple direct call of `compareScenarios2()` may look like this: ```{r} library(remind2) @@ -45,54 +68,23 @@ compareScenarios2( outputFile = "CompareScenarios2Example") ``` -To do this locally on our computer, we need some mif files to compare. One possibility is to adapt and execute the following script to download the newest *modeltest* files. - -```{r eval=FALSE} -library(tidyverse) -library(ssh) - -# TODO: Adapt the following three lines! -keyfile <- "path/to/ssh/private/key/.ssh/id_rsa" -username <- "username" -out_path <- "path/to/where/mifs/will/be/downloaded/" - -# path on the cluster where to search for mifs: -path <- "/p/projects/remind/modeltests/output/" - -# Connect to the cluster and get the file names of the newest modeltest mifs. -session <- ssh_connect(paste0(username, "@cluster.pik-potsdam.de"), keyfile=keyfile) -out <- ssh_exec_internal(session, paste("ls", path)) -out$stdout %>% - rawToChar() %>% - str_split("\n", simplify = TRUE) %>% - str_subset("^[a-zA-Z0-9-]+_\\d{4}-\\d{2}-\\d{2}_\\d{2}\\.\\d{2}\\.\\d{2}$") -> - filenames -filenames %>% - str_split("_", simplify = TRUE) %>% - matrix(ncol=3, dimnames=list(NULL, c("name", "date", "time"))) %>% - as_tibble() %>% - bind_cols(filename = filenames) %>% - mutate(date = as.Date(date)) %>% - filter(date >= max(date)-2) -> - new_file_data - -new_files <- paste0(path, new_file_data$filename, "/REMIND_generic_", new_file_data$name, ".mif") -walk(new_files, scp_download, session = session, to = out_path) # download scenario mifs - -hist_path <- paste0(path, new_file_data$filename[1], "/historical.mif") -scp_download(session = session, hist_path, to = out_path) # download historical.mif - -ssh_disconnect(session) -``` +For further examples and a description of further optional arguments to the function, see `?remind2::compareScenarios2`. + +It is possible to use paths to the cluster if called at PIK in the local network (e.g., `/p/tmp/username/...`) or in VPN (e.g., `//clusterfs.pik-potsdam.de/tmp/username/...`). # Interactive Use of the Rmd-files -In this section, we assume that we do not want to change the package `remind2`. But we may want to make changes to the compareScenarios output locally. + +In this section, we want to execute individual R-code chunks in the cs2-Rmd-files interactively in RStudio. + +First we need access to the Rmd-files. Here we essentially have two options. + +## Using outputFormat = "Rmd" We call `remind2::compareScenarios2()` with `outputFormat = "Rmd"` to obtain a (modified) copy of the Rmd-files. -```{r eval=FALSE} +```{r} library(remind2) compareScenarios2( mifScen = c("path/to/scen1.mif", "path/to/scen2.mif", "path/to/scen3.mif"), # TODO. @@ -100,7 +92,7 @@ compareScenarios2( outputDir = "path/to/where/rmds/should/be/copied/to", # TODO. outputFormat = "Rmd", # Add current time as output name to not overwrite other things: - outputFile = format(Sys.time(), "CompScen2-%Y%m%d-%H%M%S")) + outputFile = format(Sys.time(), "CompScen2-%Y%m%d-%H%M%S")) ``` The code in these Rmd-files is structured in chunks and each chunk can be run separately by clicking on its play-button. We first open `cs2_main.Rmd`. At the end of the file, there is a chunk @@ -109,47 +101,84 @@ The code in these Rmd-files is structured in chunks and each chunk can be run se # CLICK "RUN ALL CHUNKS ABOVE" HERE TO PREPARE THE ENVIRONMENT ``` ```` -Clicking on the *triangle above bar*-button on the right executes all chunks above and by that loads and prepares the data for plotting. After that one can open any section-Rmd-file and execute a chunk to create the respective plots. The plots should appear in RStudio inside the Rmd-file below the chunk. +Clicking on the *triangle above bar*-button on the right executes all chunks above and by that loads and prepares the data for plotting. After that we can open any section-Rmd-file and execute a chunk to create the respective plots. The plots should appear in RStudio inside the Rmd-file below the chunk. + +## Using a cloned remind2 repository + +Clone the [remind2 repository](https://github.com/pik-piam/remind2). Open a section-Rmd-file `cs2_NN_XXXX.Rmd` in RStudio. + +Now we want to load the `data` object so that the code-chunks can be executed. If we have VPN or are in the local network at PIK, we can load the data from the latest automated model tests (AMT) via `remind2::loadModeltest()`. If this is not possible or we want other mifs to be loaded, we can use `remind2::loadCs2Data()` to create the `data` object. See the help pages (`?`) of these functions for more information. Now the chunks in the Rmd-files should be executable. + + +# Viewing Available Variables in mif Files + +The variables in the reported mif-files (`remind/output//REMIND_generic_.mif`) have a hierarchical structure induced by the character `|`. This structure can be viewed in a hierarchical list. -# Workflow for Adding a new Plot to CompareScenarios2 +## output.R + +In a remind folder with finished runs (with mif-files): + +1. execute `Rscript output.R` +2. choose `Comparison across runs` +3. choose `varListHtml` +4. choose the runs you want to read the mif files from +5. choose a file name prefix if you like + +Then the creation of the a HTML-file containing the hierarchical list of variables names in the chosen mif files is started. Note that it does not use slurm at the moment. + +## Package Function + +See `?remind2::variablesAsList`. + +If connected to the PIK network (possibly via VPN) execute the following code to get an overview of the variables available in the automated model test mifs. + +```{r} +library(remind2) +loadModeltest() +varList <- variablesAsList(data, entry = "INFO") +View(varList) # in RStudio +``` + +See also `?createVarListHtml` to create an HTML-file containing this info. + + + +# Workflow for Adding a New Plot to CompareScenarios2 1. If not done already, fork . 2. In your personal github remind2 repository, create a new branch. 3. Clone the repository and switch to the newly created branch to get your local copy. 4. In your local copy, open `remind2.Rproj` in RStudio. 5. Press `CTRL+SHIFT+L` to call `devtools::load_all(".")`, which loads `remind2` from your local copy. -6. Assuming you have some mifs available for testing, call +6. If it is sufficient to test your new plots on the latest AMTs and you are connected to the PIK network (possibly via VPN), call `loadModeltest(folder = "some/folder")`. Otherwise call an adapted form of -```{r eval=FALSE} -compareScenarios2( - mifScen = c("path/to/scen1.mif", "path/to/scen2.mif"), # TODO. - mifHist = "path/to/historical.mif", # TODO. - outputFile = format(Sys.time(), "CompScen2-%Y%m%d-%H%M%S"), - sections = NULL, - envir = globalenv()) +```{r} +loadCs2Data( + c("path/to/scen1.mif", "path/to/scen2.mif"), # TODO. + "path/to/historical.mif") # TODO. ``` -This executes the code in `cs2_main.Rmd` without creating any plots (a mostly empty PDF-file will still be created). This will take some seconds, but it should not take minutes. Type `data` into the RStudio console and hit enter. This should show you the first rows of a table that contains all the loaded data. +This might take some time (up to 1min). After this, an R-object `data` should be available. Test this by typing `data` into the RStudio Console. The first rows of a *tibble* containing some REMIND data should appear. If a bunch of R-code appears, something went wrong. -7. Open the section file in which you want to add the plot. The files are in the folder `inst/markdown/compareScenarios2/` and have names of the form `c2_NN_XXXX.Rmd`, where `NN` is a two digit number and `XXXX` resembles the section title. +7. Open the section-Rmd-file in which you want to add the plot. The files are in the folder `inst/markdown/compareScenarios2/` and have names of the form `cs2_NN_XXXX.Rmd`, where `NN` is a two digit number and `XXXX` resembles the section title. 8. You should be able to execute any chunk in the section Rmd by clicking the play button in its top right corner. -9. Insert a new chunk by copying an old one or by pressing `CTRL+ALT+I`. +9. Insert a new chunk by copying an old one or by pressing `CTRL+ALT+I`. Note: It is better to not assign names to the chunks as cs2 will crash if you used the same name twice. 10. Add a new plot inside the chunk, e.g., `showLinePlots(data, "Your|Variable|Name")`. See [Plot Functions] below. 11. Run your newly created chunk to see the plot. -12. Run the following code to see your new plot in a PDF. +12. Run the following code to see your new plot in a PDF. If you used `loadModeltest()` in 6., mif-files should be available in `some/folder/`. -```{r eval=FALSE} +```{r} compareScenarios2( mifScen = c("path/to/scen1.mif", "path/to/scen2.mif"), # TODO. mifHist = "path/to/historical.mif", # TODO. - outputFile = format(Sys.time(), "CompScen2-%Y%m%d-%H%M%S"), - outputFormat = "PDF", # PDF is the default, so you can leave that line out. Or change it to html to get a html-version. - sections = ) # TODO: Replace by the number of the section you changed (or by "all" to build the whole compare scenarios file, which will take a while) + outputFile = format(Sys.time(), "cs2-%Y%m%d-%H%M%S"), + sections = NN) # TODO: Replace NN by the number of the section you changed +# (or by "all" to build the whole compare scenarios file, which will take a while). ``` -13. Save the file. Commit your changes. In the commit message, you may use the shorthand `cs2` to indicate that you made a change to compareScenarios2, e.g., `cs2: added plot of My|Variable`. -14. Make sure you fetched and pulled all recent changes to remind2. Then, call `lucode2::buildLibrary()`. Commit again with the commit message `` `lucode2::buildLibrary()` ``. Push your commits to github. -15. Create a pull request. +13. Commit your changes. In the commit message, you may use the shorthand `cs2` to indicate that you made a change to compareScenarios2, e.g., `cs2: added plot of My|Variable`. +14. Make sure you pulled all recent changes to remind2. Then, call `lucode2::buildLibrary()` to check the code and increase the version number of the package. Commit again with the commit message `` `lucode2::buildLibrary()` ``. Push your commits to github. +15. Create a pull request of your branch to the `master` branch of the `pik-piam/remind2` repository. # cs2_main.Rmd @@ -169,7 +198,7 @@ If provided, also config files are loaded. * Scenarios are renamed if the user specifies new names (using a named vector for the argument `mifScen` of `compareScenarios2()` or by setting the parameter `mifScenNames` in the Rmd-files) or if scenario names are duplicate. * The columns `period` (years) and `region` are filtered according to the parameters `yearsScen`, `yearsHist`, and `reg`. -* `|+|, |++|, |+++|, ...` are removed from variable names. An additional column `varplus` is added to the data frame, which retains the original name including `+`. This column is used by `mip::showAreaAndBarPlotsPlus()`. +* `|+|, |++|, |+++|, ...` are removed from variable names. See also `remind2::deletePlus`. An additional column `varplus` is added to the data frame, which retains the original name including `+`. This column is used by `mip::showAreaAndBarPlotsPlus()`. * For a specified list of variables, a new per-capita-variable is created with the name `" pCap"`. * For a specified list of variables, a new per-GDP-variable is created with the name `" pGDP"`. As the denominator, the value of `GDP|PPP pCap` is used. @@ -196,7 +225,7 @@ The user can provide one or more additional Rmd-files that are appended after th There is a first section (section number 0) and a last section in the files `cs2_00_info.Rmd` and `cs2_99_further_info.Rmd`, respectively. -### 0 Info +### 0 Info {-} This section contains a file reference table with scenario names and the paths to the respective mif files. It also show if a scenario is renamed. Renaming can be forced by the user by using a named character vector as argument `mifScen` where the names indicate the new scenario name, e.g., @@ -211,7 +240,7 @@ Or, if two scenarios have the same name, they are renamed automatically. Furthermore, this section displays a description of each scenario. This requires the YAML-parameter `cfgScen` to be set (see also [99 Further Info]). The description can be set in the `secenario_config*.csv`-file via a column `description`. -### 99 Further Info +### 99 Further Info {-} If `compareScenarios2()` is provided with paths to the `config.Rdata` files of the scenarios via the YAML-parameter `cfgScen` and the path to `default.cfg` ([this](https://github.com/remindmodel/remind/tree/develop/config/default.cfg) file) via `cfgDefault` an analysis of the configurations of the scenarios is shown. @@ -224,7 +253,9 @@ compareScenarios2( ``` -# Plot Functions +# Content Functions + +## Plot Functions The section Rmd-files mostly consist of simple calls of a `show...()`-function from the the [mip](https://github.com/pik-piam/mip) package. These functions are designed to make the creation of re-occurring kinds of plots in comapreSceanrios2 simple. Alternatively, one can build custom plots using `ggplot2`. @@ -243,3 +274,7 @@ All but `showLinePlotsWithTarget()` can use the global option `mip.mainReg`. `showMultiLinePlotsByVariable()` uses `mip.histRefModel`. See the respective function documentation for more information and example plots. + +## Table Functions + +For creating tables in cs2, see the documentation of `remind2::calcTimeSeriesStats()` and `remind2::showStatsTable()` and -- for an example -- the section **Tables of Significant Periods** in [`remind2/inst/markdown/compareScenarios2/cs2_01_summary.Rmd`](https://github.com/pik-piam/remind2/blob/master/inst/markdown/compareScenarios2/cs2_01_summary.Rmd). From 608eb2057d251d243fd4f972140f081b27c7aa6d Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 1 Sep 2022 16:17:40 +0200 Subject: [PATCH 15/31] unfinished fix in reportFE.R for compatibility --- R/reportFE.R | 74 ++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 8916fc78..7c5f70ae 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1517,43 +1517,43 @@ reportFE <- function(gdx, regionSubsetList = NULL, ) - # energy carrier split in FE energy use variables - out <- mbind(out, - # FE industry (without feedstocks) liquids: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), - # FE industry (without feedstocks) gases: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), - # FE industry (without feedstocks) solids: from fossils, biomass - setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)")) + # energy carrier split in FE energy use variables + # out <- mbind(out, + # # FE industry (without feedstocks) liquids: from fossils, biomass, hydrogen + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * + # out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / + # out[,,"FE|Industry|+|Liquids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * + # out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / + # out[,,"FE|Industry|+|Liquids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * + # out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / + # out[,,"FE|Industry|+|Liquids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), + # # FE industry (without feedstocks) gases: from fossils, biomass, hydrogen + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * + # out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / + # out[,,"FE|Industry|+|Gases (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * + # out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / + # out[,,"FE|Industry|+|Gases (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * + # out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / + # out[,,"FE|Industry|+|Gases (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), + # # FE industry (without feedstocks) solids: from fossils, biomass + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * + # out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / + # out[,,"FE|Industry|+|Solids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), + # setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * + # out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / + # out[,,"FE|Industry|+|Solids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)")) From 363d7e6192acce1707e427a4b1eaa5e99592a2a5 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Wed, 9 Nov 2022 11:34:24 +0100 Subject: [PATCH 16/31] fix compatibility in reportFE.R for feedstocks implementation --- R/reportFE.R | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 7c5f70ae..3549b84f 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1441,23 +1441,23 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(dimSums(vm_demFeSector_woNonEn[,,"sesofos"],dim=3), "FE|w/o Non-energy Use|Solids|+|Fossil (EJ/yr)") ) - out <- mbind(out, - setNames(out[,,"FE (EJ/yr)"] - - out[,,"FE|Transport|Bunkers (EJ/yr)"] - - out[,,"FE|Non-energy Use|+|Industry (EJ/yr)"], - "FE|w/o Non-energy Use w/o Bunkers (EJ/yr)"), - setNames(out[,,"FE|++|Industry (EJ/yr)"] - - out[,,"FE|Non-energy Use|+|Industry (EJ/yr)"], - "FE|w/o Non-energy Use|Industry (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Liquids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Gases (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"), - setNames(out[,,"FE|Industry|+|Solids (EJ/yr)"] - - out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids (EJ/yr)") ) + # out <- mbind(out, + # setNames(out[,,"FE (EJ/yr)"] + # - out[,,"FE|Transport|Bunkers (EJ/yr)"] + # - out[,,"FE|Non-energy Use|+|Industry (EJ/yr)"], + # "FE|w/o Non-energy Use w/o Bunkers (EJ/yr)"), + # setNames(out[,,"FE|++|Industry (EJ/yr)"] + # - out[,,"FE|Non-energy Use|+|Industry (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry (EJ/yr)"), + # setNames(out[,,"FE|Industry|+|Liquids (EJ/yr)"] + # - out[,,"FE|Non-energy Use|Industry|+|Liquids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"), + # setNames(out[,,"FE|Industry|+|Gases (EJ/yr)"] + # - out[,,"FE|Non-energy Use|Industry|+|Gases (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"), + # setNames(out[,,"FE|Industry|+|Solids (EJ/yr)"] + # - out[,,"FE|Non-energy Use|Industry|+|Solids (EJ/yr)"], + # "FE|w/o Non-energy Use|Industry|Solids (EJ/yr)") ) #FE per sector and per emission market (ETS and ESR) From 24c899532cf4fedc5f51e3f543923bec61bf6b32 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 9 Jun 2023 20:34:45 +0200 Subject: [PATCH 17/31] remove temporary fix for feedstock reporting from new reporting of feedstocks --- R/reportFE.R | 97 ---------------------------------------------------- 1 file changed, 97 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index ce68087a..3b959640 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -147,8 +147,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector } - - # ---- FE total production (incl. non-energy use) ------ out <- mbind(out, @@ -1780,85 +1778,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, ) - # energy carrier split in FE energy use variables -#####################CLEAN OUT THIS CHUNK-------------------------------------------------------- - # out <- mbind(out, - # # FE industry (without feedstocks) liquids: from fossils, biomass, hydrogen - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - # out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / - # out[,,"FE|Industry|+|Liquids (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - # out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / - # out[,,"FE|Industry|+|Liquids (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - # out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / - # out[,,"FE|Industry|+|Liquids (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), - # # FE industry (without feedstocks) gases: from fossils, biomass, hydrogen - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - # out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / - # out[,,"FE|Industry|+|Gases (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - # out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / - # out[,,"FE|Industry|+|Gases (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - # out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / - # out[,,"FE|Industry|+|Gases (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), - # # FE industry (without feedstocks) solids: from fossils, biomass - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - # out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / - # out[,,"FE|Industry|+|Solids (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), - # setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - # out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / - # out[,,"FE|Industry|+|Solids (EJ/yr)"], - # "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)")) -#---------------------------------------------------------------------------------- - out <- mbind(out, - # FE industry (without feedstocks) liquids: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Liquids (EJ/yr)"] * - out[,,"FE|Industry|Liquids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Liquids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Liquids|+|Fossil (EJ/yr)"), - # FE industry (without feedstocks) gases: from fossils, biomass, hydrogen - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Hydrogen (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Biomass (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Gases (EJ/yr)"] * - out[,,"FE|Industry|Gases|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Gases (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Gases|+|Fossil (EJ/yr)"), - # FE industry (without feedstocks) solids: from fossils, biomass - setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Fossil (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids|+|Fossil (EJ/yr)"), - setNames(out[,,"FE|w/o Non-energy Use|Industry|Solids (EJ/yr)"] * - out[,,"FE|Industry|Solids|+|Biomass (EJ/yr)"] / - out[,,"FE|Industry|+|Solids (EJ/yr)"], - "FE|w/o Non-energy Use|Industry|Solids|+|Biomass (EJ/yr)")) - - - - tryCatch( expr = { out <- mbind( @@ -1888,14 +1807,8 @@ reportFE <- function(gdx, regionSubsetList = NULL, } - ### FE variables without bunkers ---- - - - - - ### variables for which version without bunkers should be calculated fe.vars.woBunkers <- c( @@ -1904,10 +1817,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, "FE|++|Transport (EJ/yr)", "FE|Transport|+|Liquids (EJ/yr)") - - - - # add FE w/o non-energy use variables if available if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { @@ -1932,7 +1841,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, # add FE w/o non-energy use variables if available if ("FE|Non-energy Use (EJ/yr)" %in% getNames(out)) { - # bunker correction for distinction of fossil, biomass, hydrogen-based liquids fe.vars.woBunkers.fos <- c( fe.vars.woBunkers.fos, "FE|w/o Non-energy Use|Liquids|+|Fossil (EJ/yr)") @@ -1942,9 +1850,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, fe.vars.woBunkers.syn <- c( fe.vars.woBunkers.syn, "FE|w/o Non-energy Use|Liquids|+|Hydrogen (EJ/yr)") - - - } @@ -1990,10 +1895,8 @@ reportFE <- function(gdx, regionSubsetList = NULL, getNames(out.woBunkers.syn) <- gsub("\\|\\++\\|", "\\|", getNames(out.woBunkers.syn)) - out <- mbind(out, out.woBunkers, out.woBunkers.fos, out.woBunkers.bio, out.woBunkers.syn) - ### Aggregation to global values ---- # add global values From 7225b045b870b39394392621d26d0e12b82983c2 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 10 Aug 2023 16:07:50 +0200 Subject: [PATCH 18/31] unfinished: adapt feedstock and removal reporting to new industry reporting structure --- R/reportEmi.R | 91 +++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index f2a0ea91..d739751d 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -79,7 +79,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 teCCS <- readGDX(gdx, "teCCS") teBio <- readGDX(gdx, "teBio") teFosCCS <- readGDX(gdx, "teFosCCS") - + # additional sets needed @@ -92,7 +92,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 se_gas <- c("segafos", "segabio") } se_sol <- c("sesofos", "sesobio") - + # sets of hydrocarbon SE carriers entySeFos <- readGDX(gdx, "entySeFos") entySeBio <- readGDX(gdx, "entySeBio") @@ -163,14 +163,14 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (!is.null(pm_IndstCO2Captured)) { pm_IndstCO2Captured <- pm_IndstCO2Captured[se2fe[,c(1,2)]] } - # FE non-energy use + # FE non-energy use vm_demFENonEnergySector <- readGDX(gdx, "vm_demFENonEnergySector", field = "l", react = "silent")[,t,] if (length(vm_demFENonEnergySector) == 0) { vm_demFENonEnergySector <- NULL } - + vm_FeedstocksCarbon <- readGDX(gdx, "vm_FeedstocksCarbon", field = "l", restore_zeros = T, react = "silent")[,t,] - + # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) # parameter to calculate coupled production @@ -207,7 +207,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 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 @@ -312,38 +312,38 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } # Preliminary Calculations---- - + # get combinations of SE,FE,sector,emiMkt that exist in vm_demFeSector - FE.map <- se2fe %>% - left_join(entyFe2Sector) %>% - left_join(sector2emiMkt) %>% - select( -all_te) %>% - mutate( name = paste(all_enty,all_enty1,emi_sectors,all_emiMkt, sep = ".")) + FE.map <- se2fe %>% + left_join(entyFe2Sector) %>% + left_join(sector2emiMkt) %>% + select( -all_te) %>% + mutate( name = paste(all_enty,all_enty1,emi_sectors,all_emiMkt, sep = ".")) # calculate FE without non-energy use if (!is.null(vm_demFENonEnergySector)) { - vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[, , FE.map$name], + vm_demFENonEnergySector <- mselect(vm_demFENonEnergySector[, , FE.map$name], all_enty1 = entyFe2sector2emiMkt_NonEn$all_enty, emi_sectors = entyFe2sector2emiMkt_NonEn$emi_sectors, all_emiMkt = entyFe2sector2emiMkt_NonEn$all_emiMkt) - + # calculate FE without non-energy use vm_demFeSector_woNonEn <- vm_demFeSector - vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector + vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector } - - # get CDR by feedstock carbon from non-fossil sources (biogenic or synthetic) + + # get CDR by feedstock carbon from non-fossil sources (biogenic or synthetic) if (!is.null(vm_FeedstocksCarbon)) { - + # get combinations of SE,FE,sector,emiMkt that exist in vm_FeedstocksCarbon - FE.feed.map <- se2fe %>% - left_join(entyFe2Sector) %>% - left_join(sector2emiMkt) %>% - right_join(entyFe2sector2emiMkt_NonEn %>% - rename(all_enty1 = all_enty)) %>% - select( -all_te) %>% - mutate( name = paste(all_enty,all_enty1,all_emiMkt, sep = ".")) + FE.feed.map <- se2fe %>% + left_join(entyFe2Sector) %>% + left_join(sector2emiMkt) %>% + right_join(entyFe2sector2emiMkt_NonEn %>% + rename(all_enty1 = all_enty)) %>% + select( -all_te) %>% + mutate( name = paste(all_enty,all_enty1,all_emiMkt, sep = ".")) Feedstock_CDR <- mselect(vm_FeedstocksCarbon[,,FE.feed.map$name], all_enty = unique(entySeBio, entySeSyn)) @@ -466,7 +466,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } else { EmiFeCarrier <- pm_emifac.co2.fe * vm_demFeSector[, , emi.map.fe$name] } - + @@ -613,10 +613,10 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # buidings emissions setNames(dimSums(EmiFeCarrier[, , "build"], dim = 3) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|+|Buildings (Mt CO2/yr)"), - # industry emissions: fe carrier emissions - industry CCS - # (carbon captured by industry used for CCU is still accounted as industry emissions + # industry emissions: fe carrier emissions - industry CCS + # (carbon captured by industry used for CCU is still accounted as industry emissions # as only account negative emissions for the CO2 provider if carbon gets stored) - setNames((dimSums(EmiFeCarrier[, , "indst"], dim = 3) + setNames((dimSums(EmiFeCarrier[, , "indst"], dim = 3) - dimSums(vm_emiIndCCS[, , emiInd37_fuel]* p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - dimSums(Feedstock_CDR, dim=3)) * GtC_2_MtCO2, @@ -635,7 +635,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)) { - + ### calculate FE per industry subsector w/o Non-energy Use o37_demFeIndSub_woNonEn <- o37_demFeIndSub sefe.chem.feedstock <- intersect(getNames(collapseDim(o37_demFeIndSub_woNonEn[,,"chemicals.ETS"])), getNames(collapseDim(vm_demFENonEnergySector[,,"indst"]))) @@ -763,9 +763,11 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 mutate( variable = paste0(variable_prefix, .data$variable, variable_postfix)) + #browser() + # if feedstocks are represented in REMIND if (!is.null(vm_FeedstocksCarbon)){ - + out <- mbind( out, lapply(.mixer_to_selector(mixer), function(x) { @@ -782,13 +784,16 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # subtract carbon contained in plastics that are not incinerated # FIX me: this needs to be tested, I might be breaking the reporting with this: - dimSums( - Feedstock_CDR, dim=3) + ( mselect(Feedstock_CDR, x[setdiff(names(x), 'variable')]) + ), + dim = 3) ) * GtC_2_MtCO2, x[['variable']]) }) %>% mbind()) - } else { + } + else { out <- mbind( out, lapply(.mixer_to_selector(mixer), function(x) { @@ -961,7 +966,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Emi|CO2|+|Energy (Mt CO2/yr)")) ### 2.2 Non-energy CO2 emissions ---- - # (following q_emiAllMkt) + # (following q_emiAllMkt) ### 2.2.1 Industrial Process Emissions ---- # if FE non-energy use available @@ -972,7 +977,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } else { # otherwise chemical process emissions are 0 EmiProcess_Feedstocks <- vm_co2eq * 0 - } + } # add industrial process emissions in steel and chemicals specifically for ARIADNE project where those emissions are included for Germany @@ -1015,14 +1020,14 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, # industrial process CO2 (so far only cement process emissions in REMIND) - setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) + setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) # add captured CO2 from cement process which is not stored but used for CCU + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS) # add chemical process emissions + dimSums(EmiProcess_Feedstocks, dim = 3)) * GtC_2_MtCO2, "Emi|CO2|+|Industrial Processes (Mt CO2/yr)"), # process industry cement CO2 - setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) + setNames((dimSums(vm_emiMacSector[, , "co2cement_process"], dim = 3) # add captured CO2 from cement process which is not stored + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2, "Emi|CO2|Industrial Processes|+|Cement (Mt CO2/yr)"), @@ -1041,7 +1046,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ) } - + #### total energy yand industry CO2 emissions out <- mbind(out, @@ -2089,7 +2094,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 - dimSums(vm_emiIndCCS[, , emiInd37_fuel]*p_share_CCS, dim=3) # substract synthetic and biogenic carbon contained in industrial feedstocks - dimSums(Feedstock_CDR, dim=3) - # add captured CO2 from cement process which is not stored + # add captured CO2 from cement process which is not stored # (EmiMACEq for co2cement_process contains cement process emissions - captured cement co2 process emissions) + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2 + dimSums(mselect(EmiMACEq[, , "ETS"], sector = "indst"), dim = 3) @@ -2263,9 +2268,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } - - - + + + # 8. Ad-hoc fix for emissions w/o non-energy use and Aggregation to global and regional values ---- if (is.null(vm_demFENonEnergySector) && (module2realisation["industry", 2] == "fixed_shares")) { @@ -2395,7 +2400,7 @@ if (is.null(vm_demFENonEnergySector) && (module2realisation["industry", 2] == "f out <- mbind(out, out.wNonEn) - } + } } # add global values out <- mbind(out, dimSums(out, dim = 1)) From 8ef57b096d1d4951cc51ca3144b27ff5cbd2b3c5 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 10 Aug 2023 16:57:35 +0200 Subject: [PATCH 19/31] debug feedstocks removals reporting in new industry framework --- R/reportEmi.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index d739751d..257cdee7 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -763,11 +763,10 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 mutate( variable = paste0(variable_prefix, .data$variable, variable_postfix)) - #browser() - # if feedstocks are represented in REMIND if (!is.null(vm_FeedstocksCarbon)){ + Feedstock_CDR_SubSec <- add_dimension(Feedstock_CDR, dim = 3.1, add = 'secInd37', nm = 'chemicals') out <- mbind( out, lapply(.mixer_to_selector(mixer), function(x) { @@ -782,9 +781,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 ), dim = 3) # subtract carbon contained in plastics that are not incinerated - # FIX me: this needs to be tested, I might be breaking the reporting with this: - dimSums( - ( mselect(Feedstock_CDR, x[setdiff(names(x), 'variable')]) + ( mselect(Feedstock_CDR_SubSec, x[setdiff(names(x), 'variable')]) ), dim = 3) ) * GtC_2_MtCO2, From e25501a0dac20e6290ce4d7f86ab0a9bce1fa93c Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Tue, 29 Aug 2023 15:41:49 +0200 Subject: [PATCH 20/31] add plastics-related variables to emissions reporting and restrict negative emissions to feedstocks carbon contained in non-incinerated plastics --- R/reportEmi.R | 109 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 257cdee7..150ad3b6 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -169,8 +169,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_demFENonEnergySector <- NULL } - vm_FeedstocksCarbon <- readGDX(gdx, "vm_FeedstocksCarbon", field = "l", restore_zeros = T, react = "silent")[,t,] - # secondary energy production vm_prodSe <- readGDX(gdx, "vm_prodSe", field = "l", restore_zeros = F) # parameter to calculate coupled production @@ -333,10 +331,24 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 vm_demFeSector_woNonEn[,,getNames(vm_demFENonEnergySector )] <- vm_demFeSector[,,getNames(vm_demFENonEnergySector )]-vm_demFENonEnergySector } - # get CDR by feedstock carbon from non-fossil sources (biogenic or synthetic) - if (!is.null(vm_FeedstocksCarbon)) { + # Read-in plastic-related variables + vm_plasticsCarbon <- readGDX(gdx, "vm_plasticsCarbon", field = "l", restore_zeros = T, react = "silent")[,t,] + if (length(vm_plasticsCarbon) == 0) { + vm_plasticsCarbon <- NULL + } + + if (!is.null(vm_plasticsCarbon)){ + vm_feedstockEmiUnknownFate <- readGDX(gdx, "vm_feedstockEmiUnknownFate", field = "l", restore_zeros = T, react = "silent")[,t,] + vm_incinerationEmi <- readGDX(gdx, "vm_incinerationEmi", field = "l", restore_zeros = T, react = "silent")[,t,] + vm_nonIncineratedPlastics <- readGDX(gdx, "vm_nonIncineratedPlastics", field = "l", restore_zeros = T, react = "silent")[,t,] + } + + # create new variable for carbon embbeded in non-incinerated plastics: + # plastics that do not get incinerated and come from biogenic sources + # or synfuels (regardless of syfuel origin) + if (!is.null(vm_nonIncineratedPlastics)) { - # get combinations of SE,FE,sector,emiMkt that exist in vm_FeedstocksCarbon + # get combinations of SE,FE,sector,emiMkt that exist in vm_nonIncineratedPlastics FE.feed.map <- se2fe %>% left_join(entyFe2Sector) %>% left_join(sector2emiMkt) %>% @@ -344,11 +356,13 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 rename(all_enty1 = all_enty)) %>% select( -all_te) %>% mutate( name = paste(all_enty,all_enty1,all_emiMkt, sep = ".")) - Feedstock_CDR <- mselect(vm_FeedstocksCarbon[,,FE.feed.map$name], + + plastic_CDR <- mselect(vm_nonIncineratedPlastics[,,FE.feed.map$name], all_enty = unique(entySeBio, entySeSyn)) + } else { - Feedstock_CDR <- collapseDim(vm_co2eq)*0 + Plastic_CDR <- collapseDim(vm_co2eq)*0 } # Calculate Variables ---- @@ -486,7 +500,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # subtract industry CCS - dimSums(vm_emiIndCCS[, , emiInd37_fuel], dim = 3)*p_share_CCS # subtract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(Feedstock_CDR, dim=3) + - dimSums(plastic_CDR, dim=3) )*GtC_2_MtCO2, "Emi|CO2|Energy|+|Demand (Mt CO2/yr)") @@ -619,7 +633,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames((dimSums(EmiFeCarrier[, , "indst"], dim = 3) - dimSums(vm_emiIndCCS[, , emiInd37_fuel]* p_share_CCS, dim = 3) # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(Feedstock_CDR, dim=3)) * GtC_2_MtCO2, + - dimSums(plastic_CDR, dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|+|Industry (Mt CO2/yr)"), # transport emissions: fe carrier emissions setNames(dimSums(EmiFeCarrier[, , "trans"], dim = 3) * GtC_2_MtCO2, @@ -667,28 +681,28 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 EmiIndSubSec <- pm_emifac.fe.indst * o37_demFeIndSub_woNonEn[, , getNames(pm_emifac.fe.indst)] # calculate demand-side Solids, Liquids and gases emissions (after industry CO2 Capture) - if (!is.null(vm_FeedstocksCarbon)){ + if (!is.null(vm_nonIncineratedPlastics)){ out <- mbind(out, # solids emissions: direct solids emissions of all sectors - industry CCS of solids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = "fesos"), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fesos")*p_share_CCS, dim = 3) - # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("sesobio","sesosyn"), all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, + # substract synthetic and biogenic carbon contained in non-incinerated plastics + - dimSums(mselect(vm_nonIncineratedPlastics, all_enty=c("sesobio","sesosyn"), all_enty1 = "fesos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Solids (Mt CO2/yr)"), # liquids emissions: direct liquids emissions of all sectors - industry CCS of liquids setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fehos", "fepet", "fedie")), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fehos")*p_share_CCS, dim = 3) - # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("seliqbio","seliqsyn"), all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, + # substract synthetic and biogenic carbon contained in non-incinerated plastics + - dimSums(mselect(vm_nonIncineratedPlastics, all_enty=c("seliqbio","seliqsyn"), all_enty1 = "fehos"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Liquids (Mt CO2/yr)"), # gases emissions: direct gases emissions of all sectors - industry CCS of gases setNames((dimSums(mselect(EmiFeCarrier, all_enty1 = c("fegas", "fegat")), dim = 3) - dimSums(mselect(vm_emiIndCCS_Sub, all_enty1 = "fegas")*p_share_CCS, dim = 3) - # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(mselect(vm_FeedstocksCarbon, all_enty=c("segabio","segasyn"), all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, + # substract synthetic and biogenic carbon contained in non-incinerated plastics + - dimSums(mselect(vm_nonIncineratedPlastics, all_enty=c("segabio","segasyn"), all_enty1 = "fegas"), dim=3)) * GtC_2_MtCO2, "Emi|CO2|Energy|Demand|++|Gases (Mt CO2/yr)") ) } else { @@ -764,9 +778,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 variable = paste0(variable_prefix, .data$variable, variable_postfix)) # if feedstocks are represented in REMIND - if (!is.null(vm_FeedstocksCarbon)){ + if (!is.null(vm_nonIncineratedPlastics)){ - Feedstock_CDR_SubSec <- add_dimension(Feedstock_CDR, dim = 3.1, add = 'secInd37', nm = 'chemicals') + plastic_CDR_SubSec <- add_dimension(plastic_CDR, dim = 3.1, add = 'secInd37', nm = 'chemicals') out <- mbind( out, lapply(.mixer_to_selector(mixer), function(x) { @@ -782,7 +796,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 dim = 3) # subtract carbon contained in plastics that are not incinerated - dimSums( - ( mselect(Feedstock_CDR_SubSec, x[setdiff(names(x), 'variable')]) + ( mselect(plastic_CDR_SubSec, x[setdiff(names(x), 'variable')]) ), dim = 3) ) * GtC_2_MtCO2, @@ -1230,6 +1244,43 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Carbon Management|Carbon Capture (Mt CO2/yr)") ) + # carbon management: flows of carbon going into materials + out <- mbind(out, + + # carbon in plastics + setNames(dimSums(vm_plasticsCarbon, dim = 3) * GtC_2_MtCO2, + "Carbon Management|Materials|+|Plastics (Mt CO2/yr)"), + # carbon in carbon fibres + #setNames(dimSums( carbonfibersVariable * GtC_2_MtCO2, + # "Carbon Management|Materials|+|Carbon fibers (Mt CO2/yr)"), + # total co2 in materials + setNames(vm_co2capture * GtC_2_MtCO2, + "Carbon Management|Materials (Mt CO2/yr)") + ) + + # carbon management: fate of plastics + out <- mbind(out, + + # carbon in plastics that are incinerated + setNames(dimSums(vm_incinerationEmi, dim = 3) * GtC_2_MtCO2, + "Carbon Management|Materials|Plastics|++|Incineration (Mt CO2/yr)"), + # carbon in plastics with other fate + setNames(dimSums(vm_nonIncineratedPlastics, dim = 3) * GtC_2_MtCO2, + "Carbon Management|Materials|Plastics|++|Other destination (Mt CO2/yr)") + + ) + + # # carbon management: origin of plastics + + out <- mbind(out, + setNames(dimSums(mselect(vm_plasticsCarbon, all_enty=c("seliqsyn","sesosyn","segasyn")))* GtC_2_MtCO2, + "Carbon Management|Materials|Plastics|+|Synfuels (Mt CO2/yr)"), + setNames(dimSums(mselect(vm_plasticsCarbon, all_enty=c("seliqbio","sesobio","segabio")))* GtC_2_MtCO2, + "Carbon Management|Materials|Plastics|+|Biomass (Mt CO2/yr)"), + setNames(dimSums(mselect(vm_plasticsCarbon, all_enty=c("seliqfos","sesofos","segafos")))* GtC_2_MtCO2, + "Carbon Management|Materials|Plastics|+|Fossil (Mt CO2/yr)") + ) + ### report industry captured CO2 ---- if (!is.null(pm_IndstCO2Captured)) { variable_prefix <- 'Carbon Management|Carbon Capture|Industry Energy|' @@ -1500,6 +1551,14 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 p_share_atmosco2[is.infinite(p_share_atmosco2)] <- 0 p_share_atmosco2[is.na(p_share_atmosco2)] <- 0 + # calculate share of atmospheric and biogenic carbon contained in plastic products + # fix me: hide in if{} + p_share_atmBiogco2 <- dimSums( + ( out[,,"Carbon Management|Materials|Plastics|+|Biomass (Mt CO2/yr)"] + +out[,,"Carbon Management|Materials|Plastics|+|Synfuels (Mt CO2/yr)"]*p_share_atmosco2) + /(out[,,"Carbon Management|Materials|+|Plastics (Mt CO2/yr)"]+ 1e-07) + ) + # Emi|CO2|CDR is defined negative out <- mbind(out, # total negative land-use change emissions @@ -1519,6 +1578,10 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames(-out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Synfuel (Mt CO2/yr)"] * p_share_atmosco2 * p_share_CCS, "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"), + # CO2 stored in plastic products that are not incinerated and come from atmospheric or biogenic carbon + setNames(-out[, , "Carbon Management|Materials|Plastics|++|Other destination (Mt CO2/yr)"] *p_share_atmBiogco2, + "Emi|CO2|CDR|Materials|+|Plastics (Mt CO2/yr)"), + # total DACCS setNames(-out[, , "Carbon Management|Storage|+|DAC (Mt CO2/yr)"], "Emi|CO2|CDR|DACCS (Mt CO2/yr)"), @@ -1529,11 +1592,13 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out <- mbind(out, # total CDR + # double-check: if we don't account for fossil content in plastics, the emissions might not add up? setNames( out[, , "Emi|CO2|CDR|Land-Use Change (Mt CO2/yr)"] + out[, , "Emi|CO2|CDR|BECCS (Mt CO2/yr)"] + out[, , "Emi|CO2|CDR|DACCS (Mt CO2/yr)"] + out[, , "Emi|CO2|CDR|EW (Mt CO2/yr)"] - + out[, , "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"], + + out[, , "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"] + + out[, , "Emi|CO2|CDR|Materials|+|Plastics (Mt CO2/yr)"], "Emi|CO2|CDR (Mt CO2/yr)")) @@ -2090,8 +2155,8 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # industry CCS # TODO: adapt to industry ETS/ESR split - dimSums(vm_emiIndCCS[, , emiInd37_fuel]*p_share_CCS, dim=3) - # substract synthetic and biogenic carbon contained in industrial feedstocks - - dimSums(Feedstock_CDR, dim=3) + # substract synthetic and biogenic carbon contained in non-incinerated plastics + - dimSums(plastic_CDR, dim=3) # add captured CO2 from cement process which is not stored # (EmiMACEq for co2cement_process contains cement process emissions - captured cement co2 process emissions) + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2 From 41d66caf98f30ff5422b17482d58e43403fc5663 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 31 Aug 2023 15:49:45 +0200 Subject: [PATCH 21/31] add plastic plots and fix emi|CO2 accounting --- R/reportEmi.R | 16 +++++++++++----- .../compareScenarios2/cs2_03_emissions.Rmd | 8 +++++++- .../cs2_09_carbon_management.Rmd | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 150ad3b6..573f1259 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -338,6 +338,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } if (!is.null(vm_plasticsCarbon)){ + #fix me: create reported variable for vm_feedstockEmiUnknownFate -> Emi|CO2|Feedstocks unknown fate vm_feedstockEmiUnknownFate <- readGDX(gdx, "vm_feedstockEmiUnknownFate", field = "l", restore_zeros = T, react = "silent")[,t,] vm_incinerationEmi <- readGDX(gdx, "vm_incinerationEmi", field = "l", restore_zeros = T, react = "silent")[,t,] vm_nonIncineratedPlastics <- readGDX(gdx, "vm_nonIncineratedPlastics", field = "l", restore_zeros = T, react = "silent")[,t,] @@ -986,6 +987,12 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # calculate chemical process emissions from feedstocks treatment EmiFac_NonEn.co2 <- collapseDim(pm_emifacNonEnergy[,,"co2"]) EmiProcess_Feedstocks <- pm_emifacNonEnergy[,,"co2"] * vm_demFENonEnergySector[,,getNames(EmiFac_NonEn.co2)] + out <- mbind(out, + setNames(dimSums(vm_feedstockEmiUnknownFate, dim=3)* GtC_2_MtCO2, + "Emi|CO2|Feedstocks unknown fate (Mt CO2/yr)"), + setNames(dimSums(vm_incinerationEmi, dim=3)* GtC_2_MtCO2, + "Emi|CO2|Plastics incineration (Mt CO2/yr)") + ) } else { # otherwise chemical process emissions are 0 EmiProcess_Feedstocks <- vm_co2eq * 0 @@ -1044,7 +1051,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 + vm_emiIndCCS[, , "co2cement_process"]*(1-p_share_CCS)) * GtC_2_MtCO2, "Emi|CO2|Industrial Processes|+|Cement (Mt CO2/yr)"), # chemical process emissions from feedstocks treatment - setNames(dimSums(EmiProcess_Feedstocks*GtC_2_MtCO2, dim = 3), + setNames(dimSums(EmiProcess_Feedstocks, dim = 3)*GtC_2_MtCO2, "Emi|CO2|Industrial Processes|+|Chemicals (Mt CO2/yr)") ) @@ -1060,7 +1067,9 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } - #### total energy yand industry CO2 emissions + #### total energy and industry CO2 emissions + # Emi|CO2|Plastics incineration and Emi|CO2|Feedstocks unknown fate + # are accounted for in vm_emiTeMkt and therefore in Emi|CO2|+|Energy (Mt CO2/yr) out <- mbind(out, setNames(out[, , "Emi|CO2|+|Energy (Mt CO2/yr)"] + out[, , "Emi|CO2|+|Industrial Processes (Mt CO2/yr)"], @@ -1602,9 +1611,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Emi|CO2|CDR (Mt CO2/yr)")) - - - ## 4. Gross Emissions (excl. negative emissions from BECCS) ---- #### calculate gross emissions diff --git a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd index 559a1d73..e2c8e321 100644 --- a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd +++ b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd @@ -62,6 +62,9 @@ showLinePlots(data, "Emi|GHG|Outside ETS and ESR") ```{r CO2 by sector net incl. BECCS} tot <- "Emi|CO2" +# fix me: emi categories don't add up to total from sel_vm_emiAllMkt_co2 +# this one is missing: vm_feedstockEmiUnknownFate +# I need to re-name them also Emi|CO2|Plastics incineration and Emi|CO2|Feedstocks unknown fate items <- c( "Emi|CO2|Land-Use Change", "Emi|CO2|Industrial Processes", @@ -71,7 +74,10 @@ items <- c( "Emi|CO2|Energy|Supply|Non-electric", "Emi|CO2|Energy|Supply|Electricity w/ couple prod", "Emi|CO2|CDR|DACCS", - "Emi|CO2|CDR|EW") + "Emi|CO2|CDR|EW", + "Emi|CO2|Plastics incineration", + "Emi|CO2|Feedstocks unknown fate" + ) showAreaAndBarPlots(data, items, tot, scales = "fixed") ``` diff --git a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd index 5112a696..6fbdd73c 100644 --- a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd +++ b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd @@ -37,3 +37,19 @@ showAreaAndBarPlots(data, 'Carbon Management|Usage'), tot = 'Carbon Management|Carbon Capture') ``` + +# Materials + +## Total plastics production +```{r } +showLinePlots(data, "Carbon Management|Materials|Plastics") +``` + +## Plastics production by origin +```{r } +showAreaAndBarPlots(data, + vars = c("Carbon Management|Materials|Plastics|Fossil", + "Carbon Management|Materials|Plastics|Biomass", + "Carbon Management|Materials|Plastics|Synfuels"), + scales = "fixed") +``` From 5e00627740d43ba37d5f1cf25c5929d6b4cd526e Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Mon, 4 Sep 2023 17:02:32 +0200 Subject: [PATCH 22/31] add plot in cs2 for plastics fate --- inst/markdown/compareScenarios2/cs2_03_emissions.Rmd | 3 --- .../compareScenarios2/cs2_09_carbon_management.Rmd | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd index e2c8e321..f023c6a3 100644 --- a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd +++ b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd @@ -62,9 +62,6 @@ showLinePlots(data, "Emi|GHG|Outside ETS and ESR") ```{r CO2 by sector net incl. BECCS} tot <- "Emi|CO2" -# fix me: emi categories don't add up to total from sel_vm_emiAllMkt_co2 -# this one is missing: vm_feedstockEmiUnknownFate -# I need to re-name them also Emi|CO2|Plastics incineration and Emi|CO2|Feedstocks unknown fate items <- c( "Emi|CO2|Land-Use Change", "Emi|CO2|Industrial Processes", diff --git a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd index 6fbdd73c..d7b18af9 100644 --- a/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd +++ b/inst/markdown/compareScenarios2/cs2_09_carbon_management.Rmd @@ -53,3 +53,11 @@ showAreaAndBarPlots(data, "Carbon Management|Materials|Plastics|Synfuels"), scales = "fixed") ``` + +## Plastics production by fate +```{r } +showAreaAndBarPlots(data, + vars = c("Carbon Management|Materials|Plastics|Incineration", + "Carbon Management|Materials|Plastics|Other destination"), + scales = "fixed") +``` From fbfb81efa2befc56eae33fc36216346b26092855 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Mon, 4 Sep 2023 19:38:05 +0200 Subject: [PATCH 23/31] bugfix: fix backwards compatibility of emissions reporting when chemical feedstock and plastics are not included --- R/reportEmi.R | 60 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 573f1259..b9a8a0c1 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -347,7 +347,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # create new variable for carbon embbeded in non-incinerated plastics: # plastics that do not get incinerated and come from biogenic sources # or synfuels (regardless of syfuel origin) - if (!is.null(vm_nonIncineratedPlastics)) { + if (!is.null(vm_plasticsCarbon)) { # get combinations of SE,FE,sector,emiMkt that exist in vm_nonIncineratedPlastics FE.feed.map <- se2fe %>% @@ -363,7 +363,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } else { - Plastic_CDR <- collapseDim(vm_co2eq)*0 + plastic_CDR <- collapseDim(vm_co2eq)*0 } # Calculate Variables ---- @@ -682,7 +682,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 EmiIndSubSec <- pm_emifac.fe.indst * o37_demFeIndSub_woNonEn[, , getNames(pm_emifac.fe.indst)] # calculate demand-side Solids, Liquids and gases emissions (after industry CO2 Capture) - if (!is.null(vm_nonIncineratedPlastics)){ + if (!is.null(vm_plasticsCarbon)){ out <- mbind(out, # solids emissions: direct solids emissions of all sectors - industry CCS of solids @@ -779,7 +779,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 variable = paste0(variable_prefix, .data$variable, variable_postfix)) # if feedstocks are represented in REMIND - if (!is.null(vm_nonIncineratedPlastics)){ + if (!is.null(vm_plasticsCarbon)){ plastic_CDR_SubSec <- add_dimension(plastic_CDR, dim = 3.1, add = 'secInd37', nm = 'chemicals') out <- mbind( @@ -1253,17 +1253,20 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 "Carbon Management|Carbon Capture (Mt CO2/yr)") ) + # add materials-related carbon management variables only if available + if (!is.null(vm_plasticsCarbon)){ # carbon management: flows of carbon going into materials out <- mbind(out, # carbon in plastics setNames(dimSums(vm_plasticsCarbon, dim = 3) * GtC_2_MtCO2, "Carbon Management|Materials|+|Plastics (Mt CO2/yr)"), - # carbon in carbon fibres + # carbon in carbon fibres #setNames(dimSums( carbonfibersVariable * GtC_2_MtCO2, # "Carbon Management|Materials|+|Carbon fibers (Mt CO2/yr)"), # total co2 in materials - setNames(vm_co2capture * GtC_2_MtCO2, + # other materials need to be added here + setNames(dimSums(vm_plasticsCarbon, dim = 3) * GtC_2_MtCO2, "Carbon Management|Materials (Mt CO2/yr)") ) @@ -1289,6 +1292,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 setNames(dimSums(mselect(vm_plasticsCarbon, all_enty=c("seliqfos","sesofos","segafos")))* GtC_2_MtCO2, "Carbon Management|Materials|Plastics|+|Fossil (Mt CO2/yr)") ) + } ### report industry captured CO2 ---- if (!is.null(pm_IndstCO2Captured)) { @@ -1560,15 +1564,17 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 p_share_atmosco2[is.infinite(p_share_atmosco2)] <- 0 p_share_atmosco2[is.na(p_share_atmosco2)] <- 0 + # CDR reporting if plastic-removals are considered + if (!is.null(vm_plasticsCarbon)){ # calculate share of atmospheric and biogenic carbon contained in plastic products - # fix me: hide in if{} p_share_atmBiogco2 <- dimSums( ( out[,,"Carbon Management|Materials|Plastics|+|Biomass (Mt CO2/yr)"] +out[,,"Carbon Management|Materials|Plastics|+|Synfuels (Mt CO2/yr)"]*p_share_atmosco2) - /(out[,,"Carbon Management|Materials|+|Plastics (Mt CO2/yr)"]+ 1e-07) + /(out[,,"Carbon Management|Materials|+|Plastics (Mt CO2/yr)"]) ) # Emi|CO2|CDR is defined negative + out <- mbind(out, # total negative land-use change emissions setNames(EmiCDR.LUC, @@ -1609,7 +1615,45 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 + out[, , "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"] + out[, , "Emi|CO2|CDR|Materials|+|Plastics (Mt CO2/yr)"], "Emi|CO2|CDR (Mt CO2/yr)")) + #CDR reporting if plastic-removals are not considered + } else{ + # Emi|CO2|CDR is defined negative + + out <- mbind(out, + # total negative land-use change emissions + setNames(EmiCDR.LUC, + "Emi|CO2|CDR|Land-Use Change (Mt CO2/yr)"), + # total BECCS (pe2se + industry) + setNames(-out[, , "Carbon Management|Storage|+|Biomass|Pe2Se (Mt CO2/yr)"] + -out[, , "Carbon Management|Storage|Industry Energy|+|Biomass (Mt CO2/yr)"], + "Emi|CO2|CDR|BECCS (Mt CO2/yr)"), + # Pe2Se BECCS + setNames(-out[, , "Carbon Management|Storage|+|Biomass|Pe2Se (Mt CO2/yr)"], + "Emi|CO2|CDR|BECCS|Pe2Se (Mt CO2/yr)"), + # Industry BECCS + setNames(-out[, , "Carbon Management|Storage|Industry Energy|+|Biomass (Mt CO2/yr)"], + "Emi|CO2|CDR|BECCS|Industry (Mt CO2/yr)"), + # stored CO2 in industry from carbon-neutral fuels (synthetic fuels) + setNames(-out[, , "Carbon Management|Carbon Capture|Industry Energy|+|Synfuel (Mt CO2/yr)"] * p_share_atmosco2 * p_share_CCS, + "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"), + # total DACCS + setNames(-out[, , "Carbon Management|Storage|+|DAC (Mt CO2/yr)"], + "Emi|CO2|CDR|DACCS (Mt CO2/yr)"), + # total EW + # total co2 captured by EW + setNames(v33_emi[, , "weathering"] * GtC_2_MtCO2, + "Emi|CO2|CDR|EW (Mt CO2/yr)")) + out <- mbind(out, + # total CDR + # double-check: if we don't account for fossil content in plastics, the emissions might not add up? + setNames( out[, , "Emi|CO2|CDR|Land-Use Change (Mt CO2/yr)"] + + out[, , "Emi|CO2|CDR|BECCS (Mt CO2/yr)"] + + out[, , "Emi|CO2|CDR|DACCS (Mt CO2/yr)"] + + out[, , "Emi|CO2|CDR|EW (Mt CO2/yr)"] + + out[, , "Emi|CO2|CDR|Industry CCS|Synthetic Fuels (Mt CO2/yr)"], + "Emi|CO2|CDR (Mt CO2/yr)")) + } ## 4. Gross Emissions (excl. negative emissions from BECCS) ---- From a76ed3fbb15272db20b60f5a8a49b9794421f0d5 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 12 Oct 2023 12:22:54 +0200 Subject: [PATCH 24/31] add historical UNFCCC data for process emissions and line plot for FE|Chemicals --- .../compareScenarios2/cs2_03_emissions.Rmd | 25 ++++++++++++++++--- .../cs2_05_energy_demand.Rmd | 7 ++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd index f023c6a3..e6fb2f9d 100644 --- a/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd +++ b/inst/markdown/compareScenarios2/cs2_03_emissions.Rmd @@ -232,17 +232,28 @@ showLinePlots(data, "Emi|CO2|Energy|Demand|Transport|International Bunkers") ```{r CO2 Process Emissions} items <- c( - "Emi|CO2|Industrial Processes", # scenario - "Emi|CO2|FFaI|Industry|Process") # historical + "Emi|CO2|Industrial Processes"#, # scenario + # "Emi|CO2|FFaI|Industry|Process" + ) # historical showLinePlots(data, items) ``` +```{r CO2 Process Emissions} +tot <- c("Emi|CO2|Industrial Processes") +items <- c( + "Emi|CO2|Industrial Processes|Chemicals", + "Emi|CO2|Industrial Processes|Cement" + ) +showAreaAndBarPlots(data, items, tot, scales = "fixed") +``` ### Process Emissions Cement ```{r} items <- c( - "Emi|CO2|Industrial Processes|Cement") + "Emi|CO2|Industrial Processes|Cement", + "Emi|CO2|Industrial Processes|Minerals" # to display historical data from UNFCCC + ) showLinePlots(data, items) ``` @@ -255,6 +266,14 @@ items <- c( showLinePlots(data, items) ``` +### Process Emissions Metals + +```{r} +items <- c( + "Emi|CO2|Industrial Processes|Metals") +showLinePlots(data, items) +``` + ### Land-Use Change ```{r CO2 Land-Use Change} diff --git a/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd b/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd index 7d46cb5d..dd10391f 100644 --- a/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd +++ b/inst/markdown/compareScenarios2/cs2_05_energy_demand.Rmd @@ -190,6 +190,13 @@ items <- c( showAreaAndBarPlots(data, items, tot, scales = "fixed") ``` +### FE Total Chemicals +```{r FE Total Chemicals} +items <- c( + "FE|Industry|Chemicals") +showLinePlots(data, items) +``` + ### FE mix Other Industry ```{r FE mix Other Industry} tot <- "FE|Industry|Other Industry" From b4446632eca4258f12d1488957f37977ecd65370 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 23 Nov 2023 16:29:54 +0100 Subject: [PATCH 25/31] fix comments --- R/reportEmi.R | 1 - inst/markdown/compareScenarios2/cs2_01_summary.Rmd | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 95f66bd6..71055df7 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -337,7 +337,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } if (!is.null(vm_plasticsCarbon)){ - #fix me: create reported variable for vm_feedstockEmiUnknownFate -> Emi|CO2|Feedstocks unknown fate vm_feedstockEmiUnknownFate <- readGDX(gdx, "vm_feedstockEmiUnknownFate", field = "l", restore_zeros = T, react = "silent")[,t,] vm_incinerationEmi <- readGDX(gdx, "vm_incinerationEmi", field = "l", restore_zeros = T, react = "silent")[,t,] vm_nonIncineratedPlastics <- readGDX(gdx, "vm_nonIncineratedPlastics", field = "l", restore_zeros = T, react = "silent")[,t,] diff --git a/inst/markdown/compareScenarios2/cs2_01_summary.Rmd b/inst/markdown/compareScenarios2/cs2_01_summary.Rmd index 467040b8..e1a7e664 100644 --- a/inst/markdown/compareScenarios2/cs2_01_summary.Rmd +++ b/inst/markdown/compareScenarios2/cs2_01_summary.Rmd @@ -181,7 +181,7 @@ items <- c( showAreaAndBarPlots(data, items, scales = "fixed") ``` -## FE Transport by carrier (incl. international bunkers) +## FE Transport by carrier ```{r FE Transport by carrier} items <- c( From 831bd37fc02b82217f8c37d02711b87cb7960dbc Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Thu, 23 Nov 2023 16:45:01 +0100 Subject: [PATCH 26/31] bugfix: missplaced bracket --- R/reportFE.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/reportFE.R b/R/reportFE.R index 903c130a..dd6bb258 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1840,7 +1840,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, ) - } ### FE variables without bunkers ---- ### variables for which version without bunkers should be calculated From acac88318efd14dca633940804fa61a178cdfdec Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 24 Nov 2023 16:49:18 +0100 Subject: [PATCH 27/31] bugfix errors from master pull for backward compatibility --- R/reportEmi.R | 100 +++++++++++++++++++++++++------------------------- R/reportFE.R | 4 +- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 71055df7..7e2da3a3 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -885,67 +885,69 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 out) } else { - out <- mbind( - out, - lapply(.mixer_to_selector(mixer), function(x) { + out <- mbind( + out, + + lapply(.mixer_to_selector(mixer), function(x) { setNames( # extract relevant portions from EmiIndSubSec and vm_emiIndCCS_Sub, # call mselect(), but without the 'variable' column ( dimSums(mselect(EmiIndSubSec, x[setdiff(names(x), 'variable')]), dim = 3) - dimSums( - ( mselect(vm_emiIndCCS_Sub, x[setdiff(names(x), 'variable')]) + ( mselect(pm_IndstCO2Captured, x[setdiff(names(x), 'variable')]) * p_share_CCS ), dim = 3) ) * GtC_2_MtCO2, x[['variable']]) - }) %>% - mbind()) - - # Baseline emission before CCS, corresponds to energy carbon content - out <- mbind( - # energy emissions - subsector_emissions %>% - mutate( - secInd37 = case_when( - 'cement' == .data$secInd37 ~ 'Cement', - 'chemicals' == .data$secInd37 ~ 'Chemicals', - 'steel' == .data$secInd37 ~ 'Steel', - 'otherInd' == .data$secInd37 ~ 'Other Industry', - TRUE ~ NA_character_), - fety = case_when( - 'fesos' == .data$fety ~ 'Solids', - 'fehos' == .data$fety ~ 'Liquids', - 'fegas' == .data$fety ~ 'Gases', - TRUE ~ NA_character_), - origin = case_when( - grepl('fos$', .data$sety) ~ 'Fossil', - grepl('bio$', .data$sety) ~ 'Biomass', - grepl('syn$', .data$sety) ~ 'Hydrogen', - TRUE ~ NA_character_)) %>% - assert(not_na, everything()) %>% - group_by(.data$t, .data$regi, .data$secInd37, .data$fety, - .data$origin) %>% - summarise(value = sum(.data$subsector_emissions) - * as.numeric(GtC_2_MtCO2), - .groups = 'drop') %>% - mutate(d3 = paste0('Emi|CO2|pre-CCS|Energy|Demand|Industry|', - .data$secInd37, '|', .data$fety, '|', .data$origin, - ' (Mt CO2/yr)')) %>% - select('t', 'regi', 'd3', 'value') %>% - as.magpie(spatial = 2, temporal = 1, datacol = ncol(.)) %>% - `getSets<-`(fulldim = FALSE, value = getSets(out)), - - # process emissions - readGDX(gdx, 'vm_macBaseInd', field = 'l', restore_zeros = FALSE) %>% - `[`(,,'co2cement_process.cement') %>% - `*`(as.numeric(GtC_2_MtCO2)) %>% - `getSets<-`(fulldim = FALSE, value = getSets(out)) %>% - `getNames<-`( - value = 'Emi|CO2|pre-CCS|Industrial Processes|Cement (Mt CO2/yr)'), + }) %>% + mbind() + ) - out) + # Baseline emission before CCS, corresponds to energy carbon content + out <- mbind( + # energy emissions + subsector_emissions %>% + mutate( + secInd37 = case_when( + 'cement' == .data$secInd37 ~ 'Cement', + 'chemicals' == .data$secInd37 ~ 'Chemicals', + 'steel' == .data$secInd37 ~ 'Steel', + 'otherInd' == .data$secInd37 ~ 'Other Industry', + TRUE ~ NA_character_), + fety = case_when( + 'fesos' == .data$fety ~ 'Solids', + 'fehos' == .data$fety ~ 'Liquids', + 'fegas' == .data$fety ~ 'Gases', + TRUE ~ NA_character_), + origin = case_when( + grepl('fos$', .data$sety) ~ 'Fossil', + grepl('bio$', .data$sety) ~ 'Biomass', + grepl('syn$', .data$sety) ~ 'Hydrogen', + TRUE ~ NA_character_)) %>% + assert(not_na, everything()) %>% + group_by(.data$t, .data$regi, .data$secInd37, .data$fety, + .data$origin) %>% + summarise(value = sum(.data$subsector_emissions) + * as.numeric(GtC_2_MtCO2), + .groups = 'drop') %>% + mutate(d3 = paste0('Emi|CO2|pre-CCS|Energy|Demand|Industry|', + .data$secInd37, '|', .data$fety, '|', .data$origin, + ' (Mt CO2/yr)')) %>% + select('t', 'regi', 'd3', 'value') %>% + as.magpie(spatial = 2, temporal = 1, datacol = ncol(.)) %>% + `getSets<-`(fulldim = FALSE, value = getSets(out)), + + # process emissions + readGDX(gdx, 'vm_macBaseInd', field = 'l', restore_zeros = FALSE) %>% + `[`(,,'co2cement_process.cement') %>% + `*`(as.numeric(GtC_2_MtCO2)) %>% + `getSets<-`(fulldim = FALSE, value = getSets(out)) %>% + `getNames<-`( + value = 'Emi|CO2|pre-CCS|Industrial Processes|Cement (Mt CO2/yr)'), + + out) } } else { diff --git a/R/reportFE.R b/R/reportFE.R index dd6bb258..56d9604f 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1727,8 +1727,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(dimSums(mselect(vm_demFENonEnergySector, emi_sectors="indst",all_enty1="fegas", all_enty = "segasyn"), dim=3), "FE|Non-energy Use|Industry|Gases|+|Hydrogen (EJ/yr)") ) - } - ### FE without non-energy use out <- mbind(out, @@ -1838,7 +1836,7 @@ reportFE <- function(gdx, regionSubsetList = NULL, warning(e) } ) - + } ### FE variables without bunkers ---- From e346fd8e37554690428de456f41e06ac80715518 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 24 Nov 2023 17:08:45 +0100 Subject: [PATCH 28/31] delete spurious sets definitions --- R/reportFE.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 56d9604f..362cebe4 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -1375,9 +1375,6 @@ reportFE <- function(gdx, regionSubsetList = NULL, if ("DEU" %in% getRegions(vm_prodFe) & indu_mod == 'subsectors' & is.null(vm_demFENonEnergySector)) { - # some initializations required for building library with dplyr operations below - encar <- data <- value <- value_subsectors <- SSP <- Value_NonEn <- encar <- region <- period <- NULL - # read in FE industry non-energy use trajectories from industry subsectors run df.fe_nechem <- read.csv(system.file("extdata","pm_fe_nechem.cs4r",package = "remind2"), sep = ",", skip = 4, header = F) From 740212f3a47c4c6607dee193eb2dbf0f05150ef7 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 24 Nov 2023 17:19:28 +0100 Subject: [PATCH 29/31] un-do wrongly commented lines --- R/reportFE.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/reportFE.R b/R/reportFE.R index 362cebe4..4cf3535e 100644 --- a/R/reportFE.R +++ b/R/reportFE.R @@ -539,9 +539,9 @@ reportFE <- function(gdx, regionSubsetList = NULL, setNames(out[,,"FE|Transport|++|ESR (EJ/yr)"], "FE|Transport|w/o Bunkers (EJ/yr)"), setNames(out[,,"FE|Transport|++|Outside ETS and ESR (EJ/yr)"], "FE|Transport|Bunkers (EJ/yr)") ) - # out <- mbind(out, - # setNames(out[,,"FE (EJ/yr)"] - out[,,"FE|Transport|Bunkers (EJ/yr)"], "FE|w/o Bunkers (EJ/yr)") -# ) + out <- mbind(out, + setNames(out[,,"FE (EJ/yr)"] - out[,,"FE|Transport|Bunkers (EJ/yr)"], "FE|w/o Bunkers (EJ/yr)") + ) From 545affa46862bf18a0d0bb92f5505c600ea2771d Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Mon, 27 Nov 2023 11:59:29 +0100 Subject: [PATCH 30/31] modify redundant variables --- R/reportEmi.R | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 7e2da3a3..5df8ac46 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -93,10 +93,6 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 } se_sol <- c("sesofos", "sesobio") - # sets of hydrocarbon SE carriers - entySeFos <- readGDX(gdx, "entySeFos") - entySeBio <- readGDX(gdx, "entySeBio") - entySeSyn <- readGDX(gdx, "entySeSyn") # SE carriers by origin if (is.null(entySEfos <- readGDX(gdx, 'entySEfos', react = 'silent'))) @@ -357,7 +353,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 mutate( name = paste(all_enty,all_enty1,all_emiMkt, sep = ".")) plastic_CDR <- mselect(vm_nonIncineratedPlastics[,,FE.feed.map$name], - all_enty = unique(entySeBio, entySeSyn)) + all_enty = unique(entySEbio, entySEsyn)) } else { From 7e24a6d144d74d5d5ceae06b183155e69f990b07 Mon Sep 17 00:00:00 2001 From: Simon Moreno Date: Fri, 8 Dec 2023 14:51:48 +0100 Subject: [PATCH 31/31] include products EoL emissions to energy emissions to fix summation checks --- R/reportEmi.R | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/R/reportEmi.R b/R/reportEmi.R index 5df8ac46..e49bc500 100644 --- a/R/reportEmi.R +++ b/R/reportEmi.R @@ -498,10 +498,23 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 - dimSums(plastic_CDR, dim=3) )*GtC_2_MtCO2, "Emi|CO2|Energy|+|Demand (Mt CO2/yr)") - + ) - + # CO2 emissions from the end-of-life of carbon-bearing products +if (!is.null(vm_plasticsCarbon)) { + out <- mbind(out, + setNames(dimSums(vm_feedstockEmiUnknownFate, dim=3)* GtC_2_MtCO2, + "Emi|CO2|Energy|Waste|+|Feedstocks unknown fate (Mt CO2/yr)"), + setNames(dimSums(vm_incinerationEmi, dim=3)* GtC_2_MtCO2, + "Emi|CO2|Energy|Waste|+|Plastics Incineration (Mt CO2/yr)") + ) + out <- mbind(out, + setNames(out[, , "Emi|CO2|Energy|Waste|+|Feedstocks unknown fate (Mt CO2/yr)"] + + out[, , "Emi|CO2|Energy|Waste|+|Plastics Incineration (Mt CO2/yr)"], + "Emi|CO2|Energy|+|Waste (Mt CO2/yr)") + ) +} #### 2.1.2 Energy Supply ---- #### calculations required for coupled production @@ -1107,12 +1120,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 # calculate chemical process emissions from feedstocks treatment EmiFac_NonEn.co2 <- collapseDim(pm_emifacNonEnergy[,,"co2"]) EmiProcess_Feedstocks <- pm_emifacNonEnergy[,,"co2"] * vm_demFENonEnergySector[,,getNames(EmiFac_NonEn.co2)] - out <- mbind(out, - setNames(dimSums(vm_feedstockEmiUnknownFate, dim=3)* GtC_2_MtCO2, - "Emi|CO2|Feedstocks unknown fate (Mt CO2/yr)"), - setNames(dimSums(vm_incinerationEmi, dim=3)* GtC_2_MtCO2, - "Emi|CO2|Plastics incineration (Mt CO2/yr)") - ) + } else { # otherwise chemical process emissions are 0 EmiProcess_Feedstocks <- vm_co2eq * 0 @@ -1188,7 +1196,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 #### total energy and industry CO2 emissions - # Emi|CO2|Plastics incineration and Emi|CO2|Feedstocks unknown fate + # Emi|CO2|Energy|Waste|+|Plastics Incineration and Emi|CO2|Energy|Waste|+|Feedstocks unknown fate # are accounted for in vm_emiTeMkt and therefore in Emi|CO2|+|Energy (Mt CO2/yr) out <- mbind(out, setNames(out[, , "Emi|CO2|+|Energy (Mt CO2/yr)"] @@ -2525,7 +2533,7 @@ reportEmi <- function(gdx, output = NULL, regionSubsetList = NULL, t = c(seq(200 if (is.null(vm_demFENonEnergySector) && (module2realisation["industry", 2] == "fixed_shares")) { # (Note: The non-energy use variables are so far only available for REMIND-EU runs and industry fixed_shares) # TODO: add non-energy use variables for all regionmappings and sector realizations - #Note (SM): I'm not sure if I got these notes so I created the condition above to tra to make sure that this will work anyways + #Note (SM): I'm not sure if I got these notes so I created the condition above to try to make sure that this will work anyways # Note: Non-energy use emissions should not be confused with process emissions. Non-energy use emissions are emissions/carbon flow of FE carriers which are used as feedstocks in industry. if ("FE|Non-energy Use|Industry (EJ/yr)" %in% getNames(output) &&