---
title: 'full code'
output:
  html_document:
    toc: yes
    df_print: paged
  bookdown::html_document2:
    toc: yes
    toc_float: yes
---

# Setup

This file contains all code to estimate the European income-stratified footprints and then the European expenditures deciles. There are three code chunks in this 'full code' Rmarkdown file. The first shows the EXIOBASE code, the second the income-stratified-footprints using the EUROSTAT HBS, and the third the estimation of the European expenditure deciles. All three code chunks are currently set to 'eval = FALSE', and the necessary raw data files to run them are not included in the git due to their size. We have also run the first two code chunks on a high-performance cluster computer.

The code in this 'full code' Rmarkdown file writes derived data files to the folder: 'analysis' > 'data' > 'derived'. These files can be accessed there, and own analysis performed on them without running any of the code in this Rmarkdown. The derived data files are used to create the figures in the main paper and SI (the code for the figures can be found directly within the paper and si Rmarkdown files: 'analysis' > 'paper').  

To run the code in this 'full code' Rmarkdown file, follow the instructions at the start of each section (before the code chunk) explaining which files must be downloaded from where, and in which folder they should be extracted. Then remove 'eval = FALSE' in the code chunk header before running it. 

```{r setup, echo = FALSE, include = FALSE, message = FALSE}

# first load required R packages

knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  message = FALSE,
  echo = FALSE,
  comment = "#>"
)

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,
               janitor,
               readr,
               here,
               wbstats,
               ISOcodes,
               viridis,
               imputeTS,
               hrbrthemes,
               wesanderson,
               glue,
               ggridges,
               patchwork)

```

# EXIOBASE

The EXIOBASE files are publicly available online. For the results in the main paper we use EXIOBASE version3 industry-by-industry, which is available from: https://zenodo.org/record/3583071#.XjC7kSN4wpY [accessed on 12.03.2020]. In the SI, we also show some results from using EXIOBASE version3 product-by-product, which is available from:  [accessed on 12.03.2020]. 

These files are large, global input-output tables, and we performed standard input-output calculations on them to calculate and save total intensity vectors using a high-performance cluster computer. In this document we show the code that was run on the cluster computer, but have not uploaded any EXIOBASE files to the git. Running the first code chunk would require downloading the industry-by-industry version for the years 2005, 2010 and 2015, and the product-by-product version for the years 2005 and 2010. 

Each year is available as a .zip file ('IOT_year_ixi' or 'IOT_year_pxp') from the websites above. If the .zip files for the relevant study years and versions have been downloaded, they can be extracted into the 'EXIOBASE' folder in this git, which is found in the 'analysis' > 'preprocessing' folder. We have set 'EXIOBASE' empty for this purpose. Extracting each year into the EXIOBASE folder creates a folder for each year with all of the relevant files for that year and version. All code in this first code chunk leaves all file names as they are after extraction. 

```{r exiobase, eval = FALSE}

# set data directory for EXIOBASE files
data_dir_exiobase = here("analysis", "preprocessing", "EXIOBASE")

##### EXIOBASE industry-by-industry version

# set study years
years_exiobase_ixi = c(2005,2010,2015)

# 'for' loop which writes 'total intensity vectors' (and row-wise breakdowns) for all study satellite extensions and years to 'data_dir_exiobase' using downloaded EXIOBASE files
for (i in years_exiobase_ixi){
  
year_current = i

# read in A table as table
A = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/A.txt"),header = F)

# write A table as .csv file
write.csv(A, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/A.csv"))

# read in A table as .csv file and extract the data only (no labels), and convert to numeric
A = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/A.csv",sep = ""),row.names=NULL,as.is=TRUE)[4:7990,4:7990]
A[is.na(A)]=0
A = mapply(A, FUN = as.numeric)
A = matrix(data = A, ncol = 7987, nrow = 7987)

# solve the Leontief inverse
L = solve(diag(dim(A)[1])-A)  
L[is.na(L)]=0

# read in final demand table (Y) as table
FD = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/Y.txt"),header = F)

# write final demand table (Y) as .csv file
write.csv(FD, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/Y.csv"))

# read in final demand table as .csv file and extract the production sector labels
Exiobase_T_labels_ixi = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/Y.csv"))[4:7990,1:3]

# write a .csv file with only the production sector labels
write.csv(Exiobase_T_labels_ixi, paste0(data_dir_exiobase, "/Exiobase_T_labels_ixi.csv"))

# read in final demand table as .csv file and extract the final demand category labels
Exiobase_FD_labels_ixi = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/Y.csv"))[1:3,4:346]

# write a .csv file with only the final demand category labels
write.csv(Exiobase_FD_labels_ixi, paste0(data_dir_exiobase, "/Exiobase_FD_labels_ixi.csv"))

# read in final demand table as .csv file and extract the data only (no labels), and convert to numeric
FD = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/Y.csv",sep=""),row.names=NULL,as.is=TRUE)[4:7990,4:346]
FD[is.na(FD)]=0
FD = mapply(FD, FUN = as.numeric)
FD = matrix(data=FD,ncol=343,nrow=7987)

# write a .csv file with final demand data only (no labels)
write.csv(FD, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/FD_",year_current,"_ixi.csv"))

# calculate total output
total_output = L %*% rowSums(FD)

# write total output as a .csv file
write.csv(total_output, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/total_output_",year_current,"_ixi.csv"))

# read in satellite extensions table (F) as table
satellite = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/satellite/F.txt"),header = F)

# write satellite extensions table (F) as .csv file
write.csv(satellite, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/satellite/F.csv"))

# read in satellite extensions table (F) as .csv file and extract the data only (no labels), and convert to numeric
satellite = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/satellite/F.csv",sep=""),row.names=NULL,as.is=TRUE)[3:1115,3:7989]
satellite[is.na(satellite)]=0
satellite = mapply(satellite, FUN = as.numeric)
satellite = matrix(data=satellite,ncol=7987,nrow=1113)

# write a .csv file with satellite extensions data only (no labels)
write.csv(satellite, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/satellite/satellite_",year_current,"_ixi.csv"))

# read in satellite extensions on final demand table (F_hh) as table
satellite_FD = read.delim(paste0(data_dir_exiobase, "/IOT_", year_current, "_ixi/satellite/F_hh.txt"),header = F)

# write satellite extensions on final demand table (F_hh) as .csv file
write.csv(satellite_FD, paste0(data_dir_exiobase, "/IOT_", year_current, "_ixi/satellite/F_hh.csv"))

## extract the relevant satellite extensions from the satellite table, calculate the 'total intensity 
## vectors' (and their row-wise breakdowns), and save to 'data_dir_exiobase'

# CO2 - combustion - air
CO2_combustion_air = satellite[24,]
DIV_co2_combustion_air = CO2_combustion_air/total_output
DIV_co2_combustion_air[is.na(DIV_co2_combustion_air)]=0
DIV_co2_combustion_air[DIV_co2_combustion_air == Inf]<-0
TIV_co2_combustion_air = as.vector(DIV_co2_combustion_air) %*% L

write.csv(TIV_co2_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_combustion_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_combustion_air = as.vector(DIV_co2_combustion_air) * L
TIV_breakdown_co2_combustion_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_combustion_air)
TIV_country_breakdown_co2_combustion_air_w_labels = t(TIV_breakdown_co2_combustion_air_w_labels %>%
  group_by(V1) %>%
  select(-X,-V2) %>%
  summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_combustion_air_",year_current,"_ixi.csv"))

# CO2 - non-combustion - air
## cement
CO2_noncombustion_cement_air = satellite[93,]
DIV_co2_noncombustion_cement_air = CO2_noncombustion_cement_air/total_output
DIV_co2_noncombustion_cement_air[is.na(DIV_co2_noncombustion_cement_air)]=0
DIV_co2_noncombustion_cement_air[DIV_co2_noncombustion_cement_air == Inf]<-0
TIV_co2_noncombustion_cement_air = as.vector(DIV_co2_noncombustion_cement_air) %*% L

write.csv(TIV_co2_noncombustion_cement_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_noncombustion_cement_air = as.vector(DIV_co2_noncombustion_cement_air) * L
TIV_breakdown_co2_noncombustion_cement_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_noncombustion_cement_air)
TIV_country_breakdown_co2_noncombustion_cement_air_w_labels = t(TIV_breakdown_co2_noncombustion_cement_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_noncombustion_cement_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))

## lime
CO2_noncombustion_lime_air = satellite[94,]
DIV_co2_noncombustion_lime_air = CO2_noncombustion_lime_air/total_output
DIV_co2_noncombustion_lime_air[is.na(DIV_co2_noncombustion_lime_air)]=0
DIV_co2_noncombustion_lime_air[DIV_co2_noncombustion_lime_air == Inf]<-0
TIV_co2_noncombustion_lime_air = as.vector(DIV_co2_noncombustion_lime_air) %*% L

write.csv(TIV_co2_noncombustion_lime_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_noncombustion_lime_air = as.vector(DIV_co2_noncombustion_lime_air) * L
TIV_breakdown_co2_noncombustion_lime_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_noncombustion_lime_air)
TIV_country_breakdown_co2_noncombustion_lime_air_w_labels = t(TIV_breakdown_co2_noncombustion_lime_air_w_labels %>%
                                                                  group_by(V1) %>%
                                                                  select(-X,-V2) %>%
                                                                  summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_noncombustion_lime_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))

# CO2 - agriculture - peat decay - air
CO2_agriculture_peatdecay_air = satellite[428,]
DIV_co2_agriculture_peatdecay_air = CO2_agriculture_peatdecay_air/total_output
DIV_co2_agriculture_peatdecay_air[is.na(DIV_co2_agriculture_peatdecay_air)]=0
DIV_co2_agriculture_peatdecay_air[DIV_co2_agriculture_peatdecay_air == Inf]<-0
TIV_co2_agriculture_peatdecay_air = as.vector(DIV_co2_agriculture_peatdecay_air) %*% L

write.csv(TIV_co2_agriculture_peatdecay_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_agriculture_peatdecay_air = as.vector(DIV_co2_agriculture_peatdecay_air) * L
TIV_breakdown_co2_agriculture_peatdecay_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_agriculture_peatdecay_air)
TIV_country_breakdown_co2_agriculture_peatdecay_air_w_labels = t(TIV_breakdown_co2_agriculture_peatdecay_air_w_labels %>%
                                                                  group_by(V1) %>%
                                                                  select(-X,-V2) %>%
                                                                  summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_agriculture_peatdecay_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))

# CO2 - waste - air
## biogenic
CO2_waste_biogenic_air = satellite[438,]
DIV_co2_waste_biogenic_air = CO2_waste_biogenic_air/total_output
DIV_co2_waste_biogenic_air[is.na(DIV_co2_waste_biogenic_air)]=0
DIV_co2_waste_biogenic_air[DIV_co2_waste_biogenic_air == Inf]<-0
TIV_co2_biogenic_air = as.vector(DIV_co2_waste_biogenic_air) %*% L

write.csv(TIV_co2_biogenic_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_biogenic_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_biogenic_air = as.vector(DIV_co2_waste_biogenic_air) * L
TIV_breakdown_co2_biogenic_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_biogenic_air)
TIV_country_breakdown_co2_biogenic_air_w_labels = t(TIV_breakdown_co2_biogenic_air_w_labels %>%
                                                                   group_by(V1) %>%
                                                                   select(-X,-V2) %>%
                                                                   summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_biogenic_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_biogenic_air_",year_current,"_ixi.csv"))

## fossil
CO2_waste_fossil_air = satellite[439,]
DIV_co2_waste_fossil_air = CO2_waste_fossil_air/total_output
DIV_co2_waste_fossil_air[is.na(DIV_co2_waste_fossil_air)]=0
DIV_co2_waste_fossil_air[DIV_co2_waste_fossil_air == Inf]<-0
TIV_co2_waste_fossil_air = as.vector(DIV_co2_waste_fossil_air) %*% L

write.csv(TIV_co2_waste_fossil_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_waste_fossil_air_",year_current,"_ixi.csv"))

TIV_breakdown_co2_waste_fossil_air = as.vector(DIV_co2_waste_fossil_air) * L
TIV_breakdown_co2_waste_fossil_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_co2_waste_fossil_air)
TIV_country_breakdown_co2_waste_fossil_air_w_labels = t(TIV_breakdown_co2_waste_fossil_air_w_labels %>%
                                                                   group_by(V1) %>%
                                                                   select(-X,-V2) %>%
                                                                   summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_waste_fossil_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_ixi.csv"))

# CH4 - combustion - air
CH4_combustion_air = satellite[25,]
CH4_combustion_air = CH4_combustion_air*28
DIV_ch4_combustion_air = CH4_combustion_air/total_output
DIV_ch4_combustion_air[is.na(DIV_ch4_combustion_air)]=0
DIV_ch4_combustion_air[DIV_ch4_combustion_air == Inf]<-0
TIV_ch4_combustion_air = as.vector(DIV_ch4_combustion_air) %*% L

write.csv(TIV_ch4_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_combustion_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_combustion_air = as.vector(DIV_ch4_combustion_air) * L
TIV_breakdown_ch4_combustion_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_combustion_air)
TIV_country_breakdown_ch4_combustion_air_w_labels = t(TIV_breakdown_ch4_combustion_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_combustion_air_",year_current,"_ixi.csv"))

# CH4 - non-combustion - air
## gas
CH4_noncombustion_gas_air = satellite[68,]
CH4_noncombustion_gas_air = CH4_noncombustion_gas_air*28
DIV_ch4_noncombustion_gas_air = CH4_noncombustion_gas_air/total_output
DIV_ch4_noncombustion_gas_air[is.na(DIV_ch4_noncombustion_gas_air)]=0
DIV_ch4_noncombustion_gas_air[DIV_ch4_noncombustion_gas_air == Inf]<-0
TIV_ch4_noncombustion_gas_air = as.vector(DIV_ch4_noncombustion_gas_air) %*% L

write.csv(TIV_ch4_noncombustion_gas_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_gas_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_gas_air = as.vector(DIV_ch4_noncombustion_gas_air) * L
TIV_breakdown_ch4_noncombustion_gas_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_gas_air)
TIV_country_breakdown_ch4_noncombustion_gas_air_w_labels = t(TIV_breakdown_ch4_noncombustion_gas_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_gas_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_",year_current,"_ixi.csv"))

## oil
CH4_noncombustion_oil_air = satellite[69,]
CH4_noncombustion_oil_air = CH4_noncombustion_oil_air*28
DIV_ch4_noncombustion_oil_air = CH4_noncombustion_oil_air/total_output
DIV_ch4_noncombustion_oil_air[is.na(DIV_ch4_noncombustion_oil_air)]=0
DIV_ch4_noncombustion_oil_air[DIV_ch4_noncombustion_oil_air == Inf]<-0
TIV_ch4_noncombustion_oil_air = as.vector(DIV_ch4_noncombustion_oil_air) %*% L

write.csv(TIV_ch4_noncombustion_oil_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oil_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_oil_air = as.vector(DIV_ch4_noncombustion_oil_air) * L
TIV_breakdown_ch4_noncombustion_oil_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_oil_air)
TIV_country_breakdown_ch4_noncombustion_oil_air_w_labels = t(TIV_breakdown_ch4_noncombustion_oil_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_oil_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_",year_current,"_ixi.csv"))

## anthracite
CH4_noncombustion_anthracite_air = satellite[70,]
CH4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air*28
DIV_ch4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air/total_output
DIV_ch4_noncombustion_anthracite_air[is.na(DIV_ch4_noncombustion_anthracite_air)]=0
DIV_ch4_noncombustion_anthracite_air[DIV_ch4_noncombustion_anthracite_air == Inf]<-0
TIV_ch4_noncombustion_anthracite_air = as.vector(DIV_ch4_noncombustion_anthracite_air) %*% L

write.csv(TIV_ch4_noncombustion_anthracite_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_anthracite_air = as.vector(DIV_ch4_noncombustion_anthracite_air) * L
TIV_breakdown_ch4_noncombustion_anthracite_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_anthracite_air)
TIV_country_breakdown_ch4_noncombustion_anthracite_air_w_labels = t(TIV_breakdown_ch4_noncombustion_anthracite_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_anthracite_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_ixi.csv"))

## bituminous coal
CH4_noncombustion_bituminouscoal_air = satellite[71,]
CH4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air*28
DIV_ch4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air/total_output
DIV_ch4_noncombustion_bituminouscoal_air[is.na(DIV_ch4_noncombustion_bituminouscoal_air)]=0
DIV_ch4_noncombustion_bituminouscoal_air[DIV_ch4_noncombustion_bituminouscoal_air == Inf]<-0
TIV_ch4_noncombustion_bituminouscoal_air = as.vector(DIV_ch4_noncombustion_bituminouscoal_air) %*% L

write.csv(TIV_ch4_noncombustion_bituminouscoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_bituminouscoal_air = as.vector(DIV_ch4_noncombustion_bituminouscoal_air) * L
TIV_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_bituminouscoal_air)
TIV_country_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_ixi.csv"))

## coking coal
CH4_noncombustion_cokingcoal_air = satellite[72,]
CH4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air*28
DIV_ch4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air/total_output
DIV_ch4_noncombustion_cokingcoal_air[is.na(DIV_ch4_noncombustion_cokingcoal_air)]=0
DIV_ch4_noncombustion_cokingcoal_air[DIV_ch4_noncombustion_cokingcoal_air == Inf]<-0
TIV_ch4_noncombustion_cokingcoal_air = as.vector(DIV_ch4_noncombustion_cokingcoal_air) %*% L

write.csv(TIV_ch4_noncombustion_cokingcoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_cokingcoal_air = as.vector(DIV_ch4_noncombustion_cokingcoal_air) * L
TIV_breakdown_ch4_noncombustion_cokingcoal_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_cokingcoal_air)
TIV_country_breakdown_ch4_noncombustion_cokingcoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_cokingcoal_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_cokingcoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_ixi.csv"))

## lignite
CH4_noncombustion_lignite_air = satellite[73,]
CH4_noncombustion_lignite_air = CH4_noncombustion_lignite_air*28
DIV_ch4_noncombustion_lignite_air = CH4_noncombustion_lignite_air/total_output
DIV_ch4_noncombustion_lignite_air[is.na(DIV_ch4_noncombustion_lignite_air)]=0
DIV_ch4_noncombustion_lignite_air[DIV_ch4_noncombustion_lignite_air == Inf]<-0
TIV_ch4_noncombustion_lignite_air = as.vector(DIV_ch4_noncombustion_lignite_air) %*% L

write.csv(TIV_ch4_noncombustion_lignite_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_lignite_air = as.vector(DIV_ch4_noncombustion_lignite_air) * L
TIV_breakdown_ch4_noncombustion_lignite_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_lignite_air)
TIV_country_breakdown_ch4_noncombustion_lignite_air_w_labels = t(TIV_breakdown_ch4_noncombustion_lignite_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_lignite_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_ixi.csv"))

## subbituminous coal
CH4_noncombustion_subbituminouscoal_air = satellite[74,]
CH4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air*28
DIV_ch4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air/total_output
DIV_ch4_noncombustion_subbituminouscoal_air[is.na(DIV_ch4_noncombustion_subbituminouscoal_air)]=0
DIV_ch4_noncombustion_subbituminouscoal_air[DIV_ch4_noncombustion_subbituminouscoal_air == Inf]<-0
TIV_ch4_noncombustion_subbituminouscoal_air = as.vector(DIV_ch4_noncombustion_subbituminouscoal_air) %*% L

write.csv(TIV_ch4_noncombustion_subbituminouscoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_subbituminouscoal_air = as.vector(DIV_ch4_noncombustion_subbituminouscoal_air) * L
TIV_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_subbituminouscoal_air)
TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_ixi.csv"))

## oil refinery
CH4_noncombustion_oilrefinery_air = satellite[75,]
CH4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air*28
DIV_ch4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air/total_output
DIV_ch4_noncombustion_oilrefinery_air[is.na(DIV_ch4_noncombustion_oilrefinery_air)]=0
DIV_ch4_noncombustion_oilrefinery_air[DIV_ch4_noncombustion_oilrefinery_air == Inf]<-0
TIV_ch4_noncombustion_oilrefinery_air = as.vector(DIV_ch4_noncombustion_oilrefinery_air) %*% L

write.csv(TIV_ch4_noncombustion_oilrefinery_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_noncombustion_oilrefinery_air = as.vector(DIV_ch4_noncombustion_oilrefinery_air) * L
TIV_breakdown_ch4_noncombustion_oilrefinery_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_noncombustion_oilrefinery_air)
TIV_country_breakdown_ch4_noncombustion_oilrefinery_air_w_labels = t(TIV_breakdown_ch4_noncombustion_oilrefinery_air_w_labels %>%
                                                                             group_by(V1) %>%
                                                                             select(-X,-V2) %>%
                                                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_oilrefinery_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_ixi.csv"))

# CH4 - agriculture - air
CH4_agriculture_air = satellite[427,]
CH4_agriculture_air = CH4_agriculture_air*28
DIV_ch4_agriculture_air = CH4_agriculture_air/total_output
DIV_ch4_agriculture_air[is.na(DIV_ch4_agriculture_air)]=0
DIV_ch4_agriculture_air[DIV_ch4_agriculture_air == Inf]<-0
TIV_ch4_agriculture_air = as.vector(DIV_ch4_agriculture_air) %*% L

write.csv(TIV_ch4_agriculture_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_agriculture_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_agriculture_air = as.vector(DIV_ch4_agriculture_air) * L
TIV_breakdown_ch4_agriculture_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_agriculture_air)
TIV_country_breakdown_ch4_agriculture_air_w_labels = t(TIV_breakdown_ch4_agriculture_air_w_labels %>%
                                                                             group_by(V1) %>%
                                                                             select(-X,-V2) %>%
                                                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_agriculture_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_agriculture_air_",year_current,"_ixi.csv"))

# CH4 - waste - air
CH4_waste_air = satellite[436,]
CH4_waste_air = CH4_waste_air*28
DIV_ch4_waste_air = CH4_waste_air/total_output
DIV_ch4_waste_air[is.na(DIV_ch4_waste_air)]=0
DIV_ch4_waste_air[DIV_ch4_waste_air == Inf]<-0
TIV_ch4_waste_air = as.vector(DIV_ch4_waste_air) %*% L

write.csv(TIV_ch4_waste_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_waste_air_",year_current,"_ixi.csv"))

TIV_breakdown_ch4_waste_air = as.vector(DIV_ch4_waste_air) * L
TIV_breakdown_ch4_waste_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_ch4_waste_air)
TIV_country_breakdown_ch4_waste_air_w_labels = t(TIV_breakdown_ch4_waste_air_w_labels %>%
                                                                             group_by(V1) %>%
                                                                             select(-X,-V2) %>%
                                                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_waste_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_waste_air_",year_current,"_ixi.csv"))

# N2O - combustion - air
N2O_combustion_air = satellite[26,]
N2O_combustion_air = N2O_combustion_air*265
DIV_n2o_combustion_air = N2O_combustion_air/total_output
DIV_n2o_combustion_air[is.na(DIV_n2o_combustion_air)]=0
DIV_n2o_combustion_air[DIV_n2o_combustion_air == Inf]<-0
TIV_n2o_combustion_air = as.vector(DIV_n2o_combustion_air) %*% L

write.csv(TIV_n2o_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))

TIV_breakdown_n2o_combustion_air = as.vector(DIV_n2o_combustion_air) * L
TIV_breakdown_n2o_combustion_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_n2o_combustion_air)
TIV_country_breakdown_n2o_combustion_air_w_labels = t(TIV_breakdown_n2o_combustion_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_n2o_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))

# N2O - agriculture - air
N2O_agriculture_air = satellite[430,]
N2O_agriculture_air = N2O_agriculture_air*265
DIV_n2o_agriculture_air = N2O_agriculture_air/total_output
DIV_n2o_agriculture_air[is.na(DIV_n2o_agriculture_air)]=0
DIV_n2o_agriculture_air[DIV_n2o_agriculture_air == Inf]<-0
TIV_n2o_agriculture_air = as.vector(DIV_n2o_agriculture_air) %*% L

write.csv(TIV_n2o_agriculture_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))

TIV_breakdown_n2o_agriculture_air = as.vector(DIV_n2o_agriculture_air) * L
TIV_breakdown_n2o_agriculture_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_n2o_agriculture_air)
TIV_country_breakdown_n2o_agriculture_air_w_labels = t(TIV_breakdown_n2o_agriculture_air_w_labels %>%
                                                                             group_by(V1) %>%
                                                                             select(-X,-V2) %>%
                                                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_n2o_agriculture_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))

# SF6 - air
SF6_air = satellite[424,]
SF6_air = SF6_air*23500
DIV_sf6_air = SF6_air/total_output
DIV_sf6_air[is.na(DIV_sf6_air)]=0
DIV_sf6_air[DIV_sf6_air == Inf]<-0
TIV_sf6_air = as.vector(DIV_sf6_air) %*% L

write.csv(TIV_sf6_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_sf6_CO2eq_air_",year_current,"_ixi.csv"))

TIV_breakdown_sf6_air = as.vector(DIV_sf6_air) * L
TIV_breakdown_sf6_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_sf6_air)
TIV_country_breakdown_sf6_air_w_labels = t(TIV_breakdown_sf6_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_sf6_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_ixi.csv"))

# HFC - air
HFC_air = satellite[425,]
DIV_hfc_air = HFC_air/total_output
DIV_hfc_air[is.na(DIV_hfc_air)]=0
DIV_hfc_air[DIV_hfc_air == Inf]<-0
TIV_hfc_air = as.vector(DIV_hfc_air) %*% L

write.csv(TIV_hfc_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_hfc_CO2eq_air_",year_current,"_ixi.csv"))

TIV_breakdown_hfc_air = as.vector(DIV_hfc_air) * L
TIV_breakdown_hfc_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_hfc_air)
TIV_country_breakdown_hfc_air_w_labels = t(TIV_breakdown_hfc_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_hfc_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_ixi.csv"))

# PFC - air
PFC_air = satellite[426,]
DIV_pfc_air = PFC_air/total_output
DIV_pfc_air[is.na(DIV_pfc_air)]=0
DIV_pfc_air[DIV_pfc_air == Inf]<-0
TIV_pfc_air = as.vector(DIV_pfc_air) %*% L

write.csv(TIV_pfc_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_pfc_CO2eq_air_",year_current,"_ixi.csv"))

TIV_breakdown_pfc_air = as.vector(DIV_pfc_air) * L
TIV_breakdown_pfc_air_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_pfc_air)
TIV_country_breakdown_pfc_air_w_labels = t(TIV_breakdown_pfc_air_w_labels %>%
                                             group_by(V1) %>%
                                             select(-X,-V2) %>%
                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_pfc_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_ixi.csv"))

# Energy carrier - use
energy_carrier_use = satellite[470,]
write.csv(energy_carrier_use, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/satellite/energy_carrier_use_",year_current,"_ixi.csv"))
DIV_e_u = energy_carrier_use/total_output
DIV_e_u[is.na(DIV_e_u)]=0
DIV_e_u[DIV_e_u == Inf]<-0
TIV_e_u = as.vector(DIV_e_u) %*% L

write.csv(TIV_e_u, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_energy_carrier_use_",year_current,"_ixi.csv"))

TIV_breakdown_e_u = as.vector(DIV_e_u) * L
TIV_breakdown_e_u_w_labels = cbind(Exiobase_T_labels_ixi, TIV_breakdown_e_u)
TIV_country_breakdown_e_u_w_labels = t(TIV_breakdown_e_u_w_labels %>%
                                             group_by(V1) %>%
                                             select(-X,-V2) %>%
                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_e_u_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_energy_carrier_use_",year_current,"_ixi.csv"))
}


##### EXIOBASE product-by-product version

# set study years
years_exiobase_pxp = c(2005,2010)

# 'for' loop which writes 'total intensity vectors' (and row-wise breakdowns) for all study satellite extensions and years to 'data_dir_exiobase' using downloaded EXIOBASE files
for (i in years_exiobase_pxp){
  
year_current = i

# read in A table as table
A = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/A.txt"),header = F)

# write A table as .csv file
write.csv(A, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/A.csv"))

# read in A table as .csv file and extract the data only (no labels), and convert to numeric
A = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/A.csv",sep = ""),row.names=NULL,as.is=TRUE)[4:9803,4:9803]
A[is.na(A)]=0
A = mapply(A, FUN = as.numeric)
A = matrix(data = A, ncol = 9800, nrow = 9800)

# solve the Leontief inverse
L = solve(diag(dim(A)[1])-A)
L[is.na(L)]=0

# read in final demand table (Y) as table
FD = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/Y.txt"),header = F)

# write final demand table (Y) as .csv file
write.csv(FD, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/Y.csv"))

# read in final demand table as .csv file and extract the production sector labels
Exiobase_T_labels_pxp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/Y.csv"))[4:9803,1:3]

# write a .csv file with only the production sector labels
write.csv(Exiobase_T_labels_pxp, paste0(data_dir_exiobase, "/Exiobase_T_labels_pxp.csv"))

# read in final demand table as .csv file and extract the final demand category labels
Exiobase_FD_labels_pxp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/Y.csv"))[1:3,4:346]

# write a .csv file with only the final demand category labels
write.csv(Exiobase_FD_labels_pxp, paste0(data_dir_exiobase, "/Exiobase_FD_labels_pxp.csv"))

# read in final demand table as .csv file and extract the data only (no labels), and convert to numeric
FD = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/Y.csv",sep=""),row.names=NULL,as.is=TRUE)[4:9803,4:346]
FD[is.na(FD)]=0
FD = mapply(FD, FUN = as.numeric)
FD = matrix(data=FD,ncol=343,nrow=9800)

# write a .csv file with final demand data only (no labels)
write.csv(FD, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/FD_",year_current,"_pxp.csv"))

# calculate total output
total_output = L %*% rowSums(FD)

# write total output as a .csv file
write.csv(total_output, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/total_output_",year_current,"_pxp.csv"))

# read in satellite extensions table (F) as table
satellite = read.delim(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/satellite/F.txt"),header = F)

# write satellite extensions table (F) as .csv file
write.csv(satellite, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/satellite/F.csv"))

# read in satellite extensions table (F) as .csv file and extract the data only (no labels), and convert to numeric
satellite = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/satellite/F.csv",sep=""),row.names=NULL,as.is=TRUE)[3:1106,3:9802]
satellite[is.na(satellite)]=0
satellite = mapply(satellite, FUN = as.numeric)
satellite = matrix(data=satellite,ncol=9800,nrow=1104)

# write a .csv file with satellite extensions data only (no labels)
write.csv(satellite, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/satellite/satellite_",year_current,"_pxp.csv"))

# read in satellite extensions on final demand table (F_hh) as table
satellite_FD = read.delim(paste0(data_dir_exiobase, "/IOT_", year_current, "_pxp/satellite/F_hh.txt"),header = F)

# write satellite extensions on final demand table (F_hh) as .csv file
write.csv(satellite_FD, paste0(data_dir_exiobase, "/IOT_", year_current, "_pxp/satellite/F_hh.csv"))

## extract the relevant satellite extensions from the satellite table, calculate the 'total intensity 
## vectors' (and their row-wise breakdowns), and write them as .csv files to 'data_dir_exiobase'

# CO2 - combustion - air
CO2_combustion_air = satellite[24,]
DIV_co2_combustion_air = CO2_combustion_air/total_output
DIV_co2_combustion_air[is.na(DIV_co2_combustion_air)]=0
DIV_co2_combustion_air[DIV_co2_combustion_air == Inf]<-0
TIV_co2_combustion_air = as.vector(DIV_co2_combustion_air) %*% L

write.csv(TIV_co2_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_combustion_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_combustion_air = as.vector(DIV_co2_combustion_air) * L
TIV_breakdown_co2_combustion_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_combustion_air)
TIV_country_breakdown_co2_combustion_air_w_labels = t(TIV_breakdown_co2_combustion_air_w_labels %>%
  group_by(V1) %>%
  select(-X,-V2) %>%
  summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_combustion_air_",year_current,"_pxp.csv"))

# CO2 - non-combustion - air
## cement
CO2_noncombustion_cement_air = satellite[93,]
DIV_co2_noncombustion_cement_air = CO2_noncombustion_cement_air/total_output
DIV_co2_noncombustion_cement_air[is.na(DIV_co2_noncombustion_cement_air)]=0
DIV_co2_noncombustion_cement_air[DIV_co2_noncombustion_cement_air == Inf]<-0
TIV_co2_noncombustion_cement_air = as.vector(DIV_co2_noncombustion_cement_air) %*% L

write.csv(TIV_co2_noncombustion_cement_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_noncombustion_cement_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_noncombustion_cement_air = as.vector(DIV_co2_noncombustion_cement_air) * L
TIV_breakdown_co2_noncombustion_cement_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_noncombustion_cement_air)
TIV_country_breakdown_co2_noncombustion_cement_air_w_labels = t(TIV_breakdown_co2_noncombustion_cement_air_w_labels %>%
                                                                  group_by(V1) %>%
                                                                  select(-X,-V2) %>%
                                                                  summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_noncombustion_cement_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_pxp.csv"))

## lime
CO2_noncombustion_lime_air = satellite[94,]
DIV_co2_noncombustion_lime_air = CO2_noncombustion_lime_air/total_output
DIV_co2_noncombustion_lime_air[is.na(DIV_co2_noncombustion_lime_air)]=0
DIV_co2_noncombustion_lime_air[DIV_co2_noncombustion_lime_air == Inf]<-0
TIV_co2_noncombustion_lime_air = as.vector(DIV_co2_noncombustion_lime_air) %*% L

write.csv(TIV_co2_noncombustion_lime_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_noncombustion_lime_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_noncombustion_lime_air = as.vector(DIV_co2_noncombustion_lime_air) * L
TIV_breakdown_co2_noncombustion_lime_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_noncombustion_lime_air)
TIV_country_breakdown_co2_noncombustion_lime_air_w_labels = t(TIV_breakdown_co2_noncombustion_lime_air_w_labels %>%
                                                                group_by(V1) %>%
                                                                select(-X,-V2) %>%
                                                                summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_noncombustion_lime_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_pxp.csv"))

# CO2 - agriculture - peat decay - air
CO2_agriculture_peatdecay_air = satellite[428,]
DIV_co2_agriculture_peatdecay_air = CO2_agriculture_peatdecay_air/total_output
DIV_co2_agriculture_peatdecay_air[is.na(DIV_co2_agriculture_peatdecay_air)]=0
DIV_co2_agriculture_peatdecay_air[DIV_co2_agriculture_peatdecay_air == Inf]<-0
TIV_co2_agriculture_peatdecay_air = as.vector(DIV_co2_agriculture_peatdecay_air) %*% L

write.csv(TIV_co2_agriculture_peatdecay_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_agriculture_peatdecay_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_agriculture_peatdecay_air = as.vector(DIV_co2_agriculture_peatdecay_air) * L
TIV_breakdown_co2_agriculture_peatdecay_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_agriculture_peatdecay_air)
TIV_country_breakdown_co2_agriculture_peatdecay_air_w_labels = t(TIV_breakdown_co2_agriculture_peatdecay_air_w_labels %>%
                                                                   group_by(V1) %>%
                                                                   select(-X,-V2) %>%
                                                                   summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_agriculture_peatdecay_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_pxp.csv"))

# CO2 - waste - air
## biogenic
CO2_waste_biogenic_air = satellite[438,]
DIV_co2_waste_biogenic_air = CO2_waste_biogenic_air/total_output
DIV_co2_waste_biogenic_air[is.na(DIV_co2_waste_biogenic_air)]=0
DIV_co2_waste_biogenic_air[DIV_co2_waste_biogenic_air == Inf]<-0
TIV_co2_biogenic_air = as.vector(DIV_co2_waste_biogenic_air) %*% L

write.csv(TIV_co2_biogenic_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_biogenic_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_biogenic_air = as.vector(DIV_co2_waste_biogenic_air) * L
TIV_breakdown_co2_biogenic_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_biogenic_air)
TIV_country_breakdown_co2_biogenic_air_w_labels = t(TIV_breakdown_co2_biogenic_air_w_labels %>%
                                                      group_by(V1) %>%
                                                      select(-X,-V2) %>%
                                                      summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_biogenic_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_biogenic_air_",year_current,"_pxp.csv"))

## fossil
CO2_waste_fossil_air = satellite[439,]
DIV_co2_waste_fossil_air = CO2_waste_fossil_air/total_output
DIV_co2_waste_fossil_air[is.na(DIV_co2_waste_fossil_air)]=0
DIV_co2_waste_fossil_air[DIV_co2_waste_fossil_air == Inf]<-0
TIV_co2_waste_fossil_air = as.vector(DIV_co2_waste_fossil_air) %*% L

write.csv(TIV_co2_waste_fossil_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_waste_fossil_air_",year_current,"_pxp.csv"))

TIV_breakdown_co2_waste_fossil_air = as.vector(DIV_co2_waste_fossil_air) * L
TIV_breakdown_co2_waste_fossil_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_co2_waste_fossil_air)
TIV_country_breakdown_co2_waste_fossil_air_w_labels = t(TIV_breakdown_co2_waste_fossil_air_w_labels %>%
                                                          group_by(V1) %>%
                                                          select(-X,-V2) %>%
                                                          summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_co2_waste_fossil_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_pxp.csv"))

# CH4 - combustion - air
CH4_combustion_air = satellite[25,]
CH4_combustion_air = CH4_combustion_air*28
DIV_ch4_combustion_air = CH4_combustion_air/total_output
DIV_ch4_combustion_air[is.na(DIV_ch4_combustion_air)]=0
DIV_ch4_combustion_air[DIV_ch4_combustion_air == Inf]<-0
TIV_ch4_combustion_air = as.vector(DIV_ch4_combustion_air) %*% L

write.csv(TIV_ch4_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_combustion_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_combustion_air = as.vector(DIV_ch4_combustion_air) * L
TIV_breakdown_ch4_combustion_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_combustion_air)
TIV_country_breakdown_ch4_combustion_air_w_labels = t(TIV_breakdown_ch4_combustion_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_combustion_air_",year_current,"_pxp.csv"))

# CH4 - non-combustion - air
## gas
CH4_noncombustion_gas_air = satellite[68,]
CH4_noncombustion_gas_air = CH4_noncombustion_gas_air*28
DIV_ch4_noncombustion_gas_air = CH4_noncombustion_gas_air/total_output
DIV_ch4_noncombustion_gas_air[is.na(DIV_ch4_noncombustion_gas_air)]=0
DIV_ch4_noncombustion_gas_air[DIV_ch4_noncombustion_gas_air == Inf]<-0
TIV_ch4_noncombustion_gas_air = as.vector(DIV_ch4_noncombustion_gas_air) %*% L

write.csv(TIV_ch4_noncombustion_gas_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_gas_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_gas_air = as.vector(DIV_ch4_noncombustion_gas_air) * L
TIV_breakdown_ch4_noncombustion_gas_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_gas_air)
TIV_country_breakdown_ch4_noncombustion_gas_air_w_labels = t(TIV_breakdown_ch4_noncombustion_gas_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_gas_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_",year_current,"_pxp.csv"))

## oil
CH4_noncombustion_oil_air = satellite[69,]
CH4_noncombustion_oil_air = CH4_noncombustion_oil_air*28
DIV_ch4_noncombustion_oil_air = CH4_noncombustion_oil_air/total_output
DIV_ch4_noncombustion_oil_air[is.na(DIV_ch4_noncombustion_oil_air)]=0
DIV_ch4_noncombustion_oil_air[DIV_ch4_noncombustion_oil_air == Inf]<-0
TIV_ch4_noncombustion_oil_air = as.vector(DIV_ch4_noncombustion_oil_air) %*% L

write.csv(TIV_ch4_noncombustion_oil_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_oil_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_oil_air = as.vector(DIV_ch4_noncombustion_oil_air) * L
TIV_breakdown_ch4_noncombustion_oil_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_oil_air)
TIV_country_breakdown_ch4_noncombustion_oil_air_w_labels = t(TIV_breakdown_ch4_noncombustion_oil_air_w_labels %>%
                                                               group_by(V1) %>%
                                                               select(-X,-V2) %>%
                                                               summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_oil_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_",year_current,"_pxp.csv"))

## anthracite
CH4_noncombustion_anthracite_air = satellite[70,]
CH4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air*28
DIV_ch4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air/total_output
DIV_ch4_noncombustion_anthracite_air[is.na(DIV_ch4_noncombustion_anthracite_air)]=0
DIV_ch4_noncombustion_anthracite_air[DIV_ch4_noncombustion_anthracite_air == Inf]<-0
TIV_ch4_noncombustion_anthracite_air = as.vector(DIV_ch4_noncombustion_anthracite_air) %*% L

write.csv(TIV_ch4_noncombustion_anthracite_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_anthracite_air = as.vector(DIV_ch4_noncombustion_anthracite_air) * L
TIV_breakdown_ch4_noncombustion_anthracite_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_anthracite_air)
TIV_country_breakdown_ch4_noncombustion_anthracite_air_w_labels = t(TIV_breakdown_ch4_noncombustion_anthracite_air_w_labels %>%
                                                                      group_by(V1) %>%
                                                                      select(-X,-V2) %>%
                                                                      summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_anthracite_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_pxp.csv"))

## bituminous coal
CH4_noncombustion_bituminouscoal_air = satellite[71,]
CH4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air*28
DIV_ch4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air/total_output
DIV_ch4_noncombustion_bituminouscoal_air[is.na(DIV_ch4_noncombustion_bituminouscoal_air)]=0
DIV_ch4_noncombustion_bituminouscoal_air[DIV_ch4_noncombustion_bituminouscoal_air == Inf]<-0
TIV_ch4_noncombustion_bituminouscoal_air = as.vector(DIV_ch4_noncombustion_bituminouscoal_air) %*% L

write.csv(TIV_ch4_noncombustion_bituminouscoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_bituminouscoal_air = as.vector(DIV_ch4_noncombustion_bituminouscoal_air) * L
TIV_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_bituminouscoal_air)
TIV_country_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels %>%
                                                                          group_by(V1) %>%
                                                                          select(-X,-V2) %>%
                                                                          summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_bituminouscoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_pxp.csv"))

## coking coal
CH4_noncombustion_cokingcoal_air = satellite[72,]
CH4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air*28
DIV_ch4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air/total_output
DIV_ch4_noncombustion_cokingcoal_air[is.na(DIV_ch4_noncombustion_cokingcoal_air)]=0
DIV_ch4_noncombustion_cokingcoal_air[DIV_ch4_noncombustion_cokingcoal_air == Inf]<-0
TIV_ch4_noncombustion_cokingcoal_air = as.vector(DIV_ch4_noncombustion_cokingcoal_air) %*% L

write.csv(TIV_ch4_noncombustion_cokingcoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_cokingcoal_air = as.vector(DIV_ch4_noncombustion_cokingcoal_air) * L
TIV_breakdown_ch4_noncombustion_cokingcoal_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_cokingcoal_air)
TIV_country_breakdown_ch4_noncombustion_cokingcoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_cokingcoal_air_w_labels %>%
                                                                      group_by(V1) %>%
                                                                      select(-X,-V2) %>%
                                                                      summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_cokingcoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_pxp.csv"))

## lignite
CH4_noncombustion_lignite_air = satellite[73,]
CH4_noncombustion_lignite_air = CH4_noncombustion_lignite_air*28
DIV_ch4_noncombustion_lignite_air = CH4_noncombustion_lignite_air/total_output
DIV_ch4_noncombustion_lignite_air[is.na(DIV_ch4_noncombustion_lignite_air)]=0
DIV_ch4_noncombustion_lignite_air[DIV_ch4_noncombustion_lignite_air == Inf]<-0
TIV_ch4_noncombustion_lignite_air = as.vector(DIV_ch4_noncombustion_lignite_air) %*% L

write.csv(TIV_ch4_noncombustion_lignite_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_lignite_air = as.vector(DIV_ch4_noncombustion_lignite_air) * L
TIV_breakdown_ch4_noncombustion_lignite_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_lignite_air)
TIV_country_breakdown_ch4_noncombustion_lignite_air_w_labels = t(TIV_breakdown_ch4_noncombustion_lignite_air_w_labels %>%
                                                                   group_by(V1) %>%
                                                                   select(-X,-V2) %>%
                                                                   summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_lignite_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_pxp.csv"))

## subbituminous coal
CH4_noncombustion_subbituminouscoal_air = satellite[74,]
CH4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air*28
DIV_ch4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air/total_output
DIV_ch4_noncombustion_subbituminouscoal_air[is.na(DIV_ch4_noncombustion_subbituminouscoal_air)]=0
DIV_ch4_noncombustion_subbituminouscoal_air[DIV_ch4_noncombustion_subbituminouscoal_air == Inf]<-0
TIV_ch4_noncombustion_subbituminouscoal_air = as.vector(DIV_ch4_noncombustion_subbituminouscoal_air) %*% L

write.csv(TIV_ch4_noncombustion_subbituminouscoal_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_subbituminouscoal_air = as.vector(DIV_ch4_noncombustion_subbituminouscoal_air) * L
TIV_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_subbituminouscoal_air)
TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels = t(TIV_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels %>%
                                                                             group_by(V1) %>%
                                                                             select(-X,-V2) %>%
                                                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_pxp.csv"))

## oil refinery
CH4_noncombustion_oilrefinery_air = satellite[75,]
CH4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air*28
DIV_ch4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air/total_output
DIV_ch4_noncombustion_oilrefinery_air[is.na(DIV_ch4_noncombustion_oilrefinery_air)]=0
DIV_ch4_noncombustion_oilrefinery_air[DIV_ch4_noncombustion_oilrefinery_air == Inf]<-0
TIV_ch4_noncombustion_oilrefinery_air = as.vector(DIV_ch4_noncombustion_oilrefinery_air) %*% L

write.csv(TIV_ch4_noncombustion_oilrefinery_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_noncombustion_oilrefinery_air = as.vector(DIV_ch4_noncombustion_oilrefinery_air) * L
TIV_breakdown_ch4_noncombustion_oilrefinery_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_noncombustion_oilrefinery_air)
TIV_country_breakdown_ch4_noncombustion_oilrefinery_air_w_labels = t(TIV_breakdown_ch4_noncombustion_oilrefinery_air_w_labels %>%
                                                                       group_by(V1) %>%
                                                                       select(-X,-V2) %>%
                                                                       summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_noncombustion_oilrefinery_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_pxp.csv"))

# CH4 - agriculture - air
CH4_agriculture_air = satellite[427,]
CH4_agriculture_air = CH4_agriculture_air*28
DIV_ch4_agriculture_air = CH4_agriculture_air/total_output
DIV_ch4_agriculture_air[is.na(DIV_ch4_agriculture_air)]=0
DIV_ch4_agriculture_air[DIV_ch4_agriculture_air == Inf]<-0
TIV_ch4_agriculture_air = as.vector(DIV_ch4_agriculture_air) %*% L

write.csv(TIV_ch4_agriculture_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_agriculture_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_agriculture_air = as.vector(DIV_ch4_agriculture_air) * L
TIV_breakdown_ch4_agriculture_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_agriculture_air)
TIV_country_breakdown_ch4_agriculture_air_w_labels = t(TIV_breakdown_ch4_agriculture_air_w_labels %>%
                                                         group_by(V1) %>%
                                                         select(-X,-V2) %>%
                                                         summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_agriculture_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_agriculture_air_",year_current,"_pxp.csv"))

# CH4 - waste - air
CH4_waste_air = satellite[436,]
CH4_waste_air = CH4_waste_air*28
DIV_ch4_waste_air = CH4_waste_air/total_output
DIV_ch4_waste_air[is.na(DIV_ch4_waste_air)]=0
DIV_ch4_waste_air[DIV_ch4_waste_air == Inf]<-0
TIV_ch4_waste_air = as.vector(DIV_ch4_waste_air) %*% L

write.csv(TIV_ch4_waste_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_waste_air_",year_current,"_pxp.csv"))

TIV_breakdown_ch4_waste_air = as.vector(DIV_ch4_waste_air) * L
TIV_breakdown_ch4_waste_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_ch4_waste_air)
TIV_country_breakdown_ch4_waste_air_w_labels = t(TIV_breakdown_ch4_waste_air_w_labels %>%
                                                   group_by(V1) %>%
                                                   select(-X,-V2) %>%
                                                   summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_ch4_waste_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_waste_air_",year_current,"_pxp.csv"))

# N2O - combustion - air
N2O_combustion_air = satellite[26,]
N2O_combustion_air = N2O_combustion_air*265
DIV_n2o_combustion_air = N2O_combustion_air/total_output
DIV_n2o_combustion_air[is.na(DIV_n2o_combustion_air)]=0
DIV_n2o_combustion_air[DIV_n2o_combustion_air == Inf]<-0
TIV_n2o_combustion_air = as.vector(DIV_n2o_combustion_air) %*% L

write.csv(TIV_n2o_combustion_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_n2o_CO2eq_combustion_air_",year_current,"_pxp.csv"))

TIV_breakdown_n2o_combustion_air = as.vector(DIV_n2o_combustion_air) * L
TIV_breakdown_n2o_combustion_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_n2o_combustion_air)
TIV_country_breakdown_n2o_combustion_air_w_labels = t(TIV_breakdown_n2o_combustion_air_w_labels %>%
                                                        group_by(V1) %>%
                                                        select(-X,-V2) %>%
                                                        summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_n2o_combustion_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_pxp.csv"))

# N2O - agriculture - air
N2O_agriculture_air = satellite[430,]
N2O_agriculture_air = N2O_agriculture_air*265
DIV_n2o_agriculture_air = N2O_agriculture_air/total_output
DIV_n2o_agriculture_air[is.na(DIV_n2o_agriculture_air)]=0
DIV_n2o_agriculture_air[DIV_n2o_agriculture_air == Inf]<-0
TIV_n2o_agriculture_air = as.vector(DIV_n2o_agriculture_air) %*% L

write.csv(TIV_n2o_agriculture_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_n2o_CO2eq_agriculture_air_",year_current,"_pxp.csv"))

TIV_breakdown_n2o_agriculture_air = as.vector(DIV_n2o_agriculture_air) * L
TIV_breakdown_n2o_agriculture_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_n2o_agriculture_air)
TIV_country_breakdown_n2o_agriculture_air_w_labels = t(TIV_breakdown_n2o_agriculture_air_w_labels %>%
                                                         group_by(V1) %>%
                                                         select(-X,-V2) %>%
                                                         summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_n2o_agriculture_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_pxp.csv"))

# SF6 - air
SF6_air = satellite[424,]
SF6_air = SF6_air*23500
DIV_sf6_air = SF6_air/total_output
DIV_sf6_air[is.na(DIV_sf6_air)]=0
DIV_sf6_air[DIV_sf6_air == Inf]<-0
TIV_sf6_air = as.vector(DIV_sf6_air) %*% L

write.csv(TIV_sf6_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_sf6_CO2eq_air_",year_current,"_pxp.csv"))

TIV_breakdown_sf6_air = as.vector(DIV_sf6_air) * L
TIV_breakdown_sf6_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_sf6_air)
TIV_country_breakdown_sf6_air_w_labels = t(TIV_breakdown_sf6_air_w_labels %>%
                                             group_by(V1) %>%
                                             select(-X,-V2) %>%
                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_sf6_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_pxp.csv"))

# HFC - air
HFC_air = satellite[425,]
DIV_hfc_air = HFC_air/total_output
DIV_hfc_air[is.na(DIV_hfc_air)]=0
DIV_hfc_air[DIV_hfc_air == Inf]<-0
TIV_hfc_air = as.vector(DIV_hfc_air) %*% L

write.csv(TIV_hfc_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_hfc_CO2eq_air_",year_current,"_pxp.csv"))

TIV_breakdown_hfc_air = as.vector(DIV_hfc_air) * L
TIV_breakdown_hfc_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_hfc_air)
TIV_country_breakdown_hfc_air_w_labels = t(TIV_breakdown_hfc_air_w_labels %>%
                                             group_by(V1) %>%
                                             select(-X,-V2) %>%
                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_hfc_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_pxp.csv"))

# PFC - air
PFC_air = satellite[426,]
DIV_pfc_air = PFC_air/total_output
DIV_pfc_air[is.na(DIV_pfc_air)]=0
DIV_pfc_air[DIV_pfc_air == Inf]<-0
TIV_pfc_air = as.vector(DIV_pfc_air) %*% L

write.csv(TIV_pfc_air, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_pfc_CO2eq_air_",year_current,"_pxp.csv"))

TIV_breakdown_pfc_air = as.vector(DIV_pfc_air) * L
TIV_breakdown_pfc_air_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_pfc_air)
TIV_country_breakdown_pfc_air_w_labels = t(TIV_breakdown_pfc_air_w_labels %>%
                                             group_by(V1) %>%
                                             select(-X,-V2) %>%
                                             summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_pfc_air_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_pxp.csv"))

# Energy carrier - use
energy_carrier_use = satellite[470,]
DIV_e_u = energy_carrier_use/total_output
DIV_e_u[is.na(DIV_e_u)]=0
DIV_e_u[DIV_e_u == Inf]<-0
TIV_e_u = as.vector(DIV_e_u) %*% L

write.csv(TIV_e_u, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_energy_carrier_use_",year_current,"_pxp.csv"))

TIV_breakdown_e_u = as.vector(DIV_e_u) * L
TIV_breakdown_e_u_w_labels = cbind(Exiobase_T_labels_pxp, TIV_breakdown_e_u)
TIV_country_breakdown_e_u_w_labels = t(TIV_breakdown_e_u_w_labels %>%
                                         group_by(V1) %>%
                                         select(-X,-V2) %>%
                                         summarise_all(funs(sum)))

write.csv(TIV_country_breakdown_e_u_w_labels, paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_energy_carrier_use_",year_current,"_pxp.csv"))
}

```

# isf

HBS: European household budget survey from EUROSTAT, macro-data, from : https://ec.europa.eu/eurostat/web/household-budget-surveys/database [accessed on 22.05.2020]

1) 'Mean consumption expenditure by income quintile (hbs_exp_t133)'

2) 'Structure of consumption expenditure by income quintile and COICOP consumption purpose (hbs_str_t223)'

Direct energy use/emissions from households: 

1) For energy, the EUROSTAT data table 'Energy supply and use by NACE Rev. 2 activity' [env_ac_pefasu] at: http://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=env_ac_pefasu [accessed on 03.06.2020]. 

2) For emissions to air, we download the EUROSTAT data table 'Air emissions accounts by NACE Rev. 2 activity' [env_ac_ainah_r2] at: https://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=env_ac_ainah_r2&lang=en [accessed on 03.06.2020].

Trade and Transport margin tables, Taxes less Subsidies tables (National Accounts > Annual National Accounts > Supply and Use Tables > SUT Indicators):   https://stats.oecd.org/index.aspx?queryid=84864#  

  1) Trade and transport margins in percentage of final consumption expenditure by households: Export as text file (csv) - customize to include years 2005 to 2015 [accessed on: 15-04-2020]
  
  2) Taxes less subsidies on product in percentage of final consumption expenditure by households: Export as text file (csv) - customize to include years 2005 to 2015 [accessed on: 15-04-2020]


```{r income-stratified-footprints, eval = FALSE}

# set data directory for income-stratified-footprint files
data_dir_income_stratified_footprints = here("analysis", "preprocessing", "income-stratified-footprints")

################################################### main paper method ####################################################################################
##########################################################################################################################################################
##########################################################################################################################################################

# EUROSTAT Household Budget Survey (HBS)
## load 'mean consumption expenditure by income quintile (hbs_exp_t133)' table
hbs_exp_t133 = read_csv(paste0(data_dir_income_stratified_footprints, "/hbs_exp_t133.csv"))
## rename and arrange by country
mean_expenditure_by_quintile = hbs_exp_t133 %>% 
  rename(geo = 3, quintile = "quantile") %>%
  arrange(geo)

## load 'structure of consumption expenditure by income quintile and COICOP consumption purpose (hbs_str_t223)' table
hbs_str_t223 = read_csv(paste0(data_dir_income_stratified_footprints, "/hbs_str_t223.csv"))
## rename and arrange by country
mean_expenditure_by_coicop_sector = hbs_str_t223 %>% 
  rename(geo = 4, quintile = "quantile") %>%
  arrange(geo) 

## create long data set for mean expenditure by quintile table, saving it for use later when creating European expenditure deciles, and keeping both per household and per adult equivalent units
mean_expenditure_by_quintile_long = mean_expenditure_by_quintile %>%
  filter(!(quintile %in% c("UNK","TOTAL"))) %>%
  filter(!(geo %in% c("EA",
                      "EA12",
                      "EA13",
                      "EA17",
                      "EA18",
                      "EA19",
                      "EEA28",
                      "EEA30_2007",
                      "EFTA",
                      "EU15",
                      "EU25",
                      "EU27_2007",
                      "EU27_2020",
                      "EU28"))) %>%
  gather(year,pps,-quintile,-unit,-geo) %>%
  rename(mean_expenditure = pps)

## write long data set as .csv file to 'data_dir_income_stratified_footprints' - used later in final code chunk for creating European expenditure deciles
write_csv(mean_expenditure_by_quintile_long, paste0(data_dir_income_stratified_footprints, "/mean_expenditure_by_quintile_long.csv"))

## create long data set for mean expenditure by quintile table for use in this code chunk, filter for per household 
mean_expenditure_by_quintile_long = mean_expenditure_by_quintile %>%
  filter(unit == "PPS_HH") %>% # filter 'mean expenditure by quintile' in PPS per HouseHold
  filter(!(quintile %in% c("UNK","TOTAL"))) %>% # filter out unknown and total expenditure
  select(-unit) %>%
  gather(year,pps,-quintile,-geo)

## create long data set for structure of mean consumption expenditure by quintile table
### here we collapse (sum) the two 'rent' HBS sectors 'CP041' and 'CP042' to create a 
### single 'rent' sector. We make some manual adjustments where one coicop level 2 consumption 
### category is missing data, using the total coicop level 1 expenditure and expenditure in the 
### other coicop level 2 categories to calculate the difference. These may not be relevant for 
### future available updates of the EUROSTAT HBS if they have been corrected. In that case, comment out the
### mutations at the end of this code.
mean_expenditure_by_coicop_sector_long = mean_expenditure_by_coicop_sector %>% 
  filter(!(quintile %in% c("UNK","TOTAL"))) %>% # filter out unknown and total expenditure
  select(-unit) %>%
  gather(year,pm,-quintile,-coicop,-geo) %>%
  mutate(coicop = dplyr::recode(coicop, "CP041" = "rent",        
                                "CP042" = "rent")) %>%           
  group_by(geo,quintile,coicop,year) %>%                               
  mutate(pm = parse_number(pm),
         pm = as.numeric(pm)) %>%
  summarise(pm = sum(pm, na.rm = TRUE)) %>%
  ungroup() %>% 
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE1" & 
                       coicop == "CP072", 92-21-14,pm)) %>% 
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE2" & 
                       coicop == "CP072", 108-22-12,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE3" & 
                       coicop == "CP072", 124-32-11,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE4" & 
                       coicop == "CP072", 133-43-10,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE5" & 
                       coicop == "CP072", 162-81-11,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE1" & 
                       coicop == "CP044", 412-4-78-322,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE2" & 
                       coicop == "CP044", 355-5-68-265,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE3" & 
                       coicop == "CP044", 325-8-64-229,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE4" & 
                       coicop == "CP044", 300-9-58-204,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE5" & 
                       coicop == "CP044", 249-10-46-167,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE1" & 
                       coicop == "CP044", 433-3-82-340,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE2" & 
                       coicop == "CP044", 376-6-70-284,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE3" & 
                       coicop == "CP044", 351-9-67-251,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE4" & 
                       coicop == "CP044", 326-10-61-228,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE5" & 
                       coicop == "CP044", 280-9-49-195,pm)) 

# join the two long HBS tables together
join_expenditures = mean_expenditure_by_coicop_sector_long %>% 
  left_join(mean_expenditure_by_quintile_long, by = c("geo","quintile","year")) %>%
  mutate(pps = as.numeric(pps),
         pm = as.numeric(pm),
         pps_coicop = pm*(pps/1000))

################################################### main paper method, EXIOBASE industry-by-industry version ############################################# ########################################################################################################################################################## 

# EXIOBASE industry-by-industry version

## set study years
years_exb_ixi = c(2005,2010,2015)

## create NULL dataframe for dis-aggregated final demand
disaggregated_final_demand = NULL

## create NULL dataframe for 'total intensity vectors'
TIVs = NULL

## create NULL dataframe for domestic 'total intensity vectors'
domestic_TIVs = NULL

## create NULL dataframe for rest-of-Europe 'total intensity vectors'
europe_TIVs = NULL

## create NULL dataframe for national footprints
national_fp = NULL

## create NULL dataframe for national territorial energy use/emissions
national_territorial = NULL

## 'for' loop which populates the above NULL dataframes
for (i in years_exb_ixi){
  
  year_current = i
  
  ## EXIOBASE final demand table without labels (.csv file)
  Exiobase_FD =  read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/FD_",year_current,"_ixi.csv"))[,-1]
  
  ## select household (hh) final demand vectors for study countries
  AT = Exiobase_FD[,1]
  BE = Exiobase_FD[,8]
  BG = Exiobase_FD[,15]
  CY = Exiobase_FD[,22]
  CZ = Exiobase_FD[,29]
  DE = Exiobase_FD[,36]
  DK = Exiobase_FD[,43]
  EE = Exiobase_FD[,50]
  EL = Exiobase_FD[,78]
  ES = Exiobase_FD[,57]
  FI = Exiobase_FD[,64]
  FR = Exiobase_FD[,71]
  HR = Exiobase_FD[,85]
  HU = Exiobase_FD[,92]
  IE = Exiobase_FD[,99]
  IT = Exiobase_FD[,106]
  LT = Exiobase_FD[,113]
  LU = Exiobase_FD[,120]
  LV = Exiobase_FD[,127]
  MT = Exiobase_FD[,134]
  NL = Exiobase_FD[,141]
  NO = Exiobase_FD[,288]
  PL = Exiobase_FD[,148]
  PT = Exiobase_FD[,155]
  RO = Exiobase_FD[,162]
  SE = Exiobase_FD[,169]
  SI = Exiobase_FD[,176]
  SK = Exiobase_FD[,183]
  TR = Exiobase_FD[,274]
  UK = Exiobase_FD[,190]
  
  Eurostat_countries = cbind(AT,BE,BG,CY,CZ,DE,DK,EE,EL,ES,FI,FR,HR,HU,IE,IT,LT,LU,LV,MT,NL,NO,PL,PT,RO,SE,SI,SK,TR,UK)
  
  ## read in EXIOBASE production sector labels written and saved in previous code chunk
  Exiobase_T_labels = read.csv(paste0(data_dir_income_stratified_footprints, "/Exiobase_T_labels_ixi_w_coicop_mapping.csv")) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  ## bind EXIOBASE hh final demand with production sector labels
  hh_fd_with_production_sector_labels = cbind(Exiobase_T_labels,Eurostat_countries) %>% rename(geo = V1, sector = V2)
  
  ## replicate each cell of each country's hh final demand as many times as there are income quantiles in the HBS data - in this case: 5
  cells_repeat = data.frame(hh_fd_with_production_sector_labels %>% slice(rep(1:n(), each = 5))) 
  
  ## create quintile names
  quintiles = data.frame(rep(c("QUINTILE1","QUINTILE2","QUINTILE3","QUINTILE4","QUINTILE5"),163)) %>% rename_at(1,~"quintile")
  
  ## bind replicated hh final demand cells to quintile names
  replicated = cbind(cells_repeat,quintiles) %>% rename(country_of_production = geo)
  
  ## convert to long data
  replicated_long = replicated %>% gather(geo, value,-sector,-coicop,-quintile,-five_sectors,-country_of_production)
  
  ## create year column
  year = as.character(rep(year_current,nrow(replicated_long)))
  
  ## bind year with long hh fd data
  replicated_long = cbind(year,replicated_long)
  
  ## populate NULL 'dis-aggregated_final_demand' dataframe with hh fd from each study year
  disaggregated_final_demand = rbind(disaggregated_final_demand, replicated_long)
  
  
  # load total intensity vectors
  ## CO2 - combustion - air
  Exiobase_TIV_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_combustion_air_", year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_combustion_air_", year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_combustion_europe,TIV_CO2_combustion_not_europe)
  
  ## CO2 - noncombustion - cement - air
  Exiobase_TIV_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_cement_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_cement_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_cement_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_cement_europe,TIV_CO2_noncombustion_cement_not_europe)
  
  ## CO2 - noncombustion - lime - air
  Exiobase_TIV_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_lime_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_lime_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_lime_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_lime_europe,TIV_CO2_noncombustion_lime_not_europe)
  
  ## CO2 - agriculture - peat decay - air
  Exiobase_TIV_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_agriculture_peatdecay_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_agriculture_peatdecay_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_agriculture_peatdecay_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_agriculture_peatdecay_europe,TIV_CO2_agriculture_peatdecay_not_europe)
  
  ## CO2 - waste - biogenic - air
  Exiobase_TIV_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_biogenic_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_biogenic_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_biogenic_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_biogenic_europe,TIV_CO2_waste_biogenic_not_europe)
  
  ## CO2 - waste - fossil - air
  Exiobase_TIV_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_fossil_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_fossil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_fossil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_fossil_europe,TIV_CO2_waste_fossil_not_europe)
  
  ## CH4 - combustion -air
  Exiobase_TIV_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_combustion_europe,TIV_CH4_combustion_not_europe)
  
  ## CH4 - noncombustion - gas - air
  Exiobase_TIV_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_gas_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_gas_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_gas_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_gas_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_gas_europe,TIV_CH4_noncombustion_gas_not_europe)
  
  ## CH4 - noncombustion - oil - air
  Exiobase_TIV_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oil_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oil_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oil_europe,TIV_CH4_noncombustion_oil_not_europe)
  
  ## CH4 - noncombustion - anthracite - air
  Exiobase_TIV_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_anthracite_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_anthracite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_anthracite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_anthracite_europe,TIV_CH4_noncombustion_anthracite_not_europe)
  
  ## CH4 - noncombustion - bituminouscoal - air
  Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_bituminouscoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_bituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_bituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_bituminouscoal_europe,TIV_CH4_noncombustion_bituminouscoal_not_europe)
  
  ## CH4 - noncombustion - cokingcoal - air
  Exiobase_TIV_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_cokingcoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_cokingcoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_cokingcoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_cokingcoal_europe,TIV_CH4_noncombustion_cokingcoal_not_europe)
  
  ## CH4 - noncombustion - lignite - air
  Exiobase_TIV_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_lignite_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_lignite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_lignite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_lignite_europe,TIV_CH4_noncombustion_lignite_not_europe)
  
  ## CH4 - noncombustion - subbituminouscoal - air
  Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_subbituminouscoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_subbituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_subbituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_subbituminouscoal_europe,TIV_CH4_noncombustion_subbituminouscoal_not_europe)
  
  ## CH4 - noncombustion - oilrefinery - air
  Exiobase_TIV_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oilrefinery_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oilrefinery_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oilrefinery_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oilrefinery_europe,TIV_CH4_noncombustion_oilrefinery_not_europe)
  
  ## CH4 - agriculture - air
  Exiobase_TIV_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_agriculture_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_agriculture_europe,TIV_CH4_agriculture_not_europe)
  
  ## CH4 - waste - air
  Exiobase_TIV_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_waste_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_waste_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_waste_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_waste_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_waste_europe,TIV_CH4_waste_not_europe)
  
  ## N2O - combustion - air
  Exiobase_TIV_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_combustion_europe,TIV_N2O_combustion_not_europe)
  
  ## N2O - agriculture - air
  Exiobase_TIV_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_agriculture_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_agriculture_europe,TIV_N2O_agriculture_not_europe)
  
  ## SF6 - air
  Exiobase_TIV_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_SF6_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_SF6_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_SF6_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_SF6_europe,TIV_SF6_not_europe)
  
  ## HFC - air
  Exiobase_TIV_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_HFC_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_HFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_HFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_HFC_europe,TIV_HFC_not_europe)
  
  ## PFC - air
  Exiobase_TIV_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_PFC_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_PFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_PFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_PFC_europe,TIV_PFC_not_europe)
  
  ## energy use
  Exiobase_TIV_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_energy_carrier_use_",year_current,"_ixi.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_energy_carrier_use_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_energy_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_energy_carrier_use_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_energy_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_energy_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_energy_europe,TIV_energy_not_europe)
  
  # bind production sector labels with total intensity vectors
  TIV_with_labels = cbind(Exiobase_T_labels,
                          t(Exiobase_TIV_co2_combustion_bp),
                          t(Exiobase_TIV_co2_noncombustion_cement_bp),
                          t(Exiobase_TIV_co2_noncombustion_lime_bp),
                          t(Exiobase_TIV_co2_agriculture_peatdecay_bp),
                          t(Exiobase_TIV_co2_waste_biogenic_bp),
                          t(Exiobase_TIV_co2_waste_fossil_bp),
                          t(Exiobase_TIV_ch4_combustion_bp),
                          t(Exiobase_TIV_ch4_noncombustion_gas_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oil_bp),
                          t(Exiobase_TIV_ch4_noncombustion_anthracite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_lignite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp),
                          t(Exiobase_TIV_ch4_agriculture_bp),
                          t(Exiobase_TIV_ch4_waste_bp),
                          t(Exiobase_TIV_n2o_combustion_bp),
                          t(Exiobase_TIV_n2o_agriculture_bp),
                          t(Exiobase_TIV_sf6_bp),
                          t(Exiobase_TIV_hfc_bp),
                          t(Exiobase_TIV_pfc_bp),
                          t(Exiobase_TIV_energy_use_bp)) %>% 
    rename(TIV_CO2_combustion = "t(Exiobase_TIV_co2_combustion_bp)",
           TIV_CO2_noncombustion_cement = "t(Exiobase_TIV_co2_noncombustion_cement_bp)",
           TIV_CO2_noncombustion_lime = "t(Exiobase_TIV_co2_noncombustion_lime_bp)",
           TIV_CO2_agriculture_peatdecay = "t(Exiobase_TIV_co2_agriculture_peatdecay_bp)",
           TIV_CO2_waste_biogenic = "t(Exiobase_TIV_co2_waste_biogenic_bp)",
           TIV_CO2_waste_fossil = "t(Exiobase_TIV_co2_waste_fossil_bp)",
           TIV_CH4_combustion = "t(Exiobase_TIV_ch4_combustion_bp)",
           TIV_CH4_noncombustion_gas = "t(Exiobase_TIV_ch4_noncombustion_gas_bp)",
           TIV_CH4_noncombustion_oil = "t(Exiobase_TIV_ch4_noncombustion_oil_bp)",
           TIV_CH4_noncombustion_anthracite = "t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)",
           TIV_CH4_noncombustion_bituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)",
           TIV_CH4_noncombustion_cokingcoal = "t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)",
           TIV_CH4_noncombustion_lignite = "t(Exiobase_TIV_ch4_noncombustion_lignite_bp)",
           TIV_CH4_noncombustion_subbituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)",
           TIV_CH4_noncombustion_oilrefinery = "t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)",
           TIV_CH4_agriculture = "t(Exiobase_TIV_ch4_agriculture_bp)",
           TIV_CH4_waste = "t(Exiobase_TIV_ch4_waste_bp)",
           TIV_N2O_combustion = "t(Exiobase_TIV_n2o_combustion_bp)",
           TIV_N2O_agriculture = "t(Exiobase_TIV_n2o_agriculture_bp)",
           TIV_SF6 = "t(Exiobase_TIV_sf6_bp)",
           TIV_HFC = "t(Exiobase_TIV_hfc_bp)",
           TIV_PFC = "t(Exiobase_TIV_pfc_bp)",
           TIV_energy = "t(Exiobase_TIV_energy_use_bp)") %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  # create year column
  year = as.character(rep(year_current,nrow(TIV_with_labels)))
  
  # bind year with labeled total intensity vectors
  TIVs_intermediate = cbind(year,TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2)
  
  # populate NULL total intensity vectors dataframe
  TIVs = rbind(TIVs,TIVs_intermediate)
  
  # bind production sector labels with domestic total intensity vectors
  domestic_TIV_with_labels = cbind(Exiobase_T_labels,
                                   Exiobase_TIV_country_breakdown_co2_combustion_bp,
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_fossil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_waste_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_sf6_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_hfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_pfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_energy_use_bp %>% select(-country))  %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK"),
           country = dplyr::recode(country, "GR" = "EL", "GB" = "UK")) 
  
  # create year column for domestic TIVs
  year_domestic = as.character(rep(year_current,nrow(domestic_TIV_with_labels)))
  
  # bind year with labeled domestic total intensity vectors
  TIVs_domestic_intermediate = cbind(year_domestic,domestic_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, geo = country, year = year_domestic) %>%
    mutate(TIV_CO2_combustion_domestic = as.numeric(TIV_CO2_combustion_domestic),
           TIV_CO2_noncombustion_cement_domestic = as.numeric(TIV_CO2_noncombustion_cement_domestic),
           TIV_CO2_noncombustion_lime_domestic = as.numeric(TIV_CO2_noncombustion_lime_domestic),
           TIV_CO2_agriculture_peatdecay_domestic = as.numeric(TIV_CO2_agriculture_peatdecay_domestic),
           TIV_CO2_waste_biogenic_domestic = as.numeric(TIV_CO2_waste_biogenic_domestic),
           TIV_CO2_waste_fossil_domestic = as.numeric(TIV_CO2_waste_fossil_domestic),
           TIV_CH4_combustion_domestic = as.numeric(TIV_CH4_combustion_domestic),
           TIV_CH4_noncombustion_gas_domestic = as.numeric(TIV_CH4_noncombustion_gas_domestic),
           TIV_CH4_noncombustion_oil_domestic = as.numeric(TIV_CH4_noncombustion_oil_domestic),
           TIV_CH4_noncombustion_anthracite_domestic = as.numeric(TIV_CH4_noncombustion_anthracite_domestic),
           TIV_CH4_noncombustion_bituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_bituminouscoal_domestic),
           TIV_CH4_noncombustion_cokingcoal_domestic = as.numeric(TIV_CH4_noncombustion_cokingcoal_domestic),
           TIV_CH4_noncombustion_lignite_domestic = as.numeric(TIV_CH4_noncombustion_lignite_domestic),
           TIV_CH4_noncombustion_subbituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_domestic),
           TIV_CH4_noncombustion_oilrefinery_domestic = as.numeric(TIV_CH4_noncombustion_oilrefinery_domestic),
           TIV_CH4_agriculture_domestic = as.numeric(TIV_CH4_agriculture_domestic),
           TIV_CH4_waste_domestic = as.numeric(TIV_CH4_waste_domestic),
           TIV_N2O_combustion_domestic = as.numeric(TIV_N2O_combustion_domestic),
           TIV_N2O_agriculture_domestic = as.numeric(TIV_N2O_agriculture_domestic),
           TIV_SF6_domestic = as.numeric(TIV_SF6_domestic),
           TIV_HFC_domestic = as.numeric(TIV_HFC_domestic),
           TIV_PFC_domestic = as.numeric(TIV_PFC_domestic),
           TIV_energy_domestic = as.numeric(TIV_energy_domestic))
  
  # populate NULL domestic TIVs dataframe
  domestic_TIVs = rbind(domestic_TIVs, TIVs_domestic_intermediate)
  
  # bind production sector labels with European TIVs
  europe_TIV_with_labels = cbind(Exiobase_T_labels,
                                 Exiobase_TIV_europe_breakdown_co2_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp,
                                 Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_waste_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_sf6_bp,
                                 Exiobase_TIV_europe_breakdown_hfc_bp,
                                 Exiobase_TIV_europe_breakdown_pfc_bp,
                                 Exiobase_TIV_europe_breakdown_energy_use_bp) %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  # create year column for European TIVs
  year_europe = as.character(rep(year_current,nrow(europe_TIV_with_labels)))
  
  # bind year with labeled European total intensity vectors
  TIVs_europe_intermediate = cbind(year_europe,europe_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, year = year_europe) %>%
    mutate(TIV_CO2_combustion_europe = as.numeric(TIV_CO2_combustion_europe),
           TIV_CO2_noncombustion_cement_europe = as.numeric(TIV_CO2_noncombustion_cement_europe),
           TIV_CO2_noncombustion_lime_europe = as.numeric(TIV_CO2_noncombustion_lime_europe),
           TIV_CO2_agriculture_peatdecay_europe = as.numeric(TIV_CO2_agriculture_peatdecay_europe),
           TIV_CO2_waste_biogenic_europe = as.numeric(TIV_CO2_waste_biogenic_europe),
           TIV_CO2_waste_fossil_europe = as.numeric(TIV_CO2_waste_fossil_europe),
           TIV_CH4_combustion_europe = as.numeric(TIV_CH4_combustion_europe),
           TIV_CH4_noncombustion_gas_europe = as.numeric(TIV_CH4_noncombustion_gas_europe),
           TIV_CH4_noncombustion_oil_europe = as.numeric(TIV_CH4_noncombustion_oil_europe),
           TIV_CH4_noncombustion_anthracite_europe = as.numeric(TIV_CH4_noncombustion_anthracite_europe),
           TIV_CH4_noncombustion_bituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_bituminouscoal_europe),
           TIV_CH4_noncombustion_cokingcoal_europe = as.numeric(TIV_CH4_noncombustion_cokingcoal_europe),
           TIV_CH4_noncombustion_lignite_europe = as.numeric(TIV_CH4_noncombustion_lignite_europe),
           TIV_CH4_noncombustion_subbituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_europe),
           TIV_CH4_noncombustion_oilrefinery_europe = as.numeric(TIV_CH4_noncombustion_oilrefinery_europe),
           TIV_CH4_agriculture_europe = as.numeric(TIV_CH4_agriculture_europe),
           TIV_CH4_waste_europe = as.numeric(TIV_CH4_waste_europe),
           TIV_N2O_combustion_europe = as.numeric(TIV_N2O_combustion_europe),
           TIV_N2O_agriculture_europe = as.numeric(TIV_N2O_agriculture_europe),
           TIV_SF6_europe = as.numeric(TIV_SF6_europe),
           TIV_HFC_europe = as.numeric(TIV_HFC_europe),
           TIV_PFC_europe = as.numeric(TIV_PFC_europe),
           TIV_energy_europe = as.numeric(TIV_energy_europe))
  
  # populate NULL Europe TIVs dataframe
  europe_TIVs = rbind(europe_TIVs, TIVs_europe_intermediate)
  
  # calculating total national footprints
  ## read in EXIOBASE final demand labels and calculate national footprints for each study satellite extension
  Exiobase_FD_labels = as.data.frame(t(read.csv(paste0(data_dir_exiobase, "/Exiobase_FD_labels_ixi.csv")))[-1,-3]) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  national_CO2_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_co2_combustion_bp)
  
  national_CO2_noncombustion_cement_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_cement_bp)
  
  national_CO2_noncombustion_lime_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_lime_bp)
  
  national_CO2_agriculture_peatdecay_footprints = Exiobase_FD * t(Exiobase_TIV_co2_agriculture_peatdecay_bp)
  
  national_CO2_waste_biogenic_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_biogenic_bp)
  
  national_CO2_waste_fossil_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_fossil_bp)
  
  national_CH4_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_combustion_bp)
  
  national_CH4_noncombustion_gas_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_gas_bp)
  
  national_CH4_noncombustion_oil_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oil_bp)
  
  national_CH4_noncombustion_anthracite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)
  
  national_CH4_noncombustion_bituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)
  
  national_CH4_noncombustion_cokingcoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)
  
  national_CH4_noncombustion_lignite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_lignite_bp)
  
  national_CH4_noncombustion_subbituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)
  
  national_CH4_noncombustion_oilrefinery_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)
  
  national_CH4_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_agriculture_bp)
  
  national_CH4_waste_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_waste_bp)
  
  national_N2O_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_combustion_bp)
  
  national_N2O_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_agriculture_bp)
  
  national_SF6_footprints = Exiobase_FD * t(Exiobase_TIV_sf6_bp)
  
  national_HFC_footprints = Exiobase_FD * t(Exiobase_TIV_hfc_bp)
  
  national_PFC_footprints = Exiobase_FD * t(Exiobase_TIV_pfc_bp)
  
  national_energy_footprints = Exiobase_FD * t(Exiobase_TIV_energy_use_bp)
  
  ## bind FD labels and national footprints together
  national_footprints_w_labels = cbind(Exiobase_FD_labels, 
                                       rowSums(t(national_CO2_combustion_footprints)),
                                       rowSums(t(national_CO2_noncombustion_cement_footprints)), 
                                       rowSums(t(national_CO2_noncombustion_lime_footprints)), 
                                       rowSums(t(national_CO2_agriculture_peatdecay_footprints)), 
                                       rowSums(t(national_CO2_waste_biogenic_footprints)), 
                                       rowSums(t(national_CO2_waste_fossil_footprints)),
                                       rowSums(t(national_CH4_combustion_footprints)),
                                       rowSums(t(national_CH4_noncombustion_gas_footprints)),
                                       rowSums(t(national_CH4_noncombustion_oil_footprints)),
                                       rowSums(t(national_CH4_noncombustion_anthracite_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_cokingcoal_footprints)),
                                       rowSums(t(national_CH4_noncombustion_lignite_footprints)),
                                       rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_oilrefinery_footprints)), 
                                       rowSums(t(national_CH4_agriculture_footprints)),
                                       rowSums(t(national_CH4_waste_footprints)),
                                       rowSums(t(national_N2O_combustion_footprints)),
                                       rowSums(t(national_N2O_agriculture_footprints)),
                                       rowSums(t(national_SF6_footprints)),
                                       rowSums(t(national_HFC_footprints)),
                                       rowSums(t(national_PFC_footprints)),
                                       rowSums(t(national_energy_footprints))) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  ## create year column for national footprints
  year_national_fp = as.character(rep(year_current,nrow(national_footprints_w_labels)))
  
  # satellite extensions direct from household final demand
  ## load satellite extensions direct from household final demand table from 'data_dir_exiobase' and convert to numeric
  direct_FD_extensions = read.csv(paste0(data_dir_exiobase, "/IOT_", year_current, "_ixi/satellite/F_hh.csv", sep = ""),row.names=NULL,as.is=TRUE)[3:1115,3:345]
  direct_FD_extensions[is.na(direct_FD_extensions)]=0
  direct_FD_extensions = mapply(direct_FD_extensions, FUN = as.numeric)
  direct_FD_extensions = matrix(data=direct_FD_extensions,ncol=343,nrow=1113)
  
  ## extract each study satellite extension
  direct_FD_co2_combustion = direct_FD_extensions[24,]
  direct_FD_co2_noncombustion_cement = direct_FD_extensions[93,]
  direct_FD_co2_noncombustion_lime = direct_FD_extensions[94,]
  direct_FD_co2_agriculture_peatdecay = direct_FD_extensions[428,]
  direct_FD_co2_waste_biogenic = direct_FD_extensions[438,]
  direct_FD_co2_waste_fossil = direct_FD_extensions[439,]
  direct_FD_ch4_combustion = direct_FD_extensions[25,]*28
  direct_FD_ch4_noncombustion_gas = direct_FD_extensions[68,]*28
  direct_FD_ch4_noncombustion_oil = direct_FD_extensions[69,]*28
  direct_FD_ch4_noncombustion_anthracite = direct_FD_extensions[70,]*28
  direct_FD_ch4_noncombustion_bituminouscoal = direct_FD_extensions[71,]*28
  direct_FD_ch4_noncombustion_cokingcoal = direct_FD_extensions[72,]*28
  direct_FD_ch4_noncombustion_lignite = direct_FD_extensions[73,]*28
  direct_FD_ch4_noncombustion_subbituminouscoal = direct_FD_extensions[74,]*28
  direct_FD_ch4_noncombustion_oilrefinery = direct_FD_extensions[75,]*28
  direct_FD_ch4_agriculture = direct_FD_extensions[427,]*28
  direct_FD_ch4_waste = direct_FD_extensions[436,]*28
  direct_FD_n2o_combustion = direct_FD_extensions[26,]*265
  direct_FD_n2o_agriculture = direct_FD_extensions[430,]*265
  direct_FD_sf6 = direct_FD_extensions[424,]*23500
  direct_FD_hfc = direct_FD_extensions[425,]
  direct_FD_pfc = direct_FD_extensions[426,]
  direct_FD_energy = direct_FD_extensions[470,]
  
  ## bind all extensions together
  direct_FD_fp = data.frame(direct_FD_co2_combustion,
                            direct_FD_co2_noncombustion_cement,
                            direct_FD_co2_noncombustion_lime,
                            direct_FD_co2_agriculture_peatdecay,
                            direct_FD_co2_waste_biogenic,
                            direct_FD_co2_waste_fossil,
                            direct_FD_ch4_combustion,
                            direct_FD_ch4_noncombustion_gas,
                            direct_FD_ch4_noncombustion_oil,
                            direct_FD_ch4_noncombustion_anthracite,
                            direct_FD_ch4_noncombustion_bituminouscoal,
                            direct_FD_ch4_noncombustion_cokingcoal,
                            direct_FD_ch4_noncombustion_lignite,
                            direct_FD_ch4_noncombustion_subbituminouscoal,
                            direct_FD_ch4_noncombustion_oilrefinery,
                            direct_FD_ch4_agriculture,
                            direct_FD_ch4_waste,
                            direct_FD_n2o_combustion,
                            direct_FD_n2o_agriculture,
                            direct_FD_sf6,
                            direct_FD_hfc,
                            direct_FD_pfc,
                            direct_FD_energy)
  
  ## bind national footprints and direct extensions from hh final demand together (with year column)
  national_fp_intermediate = as.data.frame(cbind(year_national_fp,
                                         national_footprints_w_labels,
                                         direct_FD_fp)) %>% 
    rename(year = year_national_fp,
           geo = V1, 
           fd_category = V2, 
           co2_combustion = "rowSums(t(national_CO2_combustion_footprints))",
           co2_noncombustion_cement = "rowSums(t(national_CO2_noncombustion_cement_footprints))",
           co2_noncombustion_lime = "rowSums(t(national_CO2_noncombustion_lime_footprints))",
           co2_agriculture_peatdecay = "rowSums(t(national_CO2_agriculture_peatdecay_footprints))",
           co2_waste_biogenic = "rowSums(t(national_CO2_waste_biogenic_footprints))",
           co2_waste_fossil = "rowSums(t(national_CO2_waste_fossil_footprints))",
           ch4_combustion = "rowSums(t(national_CH4_combustion_footprints))",
           ch4_noncombustion_gas = "rowSums(t(national_CH4_noncombustion_gas_footprints))",
           ch4_noncombustion_oil = "rowSums(t(national_CH4_noncombustion_oil_footprints))",
           ch4_noncombustion_anthracite = "rowSums(t(national_CH4_noncombustion_anthracite_footprints))",
           ch4_noncombustion_bituminouscoal = "rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints))",
           ch4_noncombustion_cokingcoal = "rowSums(t(national_CH4_noncombustion_cokingcoal_footprints))",
           ch4_noncombustion_lignite = "rowSums(t(national_CH4_noncombustion_lignite_footprints))",
           ch4_noncombustion_subbituminouscoal = "rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints))",
           ch4_noncombustion_oilrefinery = "rowSums(t(national_CH4_noncombustion_oilrefinery_footprints))",
           ch4_agriculture = "rowSums(t(national_CH4_agriculture_footprints))",
           ch4_waste = "rowSums(t(national_CH4_waste_footprints))",
           n2o_combustion = "rowSums(t(national_N2O_combustion_footprints))",
           n2o_agriculture = "rowSums(t(national_N2O_agriculture_footprints))",
           sf6 = "rowSums(t(national_SF6_footprints))",
           hfc = "rowSums(t(national_HFC_footprints))",
           pfc = "rowSums(t(national_PFC_footprints))",
           energy = "rowSums(t(national_energy_footprints))") %>%
    select(year,
           geo,
           fd_category,
           co2_combustion,
           direct_FD_co2_combustion,
           co2_noncombustion_cement,
           direct_FD_co2_noncombustion_cement,
           co2_noncombustion_lime,
           direct_FD_co2_noncombustion_lime,
           co2_agriculture_peatdecay,
           direct_FD_co2_agriculture_peatdecay,
           co2_waste_biogenic,
           direct_FD_co2_waste_biogenic,
           co2_waste_fossil,
           direct_FD_co2_waste_fossil,
           ch4_combustion,
           direct_FD_ch4_combustion,
           ch4_noncombustion_gas,
           direct_FD_ch4_noncombustion_gas,
           ch4_noncombustion_oil,
           direct_FD_ch4_noncombustion_oil,
           ch4_noncombustion_anthracite,
           direct_FD_ch4_noncombustion_anthracite,
           ch4_noncombustion_bituminouscoal,
           direct_FD_ch4_noncombustion_bituminouscoal,
           ch4_noncombustion_cokingcoal,
           direct_FD_ch4_noncombustion_cokingcoal,
           ch4_noncombustion_lignite,
           direct_FD_ch4_noncombustion_lignite,
           ch4_noncombustion_subbituminouscoal,
           direct_FD_ch4_noncombustion_subbituminouscoal,
           ch4_noncombustion_oilrefinery,
           direct_FD_ch4_noncombustion_oilrefinery,
           ch4_agriculture,
           direct_FD_ch4_agriculture,
           ch4_waste,
           direct_FD_ch4_waste,
           n2o_combustion,
           direct_FD_n2o_combustion,
           n2o_agriculture,
           direct_FD_n2o_agriculture,
           sf6,
           direct_FD_sf6,
           hfc,
           direct_FD_hfc,
           pfc,
           direct_FD_pfc,
           energy,
           direct_FD_energy)
  
  ## populate NULL national footprints dataframe
  national_fp = rbind(national_fp, national_fp_intermediate)
  
  # national territorial
  ## load saved satellite extensions table (as .csv file) from 'data_dir_exiobase' and extract each study satellite extension
  satellite = read.csv(paste0(data_dir_exiobase, "/IOT_", year_current, "_ixi/satellite/satellite_",year_current,"_ixi.csv"))[,-1]
  
  CO2_combustion_air = satellite[24,]
  
  CO2_noncombustion_cement_air = satellite[93,]
  
  CO2_noncombustion_lime_air = satellite[94,]
  
  CO2_agriculture_peatdecay_air = satellite[428,]
  
  CO2_waste_biogenic_air = satellite[438,]
  
  CO2_waste_fossil_air = satellite[439,]
  
  CH4_combustion_air = satellite[25,]
  CH4_combustion_air = CH4_combustion_air*28
  
  CH4_noncombustion_gas_air = satellite[68,]
  CH4_noncombustion_gas_air = CH4_noncombustion_gas_air*28
  
  CH4_noncombustion_oil_air = satellite[69,]
  CH4_noncombustion_oil_air = CH4_noncombustion_oil_air*28
  
  CH4_noncombustion_anthracite_air = satellite[70,]
  CH4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air*28
  
  CH4_noncombustion_bituminouscoal_air = satellite[71,]
  CH4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air*28
  
  CH4_noncombustion_cokingcoal_air = satellite[72,]
  CH4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air*28
  
  CH4_noncombustion_lignite_air = satellite[73,]
  CH4_noncombustion_lignite_air = CH4_noncombustion_lignite_air*28
  
  CH4_noncombustion_subbituminouscoal_air = satellite[74,]
  CH4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air*28
  
  CH4_noncombustion_oilrefinery_air = satellite[75,]
  CH4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air*28
  
  CH4_agriculture_air = satellite[427,]
  CH4_agriculture_air = CH4_agriculture_air*28
  
  CH4_waste_air = satellite[436,]
  CH4_waste_air = CH4_waste_air*28
  
  N2O_combustion_air = satellite[26,]
  N2O_combustion_air = N2O_combustion_air*265
  
  N2O_agriculture_air = satellite[430,]
  N2O_agriculture_air = N2O_agriculture_air*265
  
  SF6_air = satellite[424,]
  SF6_air = SF6_air*23500
  
  HFC_air = satellite[425,]
  
  PFC_air = satellite[426,]
  
  energy_carrier_use = satellite[470,]
  
  ## bind together
  territorial = data.frame(t(CO2_combustion_air),
                           t(CO2_noncombustion_cement_air),
                           t(CO2_noncombustion_lime_air),
                           t(CO2_agriculture_peatdecay_air),
                           t(CO2_waste_biogenic_air),
                           t(CO2_waste_fossil_air),
                           t(CH4_combustion_air),
                           t(CH4_noncombustion_gas_air),
                           t(CH4_noncombustion_oil_air),
                           t(CH4_noncombustion_anthracite_air),
                           t(CH4_noncombustion_bituminouscoal_air),
                           t(CH4_noncombustion_cokingcoal_air),
                           t(CH4_noncombustion_lignite_air),
                           t(CH4_noncombustion_subbituminouscoal_air),
                           t(CH4_noncombustion_oilrefinery_air),
                           t(CH4_agriculture_air),
                           t(CH4_waste_air),
                           t(N2O_combustion_air),
                           t(N2O_agriculture_air),
                           t(SF6_air),
                           t(HFC_air),
                           t(PFC_air),
                           t(energy_carrier_use)) %>%
    rename(CO2_combustion = 1, 
           CO2_noncombustion_cement = 2,
           CO2_noncombustion_lime = 3,
           CO2_agriculture_peatdecay = 4,
           CO2_waste_biogenic = 5,
           CO2_waste_fossil = 6,
           CH4_combustion = 7, 
           CH4_noncombustion_gas = 8,
           CH4_noncombustion_oil = 9,
           CH4_noncombustion_anthracite = 10,
           CH4_noncombustion_bituminouscoal = 11,
           CH4_noncombustion_cokingcoal = 12,
           CH4_noncombustion_lignite = 13,
           CH4_noncombustion_subbituminouscoal = 14,
           CH4_noncombustion_oilrefinery = 15,
           CH4_agriculture = 16,
           CH4_waste = 17,
           N2O_combustion = 18, 
           N2O_agriculture = 19,
           SF6 = 20,
           HFC = 21, PFC = 22, energy = 23)
  
  ## create year column
  year_territorial = as.character(rep(year_current,nrow(territorial)))
  
  ## bind year column, production sector labels and territorial satellite extensions
  territorial_intermediate = as.data.frame(cbind(year_territorial,
                                         Exiobase_T_labels,
                                         territorial)) %>%
    rename(year = year_territorial,
           geo = V1,
           sector = V2) %>%
    select(-coicop,-five_sectors)
  
  ## populate NULL territorial dataframe
  national_territorial = rbind(national_territorial, territorial_intermediate)
  
}

# write national territorial extensions as .csv and .rds files (save to 'data_dir_income_stratified_footprints')
write.csv(national_territorial, paste0(data_dir_income_stratified_footprints, "/national_territorial_ixi.csv"))
write_rds(national_territorial, paste0(data_dir_income_stratified_footprints, "/national_territorial_ixi.rds"))

# write national footprints as .csv and .rds files (save to 'data_dir_income_stratified_footprints')
write.csv(national_fp, paste0(data_dir_income_stratified_footprints, "/national_fp_ixi.csv"))
write_rds(national_fp, paste0(data_dir_income_stratified_footprints, "/national_fp_ixi.rds"))

# calculate quintile shares within each HBS coicop category
shares = join_expenditures %>%
  group_by(coicop,geo,year) %>%
  mutate(share = pps_coicop/sum(pps_coicop))

# join disaggregated EXIOBASE hh final demand dataframe with HBS coicop shares and calculate disaggregated hh final demand using the shares
fd_exiobase = disaggregated_final_demand %>% 
  left_join(shares, by = c("year","geo","coicop","quintile")) %>%
  mutate(disaggregated_fd = value*share) %>%
  select(year,geo,quintile,country_of_production,sector,coicop,disaggregated_fd) %>%
  spread(quintile,disaggregated_fd)

# direct extensions from households
## load EUROSTAT energy use data (no Turkey)
env_ac_pefasu_no_TR = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_pefasu_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,HH_HEAT,HH_TRA,HH_OTH) 

## create column for Turkey using Bulgarian shares between HH_HEAT, HH_TRA, and HH_OTH
env_ac_pefasu_TR = env_ac_pefasu_no_TR %>%
  filter(geo == "BG") %>%
  mutate(geo = dplyr::recode(geo,
                             "BG" = "TR"))

## bind EUROSTAT energy use data (no Turkey) with created Turkey column
env_ac_pefasu = rbind(env_ac_pefasu_no_TR,env_ac_pefasu_TR) %>%
  gather(sector,share_of_total_energy,-geo)

## load EUROSTAT emissions data
env_ac_ainah_r2 = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_ainah_r2_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "Turkey" = "TR",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,AIRPOL,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,airpol,HH_HEAT,HH_TRA,HH_OTH) 

### create separate dataframe for CO2
env_ac_ainah_r2_co2 = env_ac_ainah_r2 %>%
  filter(airpol == "Carbon dioxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_co2,-geo)

### for CH4
env_ac_ainah_r2_ch4 = env_ac_ainah_r2 %>%
  filter(airpol == "Methane") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_ch4,-geo)

### for N2O
env_ac_ainah_r2_n2o = env_ac_ainah_r2 %>%
  filter(airpol == "Nitrous oxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_n2o,-geo)

## filter saved EXIOBASE national footprints dataframe, select direct extensions and repeat each cell 3 times
direct_FD_fp_long = national_fp %>%
  filter(fd_category == "Final consumption expenditure by households",
         geo %in% c("AT",
                    "BE", "BG", "CY", "CZ",
                    "DE" , "DK" , "EE" , 
                    "ES" , "FI" , "FR" , 
                    "UK", "EL", "HR" ,
                    "HU" , "IE" , "IT" ,
                    "LT" , "LU" , "LV" ,
                    "MT" , "NL" , "PL" ,
                    "PT" , "TR" , "SK" ,
                    "SI" , "SE" , "RO" ,
                    "NO")) %>%
  select(year,geo,fd_category,direct_FD_co2_combustion,
         direct_FD_co2_noncombustion_cement,
         direct_FD_co2_noncombustion_lime,
         direct_FD_co2_agriculture_peatdecay,
         direct_FD_co2_waste_biogenic,
         direct_FD_co2_waste_fossil,
         direct_FD_ch4_combustion,
         direct_FD_ch4_noncombustion_gas,
         direct_FD_ch4_noncombustion_oil,
         direct_FD_ch4_noncombustion_anthracite,
         direct_FD_ch4_noncombustion_bituminouscoal,
         direct_FD_ch4_noncombustion_cokingcoal,
         direct_FD_ch4_noncombustion_lignite,
         direct_FD_ch4_noncombustion_subbituminouscoal,
         direct_FD_ch4_noncombustion_oilrefinery,
         direct_FD_ch4_agriculture,
         direct_FD_ch4_waste,
         direct_FD_n2o_combustion,
         direct_FD_n2o_agriculture,
         direct_FD_sf6,
         direct_FD_hfc,
         direct_FD_pfc,
         direct_FD_energy) %>%   
  slice(rep(1:n(), each = 3)) 

## create column with direct household sector names (3 times each)
sector = rep(c("HH_HEAT","HH_TRA","HH_OTH"), nrow(direct_FD_fp_long)/3) 

## bind names with EXIOBASE direct extensions data, map hh sectors to COICOP categories, then bind with EUROSTAT energy and emissions data and dis-aggregate EXIOBASE direct extensions data using EUROSTAT energy and emissions shares between the three sectors/categories
direct_FD_fp_long_disagg = cbind(sector,direct_FD_fp_long) %>%
  mutate(coicop = ifelse(sector == "HH_TRA","CP072",
                         ifelse(sector == "HH_HEAT","CP045","CP05")), 
         five_sectors = ifelse(sector == "HH_TRA", "transport",
                               ifelse(sector == "HH_HEAT", "shelter", "manufactured goods"))) %>%
  left_join(env_ac_ainah_r2_co2, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_ch4, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_n2o, by = c("geo","sector")) %>%
  left_join(env_ac_pefasu, by = c("geo","sector")) %>%
  mutate(direct_FD_co2 = (direct_FD_co2_combustion +
                            direct_FD_co2_noncombustion_cement +
                            direct_FD_co2_noncombustion_lime +
                            direct_FD_co2_agriculture_peatdecay +
                            direct_FD_co2_waste_biogenic +
                            direct_FD_co2_waste_fossil)*share_of_total_co2,
         direct_FD_ch4 = (direct_FD_ch4_combustion +
                            direct_FD_ch4_noncombustion_gas +
                            direct_FD_ch4_noncombustion_oil +
                            direct_FD_ch4_noncombustion_anthracite +
                            direct_FD_ch4_noncombustion_bituminouscoal +
                            direct_FD_ch4_noncombustion_cokingcoal +
                            direct_FD_ch4_noncombustion_lignite +
                            direct_FD_ch4_noncombustion_subbituminouscoal +
                            direct_FD_ch4_noncombustion_oilrefinery +
                            direct_FD_ch4_agriculture +
                            direct_FD_ch4_waste)*share_of_total_ch4,
         direct_FD_n2o = (direct_FD_n2o_combustion +
                            direct_FD_n2o_agriculture)*share_of_total_n2o,
         direct_FD_energy = direct_FD_energy*share_of_total_energy) %>%
  left_join(shares, by = c("year","geo","coicop")) %>%
  mutate(disaggregated_direct_FD_co2 = direct_FD_co2*share,
         disaggregated_direct_FD_ch4 = direct_FD_ch4*share,
         disaggregated_direct_FD_n2o = direct_FD_n2o*share,
         disaggregated_direct_FD_energy = direct_FD_energy*share) %>%
  select(year,geo,sector, quintile,
         coicop, five_sectors,
         disaggregated_direct_FD_co2,
         disaggregated_direct_FD_ch4,
         disaggregated_direct_FD_n2o,
         disaggregated_direct_FD_energy) 

## spread quintiles across columns for CO2
direct_FD_co2 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_co2) %>%
  spread(quintile,disaggregated_direct_FD_co2) %>%
  rename(q1_co2 = QUINTILE1,
         q2_co2 = QUINTILE2,
         q3_co2 = QUINTILE3,
         q4_co2 = QUINTILE4,
         q5_co2 = QUINTILE5) %>%
  mutate(q1_co2_domestic = q1_co2,
         q2_co2_domestic = q2_co2,
         q3_co2_domestic = q3_co2,
         q4_co2_domestic = q4_co2,
         q5_co2_domestic = q5_co2,
         co2_total = q1_co2+q2_co2+q3_co2+q4_co2+q5_co2,
         co2_total_domestic = q1_co2_domestic+
           q2_co2_domestic+q3_co2_domestic+
           q4_co2_domestic+q5_co2_domestic)

## spread quintiles across columns for CH4
direct_FD_ch4 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_ch4) %>%
  spread(quintile,disaggregated_direct_FD_ch4) %>%
  rename(q1_ch4 = QUINTILE1,
         q2_ch4 = QUINTILE2,
         q3_ch4 = QUINTILE3,
         q4_ch4 = QUINTILE4,
         q5_ch4 = QUINTILE5) %>%
  mutate(q1_ch4_domestic = q1_ch4,
         q2_ch4_domestic = q2_ch4,
         q3_ch4_domestic = q3_ch4,
         q4_ch4_domestic = q4_ch4,
         q5_ch4_domestic = q5_ch4,
         ch4_total = q1_ch4+q2_ch4+q3_ch4+q4_ch4+q5_ch4,
         ch4_total_domestic = q1_ch4_domestic+
           q2_ch4_domestic+q3_ch4_domestic+
           q4_ch4_domestic+q5_ch4_domestic)

## spread quintiles across columns for N2O
direct_FD_n2o = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_n2o) %>%
  spread(quintile,disaggregated_direct_FD_n2o) %>%
  rename(q1_n2o = QUINTILE1,
         q2_n2o = QUINTILE2,
         q3_n2o = QUINTILE3,
         q4_n2o = QUINTILE4,
         q5_n2o = QUINTILE5) %>%
  mutate(q1_n2o_domestic = q1_n2o,
         q2_n2o_domestic = q2_n2o,
         q3_n2o_domestic = q3_n2o,
         q4_n2o_domestic = q4_n2o,
         q5_n2o_domestic = q5_n2o,
         n2o_total = q1_n2o+q2_n2o+q3_n2o+q4_n2o+q5_n2o,
         n2o_total_domestic = q1_n2o_domestic+
           q2_n2o_domestic+q3_n2o_domestic+
           q4_n2o_domestic+q5_n2o_domestic)

## spread quintiles across columns for energy use
direct_FD_energy = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_energy) %>%
  spread(quintile,disaggregated_direct_FD_energy) %>%
  rename(q1_energy = QUINTILE1,
         q2_energy = QUINTILE2,
         q3_energy = QUINTILE3,
         q4_energy = QUINTILE4,
         q5_energy = QUINTILE5) %>%
  mutate(q1_energy_domestic = q1_energy,
         q2_energy_domestic = q2_energy,
         q3_energy_domestic = q3_energy,
         q4_energy_domestic = q4_energy,
         q5_energy_domestic = q5_energy,
         energy_total = q1_energy+q2_energy+q3_energy+q4_energy+q5_energy,
         energy_total_domestic = q1_energy_domestic+
           q2_energy_domestic+q3_energy_domestic+
           q4_energy_domestic+q5_energy_domestic)

# join spread direct from hh fd extensions to each other and create co2eq as well
direct_FD_fp_wide = direct_FD_co2 %>%
  left_join(direct_FD_ch4, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_n2o, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_energy, by = c("year","geo",
                                     "sector","coicop",
                                     "five_sectors")) %>%
  mutate(country_of_production = geo) %>%
  mutate(q1_co2eq = q1_co2 + q1_ch4 + q1_n2o,
         q2_co2eq = q2_co2 + q2_ch4 + q2_n2o,
         q3_co2eq = q3_co2 + q3_ch4 + q3_n2o,
         q4_co2eq = q4_co2 + q4_ch4 + q4_n2o,
         q5_co2eq = q5_co2 + q5_ch4 + q5_n2o,
         co2eq_total = q1_co2eq +
           q2_co2eq + q3_co2eq +
           q4_co2eq + q5_co2eq,
         q1_co2eq_domestic = q1_co2_domestic + q1_ch4_domestic + q1_n2o_domestic,
         q2_co2eq_domestic = q2_co2_domestic + q2_ch4_domestic + q2_n2o_domestic,
         q3_co2eq_domestic = q3_co2_domestic + q3_ch4_domestic + q3_n2o_domestic,
         q4_co2eq_domestic = q4_co2_domestic + q4_ch4_domestic + q4_n2o_domestic,
         q5_co2eq_domestic = q5_co2_domestic + q5_ch4_domestic + q5_n2o_domestic,
         co2eq_total_domestic = q1_co2eq_domestic +
           q2_co2eq_domestic + q3_co2eq_domestic +
           q4_co2eq_domestic + q5_co2eq_domestic) %>%
  select(-q1_ch4,
         -q2_ch4,
         -q3_ch4,
         -q4_ch4,
         -q5_ch4,
         -ch4_total,
         -q1_ch4_domestic,
         -q2_ch4_domestic,
         -q3_ch4_domestic,
         -q4_ch4_domestic,
         -q5_ch4_domestic,
         -ch4_total_domestic,
         -q1_n2o,
         -q2_n2o,
         -q3_n2o,
         -q4_n2o,
         -q5_n2o,
         -n2o_total,
         -q1_n2o_domestic,
         -q2_n2o_domestic,
         -q3_n2o_domestic,
         -q4_n2o_domestic,
         -q5_n2o_domestic,
         -n2o_total_domestic)

# create intermediate results dataframe (without direct extensions data) by joining dis-aggregated EXIOBASE fd with total intensity vectors (plus domestic, plus Europe) and calculating co2, co2eq and energy use (total, domestic, and Europe)
results = fd_exiobase %>%
  left_join(TIVs, by = c("year", "country_of_production", "coicop", "sector")) %>%
  left_join(europe_TIVs, by = c("year", "country_of_production", "coicop", "sector", "five_sectors")) %>%
  left_join(domestic_TIVs, by = c("year", "geo", "country_of_production", "coicop", "sector", "five_sectors")) %>%
  transmute(year,geo,country_of_production,sector,coicop,five_sectors,
            QUINTILE1,
            QUINTILE2,
            QUINTILE3,
            QUINTILE4,
            QUINTILE5,
            fd_total = QUINTILE1+QUINTILE2+QUINTILE3+QUINTILE4+QUINTILE5,
            TIV_CO2 = TIV_CO2_combustion + 
              TIV_CO2_noncombustion_cement + 
              TIV_CO2_noncombustion_lime +
              TIV_CO2_agriculture_peatdecay +
              TIV_CO2_waste_biogenic + 
              TIV_CO2_waste_fossil,
            q1_co2 = QUINTILE1*TIV_CO2,
            q2_co2 = QUINTILE2*TIV_CO2,
            q3_co2 = QUINTILE3*TIV_CO2,
            q4_co2 = QUINTILE4*TIV_CO2,
            q5_co2 = QUINTILE5*TIV_CO2,
            co2_total = q1_co2+q2_co2+q3_co2+q4_co2+q5_co2,
            TIV_CO2_domestic = TIV_CO2_combustion_domestic +
              TIV_CO2_noncombustion_cement_domestic +
              TIV_CO2_noncombustion_lime_domestic +
              TIV_CO2_agriculture_peatdecay_domestic +
              TIV_CO2_waste_biogenic_domestic +
              TIV_CO2_waste_fossil_domestic,
            q1_co2_domestic = QUINTILE1*TIV_CO2_domestic,
            q2_co2_domestic = QUINTILE2*TIV_CO2_domestic,
            q3_co2_domestic = QUINTILE3*TIV_CO2_domestic,
            q4_co2_domestic = QUINTILE4*TIV_CO2_domestic,
            q5_co2_domestic = QUINTILE5*TIV_CO2_domestic,
            co2_total_domestic = q1_co2_domestic+q2_co2_domestic+q3_co2_domestic+q4_co2_domestic+q5_co2_domestic,
            TIV_CO2_europe = TIV_CO2_combustion_europe +
              TIV_CO2_noncombustion_cement_europe +
              TIV_CO2_noncombustion_lime_europe +
              TIV_CO2_agriculture_peatdecay_europe +
              TIV_CO2_waste_biogenic_europe +
              TIV_CO2_waste_fossil_europe,
            q1_co2_europe = QUINTILE1*(TIV_CO2_europe - TIV_CO2_domestic),
            q2_co2_europe = QUINTILE2*(TIV_CO2_europe - TIV_CO2_domestic),
            q3_co2_europe = QUINTILE3*(TIV_CO2_europe - TIV_CO2_domestic),
            q4_co2_europe = QUINTILE4*(TIV_CO2_europe - TIV_CO2_domestic),
            q5_co2_europe = QUINTILE5*(TIV_CO2_europe - TIV_CO2_domestic),
            co2_total_europe = q1_co2_europe+q2_co2_europe+q3_co2_europe+q4_co2_europe+q5_co2_europe,
            TIV_CO2eq = TIV_CO2 + 
              TIV_CH4_combustion + 
              TIV_CH4_noncombustion_gas +
              TIV_CH4_noncombustion_oil +
              TIV_CH4_noncombustion_anthracite +
              TIV_CH4_noncombustion_bituminouscoal +
              TIV_CH4_noncombustion_cokingcoal +
              TIV_CH4_noncombustion_lignite +
              TIV_CH4_noncombustion_subbituminouscoal +
              TIV_CH4_noncombustion_oilrefinery +
              TIV_CH4_agriculture +
              TIV_CH4_waste +
              TIV_N2O_combustion + 
              TIV_N2O_agriculture +
              TIV_SF6 + TIV_HFC + TIV_PFC,
            q1_co2eq = QUINTILE1*TIV_CO2eq,
            q2_co2eq = QUINTILE2*TIV_CO2eq,
            q3_co2eq = QUINTILE3*TIV_CO2eq,
            q4_co2eq = QUINTILE4*TIV_CO2eq,
            q5_co2eq = QUINTILE5*TIV_CO2eq,
            co2eq_total = q1_co2eq + q2_co2eq + q3_co2eq + q4_co2eq + q5_co2eq,
            TIV_CO2eq_domestic = TIV_CO2_domestic + 
              TIV_CH4_combustion_domestic + 
              TIV_CH4_noncombustion_gas_domestic +
              TIV_CH4_noncombustion_oil_domestic +
              TIV_CH4_noncombustion_anthracite_domestic +
              TIV_CH4_noncombustion_bituminouscoal_domestic +
              TIV_CH4_noncombustion_cokingcoal_domestic +
              TIV_CH4_noncombustion_lignite_domestic +
              TIV_CH4_noncombustion_subbituminouscoal_domestic +
              TIV_CH4_noncombustion_oilrefinery_domestic +
              TIV_CH4_agriculture_domestic +
              TIV_CH4_waste_domestic +
              TIV_N2O_combustion_domestic + 
              TIV_N2O_agriculture_domestic +
              TIV_SF6_domestic + TIV_HFC_domestic + TIV_PFC_domestic,
            q1_co2eq_domestic = QUINTILE1*TIV_CO2eq_domestic,
            q2_co2eq_domestic = QUINTILE2*TIV_CO2eq_domestic,
            q3_co2eq_domestic = QUINTILE3*TIV_CO2eq_domestic,
            q4_co2eq_domestic = QUINTILE4*TIV_CO2eq_domestic,
            q5_co2eq_domestic = QUINTILE5*TIV_CO2eq_domestic,
            co2eq_total_domestic = q1_co2eq_domestic + q2_co2eq_domestic + q3_co2eq_domestic + q4_co2eq_domestic + q5_co2eq_domestic,
            TIV_CO2eq_europe = TIV_CO2_europe + 
              TIV_CH4_combustion_europe + 
              TIV_CH4_noncombustion_gas_europe +
              TIV_CH4_noncombustion_oil_europe +
              TIV_CH4_noncombustion_anthracite_europe +
              TIV_CH4_noncombustion_bituminouscoal_europe +
              TIV_CH4_noncombustion_cokingcoal_europe +
              TIV_CH4_noncombustion_lignite_europe +
              TIV_CH4_noncombustion_subbituminouscoal_europe +
              TIV_CH4_noncombustion_oilrefinery_europe +
              TIV_CH4_agriculture_europe +
              TIV_CH4_waste_europe +
              TIV_N2O_combustion_europe + 
              TIV_N2O_agriculture_europe +
              TIV_SF6_europe + TIV_HFC_europe + TIV_PFC_europe,
            q1_co2eq_europe = QUINTILE1*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q2_co2eq_europe = QUINTILE2*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q3_co2eq_europe = QUINTILE3*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q4_co2eq_europe = QUINTILE4*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q5_co2eq_europe = QUINTILE5*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            co2eq_total_europe = q1_co2eq_europe + q2_co2eq_europe + q3_co2eq_europe + q4_co2eq_europe + q5_co2eq_europe,
            TIV_energy,
            q1_energy = QUINTILE1*TIV_energy,
            q2_energy = QUINTILE2*TIV_energy,
            q3_energy = QUINTILE3*TIV_energy,
            q4_energy = QUINTILE4*TIV_energy,
            q5_energy = QUINTILE5*TIV_energy,
            energy_total = q1_energy+q2_energy+q3_energy+q4_energy+q5_energy,
            TIV_energy_domestic,
            q1_energy_domestic = QUINTILE1*TIV_energy_domestic,
            q2_energy_domestic = QUINTILE2*TIV_energy_domestic,
            q3_energy_domestic = QUINTILE3*TIV_energy_domestic,
            q4_energy_domestic = QUINTILE4*TIV_energy_domestic,
            q5_energy_domestic = QUINTILE5*TIV_energy_domestic,
            energy_total_domestic = q1_energy_domestic+q2_energy_domestic+q3_energy_domestic+q4_energy_domestic+q5_energy_domestic,
            TIV_energy_europe,
            q1_energy_europe = QUINTILE1*(TIV_energy_europe - TIV_energy_domestic),
            q2_energy_europe = QUINTILE2*(TIV_energy_europe - TIV_energy_domestic),
            q3_energy_europe = QUINTILE3*(TIV_energy_europe - TIV_energy_domestic),
            q4_energy_europe = QUINTILE4*(TIV_energy_europe - TIV_energy_domestic),
            q5_energy_europe = QUINTILE5*(TIV_energy_europe - TIV_energy_domestic),
            energy_total_europe = q1_energy_europe+q2_energy_europe+q3_energy_europe+q4_energy_europe+q5_energy_europe)

# bind intermediate results dataframe with direct from hh fd extensions dataframe
results_with_direct_FD_fp = bind_rows(results,direct_FD_fp_wide)

# create compressed results rds file
## clean names of results file
dat_all = results_with_direct_FD_fp %>%
  clean_names()

## convert sector labels to IDs
sectors = dat_all %>%
  distinct(sector) %>%
  mutate(sector_id = row_number())

## write EXIOBASE production sector ids to derived data folder (as .csv file) for use in creating figures in the main paper (commented out atm - not used by current main paper figures)
# write_csv(sectors, paste0(here("/analysis/data/derived/sectors_method1_ixi.csv"))) 

## convert aggregated sector labels to IDs
sectors_agg = dat_all %>%
  distinct(five_sectors) %>%
  mutate(sector_agg_id = row_number())

## write aggregated sector ids to derived data folder (as .csv file) for use in creating figures in the main paper
write_csv(sectors_agg, paste0(here("/analysis/data/derived/sectors_agg_method1_ixi.csv"))) 

## convert COICOP labels to IDs
coicop = dat_all %>%
  distinct(coicop) %>%
  mutate(coicop_id = row_number()) 

# write COICOP consumption category ids to derived data folder (as .csv file) for use in creating figures in the main paper (commented out atm - not used by current main paper figures)
# write_csv(coicop, paste0(here("/analysis/data/derived/coicop_method1_ixi.csv"))) 

# replace sector text labels with numerical IDs (save space)
dat_compressed = dat_all %>%
  left_join(sectors, by="sector") %>%
  left_join(sectors_agg, by="five_sectors") %>%
  left_join(coicop, by = "coicop") %>%
  select(-c(sector, five_sectors,coicop))

# extract sector aggregation
sector_mapping = dat_compressed %>%
  group_by(sector_id) %>%
  summarise(sector_agg_id = first(sector_agg_id),
            coicop_id = first(coicop_id))

# collapse country of origin
dat_results = dat_compressed %>%
  select(-sector_agg_id,-coicop_id) %>%
  group_by(year, geo, sector_id) %>%
  summarise_if(is.numeric, sum, na.rm = TRUE)

# extract final demand and pivot long
cols_final_demand = c("quintile1", "quintile2", "quintile3", "quintile4", "quintile5")
tmp_fd = dat_results %>%
  select(year, geo, sector_id, cols_final_demand) %>%
  pivot_longer(cols = cols_final_demand,
               names_to = "quintile",
               values_to = "fd_me") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 and pivot long
cols_co2 = c("q1_co2", "q2_co2", "q3_co2", "q4_co2", "q5_co2")
tmp_co2 = dat_results %>%
  select(year, geo, sector_id, cols_co2) %>%
  pivot_longer(cols = cols_co2,
               names_to = "quintile",
               values_to = "co2_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 domestic and pivot long
cols_co2_domestic = c("q1_co2_domestic", "q2_co2_domestic", "q3_co2_domestic", "q4_co2_domestic", "q5_co2_domestic")
tmp_co2_domestic = dat_results %>%
  select(year, geo, sector_id, cols_co2_domestic) %>%
  pivot_longer(cols = cols_co2_domestic,
               names_to = "quintile",
               values_to = "co2_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 europe and pivot long
cols_co2_europe = c("q1_co2_europe", "q2_co2_europe", "q3_co2_europe", "q4_co2_europe", "q5_co2_europe")
tmp_co2_europe = dat_results %>%
  select(year, geo, sector_id, cols_co2_europe) %>%
  pivot_longer(cols = cols_co2_europe,
               names_to = "quintile",
               values_to = "co2_europe_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq and pivot long
cols_co2eq = c("q1_co2eq", "q2_co2eq", "q3_co2eq", "q4_co2eq", "q5_co2eq")
tmp_co2eq = dat_results %>%
  select(year, geo, sector_id, cols_co2eq) %>%
  pivot_longer(cols = cols_co2eq,
               names_to = "quintile",
               values_to = "co2eq_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq domestic and pivot long
cols_co2eq_domestic = c("q1_co2eq_domestic", "q2_co2eq_domestic", "q3_co2eq_domestic", "q4_co2eq_domestic", "q5_co2eq_domestic")
tmp_co2eq_domestic = dat_results %>%
  select(year, geo, sector_id, cols_co2eq_domestic) %>%
  pivot_longer(cols = cols_co2eq_domestic,
               names_to = "quintile",
               values_to = "co2eq_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq europe and pivot long
cols_co2eq_europe = c("q1_co2eq_europe", "q2_co2eq_europe", "q3_co2eq_europe", "q4_co2eq_europe", "q5_co2eq_europe")
tmp_co2eq_europe = dat_results %>%
  select(year, geo, sector_id, cols_co2eq_europe) %>%
  pivot_longer(cols = cols_co2eq_europe,
               names_to = "quintile",
               values_to = "co2eq_europe_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy use and pivot long
cols_energy = c("q1_energy","q2_energy","q3_energy","q4_energy","q5_energy")
tmp_energy = dat_results %>%
  select(year, geo, sector_id, cols_energy) %>%
  pivot_longer(cols = cols_energy,
               names_to = "quintile",
               values_to = "energy_use_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy use domestic and pivot long
cols_energy_domestic = c("q1_energy_domestic","q2_energy_domestic","q3_energy_domestic","q4_energy_domestic","q5_energy_domestic")
tmp_energy_domestic = dat_results %>%
  select(year, geo, sector_id, cols_energy_domestic) %>%
  pivot_longer(cols = cols_energy_domestic,
               names_to = "quintile",
               values_to = "energy_use_domestic_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy use europe and pivot long
cols_energy_europe = c("q1_energy_europe","q2_energy_europe","q3_energy_europe","q4_energy_europe","q5_energy_europe")
tmp_energy_europe = dat_results %>%
  select(year, geo, sector_id, cols_energy_europe) %>%
  pivot_longer(cols = cols_energy_europe,
               names_to = "quintile",
               values_to = "energy_use_europe_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# recombine pivoted extensions
results_recombined = tmp_fd %>%
  left_join(tmp_co2, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2_europe, by = c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq_europe, by = c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy_europe, by = c("year", "geo", "sector_id", "quint"))

# finally re-join aggregated sector IDs
results_formatted = results_recombined %>% 
  left_join(sector_mapping, by="sector_id") %>%
  ungroup() %>%
  select(-coicop_id)

# write formatted results files (.csv and .rds) to 'data_dir_income_stratified_footprints'
write.csv(results_formatted, paste0(data_dir_income_stratified_footprints, "/results_formatted_method1_ixi.csv"))
write_rds(results_formatted, paste0(data_dir_income_stratified_footprints, "/results_formatted_method1_ixi.rds"))

################################################### main paper method, EXIOBASE product-by-product version ############################################### ##########################################################################################################################################################

# EXIOBASE product-by-product version

## set study years
years_exb_pxp = c(2005,2010)

## create NULL dataframe for dis-aggregated final demand
disaggregated_final_demand = NULL

## create NULL dataframe for 'total intensity vectors'
TIVs = NULL

## create NULL dataframe for domestic 'total intensity vectors'
domestic_TIVs = NULL

## create NULL dataframe for rest-of-Europe 'total intensity vectors'
europe_TIVs = NULL

## create NULL dataframe for national footprints
national_fp = NULL

## create NULL dataframe for national territorial energy use/emissions
national_territorial = NULL

## 'for' loop which populates the above NULL dataframes
for (i in years_exb_pxp){
  year_current = i
  
  ## EXIOBASE final demand table without labels (.csv file)
  Exiobase_FD =  read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/FD_",year_current,"_pxp.csv"))[,-1]
  
  ## select household (hh) final demand vectors for study countries
  AT = Exiobase_FD[,1]
  BE = Exiobase_FD[,8]
  BG = Exiobase_FD[,15]
  CY = Exiobase_FD[,22]
  CZ = Exiobase_FD[,29]
  DE = Exiobase_FD[,36]
  DK = Exiobase_FD[,43]
  EE = Exiobase_FD[,50]
  EL = Exiobase_FD[,78]
  ES = Exiobase_FD[,57]
  FI = Exiobase_FD[,64]
  FR = Exiobase_FD[,71]
  HR = Exiobase_FD[,85]
  HU = Exiobase_FD[,92]
  IE = Exiobase_FD[,99]
  IT = Exiobase_FD[,106]
  LT = Exiobase_FD[,113]
  LU = Exiobase_FD[,120]
  LV = Exiobase_FD[,127]
  MT = Exiobase_FD[,134]
  NL = Exiobase_FD[,141]
  NO = Exiobase_FD[,288]
  PL = Exiobase_FD[,148]
  PT = Exiobase_FD[,155]
  RO = Exiobase_FD[,162]
  SE = Exiobase_FD[,169]
  SI = Exiobase_FD[,176]
  SK = Exiobase_FD[,183]
  TR = Exiobase_FD[,274]
  UK = Exiobase_FD[,190]
  
  Eurostat_countries = cbind(AT,BE,BG,CY,CZ,DE,DK,EE,EL,ES,FI,FR,HR,HU,IE,IT,LT,LU,LV,MT,NL,NO,PL,PT,RO,SE,SI,SK,TR,UK)
  
  ## read in EXIOBASE production sector labels written and saved in previous code chunk
  Exiobase_T_labels = read.csv(paste0(data_dir_income_stratified_footprints, "/data/Exiobase_T_labels_pxp_w_coicop_mapping.csv")) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  ## bind EXIOBASE hh final demand with production sector labels
  hh_fd_with_production_sector_labels = cbind(Exiobase_T_labels,Eurostat_countries) %>% rename(geo = V1, sector = V2)
  
  ## replicate each cell of each country's hh final demand as many times as there are income quantiles in the HBS data - in this case: 5
  cells_repeat = data.frame(hh_fd_with_production_sector_labels %>% slice(rep(1:n(), each = 5))) 
  
  ## create quintile names
  quintiles = data.frame(rep(c("QUINTILE1","QUINTILE2","QUINTILE3","QUINTILE4","QUINTILE5"),200)) %>% rename_at(1,~"quintile")
  
  ## bind replicated hh final demand cells to quintile names
  replicated = cbind(cells_repeat,quintiles) %>% rename(country_of_production = geo)
  
  ## convert to long data
  replicated_long = replicated %>% gather(geo, value,-sector,-coicop,-quintile,-five_sectors,-country_of_production)
  
  ## create year column
  year = as.character(rep(year_current,nrow(replicated_long)))
  
  ## bind year with long hh fd data
  replicated_long = cbind(year,replicated_long)
  
  ## populate NULL 'dis-aggregated_final_demand' dataframe with hh fd from each study year
  disaggregated_final_demand = rbind(disaggregated_final_demand, replicated_long)
  
  # load total intensity vectors
  ## CO2 - combustion - air
  Exiobase_TIV_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_combustion_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_combustion_air_", year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_combustion_air_", year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_combustion_europe,TIV_CO2_combustion_not_europe)
  
  ## CO2 - noncombustion - cement - air
  Exiobase_TIV_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_noncombustion_cement_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_cement_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_cement_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_cement_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_cement_europe,TIV_CO2_noncombustion_cement_not_europe)
  
  ## CO2 - noncombustion - lime - air
  Exiobase_TIV_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_noncombustion_lime_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_lime_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_lime_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_lime_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_lime_europe,TIV_CO2_noncombustion_lime_not_europe)
  
  ## CO2 - agriculture - peat decay - air
  Exiobase_TIV_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_agriculture_peatdecay_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_agriculture_peatdecay_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_agriculture_peatdecay_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_agriculture_peatdecay_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_agriculture_peatdecay_europe,TIV_CO2_agriculture_peatdecay_not_europe)
  
  ## CO2 - waste - biogenic - air
  Exiobase_TIV_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_biogenic_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_biogenic_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_biogenic_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_biogenic_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_biogenic_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_biogenic_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_biogenic_europe,TIV_CO2_waste_biogenic_not_europe)
  
  ## CO2 - waste - fossil - air
  Exiobase_TIV_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_co2_waste_fossil_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_fossil_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_fossil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_fossil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_fossil_europe,TIV_CO2_waste_fossil_not_europe)
  
  ## CH4 - combustion - air
  Exiobase_TIV_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_combustion_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_combustion_europe,TIV_CH4_combustion_not_europe)
  
  ## CH4 - noncombustion - gas - air
  Exiobase_TIV_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_gas_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_gas_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_gas_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_gas_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_gas_europe,TIV_CH4_noncombustion_gas_not_europe)
  
  ## CH4 - noncombustion - oil - air
  Exiobase_TIV_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_oil_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oil_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oil_europe,TIV_CH4_noncombustion_oil_not_europe)
  
  ## CH4 - noncombustion - anthracite - air
  Exiobase_TIV_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_anthracite_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_anthracite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_anthracite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_anthracite_europe,TIV_CH4_noncombustion_anthracite_not_europe)
  
  ## CH4 - noncombustion - bituminouscoal - air
  Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_bituminouscoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_bituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_bituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_bituminouscoal_europe,TIV_CH4_noncombustion_bituminouscoal_not_europe)
  
  ## CH4 - noncombustion - cokingcoal - air
  Exiobase_TIV_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_cokingcoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_cokingcoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_cokingcoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_cokingcoal_europe,TIV_CH4_noncombustion_cokingcoal_not_europe)
  
  ## CH4 - noncombustion - lignite - air
  Exiobase_TIV_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_lignite_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_lignite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_lignite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_lignite_europe,TIV_CH4_noncombustion_lignite_not_europe)
  
  ## CH4 - noncombustion - subbituminouscoal - air
  Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_subbituminouscoal_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_subbituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_subbituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_subbituminouscoal_europe,TIV_CH4_noncombustion_subbituminouscoal_not_europe)
  
  ## CH4 - noncombustion - oilrefinery - air
  Exiobase_TIV_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oilrefinery_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oilrefinery_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oilrefinery_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oilrefinery_europe,TIV_CH4_noncombustion_oilrefinery_not_europe)
  
  ## CH4 - agriculture - air
  Exiobase_TIV_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_agriculture_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_agriculture_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_agriculture_europe,TIV_CH4_agriculture_not_europe)
  
  ## CH4 - waste - air
  Exiobase_TIV_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_ch4_CO2eq_waste_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_waste_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_waste_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_waste_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_waste_europe,TIV_CH4_waste_not_europe)
  
  ## N2O - combustion - air
  Exiobase_TIV_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_n2o_CO2eq_combustion_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_combustion_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_combustion_europe,TIV_N2O_combustion_not_europe)
  
  ## N2O - agriculture - air
  Exiobase_TIV_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_n2o_CO2eq_agriculture_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_agriculture_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_agriculture_europe,TIV_N2O_agriculture_not_europe)
  
  ## SF6 - air
  Exiobase_TIV_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_sf6_CO2eq_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_SF6_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_SF6_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_SF6_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_SF6_europe,TIV_SF6_not_europe)
  
  ## HFC - air
  Exiobase_TIV_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_hfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_HFC_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_HFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_HFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_HFC_europe,TIV_HFC_not_europe)
  
  ## PFC - air
  Exiobase_TIV_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_pfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_PFC_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_PFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_PFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_PFC_europe,TIV_PFC_not_europe)
  
  ## energy use
  Exiobase_TIV_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_energy_carrier_use_",year_current,"_pxp.csv"))[,-1]
  
  ### domestic part
  Exiobase_TIV_country_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_energy_carrier_use_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_energy_domestic)
  
  ### non-domestic part
  Exiobase_TIV_europe_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_pxp/TIV_country_breakdown_energy_carrier_use_",year_current,"_pxp.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_energy_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_energy_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_energy_europe,TIV_energy_not_europe)
  
  # bind production sector labels with total intensity vectors
  TIV_with_labels = cbind(Exiobase_T_labels,
                          t(Exiobase_TIV_co2_combustion_bp),
                          t(Exiobase_TIV_co2_noncombustion_cement_bp),
                          t(Exiobase_TIV_co2_noncombustion_lime_bp),
                          t(Exiobase_TIV_co2_agriculture_peatdecay_bp),
                          t(Exiobase_TIV_co2_waste_biogenic_bp),
                          t(Exiobase_TIV_co2_waste_fossil_bp),
                          t(Exiobase_TIV_ch4_combustion_bp),
                          t(Exiobase_TIV_ch4_noncombustion_gas_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oil_bp),
                          t(Exiobase_TIV_ch4_noncombustion_anthracite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_lignite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp),
                          t(Exiobase_TIV_ch4_agriculture_bp),
                          t(Exiobase_TIV_ch4_waste_bp),
                          t(Exiobase_TIV_n2o_combustion_bp),
                          t(Exiobase_TIV_n2o_agriculture_bp),
                          t(Exiobase_TIV_sf6_bp),
                          t(Exiobase_TIV_hfc_bp),
                          t(Exiobase_TIV_pfc_bp),
                          t(Exiobase_TIV_energy_use_bp)) %>% 
    rename(TIV_CO2_combustion = "t(Exiobase_TIV_co2_combustion_bp)",
           TIV_CO2_noncombustion_cement = "t(Exiobase_TIV_co2_noncombustion_cement_bp)",
           TIV_CO2_noncombustion_lime = "t(Exiobase_TIV_co2_noncombustion_lime_bp)",
           TIV_CO2_agriculture_peatdecay = "t(Exiobase_TIV_co2_agriculture_peatdecay_bp)",
           TIV_CO2_waste_biogenic = "t(Exiobase_TIV_co2_waste_biogenic_bp)",
           TIV_CO2_waste_fossil = "t(Exiobase_TIV_co2_waste_fossil_bp)",
           TIV_CH4_combustion = "t(Exiobase_TIV_ch4_combustion_bp)",
           TIV_CH4_noncombustion_gas = "t(Exiobase_TIV_ch4_noncombustion_gas_bp)",
           TIV_CH4_noncombustion_oil = "t(Exiobase_TIV_ch4_noncombustion_oil_bp)",
           TIV_CH4_noncombustion_anthracite = "t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)",
           TIV_CH4_noncombustion_bituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)",
           TIV_CH4_noncombustion_cokingcoal = "t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)",
           TIV_CH4_noncombustion_lignite = "t(Exiobase_TIV_ch4_noncombustion_lignite_bp)",
           TIV_CH4_noncombustion_subbituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)",
           TIV_CH4_noncombustion_oilrefinery = "t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)",
           TIV_CH4_agriculture = "t(Exiobase_TIV_ch4_agriculture_bp)",
           TIV_CH4_waste = "t(Exiobase_TIV_ch4_waste_bp)",
           TIV_N2O_combustion = "t(Exiobase_TIV_n2o_combustion_bp)",
           TIV_N2O_agriculture = "t(Exiobase_TIV_n2o_agriculture_bp)",
           TIV_SF6 = "t(Exiobase_TIV_sf6_bp)",
           TIV_HFC = "t(Exiobase_TIV_hfc_bp)",
           TIV_PFC = "t(Exiobase_TIV_pfc_bp)",
           TIV_energy = "t(Exiobase_TIV_energy_use_bp)") %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK"))  
  
  # create year column
  year = as.character(rep(year_current,nrow(TIV_with_labels)))
  
  # bind year with labeled total intensity vectors
  TIVs_intermediate = cbind(year,TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2)
  
  # populate NULL total intensity vectors dataframe
  TIVs = rbind(TIVs, TIVs_intermediate)
  
  # bind production sector labels with domestic total intensity vectors
  domestic_TIV_with_labels = cbind(Exiobase_T_labels,
                                   Exiobase_TIV_country_breakdown_co2_combustion_bp,
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_fossil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_waste_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_sf6_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_hfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_pfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_energy_use_bp %>% select(-country))  %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK"),
           country = dplyr::recode(country, "GR" = "EL", "GB" = "UK"))
  
  # create year column for domestic TIVs
  year_domestic = as.character(rep(year_current,nrow(domestic_TIV_with_labels)))
  
  # bind year with labeled domestic total intensity vectors
  TIVs_domestic_intermediate = cbind(year_domestic,domestic_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, geo = country, year = year_domestic) %>%
    mutate(TIV_CO2_combustion_domestic = as.numeric(TIV_CO2_combustion_domestic),
           TIV_CO2_noncombustion_cement_domestic = as.numeric(TIV_CO2_noncombustion_cement_domestic),
           TIV_CO2_noncombustion_lime_domestic = as.numeric(TIV_CO2_noncombustion_lime_domestic),
           TIV_CO2_agriculture_peatdecay_domestic = as.numeric(TIV_CO2_agriculture_peatdecay_domestic),
           TIV_CO2_waste_biogenic_domestic = as.numeric(TIV_CO2_waste_biogenic_domestic),
           TIV_CO2_waste_fossil_domestic = as.numeric(TIV_CO2_waste_fossil_domestic),
           TIV_CH4_combustion_domestic = as.numeric(TIV_CH4_combustion_domestic),
           TIV_CH4_noncombustion_gas_domestic = as.numeric(TIV_CH4_noncombustion_gas_domestic),
           TIV_CH4_noncombustion_oil_domestic = as.numeric(TIV_CH4_noncombustion_oil_domestic),
           TIV_CH4_noncombustion_anthracite_domestic = as.numeric(TIV_CH4_noncombustion_anthracite_domestic),
           TIV_CH4_noncombustion_bituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_bituminouscoal_domestic),
           TIV_CH4_noncombustion_cokingcoal_domestic = as.numeric(TIV_CH4_noncombustion_cokingcoal_domestic),
           TIV_CH4_noncombustion_lignite_domestic = as.numeric(TIV_CH4_noncombustion_lignite_domestic),
           TIV_CH4_noncombustion_subbituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_domestic),
           TIV_CH4_noncombustion_oilrefinery_domestic = as.numeric(TIV_CH4_noncombustion_oilrefinery_domestic),
           TIV_CH4_agriculture_domestic = as.numeric(TIV_CH4_agriculture_domestic),
           TIV_CH4_waste_domestic = as.numeric(TIV_CH4_waste_domestic),
           TIV_N2O_combustion_domestic = as.numeric(TIV_N2O_combustion_domestic),
           TIV_N2O_agriculture_domestic = as.numeric(TIV_N2O_agriculture_domestic),
           TIV_SF6_domestic = as.numeric(TIV_SF6_domestic),
           TIV_HFC_domestic = as.numeric(TIV_HFC_domestic),
           TIV_PFC_domestic = as.numeric(TIV_PFC_domestic),
           TIV_energy_domestic = as.numeric(TIV_energy_domestic))
  
  # populate NULL domestic TIVs dataframe
  domestic_TIVs = rbind(domestic_TIVs, TIVs_domestic_intermediate)
  
  # bind production sector labels with European TIVs
  europe_TIV_with_labels = cbind(Exiobase_T_labels,
                                 Exiobase_TIV_europe_breakdown_co2_combustion-bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp,
                                 Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_waste_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_sf6_bp,
                                 Exiobase_TIV_europe_breakdown_hfc_bp,
                                 Exiobase_TIV_europe_breakdown_pfc_bp,
                                 Exiobase_TIV_europe_breakdown_energy_use_bp) %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  # create year column for European TIVs
  year_europe = as.character(rep(year_current,nrow(europe_TIV_with_labels)))
  
  # bind year with labeled European total intensity vectors
  TIVs_europe_intermediate = cbind(year_europe,europe_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, year = year_europe) %>%
    mutate(TIV_CO2_combustion_europe = as.numeric(TIV_CO2_combustion_europe),
           TIV_CO2_noncombustion_cement_europe = as.numeric(TIV_CO2_noncombustion_cement_europe),
           TIV_CO2_noncombustion_lime_europe = as.numeric(TIV_CO2_noncombustion_lime_europe),
           TIV_CO2_agriculture_peatdecay_europe = as.numeric(TIV_CO2_agriculture_peatdecay_europe),
           TIV_CO2_waste_biogenic_europe = as.numeric(TIV_CO2_waste_biogenic_europe),
           TIV_CO2_waste_fossil_europe = as.numeric(TIV_CO2_waste_fossil_europe),
           TIV_CH4_combustion_europe = as.numeric(TIV_CH4_combustion_europe),
           TIV_CH4_noncombustion_gas_europe = as.numeric(TIV_CH4_noncombustion_gas_europe),
           TIV_CH4_noncombustion_oil_europe = as.numeric(TIV_CH4_noncombustion_oil_europe),
           TIV_CH4_noncombustion_anthracite_europe = as.numeric(TIV_CH4_noncombustion_anthracite_europe),
           TIV_CH4_noncombustion_bituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_bituminouscoal_europe),
           TIV_CH4_noncombustion_cokingcoal_europe = as.numeric(TIV_CH4_noncombustion_cokingcoal_europe),
           TIV_CH4_noncombustion_lignite_europe = as.numeric(TIV_CH4_noncombustion_lignite_europe),
           TIV_CH4_noncombustion_subbituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_europe),
           TIV_CH4_noncombustion_oilrefinery_europe = as.numeric(TIV_CH4_noncombustion_oilrefinery_europe),
           TIV_CH4_agriculture_europe = as.numeric(TIV_CH4_agriculture_europe),
           TIV_CH4_waste_europe = as.numeric(TIV_CH4_waste_europe),
           TIV_N2O_combustion_europe = as.numeric(TIV_N2O_combustion_europe),
           TIV_N2O_agriculture_europe = as.numeric(TIV_N2O_agriculture_europe),
           TIV_SF6_europe = as.numeric(TIV_SF6_europe),
           TIV_HFC_europe = as.numeric(TIV_HFC_europe),
           TIV_PFC_europe = as.numeric(TIV_PFC_europe),
           TIV_energy_europe = as.numeric(TIV_energy_europe))
  
  # populate NULL Europe TIVs dataframe
  europe_TIVs = rbind(europe_TIVs, TIVs_europe_intermediate)
  
  # calculating total national footprints
  ## read in EXIOBASE final demand labels and calculate national footprints for each study satellite extension
  Exiobase_FD_labels = as.data.frame(t(read.csv(paste0(data_dir_exiobase, "/Exiobase_FD_labels_pxp.csv")))[-1,-3]) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  national_CO2_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_co2_combustion_bp)
  
  national_CO2_noncombustion_cement_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_cement_bp)
  
  national_CO2_noncombustion_lime_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_lime_bp)
  
  national_CO2_agriculture_peatdecay_footprints = Exiobase_FD * t(Exiobase_TIV_co2_agriculture_peatdecay_bp)
  
  national_CO2_waste_biogenic_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_biogenic_bp)
  
  national_CO2_waste_fossil_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_fossil_bp)

  national_CH4_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_combustion_bp)
  
  national_CH4_noncombustion_gas_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_gas_bp)
  
  national_CH4_noncombustion_oil_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oil_bp)
  
  national_CH4_noncombustion_anthracite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)
  
  national_CH4_noncombustion_bituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)
  
  national_CH4_noncombustion_cokingcoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)
  
  national_CH4_noncombustion_lignite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_lignite_bp)
  
  national_CH4_noncombustion_subbituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)
  
  national_CH4_noncombustion_oilrefinery_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)
  
  national_CH4_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_agriculture_bp)
  
  national_CH4_waste_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_waste_bp)
  
  national_N2O_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_combustion_bp)
  
  national_N2O_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_agriculture_bp)
  
  national_SF6_footprints = Exiobase_FD * t(Exiobase_TIV_sf6_bp)
  
  national_HFC_footprints = Exiobase_FD * t(Exiobase_TIV_hfc_bp)
  
  national_PFC_footprints = Exiobase_FD * t(Exiobase_TIV_pfc_bp)
  
  national_energy_footprints = Exiobase_FD * t(Exiobase_TIV_energy_use_bp)
  
  ## bind FD labels and national footprints together
  national_footprints_w_labels = cbind(Exiobase_FD_labels, 
                                       rowSums(t(national_CO2_combustion_footprints)),
                                       rowSums(t(national_CO2_noncombustion_cement_footprints)), 
                                       rowSums(t(national_CO2_noncombustion_lime_footprints)), 
                                       rowSums(t(national_CO2_agriculture_peatdecay_footprints)), 
                                       rowSums(t(national_CO2_waste_biogenic_footprints)), 
                                       rowSums(t(national_CO2_waste_fossil_footprints)),
                                       rowSums(t(national_CH4_combustion_footprints)),
                                       rowSums(t(national_CH4_noncombustion_gas_footprints)),
                                       rowSums(t(national_CH4_noncombustion_oil_footprints)),
                                       rowSums(t(national_CH4_noncombustion_anthracite_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_cokingcoal_footprints)),
                                       rowSums(t(national_CH4_noncombustion_lignite_footprints)),
                                       rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_oilrefinery_footprints)), 
                                       rowSums(t(national_CH4_agriculture_footprints)),
                                       rowSums(t(national_CH4_waste_footprints)),
                                       rowSums(t(national_N2O_combustion_footprints)),
                                       rowSums(t(national_N2O_agriculture_footprints)),
                                       rowSums(t(national_SF6_footprints)),
                                       rowSums(t(national_HFC_footprints)),
                                       rowSums(t(national_PFC_footprints)),
                                       rowSums(t(national_energy_footprints))) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  ## create year column for national footprints
  year_national_fp = as.character(rep(year_current,nrow(national_footprints_w_labels)))

  # satellite extensions direct from household final demand
  ## load satellite extensions direct from household final demand table from 'data_dir_exiobase' and convert to numeric
  direct_FD_extensions = read.csv(paste0(data_dir_exiobase, "/IOT_", year_current, "_pxp/satellite/F_hh.csv", sep = ""),row.names=NULL,as.is=TRUE)[3:1106,3:345]
  direct_FD_extensions[is.na(direct_FD_extensions)]=0
  direct_FD_extensions = mapply(direct_FD_extensions, FUN = as.numeric)
  direct_FD_extensions = matrix(data=direct_FD_extensions,ncol=343,nrow=1104)
  
  ## extract each study satellite extension
  direct_FD_co2_combustion = direct_FD_extensions[24,]
  direct_FD_co2_noncombustion_cement = direct_FD_extensions[93,]
  direct_FD_co2_noncombustion_lime = direct_FD_extensions[94,]
  direct_FD_co2_agriculture_peatdecay = direct_FD_extensions[428,]
  direct_FD_co2_waste_biogenic = direct_FD_extensions[438,]
  direct_FD_co2_waste_fossil = direct_FD_extensions[439,]
  direct_FD_ch4_combustion = direct_FD_extensions[25,]*28
  direct_FD_ch4_noncombustion_gas = direct_FD_extensions[68,]*28
  direct_FD_ch4_noncombustion_oil = direct_FD_extensions[69,]*28
  direct_FD_ch4_noncombustion_anthracite = direct_FD_extensions[70,]*28
  direct_FD_ch4_noncombustion_bituminouscoal = direct_FD_extensions[71,]*28
  direct_FD_ch4_noncombustion_cokingcoal = direct_FD_extensions[72,]*28
  direct_FD_ch4_noncombustion_lignite = direct_FD_extensions[73,]*28
  direct_FD_ch4_noncombustion_subbituminouscoal = direct_FD_extensions[74,]*28
  direct_FD_ch4_noncombustion_oilrefinery = direct_FD_extensions[75,]*28
  direct_FD_ch4_agriculture = direct_FD_extensions[427,]*28
  direct_FD_ch4_waste = direct_FD_extensions[436,]*28
  direct_FD_n2o_combustion = direct_FD_extensions[26,]*265
  direct_FD_n2o_agriculture = direct_FD_extensions[430,]*265
  direct_FD_sf6 = direct_FD_extensions[424,]*23500
  direct_FD_hfc = direct_FD_extensions[425,]
  direct_FD_pfc = direct_FD_extensions[426,]
  direct_FD_energy = direct_FD_extensions[470,]
  
  ## bind all extensions together
  direct_FD_fp = data.frame(direct_FD_co2_combustion,
                            direct_FD_co2_noncombustion_cement,
                            direct_FD_co2_noncombustion_lime,
                            direct_FD_co2_agriculture_peatdecay,
                            direct_FD_co2_waste_biogenic,
                            direct_FD_co2_waste_fossil,
                            direct_FD_ch4_combustion,
                            direct_FD_ch4_noncombustion_gas,
                            direct_FD_ch4_noncombustion_oil,
                            direct_FD_ch4_noncombustion_anthracite,
                            direct_FD_ch4_noncombustion_bituminouscoal,
                            direct_FD_ch4_noncombustion_cokingcoal,
                            direct_FD_ch4_noncombustion_lignite,
                            direct_FD_ch4_noncombustion_subbituminouscoal,
                            direct_FD_ch4_noncombustion_oilrefinery,
                            direct_FD_ch4_agriculture,
                            direct_FD_ch4_waste,
                            direct_FD_n2o_combustion,
                            direct_FD_n2o_agriculture,
                            direct_FD_sf6,
                            direct_FD_hfc,
                            direct_FD_pfc,
                            direct_FD_energy)
  
  ## bind national footprints and direct extensions from hh final demand together (with year column)
  national_fp_intermediate = as.data.frame(cbind(year_national_fp,
                                         national_footprints_w_labels,
                                         direct_FD_fp)) %>% 
    rename(year = year_national_fp,
           geo = V1, 
           fd_category = V2, 
           co2_combustion = "rowSums(t(national_CO2_combustion_footprints))",
           co2_noncombustion_cement = "rowSums(t(national_CO2_noncombustion_cement_footprints))",
           co2_noncombustion_lime = "rowSums(t(national_CO2_noncombustion_lime_footprints))",
           co2_agriculture_peatdecay = "rowSums(t(national_CO2_agriculture_peatdecay_footprints))",
           co2_waste_biogenic = "rowSums(t(national_CO2_waste_biogenic_footprints))",
           co2_waste_fossil = "rowSums(t(national_CO2_waste_fossil_footprints))",
           ch4_combustion = "rowSums(t(national_CH4_combustion_footprints))",
           ch4_noncombustion_gas = "rowSums(t(national_CH4_noncombustion_gas_footprints))",
           ch4_noncombustion_oil = "rowSums(t(national_CH4_noncombustion_oil_footprints))",
           ch4_noncombustion_anthracite = "rowSums(t(national_CH4_noncombustion_anthracite_footprints))",
           ch4_noncombustion_bituminouscoal = "rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints))",
           ch4_noncombustion_cokingcoal = "rowSums(t(national_CH4_noncombustion_cokingcoal_footprints))",
           ch4_noncombustion_lignite = "rowSums(t(national_CH4_noncombustion_lignite_footprints))",
           ch4_noncombustion_subbituminouscoal = "rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints))",
           ch4_noncombustion_oilrefinery = "rowSums(t(national_CH4_noncombustion_oilrefinery_footprints))",
           ch4_agriculture = "rowSums(t(national_CH4_agriculture_footprints))",
           ch4_waste = "rowSums(t(national_CH4_waste_footprints))",
           n2o_combustion = "rowSums(t(national_N2O_combustion_footprints))",
           n2o_agriculture = "rowSums(t(national_N2O_agriculture_footprints))",
           sf6 = "rowSums(t(national_SF6_footprints))",
           hfc = "rowSums(t(national_HFC_footprints))",
           pfc = "rowSums(t(national_PFC_footprints))",
           energy = "rowSums(t(national_energy_footprints))") %>%
    select(year,
           geo,
           fd_category,
           co2_combustion,
           direct_FD_co2_combustion,
           co2_noncombustion_cement,
           direct_FD_co2_noncombustion_cement,
           co2_noncombustion_lime,
           direct_FD_co2_noncombustion_lime,
           co2_agriculture_peatdecay,
           direct_FD_co2_agriculture_peatdecay,
           co2_waste_biogenic,
           direct_FD_co2_waste_biogenic,
           co2_waste_fossil,
           direct_FD_co2_waste_fossil,
           ch4_combustion,
           direct_FD_ch4_combustion,
           ch4_noncombustion_gas,
           direct_FD_ch4_noncombustion_gas,
           ch4_noncombustion_oil,
           direct_FD_ch4_noncombustion_oil,
           ch4_noncombustion_anthracite,
           direct_FD_ch4_noncombustion_anthracite,
           ch4_noncombustion_bituminouscoal,
           direct_FD_ch4_noncombustion_bituminouscoal,
           ch4_noncombustion_cokingcoal,
           direct_FD_ch4_noncombustion_cokingcoal,
           ch4_noncombustion_lignite,
           direct_FD_ch4_noncombustion_lignite,
           ch4_noncombustion_subbituminouscoal,
           direct_FD_ch4_noncombustion_subbituminouscoal,
           ch4_noncombustion_oilrefinery,
           direct_FD_ch4_noncombustion_oilrefinery,
           ch4_agriculture,
           direct_FD_ch4_agriculture,
           ch4_waste,
           direct_FD_ch4_waste,
           n2o_combustion,
           direct_FD_n2o_combustion,
           n2o_agriculture,
           direct_FD_n2o_agriculture,
           sf6,
           direct_FD_sf6,
           hfc,
           direct_FD_hfc,
           pfc,
           direct_FD_pfc,
           energy,
           direct_FD_energy)
  
  ## populate NULL national footprints dataframe
  national_fp = rbind(national_fp, national_fp_intermediate)
  
  # national territorial
  ## load saved satellite extensions table (as .csv file) from 'data_dir_exiobase' and extract each study satellite extension
  satellite = read.csv(paste0(data_dir_exiobase, "/IOT_", year_current, "_pxp/satellite/satellite_",year_current,"_pxp.csv"))[,-1]
  
  CO2_combustion_air = satellite[24,]
  
  CO2_noncombustion_cement_air = satellite[93,]
  
  CO2_noncombustion_lime_air = satellite[94,]
  
  CO2_agriculture_peatdecay_air = satellite[428,]
  
  CO2_waste_biogenic_air = satellite[438,]
  
  CO2_waste_fossil_air = satellite[439,]
  
  CH4_combustion_air = satellite[25,]
  CH4_combustion_air = CH4_combustion_air*28
  
  CH4_noncombustion_gas_air = satellite[68,]
  CH4_noncombustion_gas_air = CH4_noncombustion_gas_air*28
  
  CH4_noncombustion_oil_air = satellite[69,]
  CH4_noncombustion_oil_air = CH4_noncombustion_oil_air*28
  
  CH4_noncombustion_anthracite_air = satellite[70,]
  CH4_noncombustion_anthracite_air = CH4_noncombustion_anthracite_air*28
  
  CH4_noncombustion_bituminouscoal_air = satellite[71,]
  CH4_noncombustion_bituminouscoal_air = CH4_noncombustion_bituminouscoal_air*28
  
  CH4_noncombustion_cokingcoal_air = satellite[72,]
  CH4_noncombustion_cokingcoal_air = CH4_noncombustion_cokingcoal_air*28
  
  CH4_noncombustion_lignite_air = satellite[73,]
  CH4_noncombustion_lignite_air = CH4_noncombustion_lignite_air*28
  
  CH4_noncombustion_subbituminouscoal_air = satellite[74,]
  CH4_noncombustion_subbituminouscoal_air = CH4_noncombustion_subbituminouscoal_air*28
  
  CH4_noncombustion_oilrefinery_air = satellite[75,]
  CH4_noncombustion_oilrefinery_air = CH4_noncombustion_oilrefinery_air*28
  
  CH4_agriculture_air = satellite[427,]
  CH4_agriculture_air = CH4_agriculture_air*28
  
  CH4_waste_air = satellite[436,]
  CH4_waste_air = CH4_waste_air*28
  
  N2O_combustion_air = satellite[26,]
  N2O_combustion_air = N2O_combustion_air*265
  
  N2O_agriculture_air = satellite[430,]
  N2O_agriculture_air = N2O_agriculture_air*265
  
  SF6_air = satellite[424,]
  SF6_air = SF6_air*23500
  
  HFC_air = satellite[425,]
  
  PFC_air = satellite[426,]
  
  energy_carrier_use = satellite[470,]
  
  ## bind together
  territorial = data.frame(t(CO2_combustion_air),
                           t(CO2_noncombustion_cement_air),
                           t(CO2_noncombustion_lime_air),
                           t(CO2_agriculture_peatdecay_air),
                           t(CO2_waste_biogenic_air),
                           t(CO2_waste_fossil_air),
                           t(CH4_combustion_air),
                           t(CH4_noncombustion_gas_air),
                           t(CH4_noncombustion_oil_air),
                           t(CH4_noncombustion_anthracite_air),
                           t(CH4_noncombustion_bituminouscoal_air),
                           t(CH4_noncombustion_cokingcoal_air),
                           t(CH4_noncombustion_lignite_air),
                           t(CH4_noncombustion_subbituminouscoal_air),
                           t(CH4_noncombustion_oilrefinery_air),
                           t(CH4_agriculture_air),
                           t(CH4_waste_air),
                           t(N2O_combustion_air),
                           t(N2O_agriculture_air),
                           t(SF6_air),
                           t(HFC_air),
                           t(PFC_air),
                           t(energy_carrier_use)) %>%
    rename(CO2_combustion = 1, 
           CO2_noncombustion_cement = 2,
           CO2_noncombustion_lime = 3,
           CO2_agriculture_peatdecay = 4,
           CO2_waste_biogenic = 5,
           CO2_waste_fossil = 6,
           CH4_combustion = 7, 
           CH4_noncombustion_gas = 8,
           CH4_noncombustion_oil = 9,
           CH4_noncombustion_anthracite = 10,
           CH4_noncombustion_bituminouscoal = 11,
           CH4_noncombustion_cokingcoal = 12,
           CH4_noncombustion_lignite = 13,
           CH4_noncombustion_subbituminouscoal = 14,
           CH4_noncombustion_oilrefinery = 15,
           CH4_agriculture = 16,
           CH4_waste = 17,
           N2O_combustion = 18, 
           N2O_agriculture = 19,
           SF6 = 20,
           HFC = 21, PFC = 22, energy = 23)
  
  ## create year column
  year_territorial = as.character(rep(year_current,nrow(territorial)))
  
  ## bind year column, production sector labels and territorial satellite extensions
  territorial_intermediate = as.data.frame(cbind(year_territorial,
                                         Exiobase_T_labels,
                                         territorial)) %>%
    rename(year = year_territorial,
           geo = V1,
           sector = V2) %>%
    select(-coicop,-five_sectors)
  
  ## populate NULL territorial dataframe
  national_territorial = rbind(national_territorial, territorial_intermediate)
  
}

# write national territorial extensions as .csv and .rds files (save to 'data_dir_income_stratified_footprints')
write.csv(national_territorial, paste0(data_dir_income_stratified_footprints, "/national_territorial_pxp.csv"))
write_rds(national_territorial, paste0(data_dir_income_stratified_footprints, "/national_territorial_pxp.rds"))

# write national footprints as .csv and .rds files (save to 'data_dir_income_stratified_footprints')
write.csv(national_fp, paste0(data_dir_income_stratified_footprints, "/national_fp_pxp.csv"))
write_rds(national_fp, paste0(data_dir_income_stratified_footprints, "/national_fp_pxp.rds"))

# calculate quintile shares within each HBS coicop category
shares = join_expenditures %>%
  group_by(coicop,geo,year) %>%
  mutate(share = pps_coicop/sum(pps_coicop))

# join disaggregated EXIOBASE hh final demand dataframe with HBS coicop shares and calculate disaggregated hh final demand using the shares
fd_exiobase = disaggregated_final_demand %>% 
  left_join(shares, by = c("year","geo","coicop","quintile")) %>%
  mutate(disaggregated_fd = value*share) %>%
  select(year,geo,quintile,country_of_production,sector,coicop,disaggregated_fd) %>%
  spread(quintile,disaggregated_fd)

# direct extensions from households
## load EUROSTAT energy use data (no Turkey)
env_ac_pefasu_no_TR = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_pefasu_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,HH_HEAT,HH_TRA,HH_OTH) 

## create column for Turkey using Bulgarian shares between HH_HEAT, HH_TRA, and HH_OTH
env_ac_pefasu_TR = env_ac_pefasu_no_TR %>%
  filter(geo == "BG") %>%
  mutate(geo = dplyr::recode(geo,
                             "BG" = "TR"))

## bind EUROSTAT energy use data (no Turkey) with created Turkey column
env_ac_pefasu = rbind(env_ac_pefasu_no_TR,env_ac_pefasu_TR) %>%
  gather(sector,share_of_total_energy,-geo)

## load EUROSTAT emissions data
env_ac_ainah_r2 = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_ainah_r2_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "Turkey" = "TR",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,AIRPOL,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,airpol,HH_HEAT,HH_TRA,HH_OTH) 

### create separate dataframe for CO2
env_ac_ainah_r2_co2 = env_ac_ainah_r2 %>%
  filter(airpol == "Carbon dioxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_co2,-geo)

### for CH4
env_ac_ainah_r2_ch4 = env_ac_ainah_r2 %>%
  filter(airpol == "Methane") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_ch4,-geo)

### for N2O
env_ac_ainah_r2_n2o = env_ac_ainah_r2 %>%
  filter(airpol == "Nitrous oxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_n2o,-geo)

## filter saved EXIOBASE national footprints dataframe, select direct extensions and repeat each cell 3 times
direct_FD_fp_long = national_fp %>%
  filter(fd_category == "Final consumption expenditure by households",
         geo %in% c("AT",
                    "BE", "BG", "CY", "CZ",
                    "DE" , "DK" , "EE" , 
                    "ES" , "FI" , "FR" , 
                    "UK", "EL", "HR" ,
                    "HU" , "IE" , "IT" ,
                    "LT" , "LU" , "LV" ,
                    "MT" , "NL" , "PL" ,
                    "PT" , "TR" , "SK" ,
                    "SI" , "SE" , "RO" ,
                    "NO")) %>%
  select(year,geo,fd_category,direct_FD_co2_combustion,
         direct_FD_co2_noncombustion_cement,
         direct_FD_co2_noncombustion_lime,
         direct_FD_co2_agriculture_peatdecay,
         direct_FD_co2_waste_biogenic,
         direct_FD_co2_waste_fossil,
         direct_FD_ch4_combustion,
         direct_FD_ch4_noncombustion_gas,
         direct_FD_ch4_noncombustion_oil,
         direct_FD_ch4_noncombustion_anthracite,
         direct_FD_ch4_noncombustion_bituminouscoal,
         direct_FD_ch4_noncombustion_cokingcoal,
         direct_FD_ch4_noncombustion_lignite,
         direct_FD_ch4_noncombustion_subbituminouscoal,
         direct_FD_ch4_noncombustion_oilrefinery,
         direct_FD_ch4_agriculture,
         direct_FD_ch4_waste,
         direct_FD_n2o_combustion,
         direct_FD_n2o_agriculture,
         direct_FD_sf6,
         direct_FD_hfc,
         direct_FD_pfc,
         direct_FD_energy) %>%   
  slice(rep(1:n(), each = 3)) 

## create column with direct household sector names (3 times each)
sector = rep(c("HH_HEAT","HH_TRA","HH_OTH"), nrow(direct_FD_fp_long)/3) 

## bind names with EXIOBASE direct extensions data, map hh sectors to COICOP categories, then bind with EUROSTAT energy and emissions data and dis-aggregate EXIOBASE direct extensions data using EUROSTAT energy and emissions shares between the three sectors/categories
direct_FD_fp_long_disagg = cbind(sector,direct_FD_fp_long) %>%
  mutate(coicop = ifelse(sector == "HH_TRA","CP072",
                         ifelse(sector == "HH_HEAT","CP045","CP05")), 
         five_sectors = ifelse(sector == "HH_TRA", "transport",
                               ifelse(sector == "HH_HEAT", "shelter", "manufactured goods"))) %>%
  left_join(env_ac_ainah_r2_co2, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_ch4, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_n2o, by = c("geo","sector")) %>%
  left_join(env_ac_pefasu, by = c("geo","sector")) %>%
  mutate(direct_FD_co2 = (direct_FD_co2_combustion +
                            direct_FD_co2_noncombustion_cement +
                            direct_FD_co2_noncombustion_lime +
                            direct_FD_co2_agriculture_peatdecay +
                            direct_FD_co2_waste_biogenic +
                            direct_FD_co2_waste_fossil)*share_of_total_co2,
         direct_FD_ch4 = (direct_FD_ch4_combustion +
                            direct_FD_ch4_noncombustion_gas +
                            direct_FD_ch4_noncombustion_oil +
                            direct_FD_ch4_noncombustion_anthracite +
                            direct_FD_ch4_noncombustion_bituminouscoal +
                            direct_FD_ch4_noncombustion_cokingcoal +
                            direct_FD_ch4_noncombustion_lignite +
                            direct_FD_ch4_noncombustion_subbituminouscoal +
                            direct_FD_ch4_noncombustion_oilrefinery +
                            direct_FD_ch4_agriculture +
                            direct_FD_ch4_waste)*share_of_total_ch4,
         direct_FD_n2o = (direct_FD_n2o_combustion +
                            direct_FD_n2o_agriculture)*share_of_total_n2o,
         direct_FD_energy = direct_FD_energy*share_of_total_energy) %>%
  left_join(shares, by = c("year","geo","coicop")) %>%
  mutate(disaggregated_direct_FD_co2 = direct_FD_co2*share,
         disaggregated_direct_FD_ch4 = direct_FD_ch4*share,
         disaggregated_direct_FD_n2o = direct_FD_n2o*share,
         disaggregated_direct_FD_energy = direct_FD_energy*share) %>%
  select(year,geo,sector, quintile,
         coicop, five_sectors,
         disaggregated_direct_FD_co2,
         disaggregated_direct_FD_ch4,
         disaggregated_direct_FD_n2o,
         disaggregated_direct_FD_energy) 

## spread quintiles across columns for CO2
direct_FD_co2 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_co2) %>%
  spread(quintile,disaggregated_direct_FD_co2) %>%
  rename(q1_co2 = QUINTILE1,
         q2_co2 = QUINTILE2,
         q3_co2 = QUINTILE3,
         q4_co2 = QUINTILE4,
         q5_co2 = QUINTILE5) %>%
  mutate(q1_co2_domestic = q1_co2,
         q2_co2_domestic = q2_co2,
         q3_co2_domestic = q3_co2,
         q4_co2_domestic = q4_co2,
         q5_co2_domestic = q5_co2,
         co2_total = q1_co2+q2_co2+q3_co2+q4_co2+q5_co2,
         co2_total_domestic = q1_co2_domestic+
           q2_co2_domestic+q3_co2_domestic+
           q4_co2_domestic+q5_co2_domestic)

## spread quintiles across columns for CH4
direct_FD_ch4 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_ch4) %>%
  spread(quintile,disaggregated_direct_FD_ch4) %>%
  rename(q1_ch4 = QUINTILE1,
         q2_ch4 = QUINTILE2,
         q3_ch4 = QUINTILE3,
         q4_ch4 = QUINTILE4,
         q5_ch4 = QUINTILE5) %>%
  mutate(q1_ch4_domestic = q1_ch4,
         q2_ch4_domestic = q2_ch4,
         q3_ch4_domestic = q3_ch4,
         q4_ch4_domestic = q4_ch4,
         q5_ch4_domestic = q5_ch4,
         ch4_total = q1_ch4+q2_ch4+q3_ch4+q4_ch4+q5_ch4,
         ch4_total_domestic = q1_ch4_domestic+
           q2_ch4_domestic+q3_ch4_domestic+
           q4_ch4_domestic+q5_ch4_domestic)

## spread quintiles across columns for N2O
direct_FD_n2o = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_n2o) %>%
  spread(quintile,disaggregated_direct_FD_n2o) %>%
  rename(q1_n2o = QUINTILE1,
         q2_n2o = QUINTILE2,
         q3_n2o = QUINTILE3,
         q4_n2o = QUINTILE4,
         q5_n2o = QUINTILE5) %>%
  mutate(q1_n2o_domestic = q1_n2o,
         q2_n2o_domestic = q2_n2o,
         q3_n2o_domestic = q3_n2o,
         q4_n2o_domestic = q4_n2o,
         q5_n2o_domestic = q5_n2o,
         n2o_total = q1_n2o+q2_n2o+q3_n2o+q4_n2o+q5_n2o,
         n2o_total_domestic = q1_n2o_domestic+
           q2_n2o_domestic+q3_n2o_domestic+
           q4_n2o_domestic+q5_n2o_domestic)

## spread quintiles across columns for energy use
direct_FD_energy = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_energy) %>%
  spread(quintile,disaggregated_direct_FD_energy) %>%
  rename(q1_energy = QUINTILE1,
         q2_energy = QUINTILE2,
         q3_energy = QUINTILE3,
         q4_energy = QUINTILE4,
         q5_energy = QUINTILE5) %>%
  mutate(q1_energy_domestic = q1_energy,
         q2_energy_domestic = q2_energy,
         q3_energy_domestic = q3_energy,
         q4_energy_domestic = q4_energy,
         q5_energy_domestic = q5_energy,
         energy_total = q1_energy+q2_energy+q3_energy+q4_energy+q5_energy,
         energy_total_domestic = q1_energy_domestic+
           q2_energy_domestic+q3_energy_domestic+
           q4_energy_domestic+q5_energy_domestic)

# join spread direct from hh fd extensions to each other and create co2eq as well
direct_FD_fp_wide = direct_FD_co2 %>%
  left_join(direct_FD_ch4, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_n2o, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_energy, by = c("year","geo",
                                     "sector","coicop",
                                     "five_sectors")) %>%
  mutate(country_of_production = geo) %>%
  mutate(q1_co2eq = q1_co2 + q1_ch4 + q1_n2o,
         q2_co2eq = q2_co2 + q2_ch4 + q2_n2o,
         q3_co2eq = q3_co2 + q3_ch4 + q3_n2o,
         q4_co2eq = q4_co2 + q4_ch4 + q4_n2o,
         q5_co2eq = q5_co2 + q5_ch4 + q5_n2o,
         co2eq_total = q1_co2eq +
           q2_co2eq + q3_co2eq +
           q4_co2eq + q5_co2eq,
         q1_co2eq_domestic = q1_co2_domestic + q1_ch4_domestic + q1_n2o_domestic,
         q2_co2eq_domestic = q2_co2_domestic + q2_ch4_domestic + q2_n2o_domestic,
         q3_co2eq_domestic = q3_co2_domestic + q3_ch4_domestic + q3_n2o_domestic,
         q4_co2eq_domestic = q4_co2_domestic + q4_ch4_domestic + q4_n2o_domestic,
         q5_co2eq_domestic = q5_co2_domestic + q5_ch4_domestic + q5_n2o_domestic,
         co2eq_total_domestic = q1_co2eq_domestic +
           q2_co2eq_domestic + q3_co2eq_domestic +
           q4_co2eq_domestic + q5_co2eq_domestic) %>%
  select(-q1_ch4,
         -q2_ch4,
         -q3_ch4,
         -q4_ch4,
         -q5_ch4,
         -ch4_total,
         -q1_ch4_domestic,
         -q2_ch4_domestic,
         -q3_ch4_domestic,
         -q4_ch4_domestic,
         -q5_ch4_domestic,
         -ch4_total_domestic,
         -q1_n2o,
         -q2_n2o,
         -q3_n2o,
         -q4_n2o,
         -q5_n2o,
         -n2o_total,
         -q1_n2o_domestic,
         -q2_n2o_domestic,
         -q3_n2o_domestic,
         -q4_n2o_domestic,
         -q5_n2o_domestic,
         -n2o_total_domestic)

# create intermediate results dataframe (without direct extensions data) by joining dis-aggregated EXIOBASE fd with total intensity vectors (plus domestic, plus Europe) and calculating co2, co2eq and energy use (total, domestic, and Europe)
results = fd_exiobase %>%
  left_join(TIVs, by = c("year", "country_of_production", "coicop", "sector")) %>%
  left_join(europe_TIVs, by = c("year", "country_of_production", "coicop", "sector", "five_sectors")) %>%
  left_join(domestic_TIVs, by = c("year", "geo", "country_of_production", "coicop", "sector", "five_sectors")) %>%
  transmute(year,geo,country_of_production,sector,coicop,five_sectors,
            QUINTILE1,
            QUINTILE2,
            QUINTILE3,
            QUINTILE4,
            QUINTILE5,
            fd_total = QUINTILE1+QUINTILE2+QUINTILE3+QUINTILE4+QUINTILE5,
            TIV_CO2 = TIV_CO2_combustion + 
              TIV_CO2_noncombustion_cement + 
              TIV_CO2_noncombustion_lime +
              TIV_CO2_agriculture_peatdecay +
              TIV_CO2_waste_biogenic + 
              TIV_CO2_waste_fossil,
            q1_co2 = QUINTILE1*TIV_CO2,
            q2_co2 = QUINTILE2*TIV_CO2,
            q3_co2 = QUINTILE3*TIV_CO2,
            q4_co2 = QUINTILE4*TIV_CO2,
            q5_co2 = QUINTILE5*TIV_CO2,
            co2_total = q1_co2+q2_co2+q3_co2+q4_co2+q5_co2,
            TIV_CO2_domestic = TIV_CO2_combustion_domestic +
              TIV_CO2_noncombustion_cement_domestic +
              TIV_CO2_noncombustion_lime_domestic +
              TIV_CO2_agriculture_peatdecay_domestic +
              TIV_CO2_waste_biogenic_domestic +
              TIV_CO2_waste_fossil_domestic,
            q1_co2_domestic = QUINTILE1*TIV_CO2_domestic,
            q2_co2_domestic = QUINTILE2*TIV_CO2_domestic,
            q3_co2_domestic = QUINTILE3*TIV_CO2_domestic,
            q4_co2_domestic = QUINTILE4*TIV_CO2_domestic,
            q5_co2_domestic = QUINTILE5*TIV_CO2_domestic,
            co2_total_domestic = q1_co2_domestic+q2_co2_domestic+q3_co2_domestic+q4_co2_domestic+q5_co2_domestic,
            TIV_CO2_europe = TIV_CO2_combustion_europe +
              TIV_CO2_noncombustion_cement_europe +
              TIV_CO2_noncombustion_lime_europe +
              TIV_CO2_agriculture_peatdecay_europe +
              TIV_CO2_waste_biogenic_europe +
              TIV_CO2_waste_fossil_europe,
            q1_co2_europe = QUINTILE1*(TIV_CO2_europe - TIV_CO2_domestic),
            q2_co2_europe = QUINTILE2*(TIV_CO2_europe - TIV_CO2_domestic),
            q3_co2_europe = QUINTILE3*(TIV_CO2_europe - TIV_CO2_domestic),
            q4_co2_europe = QUINTILE4*(TIV_CO2_europe - TIV_CO2_domestic),
            q5_co2_europe = QUINTILE5*(TIV_CO2_europe - TIV_CO2_domestic),
            co2_total_europe = q1_co2_europe+q2_co2_europe+q3_co2_europe+q4_co2_europe+q5_co2_europe,
            TIV_CO2eq = TIV_CO2 + 
              TIV_CH4_combustion + 
              TIV_CH4_noncombustion_gas +
              TIV_CH4_noncombustion_oil +
              TIV_CH4_noncombustion_anthracite +
              TIV_CH4_noncombustion_bituminouscoal +
              TIV_CH4_noncombustion_cokingcoal +
              TIV_CH4_noncombustion_lignite +
              TIV_CH4_noncombustion_subbituminouscoal +
              TIV_CH4_noncombustion_oilrefinery +
              TIV_CH4_agriculture +
              TIV_CH4_waste +
              TIV_N2O_combustion + 
              TIV_N2O_agriculture +
              TIV_SF6 + TIV_HFC + TIV_PFC,
            q1_co2eq = QUINTILE1*TIV_CO2eq,
            q2_co2eq = QUINTILE2*TIV_CO2eq,
            q3_co2eq = QUINTILE3*TIV_CO2eq,
            q4_co2eq = QUINTILE4*TIV_CO2eq,
            q5_co2eq = QUINTILE5*TIV_CO2eq,
            co2eq_total = q1_co2eq + q2_co2eq + q3_co2eq + q4_co2eq + q5_co2eq,
            TIV_CO2eq_domestic = TIV_CO2_domestic + 
              TIV_CH4_combustion_domestic + 
              TIV_CH4_noncombustion_gas_domestic +
              TIV_CH4_noncombustion_oil_domestic +
              TIV_CH4_noncombustion_anthracite_domestic +
              TIV_CH4_noncombustion_bituminouscoal_domestic +
              TIV_CH4_noncombustion_cokingcoal_domestic +
              TIV_CH4_noncombustion_lignite_domestic +
              TIV_CH4_noncombustion_subbituminouscoal_domestic +
              TIV_CH4_noncombustion_oilrefinery_domestic +
              TIV_CH4_agriculture_domestic +
              TIV_CH4_waste_domestic +
              TIV_N2O_combustion_domestic + 
              TIV_N2O_agriculture_domestic +
              TIV_SF6_domestic + TIV_HFC_domestic + TIV_PFC_domestic,
            q1_co2eq_domestic = QUINTILE1*TIV_CO2eq_domestic,
            q2_co2eq_domestic = QUINTILE2*TIV_CO2eq_domestic,
            q3_co2eq_domestic = QUINTILE3*TIV_CO2eq_domestic,
            q4_co2eq_domestic = QUINTILE4*TIV_CO2eq_domestic,
            q5_co2eq_domestic = QUINTILE5*TIV_CO2eq_domestic,
            co2eq_total_domestic = q1_co2eq_domestic + q2_co2eq_domestic + q3_co2eq_domestic + q4_co2eq_domestic + q5_co2eq_domestic,
            TIV_CO2eq_europe = TIV_CO2_europe + 
              TIV_CH4_combustion_europe + 
              TIV_CH4_noncombustion_gas_europe +
              TIV_CH4_noncombustion_oil_europe +
              TIV_CH4_noncombustion_anthracite_europe +
              TIV_CH4_noncombustion_bituminouscoal_europe +
              TIV_CH4_noncombustion_cokingcoal_europe +
              TIV_CH4_noncombustion_lignite_europe +
              TIV_CH4_noncombustion_subbituminouscoal_europe +
              TIV_CH4_noncombustion_oilrefinery_europe +
              TIV_CH4_agriculture_europe +
              TIV_CH4_waste_europe +
              TIV_N2O_combustion_europe + 
              TIV_N2O_agriculture_europe +
              TIV_SF6_europe + TIV_HFC_europe + TIV_PFC_europe,
            q1_co2eq_europe = QUINTILE1*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q2_co2eq_europe = QUINTILE2*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q3_co2eq_europe = QUINTILE3*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q4_co2eq_europe = QUINTILE4*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            q5_co2eq_europe = QUINTILE5*(TIV_CO2eq_europe - TIV_CO2eq_domestic),
            co2eq_total_europe = q1_co2eq_europe + q2_co2eq_europe + q3_co2eq_europe + q4_co2eq_europe + q5_co2eq_europe,
            TIV_energy,
            q1_energy = QUINTILE1*TIV_energy,
            q2_energy = QUINTILE2*TIV_energy,
            q3_energy = QUINTILE3*TIV_energy,
            q4_energy = QUINTILE4*TIV_energy,
            q5_energy = QUINTILE5*TIV_energy,
            energy_total = q1_energy+q2_energy+q3_energy+q4_energy+q5_energy,
            TIV_energy_domestic,
            q1_energy_domestic = QUINTILE1*TIV_energy_domestic,
            q2_energy_domestic = QUINTILE2*TIV_energy_domestic,
            q3_energy_domestic = QUINTILE3*TIV_energy_domestic,
            q4_energy_domestic = QUINTILE4*TIV_energy_domestic,
            q5_energy_domestic = QUINTILE5*TIV_energy_domestic,
            energy_total_domestic = q1_energy_domestic+q2_energy_domestic+q3_energy_domestic+q4_energy_domestic+q5_energy_domestic,
            TIV_energy_europe,
            q1_energy_europe = QUINTILE1*(TIV_energy_europe - TIV_energy_domestic),
            q2_energy_europe = QUINTILE2*(TIV_energy_europe - TIV_energy_domestic),
            q3_energy_europe = QUINTILE3*(TIV_energy_europe - TIV_energy_domestic),
            q4_energy_europe = QUINTILE4*(TIV_energy_europe - TIV_energy_domestic),
            q5_energy_europe = QUINTILE5*(TIV_energy_europe - TIV_energy_domestic),
            energy_total_europe = q1_energy_europe+q2_energy_europe+q3_energy_europe+q4_energy_europe+q5_energy_europe)

# bind intermediate results dataframe with direct from hh fd extensions dataframe
results_with_direct_FD_fp = bind_rows(results,direct_FD_fp_wide)

# create compressed results rds file
## clean names of results file
dat_all = results_with_direct_FD_fp %>%
  clean_names()

# convert sector labels to IDs
sectors = dat_all %>%
  distinct(sector) %>%
  mutate(sector_id = row_number())

## write EXIOBASE production sector ids to derived data folder (as .csv file) for use in creating figures in the main paper (commented out atm - not used by current main paper figures)
#write_csv(sectors, paste0(here("/analysis/data/derived/si/sectors_method1_pxp.csv")))

# convert aggregated sector labels to IDs
sectors_agg = dat_all %>%
  distinct(five_sectors) %>%
  mutate(sector_agg_id = row_number())

## write aggregated sector ids to derived data folder (as .csv file) for use in creating figures in the main paper. If interested in looking at a sectoral breakdown of the product-by-product version results, un-comment line below
#write_csv(sectors_agg, paste0(here("analysis/data/derived/si/sectors_agg_method1_pxp.csv")))

# convert COICOP labels to IDs
coicop = dat_all %>%
  distinct(coicop) %>%
  mutate(coicop_id = row_number()) 

## write COICOP consumption category ids to derived data folder (as .csv file) for use in creating figures in the main paper (commented out atm - not used by current main paper figures)
#write_csv(coicop, paste0(here("analysis/data/derived/si/coicop_method1_pxp.csv")))

# replace sector text labels with numerical IDs (save space)
dat_compressed = dat_all %>%
  left_join(sectors, by="sector") %>%
  left_join(sectors_agg, by="five_sectors") %>%
  left_join(coicop, by = "coicop") %>%
  select(-c(sector, five_sectors,coicop))

# extract sector aggregation
sector_mapping = dat_compressed %>%
  group_by(sector_id) %>%
  summarise(sector_agg_id = first(sector_agg_id),
            coicop_id = first(coicop_id))

# collapse country of origin
dat_results = dat_compressed %>%
  select(-sector_agg_id,-coicop_id) %>%
  group_by(year, geo, sector_id) %>%
  summarise_if(is.numeric, sum, na.rm = TRUE)

# extract final demand and pivot long
cols_final_demand = c("quintile1", "quintile2", "quintile3", "quintile4", "quintile5")
tmp_fd = dat_results %>%
  select(year, geo, sector_id, cols_final_demand) %>%
  pivot_longer(cols = cols_final_demand,
               names_to = "quintile",
               values_to = "fd_me") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 and pivot long
cols_co2 = c("q1_co2", "q2_co2", "q3_co2", "q4_co2", "q5_co2")
tmp_co2 = dat_results %>%
  select(year, geo, sector_id, cols_co2) %>%
  pivot_longer(cols = cols_co2,
               names_to = "quintile",
               values_to = "co2_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 domestic and pivot long
cols_co2_domestic = c("q1_co2_domestic", "q2_co2_domestic", "q3_co2_domestic", "q4_co2_domestic", "q5_co2_domestic")
tmp_co2_domestic = dat_results %>%
  select(year, geo, sector_id, cols_co2_domestic) %>%
  pivot_longer(cols = cols_co2_domestic,
               names_to = "quintile",
               values_to = "co2_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2 europe and pivot long
cols_co2_europe = c("q1_co2_europe", "q2_co2_europe", "q3_co2_europe", "q4_co2_europe", "q5_co2_europe")
tmp_co2_europe = dat_results %>%
  select(year, geo, sector_id, cols_co2_europe) %>%
  pivot_longer(cols = cols_co2_europe,
               names_to = "quintile",
               values_to = "co2_europe_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq and pivot long
cols_co2eq = c("q1_co2eq", "q2_co2eq", "q3_co2eq", "q4_co2eq", "q5_co2eq")
tmp_co2eq = dat_results %>%
  select(year, geo, sector_id, cols_co2eq) %>%
  pivot_longer(cols = cols_co2eq,
               names_to = "quintile",
               values_to = "co2eq_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq domestic and pivot long
cols_co2eq_domestic = c("q1_co2eq_domestic", "q2_co2eq_domestic", "q3_co2eq_domestic", "q4_co2eq_domestic", "q5_co2eq_domestic")
tmp_co2eq_domestic = dat_results %>%
  select(year, geo, sector_id, cols_co2eq_domestic) %>%
  pivot_longer(cols = cols_co2eq_domestic,
               names_to = "quintile",
               values_to = "co2eq_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract co2eq europe and pivot long
cols_co2eq_europe = c("q1_co2eq_europe", "q2_co2eq_europe", "q3_co2eq_europe", "q4_co2eq_europe", "q5_co2eq_europe")
tmp_co2eq_europe = dat_results %>%
  select(year, geo, sector_id, cols_co2eq_europe) %>%
  pivot_longer(cols = cols_co2eq_europe,
               names_to = "quintile",
               values_to = "co2eq_europe_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy use and pivot long
cols_energy = c("q1_energy","q2_energy","q3_energy","q4_energy","q5_energy")
tmp_energy = dat_results %>%
  select(year, geo, sector_id, cols_energy) %>%
  pivot_longer(cols = cols_energy,
               names_to = "quintile",
               values_to = "energy_use_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy domestic and pivot long
cols_energy_domestic = c("q1_energy_domestic","q2_energy_domestic","q3_energy_domestic","q4_energy_domestic","q5_energy_domestic")
tmp_energy_domestic = dat_results %>%
  select(year, geo, sector_id, cols_energy_domestic) %>%
  pivot_longer(cols = cols_energy_domestic,
               names_to = "quintile",
               values_to = "energy_use_domestic_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# extract energy europe and pivot long
cols_energy_europe = c("q1_energy_europe","q2_energy_europe","q3_energy_europe","q4_energy_europe","q5_energy_europe")
tmp_energy_europe = dat_results %>%
  select(year, geo, sector_id, cols_energy_europe) %>%
  pivot_longer(cols = cols_energy_europe,
               names_to = "quintile",
               values_to = "energy_use_europe_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

# recombine pivoted extensions
results_recombined = tmp_fd %>%
  left_join(tmp_co2, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2_europe, by = c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_co2eq_europe, by = c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy_domestic, by=c("year", "geo", "sector_id", "quint")) %>%
  left_join(tmp_energy_europe, by = c("year", "geo", "sector_id", "quint"))

# finally re-join aggregated sector IDs
results_formatted = results_recombined %>% 
  left_join(sector_mapping, by="sector_id") %>%
  ungroup() %>%
  select(-coicop_id)

# write formatted results files (.csv and .rds) to 'data_dir_income_stratified_footprints'
write.csv(results_formatted, paste0(data_dir_income_stratified_footprints, "/results_formatted_method1_pxp.csv"))
write_rds(results_formatted, paste0(data_dir_income_stratified_footprints, "/results_formatted_method1_pxp.rds"))


################################################### !!!! method 2 !!!! - IXI version #############################
###############################################################################################################################################################
###############################################################################################################################################################


# 'results' data frame the second way

# aggregate - playing around trying to go the other way

# load 'mean expenditure by quintile' data
hbs_exp_t133 = read_csv(paste0(data_dir_income_stratified_footprints, "/hbs_exp_t133.csv"))
# rename and arrange by country
mean_expenditure_by_quintile = hbs_exp_t133 %>% 
  rename(geo = 3, quintile = "quantile") %>%
  arrange(geo)

# load 'mean expenditure by quintile and coicop' data
hbs_str_t223 = read_csv(paste0(data_dir_income_stratified_footprints, "/hbs_str_t223.csv"))
# rename and arrange by country
mean_expenditure_by_coicop_sector = hbs_str_t223 %>% 
  rename(geo = 4, quintile = "quantile") %>%
  arrange(geo) 

# create long data sets for both

mean_expenditure_by_quintile_long = mean_expenditure_by_quintile %>%
  filter(unit == "PPS_HH") %>%
  filter(!(quintile %in% c("UNK","TOTAL"))) %>%
  select(-unit) %>%
  gather(year,euro_pps,-quintile,-geo) 

mean_expenditure_by_coicop_sector_long = mean_expenditure_by_coicop_sector %>% 
  filter(!(quintile %in% c("UNK","TOTAL"))) %>%
  select(-unit) %>%
  gather(year,pm,-quintile,-coicop,-geo) %>%
  mutate(coicop = dplyr::recode(coicop, "CP041" = "rent",        
                                "CP042" = "rent")) %>%           
  group_by(geo,quintile,coicop,year) %>%                               
  mutate(pm = parse_number(pm),
         pm = as.numeric(pm)) %>%
  summarise(pm = sum(pm, na.rm = TRUE)) %>%
  ungroup() %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE1" & 
                       coicop == "CP072", 92-21-14,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE2" & 
                       coicop == "CP072", 108-22-12,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE3" & 
                       coicop == "CP072", 124-32-11,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE4" & 
                       coicop == "CP072", 133-43-10,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2005 & quintile == "QUINTILE5" & 
                       coicop == "CP072", 162-81-11,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE1" & 
                       coicop == "CP044", 412-4-78-322,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE2" & 
                       coicop == "CP044", 355-5-68-265,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE3" & 
                       coicop == "CP044", 325-8-64-229,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE4" & 
                       coicop == "CP044", 300-9-58-204,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2010 & quintile == "QUINTILE5" & 
                       coicop == "CP044", 249-10-46-167,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE1" & 
                       coicop == "CP044", 433-3-82-340,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE2" & 
                       coicop == "CP044", 376-6-70-284,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE3" & 
                       coicop == "CP044", 351-9-67-251,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE4" & 
                       coicop == "CP044", 326-10-61-228,pm)) %>%
  mutate(pm = ifelse(geo == "DE" & year == 2015 & quintile == "QUINTILE5" & 
                       coicop == "CP044", 280-9-49-195,pm))

join_expenditures = mean_expenditure_by_coicop_sector_long %>% 
  left_join(mean_expenditure_by_quintile_long, by = c("geo","quintile","year")) %>%
  mutate(euro_pps = as.numeric(euro_pps),
         pm = as.numeric(pm),
         euro_pps_coicop = pm*(euro_pps/1000))


# load margin tables

trade_and_transport = read.csv(paste0(data_dir_income_stratified_footprints, "/SNA_TABLE45_20042020103737298.csv")) %>%
  select(LOCATION, PRODUCT, Product, Year, Value) %>% 
  mutate(geo = dplyr::recode(LOCATION,"AUT" = "AT", 
                             "BEL" = "BE",
                             "CYP" = "CY",
                             "CZE" = "CZ",
                             "DNK" = "DK",
                             "EST" = "EE",
                             "FIN" = "FI",
                             "FRA" = "FR",
                             "DEU" = "DE",
                             "GRC" = "EL",
                             "HUN" = "HU",
                             "IRL" = "IE",
                             "ITA" = "IT",
                             "LVA" = "LV",
                             "LTU" = "LT",
                             "LUX" = "LU",
                             "MLT" = "MT",
                             "MNE" = "ME",
                             "NLD" = "NL",
                             "NOR" = "NO",
                             "POL" = "PL",
                             "PRT" = "PT",
                             "ROU" = "RO",
                             "SRB" = "RS",
                             "SVK" = "SK",
                             "SVN" = "SI",
                             "ESP" = "ES",
                             "SWE" = "SE",
                             "CHE" = "CH",
                             "MKD" = "MK",
                             "TUR" = "TR",
                             "GBR" = "UK",
                             "BGR" = "BG",
                             "HRV" = "HR")) %>%
  select(geo, Year, PRODUCT, Value) %>%
  rename(year = Year,
         trade_and_transport = Value) %>%
  mutate(trade_and_transport = trade_and_transport/100) %>%
  rename(geo_join = geo)



taxes_less_subsidies = read.csv(paste0(data_dir_income_stratified_footprints, "/SNA_TABLE45_20042020104120395.csv")) %>%
  select(LOCATION, PRODUCT, Product, Year, Value) %>%
  mutate(geo = dplyr::recode(LOCATION,"AUT" = "AT", 
                             "BEL" = "BE",
                             "CYP" = "CY",
                             "CZE" = "CZ",
                             "DNK" = "DK",
                             "EST" = "EE",
                             "FIN" = "FI",
                             "FRA" = "FR",
                             "DEU" = "DE",
                             "GRC" = "EL",
                             "HUN" = "HU",
                             "IRL" = "IE",
                             "ITA" = "IT",
                             "LVA" = "LV",
                             "LTU" = "LT",
                             "LUX" = "LU",
                             "MLT" = "MT",
                             "MNE" = "ME",
                             "NLD" = "NL",
                             "NOR" = "NO",
                             "POL" = "PL",
                             "PRT" = "PT",
                             "ROU" = "RO",
                             "SRB" = "RS",
                             "SVK" = "SK",
                             "SVN" = "SI",
                             "ESP" = "ES",
                             "SWE" = "SE",
                             "CHE" = "CH",
                             "MKD" = "MK",
                             "TUR" = "TR",
                             "GBR" = "UK",
                             "BGR" = "BG",
                             "HRV" = "HR")) %>%
  select(geo, Year, PRODUCT, Value) %>%
  rename(year = Year,
         taxes_less_subsidies = Value) %>%
  mutate(taxes_less_subsidies = taxes_less_subsidies/100) %>%
  rename(geo_join = geo)

# create margins dataframe

geo_real = rep(c("AT", 
                 "BE",
                 "CY",
                 "CZ",
                 "DK",
                 "EE",
                 "FI",
                 "FR",
                 "DE",
                 "EL",
                 "HU",
                 "IE",
                 "IT",
                 "LV",
                 "LT",
                 "LU",
                 "MT",
                 "ME",
                 "NL",
                 "NO",
                 "PL",
                 "PT",
                 "RO",
                 "RS",
                 "SK",
                 "SI",
                 "ES",
                 "SE",
                 "MK",
                 "TR",
                 "UK",
                 "BG",
                 "HR"),each = 16)

geo_join = rep(c("AT", 
                 "BE",
                 "CY",
                 "CZ",
                 "DK",
                 "LV",
                 "FI",
                 "FR",
                 "AT",
                 "EL",
                 "HU",
                 "UK",
                 "IT",
                 "LV",
                 "LV",
                 "LU",
                 "MT",
                 "ME",
                 "NL",
                 "FI",
                 "PL",
                 "PT",
                 "RO",
                 "RS",
                 "SK",
                 "SI",
                 "PT",
                 "FI",
                 "MK",
                 "BG",
                 "UK",
                 "BG",
                 "HR"),each = 16)

year = rep(2010,length(geo_real))

PRODUCT = c("P10_12",
            "P13_15",
            "P68A",
            "PF",
            "PE",
            "PD",
            "PC",
            "PQ",
            "P29",
            "P19",
            "P30",
            "P61",
            "PR",
            "PP",
            "PI",
            "PS")

margin_sectors = data.frame(geo_real,geo_join,year,PRODUCT)

# join everything - and impute

margins = margin_sectors %>%
  left_join(taxes_less_subsidies, by = c("geo_join","year","PRODUCT")) %>%
  left_join(trade_and_transport, by = c("geo_join","year","PRODUCT")) %>%
  select(-geo_join) %>%
  rename(geo = geo_real) %>%
  mutate(taxes_less_subsidies = ifelse(geo == "CZ" & PRODUCT == "P68A", 0, taxes_less_subsidies)) %>%
  mutate(trade_and_transport = ifelse(geo == "CZ" & PRODUCT == "P68A", 0, trade_and_transport)) %>%
  mutate(taxes_less_subsidies = ifelse(geo == "SK" & PRODUCT == "P68A", 0, taxes_less_subsidies)) %>%
  mutate(trade_and_transport = ifelse(geo == "SK" & PRODUCT == "P68A", 0, trade_and_transport)) %>%
  select(-year)


# join margin data to join_expenditures 

with_margins = join_expenditures %>%
  mutate(year = as.numeric(year),
         PRODUCT = dplyr::recode(coicop,
                                 "CP011" =  "P10_12",
                                 "CP012" =  "P10_12",
                                 "CP02" = "P10_12",
                                 "CP03" = "P13_15",
                                 "rent" = "P68A",
                                 "CP043" = "PF",
                                 "CP044" = "PE",
                                 "CP045" = "PD",
                                 "CP05" = "PC",
                                 "CP06" = "PQ",
                                 "CP071" = "P29",
                                 "CP072" = "P19",
                                 "CP073" = "P30",
                                 "CP08" = "P61",
                                 "CP09" = "PR",
                                 "CP10" = "PP",
                                 "CP11" = "PI",
                                 "CP12" = "PS")) %>%
  left_join(margins, by = c("geo","PRODUCT")) %>%
  mutate(euro_pps_coicop_bp = euro_pps_coicop*(1 - (trade_and_transport + taxes_less_subsidies))) 


# re-create expenditure 

mean_expenditure_by_quintile_long_bp = with_margins %>%
  group_by(quintile,geo,year) %>%
  summarise(euro_pps_bp = sum(euro_pps_coicop_bp, na.rm = TRUE))


mean_expenditure_by_coicop_sector_long_bp = with_margins %>%
  left_join(mean_expenditure_by_quintile_long_bp, by = c("quintile","geo","year")) %>%
  mutate(pm_bp = (euro_pps_coicop_bp/euro_pps_bp)*1000) %>%
  select(quintile,coicop,geo,year,pm_bp)


###

shares = join_expenditures %>%
  group_by(coicop,geo,year) %>%
  mutate(share = euro_pps_coicop/sum(euro_pps_coicop))


################################################### !!!! method 2 - IXI version - PPS HH NO RENT !!!! ####################################################
##########################################################################################################################################################
##########################################################################################################################################################

# Exiobase - ixi version

years_exb_ixi = c(2005,2010,2015)


Eurostat_countries_hh_fd = NULL

total_fd = NULL

TIVs = NULL

domestic_TIVs = NULL

europe_TIVs = NULL

national_fp = NULL

for (i in years_exb_ixi){
  year_current = i
  
  Exiobase_FD =  read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/FD_",year_current,"_ixi.csv"))[,-1]
  
  # select household final demand vectors for relevant countries - figure out how to soft code this
  
  
  AT = Exiobase_FD[,1]
  BE = Exiobase_FD[,8]
  BG = Exiobase_FD[,15]
  CY = Exiobase_FD[,22]
  CZ = Exiobase_FD[,29]
  DE = Exiobase_FD[,36]
  DK = Exiobase_FD[,43]
  EE = Exiobase_FD[,50]
  EL = Exiobase_FD[,78]
  ES = Exiobase_FD[,57]
  FI = Exiobase_FD[,64]
  FR = Exiobase_FD[,71]
  HR = Exiobase_FD[,85]
  HU = Exiobase_FD[,92]
  IE = Exiobase_FD[,99]
  IT = Exiobase_FD[,106]
  LT = Exiobase_FD[,113]
  LU = Exiobase_FD[,120]
  LV = Exiobase_FD[,127]
  MT = Exiobase_FD[,134]
  NL = Exiobase_FD[,141]
  NO = Exiobase_FD[,288]
  PL = Exiobase_FD[,148]
  PT = Exiobase_FD[,155]
  RO = Exiobase_FD[,162]
  SE = Exiobase_FD[,169]
  SI = Exiobase_FD[,176]
  SK = Exiobase_FD[,183]
  TR = Exiobase_FD[,274]
  UK = Exiobase_FD[,190]
  
  Eurostat_countries = cbind(AT,BE,BG,CY,CZ,DE,DK,EE,EL,ES,FI,FR,HR,HU,IE,IT,LT,LU,LV,MT,NL,NO,PL,PT,RO,SE,SI,SK,TR,UK)
  
  year = as.character(rep(year_current,nrow(Eurostat_countries)))
  look_Eurostat_countries = cbind(year,Eurostat_countries) 
  
  Eurostat_countries_hh_fd = rbind(Eurostat_countries_hh_fd,look_Eurostat_countries)
  
  
  eurostat_countries_colsums = colSums(Eurostat_countries)
  
  geo = data.frame(c("AT","BE","BG","CY","CZ","DE","DK","EE","EL","ES","FI",
                     "FR","HR","HU","IE","IT","LT","LU","LV","MT","NL","NO",
                     "PL","PT","RO","SE","SI","SK","TR","UK")) %>% rename_at(1,~"geo")
  
  year = rep(year_current, 30)
  
  fds = cbind(geo,year,eurostat_countries_colsums) %>% slice(rep(1:n(), each = 5))
  
  quintiles = data.frame(rep(c("QUINTILE1","QUINTILE2","QUINTILE3","QUINTILE4","QUINTILE5"),30)) %>% rename_at(1,~"quintile")
  
  total_fd_year_current = cbind(fds,quintiles)
  
  total_fd = rbind(total_fd, total_fd_year_current)
  
  # labels
  
  Exiobase_T_labels = read.csv(paste0(data_dir_income_stratified_footprints, "/Exiobase_T_labels_ixi_w_coicop_mapping.csv")) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  # TIVs
  
  # CO2 - combustion - air
  
  Exiobase_TIV_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_combustion_air_", year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_combustion_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_combustion_air_", year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_combustion_europe,TIV_CO2_combustion_not_europe)
  
  # CO2 - noncombustion - cement - air
  
  Exiobase_TIV_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_cement_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_cement_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_cement_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_cement_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_cement_europe,TIV_CO2_noncombustion_cement_not_europe)
  
  # CO2 - noncombustion - lime - air
  
  Exiobase_TIV_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_noncombustion_lime_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_noncombustion_lime_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_noncombustion_lime_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_noncombustion_lime_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_noncombustion_lime_europe,TIV_CO2_noncombustion_lime_not_europe)
  
  # CO2 - agriculture - peat decay - air
  
  Exiobase_TIV_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_agriculture_peatdecay_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_agriculture_peatdecay_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_agriculture_peatdecay_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_agriculture_peatdecay_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_agriculture_peatdecay_europe,TIV_CO2_agriculture_peatdecay_not_europe)
  
  # CO2 - waste - biogenic - air
  
  Exiobase_TIV_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_biogenic_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_biogenic_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_biogenic_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_biogenic_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_biogenic_europe,TIV_CO2_waste_biogenic_not_europe)
  
  # CO2 - waste - fossil - air
  
  Exiobase_TIV_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CO2_waste_fossil_domestic)
  
  Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_co2_waste_fossil_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CO2_waste_fossil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CO2_waste_fossil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CO2_waste_fossil_europe,TIV_CO2_waste_fossil_not_europe)
  

  # CH4 - combustion -air
  
  Exiobase_TIV_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_combustion_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_combustion_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_combustion_europe,TIV_CH4_combustion_not_europe)
  
  # CH4 - noncombustion - gas - air
  
  Exiobase_TIV_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_gas_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_gas_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_gas_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_gas_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_gas_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_gas_europe,TIV_CH4_noncombustion_gas_not_europe)
  
  # CH4 - noncombustion - oil - air
  
  Exiobase_TIV_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oil_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oil_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oil_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oil_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oil_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oil_europe,TIV_CH4_noncombustion_oil_not_europe)
  
  # CH4 - noncombustion - anthracite - air
  
  Exiobase_TIV_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_anthracite_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_anthracite_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_anthracite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_anthracite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_anthracite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_anthracite_europe,TIV_CH4_noncombustion_anthracite_not_europe)
  
  # CH4 - noncombustion - bituminouscoal - air
  
  Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_bituminouscoal_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_bituminouscoal_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_bituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_bituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_bituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_bituminouscoal_europe,TIV_CH4_noncombustion_bituminouscoal_not_europe)
  
  # CH4 - noncombustion - cokingcoal - air
  
  Exiobase_TIV_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_cokingcoal_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_cokingcoal_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_cokingcoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_cokingcoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_cokingcoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_cokingcoal_europe,TIV_CH4_noncombustion_cokingcoal_not_europe)
  
  # CH4 - noncombustion - lignite - air
  
  Exiobase_TIV_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_lignite_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_lignite_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_lignite_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_lignite_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_lignite_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_lignite_europe,TIV_CH4_noncombustion_lignite_not_europe)
  
  # CH4 - noncombustion - subbituminouscoal - air
  
  Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_subbituminouscoal_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_subbituminouscoal_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_subbituminouscoal_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_subbituminouscoal_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_subbituminouscoal_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_subbituminouscoal_europe,TIV_CH4_noncombustion_subbituminouscoal_not_europe)
  
  # CH4 - noncombustion - oilrefinery - air
  
  Exiobase_TIV_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_noncombustion_oilrefinery_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_noncombustion_oilrefinery_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_noncombustion_oilrefinery_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_noncombustion_oilrefinery_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_noncombustion_oilrefinery_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_noncombustion_oilrefinery_europe,TIV_CH4_noncombustion_oilrefinery_not_europe)
  
  # CH4 - agriculture - air
  
  Exiobase_TIV_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_agriculture_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_agriculture_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_agriculture_europe,TIV_CH4_agriculture_not_europe)
  
  # CH4 - waste - air
  
  Exiobase_TIV_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_ch4_CO2eq_waste_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_CH4_waste_domestic)
  
  Exiobase_TIV_europe_breakdown_ch4_waste_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_ch4_CO2eq_waste_air_", year_current, "_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_CH4_waste_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_CH4_waste_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_CH4_waste_europe,TIV_CH4_waste_not_europe)
  
  
  # N2O - combustion - air
  
  Exiobase_TIV_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_combustion_domestic)
  
  Exiobase_TIV_europe_breakdown_n2o_combustion_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_combustion_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_combustion_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_combustion_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_combustion_europe,TIV_N2O_combustion_not_europe)
  
  # N2O - agriculture - air
  
  Exiobase_TIV_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_N2O_agriculture_domestic)
  
  Exiobase_TIV_europe_breakdown_n2o_agriculture_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_n2o_CO2eq_agriculture_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_N2O_agriculture_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_N2O_agriculture_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_N2O_agriculture_europe,TIV_N2O_agriculture_not_europe)
  
  # SF6 - air
  
  Exiobase_TIV_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_SF6_domestic)
  
  Exiobase_TIV_europe_breakdown_sf6_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_sf6_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_SF6_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_SF6_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_SF6_europe,TIV_SF6_not_europe)
  
  # HFC - air
  
  Exiobase_TIV_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_HFC_domestic)
  
  Exiobase_TIV_europe_breakdown_hfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_hfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_HFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_HFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_HFC_europe,TIV_HFC_not_europe)
  
  # PFC - air
  
  Exiobase_TIV_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_PFC_domestic)
  
  Exiobase_TIV_europe_breakdown_pfc_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_pfc_CO2eq_air_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_PFC_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_PFC_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_PFC_europe,TIV_PFC_not_europe)
  
  # Energy use
  
  Exiobase_TIV_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_energy_carrier_use_",year_current,"_ixi.csv"))[,-1]
  
  Exiobase_TIV_country_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_energy_carrier_use_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    gather(country, TIV_energy_domestic)
  
  Exiobase_TIV_europe_breakdown_energy_use_bp = read.csv(paste0(data_dir_exiobase, "/IOT_",year_current,"_ixi/TIV_country_breakdown_energy_carrier_use_",year_current,"_ixi.csv"))[,-1] %>%
    row_to_names(row_number = 1) %>%
    mutate_at(vars(AT:ZA), funs(as.numeric(as.character(.)))) %>%
    mutate(TIV_energy_europe = AT +
             BE + BG + CY + CZ +
             DE + DK + EE + 
             ES + FI + FR + 
             GB + GR + HR +
             HU + IE + IT +
             LT + LU + LV +
             MT + NL + PL +
             PT + TR + SK + 
             SI + SE + RO +
             NO,
           TIV_energy_not_europe = AU +
             BR + CA + CH + CN +
             ID + IN + JP + KR +
             MX + RU + TW + US + 
             WA + WE + WF + WL + WM +
             ZA) %>%
    select(TIV_energy_europe,TIV_energy_not_europe)
  
  # join with labels
  
  TIV_with_labels = cbind(Exiobase_T_labels,
                          t(Exiobase_TIV_co2_combustion_bp),
                          t(Exiobase_TIV_co2_noncombustion_cement_bp),
                          t(Exiobase_TIV_co2_noncombustion_lime_bp),
                          t(Exiobase_TIV_co2_agriculture_peatdecay_bp),
                          t(Exiobase_TIV_co2_waste_biogenic_bp),
                          t(Exiobase_TIV_co2_waste_fossil_bp),
                          t(Exiobase_TIV_ch4_combustion_bp),
                          t(Exiobase_TIV_ch4_noncombustion_gas_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oil_bp),
                          t(Exiobase_TIV_ch4_noncombustion_anthracite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_lignite_bp),
                          t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp),
                          t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp),
                          t(Exiobase_TIV_ch4_agriculture_bp),
                          t(Exiobase_TIV_ch4_waste_bp),
                          t(Exiobase_TIV_n2o_combustion_bp),
                          t(Exiobase_TIV_n2o_agriculture_bp),
                          t(Exiobase_TIV_sf6_bp),
                          t(Exiobase_TIV_hfc_bp),
                          t(Exiobase_TIV_pfc_bp),
                          t(Exiobase_TIV_energy_use_bp)) %>% 
    rename(TIV_CO2_combustion = "t(Exiobase_TIV_co2_combustion_bp)",
           TIV_CO2_noncombustion_cement = "t(Exiobase_TIV_co2_noncombustion_cement_bp)",
           TIV_CO2_noncombustion_lime = "t(Exiobase_TIV_co2_noncombustion_lime_bp)",
           TIV_CO2_agriculture_peatdecay = "t(Exiobase_TIV_co2_agriculture_peatdecay_bp)",
           TIV_CO2_waste_biogenic = "t(Exiobase_TIV_co2_waste_biogenic_bp)",
           TIV_CO2_waste_fossil = "t(Exiobase_TIV_co2_waste_fossil_bp)",
           TIV_CH4_combustion = "t(Exiobase_TIV_ch4_combustion_bp)",
           TIV_CH4_noncombustion_gas = "t(Exiobase_TIV_ch4_noncombustion_gas_bp)",
           TIV_CH4_noncombustion_oil = "t(Exiobase_TIV_ch4_noncombustion_oil_bp)",
           TIV_CH4_noncombustion_anthracite = "t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)",
           TIV_CH4_noncombustion_bituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)",
           TIV_CH4_noncombustion_cokingcoal = "t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)",
           TIV_CH4_noncombustion_lignite = "t(Exiobase_TIV_ch4_noncombustion_lignite_bp)",
           TIV_CH4_noncombustion_subbituminouscoal = "t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)",
           TIV_CH4_noncombustion_oilrefinery = "t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)",
           TIV_CH4_agriculture = "t(Exiobase_TIV_ch4_agriculture_bp)",
           TIV_CH4_waste = "t(Exiobase_TIV_ch4_waste_bp)",
           TIV_N2O_combustion = "t(Exiobase_TIV_n2o_combustion_bp)",
           TIV_N2O_agriculture = "t(Exiobase_TIV_n2o_agriculture_bp)",
           TIV_SF6 = "t(Exiobase_TIV_sf6_bp)",
           TIV_HFC = "t(Exiobase_TIV_hfc_bp)",
           TIV_PFC = "t(Exiobase_TIV_pfc_bp)",
           TIV_energy = "t(Exiobase_TIV_energy_use_bp)") %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK"))  
  
  year = as.character(rep(year_current,nrow(TIV_with_labels)))
  
  look = cbind(year,TIV_with_labels) %>%
    rename(country_of_production = V1, sector = V2)
  
  TIVs = rbind(TIVs,look)
  
  # domestic TIVs
  
  domestic_TIV_with_labels = cbind(Exiobase_T_labels,
                                   Exiobase_TIV_country_breakdown_co2_combustion_bp,
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_cement_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_noncombustion_lime_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_agriculture_peatdecay_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_biogenic_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_co2_waste_fossil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_gas_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oil_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_anthracite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_bituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_cokingcoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_lignite_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_subbituminouscoal_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_noncombustion_oilrefinery_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_ch4_waste_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_combustion_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_n2o_agriculture_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_sf6_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_hfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_pfc_bp %>% select(-country),
                                   Exiobase_TIV_country_breakdown_energy_use_bp %>% select(-country))  %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK"),
           country = dplyr::recode(country, "GR" = "EL", "GB" = "UK"))  
  
  year_domestic = as.character(rep(year_current,nrow(domestic_TIV_with_labels)))
  
  look_domestic = cbind(year_domestic,domestic_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, geo = country, year = year_domestic) %>%
    mutate(TIV_CO2_combustion_domestic = as.numeric(TIV_CO2_combustion_domestic),
           TIV_CO2_noncombustion_cement_domestic = as.numeric(TIV_CO2_noncombustion_cement_domestic),
           TIV_CO2_noncombustion_lime_domestic = as.numeric(TIV_CO2_noncombustion_lime_domestic),
           TIV_CO2_agriculture_peatdecay_domestic = as.numeric(TIV_CO2_agriculture_peatdecay_domestic),
           TIV_CO2_waste_biogenic_domestic = as.numeric(TIV_CO2_waste_biogenic_domestic),
           TIV_CO2_waste_fossil_domestic = as.numeric(TIV_CO2_waste_fossil_domestic),
           TIV_CH4_combustion_domestic = as.numeric(TIV_CH4_combustion_domestic),
           TIV_CH4_noncombustion_gas_domestic = as.numeric(TIV_CH4_noncombustion_gas_domestic),
           TIV_CH4_noncombustion_oil_domestic = as.numeric(TIV_CH4_noncombustion_oil_domestic),
           TIV_CH4_noncombustion_anthracite_domestic = as.numeric(TIV_CH4_noncombustion_anthracite_domestic),
           TIV_CH4_noncombustion_bituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_bituminouscoal_domestic),
           TIV_CH4_noncombustion_cokingcoal_domestic = as.numeric(TIV_CH4_noncombustion_cokingcoal_domestic),
           TIV_CH4_noncombustion_lignite_domestic = as.numeric(TIV_CH4_noncombustion_lignite_domestic),
           TIV_CH4_noncombustion_subbituminouscoal_domestic = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_domestic),
           TIV_CH4_noncombustion_oilrefinery_domestic = as.numeric(TIV_CH4_noncombustion_oilrefinery_domestic),
           TIV_CH4_agriculture_domestic = as.numeric(TIV_CH4_agriculture_domestic),
           TIV_CH4_waste_domestic = as.numeric(TIV_CH4_waste_domestic),
           TIV_N2O_combustion_domestic = as.numeric(TIV_N2O_combustion_domestic),
           TIV_N2O_agriculture_domestic = as.numeric(TIV_N2O_agriculture_domestic),
           TIV_SF6_domestic = as.numeric(TIV_SF6_domestic),
           TIV_HFC_domestic = as.numeric(TIV_HFC_domestic),
           TIV_PFC_domestic = as.numeric(TIV_PFC_domestic),
           TIV_energy_domestic = as.numeric(TIV_energy_domestic))
  
  domestic_TIVs = rbind(domestic_TIVs, look_domestic)
  
  # europe TIVs with labels 
  
  europe_TIV_with_labels = cbind(Exiobase_T_labels,
                                 Exiobase_TIV_europe_breakdown_co2_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_cement_bp,
                                 Exiobase_TIV_europe_breakdown_co2_noncombustion_lime_bp,
                                 Exiobase_TIV_europe_breakdown_co2_agriculture_peatdecay_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_biogenic_bp,
                                 Exiobase_TIV_europe_breakdown_co2_waste_fossil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_gas_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oil_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_anthracite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_bituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_cokingcoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_lignite_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_subbituminouscoal_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_noncombustion_oilrefinery_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_ch4_waste_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_combustion_bp,
                                 Exiobase_TIV_europe_breakdown_n2o_agriculture_bp,
                                 Exiobase_TIV_europe_breakdown_sf6_bp,
                                 Exiobase_TIV_europe_breakdown_hfc_bp,
                                 Exiobase_TIV_europe_breakdown_pfc_bp,
                                 Exiobase_TIV_europe_breakdown_energy_use_bp) %>% 
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  year_europe = as.character(rep(year_current,nrow(europe_TIV_with_labels)))
  
  look_europe = cbind(year_europe,europe_TIV_with_labels) %>% 
    rename(country_of_production = V1, sector = V2, year = year_europe) %>%
    mutate(TIV_CO2_combustion_europe = as.numeric(TIV_CO2_combustion_europe),
           TIV_CO2_noncombustion_cement_europe = as.numeric(TIV_CO2_noncombustion_cement_europe),
           TIV_CO2_noncombustion_lime_europe = as.numeric(TIV_CO2_noncombustion_lime_europe),
           TIV_CO2_agriculture_peatdecay_europe = as.numeric(TIV_CO2_agriculture_peatdecay_europe),
           TIV_CO2_waste_biogenic_europe = as.numeric(TIV_CO2_waste_biogenic_europe),
           TIV_CO2_waste_fossil_europe = as.numeric(TIV_CO2_waste_fossil_europe),
           TIV_CH4_combustion_europe = as.numeric(TIV_CH4_combustion_europe),
           TIV_CH4_noncombustion_gas_europe = as.numeric(TIV_CH4_noncombustion_gas_europe),
           TIV_CH4_noncombustion_oil_europe = as.numeric(TIV_CH4_noncombustion_oil_europe),
           TIV_CH4_noncombustion_anthracite_europe = as.numeric(TIV_CH4_noncombustion_anthracite_europe),
           TIV_CH4_noncombustion_bituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_bituminouscoal_europe),
           TIV_CH4_noncombustion_cokingcoal_europe = as.numeric(TIV_CH4_noncombustion_cokingcoal_europe),
           TIV_CH4_noncombustion_lignite_europe = as.numeric(TIV_CH4_noncombustion_lignite_europe),
           TIV_CH4_noncombustion_subbituminouscoal_europe = as.numeric(TIV_CH4_noncombustion_subbituminouscoal_europe),
           TIV_CH4_noncombustion_oilrefinery_europe = as.numeric(TIV_CH4_noncombustion_oilrefinery_europe),
           TIV_CH4_agriculture_europe = as.numeric(TIV_CH4_agriculture_europe),
           TIV_CH4_waste_europe = as.numeric(TIV_CH4_waste_europe),
           TIV_N2O_combustion_europe = as.numeric(TIV_N2O_combustion_europe),
           TIV_N2O_agriculture_europe = as.numeric(TIV_N2O_agriculture_europe),
           TIV_SF6_europe = as.numeric(TIV_SF6_europe),
           TIV_HFC_europe = as.numeric(TIV_HFC_europe),
           TIV_PFC_europe = as.numeric(TIV_PFC_europe),
           TIV_energy_europe = as.numeric(TIV_energy_europe))
  
  europe_TIVs = rbind(europe_TIVs, look_europe)
  
  # total national footprints
  
  # FD labels
  
  Exiobase_FD_labels = as.data.frame(t(read.csv(paste0(data_dir_exiobase, "/Exiobase_FD_labels_ixi.csv")))[-1,-3]) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  national_CO2_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_co2_combustion_bp)
  
  national_CO2_noncombustion_cement_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_cement_bp)
  
  national_CO2_noncombustion_lime_footprints = Exiobase_FD * t(Exiobase_TIV_co2_noncombustion_lime_bp)
  
  national_CO2_agriculture_peatdecay_footprints = Exiobase_FD * t(Exiobase_TIV_co2_agriculture_peatdecay_bp)
  
  national_CO2_waste_biogenic_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_biogenic_bp)
  
  national_CO2_waste_fossil_footprints = Exiobase_FD * t(Exiobase_TIV_co2_waste_fossil_bp)
  
  national_CH4_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_combustion_bp)
  
  national_CH4_noncombustion_gas_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_gas_bp)
  
  national_CH4_noncombustion_oil_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oil_bp)
  
  national_CH4_noncombustion_anthracite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_anthracite_bp)
  
  national_CH4_noncombustion_bituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_bituminouscoal_bp)
  
  national_CH4_noncombustion_cokingcoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_cokingcoal_bp)
  
  national_CH4_noncombustion_lignite_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_lignite_bp)
  
  national_CH4_noncombustion_subbituminouscoal_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_subbituminouscoal_bp)
  
  national_CH4_noncombustion_oilrefinery_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_noncombustion_oilrefinery_bp)
  
  national_CH4_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_agriculture_bp)
  
  national_CH4_waste_footprints = Exiobase_FD * t(Exiobase_TIV_ch4_waste_bp)
  
  national_N2O_combustion_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_combustion_bp)
  
  national_N2O_agriculture_footprints = Exiobase_FD * t(Exiobase_TIV_n2o_agriculture_bp)
  
  national_SF6_footprints = Exiobase_FD * t(Exiobase_TIV_sf6_bp)
  
  national_HFC_footprints = Exiobase_FD * t(Exiobase_TIV_hfc_bp)
  
  national_PFC_footprints = Exiobase_FD * t(Exiobase_TIV_pfc_bp)
  
  national_energy_footprints = Exiobase_FD * t(Exiobase_TIV_energy_use_bp)
  
  
  # together
  
  national_footprints_w_labels = cbind(Exiobase_FD_labels, 
                                       rowSums(t(national_CO2_combustion_footprints)),
                                       rowSums(t(national_CO2_noncombustion_cement_footprints)), 
                                       rowSums(t(national_CO2_noncombustion_lime_footprints)), 
                                       rowSums(t(national_CO2_agriculture_peatdecay_footprints)), 
                                       rowSums(t(national_CO2_waste_biogenic_footprints)), 
                                       rowSums(t(national_CO2_waste_fossil_footprints)),
                                       rowSums(t(national_CH4_combustion_footprints)),
                                       rowSums(t(national_CH4_noncombustion_gas_footprints)),
                                       rowSums(t(national_CH4_noncombustion_oil_footprints)),
                                       rowSums(t(national_CH4_noncombustion_anthracite_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_cokingcoal_footprints)),
                                       rowSums(t(national_CH4_noncombustion_lignite_footprints)),
                                       rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints)), 
                                       rowSums(t(national_CH4_noncombustion_oilrefinery_footprints)), 
                                       rowSums(t(national_CH4_agriculture_footprints)),
                                       rowSums(t(national_CH4_waste_footprints)),
                                       rowSums(t(national_N2O_combustion_footprints)),
                                       rowSums(t(national_N2O_agriculture_footprints)),
                                       rowSums(t(national_SF6_footprints)),
                                       rowSums(t(national_HFC_footprints)),
                                       rowSums(t(national_PFC_footprints)),
                                       rowSums(t(national_energy_footprints))) %>%
    mutate(V1 = dplyr::recode(V1,"GR" = "EL","GB" = "UK")) 
  
  year_national_fp = as.character(rep(year_current,nrow(national_footprints_w_labels)))
  
  # direct FD emissions
  
  direct_FD_extensions = read.csv(paste0(data_dir_exiobase, "/IOT_", year_current, "_ixi/satellite/F_hh.csv", sep = ""),row.names=NULL,as.is=TRUE)[3:1115,3:345]
  direct_FD_extensions[is.na(direct_FD_extensions)]=0
  direct_FD_extensions = mapply(direct_FD_extensions, FUN = as.numeric)
  direct_FD_extensions = matrix(data=direct_FD_extensions,ncol=343,nrow=1113)
  
  direct_FD_co2_combustion = direct_FD_extensions[24,]
  direct_FD_co2_noncombustion_cement = direct_FD_extensions[93,]
  direct_FD_co2_noncombustion_lime = direct_FD_extensions[94,]
  direct_FD_co2_agriculture_peatdecay = direct_FD_extensions[428,]
  direct_FD_co2_waste_biogenic = direct_FD_extensions[438,]
  direct_FD_co2_waste_fossil = direct_FD_extensions[439,]
  direct_FD_ch4_combustion = direct_FD_extensions[25,]*28
  direct_FD_ch4_noncombustion_gas = direct_FD_extensions[68,]*28
  direct_FD_ch4_noncombustion_oil = direct_FD_extensions[69,]*28
  direct_FD_ch4_noncombustion_anthracite = direct_FD_extensions[70,]*28
  direct_FD_ch4_noncombustion_bituminouscoal = direct_FD_extensions[71,]*28
  direct_FD_ch4_noncombustion_cokingcoal = direct_FD_extensions[72,]*28
  direct_FD_ch4_noncombustion_lignite = direct_FD_extensions[73,]*28
  direct_FD_ch4_noncombustion_subbituminouscoal = direct_FD_extensions[74,]*28
  direct_FD_ch4_noncombustion_oilrefinery = direct_FD_extensions[75,]*28
  direct_FD_ch4_agriculture = direct_FD_extensions[427,]*28
  direct_FD_ch4_waste = direct_FD_extensions[436,]*28
  direct_FD_n2o_combustion = direct_FD_extensions[26,]*265
  direct_FD_n2o_agriculture = direct_FD_extensions[430,]*265
  direct_FD_sf6 = direct_FD_extensions[424,]*23500
  direct_FD_hfc = direct_FD_extensions[425,]
  direct_FD_pfc = direct_FD_extensions[426,]
  direct_FD_energy = direct_FD_extensions[470,]
  
  
  direct_FD_fp = data.frame(direct_FD_co2_combustion,
                            direct_FD_co2_noncombustion_cement,
                            direct_FD_co2_noncombustion_lime,
                            direct_FD_co2_agriculture_peatdecay,
                            direct_FD_co2_waste_biogenic,
                            direct_FD_co2_waste_fossil,
                            direct_FD_ch4_combustion,
                            direct_FD_ch4_noncombustion_gas,
                            direct_FD_ch4_noncombustion_oil,
                            direct_FD_ch4_noncombustion_anthracite,
                            direct_FD_ch4_noncombustion_bituminouscoal,
                            direct_FD_ch4_noncombustion_cokingcoal,
                            direct_FD_ch4_noncombustion_lignite,
                            direct_FD_ch4_noncombustion_subbituminouscoal,
                            direct_FD_ch4_noncombustion_oilrefinery,
                            direct_FD_ch4_agriculture,
                            direct_FD_ch4_waste,
                            direct_FD_n2o-combustion,
                            direct_FD_n2o_agriculture,
                            direct_FD_sf6,
                            direct_FD_hfc,
                            direct_FD_pfc,
                            direct_FD_energy)
  
  look_national_fp = as.data.frame(cbind(year_national_fp,
                                         national_footprints_w_labels,
                                         direct_FD_fp)) %>% 
    rename(year = year_national_fp,
           geo = V1, 
           fd_category = V2, 
           co2_combustion = "rowSums(t(national_CO2_combustion_footprints))",
           co2_noncombustion_cement = "rowSums(t(national_CO2_noncombustion_cement_footprints))",
           co2_noncombustion_lime = "rowSums(t(national_CO2_noncombustion_lime_footprints))",
           co2_agriculture_peatdecay = "rowSums(t(national_CO2_agriculture_peatdecay_footprints))",
           co2_waste_biogenic = "rowSums(t(national_CO2_waste_biogenic_footprints))",
           co2_waste_fossil = "rowSums(t(national_CO2_waste_fossil_footprints))",
           ch4_combustion = "rowSums(t(national_CH4_combustion_footprints))",
           ch4_noncombustion_gas = "rowSums(t(national_CH4_noncombustion_gas_footprints))",
           ch4_noncombustion_oil = "rowSums(t(national_CH4_noncombustion_oil_footprints))",
           ch4_noncombustion_anthracite = "rowSums(t(national_CH4_noncombustion_anthracite_footprints))",
           ch4_noncombustion_bituminouscoal = "rowSums(t(national_CH4_noncombustion_bituminouscoal_footprints))",
           ch4_noncombustion_cokingcoal = "rowSums(t(national_CH4_noncombustion_cokingcoal_footprints))",
           ch4_noncombustion_lignite = "rowSums(t(national_CH4_noncombustion_lignite_footprints))",
           ch4_noncombustion_subbituminouscoal = "rowSums(t(national_CH4_noncombustion_subbituminouscoal_footprints))",
           ch4_noncombustion_oilrefinery = "rowSums(t(national_CH4_noncombustion_oilrefinery_footprints))",
           ch4_agriculture = "rowSums(t(national_CH4_agriculture_footprints))",
           ch4_waste = "rowSums(t(national_CH4_waste_footprints))",
           n2o_combustion = "rowSums(t(national_N2O_combustion_footprints))",
           n2o_agriculture = "rowSums(t(national_N2O_agriculture_footprints))",
           sf6 = "rowSums(t(national_SF6_footprints))",
           hfc = "rowSums(t(national_HFC_footprints))",
           pfc = "rowSums(t(national_PFC_footprints))",
           energy = "rowSums(t(national_energy_footprints))") %>%
    select(year,
           geo,
           fd_category,
           co2_combustion,
           direct_FD_co2_combustion,
           co2_noncombustion_cement,
           direct_FD_co2_noncombustion_cement,
           co2_noncombustion_lime,
           direct_FD_co2_noncombustion_lime,
           co2_agriculture_peatdecay,
           direct_FD_co2_agriculture_peatdecay,
           co2_waste_biogenic,
           direct_FD_co2_waste_biogenic,
           co2_waste_fossil,
           direct_FD_co2_waste_fossil,
           ch4_combustion,
           direct_FD_ch4_combustion,
           ch4_noncombustion_gas,
           direct_FD_ch4_noncombustion_gas,
           ch4_noncombustion_oil,
           direct_FD_ch4_noncombustion_oil,
           ch4_noncombustion_anthracite,
           direct_FD_ch4_noncombustion_anthracite,
           ch4_noncombustion_bituminouscoal,
           direct_FD_ch4_noncombustion_bituminouscoal,
           ch4_noncombustion_cokingcoal,
           direct_FD_ch4_noncombustion_cokingcoal,
           ch4_noncombustion_lignite,
           direct_FD_ch4_noncombustion_lignite,
           ch4_noncombustion_subbituminouscoal,
           direct_FD_ch4_noncombustion_subbituminouscoal,
           ch4_noncombustion_oilrefinery,
           direct_FD_ch4_noncombustion_oilrefinery,
           ch4_agriculture,
           direct_FD_ch4_agriculture,
           ch4_waste,
           direct_FD_ch4_waste,
           n2o_combustion,
           direct_FD_n2o_combustion,
           n2o_agriculture,
           direct_FD_n2o_agriculture,
           sf6,
           direct_FD_sf6,
           hfc,
           direct_FD_hfc,
           pfc,
           direct_FD_pfc,
           energy,
           direct_FD_energy)
  
  
  national_fp = rbind(national_fp, look_national_fp)
  
  
}



# option holding HBS exp ratios 

mean_expenditure_by_quintile_toggle = mean_expenditure_by_quintile_long_bp %>% 
  filter(!(quintile %in% c("TOTAL","UNK"))) %>%
  group_by(geo,year) %>%
  mutate(euro_pps_bp = as.numeric(euro_pps_bp),
         mean_exp_shares = euro_pps_bp/sum(euro_pps_bp))


ala = total_fd %>%
  left_join(mean_expenditure_by_quintile_toggle, by = c("geo","year","quintile"))



join_ala = mean_expenditure_by_coicop_sector_long_bp %>%
  left_join(ala, by = c("geo","quintile","year")) %>%
  mutate(year = as.numeric(year),
         eurostat_countries_colsums = as.numeric(eurostat_countries_colsums),
         pm_bp = as.numeric(pm_bp),
         fd_me = pm_bp*((eurostat_countries_colsums*mean_exp_shares)/1000)) 

Eurostat_countries_hh_fd_mean_TIV = as.data.frame(Eurostat_countries_hh_fd) %>% select(-year)

weighted_mean_TIV_with_labels = cbind(TIVs,Eurostat_countries_hh_fd_mean_TIV) %>%
  gather(geo,fd,-country_of_production,-year,-sector,-coicop,-five_sectors,
         -TIV_CO2_combustion,-TIV_CO2_noncombustion_cement,-TIV_CO2_noncombustion_lime,
         -TIV_CO2_agriculture_peatdecay,-TIV_CO2_waste_biogenic,
         -TIV_CO2_waste_fossil,-TIV_CH4_combustion,
         -TIV_CH4_noncombustion_gas,-TIV_CH4_noncombustion_oil,
         -TIV_CH4_noncombustion_anthracite,-TIV_CH4_noncombustion_bituminouscoal,
         -TIV_CH4_noncombustion_cokingcoal,-TIV_CH4_noncombustion_lignite,
         -TIV_CH4_noncombustion_subbituminouscoal,-TIV_CH4_noncombustion_oilrefinery,
         -TIV_CH4_agriculture,-TIV_CH4_waste,
         -TIV_N2O_combustion,-TIV_N2O_agriculture,-TIV_SF6,-TIV_HFC,-TIV_PFC,
         -TIV_energy) %>%
  group_by(geo,year,coicop) %>%
  mutate(fd = as.numeric(fd)) %>%
  mutate(TIV_CO2_combustion_weighted_average = sum((fd/sum(fd))*TIV_CO2_combustion),
         TIV_CO2_noncombustion_cement_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_cement),
         TIV_CO2_noncombustion_lime_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_lime),
         TIV_CO2_agriculture_peatdecay_weighted_average = sum((fd/sum(fd))*TIV_CO2_agriculture_peatdecay),
         TIV_CO2_waste_biogenic_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_biogenic),
         TIV_CO2_waste_fossil_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_fossil),
         TIV_CH4_combustion_weighted_average = sum((fd/sum(fd))*TIV_CH4_combustion),
         TIV_CH4_noncombustion_gas_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_gas),
         TIV_CH4_noncombustion_oil_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oil),
         TIV_CH4_noncombustion_anthracite_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_anthracite),
         TIV_CH4_noncombustion_bituminouscoal_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_bituminouscoal),
         TIV_CH4_noncombustion_cokingcoal_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_cokingcoal),
         TIV_CH4_noncombustion_lignite_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_lignite),
         TIV_CH4_noncombustion_subbituminouscoal_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_subbituminouscoal),
         TIV_CH4_noncombustion_oilrefinery_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oilrefinery),
         TIV_CH4_agriculture_weighted_average = sum((fd/sum(fd))*TIV_CH4_agriculture),
         TIV_CH4_waste_weighted_average = sum((fd/sum(fd))*TIV_CH4_waste),
         TIV_N2O_combustion_weighted_average = sum((fd/sum(fd))*TIV_N2O_combustion),
         TIV_N2O_agriculture_weighted_average = sum((fd/sum(fd))*TIV_N2O_agriculture),
         TIV_SF6_weighted_average = sum((fd/sum(fd))*TIV_SF6),
         TIV_HFC_weighted_average = sum((fd/sum(fd))*TIV_HFC),
         TIV_PFC_weighted_average = sum((fd/sum(fd))*TIV_PFC),
         TIV_energy_weighted_average = sum((fd/sum(fd))*TIV_energy)) %>%
  select(year,geo,coicop,TIV_CO2_combustion_weighted_average,
         TIV_CO2_noncombustion_cement_weighted_average,
         TIV_CO2_noncombustion_lime_weighted_average,
         TIV_CO2_agriculture_peatdecay_weighted_average,
         TIV_CO2_waste_biogenic_weighted_average,
         TIV_CO2_waste_fossil_weighted_average,
         TIV_CH4_combustion_weighted_average,
         TIV_CH4_noncombustion_gas_weighted_average,
         TIV_CH4_noncombustion_oil_weighted_average,
         TIV_CH4_noncombustion_anthracite_weighted_average,
         TIV_CH4_noncombustion_bituminouscoal_weighted_average,
         TIV_CH4_noncombustion_cokingcoal_weighted_average,
         TIV_CH4_noncombustion_lignite_weighted_average,
         TIV_CH4_noncombustion_subbituminouscoal_weighted_average,
         TIV_CH4_noncombustion_oilrefinery_weighted_average,
         TIV_CH4_agriculture_weighted_average,
         TIV_CH4_waste_weighted_average,
         TIV_N2O_combustion_weighted_average,
         TIV_N2O_agriculture_weighted_average,
         TIV_SF6_weighted_average,
         TIV_HFC_weighted_average,
         TIV_PFC_weighted_average,
         TIV_energy_weighted_average) %>%
  unique() 

weighted_mean_europe_TIV_with_labels = cbind(europe_TIVs, Eurostat_countries_hh_fd_mean_TIV) %>%
  gather(geo,fd,-country_of_production,-year,-sector,-coicop,-five_sectors,
         -TIV_CO2_combustion_europe,-TIV_CO2_combustion_not_europe,
         -TIV_CO2_noncombustion_cement_europe,-TIV_CO2_noncombustion_cement_not_europe,
         -TIV_CO2_noncombustion_lime_europe, -TIV_CO2_noncombustion_lime_not_europe,
         -TIV_CO2_agriculture_peatdecay_europe,-TIV_CO2_agriculture_peatdecay_not_europe,
         -TIV_CO2_waste_biogenic_europe, -TIV_CO2_waste_biogenic_not_europe,
         -TIV_CO2_waste_fossil_europe, -TIV_CO2_waste_fossil_not_europe,
         -TIV_CH4_combustion_europe,-TIV_CH4_combustion_not_europe,
         -TIV_CH4_noncombustion_gas_europe, -TIV_CH4_noncombustion_gas_not_europe,
         -TIV_CH4_noncombustion_oil_europe,-TIV_CH4_noncombustion_oil_not_europe,
         -TIV_CH4_noncombustion_anthracite_europe,-TIV_CH4_noncombustion_anthracite_not_europe,
         -TIV_CH4_noncombustion_bituminouscoal_europe,-TIV_CH4_noncombustion_bituminouscoal_not_europe,
         -TIV_CH4_noncombustion_cokingcoal_europe,-TIV_CH4_noncombustion_cokingcoal_not_europe,
         -TIV_CH4_noncombustion_lignite_europe,-TIV_CH4_noncombustion_lignite_not_europe,
         -TIV_CH4_noncombustion_subbituminouscoal_europe,-TIV_CH4_noncombustion_subbituminouscoal_not_europe,
         -TIV_CH4_noncombustion_oilrefinery_europe, -TIV_CH4_noncombustion_oilrefinery_not_europe,
         -TIV_CH4_agriculture_europe, -TIV_CH4_agriculture_not_europe,
         -TIV_CH4_waste_europe,-TIV_CH4_waste_not_europe,
         -TIV_N2O_combustion_europe,-TIV_N2O_combustion_not_europe,
         -TIV_N2O_agriculture_europe,-TIV_N2O_agriculture_not_europe,
         -TIV_SF6_europe,-TIV_SF6_not_europe,
         -TIV_HFC_europe,-TIV_HFC_not_europe,-TIV_PFC_europe,-TIV_PFC_not_europe,
         -TIV_energy_europe,-TIV_energy_not_europe) %>%
  group_by(geo,year,coicop) %>%
  mutate(fd = as.numeric(fd)) %>%
  mutate(TIV_CO2_combustion_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_combustion_europe),
         TIV_CO2_noncombustion_cement_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_cement_europe),
         TIV_CO2_noncombustion_lime_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_lime_europe),
         TIV_CO2_agriculture_peatdecay_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_agriculture_peatdecay_europe),
         TIV_CO2_waste_biogenic_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_biogenic_europe),
         TIV_CO2_waste_fossil_europe_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_fossil_europe),
         TIV_CH4_combustion_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_combustion_europe),
         TIV_CH4_noncombustion_gas_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_gas_europe),
         TIV_CH4_noncombustion_oil_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oil_europe),
         TIV_CH4_noncombustion_anthracite_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_anthracite_europe),
         TIV_CH4_noncombustion_bituminouscoal_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_bituminouscoal_europe),
         TIV_CH4_noncombustion_cokingcoal_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_cokingcoal_europe),
         TIV_CH4_noncombustion_lignite_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_lignite_europe),
         TIV_CH4_noncombustion_subbituminouscoal_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_subbituminouscoal_europe),
         TIV_CH4_noncombustion_oilrefinery_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oilrefinery_europe),
         TIV_CH4_agriculture_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_agriculture_europe),
         TIV_CH4_waste_europe_weighted_average = sum((fd/sum(fd))*TIV_CH4_waste_europe),
         TIV_N2O_combustion_europe_weighted_average = sum((fd/sum(fd))*TIV_N2O_combustion_europe),
         TIV_N2O_agriculture_europe_weighted_average = sum((fd/sum(fd))*TIV_N2O_agriculture_europe),
         TIV_SF6_europe_weighted_average = sum((fd/sum(fd))*TIV_SF6_europe),
         TIV_HFC_europe_weighted_average = sum((fd/sum(fd))*TIV_HFC_europe),
         TIV_PFC_europe_weighted_average = sum((fd/sum(fd))*TIV_PFC_europe),
         TIV_energy_europe_weighted_average = sum((fd/sum(fd))*TIV_energy_europe)) %>%
  select(year,geo,coicop,TIV_CO2_combustion_europe_weighted_average,
         TIV_CO2_noncombustion_cement_europe_weighted_average,
         TIV_CO2_noncombustion_lime_europe_weighted_average,
         TIV_CO2_agriculture_peatdecay_europe_weighted_average,
         TIV_CO2_waste_biogenic_europe_weighted_average,
         TIV_CO2_waste_fossil_europe_weighted_average,
         TIV_CH4_combustion_europe_weighted_average,
         TIV_CH4_noncombustion_gas_europe_weighted_average,
         TIV_CH4_noncombustion_oil_europe_weighted_average,
         TIV_CH4_noncombustion_anthracite_europe_weighted_average,
         TIV_CH4_noncombustion_bituminouscoal_europe_weighted_average,
         TIV_CH4_noncombustion_cokingcoal_europe_weighted_average,
         TIV_CH4_noncombustion_lignite_europe_weighted_average,
         TIV_CH4_noncombustion_subbituminouscoal_europe_weighted_average,
         TIV_CH4_noncombustion_oilrefinery_europe_weighted_average,
         TIV_CH4_agriculture_europe_weighted_average,
         TIV_CH4_waste_europe_weighted_average,
         TIV_N2O_combustion_europe_weighted_average,
         TIV_N2O_agriculture_europe_weighted_average,
         TIV_SF6_europe_weighted_average,
         TIV_HFC_europe_weighted_average,
         TIV_PFC_europe_weighted_average,
         TIV_energy_europe_weighted_average) %>%
  unique()

domestic_TIVs_Eurostat = domestic_TIVs %>%
  filter(geo %in% c("AT",
                    "BG",
                    "BE",
                    "CY",
                    "CZ",
                    "DE",
                    "DK",
                    "EE",
                    "EL",
                    "ES",
                    "FI",
                    "FR",
                    "HR",
                    "HU",
                    "IE",
                    "IT",
                    "LT",
                    "LU",
                    "LV",
                    "MT",
                    "NL",
                    "NO",
                    "PL",
                    "PT",
                    "RO",
                    "SE",
                    "SI",
                    "SK",
                    "TR",
                    "UK"))

Eurostat_countries_hh_fd_long = as.data.frame(Eurostat_countries_hh_fd) %>%
  gather(geo,fd,-year) %>%
  arrange(year, match(geo, c("AT",
                             "BE",
                             "BG",
                             "CY",
                             "CZ",
                             "DE",
                             "DK",
                             "EE",
                             "ES",
                             "FI",
                             "FR",
                             "UK",
                             "EL",
                             "HR",
                             "HU",
                             "IE",
                             "IT",
                             "LT",
                             "LU",
                             "LV",
                             "MT",
                             "NL",
                             "NO",
                             "PL",
                             "PT",
                             "RO",
                             "SE",
                             "SI",
                             "SK",
                             "TR"))) %>% select(-year,-geo)

weighted_mean_domestic_TIV_with_labels = cbind(domestic_TIVs_Eurostat,Eurostat_countries_hh_fd_long) %>%
  group_by(geo,year,coicop) %>%
  mutate(fd = as.numeric(fd)) %>%
  mutate(TIV_CO2_combustion_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_combustion_domestic),
         TIV_CO2_noncombustion_cement_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_cement_domestic),
         TIV_CO2_noncombustion_lime_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_noncombustion_lime_domestic),
         TIV_CO2_agriculture_peatdecay_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_agriculture_peatdecay_domestic),
         TIV_CO2_waste_biogenic_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_biogenic_domestic),
         TIV_CO2_waste_fossil_domestic_weighted_average = sum((fd/sum(fd))*TIV_CO2_waste_fossil_domestic),
         TIV_CH4_combustion_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_combustion_domestic),
         TIV_CH4_noncombustion_gas_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_gas_domestic),
         TIV_CH4_noncombustion_oil_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oil_domestic),
         TIV_CH4_noncombustion_anthracite_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_anthracite_domestic),
         TIV_CH4_noncombustion_bituminouscoal_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_bituminouscoal_domestic),
         TIV_CH4_noncombustion_cokingcoal_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_cokingcoal_domestic),
         TIV_CH4_noncombustion_lignite_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_lignite_domestic),
         TIV_CH4_noncombustion_subbituminouscoal_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_subbituminouscoal_domestic),
         TIV_CH4_noncombustion_oilrefinery_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_noncombustion_oilrefinery_domestic),
         TIV_CH4_agriculture_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_agriculture_domestic),
         TIV_CH4_waste_domestic_weighted_average = sum((fd/sum(fd))*TIV_CH4_waste_domestic),
         TIV_N2O_combustion_domestic_weighted_average = sum((fd/sum(fd))*TIV_N2O_combustion_domestic),
         TIV_N2O_agriculture_domestic_weighted_average = sum((fd/sum(fd))*TIV_N2O_agriculture_domestic),
         TIV_SF6_domestic_weighted_average = sum((fd/sum(fd))*TIV_SF6_domestic),
         TIV_HFC_domestic_weighted_average = sum((fd/sum(fd))*TIV_HFC_domestic),
         TIV_PFC_domestic_weighted_average = sum((fd/sum(fd))*TIV_PFC_domestic),
         TIV_energy_domestic_weighted_average = sum((fd/sum(fd))*TIV_energy_domestic)) %>%
  select(year,geo,coicop,TIV_CO2_combustion_domestic_weighted_average,
         TIV_CO2_noncombustion_cement_domestic_weighted_average,
         TIV_CO2_noncombustion_lime_domestic_weighted_average,
         TIV_CO2_agriculture_peatdecay_domestic_weighted_average,
         TIV_CO2_waste_biogenic_domestic_weighted_average,
         TIV_CO2_waste_fossil_domestic_weighted_average,
         TIV_CH4_combustion_domestic_weighted_average,
         TIV_CH4_noncombustion_gas_domestic_weighted_average,
         TIV_CH4_noncombustion_oil_domestic_weighted_average,
         TIV_CH4_noncombustion_anthracite_domestic_weighted_average,
         TIV_CH4_noncombustion_bituminouscoal_domestic_weighted_average,
         TIV_CH4_noncombustion_cokingcoal_domestic_weighted_average,
         TIV_CH4_noncombustion_lignite_domestic_weighted_average,
         TIV_CH4_noncombustion_subbituminouscoal_domestic_weighted_average,
         TIV_CH4_noncombustion_oilrefinery_domestic_weighted_average,
         TIV_CH4_agriculture_domestic_weighted_average,
         TIV_CH4_waste_domestic_weighted_average,
         TIV_N2O_combustion_domestic_weighted_average,
         TIV_N2O_agriculture_domestic_weighted_average,
         TIV_SF6_domestic_weighted_average,
         TIV_HFC_domestic_weighted_average,
         TIV_PFC_domestic_weighted_average,
         TIV_energy_domestic_weighted_average) %>%
  unique() 





ok = join_ala %>% 
  mutate(year = as.character(year)) %>%
  left_join(weighted_mean_TIV_with_labels, by = c("geo","year","coicop")) %>%
  left_join(weighted_mean_europe_TIV_with_labels, by = c("geo","year","coicop")) %>%
  left_join(weighted_mean_domestic_TIV_with_labels, by = c("geo", "year", "coicop")) %>%
  mutate(co2_kg = fd_me*(TIV_CO2_combustion_weighted_average + TIV_CO2_noncombustion_cement_weighted_average +
                           TIV_CO2_noncombustion_lime_weighted_average + TIV_CO2_agriculture_peatdecay_weighted_average +
                           TIV_CO2_waste_biogenic_weighted_average + TIV_CO2_waste_fossil_weighted_average),
         co2_domestic_kg = fd_me*(TIV_CO2_combustion_domestic_weighted_average + TIV_CO2_noncombustion_cement_domestic_weighted_average +
                                    TIV_CO2_noncombustion_lime_domestic_weighted_average + TIV_CO2_agriculture_peatdecay_domestic_weighted_average +
                                    TIV_CO2_waste_biogenic_domestic_weighted_average + TIV_CO2_waste_fossil_domestic_weighted_average),
         co2_europe_kg = fd_me*((TIV_CO2_combustion_europe_weighted_average + TIV_CO2_noncombustion_cement_europe_weighted_average +
                                   TIV_CO2_noncombustion_lime_europe_weighted_average + TIV_CO2_agriculture_peatdecay_europe_weighted_average +
                                   TIV_CO2_waste_biogenic_europe_weighted_average + TIV_CO2_waste_fossil_europe_weighted_average) - (TIV_CO2_combustion_domestic_weighted_average + TIV_CO2_noncombustion_cement_domestic_weighted_average +
                                                                     TIV_CO2_noncombustion_lime_domestic_weighted_average + TIV_CO2_agriculture_peatdecay_domestic_weighted_average +
                                                                     TIV_CO2_waste_biogenic_domestic_weighted_average + TIV_CO2_waste_fossil_domestic_weighted_average)),
         co2eq_kg = fd_me*(TIV_CO2_combustion_weighted_average +
                             TIV_CO2_noncombustion_cement_weighted_average +
                             TIV_CO2_noncombustion_lime_weighted_average + TIV_CO2_agriculture_peatdecay_weighted_average +
                             TIV_CO2_waste_biogenic_weighted_average + TIV_CO2_waste_fossil_weighted_average +
                             TIV_CH4_combustion_weighted_average +
                             TIV_CH4_noncombustion_gas_weighted_average +
                           TIV_CH4_noncombustion_oil_weighted_average +
                           TIV_CH4_noncombustion_anthracite_weighted_average +
                           TIV_CH4_noncombustion_bituminouscoal_weighted_average +
                           TIV_CH4_noncombustion_cokingcoal_weighted_average +
                           TIV_CH4_noncombustion_lignite_weighted_average +
                           TIV_CH4_noncombustion_subbituminouscoal_weighted_average +
                           TIV_CH4_noncombustion_oilrefinery_weighted_average +
                           TIV_CH4_agriculture_weighted_average +
                           TIV_CH4_waste_weighted_average +
                             TIV_N2O_combustion_weighted_average +
                             TIV_N2O_agriculture_weighted_average + 
                             TIV_SF6_weighted_average +
                             TIV_HFC_weighted_average + 
                             TIV_PFC_weighted_average),
         co2eq_domestic_kg = fd_me*(TIV_CO2_combustion_domestic_weighted_average +
                                      TIV_CO2_noncombustion_cement_domestic_weighted_average +
                                      TIV_CO2_noncombustion_lime_domestic_weighted_average + TIV_CO2_agriculture_peatdecay_domestic_weighted_average +
                                      TIV_CO2_waste_biogenic_domestic_weighted_average + TIV_CO2_waste_fossil_domestic_weighted_average +
                                      TIV_CH4_combustion_domestic_weighted_average +
                                      TIV_CH4_noncombustion_gas_domestic_weighted_average +
                                      TIV_CH4_noncombustion_oil_domestic_weighted_average +
                                      TIV_CH4_noncombustion_anthracite_domestic_weighted_average +
                                      TIV_CH4_noncombustion_bituminouscoal_domestic_weighted_average +
                                      TIV_CH4_noncombustion_cokingcoal_domestic_weighted_average +
                                      TIV_CH4_noncombustion_lignite_domestic_weighted_average +
                                      TIV_CH4_noncombustion_subbituminouscoal_domestic_weighted_average +
                                      TIV_CH4_noncombustion_oilrefinery_domestic_weighted_average +
                                      TIV_CH4_agriculture_domestic_weighted_average +
                                      TIV_CH4_waste_domestic_weighted_average +
                                      TIV_N2O_combustion_domestic_weighted_average +
                                      TIV_N2O_agriculture_domestic_weighted_average +
                                      TIV_SF6_domestic_weighted_average +
                                      TIV_HFC_domestic_weighted_average + 
                                      TIV_PFC_domestic_weighted_average),
         co2eq_europe_kg = fd_me*((TIV_CO2_combustion_europe_weighted_average +
                                     TIV_CO2_noncombustion_cement_europe_weighted_average +
                                     TIV_CO2_noncombustion_lime_europe_weighted_average + TIV_CO2_agriculture_peatdecay_europe_weighted_average +
                                     TIV_CO2_waste_biogenic_europe_weighted_average + TIV_CO2_waste_fossil_europe_weighted_average +
                                     TIV_CH4_combustion_europe_weighted_average +
                                     TIV_CH4_noncombustion_gas_europe_weighted_average +
                                     TIV_CH4_noncombustion_oil_europe_weighted_average +
                                     TIV_CH4_noncombustion_anthracite_europe_weighted_average +
                                     TIV_CH4_noncombustion_bituminouscoal_europe_weighted_average +
                                     TIV_CH4_noncombustion_cokingcoal_europe_weighted_average +
                                     TIV_CH4_noncombustion_lignite_europe_weighted_average +
                                     TIV_CH4_noncombustion_subbituminouscoal_europe_weighted_average +
                                     TIV_CH4_noncombustion_oilrefinery_europe_weighted_average +
                                     TIV_CH4_agriculture_europe_weighted_average +
                                     TIV_CH4_waste_europe_weighted_average +
                                     TIV_N2O_combustion_europe_weighted_average +
                                     TIV_N2O_agriculture_europe_weighted_average +
                                     TIV_SF6_europe_weighted_average +
                                     TIV_HFC_europe_weighted_average + 
                                     TIV_PFC_europe_weighted_average) - 
                                    (TIV_CO2_combustion_domestic_weighted_average +
                                       TIV_CO2_noncombustion_cement_domestic_weighted_average +
                                       TIV_CO2_noncombustion_lime_domestic_weighted_average + TIV_CO2_agriculture_peatdecay_domestic_weighted_average +
                                       TIV_CO2_waste_biogenic_domestic_weighted_average + TIV_CO2_waste_fossil_domestic_weighted_average +
                                       TIV_CH4_combustion_domestic_weighted_average +
                                       TIV_CH4_noncombustion_gas_domestic_weighted_average +
                                       TIV_CH4_noncombustion_oil_domestic_weighted_average +
                                       TIV_CH4_noncombustion_anthracite_domestic_weighted_average +
                                       TIV_CH4_noncombustion_bituminouscoal_domestic_weighted_average +
                                       TIV_CH4_noncombustion_cokingcoal_domestic_weighted_average +
                                       TIV_CH4_noncombustion_lignite_domestic_weighted_average +
                                       TIV_CH4_noncombustion_subbituminouscoal_domestic_weighted_average +
                                       TIV_CH4_noncombustion_oilrefinery_domestic_weighted_average +
                                       TIV_CH4_agriculture_domestic_weighted_average +
                                       TIV_CH4_waste_domestic_weighted_average +
                                       TIV_N2O_combustion_domestic_weighted_average +
                                       TIV_N2O_agriculture_domestic_weighted_average +
                                       TIV_SF6_domestic_weighted_average +
                                       TIV_HFC_domestic_weighted_average + 
                                       TIV_PFC_domestic_weighted_average)),
         energy_use_TJ = fd_me*(TIV_energy_weighted_average),
         energy_use_domestic_TJ = fd_me*(TIV_energy_domestic_weighted_average),
         energy_use_europe_TJ = fd_me*(TIV_energy_europe_weighted_average - 
                                         TIV_energy_domestic_weighted_average))

# direct from FD - to go back to results without direct FD fp, do not run this next chunk and do not bind_rows with 'results'

env_ac_pefasu_no_TR = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_pefasu_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,HH_HEAT,HH_TRA,HH_OTH) 


env_ac_pefasu_TR = env_ac_pefasu_no_TR %>%
  filter(geo == "BG") %>%
  mutate(geo = dplyr::recode(geo,
                             "BG" = "TR"))

env_ac_pefasu = rbind(env_ac_pefasu_no_TR,env_ac_pefasu_TR) %>%
  gather(sector,share_of_total_energy,-geo)

env_ac_ainah_r2 = read_csv(paste0(data_dir_income_stratified_footprints, "/env_ac_ainah_r2_1_Data.csv")) %>%
  filter(TIME == 2015) %>%
  mutate(geo = dplyr::recode(GEO,"Austria" = "AT", 
                             "Belgium" = "BE",
                             "Cyprus" = "CY",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Estonia" = "EE",
                             "Finland" = "FI",
                             "France" = "FR",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Greece" = "EL",
                             "Hungary" = "HU",
                             "Ireland" = "IE",
                             "Italy" = "IT",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Norway" = "NO",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovakia" = "SK",
                             "Slovenia" = "SI",
                             "Spain" = "ES",
                             "Sweden" = "SE",
                             "Turkey" = "TR",
                             "United Kingdom" = "UK",
                             "Bulgaria" = "BG",
                             "Croatia" = "HR")) %>%
  select(NACE_R2,AIRPOL,geo,Value) %>%
  mutate(Value = parse_number(Value),
         Value = as.numeric(Value)) %>%
  spread(NACE_R2,Value) %>%
  clean_names() %>%
  mutate(HH_HEAT = heating_cooling_activities_by_households/total_activities_by_households,
         HH_TRA = transport_activities_by_households/total_activities_by_households,
         HH_OTH = other_activities_by_households/total_activities_by_households) %>%
  select(geo,airpol,HH_HEAT,HH_TRA,HH_OTH) 


env_ac_ainah_r2_co2 = env_ac_ainah_r2 %>%
  filter(airpol == "Carbon dioxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_co2,-geo)

env_ac_ainah_r2_ch4 = env_ac_ainah_r2 %>%
  filter(airpol == "Methane") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_ch4,-geo)

env_ac_ainah_r2_n2o = env_ac_ainah_r2 %>%
  filter(airpol == "Nitrous oxide") %>%
  select(-airpol) %>%
  gather(sector,share_of_total_n2o,-geo)

direct_FD_fp_long = national_fp %>%
  filter(fd_category == "Final consumption expenditure by households",
         geo %in% c("AT",
                    "BE", "BG", "CY", "CZ",
                    "DE" , "DK" , "EE" , 
                    "ES" , "FI" , "FR" , 
                    "UK", "EL", "HR" ,
                    "HU" , "IE" , "IT" ,
                    "LT" , "LU" , "LV" ,
                    "MT" , "NL" , "PL" ,
                    "PT" , "TR" , "SK" ,
                    "SI" , "SE" , "RO" ,
                    "NO")) %>%
  select(year,geo,fd_category,direct_FD_co2_combustion,
         direct_FD_co2_noncombustion_cement,
         direct_FD_co2_noncombustion_lime,
         direct_FD_co2_agriculture_peatdecay,
         direct_FD_co2_waste_biogenic,
         direct_FD_co2_waste_fossil,
         direct_FD_ch4_combustion,
         direct_FD_ch4_noncombustion_gas,
         direct_FD_ch4_noncombustion_oil,
         direct_FD_ch4_noncombustion_anthracite,
         direct_FD_ch4_noncombustion_bituminouscoal,
         direct_FD_ch4_noncombustion_cokingcoal,
         direct_FD_ch4_noncombustion_lignite,
         direct_FD_ch4_noncombustion_subbituminouscoal,
         direct_FD_ch4_noncombustion_oilrefinery,
         direct_FD_ch4_agriculture,
         direct_FD_ch4_waste,
         direct_FD_n2o_combustion,
         direct_FD_n2o_agriculture,
         direct_FD_sf6,
         direct_FD_hfc,
         direct_FD_pfc,
         direct_FD_energy) %>%   
  slice(rep(1:n(), each = 3)) 

sector = rep(c("HH_HEAT","HH_TRA","HH_OTH"), nrow(direct_FD_fp_long)/3) 

direct_FD_fp_long_disagg = cbind(sector,direct_FD_fp_long) %>%
  mutate(coicop = ifelse(sector == "HH_TRA","CP072",
                         ifelse(sector == "HH_HEAT","CP045","CP05")), 
         five_sectors = ifelse(sector == "HH_TRA", "transport",
                               ifelse(sector == "HH_HEAT", "shelter", "manufactured goods"))) %>%
  left_join(env_ac_ainah_r2_co2, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_ch4, by = c("geo","sector")) %>%
  left_join(env_ac_ainah_r2_n2o, by = c("geo","sector")) %>%
  left_join(env_ac_pefasu, by = c("geo","sector")) %>%
  mutate(direct_FD_co2 = (direct_FD_co2_combustion +
                            direct_FD_co2_noncombustion_cement +
                            direct_FD_co2_noncombustion_lime +
                            direct_FD_co2_agriculture_peatdecay +
                            direct_FD_co2_waste_biogenic +
                            direct_FD_co2_waste_fossil)*share_of_total_co2,
         direct_FD_ch4 = (direct_FD_ch4_combustion +
                            direct_FD_ch4_noncombustion_gas +
                            direct_FD_ch4_noncombustion_oil +
                            direct_FD_ch4_noncombustion_anthracite +
                            direct_FD_ch4_noncombustion_bituminouscoal +
                            direct_FD_ch4_noncombustion_cokingcoal +
                            direct_FD_ch4_noncombustion_lignite +
                            direct_FD_ch4_noncombustion_subbituminouscoal +
                            direct_FD_ch4_noncombustion_oilrefinery +
                            direct_FD_ch4_agriculture +
                            direct_FD_ch4_waste)*share_of_total_ch4,
         direct_FD_n2o = (direct_FD_n2o_combustion +
                            direct_FD_n2o_agriculture)*share_of_total_n2o,
         direct_FD_energy = direct_FD_energy*share_of_total_energy) %>%
  left_join(shares, by = c("year","geo","coicop")) %>%
  mutate(disaggregated_direct_FD_co2 = direct_FD_co2*share,
         disaggregated_direct_FD_ch4 = direct_FD_ch4*share,
         disaggregated_direct_FD_n2o = direct_FD_n2o*share,
         disaggregated_direct_FD_energy = direct_FD_energy*share) %>%
  select(year,geo,sector, quintile,
         coicop, five_sectors,
         disaggregated_direct_FD_co2,
         disaggregated_direct_FD_ch4,
         disaggregated_direct_FD_n2o,
         disaggregated_direct_FD_energy) 

direct_FD_co2 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_co2) %>%
  spread(quintile,disaggregated_direct_FD_co2) %>%
  rename(q1_co2 = QUINTILE1,
         q2_co2 = QUINTILE2,
         q3_co2 = QUINTILE3,
         q4_co2 = QUINTILE4,
         q5_co2 = QUINTILE5) %>%
  mutate(q1_co2_domestic = q1_co2,
         q2_co2_domestic = q2_co2,
         q3_co2_domestic = q3_co2,
         q4_co2_domestic = q4_co2,
         q5_co2_domestic = q5_co2,
         co2_total = q1_co2+q2_co2+q3_co2+q4_co2+q5_co2,
         co2_total_domestic = q1_co2_domestic+
           q2_co2_domestic+q3_co2_domestic+
           q4_co2_domestic+q5_co2_domestic)

direct_FD_ch4 = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_ch4) %>%
  spread(quintile,disaggregated_direct_FD_ch4) %>%
  rename(q1_ch4 = QUINTILE1,
         q2_ch4 = QUINTILE2,
         q3_ch4 = QUINTILE3,
         q4_ch4 = QUINTILE4,
         q5_ch4 = QUINTILE5) %>%
  mutate(q1_ch4_domestic = q1_ch4,
         q2_ch4_domestic = q2_ch4,
         q3_ch4_domestic = q3_ch4,
         q4_ch4_domestic = q4_ch4,
         q5_ch4_domestic = q5_ch4,
         ch4_total = q1_ch4+q2_ch4+q3_ch4+q4_ch4+q5_ch4,
         ch4_total_domestic = q1_ch4_domestic+
           q2_ch4_domestic+q3_ch4_domestic+
           q4_ch4_domestic+q5_ch4_domestic)


direct_FD_n2o = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_n2o) %>%
  spread(quintile,disaggregated_direct_FD_n2o) %>%
  rename(q1_n2o = QUINTILE1,
         q2_n2o = QUINTILE2,
         q3_n2o = QUINTILE3,
         q4_n2o = QUINTILE4,
         q5_n2o = QUINTILE5) %>%
  mutate(q1_n2o_domestic = q1_n2o,
         q2_n2o_domestic = q2_n2o,
         q3_n2o_domestic = q3_n2o,
         q4_n2o_domestic = q4_n2o,
         q5_n2o_domestic = q5_n2o,
         n2o_total = q1_n2o+q2_n2o+q3_n2o+q4_n2o+q5_n2o,
         n2o_total_domestic = q1_n2o_domestic+
           q2_n2o_domestic+q3_n2o_domestic+
           q4_n2o_domestic+q5_n2o_domestic)

direct_FD_energy = direct_FD_fp_long_disagg %>%
  select(year,geo,sector,quintile,coicop,five_sectors,disaggregated_direct_FD_energy) %>%
  spread(quintile,disaggregated_direct_FD_energy) %>%
  rename(q1_energy = QUINTILE1,
         q2_energy = QUINTILE2,
         q3_energy = QUINTILE3,
         q4_energy = QUINTILE4,
         q5_energy = QUINTILE5) %>%
  mutate(q1_energy_domestic = q1_energy,
         q2_energy_domestic = q2_energy,
         q3_energy_domestic = q3_energy,
         q4_energy_domestic = q4_energy,
         q5_energy_domestic = q5_energy,
         energy_total = q1_energy+q2_energy+q3_energy+q4_energy+q5_energy,
         energy_total_domestic = q1_energy_domestic+
           q2_energy_domestic+q3_energy_domestic+
           q4_energy_domestic+q5_energy_domestic)


direct_FD_fp_wide = direct_FD_co2 %>%
  left_join(direct_FD_ch4, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_n2o, by = c("year","geo",
                                  "sector","coicop",
                                  "five_sectors")) %>%
  left_join(direct_FD_energy, by = c("year","geo",
                                     "sector","coicop",
                                     "five_sectors")) %>%
  mutate(country_of_production = geo) %>%
  mutate(q1_co2eq = q1_co2 + q1_ch4 + q1_n2o,
         q2_co2eq = q2_co2 + q2_ch4 + q2_n2o,
         q3_co2eq = q3_co2 + q3_ch4 + q3_n2o,
         q4_co2eq = q4_co2 + q4_ch4 + q4_n2o,
         q5_co2eq = q5_co2 + q5_ch4 + q5_n2o,
         co2eq_total = q1_co2eq +
           q2_co2eq + q3_co2eq +
           q4_co2eq + q5_co2eq,
         q1_co2eq_domestic = q1_co2_domestic + q1_ch4_domestic + q1_n2o_domestic,
         q2_co2eq_domestic = q2_co2_domestic + q2_ch4_domestic + q2_n2o_domestic,
         q3_co2eq_domestic = q3_co2_domestic + q3_ch4_domestic + q3_n2o_domestic,
         q4_co2eq_domestic = q4_co2_domestic + q4_ch4_domestic + q4_n2o_domestic,
         q5_co2eq_domestic = q5_co2_domestic + q5_ch4_domestic + q5_n2o_domestic,
         co2eq_total_domestic = q1_co2eq_domestic +
           q2_co2eq_domestic + q3_co2eq_domestic +
           q4_co2eq_domestic + q5_co2eq_domestic) %>%
  select(-q1_ch4,
         -q2_ch4,
         -q3_ch4,
         -q4_ch4,
         -q5_ch4,
         -ch4_total,
         -q1_ch4_domestic,
         -q2_ch4_domestic,
         -q3_ch4_domestic,
         -q4_ch4_domestic,
         -q5_ch4_domestic,
         -ch4_total_domestic,
         -q1_n2o,
         -q2_n2o,
         -q3_n2o,
         -q4_n2o,
         -q5_n2o,
         -n2o_total,
         -q1_n2o_domestic,
         -q2_n2o_domestic,
         -q3_n2o_domestic,
         -q4_n2o_domestic,
         -q5_n2o_domestic,
         -n2o_total_domestic)

direct_FD_fp_wide_all = direct_FD_fp_wide %>%
  clean_names() %>%
  select(year,geo,coicop,q1_co2:q5_co2,
         q1_co2_domestic:q5_co2_domestic,
         q1_co2eq:q5_co2eq,
         q1_co2eq_domestic:q5_co2eq_domestic,
         q1_energy:q5_energy,
         q1_energy_domestic:q5_energy_domestic)

## extract co2 and pivot long
cols_co2 = c("q1_co2", "q2_co2", "q3_co2", "q4_co2", "q5_co2")
tmp_co2 = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_co2) %>%
  pivot_longer(cols = cols_co2,
               names_to = "quintile",
               values_to = "co2_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

## extract co2 domestic and pivot long
cols_co2_domestic = c("q1_co2_domestic", "q2_co2_domestic", "q3_co2_domestic", "q4_co2_domestic", "q5_co2_domestic")
tmp_co2_domestic = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_co2_domestic) %>%
  pivot_longer(cols = cols_co2_domestic,
               names_to = "quintile",
               values_to = "co2_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

## extract co2eq and pivot long
cols_co2eq = c("q1_co2eq", "q2_co2eq", "q3_co2eq", "q4_co2eq", "q5_co2eq")
tmp_co2eq = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_co2eq) %>%
  pivot_longer(cols = cols_co2eq,
               names_to = "quintile",
               values_to = "co2eq_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

## extract co2eq domestic and pivot long
cols_co2eq_domestic = c("q1_co2eq_domestic", "q2_co2eq_domestic", "q3_co2eq_domestic", "q4_co2eq_domestic", "q5_co2eq_domestic")
tmp_co2eq_domestic = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_co2eq_domestic) %>%
  pivot_longer(cols = cols_co2eq_domestic,
               names_to = "quintile",
               values_to = "co2eq_domestic_kg") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

## extract energy use and pivot long
cols_energy = c("q1_energy","q2_energy","q3_energy","q4_energy","q5_energy")
tmp_energy = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_energy) %>%
  pivot_longer(cols = cols_energy,
               names_to = "quintile",
               values_to = "energy_use_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

## extract energy domestic and pivot long
cols_energy_domestic = c("q1_energy_domestic","q2_energy_domestic","q3_energy_domestic","q4_energy_domestic","q5_energy_domestic")
tmp_energy_domestic = direct_FD_fp_wide_all %>%
  select(year, geo, coicop, cols_energy_domestic) %>%
  pivot_longer(cols = cols_energy_domestic,
               names_to = "quintile",
               values_to = "energy_use_domestic_TJ") %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile)

direct_FD_fp_wide_recombined = tmp_co2 %>%
  left_join(tmp_co2_domestic, by=c("year", "geo", "coicop", "quint")) %>%
  left_join(tmp_co2eq, by=c("year", "geo", "coicop", "quint")) %>%
  left_join(tmp_co2eq_domestic, by=c("year", "geo", "coicop", "quint")) %>%
  left_join(tmp_energy, by=c("year", "geo", "coicop", "quint")) %>%
  left_join(tmp_energy_domestic, by=c("year", "geo", "coicop", "quint")) %>%
  clean_names() %>%
  mutate(year = as.numeric(year))

results = ok %>% 
  filter(!(geo %in% c("EA","EA12","EA13","EA17",
                      "EA18","EA19","EEA28","EEA30_2007",
                      "EFTA","EU15","EU25",
                      "EU27_2007", "EU27_2020",
                      "EU28","XK", "RS", 
                      "MK", "ME")),
         !(quintile %in% c("TOTAL","UNK")),
         !(year %in% c(1988,1994,1999))) %>%
  group_by(geo,quintile,year,coicop) %>%
  summarise(fd_me = sum(fd_me, na.rm = TRUE),
            co2_kg = sum(co2_kg, na.rm = TRUE),
            co2_domestic_kg = sum(co2_domestic_kg, na.rm = TRUE),
            co2_europe_kg = sum(co2_europe_kg, na.rm = TRUE),
            co2eq_kg = sum(co2eq_kg, na.rm = TRUE),
            co2eq_domestic_kg = sum(co2eq_domestic_kg, na.rm = TRUE),
            co2eq_europe_kg = sum(co2eq_europe_kg, na.rm = TRUE),
            energy_use_TJ = sum(energy_use_TJ, na.rm = TRUE),
            energy_use_domestic_TJ = sum(energy_use_domestic_TJ, na.rm = TRUE),
            energy_use_europe_TJ = sum(energy_use_europe_TJ, na.rm = TRUE)) %>% 
  ungroup() %>%
  mutate(year = as.numeric(year)) %>%
  na.omit() 

results_formatted = results %>%
  clean_names() %>%
  mutate(quint = parse_number(quintile)) %>%
  select(-quintile) %>%
  filter(coicop %in% c("CP011",
                       "CP012",
                       "CP02",
                       "CP03",
                       "rent",
                       "CP043",
                       "CP044",
                       "CP045",
                       "CP05",
                       "CP06",
                       "CP071",
                       "CP072",
                       "CP073",
                       "CP08",
                       "CP09",
                       "CP10",
                       "CP11",
                       "CP12"))

results_formatted_with_direct_FD_fp = bind_rows(results_formatted,direct_FD_fp_wide_recombined)


write.csv(results_formatted_with_direct_FD_fp, paste0(data_dir_income_stratified_footprints, "/results_formatted_method2_ixi.csv"))
write_rds(results_formatted_with_direct_FD_fp, paste0(data_dir_income_stratified_footprints, "/results_formatted_method2_ixi.rds"))

```

# European expenditure deciles

- need to download EUROSTAT households and Norway households

1) The 'lfst_hhnhtych' table from EUROSTAT, selecting all available years and total households [accessed on 04.05.2020]. 

2) Norway is missing from the 'lfst_hhnhtych' EUROSTAT table. We download Norwegian data from the Norwegian statistical office: https://www.ssb.no/en/statbank/table/10986/. We select 'Private households', 'the whole country', no household type selection, all years (2005-2019), and continue with 'Table - Layout 1', then save the table as a 'Tab delimited without heading (csv)' file [accessed on 04.05.2020].

```{r european-expenditure-deciles, eval = FALSE}

# set target number of quantiles
target_eu_ntiles = 10

##### main paper results (main paper method, EXIOBASE industry-by-industry version)

# 1) load income-stratified-footprints formatted results file from previous code chunk
dat_results_raw = read_rds(here("analysis", "preprocessing", "income-stratified-footprints",
                                "results_formatted_method1_ixi.rds")) %>%
  ungroup() %>%
  mutate(year= strtoi(year)) %>%
  rename(iso2 = geo)

# get iso3 country codes to join with household data
country_codes = ISOcodes::ISO_3166_1 %>%
  select(iso2 = Alpha_2, iso3 = Alpha_3) %>%
  # resolve inconsistency between EUROSTAT and ISO for Greece and UK/Great Britain
  mutate(iso2 = if_else(iso2=="GR", "EL", iso2)) %>%
  mutate(iso2 = if_else(iso2=="GB", "UK", iso2))

# read in total private households data from EUROSTAT and Norway, merge, and write .csv 
## set 4 digits per value
options(digits=4)

## EUROSTAT total private households
total_private_households_Eurostat = read.csv(here("/analysis/preprocessing/income-stratified-footprints/lfst_hhnhtych_1_Data.csv")) %>%
  filter(!(GEO %in% c("European Union - 27 countries (from 2020)",
                    "Euro area - 19 countries  (from 2015)",
                    "European Union - 28 countries (2013-2020)",
                    "European Union - 15 countries (1995-2004)"))) %>%
  mutate(geo = dplyr::recode(GEO,
                             "Belgium" = "BE",
                             "Bulgaria" = "BG",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Estonia" = "EE",
                             "Ireland" = "IE",
                             "Greece" = "EL",
                             "Spain" = "ES",
                             "France" = "FR",
                             "Croatia" = "HR",
                             "Italy" = "IT",
                             "Cyprus" = "CY",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Hungary" = "HU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Austria" = "AT",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovenia" = "SI",
                             "Slovakia" = "SK",
                             "Finland" = "FI",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Montenegro" = "ME",
                             "North Macedonia" = "MK",
                             "Serbia" = "RS",
                             "Turkey" = "TR")) %>%
  select(TIME,geo,Value) %>%
  rename(year = TIME, total_private_households = Value) %>%
  mutate(total_private_households = as.character(total_private_households),
         total_private_households = parse_number(total_private_households),
         total_private_households = as.numeric(total_private_households),
         total_private_households = total_private_households*1000)

## Norway total private households
total_private_households_Norway = read.csv(here("/analysis/preprocessing/income-stratified-footprints/Privathusholdninger.csv")) %>%
  gather(year, total_private_households, Private.households.2005:Private.households.2019) %>%
  mutate(geo = dplyr::recode(region,
                             "0 The whole country" = "NO"),
         year = dplyr::recode(year,
                              "Private.households.2005" = 2005,
                              "Private.households.2006" = 2006,
                              "Private.households.2007" = 2007,
                              "Private.households.2008" = 2008,
                              "Private.households.2009" = 2009,
                              "Private.households.2010" = 2010,
                              "Private.households.2011" = 2011,
                              "Private.households.2012" = 2012,
                              "Private.households.2013" = 2013,
                              "Private.households.2014" = 2014,
                              "Private.households.2015" = 2015,
                              "Private.households.2016" = 2016,
                              "Private.households.2017" = 2017,
                              "Private.households.2018" = 2018,
                              "Private.households.2019" = 2019)) %>%
  select(year,geo,total_private_households)

## merge EUROSTAT and Norway total private households data
total_private_households = rbind(total_private_households_Eurostat,
                                 total_private_households_Norway) %>%
  mutate(geo = as.character(geo),
         year = as.numeric(year),
         total_private_households = as.numeric(total_private_households))

## write .csv file with all total private households data
write_csv(total_private_households, here("/analysis/preprocessing/income-stratified-footprints/total_private_households.csv"))
# the original 'total_private_households_original.csv' is in 'income-stratified-footprints-in-europe/data' - if something goes wrong
# with the main paper results (pulling from the mrio results file written later) then the problem is here, when I added 
# the as.character mutation in the total_private_households_Eurostat

## return to 2 digits per value
options(digits=2)

# 2) load merged total private households data 
hh_data = read_csv(here("analysis", "preprocessing", "income-stratified-footprints", 
                        "total_private_households.csv")) %>%
  mutate(imputed = if_else(is.na(total_private_households), TRUE, FALSE)) %>%
  rename(iso2 = geo) %>%
  group_by(iso2) %>%
  # impute households with next available neighbour
  mutate(hh = na_locf(total_private_households)) %>%
  left_join(country_codes, by="iso2") %>%
  select(-total_private_households)

#3) load EUROSTAT mean expenditures per household income quintile per household and per adult equivalent (written and saved in the previous, income-stratified-footprints code chunk)
df_expenditure_long = read_csv(here("analysis",
                                    "preprocessing",
                                    "income-stratified-footprints",
                                    "mean_expenditure_by_quintile_long.csv"),
                               na = ":") %>%
  #filter(year >=2010, geo != "IT") %>%
  #filter(year >=2005, geo != "IT") %>%
  #decide here
  filter(year >=2005) %>%
  mutate(imputed = if_else(is.na(mean_expenditure), TRUE, FALSE)) %>%
  group_by(geo,unit,quintile) %>%
  mutate(value = na_locf(mean_expenditure)) %>%
  select(-mean_expenditure) %>% 
  ungroup()

## calculate adult equivalents per household
df_adult_e_p_hh = df_expenditure_long %>%
  rename(iso2 = geo) %>%
  pivot_wider(id_cols = c(iso2, year, quintile, imputed), 
              names_from = unit, 
              values_from = value) %>%
  clean_names() %>%
  mutate(adult_e_p_hh = pps_hh/pps_ae) %>%
  left_join(country_codes, by="iso2") %>%
  mutate(iso3 = if_else(iso2 == "XK", "XKX", iso3),
         quint = parse_number(quintile))

## add quintile population data
mrio_results_with_adult_eq_all = dat_results_raw %>%
  filter(year %in% c(2005, 2010, 2015)) %>%
  left_join(hh_data, by=c("iso2", "year")) %>%
  mutate(hh_quintile = hh/5) %>% # population per country quinitle
  select(-hh) %>%
  rename(hh_imputed = imputed) %>%
  left_join(df_adult_e_p_hh %>%
              select(iso2, year, quint, imputed_ae = imputed, adult_e_p_hh), 
            by=c("iso2", "year", "quint")) %>%
  mutate(ae_quintile = hh_quintile * adult_e_p_hh) %>%
  select(-c(hh_quintile, adult_e_p_hh))

## for the European expenditure deciles we use only countries with data in 2005, 2010 and 2015. This excludes Luxembourg and Italy. 
complete_countries = mrio_results_with_adult_eq_all %>%
  group_by(year, iso2) %>%
  summarise(co2_kg = sum(co2_kg)) %>%
  ungroup() %>%
  filter(co2_kg>0) %>%
  select(iso2, year, co2_kg) %>%
  pivot_wider(id_cols = c(iso2), names_from = year, values_from = co2_kg) %>%
  drop_na() %>%
  select(iso2) %>%
  pull()

#df_adult_e_p_hh %>% #what is this?
#  filter(iso2 %in% complete_countries, year<=2015, year>=2005) %>%
#  mutate(quint = parse_number(quintile)) 

# calculate European expenditure deciles based on loaded income-stratified-footprints result file and adult equivalents.
# returns country quintiles mapped to European ntile rank and European ntile boundaries
# helper function called by function below
calculate_eu_ntiles <- function(pyear, pquantile_count=10) {
  
  country_data_annual_sorted = summary_country_fd %>%
    ungroup() %>%
    filter(year==pyear) %>%
    arrange(fd_pae_e) %>%
    mutate(idx = 1:n(),
           eu_q_rank = 0) # later to be filled with euro quintile rank
  
  # total European adult equivalents (of included countries) in year
  total_ae_in_year = sum(country_data_annual_sorted$ae_quintile)
  
  # quantile target ae population
  eu_decile_adult_eq = total_ae_in_year/pquantile_count
  
  # country quintles must be split to allocate ae population according to eu quantile target ae population
  # filtering by condition that can't be fulfilled is a lazy way to create an empty dataframe
  # of the same structure as country_data_annual_sorted
  additional_rows = country_data_annual_sorted %>%
    filter(year==1)
  
  # store quantile split values
  eu_quantile_boundaries = data.frame(euro_q_rank = 1:pquantile_count, p = 0)
  
  # loops through the ordered dataset, assigns European quantile rank
  # and splits quintiles where necessary
  eu_ae_current = 0
  euro_q_rank_current = 1
  for (row_idx in 1:nrow(country_data_annual_sorted)) {
    row = country_data_annual_sorted[row_idx,]
    if (row["ae_quintile"] + eu_ae_current <= eu_decile_adult_eq) {
      eu_ae_current = eu_ae_current + row["ae_quintile"]
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
    } else {
      ae_diff = eu_decile_adult_eq - eu_ae_current
      ## write rest of this European decile (split country quintile)
      new_row = country_data_annual_sorted[row_idx, ]
      new_row[1, "eu_q_rank"] = euro_q_rank_current
      new_row[1, "ae_quintile"] = ae_diff
      ## record European quantile boundary
      eu_quantile_boundaries[eu_quantile_boundaries$euro_q_rank==euro_q_rank_current, "p"] =
        country_data_annual_sorted[row_idx, "fd_pae_e"]
      ## put first part of population in overflow dataframe
      additional_rows = additional_rows %>%
        bind_rows(new_row)
      ## classify rest of country quintile population to next European quantile
      country_data_annual_sorted[row_idx, "ae_quintile"] = 
        country_data_annual_sorted[row_idx, "ae_quintile"] - (ae_diff+0.0001)
      euro_q_rank_current = euro_q_rank_current + 1
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
      eu_ae_current = country_data_annual_sorted[row_idx, "ae_quintile"]
      
    }
  }
  
  country_data_eu_quantiles = country_data_annual_sorted %>%
  bind_rows(additional_rows) %>%
  arrange(fd_pae_e, eu_q_rank) %>%
  mutate(idx = 1:n())

  # add zeroth and nth quantile (min and max)
  eu_quantile_boundaries[pquantile_count, "p"] = max(country_data_eu_quantiles$fd_pae_e)
  
  list("df_q_data" = country_data_eu_quantiles, "df_q_boundaries" = eu_quantile_boundaries)
}

# maps income-stratified-footprint results to European ntile ranks, returns mapping and ntile European boundaries
map_mrio_results_to_eu_ntiles <- function(pyear, ptarget_ntiles) {
  
  df_eu_ntiles = calculate_eu_ntiles(pyear, pquantile_count = ptarget_ntiles)
  df_eu_ntiles_data = df_eu_ntiles$df_q_data
  
  sector_mapping = mrio_results_with_adult_eq %>%
    group_by(sector_id) %>%#
    summarise(sector_agg_id = first(sector_agg_id)) %>%
    ungroup()
  
  df_mapped_data = mrio_results_with_adult_eq %>%
    select(year, 
           iso2, 
           quint, 
           sector_id, 
           fd_me, 
           co2_kg, 
           co2_domestic_kg, 
           co2_europe_kg,
           co2eq_kg, 
           co2eq_domestic_kg, 
           co2eq_europe_kg,
           energy_use_TJ, 
           energy_use_domestic_TJ,
           energy_use_europe_TJ,
           ae_quintile) %>%
    filter(year==pyear) %>%
    # calculate per adult equivalent values in quintiles
    mutate(fd_pae_e = fd_me*1000000/ae_quintile,
           co2_pae_kg = co2_kg/ae_quintile,
           co2_pae_dom_kg = co2_domestic_kg/ae_quintile,
           co2_pae_eu_kg = co2_europe_kg/ae_quintile,
           co2eq_pae_kg = co2eq_kg/ae_quintile,
           co2eq_pae_dom_kg = co2eq_domestic_kg/ae_quintile,
           co2eq_pae_eu_kg = co2eq_europe_kg/ae_quintile,
           energy_use_pae_tj = energy_use_TJ/ae_quintile,
           energy_use_dom_pae_tj = energy_use_domestic_TJ/ae_quintile,
           energy_use_eu_pae_tj = energy_use_europe_TJ/ae_quintile) %>%
    # remove totals
    select(-c(fd_me, 
              co2_kg,
              co2_domestic_kg,
              co2_europe_kg,
              co2eq_kg,
              co2eq_domestic_kg,
              co2eq_europe_kg,
              energy_use_TJ,
              energy_use_domestic_TJ,
              energy_use_europe_TJ,
              year, ae_quintile)) %>%
    full_join(df_eu_ntiles_data %>%
                rename(fd_pae_e_quint_tmp = fd_pae_e), by=c("iso2", "quint")) %>%
    rename(adult_eq = ae_quintile) %>% # country quintile and their split fraction population
    # recalculate totals
    mutate(fd_me = fd_pae_e*adult_eq/1000000,
           co2_kg = co2_pae_kg*adult_eq,
           co2_dom_kg = co2_pae_dom_kg*adult_eq,
           co2_eu_kg = co2_pae_eu_kg*adult_eq,
           co2eq_kg = co2eq_pae_kg*adult_eq,
           co2eq_dom_kg = co2eq_pae_dom_kg*adult_eq,
           co2eq_eu_kg = co2eq_pae_eu_kg*adult_eq,
           energy_use_tj = energy_use_pae_tj*adult_eq,
           energy_use_dom_tj = energy_use_dom_pae_tj*adult_eq,
           energy_use_eu_tj = energy_use_eu_pae_tj*adult_eq
           ) %>%
    left_join(sector_mapping, by="sector_id")
  
  list("df_mapped_data" = df_mapped_data, "df_ntile_boundaries" = df_eu_ntiles$df_q_boundaries)
  
}

# filter only countries with complete info for years 2005, 2010, 2015
mrio_results_with_adult_eq = mrio_results_with_adult_eq_all %>%
  filter(iso2 %in% complete_countries)

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for complete countries
summary_country_fd = mrio_results_with_adult_eq %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000)/(ae_quintile))

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for all countries
summary_country_fd_all = mrio_results_with_adult_eq_all %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000)/(ae_quintile))

df_mapped_result_2005 = map_mrio_results_to_eu_ntiles(2005, target_eu_ntiles)
df_mapped_result_2005_data = df_mapped_result_2005$df_mapped_data
df_mapped_result_2005_ntiles = df_mapped_result_2005$df_ntile_boundaries

df_mapped_result_2010 = map_mrio_results_to_eu_ntiles(2010, target_eu_ntiles)
df_mapped_result_2010_data = df_mapped_result_2010$df_mapped_data
df_mapped_result_2010_ntiles = df_mapped_result_2010$df_ntile_boundaries

df_mapped_result_2015 = map_mrio_results_to_eu_ntiles(2015, target_eu_ntiles)
df_mapped_result_2015_data = df_mapped_result_2015$df_mapped_data
df_mapped_result_2015_ntiles = df_mapped_result_2015$df_ntile_boundaries

df_mapped_result_data = df_mapped_result_2005_data %>%
  bind_rows(df_mapped_result_2010_data) %>%
  bind_rows(df_mapped_result_2015_data)

write_csv(df_mapped_result_data, 
          here(paste0("analysis/data/derived/mrio_results_eu_ntile_mapped_n_", target_eu_ntiles, ".csv")))


###### SI results, main paper method, EXIOBASE product-by-product version

# 1) load income-stratified-footprints formatted results file
dat_results_raw = read_rds(here("analysis", "preprocessing", "income-stratified-footprints",
                            "results_formatted_method1_pxp.rds")) %>%
  ungroup() %>%
  mutate(year= strtoi(year)) %>%
  rename(iso2 = geo)

# get iso3 country codes to join with household data
country_codes = ISOcodes::ISO_3166_1 %>%
  select(iso2 = Alpha_2, iso3 = Alpha_3) %>%
  # resolve inconsistency between EUROSTAT and ISO for Greece and UK/Great Britain
  mutate(iso2 = if_else(iso2=="GR", "EL", iso2)) %>%
  mutate(iso2 = if_else(iso2=="GB", "UK", iso2))

# read in total private households data from EUROSTAT and Norway, merge, and write .csv 
## set 4 digits per value
options(digits=4)

## EUROSTAT total private households
total_private_households_Eurostat = read.csv(here("/analysis/preprocessing/income-stratified-footprints/lfst_hhnhtych_1_Data.csv")) %>%
  filter(!(GEO %in% c("European Union - 27 countries (from 2020)",
                    "Euro area - 19 countries  (from 2015)",
                    "European Union - 28 countries (2013-2020)",
                    "European Union - 15 countries (1995-2004)"))) %>%
  mutate(geo = dplyr::recode(GEO,
                             "Belgium" = "BE",
                             "Bulgaria" = "BG",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Estonia" = "EE",
                             "Ireland" = "IE",
                             "Greece" = "EL",
                             "Spain" = "ES",
                             "France" = "FR",
                             "Croatia" = "HR",
                             "Italy" = "IT",
                             "Cyprus" = "CY",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Hungary" = "HU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Austria" = "AT",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovenia" = "SI",
                             "Slovakia" = "SK",
                             "Finland" = "FI",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Montenegro" = "ME",
                             "North Macedonia" = "MK",
                             "Serbia" = "RS",
                             "Turkey" = "TR")) %>%
  select(TIME,geo,Value) %>%
  rename(year = TIME, total_private_households = Value) %>%
  mutate(total_private_households = as.character(total_private_households),
         total_private_households = parse_number(total_private_households),
         total_private_households = as.numeric(total_private_households),
         total_private_households = total_private_households*1000)

## Norway total private households
total_private_households_Norway = read.csv(here("/analysis/preprocessing/income-stratified-footprints/Privathusholdninger.csv")) %>%
  gather(year, total_private_households, Private.households.2005:Private.households.2019) %>%
  mutate(geo = dplyr::recode(region,
                             "0 The whole country" = "NO"),
         year = dplyr::recode(year,
                              "Private.households.2005" = 2005,
                              "Private.households.2006" = 2006,
                              "Private.households.2007" = 2007,
                              "Private.households.2008" = 2008,
                              "Private.households.2009" = 2009,
                              "Private.households.2010" = 2010,
                              "Private.households.2011" = 2011,
                              "Private.households.2012" = 2012,
                              "Private.households.2013" = 2013,
                              "Private.households.2014" = 2014,
                              "Private.households.2015" = 2015,
                              "Private.households.2016" = 2016,
                              "Private.households.2017" = 2017,
                              "Private.households.2018" = 2018,
                              "Private.households.2019" = 2019)) %>%
  select(year,geo,total_private_households)

## merge EUROSTAT and Norway total private households data
total_private_households = rbind(total_private_households_Eurostat,
                                 total_private_households_Norway) %>%
  mutate(geo = as.character(geo),
         year = as.numeric(year),
         total_private_households = as.numeric(total_private_households))

## write .csv file with all total private households data
write_csv(total_private_households, here("/analysis/preprocessing/income-stratified-footprints/total_private_households.csv"))
# the original 'total_private_households_original.csv' is in 'income-stratified-footprints-in-europe/data' - if something goes wrong
# with the main paper results (pulling from the mrio results file written later) then the problem is here, when I added 
# the as.character mutation in the total_private_households_Eurostat

## return to 2 digits per value
options(digits=2)

# 2) load merged total private households data 
hh_data = read_csv(here("analysis", "preprocessing", "income-stratified-footprints",
                        "total_private_households.csv")) %>%
  mutate(imputed = if_else(is.na(total_private_households), TRUE, FALSE)) %>%
  rename(iso2 = geo) %>%
  group_by(iso2) %>%
  # impute households with next available neighbour
  mutate(hh = na_locf(total_private_households)) %>%
  left_join(country_codes, by="iso2") %>%
  select(-total_private_households)

#3) load EUROSTAT mean expenditures per household income quintile per household and per adult equivalent (written and saved in the previous, income-stratified-footprints code chunk)
df_expenditure_long = read_csv(here("analysis", "preprocessing", "income-stratified-footprints",
                                    "mean_expenditure_by_quintile_long.csv"),
                               na = ":") %>%
  #filter(year >=2010, geo != "IT") %>%
  #filter(year >=2005, geo != "IT") %>%
  #decide here
  filter(year >=2005) %>%
  mutate(imputed = if_else(is.na(mean_expenditure), TRUE, FALSE)) %>%
  group_by(geo,unit,quintile) %>%
  mutate(value = na_locf(mean_expenditure)) %>%
  select(-mean_expenditure) %>% 
  ungroup()

## calculate adult equivalents per household
df_adult_e_p_hh = df_expenditure_long %>%
  rename(iso2 = geo) %>%
  pivot_wider(id_cols = c(iso2, year, quintile, imputed), 
              names_from = unit, 
              values_from = value) %>%
  clean_names() %>%
  mutate(adult_e_p_hh = pps_hh/pps_ae) %>%
  left_join(country_codes, by="iso2") %>%
  mutate(iso3 = if_else(iso2 == "XK", "XKX", iso3),
         quint = parse_number(quintile))

## add quintile population data
mrio_results_with_adult_eq_all = dat_results_raw %>%
  filter(year %in% c(2005, 2010, 2015)) %>%
  left_join(hh_data, by=c("iso2", "year")) %>%
  mutate(hh_quintile = hh/5) %>% # population per country quinitle
  select(-hh) %>%
  rename(hh_imputed = imputed) %>%
  left_join(df_adult_e_p_hh %>%
              select(iso2, year, quint, imputed_ae = imputed, adult_e_p_hh), 
            by=c("iso2", "year", "quint")) %>%
  mutate(ae_quintile = hh_quintile * adult_e_p_hh) %>%
  select(-c(hh_quintile, adult_e_p_hh))

## for the European expenditure deciles we use only countries with data in 2005, 2010 and 2015. This excludes Luxembourg and Italy. 
complete_countries = mrio_results_with_adult_eq_all %>%
  group_by(year, iso2) %>%
  summarise(co2_kg = sum(co2_kg)) %>%
  ungroup() %>%
  filter(co2_kg>0) %>%
  select(iso2, year, co2_kg) %>%
  pivot_wider(id_cols = c(iso2), names_from = year, values_from = co2_kg) %>%
  drop_na() %>%
  select(iso2) %>%
  pull()

df_adult_e_p_hh %>%
  filter(iso2 %in% complete_countries, year<=2015, year>=2005) %>%
  mutate(quint = parse_number(quintile)) 

# calculate European expenditure deciles based on loaded income-stratified-footprints result file and adult equivalents.
# returns country quintiles mapped to European ntile rank and European ntile boundaries
# helper function called by function below
calculate_eu_ntiles <- function(pyear, pquantile_count=10) {
  
  country_data_annual_sorted = summary_country_fd %>%
    ungroup() %>%
    filter(year==pyear) %>%
    arrange(fd_pae_e) %>%
    mutate(idx = 1:n(),
           eu_q_rank = 0) # later to be filled with euro quintile rank
  
  # total European adult equivalents (of included countries) in year
  total_ae_in_year = sum(country_data_annual_sorted$ae_quintile)
  
  # quantile target ae population
  eu_decile_adult_eq = total_ae_in_year/pquantile_count
  
  # country quintiles must be split to allocate ae population according to European quantile target ae population
  # filtering by condition that cant be fulfilled is a lazy way to create an empty dataframe
  # of the same structure as country_data_annual_sorted
  additional_rows = country_data_annual_sorted %>%
    filter(year==1)
  
  # store quantile split values
  eu_quantile_boundaries = data.frame(euro_q_rank = 1:pquantile_count, p = 0)
  
  # loops through the ordered dataset, assignes euro quantile rank
  # and splits quintiles where necessary
  eu_ae_current = 0
  euro_q_rank_current = 1
  for (row_idx in 1:nrow(country_data_annual_sorted)) {
    row = country_data_annual_sorted[row_idx,]
    if (row["ae_quintile"] + eu_ae_current <= eu_decile_adult_eq) {
      eu_ae_current = eu_ae_current + row["ae_quintile"]
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
    } else {
      ae_diff = eu_decile_adult_eq - eu_ae_current
      ## write rest of this European decile (split country quintile)
      new_row = country_data_annual_sorted[row_idx, ]
      new_row[1, "eu_q_rank"] = euro_q_rank_current
      new_row[1, "ae_quintile"] = ae_diff
      ## record European quantile boundary
      eu_quantile_boundaries[eu_quantile_boundaries$euro_q_rank==euro_q_rank_current, "p"] =
        country_data_annual_sorted[row_idx, "fd_pae_e"]
      ## put first part of population in overflow dataframe
      additional_rows = additional_rows %>%
        bind_rows(new_row)
      ## classify rest of country quintile population to next European quantile
      country_data_annual_sorted[row_idx, "ae_quintile"] = 
        country_data_annual_sorted[row_idx, "ae_quintile"] - (ae_diff+0.0001)
      euro_q_rank_current = euro_q_rank_current + 1
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
      eu_ae_current = country_data_annual_sorted[row_idx, "ae_quintile"]
      
    }
  }
  
  country_data_eu_quantiles = country_data_annual_sorted %>%
  bind_rows(additional_rows) %>%
  arrange(fd_pae_e, eu_q_rank) %>%
  mutate(idx = 1:n())

  # add zeroth and nth quantile (min and max)
  eu_quantile_boundaries[pquantile_count, "p"] = max(country_data_eu_quantiles$fd_pae_e)
  
  list("df_q_data" = country_data_eu_quantiles, "df_q_boundaries" = eu_quantile_boundaries)
}

# maps income-stratified-footprint results to European ntile ranks, returns mapping and ntile European boundaries
map_mrio_results_to_eu_ntiles <- function(pyear, ptarget_ntiles) {
  
  df_eu_ntiles = calculate_eu_ntiles(pyear, pquantile_count = ptarget_ntiles)
  df_eu_ntiles_data = df_eu_ntiles$df_q_data
  
  sector_mapping = mrio_results_with_adult_eq %>%
    group_by(sector_id) %>%#
    summarise(sector_agg_id = first(sector_agg_id)) %>%
    ungroup()
  
  df_mapped_data = mrio_results_with_adult_eq %>%
    select(year, 
           iso2, 
           quint, 
           sector_id, 
           fd_me, 
           co2_kg, 
           co2_domestic_kg, 
           co2_europe_kg,
           co2eq_kg, 
           co2eq_domestic_kg, 
           co2eq_europe_kg,
           energy_use_TJ, 
           energy_use_domestic_TJ,
           energy_use_europe_TJ,
           ae_quintile) %>%
    filter(year==pyear) %>%
    # calculate per adult equivalent values in quintiles
    mutate(fd_pae_e = fd_me*1000000/ae_quintile,
           co2_pae_kg = co2_kg/ae_quintile,
           co2_pae_dom_kg = co2_domestic_kg/ae_quintile,
           co2_pae_eu_kg = co2_europe_kg/ae_quintile,
           co2eq_pae_kg = co2eq_kg/ae_quintile,
           co2eq_pae_dom_kg = co2eq_domestic_kg/ae_quintile,
           co2eq_pae_eu_kg = co2eq_europe_kg/ae_quintile,
           energy_use_pae_tj = energy_use_TJ/ae_quintile,
           energy_use_dom_pae_tj = energy_use_domestic_TJ/ae_quintile,
           energy_use_eu_pae_tj = energy_use_europe_TJ/ae_quintile) %>%
    # remove totals
    select(-c(fd_me, 
              co2_kg,
              co2_domestic_kg,
              co2_europe_kg,
              co2eq_kg,
              co2eq_domestic_kg,
              co2eq_europe_kg,
              energy_use_TJ,
              energy_use_domestic_TJ,
              energy_use_europe_TJ,
              year, ae_quintile)) %>%
    full_join(df_eu_ntiles_data %>%
                rename(fd_pae_e_quint_tmp = fd_pae_e), by=c("iso2", "quint")) %>%
    rename(adult_eq = ae_quintile) %>% # country quintile and their split fraction population
    # recalculate totals
    mutate(fd_me = fd_pae_e*adult_eq/1000000,
           co2_kg = co2_pae_kg*adult_eq,
           co2_dom_kg = co2_pae_dom_kg*adult_eq,
           co2_eu_kg = co2_pae_eu_kg*adult_eq,
           co2eq_kg = co2eq_pae_kg*adult_eq,
           co2eq_dom_kg = co2eq_pae_dom_kg*adult_eq,
           co2eq_eu_kg = co2eq_pae_eu_kg*adult_eq,
           energy_use_tj = energy_use_pae_tj*adult_eq,
           energy_use_dom_tj = energy_use_dom_pae_tj*adult_eq,
           energy_use_eu_tj = energy_use_eu_pae_tj*adult_eq
           ) %>%
    left_join(sector_mapping, by="sector_id")
  
  list("df_mapped_data" = df_mapped_data, "df_ntile_boundaries" = df_eu_ntiles$df_q_boundaries)
  
}

# filter only countries with complete info for years 2005, 2010, 2015
mrio_results_with_adult_eq = mrio_results_with_adult_eq_all %>%
  filter(iso2 %in% complete_countries)

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for complete countries
summary_country_fd = mrio_results_with_adult_eq %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000)/(ae_quintile))

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for all countries
summary_country_fd_all = mrio_results_with_adult_eq_all %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000)/(ae_quintile))

df_mapped_result_2005 = map_mrio_results_to_eu_ntiles(2005, target_eu_ntiles)
df_mapped_result_2005_data = df_mapped_result_2005$df_mapped_data
df_mapped_result_2005_ntiles = df_mapped_result_2005$df_ntile_boundaries

df_mapped_result_2010 = map_mrio_results_to_eu_ntiles(2010, target_eu_ntiles)
df_mapped_result_2010_data = df_mapped_result_2010$df_mapped_data
df_mapped_result_2010_ntiles = df_mapped_result_2010$df_ntile_boundaries

df_mapped_result_data = df_mapped_result_2005_data %>%
  bind_rows(df_mapped_result_2010_data) 

write_csv(df_mapped_result_data, 
          here(paste0("analysis/data/derived/si/mrio_results_eu_ntile_mapped_n_", target_eu_ntiles, "_pxp.csv")))


##### SI results, alternative method, EXOIBASE industry-by-industry version

# 1) load income-stratified-footprints formatted results file
dat_results_raw = read_rds(here("analysis", "preprocessing", "income-stratified-footprints",
                                "results_formatted_method2_ixi.rds")) %>%
  ungroup() %>%
  mutate(year= strtoi(year)) %>%
  rename(iso2 = geo)

# get iso3 country codes to join with hh data
country_codes = ISOcodes::ISO_3166_1 %>%
  select(iso2 = Alpha_2, iso3 = Alpha_3) %>%
  # resolve inconsistency between EUROSTAT and ISO for Greece and UK/Great Britain
  mutate(iso2 = if_else(iso2=="GR", "EL", iso2)) %>%
  mutate(iso2 = if_else(iso2=="GB", "UK", iso2))

# read in total private households data from EUROSTAT and Norway, merge, and write .csv 
## set 4 digits per value
options(digits=4)

## EUROSTAT total private households
total_private_households_Eurostat = read.csv(here("/analysis/preprocessing/income-stratified-footprints/lfst_hhnhtych_1_Data.csv")) %>%
  filter(!(GEO %in% c("European Union - 27 countries (from 2020)",
                    "Euro area - 19 countries  (from 2015)",
                    "European Union - 28 countries (2013-2020)",
                    "European Union - 15 countries (1995-2004)"))) %>%
  mutate(geo = dplyr::recode(GEO,
                             "Belgium" = "BE",
                             "Bulgaria" = "BG",
                             "Czechia" = "CZ",
                             "Denmark" = "DK",
                             "Germany (until 1990 former territory of the FRG)" = "DE",
                             "Estonia" = "EE",
                             "Ireland" = "IE",
                             "Greece" = "EL",
                             "Spain" = "ES",
                             "France" = "FR",
                             "Croatia" = "HR",
                             "Italy" = "IT",
                             "Cyprus" = "CY",
                             "Latvia" = "LV",
                             "Lithuania" = "LT",
                             "Luxembourg" = "LU",
                             "Hungary" = "HU",
                             "Malta" = "MT",
                             "Netherlands" = "NL",
                             "Austria" = "AT",
                             "Poland" = "PL",
                             "Portugal" = "PT",
                             "Romania" = "RO",
                             "Slovenia" = "SI",
                             "Slovakia" = "SK",
                             "Finland" = "FI",
                             "Sweden" = "SE",
                             "United Kingdom" = "UK",
                             "Montenegro" = "ME",
                             "North Macedonia" = "MK",
                             "Serbia" = "RS",
                             "Turkey" = "TR")) %>%
  select(TIME,geo,Value) %>%
  rename(year = TIME, total_private_households = Value) %>%
  mutate(total_private_households = as.character(total_private_households),
         total_private_households = parse_number(total_private_households),
         total_private_households = as.numeric(total_private_households),
         total_private_households = total_private_households*1000)

## Norway total private households
total_private_households_Norway = read.csv(here("/analysis/preprocessing/income-stratified-footprints/Privathusholdninger.csv")) %>%
  gather(year, total_private_households, Private.households.2005:Private.households.2019) %>%
  mutate(geo = dplyr::recode(region,
                             "0 The whole country" = "NO"),
         year = dplyr::recode(year,
                              "Private.households.2005" = 2005,
                              "Private.households.2006" = 2006,
                              "Private.households.2007" = 2007,
                              "Private.households.2008" = 2008,
                              "Private.households.2009" = 2009,
                              "Private.households.2010" = 2010,
                              "Private.households.2011" = 2011,
                              "Private.households.2012" = 2012,
                              "Private.households.2013" = 2013,
                              "Private.households.2014" = 2014,
                              "Private.households.2015" = 2015,
                              "Private.households.2016" = 2016,
                              "Private.households.2017" = 2017,
                              "Private.households.2018" = 2018,
                              "Private.households.2019" = 2019)) %>%
  select(year,geo,total_private_households)

## merge EUROSTAT and Norway total private households data
total_private_households = rbind(total_private_households_Eurostat,
                                 total_private_households_Norway) %>%
  mutate(geo = as.character(geo),
         year = as.numeric(year),
         total_private_households = as.numeric(total_private_households))

## write .csv file with all total private households data
write_csv(total_private_households, here("/analysis/preprocessing/income-stratified-footprints/total_private_households.csv"))
# the original 'total_private_households_original.csv' is in 'income-stratified-footprints-in-europe/data' - if something goes wrong
# with the main paper results (pulling from the mrio results file written later) then the problem is here, when I added 
# the as.character mutation in the total_private_households_Eurostat

## return to 2 digits per value
options(digits=2)

# 2) load EUROSTAT household data
hh_data = read_csv(here("analysis", "preprocessing", "income-stratified-footprints",
                        "total_private_households.csv")) %>%
  mutate(imputed = if_else(is.na(total_private_households), TRUE, FALSE)) %>%
  rename(iso2 = geo) %>%
  group_by(iso2) %>%
  # impute households with next available neighbour
  mutate(hh = na_locf(total_private_households)) %>%
  left_join(country_codes, by="iso2") %>%
  select(-total_private_households)

#3) EUROSTAT mean expenditures per household income quintile per household and per adult equivalent
df_expenditure_long = read_csv(here("analysis", "preprocessing", "income-stratified-footprints",
                                    "mean_expenditure_by_quintile_long.csv"),
                               na = ":") %>%
  #filter(year >=2010, geo != "IT") %>%
  #filter(year >=2005, geo != "IT") %>%
  #decide here
  filter(year >=2005) %>%
  mutate(imputed = if_else(is.na(mean_expenditure), TRUE, FALSE)) %>%
  group_by(geo,unit,quintile) %>%
  mutate(value = na_locf(mean_expenditure)) %>%
  select(-mean_expenditure) %>% 
  ungroup()

## calculate adult equivalents per household
df_adult_e_p_hh = df_expenditure_long %>%
  rename(iso2 = geo) %>%
  pivot_wider(id_cols = c(iso2, year, quintile, imputed), 
              names_from = unit, 
              values_from = value) %>%
  clean_names() %>%
  mutate(adult_e_p_hh = pps_hh/pps_ae) %>%
  left_join(country_codes, by="iso2") %>%
  mutate(iso3 = if_else(iso2 == "XK", "XKX", iso3),
         quint = parse_number(quintile))

## add quintile population data
mrio_results_with_adult_eq_all = dat_results_raw %>%
  filter(year %in% c(2005, 2010, 2015)) %>%
  left_join(hh_data, by=c("iso2", "year")) %>%
  mutate(hh_quintile = hh/5) %>% # population per country quinitle
  select(-hh) %>%
  rename(hh_imputed = imputed) %>%
  left_join(df_adult_e_p_hh %>%
              select(iso2, year, quint, imputed_ae = imputed, adult_e_p_hh), 
            by=c("iso2", "year", "quint")) %>%
  mutate(ae_quintile = hh_quintile * adult_e_p_hh) %>%
  select(-c(hh_quintile, adult_e_p_hh))

## for the European expenditure deciles we use only countries with data in 2005, 2010 and 2015. This excludes Luxembourg and Italy. 
complete_countries = mrio_results_with_adult_eq_all %>%
  group_by(year, iso2) %>%
  summarise(co2_kg = sum(co2_kg)) %>%
  ungroup() %>%
  filter(co2_kg>0) %>%
  select(iso2, year, co2_kg) %>%
  pivot_wider(id_cols = c(iso2), names_from = year, values_from = co2_kg) %>%
  drop_na() %>%
  select(iso2) %>%
  pull()

df_adult_e_p_hh %>%
  filter(iso2 %in% complete_countries, year<=2015, year>=2005) %>%
  mutate(quint = parse_number(quintile))

# calculate European expenditure deciles based on loaded income-stratified-footprints result file and adult equivalents.
# returns country quintiles mapped to European ntile rank and European ntile boundaries
# helper function called by function below
calculate_eu_ntiles <- function(pyear, pquantile_count=10) {
  
  country_data_annual_sorted = summary_country_fd %>%
    ungroup() %>%
    filter(year==pyear) %>%
    arrange(fd_pae_e) %>%
    mutate(idx = 1:n(),
           eu_q_rank = 0) # later to be filled with euro quintile rank
  
  # total European adult equivalents (of included countries) in year
  total_ae_in_year = sum(country_data_annual_sorted$ae_quintile)
  
  # quantile target ae population
  eu_decile_adult_eq = total_ae_in_year/pquantile_count
  
  # country quintiles must be split to allocate ae population according to European quantile target ae population
  # filtering by condition that can't be fulfilled is a lazy way to create an empty dataframe
  # of the same structure as country_data_annual_sorted
  additional_rows = country_data_annual_sorted %>%
    filter(year==1)
  
  # store quantile split values
  eu_quantile_boundaries = data.frame(euro_q_rank = 1:pquantile_count, p = 0)

  ## loops through the ordered dataset, assigns European quantile rank
  ## and splits quintiles where necessary
  eu_ae_current = 0
  euro_q_rank_current = 1
  for (row_idx in 1:nrow(country_data_annual_sorted)) {
    row = country_data_annual_sorted[row_idx,]
    if (row["ae_quintile"] + eu_ae_current <= eu_decile_adult_eq) {
      eu_ae_current = eu_ae_current + row["ae_quintile"]
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
    } else {
      ae_diff = eu_decile_adult_eq - eu_ae_current
      ## write rest of this European decile (split country quintile)
      new_row = country_data_annual_sorted[row_idx, ]
      new_row[1, "eu_q_rank"] = euro_q_rank_current
      new_row[1, "ae_quintile"] = ae_diff
      ## record European quantile boundary
      eu_quantile_boundaries[eu_quantile_boundaries$euro_q_rank==euro_q_rank_current, "p"] =
        country_data_annual_sorted[row_idx, "fd_pae_e"]
      ## put first part of population in overflow dataframe
      additional_rows = additional_rows %>%
        bind_rows(new_row)
      ## classify rest of country quintile population to next European quantile
      country_data_annual_sorted[row_idx, "ae_quintile"] = 
        country_data_annual_sorted[row_idx, "ae_quintile"] - (ae_diff+0.0001)
      euro_q_rank_current = euro_q_rank_current + 1
      country_data_annual_sorted[row_idx, "eu_q_rank"] = euro_q_rank_current
      eu_ae_current = country_data_annual_sorted[row_idx, "ae_quintile"]
      
    }
  }
  
  country_data_eu_quantiles = country_data_annual_sorted %>%
  bind_rows(additional_rows) %>%
  arrange(fd_pae_e, eu_q_rank) %>%
  mutate(idx = 1:n())

  # add zeroth and nth quantile (min and max)
  eu_quantile_boundaries[pquantile_count, "p"] = max(country_data_eu_quantiles$fd_pae_e)
  
  list("df_q_data" = country_data_eu_quantiles, "df_q_boundaries" = eu_quantile_boundaries)
}

# maps income-stratified-footprint results to European ntile ranks, returns mapping and ntile European boundaries
map_mrio_results_to_eu_ntiles <- function(pyear, ptarget_ntiles) {
  
  df_eu_ntiles = calculate_eu_ntiles(pyear, pquantile_count = ptarget_ntiles)
  df_eu_ntiles_data = df_eu_ntiles$df_q_data
  
  sector_mapping = mrio_results_with_adult_eq %>%
    group_by(coicop) %>%#
    summarise(coicop = first(coicop)) %>%
    ungroup()
  
  df_mapped_data = mrio_results_with_adult_eq %>%
    select(year, 
           iso2, 
           quint, 
           coicop, 
           fd_me, 
           co2_kg, 
           co2_domestic_kg, 
           co2_europe_kg,
           co2eq_kg, 
           co2eq_domestic_kg, 
           co2eq_europe_kg,
           energy_use_tj, 
           energy_use_domestic_tj,
           energy_use_europe_tj,
           ae_quintile) %>%
    filter(year==pyear) %>%
    # calculate per adult equivalent values in quintiles
    mutate(fd_pae_e = fd_me*1000000/ae_quintile,
           co2_pae_kg = co2_kg/ae_quintile,
           co2_pae_dom_kg = co2_domestic_kg/ae_quintile,
           co2_pae_eu_kg = co2_europe_kg/ae_quintile,
           co2eq_pae_kg = co2eq_kg/ae_quintile,
           co2eq_pae_dom_kg = co2eq_domestic_kg/ae_quintile,
           co2eq_pae_eu_kg = co2eq_europe_kg/ae_quintile,
           energy_use_pae_tj = energy_use_tj/ae_quintile,
           energy_use_dom_pae_tj = energy_use_domestic_tj/ae_quintile,
           energy_use_eu_pae_tj = energy_use_europe_tj/ae_quintile) %>%
    # remove totals
    select(-c(fd_me, 
              co2_kg,
              co2_domestic_kg,
              co2_europe_kg,
              co2eq_kg,
              co2eq_domestic_kg,
              co2eq_europe_kg,
              energy_use_tj,
              energy_use_domestic_tj,
              energy_use_europe_tj,
              year, ae_quintile)) %>%
    full_join(df_eu_ntiles_data %>%
                rename(fd_pae_e_quint_tmp = fd_pae_e), by=c("iso2", "quint")) %>%
    rename(adult_eq = ae_quintile) %>% # country quintile and their split fraction population
    # recalculate totals
    mutate(fd_me = fd_pae_e*adult_eq/1000000,
           co2_kg = co2_pae_kg*adult_eq,
           co2_dom_kg = co2_pae_dom_kg*adult_eq,
           co2_eu_kg = co2_pae_eu_kg*adult_eq,
           co2eq_kg = co2eq_pae_kg*adult_eq,
           co2eq_dom_kg = co2eq_pae_dom_kg*adult_eq,
           co2eq_eu_kg = co2eq_pae_eu_kg*adult_eq,
           energy_use_tj = energy_use_pae_tj*adult_eq,
           energy_use_dom_tj = energy_use_dom_pae_tj*adult_eq,
           energy_use_eu_tj = energy_use_eu_pae_tj*adult_eq
           ) #%>%
    #left_join(sector_mapping, by="sector_id") # comment out joining sectors (only working with coicop categories in method2)
  
  list("df_mapped_data" = df_mapped_data, "df_ntile_boundaries" = df_eu_ntiles$df_q_boundaries)
  
}

# filter only countries with complete info for years 2005, 2010, 2015
mrio_results_with_adult_eq = mrio_results_with_adult_eq_all %>%
  filter(iso2 %in% complete_countries)

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for complete countries
summary_country_fd = mrio_results_with_adult_eq %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000, na.rm = T)/(ae_quintile))

# summarize final demand per adult equivalent per quintile across all sectors as basis for European deciles for all countries
summary_country_fd_all = mrio_results_with_adult_eq_all %>%
  group_by(iso2, year, quint) %>%
  summarise(ae_quintile = first(ae_quintile),
            fd_pae_e = sum(fd_me*1000000, na.rm = T)/(ae_quintile))

df_mapped_result_2005 = map_mrio_results_to_eu_ntiles(2005, target_eu_ntiles)
df_mapped_result_2005_data = df_mapped_result_2005$df_mapped_data
df_mapped_result_2005_ntiles = df_mapped_result_2005$df_ntile_boundaries

df_mapped_result_2010 = map_mrio_results_to_eu_ntiles(2010, target_eu_ntiles)
df_mapped_result_2010_data = df_mapped_result_2010$df_mapped_data
df_mapped_result_2010_ntiles = df_mapped_result_2010$df_ntile_boundaries

df_mapped_result_2015 = map_mrio_results_to_eu_ntiles(2015, target_eu_ntiles)
df_mapped_result_2015_data = df_mapped_result_2015$df_mapped_data
df_mapped_result_2015_ntiles = df_mapped_result_2015$df_ntile_boundaries

df_mapped_result_data = df_mapped_result_2005_data %>%
  bind_rows(df_mapped_result_2010_data) %>%
  bind_rows(df_mapped_result_2015_data)

write_csv(df_mapped_result_data, 
          here(paste0("analysis/data/derived/si/mrio_results_eu_ntile_mapped_n_", target_eu_ntiles, "_method2_ixi.csv")))

```








