-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3_SummarizeResults_HighLevel.R
348 lines (270 loc) · 9.76 KB
/
3_SummarizeResults_HighLevel.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Summarize results from indicators analysis
# Contact: Cameryn Brock
# Last updated: 3/20/2022
library(tidyverse)
year <- "2022"
#####
##### Read in data
#####
# read in results tables as defined in script B
# irrecoverable carbon broken up by ecosystem
sites_ic <- read_csv(
paste0("results/FY",
str_sub(year, start = 3),
"_ImpactIndicators_IrrecoverableCarbon_Sites.csv")) %>%
dplyr::select(!tonnes_ha_ic) # can't sum
overlaps_ic <- readRDS(
paste0("results/FY",
str_sub(year, start = 3),
"_ImpactIndicators_IrrecoverableCarbon_Overlaps.rds")) %>%
dplyr::select(!tonnes_ha_ic) # can't sum
# other indicators = population, woody & soil carbon, carbon sequestration potential
sites_other <- read_csv(
paste0("results/FY", str_sub(year, start = 3), "_ImpactIndicators_Other_Sites.csv"))
overlaps_other <- readRDS(
paste0("results/FY", str_sub(year, start = 3), "_ImpactIndicators_Other_Overlaps.rds"))
### avoided emissions - by site, already prepped by Alex
ae <- readRDS("data/avoided_emissions/ae_by_site_by_year.rds") %>%
dplyr::select(site_id, emissions_avoided_mgco2e_2020) %>%
rename(ci_id = site_id)
# note fields are equal within overlapping polygons for country and ci_divsio, but not always for ci_divis_1 or sls s
#####
##### Tidy and define functions
#####
# Irrecoverable carbon
# create csv for aggregating all sites
# multiply value by (# of overlaps - 1) to account for triple/quadruple/etc counting
# and make values negative to easily sum
# note we only use this for variables we know don't vary within overlapping sites - for example, all overlapping sites are within the same country, but they are not all within the same sls
overlaps_ic_corrected <- overlaps_ic %>%
mutate(across(
.cols = contains("_ic"),
~ .x * -(n_overlaps - 1))) %>%
dplyr::select(!n_overlaps)
# define function for summarizing
ic_summarize <- function(df){
df %>%
summarize(across(
.cols = contains("_ic"),
sum, na.rm = TRUE),
.groups = "keep")
}
overlaps_other_corrected <- overlaps_other %>%
mutate(across(
.cols = c(area_ha, rest_area, population,
tstor_woody, tstor_soil, tstor_total,
carbon_seq_potl),
~ .x * -(n_overlaps - 1))) %>%
dplyr::select(!n_overlaps)
other_summarize <- function(df){
if(any(str_detect(colnames(df), 'emissions_avoided_mgco2e'))) {
df %>%
summarize(across(
.cols = c(area_ha, rest_area, population,
tstor_woody, tstor_soil, tstor_total,
carbon_seq_potl, emissions_avoided_mgco2e),
sum, na.rm = TRUE),
.groups = "keep")
} else {
df %>%
summarize(across(
.cols = c(area_ha, rest_area, population,
tstor_woody, tstor_soil, tstor_total,
carbon_seq_potl),
sum, na.rm = TRUE),
.groups = "keep")
}
}
#####
##### Country-level
#####
# irrecoverable carbon
country_sites_ic <- sites_ic %>%
group_by(country) %>%
ic_summarize() %>%
ungroup()
country_overlaps_ic <- overlaps_ic_corrected %>%
rowwise() %>%
mutate(country = unique(country)) %>% # all countries within polygons are the same
ungroup() %>%
group_by(country) %>%
ic_summarize() %>%
ungroup()
country_corrected_ic <- country_sites_ic %>%
bind_rows(country_overlaps_ic) %>%
group_by(country) %>%
ic_summarize() %>% # can sum bc overlap values are negative
ungroup()
write_csv(country_corrected_ic,
paste0("results/summaries_", year, "/ImpactIndicators_CountrySummary_IC.csv"))
# other indicators
country_sites_other <- sites_other %>%
group_by(country) %>%
other_summarize() %>%
ungroup()
country_overlaps_other_wo_ae <- overlaps_other_corrected %>%
rowwise() %>%
mutate(country = unique(country)) %>% # all countries within polygons are the same
ungroup() %>%
group_by(country) %>%
other_summarize() %>%
ungroup()
# bring in emissions avoided to overlap
# break up ae by % area
total_aes <- country_sites_other %>%
dplyr::select(country, area_ha, emissions_avoided_mgco2e) %>%
rename(total_area = area_ha)
ae_ref <- country_overlaps_other_wo_ae %>%
dplyr::select(country, area_ha) %>%
left_join(total_aes, by = "country") %>%
mutate(pct_area = area_ha/total_area) %>%
mutate(emissions_avoided_mgco2e = emissions_avoided_mgco2e * pct_area) %>%
dplyr::select(country, emissions_avoided_mgco2e)
country_overlaps_other <- country_overlaps_other_wo_ae %>%
left_join(ae_ref, by = 'country')
country_corrected_other <- country_sites_other %>%
bind_rows(country_overlaps_other) %>%
group_by(country) %>%
other_summarize() %>% # can sum bc overlap values are negative
ungroup()
write_csv(country_corrected_other,
paste0("results/summaries_", year, "/ImpactIndicators_CountrySummary_OtherIndicators.csv"))
#####
##### Division-level
#####
# irrecoverable carbon
division_sites_ic <- sites_ic %>%
group_by(ci_divisio, ecosystem) %>%
ic_summarize() %>%
ungroup()
division_overlaps_ic <- overlaps_ic_corrected %>%
rowwise() %>%
mutate(ci_divisio = unique(ci_divisio)) %>%
ungroup() %>%
group_by(ci_divisio, ecosystem) %>%
ic_summarize() %>%
ungroup()
division_corrected_ic <- division_sites_ic %>%
bind_rows(division_overlaps_ic) %>%
group_by(ci_divisio, ecosystem) %>%
ic_summarize() %>%
ungroup()
write_csv(division_corrected_ic,
paste0("results/summaries_", year, "/ImpactIndicators_DivisionSummary_IC.csv"))
# other indicators
division_sites_other <- sites_other %>%
group_by(ci_divisio) %>%
other_summarize() %>%
ungroup()
division_overlaps_other_wo_ae <- overlaps_other_corrected %>%
rowwise() %>%
mutate(ci_divisio = unique(ci_divisio)) %>%
ungroup() %>%
group_by(ci_divisio) %>%
other_summarize() %>%
ungroup()
# bring in emissions avoided to overlap
# break up ae by % area
total_aes <- division_sites_other %>%
dplyr::select(ci_divisio, area_ha, emissions_avoided_mgco2e) %>%
rename(total_area = area_ha)
ae_ref <- division_overlaps_other_wo_ae %>%
dplyr::select(ci_divisio, area_ha) %>%
left_join(total_aes, by = "ci_divisio") %>%
mutate(pct_area = area_ha/total_area) %>%
mutate(emissions_avoided_mgco2e = emissions_avoided_mgco2e * pct_area) %>%
dplyr::select(ci_divisio, emissions_avoided_mgco2e)
division_overlaps_other <- division_overlaps_other_wo_ae %>%
left_join(ae_ref, by = 'ci_divisio')
division_corrected_other <- division_sites_other %>%
bind_rows(division_overlaps_other) %>%
group_by(ci_divisio) %>%
other_summarize() %>%
ungroup()
write_csv(division_corrected_other,
paste0("results/summaries_", year, "/ImpactIndicators_DivisionSummary_OtherIndicators.csv"))
#####
##### SlS-level
#####
# sls is more complicated because there are overlapping sites with different sls values
# irrecoverable carbon
sls_sites_ic <- sites_ic %>%
group_by(ci_sls_2, ecosystem) %>%
ic_summarize() %>%
ungroup()
sls_overlaps_ic <- overlaps_ic %>%
rowwise() %>%
# get a list of duplicated values
mutate(duplicate_sls = list(ci_sls_2[duplicated(ci_sls_2)])) %>%
ungroup() %>%
# remove those without any duplicates
# (i.e. birds head & west papua have some overlaps that aren't double counting when we group by sls)
filter(!duplicate_sls == "character(0)") %>%
# multiply values of interest by the number of times its duplicated & make negative
rowwise() %>%
mutate(across(
.cols = contains("_ic"),
~ .x * -length(duplicate_sls))) %>%
mutate(ci_sls_2 = unique(duplicate_sls)) %>%
# using duplicated[] already accounts for the -1
ungroup() %>%
group_by(ci_sls_2, ecosystem) %>%
ic_summarize() %>%
ungroup()
sls_corrected_ic <- sls_sites_ic %>%
bind_rows(sls_overlaps_ic) %>%
group_by(ci_sls_2, ecosystem) %>%
ic_summarize() %>%
ungroup()
write_csv(sls_corrected_ic,
paste0("results/summaries_", year, "/ImpactIndicators_SLSSummary_IC.csv"))
# other indicators
sls_sites_other <- sites_other %>%
group_by(ci_sls_2) %>%
other_summarize() %>%
ungroup()
sls_overlaps_other_wo_ae <- overlaps_other %>%
rowwise() %>%
mutate(duplicate_sls = list(ci_sls_2[duplicated(ci_sls_2)])) %>%
ungroup() %>%
filter(!duplicate_sls == "character(0)") %>%
rowwise() %>%
mutate(across(
.cols = c(area_ha, rest_area, population,
tstor_woody, tstor_soil, tstor_total,
carbon_seq_potl),
~ .x * -length(duplicate_sls))) %>%
mutate(ci_sls_2 = unique(duplicate_sls)) %>%
ungroup() %>%
group_by(ci_sls_2) %>%
other_summarize() %>%
ungroup()
# bring in emissions avoided to overlap
# break up ae by % area
total_aes <- sls_sites_other %>%
dplyr::select(ci_sls_2, area_ha, emissions_avoided_mgco2e) %>%
rename(total_area = area_ha)
ae_ref <- sls_overlaps_other_wo_ae %>%
dplyr::select(ci_sls_2, area_ha) %>%
left_join(total_aes, by = "ci_sls_2") %>%
mutate(pct_area = area_ha/total_area) %>%
mutate(emissions_avoided_mgco2e = emissions_avoided_mgco2e * pct_area) %>%
dplyr::select(ci_sls_2, emissions_avoided_mgco2e)
sls_overlaps_other <- sls_overlaps_other_wo_ae %>%
left_join(ae_ref, by = 'ci_sls_2')
sls_corrected_other <- sls_sites_other %>%
bind_rows(sls_overlaps_other) %>%
group_by(ci_sls_2) %>%
other_summarize() %>%
ungroup()
write_csv(sls_corrected_other,
paste0("results/summaries_", year, "/ImpactIndicators_SLSSummary_OtherIndicators.csv"))
#####
##### Site-level
#####
# overlaps don't occur at site level
# due to that, note can't sum for total (or will have double counting)
write_csv(sites_ic,
paste0("results/summaries_", year, "/ImpactIndicators_SiteSummary_IC.csv"))
write_csv(sites_other,
paste0("results/summaries_", year, "/ImpactIndicators_SiteSummary_OtherIndicators.csv"))