-
Notifications
You must be signed in to change notification settings - Fork 3
/
CalculateEmissionFactors.Rmd
119 lines (92 loc) · 4.31 KB
/
CalculateEmissionFactors.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
title: "Supply Chain GHG Emission Factors for Commodities by NAICS-6"
output:
md_document:
variant: gfm
params:
dollaryear: 2022
---
Generate supply chain GHG emission factors for NAICS-6 Commodities using 2022 GHG data in CO2-equivalent and by gas.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
source("R/Functions.R")
source("R/utils.R")
SEFversion <- get_latest_SEF_version()
install_useeior(SEFversion)
library(useeior)
library(kableExtra)
price_adjust_matrices <- c("B", "D", "M", "N", "M_margin", "N_margin")
MajorGHGs <- c("Carbon dioxide", "Methane", "Nitrous oxide")
dollaryear <- params$dollaryear
modelname <- "USEEIOv2.2.22-GHG"
checkModelIOYear(modelname)
```
```{r build-model, include=FALSE}
# Build model
modelconfigfile <- c(paste0("model-specs/", modelname, ".yml"))
model <- buildModel(modelname, configpaths=modelconfigfile)
# Calculate margin impacts for M and N matrices
model <- c(model, calculateMarginSectorImpacts(model))
# Adjust price of multipliers in model
price_adjusted_model <- list()
for (matrix in price_adjust_matrices) {
price_adjusted_model[[paste0(matrix, "_pro_", dollaryear)]] <- adjustResultMatrixPrice(matrix, dollaryear, purchaser_price = FALSE, model)
price_adjusted_model[[paste0(matrix, "_pur_", dollaryear)]] <- adjustResultMatrixPrice(matrix, dollaryear, purchaser_price = TRUE, model)
}
```
```{r}
prepareSEFtable <- function(table, factorform="Scientific Notation") {
table[, 1:3] <- table[, c("Sector", "Name", "Flowable")]
# Rename columns in table
useeiocodefield <- paste(model$specs$CommodityorIndustryType, "Code")
useeionamefield <- paste(model$specs$CommodityorIndustryType, "Name")
seffields <- c("Supply Chain Emission Factors without Margins","Margins of Supply Chain Emission Factors",
"Supply Chain Emission Factors with Margins")
colnames(table) <- c(useeiocodefield,
useeionamefield,
"GHG",
"Unit",
seffields)
# Remove records of all gov, Rest of World adjustment and electricity
#table <- table[!substr(table[, paste(params$modeltype, "Code")], 1, 1)%in%c("S", "G"), ]
table <- table[!substr(table[, useeiocodefield], 1, 1)%in%c("S", "G"), ]
table <- table[!substr(table[, useeiocodefield], 1, 4) == "2211", ]
# Drop 0s
table <- table[table[,seffields[1]] != 0,]
# Associate SEFs with 2017 NAICS-6
table <- mapto2017NAICS(table,model,useeiocodefield,useeionamefield,seffields)
if (factorform=="Decimal Form") {
# Round table values to 3 digits
table[, sapply(table, is.numeric)] <- round(table[, sapply(table, is.numeric)], 3)
} else {
table[, seffields] <- lapply(table[, seffields],
function(x) scales::scientific(x, 3))
}
return(table)
}
```
Prepare table with 1 factor for All GHGs per commodity in kg CO2e/USD
```{r}
SEF_df <- generateEmissionFactorTable(model, price_adjusted_model, margin = FALSE, characterized=TRUE)
MEF_df <- generateEmissionFactorTable(model, price_adjusted_model, margin = TRUE, characterized=TRUE)
# Combine SEF and MEF tables
df <- cbind(SEF_df, MEF_df[, "FlowAmount"], SEF_df[, "FlowAmount"] + MEF_df[, "FlowAmount"])
# Filter only to include records which used the GWP-AR4-100 indicator
table_characterized <- df[df$Flowable == "All GHGs", ]
table_characterized <- prepareSEFtable(table_characterized,factorform="Decimal Form")
write_to_filename <- paste0("SupplyChainGHGEmissionFactors_",SEFversion,"_NAICS_CO2e_USD", dollaryear, ".csv")
write.csv(table_characterized, write_to_filename, row.names = FALSE, fileEncoding = "UTF-8")
```
Saved `r write_to_filename`.
Prepare table with factors for each GHG in kg gas/USD
```{r create-table-to-CSV}
SEF_df <- generateEmissionFactorTable(model, price_adjusted_model, margin = FALSE, characterized=FALSE)
MEF_df <- generateEmissionFactorTable(model, price_adjusted_model, margin = TRUE, characterized=FALSE)
df_un <- cbind(SEF_df, MEF_df[, "FlowAmount"], SEF_df[, "FlowAmount"] + MEF_df[, "FlowAmount"])
df <- rbind(df,df_un)
table_by_gas <- df_un
table_by_gas <- prepareSEFtable(table_by_gas)
write_to_filename <- paste0("SupplyChainGHGEmissionFactors_",SEFversion,"_NAICS_byGHG_USD", dollaryear,".csv")
write.csv(table_by_gas, write_to_filename, row.names = FALSE, fileEncoding = "UTF-8")
```
Saved `r write_to_filename`.