-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrop-drivers.R
315 lines (246 loc) · 11.5 KB
/
crop-drivers.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
## ------------------------------------------------------- ##
# Silica WG - Crop Spatial Data
## ------------------------------------------------------- ##
# Written by:
## Nick J Lyon
# Purpose:
## Crop spatial data retrieved using the AppEEARS database
## https://appeears.earthdatacloud.nasa.gov/
## ------------------------------------------------------- ##
# Housekeeping ----
## ------------------------------------------------------- ##
# Read needed libraries
# install.packages("librarian")
librarian::shelf(tidyverse, sf, ncdf4, stars, terra)
# Clear environment
rm(list = ls())
# Identify path to location of shared data
(path <- scicomptools::wd_loc(local = F, remote_path = file.path('/', "home", "shares", "lter-si", "si-watershed-extract")))
## ------------------------------------------------------- ##
# List Raw Files - Russia East ----
## ------------------------------------------------------- ##
# The bounding box for the "russia-east" appEEARS data extends slightly too far west
# Need to slice off a thin strip of the western edge of this 'tile'
# Make an empty list
file_list <- list()
# Identify all tiles of this region for each driver
for(driver in c("raw-evapo-modis16a2-v006", "raw-snow-modis10a2-v006", "raw-greenup", "raw-npp")){
# Identify files
file_df <- data.frame("driver_id" = driver,
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "russia-east")))
# Add to list
file_list[[driver]] <- file_df }
# Wrangle the list
file_all <- file_list %>%
# Unlist the loop's output
purrr::map_dfr(.f = dplyr::select, dplyr::everything())
# Glimpse it
dplyr::glimpse(file_all)
# Clean up environment
rm(list = setdiff(ls(), c('path', 'file_all')))
## ------------------------------------------------------- ##
# Identify Cropped Files - Russia East ----
## ------------------------------------------------------- ##
# Make an empty list
crop_list <- list()
# Identify all tiles of this region for each driver
for(driver in c("raw-evapo-modis16a2-v006", "raw-snow-modis10a2-v006", "raw-greenup")){
# Identify files
crop_df <- data.frame("driver_id" = driver,
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "cropped-russia-east")))
# Add to list
crop_list[[driver]] <- crop_df }
# Wrangle the list
crop_all <- crop_list %>%
# Unlist the loop's output
purrr::map_dfr(.f = dplyr::select, dplyr::everything())
# Glimpse it
dplyr::glimpse(crop_all)
# Clean up environment
rm(list = setdiff(ls(), c('path', 'file_all', 'crop_all')))
## ------------------------------------------------------- ##
# Crop - Russia East ----
## ------------------------------------------------------- ##
# Identify undone files
undone <- file_all %>%
dplyr::filter(!files %in% crop_all$files)
# Create needed export folders
for(driver in unique(undone$driver_id)){
dir.create(path = file.path(path, "raw-driver-data", driver, "cropped-russia-east"),
showWarnings = F) }
# Identify number of files to crop
(file_total <- length(unique(undone$files)))
# For each file
for(k in 1:file_total){
# Read in that raster
rast <- terra::rast(file.path(path, "raw-driver-data", undone$driver_id[k],
"russia-east", undone$files[k]))
# Define desired extent
bbox <- terra::ext(143, 172, 60, 73)
# Crop using that bounding box
cropped_rast <- terra::crop(x = rast, y = bbox)
# Write that raster out
terra::writeRaster(x = cropped_rast, overwrite = T,
filename = file.path(path, "raw-driver-data", undone$driver_id[k],
"cropped-russia-east", undone$files[k]))
# Completion message
message("Completed processing for file ", k, " of ", file_total) }
# Test plot to see the issue and how we fixed it by cropping
## Grab the shapefiles of the watersheds
sheds <- sf::st_read(dsn = file.path(path, "site-coordinates", "silica-watersheds.shp"))
## Uncropped (slight overlap with another watershed)
plot(rast, axes = T, reset = F, main = "russia-east UNCROPPED")
plot(sheds, axes = T, add = T)
## Cropped
plot(cropped_rast, axes = T, reset = F, main = "russia-east CROPPED")
plot(sheds, axes = T, add = T)
# Clean up environment
rm(list = setdiff(ls(), c('path')))
## ------------------------------------------------------- ##
# List Raw Files - Russia West, West 2 & Center ----
## ------------------------------------------------------- ##
# Had to download several separate bounding boxes to get Russian GRO rivers
# Need to crop these so there is no overlap between them
# Make an empty list
file_list <- list()
# Identify all tiles of this region for each driver
for(driver in c("raw-evapo-modis16a2-v006", "raw-snow-modis10a2-v006", "raw-greenup", "raw-npp")){
# Identify files for each region
west_df <- data.frame("driver_id" = driver,
"region" = "russia-west",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "russia-west")))
west2_df <- data.frame("driver_id" = driver,
"region" = "russia-west-2",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "russia-west-2")))
ctr_df <- data.frame("driver_id" = driver,
"region" = "russia-center",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "russia-center")))
# Bind them together
file_df <- west_df %>%
dplyr::bind_rows(y = west2_df) %>%
dplyr::bind_rows(y = ctr_df)
# Add to list
file_list[[driver]] <- file_df }
# Wrangle the list
file_all <- file_list %>%
# Unlist the loop's output
purrr::map_dfr(.f = dplyr::select, dplyr::everything()) %>%
# Make a combination column of region + file name
dplyr::mutate(id = paste0(region, "_", files))
# Glimpse it
dplyr::glimpse(file_all)
# Clean up environment
rm(list = setdiff(ls(), c('path', 'file_all')))
## ------------------------------------------------------- ##
# Identify Cropped Files - Russia West, West 2 & Center ----
## ------------------------------------------------------- ##
# Make an empty list
crop_list <- list()
# Identify all tiles of this region for each driver
for(driver in c("raw-evapo-modis16a2-v006", "raw-snow-modis10a2-v006", "raw-greenup")){
# Identify files for each region
crop_west_df <- data.frame("driver_id" = driver,
"region" = "russia-west",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "cropped-russia-west")))
crop_west2_df <- data.frame("driver_id" = driver,
"region" = "russia-west-2",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "cropped-russia-west-2")))
crop_ctr_df <- data.frame("driver_id" = driver,
"region" = "russia-center",
"files" = dir(path = file.path(path, "raw-driver-data",
driver, "cropped-russia-center")))
# Bind them together
crop_df <- crop_west_df %>%
dplyr::bind_rows(y = crop_west2_df) %>%
dplyr::bind_rows(y = crop_ctr_df)
# Add to list
crop_list[[driver]] <- crop_df }
# Wrangle the list
crop_all <- crop_list %>%
# Unlist the loop's output
purrr::map_dfr(.f = dplyr::select, dplyr::everything()) %>%
# Make a combination column of region + file name
dplyr::mutate(id = paste0(region, "_", files))
# Glimpse it
dplyr::glimpse(crop_all)
# Clean up environment
rm(list = setdiff(ls(), c('path', 'file_all', 'crop_all')))
## ------------------------------------------------------- ##
# Crop - Russia West, West 2 & Center ----
## ------------------------------------------------------- ##
# Identify undone files
undone <- file_all %>%
dplyr::filter(!id %in% crop_all$id)
# Create needed export folders
for(driver in unique(undone$driver_id)){
for(reg in unique(undone$region)){
dir.create(path = file.path(path, "raw-driver-data", driver,
paste0("cropped-", reg)),
showWarnings = F) } }
# Identify number of files to crop
(file_total <- nrow(undone))
# For each file
for(k in 1:file_total){
# Read in that raster
rast <- terra::rast(file.path(path, "raw-driver-data", undone$driver_id[k],
undone$region[k], undone$files[k]))
# Define desired extent
if(undone$region[k] == "russia-west"){ bbox <- terra::ext(57, 87, 46, 73) }
if(undone$region[k] == "russia-west-2"){ bbox <- terra::ext(87, 122, 46, 73) }
if(undone$region[k] == "russia-center"){ bbox <- terra::ext(122, 142, 46, 73) }
# Crop using that bounding box
cropped_rast <- terra::crop(x = rast, y = bbox)
# Write that raster out
terra::writeRaster(x = cropped_rast, overwrite = T,
filename = file.path(path, "raw-driver-data", undone$driver_id[k],
paste0("cropped-", undone$region[k]),
undone$files[k]))
# Completion message
message("Completed processing for file ", k, " of ", file_total) }
# Identify one file for each region we just cropped
test_files <- file_all %>%
dplyr::group_by(driver_id, region) %>%
dplyr::summarize(files = dplyr::first(files)) %>%
dplyr::ungroup()
# Read in one of each raster
## Uncropped
rast1 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[1],
test_files$region[1], test_files$files[1]))
rast2 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[2],
test_files$region[2], test_files$files[2]))
rast3 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[3],
test_files$region[3], test_files$files[3]))
## Cropped
rast4 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[1],
paste0("cropped-", test_files$region[1]), test_files$files[1]))
rast5 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[2],
paste0("cropped-", test_files$region[2]), test_files$files[2]))
rast6 <- terra::rast(file.path(path, "raw-driver-data", test_files$driver_id[3],
paste0("cropped-", test_files$region[3]), test_files$files[3]))
# Grab the shapefiles of the watersheds
sheds <- sf::st_read(dsn = file.path(path, "site-coordinates", "silica-watersheds.shp"))
# Make an empty raster 'frame' around the whole area
frame_rast <- terra::rast(terra::ext(55, 140, 45, 80))
# Test plot to see the issue and how we fixed it by cropping
## Uncropped (slight overlap with each other)
suppressWarnings(plot(frame_rast, axes = T, reset = F, main = "Russia COMPOSITE (Uncropped)"))
plot(rast1, axes = T, add = T)
plot(rast2, axes = T, add = T)
plot(rast3, axes = T, add = T)
plot(sheds, axes = T, add = T)
## Cropped
suppressWarnings(plot(frame_rast, axes = T, reset = F, main = "Russia COMPOSITE (Cropped)"))
plot(rast4, axes = T, add = T)
plot(rast5, axes = T, add = T)
plot(rast6, axes = T, add = T)
plot(sheds, axes = T, add = T)
# Clean up environment
rm(list = setdiff(ls(), c('path')))
# End ----