-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02_extract_metadata.R
More file actions
76 lines (57 loc) · 2.21 KB
/
02_extract_metadata.R
File metadata and controls
76 lines (57 loc) · 2.21 KB
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
# This script extracts relevant fire metadata from the Monitoring Trends in
# Burn Severity database.
# Load necessary packages -------------------------------------------------
library(stringr)
library(tidyr)
library(readr)
library(data.table)
# Read in all the fire metadata files
metadata_list <- list.files(path = "../data/derived_products/mtbs/",
pattern = "metadata.txt",
full.names = TRUE)
# For each metadata text file, retrieve some data:
for (m in 1:length(metadata_list)) {
f <- readLines(metadata_list[m])
# First get fire ID
cline <- grep("MTBS Fire ID:", f,
value = TRUE)
fire_id <- as.character(str_extract(cline, "[a-z]+[0-9]+$"))
# Second get date of fire
cline <- grep("Date of Fire: ", f, value = TRUE)
cline_sep <- strsplit(cline, " ")
year <- as.numeric(unlist(cline_sep)[[6]])
# Third, get the acreage burned
cline <- grep("Acres within Fire Perimeter:", f,
value = TRUE)
fire_acres <- as.numeric(str_extract(cline, "[0-9]+$"))
# Fourth, get the coordinates
# West
cline <- grep("West_Bounding_Coordinate: ", f,
value = TRUE)
west <- as.numeric(str_extract(cline, "\\-*\\d+\\.*\\d*"))
# East
cline <- grep("East_Bounding_Coordinate: ", f,
value = TRUE)
east <- as.numeric(str_extract(cline, "\\-*\\d+\\.*\\d*"))
# North
cline <- grep("North_Bounding_Coordinate: ", f,
value = TRUE)
north <- as.numeric(str_extract(cline, "\\-*\\d+\\.*\\d*"))
# South
cline <- grep("South_Bounding_Coordinate: ", f,
value = TRUE)
south <- as.numeric(str_extract(cline, "\\-*\\d+\\.*\\d*"))
# Average the coordinate values to create a center point
centr_lat <- mean(c(north, south))
centr_lon <- mean(c(east, west))
# Lastly, create a data frame of the retrieved values
if (m == 1) {
dat_combined <- data.frame(fire_id, year, fire_acres, centr_lat, centr_lon)
} else {
temp_dat <- data.frame(fire_id, year, fire_acres, centr_lat, centr_lon)
dat_combined <- rbind(dat_combined, temp_dat)
}
}
# Export the new dataset
fwrite(x = dat_combined,
file = paste("../data/derived_products/combined_fire_metadata.csv"))