forked from SFrav/Ruminant-feed-balance-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
363 lines (363 loc) · 22.3 KB
/
.Rhistory
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Load libraries
library(raster)
#library(stars)
library(sf)
library(dplyr)
library(tidyr)
library(exactextractr)
library(terra)
rasterOptions(tmpdir = "/home/s2255815/rspovertygroup/JameelObs/FeedBaskets/AUTemp") # Process needs > 40GB of temporary disk space
rasterOptions(maxmemory = 5e+20) # 6e+10 ~51GB allowed
rasterOptions(todisk = TRUE)
# root folder
root <- "."
root <- "/home/s2255815/rdrive/AU_IBAR/ruminant-feed-balance"
country <- "Nigeria"
# paths
spatialDir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData")
CropParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/CropParams")
LivestockParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/LivestockParams")
Results_dir <- paste0(root, "/src/3Balance-estimates/", country, "/Results")
Outputs_dir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData/outputs")
# Feed DM totals
# Loop through years
yearList <- c("2020", "2021", "2022", "2023")
for(year in yearList){
tFeedDM <- rast(list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern=year,full.names = T)) %>% app(., fun="sum", na.rm=TRUE)
writeRaster(tFeedDM, paste0(spatialDir, "/inputs/Feed_quantity/DMPTotal_", year, ".tif"), overwrite = TRUE)
}
tFeedDM <- stack(list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern="DMPTotal",full.names = T))
#extract by aggregation zones
aggregation_zones <- c("country", "region", "state")
for(aggregation_zone in aggregation_zones){
if(aggregation_zone == "country"){
zones <- st_read(paste0(spatialDir, "/inputs/aoi0.shp"))
zones <- bind_cols(select(zones, COUNTRY), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}else if(aggregation_zone == "region"){
zones <- st_read(paste0(root, "/src/3Balance-estimates/", country, "/SpatialData/intermediate/zones.gpkg"))
zones <- bind_cols(select(zones, ECOZone), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}else if(aggregation_zone == "state"){
zones <- st_read(paste0(spatialDir, "/inputs/aoi1.shp"))
zones <- bind_cols(select(zones, NAME_1), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}
st_geometry(zones) <- NULL
tsSum <- data.frame(zones)
colnames(tsSum) <- c("NAME_1", "feedDM_2020", "feedDM_2021", "feedDM_2022", "feedDM_2023")
write.csv(tsSum, paste0(Results_dir, "/totals_timeseries_DM_", aggregation_zone, ".csv"), row.names=FALSE)
cat("Completed extracting DM stats for: ", aggregation_zone, "\n")
}
107784445952/983443
107784445952/92376800
#Table 2
x <- select(tsSum[tsSum$year == 2023,], c(zone, totalME_mean, cropME_mean, grassME_mean, browseME_mean, afterME_mean))
gc()
rm(list=ls())
# avoid scientific notation
options(scipen = 999)
# Load libraries
library(raster)
#library(stars)
library(sf)
library(exactextractr)
library(rnaturalearth)
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
library(ggtext)
library(ggsci)
library(extrafont)
library(stars)
loadfonts(device = "all")
# root folder
root <- "."
root <- "/home/s2255815/rdrive/AU_IBAR/ruminant-feed-balance"
country <- "Nigeria"
# paths
spatialDir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData")
Results_dir <- paste0(root, "/src/3Balance-estimates/", country, "/Results")
plotsDir <- paste0(root, "/src/3Balance-estimates/", country, "/Plots"); dir.create(plotsDir, F, T)
##Set plot theme
themeLabs <- theme(text=element_text(family="serif"),
plot.title.position = "plot",
plot.title = element_textbox_simple(
size = 8,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
#fill = "cornsilk"
),
plot.subtitle = element_textbox_simple(
size = 8,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
#fill = "cornsilk"
),
plot.caption = element_textbox_simple(
size = 8,
width = NULL,
padding = margin(0, 0, 0, 0),
margin = margin(0, 0, 0, 0),
#linetype = 1,
#r = grid::unit(8, "pt"),
#fill = "azure1"
),
legend.title = element_textbox_simple(
size = 8,
width = NULL,
padding = margin(0, 0, 0, 0),
margin = margin(0, 0, 0, 0),
),
axis.text = element_blank(),
strip.background = element_blank(),
panel.grid.major = element_line(colour = "transparent") #remove map grid lines#,
)
######
##Feed timeseries breakdown
tsSum <- read.csv(paste0(Results_dir, "/disaggregated_timeseries.csv"), stringsAsFactors = F)
#@ There is an overestimation of post-harvest growth in the Sahel - due to misclassification of river sides (etc) as cropping - with short growing period and long dry season.
#Correction by reallocating post-harvest growth to grass
tmp <- tsSum[tsSum$zone == "(Agro)pastoral sahel",]
tmp$grassME_mean <- tmp$grassME_mean + (tmp$afterME_mean * 0.94)
tmp$grassME_min <- tmp$grassME_min + (tmp$afterME_min * 0.94)
tmp$grassME_max <- tmp$grassME_max + (tmp$afterME_max * 0.94)
tmp$afterME_mean <- (tmp$afterME_mean * 0.06)
tmp$afterME_min <- (tmp$afterME_min * 0.06)
tmp$afterME_max <- (tmp$afterME_max * 0.06)
tsSum <- rbind(tsSum[!tsSum$zone == "(Agro)pastoral sahel",], tmp)
tsSum_plot <- pivot_longer(dplyr::select(tsSum, zone, year, cropME_mean, grassME_mean, browseME_mean, afterME_mean), cols = -c(zone, year))
lower <- pivot_longer(dplyr::select(tsSum, zone, year, cropME_min, grassME_min, browseME_min, afterME_min), cols = -c(zone, year), values_to = "lower")
upper <- pivot_longer(dplyr::select(tsSum, zone, year, cropME_max, grassME_max, browseME_max, afterME_max), cols = -c(zone, year), values_to = "upper")
tsSum_plot <- cbind(tsSum_plot, select(lower, lower))
tsSum_plot <- cbind(tsSum_plot, select(upper, upper))
tsSum_plot$value <- tsSum_plot$value/1000000
tsSum_plot$lower <- tsSum_plot$lower/1000000
tsSum_plot$upper <- tsSum_plot$upper/1000000
tsSum_plot <- transform(tsSum_plot, zone=factor(zone,levels=c("(Agro)pastoral sahel", "Central mixed", "Forest mixed", "Northern mixed", "Southern mixed")))
tsSum_plot <- transform(tsSum_plot, name=factor(name,levels= c("grassME_mean", "cropME_mean", "browseME_mean", "afterME_mean")))
Fig1 <- ggplot(tsSum_plot[tsSum_plot$year == 2023,], aes(name, value, fill = name)) + geom_col(position = "identity") + geom_errorbar(aes(ymin = lower, ymax=upper)) + ylab("Energy available (TJ ME)") + xlab("Feed category") + scale_fill_manual(name = "", labels = c("Grass", "Crop residue", "Browse", "Other biomass"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "afterME_mean" = "#000000", "browseME_mean" = "#FF0000")) + theme_classic() + theme(text=element_text(family="serif", size = 12), axis.text.x=element_blank (), strip.background = element_blank()) + facet_wrap(~zone, ncol = 5) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
ggsave(paste0(plotsDir, "/NGAFig1_1000.tiff"), Fig1, device = "tiff", dpi = 1000, width=90 * (14/5), height=20 * (14/5), units = "mm")
tsSum <- tsSum %>% rowwise() %>% mutate(totalME_mean = sum(cropME_mean, grassME_mean, browseME_mean, afterME_mean))
tsSum <- tsSum %>% rowwise() %>% mutate(cropMEprop_mean = cropME_mean/ totalME_mean, grassMEprop_mean = grassME_mean / totalME_mean, browseMEprop_mean = browseME_mean / totalME_mean, afterMEprop_mean = afterME_mean / totalME_mean)
tsSum_plot2 <- pivot_longer(dplyr::select(tsSum, zone, year, cropMEprop_mean, grassMEprop_mean, browseMEprop_mean, afterMEprop_mean), cols = -c(zone, year))
tsSum_plot2 <- transform(tsSum_plot2, zone=factor(zone,levels=c("(Agro)pastoral sahel", "Central mixed", "Forest mixed", "Northern mixed", "Southern mixed")))
tsSum_plot2 <- transform(tsSum_plot2, name=factor(name,levels=c("grassMEprop_mean", "cropMEprop_mean", "browseMEprop_mean", "afterMEprop_mean")))
SI1 <- ggplot(tsSum_plot2, aes(year, value, colour = name, fill = name)) + geom_line() + scale_x_continuous(breaks=c(2020, 2021, 2022, 2023)) + scale_y_continuous(limits = c(0,1), breaks = c(0,0.2,0.4,0.6,0.8)) + ylab("Proportion of energy") + xlab("Year") + scale_colour_manual(name = "", labels = c("Grass", "Crop residue", "Browse", "Other biomass"), values = c("cropMEprop_mean" = "#F8CA02", "grassMEprop_mean" = "#008D1F", "afterMEprop_mean" = "#000000", "browseMEprop_mean" = "#FF0000")) + theme_classic() + theme(text=element_text(family="serif", size = 12), strip.background = element_blank(), panel.spacing = unit(6, "mm")) + facet_wrap(~zone, nrow = 1) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
ggsave(paste0(plotsDir, "/NGASI1.tiff"), SI1, device = "tiff", dpi = 300, width=90 * (14/5), height=20 * (14/5), units = "mm")
#Table 2
x <- select(tsSum[tsSum$year == 2023,], c(zone, totalME_mean, cropME_mean, grassME_mean, browseME_mean, afterME_mean))
x$eco <- c("sav", "for", "sav", "sav", "sah")
x <- group_by(x, eco)
x <- summarise_all(select(x, -zone), sum)
x <- x %>% rowwise() %>% mutate(cropMEprop_mean = cropME_mean/ totalME_mean, grassMEprop_mean = grassME_mean / totalME_mean, browseMEprop_mean = browseME_mean / totalME_mean, afterMEprop_mean = afterME_mean / totalME_mean)
x <- select(x, c(eco, totalME_mean, cropMEprop_mean, grassMEprop_mean, browseMEprop_mean, afterMEprop_mean))
View(x)
View(x)
#.libPaths(c(.libPaths()[2], .libPaths()[3]))
library(terra)
#library(stars)
library(sf)
library(exactextractr)
library(rnaturalearth)
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
library(ggtext)
library(ggsci)
library(extrafont)
library(stars)
library(tidyterra)
#loadfonts(device = "linux")
loadfonts()
terraOptions(tempdir = "/home/s2255815/scratch/AUTemp") # Process needs > 40GB of temporary disk space
terraOptions(memfrac=0.5)
terraOptions(todisk=TRUE)
# study area
region <- "BurkinaFaso"
# Paths, directories
root <- "/home/s2255815/rdrive/AU_IBAR/References/Example"
datadir <- paste0(root, "/src/3Balance-estimates/", region, "/SpatialData/inputs")
intdatadir <- paste0(root, "/src/3Balance-estimates/", region, "/SpatialData/intermediate")
spatialResultsDir <- paste0(root, "/src/3Balance-estimates/", region, "/SpatialData/outputs")
resultsDir <- paste0(root, "/src/3Balance-estimates/", region, "/Results")
qcChecksDir <- paste0(root, "/src/3Balance-estimates/", region, "/QC_checks")
plotsDir <- paste0(root, "/src/3Balance-estimates/", region, "/Plots"); dir.create(plotsDir, F, T)
##Set plot theme
themeLabs <- theme(text=element_text(family="serif"),
plot.title.position = "plot",
plot.title = element_textbox_simple(
size = 8,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
#fill = "cornsilk"
),
plot.subtitle = element_textbox_simple(
size = 8,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
#fill = "cornsilk"
),
plot.caption = element_textbox_simple(
size = 8,
width = NULL,
padding = margin(0, 0, 0, 0),
margin = margin(0, 0, 0, 0),
#linetype = 1,
#r = grid::unit(8, "pt"),
#fill = "azure1"
),
legend.title = element_textbox_simple(
size = 8,
width = NULL,
padding = margin(0, 0, 0, 0),
margin = margin(0, 0, 0, 0),
),
axis.text = element_blank(),
strip.background = element_blank(),
panel.grid.major = element_line(colour = "transparent") #remove map grid lines#,
)
#######
##Feed timeseries breakdown
#tsSum <- read.csv('LivestockParams/totals_timeseries.csv')
tsSum <- read.csv(paste0(resultsDir, "/disaggregated_timeseries.csv"), stringsAsFactors = F)
tsSum$region <- gsub("Sahel", "(Agro)pastoral Sahel", tsSum$region)
tsSum$region <- gsub("Central", "Central mixed", tsSum$region)
tsSum$region <- gsub("Mixed crop-livestock \\(north)", "North mixed", tsSum$region)
tsSum$region <- gsub("Mixed crop-livestock \\(south)", "South mixed", tsSum$region)
#@ There is an overestimation of post-harvest growth in the Sahel - due to misclassification of river sides (etc) as cropping - with short growing period and long dry season.
#Correction by reallocating post-harvest growth to grass
tmp <- tsSum[tsSum$region == "(Agro)pastoral Sahel",]
tmp$grassME_mean <- tmp$grassME_mean + (tmp$afterME_mean * 0.94)
tmp$grassME_min <- tmp$grassME_min + (tmp$afterME_min * 0.94)
tmp$grassME_max <- tmp$grassME_max + (tmp$afterME_max * 0.94)
tmp$afterME_mean <- (tmp$afterME_mean * 0.06)
tmp$afterME_min <- (tmp$afterME_min * 0.06)
tmp$afterME_max <- (tmp$afterME_max * 0.06)
tsSum <- rbind(tsSum[!tsSum$region == "(Agro)pastoral Sahel",], tmp)
#outCropMEmean <- tsSum$cropME_mean[tsSum$year == 2019] / tsSum$cropDM[tsSum$year == 2019]
#outCropMEmin <- tsSum$cropME_min[tsSum$year == 2019] / tsSum$cropDM[tsSum$year == 2019]
#outCropMEmax <- tsSum$cropME_max[tsSum$year == 2019] / tsSum$cropDM[tsSum$year == 2019]
#outME <- data.frame(region = unique(tsSum$region), outCropMEmean, outCropMEmin, outCropMEmax)
#geomMean <- sqrt(outME$outCropMEmin*outME$outCropMEmax)
#outME$sd <- (outME$outCropMEmean/geomMean)*(sqrt((outME$outCropMEmean^2)-(geomMean^2)))
#outME
tsSum_plot <- pivot_longer(dplyr::select(tsSum, region, year, cropME_mean, grassME_mean, browseME_mean, afterME_mean), cols = -c(region, year))
lower <- pivot_longer(dplyr::select(tsSum, region, year, cropME_min, grassME_min, browseME_min, afterME_min), cols = -c(region, year), values_to = "lower")
upper <- pivot_longer(dplyr::select(tsSum, region, year, cropME_max, grassME_max, browseME_max, afterME_max), cols = -c(region, year), values_to = "upper")
tsSum_plot <- cbind(tsSum_plot, select(lower, lower))
tsSum_plot <- cbind(tsSum_plot, select(upper, upper))
#tsSum <- pivot_longer(dplyr::select(tsSum, region, year, cropME_mean, grassME_mean, browseME_mean, afterME_mean, cropME_min, grassME_min, browseME_min, afterME_min), c(lvstReq, cropME_mean, grassME_mean, browseME_mean, afterME_mean))
#tsSum <- pivot_longer(dplyr::select(tsSum, region, year, cropME_mean, grassME_mean, browseME_mean), c(lvstReq, cropME_mean, grassME_mean, browseME_mean))
tsSum_plot$value <- tsSum_plot$value/1000000
tsSum_plot$lower <- tsSum_plot$lower/1000000
tsSum_plot$upper <- tsSum_plot$upper/1000000
# Correct region names, not sure why they are mispelled
tsSum_plot <- tsSum_plot %>%
mutate(region = dplyr::recode(region,
"(Agro)pastoral (Agro)pastoral Sahel" = "(Agro)pastoral Sahel",
"Central mixed mixed" = "Central mixed"))
#ggplot(tsSum_plot[tsSum_plot$year == 2019,], aes(name, value, fill = name)) + geom_col(position = "identity") + coord_flip() + geom_errorbar(aes(ymin = lower, ymax=upper)) + ylab("Energy available (TJ ME)") + xlab("Feed category") + scale_fill_manual(name = "", labels = c("Crop residue", "Grass", "Other biomass", "Browse"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "afterME_mean" = "#000000", "browseME_mean" = "#FF0000")) + theme_classic() + theme(text=element_text(family="serif"), axis.text.x=element_blank ()) + facet_wrap(~region) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
tsSum_plot <- transform(tsSum_plot, region=factor(region,levels=c("(Agro)pastoral Sahel", "North mixed","Central mixed", "South mixed", "Cropping")))
#ggplot(tsSum_plot[tsSum_plot$year == 2019,], aes(name, value, fill = name)) + geom_col(position = "identity") + coord_flip() + geom_errorbar(aes(ymin = lower, ymax=upper)) + ylab("Energy available (TJ ME)") + xlab("Feed category") + scale_fill_manual(name = "", labels = c("Crop residue", "Grass", "Other biomass", "Browse"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "afterME_mean" = "#000000", "browseME_mean" = "#FF0000")) + theme_classic() + theme(text=element_text(family="serif"), axis.text.y=element_blank ()) + facet_wrap(~region, ncol = 1) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
tsSum_plot <- transform(tsSum_plot, name=factor(name,levels= c("grassME_mean", "cropME_mean", "browseME_mean", "afterME_mean")))
Fig1 <- ggplot(tsSum_plot[tsSum_plot$year == 2019,], aes(name, value, fill = name)) +
geom_col(position = "identity") +
geom_errorbar(aes(ymin = lower, ymax=upper)) +
ylab("Energy available (TJ ME)") +
xlab("Feed category") +
scale_fill_manual(name = "", labels = c("Grass", "Crop residue", "Browse", "Other biomass"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "afterME_mean" = "#000000", "browseME_mean" = "#FF0000")) +
theme_classic() +
theme(text=element_text(family="serif", size = 14), axis.text.x=element_blank (), strip.background = element_blank()) +
facet_wrap(~region, ncol = 5) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
ggsave(paste0(plotsDir, "/BFFig1_1000.tiff"), Fig1, device = "tiff", dpi = 1000, width=85 * (14/5), height=20 * (14/5), units = "mm")
tsSum <- tsSum %>% rowwise() %>% mutate(totalME_mean = sum(cropME_mean, grassME_mean, browseME_mean, afterME_mean))
tsSum <- tsSum %>% rowwise() %>% mutate(cropMEprop_mean = cropME_mean/ totalME_mean, grassMEprop_mean = grassME_mean / totalME_mean, browseMEprop_mean = browseME_mean / totalME_mean, afterMEprop_mean = afterME_mean / totalME_mean)
tsSum <- tsSum %>%
mutate(region = dplyr::recode(region,
"(Agro)pastoral (Agro)pastoral Sahel" = "(Agro)pastoral Sahel",
"Central mixed mixed" = "Central mixed"))
tsSum_plot2 <- pivot_longer(dplyr::select(tsSum, region, year, cropMEprop_mean, grassMEprop_mean, browseMEprop_mean, afterMEprop_mean), cols = -c(region, year))
tsSum_plot2 <- transform(tsSum_plot2, region=factor(region,levels=c("(Agro)pastoral Sahel", "North mixed","Central mixed", "South mixed", "Cropping")))
tsSum_plot2 <- transform(tsSum_plot2, name=factor(name,levels=c("grassMEprop_mean", "cropMEprop_mean", "browseMEprop_mean", "afterMEprop_mean")))
SI1 <- ggplot(tsSum_plot2, aes(year, value, colour = name, fill = name)) + geom_line() + scale_x_continuous(breaks=c(2015, 2017, 2019)) + scale_y_continuous(limits = c(0,1), breaks = c(0,0.2,0.4,0.6,0.8)) + ylab("Proportion of energy") + xlab("Year") + scale_colour_manual(name = "", labels = c("Grass", "Crop residue", "Browse", "Other biomass"), values = c("cropMEprop_mean" = "#F8CA02", "grassMEprop_mean" = "#008D1F", "afterMEprop_mean" = "#000000", "browseMEprop_mean" = "#FF0000")) + theme_classic() + theme(text=element_text(family="serif", size = 14), strip.background = element_blank()) + facet_wrap(~region, nrow = 1) #scale_colour_manual(name = "", labels = c("Crop", "Grass", "Livestock"), values = c("cropME_mean" = "#F8CA02", "grassME_mean" = "#008D1F", "lvstReq" = "#FF0101")) +
ggsave(paste0(plotsDir, "/BFSI1.tiff"), SI1, device = "tiff", dpi = 300, width=85 * (14/5), height=20 * (14/5), units = "mm")
#Table 2
x <- select(tsSum[tsSum$year == 2019,], c(region, totalME_mean, cropME_mean, grassME_mean, browseME_mean, afterME_mean))
x$aez <- c("sud", "sud", "sud", "sud", "sah")
x <- group_by(x, aez)
x <- summarise_all(select(x, -region), sum)
x <- x %>% rowwise() %>% mutate(cropMEprop_mean = cropME_mean/ totalME_mean, grassMEprop_mean = grassME_mean / totalME_mean, browseMEprop_mean = browseME_mean / totalME_mean, afterMEprop_mean = afterME_mean / totalME_mean)
x <- select(x, c(aez, totalME_mean, cropMEprop_mean, grassMEprop_mean, browseMEprop_mean, afterMEprop_mean))
View(x)
0.3090139+0.4927767+0.01456678+0.1836427
# Load libraries
library(raster)
#library(stars)
library(sf)
library(dplyr)
library(tidyr)
library(exactextractr)
library(terra)
rasterOptions(tmpdir = "/home/s2255815/rspovertygroup/JameelObs/FeedBaskets/AUTemp") # Process needs > 40GB of temporary disk space
rasterOptions(maxmemory = 5e+20) # 6e+10 ~51GB allowed
rasterOptions(todisk = TRUE)
country <- "Nigeria"
# paths
spatialDir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData")
CropParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/CropParams")
LivestockParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/LivestockParams")
Results_dir <- paste0(root, "/src/3Balance-estimates/", country, "/Results")
Outputs_dir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData/outputs")
# Feed DM totals
# Loop through years
yearList <- c("2020", "2021", "2022", "2023")
year <- yearList[1]
list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern=year,full.names = T)
spatialDir
root <- "/home/s2255815/rdrive/AU_IBAR/ruminant-feed-balance"
# paths
spatialDir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData")
CropParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/CropParams")
LivestockParams_dir <- paste0(root, "/src/3Balance-estimates/", country, "/LivestockParams")
Results_dir <- paste0(root, "/src/3Balance-estimates/", country, "/Results")
Outputs_dir <- paste0(root, "/src/3Balance-estimates/", country, "/SpatialData/outputs")
list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern=year,full.names = T)
paste0("mean_", year, "\\.tif$")
list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern=paste0("mean_", year, "\\.tif$"),full.names = T)
# Feed DM totals
# Loop through years
yearList <- c("2020", "2021", "2022", "2023")
for(year in yearList){
tFeedDM <- rast(list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern=paste0("mean_", year, "\\.tif$"),full.names = T)) %>% app(., fun="sum", na.rm=TRUE)
writeRaster(tFeedDM, paste0(spatialDir, "/inputs/Feed_quantity/DMPTotal_", year, ".tif"), overwrite = TRUE)
}
tFeedDM <- stack(list.files(path = paste0(spatialDir, "/inputs/Feed_quantity"), pattern="DMPTotal",full.names = T))
#extract by aggregation zones
aggregation_zones <- c("country", "region", "state")
for(aggregation_zone in aggregation_zones){
if(aggregation_zone == "country"){
zones <- st_read(paste0(spatialDir, "/inputs/aoi0.shp"))
zones <- bind_cols(select(zones, COUNTRY), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}else if(aggregation_zone == "region"){
zones <- st_read(paste0(root, "/src/3Balance-estimates/", country, "/SpatialData/intermediate/zones.gpkg"))
zones <- bind_cols(select(zones, ECOZone), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}else if(aggregation_zone == "state"){
zones <- st_read(paste0(spatialDir, "/inputs/aoi1.shp"))
zones <- bind_cols(select(zones, NAME_1), exact_extract(terra::rast(tFeedDM), zones, fun = "sum"))
}
st_geometry(zones) <- NULL
tsSum <- data.frame(zones)
colnames(tsSum) <- c("NAME_1", "feedDM_2020", "feedDM_2021", "feedDM_2022", "feedDM_2023")
write.csv(tsSum, paste0(Results_dir, "/totals_timeseries_DM_", aggregation_zone, ".csv"), row.names=FALSE)
cat("Completed extracting DM stats for: ", aggregation_zone, "\n")
}
78526494898/274200
78526494898/27420000
7241994322392/92376800
root
2029993302174/27420000
2029993302174/92376800