-
Notifications
You must be signed in to change notification settings - Fork 0
/
FWI-grid-cell.R
149 lines (119 loc) · 6.18 KB
/
FWI-grid-cell.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
# Extracting fire danger indices from https://cds.climate.copernicus.eu/cdsapp#!/dataset/cems-fire-historical?tab=overview within grid cells
# Clear data
rm(list = ls())
# Load libraries
library(sf)
library(raster)
library(lubridate)
library(dplyr)
# Read in shapefiles
setwd("~/BTO projects/Polesia wildfires/fire shapefiles/pixels")
pix <- st_read("all_pixels.shp")
# Calculate months 3, 6 and 12 months before grid cell dates and match dates to raster filenames by removing the dash
pix$month3 <- gsub("-", "", as.Date(pix$dates) %m-% months(3))
pix$month6 <- gsub("-", "", as.Date(pix$dates) %m-% months(6))
pix$month12 <- gsub("-", "", as.Date(pix$dates) %m-% months(12))
# Match grid cell dates to raster filenames by removing the dash
pix$dates <- gsub("-", "", pix$dates)
# Create vector of unique dates
month3 <- unique(pix$month3)
month6 <- unique(pix$month6)
month12 <- unique(pix$month12)
dates <- unique(pix$dates)
# DROUGHT CODE ##########################
# Name the main folder where the input variables are stored
dc.wd <- "~/BTO projects/Polesia wildfires/input variables/dataset-cems-fire-historical-DC"
# Create empty lists
dc.lst <- list()
dc.3.lst <- list() ## 3 months before grid cell date
dc.6.lst <- list() ## 6 months
dc.12.lst <- list() ## 12 months
# Loop through each date reading in rasters calculating the mean value within grid cells
for (i in 1:length(dates)) {
dc.lst[[i]] <- as.data.frame(extract(raster(list.files(path = dc.wd, pattern = dates[i], full.names = T, recursive = T)),
pix[pix$dates == dates[i],], fun = mean))
# And lagged drought code (3, 6 and 12 months before)
dc.3.lst[[i]] <- as.data.frame(extract(raster(list.files(path = dc.wd, pattern = month3[i], full.names = T, recursive = T)),
pix[pix$month3 == month3[i],], fun = mean))
dc.6.lst[[i]] <- as.data.frame(extract(raster(list.files(path = dc.wd, pattern = month6[i],full.names = T, recursive = T)),
pix[pix$month6 == month6[i],], fun = mean))
dc.12.lst[[i]] <- as.data.frame(extract(raster(list.files(path = dc.wd, pattern = month12[i], full.name = T, recursive = T)),
pix[pix$month12 == month12[i],], fun = mean))
}
# Merge dataframes in list
dc.df <- do.call("rbind", dc.lst)
dc.3.df <- do.call("rbind", dc.3.lst)
dc.6.df <- do.call("rbind", dc.6.lst)
dc.12.df <- do.call("rbind", dc.12.lst)
# Change column names
colnames(dc.df) <- "dc"
colnames(dc.3.df) <- "dc_3"
colnames(dc.6.df) <- "dc_6"
colnames(dc.12.df) <- "dc_12"
# KBDI ####
# Name the main folder where the input variables are stored
kbdi.wd <- "~/BTO projects/Polesia wildfires/input variables/dataset-cems-fire-historical-KBDI"
# Create empty lists
kbdi.lst <- list()
kbdi.3.lst <- list()
kbdi.6.lst <- list()
kbdi.12.lst <- list()
# Loop through each date reading in rasters calculating the mean value within grid cells
for (i in 1:length(dates)) {
kbdi.lst[[i]] <- as.data.frame(extract(raster(list.files(path = kbdi.wd, pattern = dates[i], full.names = T, recursive = T)),
pix[pix$dates == dates[i],], fun = mean))
# And lagged drought code (3, 6 and 12 months before)
kbdi.3.lst[[i]] <- as.data.frame(extract(raster(list.files(path = kbdi.wd, pattern = month3[i], full.names = T, recursive = T)),
pix[pix$month3 == month3[i],], fun = mean))
kbdi.6.lst[[i]] <- as.data.frame(extract(raster(list.files(path = kbdi.wd, pattern = month6[i], full.names = T, recursive = T)),
pix[pix$month6 == month6[i],], fun = mean))
kbdi.12.lst[[i]] <- as.data.frame(extract(raster(list.files(path = kbdi.wd, pattern = month12[i], full.names = T, recursive = T)),
pix[pix$month12 == month12[i],], fun = mean))
}
# Merge dataframes in list
kbdi.df <- do.call("rbind", kbdi.lst)
kbdi.3.df <- do.call("rbind", kbdi.3.lst)
kbdi.6.df <- do.call("rbind", kbdi.6.lst)
kbdi.12.df <- do.call("rbind", kbdi.12.lst)
# Change column names
colnames(kbdi.df) <- "kbdi"
colnames(kbdi.3.df) <- "kbdi_3"
colnames(kbdi.6.df) <- "kbdi_6"
colnames(kbdi.12.df) <- "kbdi_12"
# FINE FUEL MOISTURE CONTENT ##########################
# Name the main folder where the input variables are stored
ffmc.wd <- "~/BTO projects/Polesia wildfires/input variables/dataset-cems-fire-historical-FFMC"
# Create empty list to fill with FFMC data
ffmc.lst <- list()
# Loop through each date reading in rasters calculating the mean value within grid cells
for (i in 1:length(dates)) {
ffmc.lst[[i]] <- as.data.frame(extract(raster(list.files(path = ffmc.wd, pattern = dates[i], full.names = T, recursive = T)),
pix[pix$dates == dates[i],], fun = mean))
} ## No lagged data for moisture content
# Merge dataframes in list
ffmc.df <- do.call("rbind", ffmc.lst)
# Change column name
colnames(ffmc.df) <- "ffmc"
# FWI ##########################
# Name the main folder where the input variables are stored
fwi.wd <- "~/BTO projects/Polesia wildfires/input variables/dataset-cems-fire-historical-FWI"
# Create empty list
fwi.lst <- list()
# Loop through each date reading in all rasters and calculating the mean value within grid cells
for (i in 1:length(dates)) {
fwi.lst[[i]] <- as.data.frame(extract(raster(list.files(path = fwi.wd, pattern = dates[i], full.names = T, recursive = T)),
pix[pix$dates == dates[i],], fun = mean))
} ## No lagged data for fire weather index
# Merge dataframes in list
fwi.df <- do.call("rbind", fwi.lst)
# Change column name
colnames(fwi.df) <- "fwi"
#### Merge all dataframes ####
dfs <- sapply(.GlobalEnv, is.data.frame)
df <- do.call(cbind, unname(mget(names(dfs)[dfs])))
# Rename columns
colnames(df)[11:12] <- c("z", "pix")
df <- select(df, -contains("pix.")) # Remove dates and geometry
# Export as csv file
setwd("~/BTO projects/Polesia wildfires/input variables") # Change working directory to where you are storing your output files
write.csv(df, "FWI.csv", row.names = F) ## Outputs are dataframe of fire weather indices and grid cell IDs