generated from IMMM-SFA/metarepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmp_2016_2018.R
215 lines (191 loc) · 8.21 KB
/
cmp_2016_2018.R
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
library("dplyr")
grid.level = "coarse"
if (grid.level == "coarse") {
grid.suf = ""
} else if (grid.level == "finer"){
grid.suf = "_finer"
} else if (grid.level == "tract") {
grid.suf = "_tract"
}
if (grid.level != "finer") {
data.2016 <- readr::read_csv(sprintf("intermediate_data/hourly_heat_energy/annual_2016%s.csv",
grid.suf))
data.2018 <- readr::read_csv(sprintf("output_data/hourly_heat_energy/annual_2018%s.csv",
grid.suf))
} else {
data.2016 <- readr::read_csv(sprintf("intermediate_data/hourly_heat_energy/annual_2016_07%s.csv",
grid.suf))
data.2018 <- readr::read_csv(sprintf("output_data/hourly_heat_energy/annual_2018%s_07.csv",
grid.suf))
}
annual.total.2016 <- data.2016 %>%
dplyr::group_by(geoid) %>%
dplyr::summarise_if(is.numeric, sum) %>%
dplyr::ungroup()
annual.total.2018 <- data.2018 %>%
dplyr::group_by(geoid) %>%
dplyr::summarise_if(is.numeric, sum) %>%
dplyr::ungroup()
annual.total.2016 %>%
dplyr::mutate(year = 2016) %>%
dplyr::bind_rows(annual.total.2018 %>%
dplyr::mutate(year = 2018)) %>%
dplyr::select(year, geoid, starts_with("energy")) %>%
tidyr::gather(variable, value, energy.elec:energy.overall) %>%
dplyr::mutate(value = value * 1e-12) %>%
dplyr::mutate(year = factor(year)) %>%
dplyr::mutate_at(vars(variable), recode,
"energy.elec" = "electricity",
"energy.gas" = "gas",
"energy.overall" = "total (electricity + gas)") %>%
ggplot2::ggplot(ggplot2::aes(x = year, y = value, group = year)) +
ggplot2::geom_boxplot() +
ggplot2::facet_wrap(.~variable) +
ggplot2::ylab("TJ") +
ggplot2::theme(text = ggplot2::element_text(size = 18))
ggplot2::ggsave(sprintf("figures/cmp_2016_2018_energy_%s.png", grid.level), width=8, height=6)
annual.total.2016 %>%
dplyr::mutate(year = 2016) %>%
dplyr::bind_rows(annual.total.2018 %>%
dplyr::mutate(year = 2018)) %>%
dplyr::select(year, geoid, starts_with("energy")) %>%
tidyr::gather(variable, value, energy.elec:energy.overall) %>%
ggplot2::ggplot(ggplot2::aes(x = value, group = year, color = year)) +
ggplot2::geom_density() +
ggplot2::facet_wrap(.~variable, ncol = 1) +
ggplot2::theme()
monthly.total <- data.2016 %>%
dplyr::mutate(year = 2016) %>%
dplyr::bind_rows(data.2018 %>%
dplyr::mutate(year = 2018)) %>%
tidyr::separate(timestamp, into = c("month", "suffix"), sep = "/") %>%
dplyr::group_by(geoid, year, month) %>%
dplyr::summarise_if(is.numeric, sum) %>%
dplyr::ungroup() %>%
{.}
weather <- readr::read_csv("intermediate_data/weather_2018.csv") %>%
dplyr::mutate(year = 2018) %>%
dplyr::bind_rows(readr::read_csv("intermediate_data/weather_2016.csv") %>%
dplyr::mutate(year = 2016)) %>%
{.}
weather %>%
dplyr::group_by(year) %>%
dplyr::summarise_at(vars(DryBulb.C, RelHum.percent, `WindSpd.m/s`), mean) %>%
dplyr::ungroup() %>%
tidyr::gather(variable, value, -year) %>%
tidyr::spread(year, value) %>%
dplyr::mutate(percent.diff = (`2016` - `2018`) / `2018`) %>%
dplyr::arrange(desc(abs(percent.diff))) %>%
{.}
weather.summary %>%
tidyr::gather(variable, value, DryBulb.C:`WindSpd.m/s`) %>%
dplyr::mutate(year = factor(year),
month = factor(month)) %>%
ggplot2::ggplot(ggplot2::aes(x = month, y = value, color = year, group = interaction(variable, year), label = round(value, 1))) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggrepel::geom_text_repel() +
ggplot2::facet_wrap(.~variable, scales = "free_y", ncol = 1) +
ggplot2::theme_bw() +
ggplot2::theme()
ggplot2::ggsave("figures/cmp_2016_2018_weather.png", width = 9, height = 6)
to.plot <- monthly.total %>%
dplyr::mutate(year = factor(year)) %>%
dplyr::select(year, month, geoid, energy.overall, emission.overall) %>%
tidyr::gather(variable, value, energy.overall:emission.overall) %>%
{.}
to.plot %>%
ggplot2::ggplot(ggplot2::aes(x = month, y = value, fill = year)) +
ggplot2::geom_boxplot(outlier.size = 0.5) +
ggplot2::facet_wrap(.~variable, ncol = 1, scales = "free_y") +
ggplot2::theme_bw() +
ggplot2::theme()
ggplot2::ggsave("figures/cmp_2016_2018_energy_heat.png", width = 9, height = 6)
## get county total comparison
building.metadata <- readr::read_csv("output_data/building_metadata.csv")
prototype.area <- readr::read_csv("input_data/prototype_bldg_area.csv") %>%
dplyr::mutate(idf.kw = gsub(".idf", "", idf.name, fixed=TRUE)) %>%
dplyr::mutate(idf.kw = gsub(".", "_", idf.kw, fixed=TRUE)) %>%
{.}
get.agg.sim.energy <- function(year) {
df.sim <- readr::read_csv(sprintf("intermediate_data/monthly_total_result_%d.csv", year))
building.metadata %>%
tibble::as_tibble() %>%
dplyr::select(OBJECTID, building.area.m2, idf.kw, id.grid.coarse) %>%
dplyr::rename(epw.id = id.grid.coarse) %>%
dplyr::group_by(idf.kw, epw.id) %>%
dplyr::summarise(building.area.m2 = sum(building.area.m2)) %>%
dplyr::ungroup() %>%
dplyr::left_join(df.sim, by=c("idf.kw", "epw.id")) %>%
dplyr::left_join(prototype.area, by="idf.kw") %>%
tidyr::gather(variable, value, emission.exfiltration:energy.gas) %>%
dplyr::mutate(value = value / prototype.m2 * building.area.m2) %>%
tidyr::spread(variable, value) %>%
{.}
}
df.sim.agg <- get.agg.sim.energy(2016) %>%
dplyr::mutate(year = 2016) %>%
dplyr::bind_rows(get.agg.sim.energy(2018) %>%
dplyr::mutate(year = 2018) %>%
{.}) %>%
dplyr::select(-(idf.kw:building.area.m2), -idf.name, -prototype.m2) %>%
dplyr::group_by(year, month) %>%
dplyr::summarise_if(is.numeric, sum) %>%
dplyr::ungroup() %>%
{.}
to.plot.county <- df.sim.agg %>%
dplyr::select(year, month, emission.overall, energy.overall) %>%
dplyr::mutate(year = factor(year)) %>%
tidyr::gather(variable, value, emission.overall:energy.overall) %>%
## convert to TJ
dplyr::mutate(value = value * 1e-12) %>%
{.}
to.plot.county %>%
ggplot2::ggplot(ggplot2::aes(x = month, y = value, color = year, group = year)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::facet_wrap(.~variable, scales = "free_y", ncol = 1) +
ggplot2::theme_bw() +
ggplot2::ylab("TJ") +
ggplot2::theme()
ggplot2::ggsave("figures/cmp_2016_2018_county_total.png", width = 9, height = 6)
to.plot.county %>%
ggplot2::ggplot(ggplot2::aes(x = month, y = value, color = year, group = year)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::facet_wrap(.~variable, scales = "free_y", ncol = 1) +
ggplot2::theme_bw() +
ggplot2::ylab("TJ") +
ggplot2::theme()
ggplot2::ggsave("figures/cmp_2016_2018_county_total_no_surf.png", width = 9, height = 6)
## following produces summary numbers in the last part of the background section
options(pillar.sigfig = 7)
weather %>%
dplyr::group_by(year, month) %>%
dplyr::summarise_at(vars(DryBulb.C, RelHum.percent, `WindSpd.m/s`), mean) %>%
dplyr::ungroup() %>%
tidyr::gather(variable, value, -(year:month)) %>%
tidyr::spread(year, value) %>%
dplyr::mutate(percent.diff = (`2016` - `2018`) / `2018`) %>%
dplyr::filter(variable == "DryBulb.C") %>%
dplyr::arrange(desc(abs(percent.diff))) %>%
{.}
weather.summary <- weather %>%
dplyr::group_by(year, month) %>%
dplyr::summarise_at(vars(DryBulb.C, RelHum.percent, `WindSpd.m/s`), mean) %>%
dplyr::ungroup()
to.plot.county %>%
dplyr::filter(variable == "energy.overall") %>%
## dplyr::filter(variable == "emission.overall") %>%
tidyr::spread(year, value) %>%
dplyr::mutate(percent.diff = (`2016` - `2018`) / `2018`) %>%
dplyr::arrange(desc(abs(percent.diff))) %>%
{.}
to.plot.county %>%
dplyr::group_by(year, variable) %>%
dplyr::summarise(value = sum(value)) %>%
dplyr::ungroup() %>%
tidyr::spread(year, value) %>%
dplyr::mutate(percent.diff = (`2016` - `2018`) / `2018`) %>%
dplyr::arrange(desc(abs(percent.diff))) %>%
{.}