From 2204bdb2757682d95321ffa1e264a798eb849ad7 Mon Sep 17 00:00:00 2001
From: Marianna Rottoli <marianna.rottoli@mail.polimi.it>
Date: Mon, 25 May 2020 09:56:09 +0200
Subject: [PATCH] Emissions plots in dashboard represent fossil VS electricity
 emissions.

---
 scripts/output/comparison/EDGEcomparison.R    | 56 ++++++------
 .../notebook_templates/EDGEdashboard.Rmd      | 13 ++-
 .../notebook_templates/helper_dashboard.R     | 90 ++++++++++---------
 3 files changed, 80 insertions(+), 79 deletions(-)

diff --git a/scripts/output/comparison/EDGEcomparison.R b/scripts/output/comparison/EDGEcomparison.R
index e0361a1..f2c3526 100644
--- a/scripts/output/comparison/EDGEcomparison.R
+++ b/scripts/output/comparison/EDGEcomparison.R
@@ -45,8 +45,8 @@ CO2km_int_newsales_all = NULL
 emidem_all = NULL
 EJfuelsPass_all = NULL
 EJfuelsFrgt_all = NULL
-emipdem_all = NULL
-emipUp_all = NULL
+emipfos_all = NULL
+emipFosEl_all = NULL
 
 scenNames <- getScenNames(outputdirs)
 EDGEdata_path  <- path(outputdirs, paste("EDGE-T/"))
@@ -332,25 +332,21 @@ emidemFun = function(emidem){
 }
 
 
-emidemPassFun = function(emidemPass){
-  emidemPass = emidemPass[region!="World" & year >= 2015 & year <= 2100]
-  emidemPass[, variable := as.character(variable)]
-  emidemPass[, c("model", "scenario", "variable", "unit") := NULL]
-  setnames(emidemPass, old = "value", new = "emi_sum")
-  emidemPass[, type := "Demand"]
-  return(emidemPass)
+emipfosFun = function(emipfos){
+  emipfos = emipfos[region!="World" & year >= 2015 & year <= 2100]
+  emipfos[, variable := as.character(variable)]
+  emipfos[, c("model", "scenario", "variable", "unit") := NULL]
+  setnames(emipfos, old = "value", new = "emi_sum")
+  emipfos[, type := "Fossil fuels"]
+  return(emipfos)
 }
 
-emipUpFun = function(prodSe, prodFe, vemi, pemi, pass_sm_el_emi){
-    prodSe <- readgdx(gdx, "vm_prodSe")[
-    tall >= minyr & tall <= maxyr]
-  prodFe <- readgdx(gdx, "vm_prodFe")[
-    ttot >= minyr & ttot <= maxyr]
+emipFosElFun = function(prodSe, prodFe, vemi, pemi, pass_sm_el_emi){
   
   prodFefos <- prodFe[all_te %in% c("tdfosdie", "tdfospet")]
   TWa_2_EJ <- 31.536
   GtC_2_MtCO2 <- 44 / 12 * 1000
-
+  
   prodSe[, value := value*TWa_2_EJ]
   prodFe[, value := value*TWa_2_EJ]
   
@@ -389,9 +385,9 @@ emipUpFun = function(prodSe, prodFe, vemi, pemi, pass_sm_el_emi){
                lname=c("biofuels", "fossil fuels", "synfuels")),
     on="se"]
   
-  ## Electricity production - not really important but interesting
+  ## Electricity production
   prodEl <- prodSe[se == "seel"]
-
+  
   ## Electricity consumption for H2 production
   prodEl[, allEl := sum(value), by=.(year, region)][
     , c("pe", "se", "te", "value") := NULL
@@ -450,8 +446,8 @@ emipUpFun = function(prodSe, prodFe, vemi, pemi, pass_sm_el_emi){
   pass_sm_el_emi = pass_sm_el_emi[year %in% unique(emi_all$year)]
   setnames(pass_sm_el_emi, c("region", "co2", "year", "se"))
   emi_all = rbind(emi_all, pass_sm_el_emi)
-  emi_all = emi_all[, .(emi_upstr = sum(co2)), by=.(year, region)]
-  emi_all[, type := "Upstream"]
+  emi_all = emi_all[, .(emi_tot = sum(co2)), by=.(year, region)]
+  emi_all[, type := "Total"]
   return(emi_all)
 }
 
@@ -494,7 +490,7 @@ for (outputdir in outputdirs) {
   ## select useful entries from mif file
   FEliq_source = miffile[variable %in% c("FE|Transport|Liquids|Biomass", "FE|Transport|Liquids|Hydrogen", "FE|Transport|Liquids|Coal", "FE|Transport|Liquids|Oil"),]
   emidem = miffile[variable %in% c("Emi|CO2|Transport|Demand"),]
-  emidemPass = miffile[variable %in% c("Emi|CO2|Transport|Pass|Short-Medium Distance|Demand"),]
+  emipassfos = miffile[variable %in% c("Emi|CO2|Transport|Pass|Short-Medium Distance|Liquids"),]
   pass_sm_el_emi = miffile[variable %in% c("Emi|CO2|Transport|Pass|Short-Medium Distance|Electricity")]
   ## modify mif file entries to be used in the functions
   FEliq_source = FEliq_sourceFun(FEliq_source, gdp)
@@ -520,10 +516,10 @@ for (outputdir in outputdirs) {
   EJfuelsFrgt = EJfuels[["demandEJfrgt"]]
   ## calculate demand emissions
   emidem = emidemFun(emidem)
-  ## calculate emissions from passenger demand SM
-  emipdem =  emidemPassFun(emidemPass)
-  ## calculate upstream emissions (accounting for synfuels, hydrogen and electricity production)
-  emipUp = emipUpFun(prodSe, prodFe, vemi, pemi, pass_sm_el_emi)
+  ## calculate emissions from passenger SM fossil fuels (liquids)
+  emipfos =  emipfosFun(emipassfos)
+  ## calculate upstream emissions (accounting for fossils, synfuels, hydrogen and electricity production)
+  emipFosEl = emipFosElFun(prodSe, prodFe, vemi, pemi, pass_sm_el_emi)
   ## add scenario dimension to the results
   fleet[, scenario := as.character(unique(miffile$scenario))]
   salescomp[, scenario := unique(miffile$scenario)]
@@ -535,8 +531,8 @@ for (outputdir in outputdirs) {
   emidem[, scenario := as.character(unique(miffile$scenario))]
   EJfuelsPass[, scenario := as.character(unique(miffile$scenario))]
   EJfuelsFrgt[, scenario := as.character(unique(miffile$scenario))]
-  emipdem[, scenario := as.character(unique(miffile$scenario))]
-  emipUp[, scenario := as.character(unique(miffile$scenario))]
+  emipfos[, scenario := as.character(unique(miffile$scenario))]
+  emipFosEl[, scenario := as.character(unique(miffile$scenario))]
   ## rbind scenarios
   salescomp_all = rbind(salescomp_all, salescomp)
   fleet_all = rbind(fleet_all, fleet)
@@ -548,8 +544,8 @@ for (outputdir in outputdirs) {
   emidem_all = rbind(emidem_all, emidem)
   EJfuelsPass_all = rbind(EJfuelsPass_all, EJfuelsPass)
   EJfuelsFrgt_all = rbind(EJfuelsFrgt_all, EJfuelsFrgt)
-  emipdem_all = rbind(emipdem_all, emipdem)
-  emipUp_all = rbind(emipUp_all, emipUp)
+  emipfos_all = rbind(emipfos_all, emipfos)
+  emipFosEl_all = rbind(emipFosEl_all, emipFosEl)
 }
 
 outdir = paste0("output/comparerunEDGE", gsub(" | ^([[:alpha:]]*).*","", Sys.time()))
@@ -567,8 +563,8 @@ saveRDS(CO2km_int_newsales_all, paste0(outdir, "/CO2km_int_newsales_all.RDS"))
 saveRDS(emidem_all, paste0(outdir, "/emidem_all.RDS"))
 saveRDS(EJfuelsPass_all, paste0(outdir, "/EJfuelsPass_all.RDS"))
 saveRDS(EJfuelsFrgt_all, paste0(outdir, "/EJfuelsFrgt_all.RDS"))
-saveRDS(emipdem_all, paste0(outdir, "/emipdem_all.RDS"))
-saveRDS(emipUp_all, paste0(outdir, "/emipUp_all.RDS"))
+saveRDS(emipfos_all, paste0(outdir, "/emipfos_all.RDS"))
+saveRDS(emipFosEl_all, paste0(outdir, "/emipFosEl_all.RDS"))
 file.copy(file.path("./scripts/output/comparison/notebook_templates", md_template), outdir)
 rmarkdown::render(path(outdir, md_template), output_format="pdf_document")
 
diff --git a/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd b/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd
index d7a8233..c722c98 100644
--- a/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd
+++ b/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd
@@ -38,7 +38,7 @@ legend=plotlist$legend
   #data frame with help tooltips
 
   helpTooltip_df <- data.frame(
-    title=c("Per capita Passenger Transport Energy Services Demand", "Total Passenger Transport Energy Services Demand", "Sales composition", "Final energy LDVs by fuel","Transport Passenger Final Energy Demand", "Fleet composition", "Fleet composition comparison", "Emission intensity, new sales comparison", "Comparison of passenger final energy demand", "Emissions passenger transport", "Emission intensity of new sales", "Comparison of sales composition", "Comparison of passenger demand emissions", "Comparison of passenger upstream emissions"),
+    title=c("Per capita Passenger Transport Energy Services Demand", "Total Passenger Transport Energy Services Demand", "Sales composition", "Final energy LDVs by fuel","Transport Passenger Final Energy Demand", "Fleet composition", "Fleet composition comparison", "Emission intensity, new sales comparison", "Comparison of passenger final energy demand", "Emissions passenger transport", "Emission intensity of new sales", "Comparison of sales composition", "Comparison of passenger transport fossil fuels emissions", "Comparison of passenger electricity production emissions"),
     placement=c("right", "left", "right", "left", "left", "left", "right", "left", "right", "left", "left", "left", "left", "left"))
 
 
@@ -756,10 +756,10 @@ plotlist$comparison$plot$vintscen
 plotlist$comparison$plot$CO2km_intensity_newsales_scen
 ```
 
-### Comparison of passenger demand emissions
+### Comparison of passenger transport fossil fuels emissions
 
 ```{r}
-plotlist$comparison$plot$emipdem_scen
+plotlist$comparison$plot$emipfos_scen
 ```
 
 Row {data-height = 450}
@@ -775,9 +775,9 @@ plotlist$comparison$plot$EJpassfuels_scen
 plotlist$comparison$plot$salescom_scen
 ```
 
-### Comparison of passenger upstream emissions
+### Comparison of passenger electricity production emissions
 ```{r}
-plotlist$comparison$plot$emipup_scen
+plotlist$comparison$plot$emipel_scen
 ```
 
 
@@ -789,8 +789,6 @@ Column {data-width= 450}
 
 ### Conventional Case (NoTax) {data-height=200}
 
-<!--An overview of the passenger transport sector is provided in [Overview], while a detailed results about light duty vehicles projections are in [LDVs]. -->
-
 * Conventional consumers patterns
 * No policies to promote alternative vehicles
 * Pessimistic trend of electric recharging stations
@@ -830,6 +828,7 @@ Column {data-width= 450}
 * Market-driven behavior of light duty vehicles powertrain choice
 * Synfuels are forced in the liquids mix reaching 10\% of liquids fuels in transportation by 2035
 * Hydrogen from renewable resources (green hydrogen) is at least 95% of the total hydrogen
+* Emissions of CO~2~ are captured and reused to produce syntethic fuels (Carbon Capture and Utilization)
 
 
 <!-- creating information tooltip -->
diff --git a/scripts/output/comparison/notebook_templates/helper_dashboard.R b/scripts/output/comparison/notebook_templates/helper_dashboard.R
index 8b83121..7ea8458 100644
--- a/scripts/output/comparison/notebook_templates/helper_dashboard.R
+++ b/scripts/output/comparison/notebook_templates/helper_dashboard.R
@@ -70,9 +70,9 @@ cols <- c("NG" = "#d11141",
           "ConvCaseNoTax" = "#d11141",
           "ConvCaseWise" = "#d11141",
           "SynSurge" = "orchid",
-          "Tailpipe" = "#113245",
-          "Tailpipe+Energy system" = "#f37735",
-          "Energy system" = "#6495ed")
+          "Fossil fuels" = "#113245",
+          "Fossil fuels + Electricity production" = "#f37735",
+          "Electricity production" = "#6495ed")
 
 legend_ord_modes <- c("Freight Rail", "Truck", "Shipping", "International Shipping", "Domestic Shipping",  "Trucks",
                       "Motorbikes", "Small Cars", "Large Cars", "Van",
@@ -96,8 +96,8 @@ ESmodecap_all = readRDS("ESmodecap_all.RDS")
 ESmodeabs_all = readRDS("ESmodeabs_all.RDS")
 CO2km_int_newsales_all = readRDS("CO2km_int_newsales_all.RDS")
 EJpass_all = readRDS("EJfuelsPass_all.RDS")
-emipdem_all = readRDS("emipdem_all.RDS")
-emipUp_all = readRDS("emipUp_all.RDS")
+emipfos_all = readRDS("emipfos_all.RDS")
+emipFosEl_all = readRDS("emipFosEl_all.RDS")
 
 ## scenarios
 scens = unique(EJmode_all$scenario)
@@ -551,33 +551,32 @@ EJLDVdash <- function(dt, scen){
   
 }
 
-emip_dash = function(dt1, dt_upstr1, scen){
-    dt = copy(dt1)
-  dt_upstr = copy(dt_upstr1)
+emip_dash = function(dt1, dt_tot1, scen){
+  dt = copy(dt1)
+  dt_tot = copy(dt_tot1)
   setnames(dt, old = "emi_sum", new = "value")
-  setnames(dt_upstr, old = "emi_upstr", new = "value")
-  dt = rbind(dt, dt_upstr)
+  setnames(dt_tot, old = "emi_tot", new = "value")
+  dt = rbind(dt, dt_tot)
   dt[, year:= as.numeric(year)]
   dt = dt[region == region_plot & scenario == scen & year <= 2050 & year >= 2020]
   dt = dcast(dt, region + year + scenario  ~ type, value.var = "value")
   
-  dt[, diff := Upstream - Demand]
+  dt[, diff := Total - `Fossil fuels`]
   dt = melt(dt, id.vars = c("region", "year", "scenario"))
   setnames(dt, old = "variable", new = "type")
-  dt[type == "diff", type := "Energy system"]
-  dt[type == "Upstream", type := "Tailpipe+Energy system"]
-  dt[type == "Demand", type := "Tailpipe"]
+  dt[type == "diff", type := "Electricity production"]
+  dt[type == "Total", type := "Fossil fuels + Electricity production"]
   
   dt[, details := paste0("Emissions: ", round(value, digits = 0), " [MtCO<sub>2</sub>]", "<br>", "Type: ", type, "<br>", "Region: ", region," <br>", "Year: ", year) ] 
   
   
   plot = ggplot()+
-    geom_area(data = dt[year >= 2020 & type == "Tailpipe+Energy system"], aes(x = year, y = value, text = details, fill =type, group = type), alpha = 0.4, position = position_stack())+
-    geom_line(data = dt[year >= 2020 & type != "Tailpipe+Energy system"], aes(x = year, y = value, text = details, group = type, color = type))+
+    geom_area(data = dt[year >= 2020 & type == "Fossil fuels + Electricity production"], aes(x = year, y = value, text = details, fill =type, group = type), alpha = 0.4, position = position_stack())+
+    geom_line(data = dt[year >= 2020 & type != "Fossil fuels + Electricity production"], aes(x = year, y = value, text = details, group = type, color = type))+
     labs(x = "", y = "")+
     theme_minimal()+
     expand_limits(y = c(0,1))+
-    # ylim(0,1400)+
+    ylim(0,1800)+
     scale_x_continuous(breaks = c(2015, 2030, 2050))+
     theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
@@ -600,29 +599,36 @@ emip_dash = function(dt1, dt_upstr1, scen){
   
 }
 
-emipcom_dash = function(dt1, dt_upstr1){
+emipscen_dash = function(dt1, dt_tot1){
   dt = copy(dt1)
-  dt_upstr = copy(dt_upstr1)
+  dt_tot = copy(dt_tot1)
   setnames(dt, old = "emi_sum", new = "value")
-  setnames(dt_upstr, old = "emi_upstr", new = "value")
-  dt = rbind(dt, dt_upstr)
-
+  setnames(dt_tot, old = "emi_tot", new = "value")
+  dt = rbind(dt, dt_tot)
+  
   dt = dt[region == region_plot & year <= 2050 & year >= 2020]
   dt[, year := as.numeric(year)]
   
   dt[, scenario := ifelse(scenario == "Base_ConvCase", "ConvCaseNoTax", scenario)]
   dt[, scenario := gsub(".*_", "", scenario)]
   
-  dt[, details := scenario ] 
   
   
+  dt = dcast(dt, region + year + scenario  ~ type, value.var = "value")
+  
+  dt[, diff := Total - `Fossil fuels`]
+  dt = melt(dt, id.vars = c("region", "year", "scenario"))
+  setnames(dt, old = "variable", new = "type")
+  dt[type == "diff", type := "Electricity production"]
+  dt[type == "Total", type := "Fossil fuels + Electricity production"]
+  dt[, details := scenario ] 
   
-  pdem = ggplot()+
-    geom_line(data = dt[type == "Demand"], aes(x = year, y = value, text = details, group = scenario, color = scenario))+
+  pfos = ggplot()+
+    geom_line(data = dt[type == "Fossil fuels"], aes(x = year, y = value, text = details, group = scenario, color = scenario))+
     labs(x = "", y = "")+
     theme_minimal()+
     expand_limits(y = c(0,1))+
-    ylim(0,1400)+
+    ylim(0,1800)+
     scale_x_continuous(breaks = c(2015, 2030, 2050))+
     theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
@@ -634,19 +640,19 @@ emipcom_dash = function(dt1, dt_upstr1){
           strip.background = element_rect(color = "grey"))+
     scale_color_manual(values = cols)
   
-  pdem = ggplotly(pdem, tooltip = c("text")) %>%
+  pfos = ggplotly(pfos, tooltip = c("text")) %>%
     config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) %>%
     layout(yaxis=list(title='[MtCO<sub>2</sub>]', titlefont = list(size = 10)))
   
   vars = as.character(unique(dt$scenario))
   
   
-  pup = ggplot()+
-    geom_line(data = dt[type == "Upstream"], aes(x = year, y = value, text = scenario, group = scenario, color = scenario))+
+  pel = ggplot()+
+    geom_line(data = dt[type == "Electricity production"], aes(x = year, y = value, text = scenario, group = scenario, color = scenario))+
     labs(x = "", y = "")+
     theme_minimal()+
     expand_limits(y = c(0,1))+
-    # ylim(0,1400)+
+    ylim(0, 1000)+
     scale_x_continuous(breaks = c(2015, 2030, 2050))+
     theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
@@ -658,11 +664,11 @@ emipcom_dash = function(dt1, dt_upstr1){
           strip.background = element_rect(color = "grey"))+
     scale_color_manual(values = cols)
   
-  pup = ggplotly(pup, tooltip = c("text")) %>%
+  pel = ggplotly(pel, tooltip = c("text")) %>%
     config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) %>%
     layout(yaxis=list(title='[MtCO<sub>2</sub>]', titlefont = list(size = 10)))
   
-  plot = list(pdem = pdem, pup = pup, vars = vars)
+  plot = list(pfos = pfos, pel = pel, vars = vars)
   
   return(plot)
   
@@ -771,7 +777,7 @@ create_plotlist = function(scens, salescomp_all, fleet_all, ESmodecap_all, EJfue
     ## final energy LDVs by fuel
     EJLDV = EJLDVdash(EJroad_all, scen)
     ## emissions passenger transport demand and upstream emissions
-    emip = emip_dash(emipdem_all, emipUp_all, scen)
+    emip = emip_dash(emipfos_all, emipFosEl_all, scen)
     
     ## collect plots
     output[[scenname]]$plot$vintcomp = vintcomp$plot
@@ -796,15 +802,15 @@ create_plotlist = function(scens, salescomp_all, fleet_all, ESmodecap_all, EJfue
   ## sales
   salescom_scen = salescom_scen_dash(salescomp_all)
   ## emissions
-  emip_scen = emipcom_dash(emipdem_all, emipUp_all)
+  emip_scen = emipscen_dash(emipfos_all, emipFosEl_all)
   
   
   output[["comparison"]]$plot$vintscen = vintscen$plot
   output[["comparison"]]$plot$CO2km_intensity_newsales_scen = CO2km_intensity_newsales_scen$plot
   output[["comparison"]]$plot$EJpassfuels_scen = EJpassfuels_scen$plot
   output[["comparison"]]$plot$salescom_scen = salescom_scen$plot
-  output[["comparison"]]$plot$emipdem_scen = emip_scen$pdem
-  output[["comparison"]]$plot$emipup_scen = emip_scen$pup
+  output[["comparison"]]$plot$emipfos_scen = emip_scen$pfos
+  output[["comparison"]]$plot$emipel_scen = emip_scen$pel
   
   
   
@@ -861,13 +867,13 @@ create_plotlist = function(scens, salescomp_all, fleet_all, ESmodecap_all, EJfue
   names(legend$'Comparison of sales composition'$contents) <- salescom_scen$vars
   legend$'Comparison of sales composition'$description <- "<p>Composition of sales of light duty vehicles in selected years, in percentage</p>"
   
-  legend$'Comparison of passenger demand emissions'$contents <- lapply(emip_scen$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) })
-  names(legend$'Comparison of passenger demand emissions'$contents) <- emip_scen$vars
-  legend$'Comparison of passenger demand emissions'$description <- "<p>Comparison of emissions from passenger transport demand across scenarios</p>"
+  legend$'Comparison of passenger transport fossil fuels emissions'$contents <- lapply(emip_scen$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) })
+  names(legend$'Comparison of passenger transport fossil fuels emissions'$contents) <- emip_scen$vars
+  legend$'Comparison of passenger transport fossil fuels emissions'$description <- "<p>Comparison of emissions from passenger transport fossil fuels across scenarios</p>"
   
-  legend$'Comparison of passenger upstream emissions'$contents <- lapply(emip_scen$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) })
-  names(legend$'Comparison of passenger upstream emissions'$contents) <- emip_scen$vars
-  legend$'Comparison of passenger upstream emissions'$description <- "<p>Comparison of emissions from passenger transport upstream across scenarios</p>"
+  legend$'Comparison of passenger electricity production emissions'$contents <- lapply(emip_scen$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) })
+  names(legend$'Comparison of passenger electricity production emissions'$contents) <- emip_scen$vars
+  legend$'Comparison of passenger electricity production emissions'$description <- "<p>Comparison of emissions from passenger transport electricity production across scenarios</p>"
   
   output$legend = legend
   return(output)
-- 
GitLab