-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_targets.R
More file actions
359 lines (318 loc) · 11 KB
/
Copy path_targets.R
File metadata and controls
359 lines (318 loc) · 11 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
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
# Created by use_targets().
# Follow the comments below to fill in this target script.
# Then follow the manual to check and run the pipeline:
# https://books.ropensci.org/targets/walkthrough.html#inspect-the-pipeline
# Load packages required to define the pipeline:
library(targets)
library(tarchetypes)
library(geotargets) #dev version for tar_terra_tiles()
library(rlang) #for syms()
library(crew)
library(quarto) #only required for rendering reports
# Set up a "controller" for parallelization of tasks. Here I'm using a max of 4 concurrent workers.
controller_local <-
crew::crew_controller_local(
name = "local",
workers = 3, # max workers
seconds_idle = 60, # how long a worker can be doing nothing before it is shut down
local_log_directory = "logs"
)
# Use d:// drive for targets store so all have access
# TODO: this doesn't work and also might not be a good idea because different
# people using the same _targets/ store will cause race conditions and
# overwriting of work. Versioned S3 bucket is a better way to go for this.
# tar_config_set(
# store = "d://targets_stores/AZ-AGB-trend/_targets"
# )
# Set target options:
tar_option_set(
packages = c("ncdf4", "terra", "geotargets", "fs", "purrr", "car", "dplyr", "exactextractr", "sf"), # Packages that your targets need for their tasks.
controller = controller_local,
# improve memory performance
memory = "transient",
garbage_collection = TRUE,
# allow workers to access _targets/ store directly
storage = "worker",
retrieval = "worker"
)
# Run the R scripts in the R/ folder with your custom functions:
tar_source()
## use data on mounted "snow" drive
## (reading files dircetly from snow is quite slow for me working from home over
## the VPN, and I prefer to just copy them to my hard drive)
# root <- "/Volumes/moore/"
#use local data in this project
root <- "d://"
# Inputs ------------------------------------------------------------------
targets_inputs <- tar_plan(
# create shapefiles for southwest
tar_file_fast(file_az, path(root, "shapefiles/az_border/azboundary.geojson")),
tar_terra_vect(az, terra::vect(file_az)),
tar_file_fast(dir_pima, path(root, "shapefiles/pima_county/Pima_County_Boundary.geojson")),
tar_terra_vect(pima, terra::vect(dir_pima)),
tar_file_fast(file_forest, path(root, "shapefiles/sw_forest/R03_AdministrativeForest_8682094378425567158.gpkg")),
tar_terra_vect(forest, read_az_landuse(file_forest, az)),
tar_file_fast(file_wilderness, path(root, "shapefiles/sw_wilderness/R03_WildernessStatus_-8694334871182256119.gpkg")),
#currently subsets to just national wilderness
tar_terra_vect(wilderness, read_az_wilderness(file_wilderness, az)),
tar_file_fast(file_grazing, path(root, "shapefiles/sw_grazing/allot_-3402377083130374287.gpkg")),
tar_terra_vect(grazing, read_az_landuse(file_grazing, az)),
# Track raster files
tar_file_fast(file_xu, fs::path(root, "AGB_cleaned/xu/xu_2000-2019.tif")),
tar_file_fast(file_liu, fs::path(root, "AGB_cleaned/liu/liu_1993-2012.tif")),
tar_file_fast(file_menlove, fs::path(root, "AGB_cleaned/menlove/menlove_2009-2019.tif")),
tar_file_fast(file_gedi, fs::path(root, "AGB_cleaned/gedi/gedi_2019-2023.tif")),
tar_file_fast(file_chopping, fs::path(root, "AGB_cleaned/chopping/chopping_2000-2021.tif")),
# These are tiles
tar_file_fast(dir_esa, fs::path(root, "AGB_cleaned/esa_cci/")),
tar_file_fast(dir_ltgnn, fs::path(root, "AGB_cleaned/lt_gnn/")),
# Read in rasters (all layers for now)
tar_terra_rast(agb_xu, read_agb(file_xu, az)),
tar_terra_rast(agb_liu, read_agb(file_liu, az)),
tar_terra_rast(agb_chopping, read_agb(file_chopping, az)),
tar_terra_rast(agb_esa, read_agb(dir_esa, az)),
tar_terra_rast(agb_ltgnn, read_agb(dir_ltgnn, az)),
)
# Yearly summary stats ----------------------------------------------------
target_yearly <- tar_plan(
tar_map(
values = tidyr::expand_grid(
product = syms(c("agb_esa", "agb_xu", "agb_liu", "agb_chopping", "agb_ltgnn")),
subset = syms(c("az", "forest", "wilderness", "grazing", "pima"))
),
tar_target(
summary,
summarize_yearly(product, subset)
)
)
)
target_yearly_summary <- tar_plan(
tar_combine(
summary_yearly,
target_yearly, #all the targets defined above
command = dplyr::bind_rows(!!!.x) |>
arrange(subset, product)
),
tar_file(
summary_yearly_csv,
tar_write_csv(summary_yearly, "output/yearly/yearly_summary.csv")
)
)
# Averages ----------------------------------------------------------------
target_averages <- tar_plan(
# These products just have one layer that is an average over time
tar_terra_rast(avg_menlove, read_agb(file_menlove, az)),
tar_terra_rast(avg_gedi, read_agb(file_gedi, az)),
# These need to be calculated
tar_terra_rast(avg_esa, mean(agb_esa, na.rm = TRUE)),
tar_terra_rast(avg_xu, mean(agb_xu, na.rm = TRUE)),
tar_terra_rast(avg_liu, mean(agb_liu, na.rm = TRUE)),
tar_terra_rast(avg_chopping, mean(agb_chopping, na.rm = TRUE)),
tar_terra_rast(avg_ltgnn, mean(agb_ltgnn, na.rm = TRUE))
)
target_avg_maps <- tar_plan(
tar_map(
values = list(
product = syms(c("avg_menlove", "avg_gedi", "avg_esa", "avg_xu", "avg_liu", "avg_chopping", "avg_ltgnn"))
),
tar_file(
map,
plot_avg_map(product, az),
packages = c("ggplot2", "tidyterra", "colorspace", "stringr", "ggtext")
)
)
)
target_avg_summary <- tar_plan(
tar_map(
values = tidyr::expand_grid(
avg_products = syms(c("avg_menlove", "avg_gedi", "avg_esa", "avg_xu", "avg_liu", "avg_chopping", "avg_ltgnn")),
subsets = syms(c("az", "forest", "wilderness", "grazing", "pima"))
),
tar_target(
summary,
summarize_means(avg_products, subsets)
)
)
)
target_avg_summary_combine <- tar_plan(
tar_combine(
summary_avg,
target_avg_summary, #all the targets defined above
command = dplyr::bind_rows(!!!.x) |>
arrange(subset, product)
),
tar_file(
summary_avg_csv,
tar_write_csv(summary_avg, "output/average/average_summary.csv"),
packages = c("readr")
)
)
# Slopes ------------------------------------------------------------------
# Calculate trends in AGB
# Only some products have multiple layers, so only those are included in these targets
targets_slopes <- tar_plan(
tar_terra_rast(slope_xu, calc_slopes(agb_xu)),
tar_terra_rast(slope_liu, calc_slopes(agb_liu)),
#for higher res products, split into a few tiles for dynamic branching
tar_terra_tiles(tiles_agb_chopping, raster = agb_chopping, ncol = 3, nrow = 4),
tar_terra_rast(
tiles_slope_chopping,
calc_slopes(tiles_agb_chopping),
iteration = "list",
pattern = map(tiles_agb_chopping)
),
tar_terra_rast(slope_chopping, merge(sprc(tiles_slope_chopping)), description = "recombine tiles"),
tar_terra_tiles(tiles_agb_esa, raster = agb_esa, ncol = 3, nrow = 3),
tar_terra_rast(
tiles_slope_esa,
calc_slopes(tiles_agb_esa),
iteration = "list",
pattern = map(tiles_agb_esa)
),
tar_terra_rast(slope_esa, merge(sprc(tiles_slope_esa)), description = "recombine tiles"),
tar_terra_tiles(tiles_agb_ltgnn, raster = agb_ltgnn, ncol = 6, nrow = 6),
tar_terra_rast(
tiles_slope_ltgnn,
calc_slopes(tiles_agb_ltgnn),
iteration = "list",
pattern = map(tiles_agb_ltgnn)
),
tar_terra_rast(slope_ltgnn, merge(sprc(tiles_slope_ltgnn)), description = "recombine tiles")
)
targets_slope_plots <- tar_plan(
tar_map(
values = list(
product = syms(c(
"slope_xu", "slope_liu",
"slope_esa", "slope_chopping", "slope_ltgnn"
))
),
tar_file(
plot,
plot_slopes(product, az),
packages = c("ggplot2", "tidyterra", "colorspace", "stringr", "ggtext"),
garbage_collection = TRUE
)
)
)
targets_slope_summary <- tar_plan(
tar_map(#for each raster * subset combination
values = tidyr::expand_grid(
rasters = syms(c(
"slope_liu", "slope_xu",
"slope_esa", "slope_chopping", "slope_ltgnn"
)),
subsets = syms(c(
"az", "forest", "wilderness", "grazing", "pima"
))
),
#get summary stats
tar_target(
summary,
summarize_slopes(rasters, subsets)
)
)
)
targets_slope_summary_combine <- tar_plan(
tar_combine(
summary_slope,
targets_slope_summary, #combine all the targets defined above in slope_summary
command = dplyr::bind_rows(!!!.x) |>
arrange(subset, product)
),
tar_file(
summary_slope_csv,
tar_write_csv(summary_slope, "output/slopes/slopes_summary.csv"),
packages = c("readr")
)
)
# # plot summary statistics
targets_slope_summary_plot <- tar_plan(
tar_target(
summary_slope_plot,
plot_summary_stats(summary_slope),
packages = c("ggplot2", "ggtext")
),
tar_file(
summary_slope_plot_png,
ggplot2::ggsave("output/slopes/figs/summary_plot.png", summary_slope_plot, bg = "white")
)
)
# Product comparison ------------------------------------------------------
# re-project to common CRS and resolution and calculate pixel-wise standard
# deviation across products to get a sense of how variation ("disagreement")
# varies spatially
targets_comparison <- tar_plan(
tar_terra_rast(
avg_stack,
reproject_stack(avg_esa, avg_menlove, avg_gedi, avg_xu, avg_liu, avg_chopping, avg_ltgnn)
),
tar_terra_rast(
stdev,
terra::stdev(avg_stack, na.rm = TRUE),
description = "Calculate pixel-wise standard deviation across product means"
),
tar_file(
stdev_plot,
plot_stdev(stdev, az),
packages = c("ggplot2", "tidyterra", "ggtext", "scales")
)
)
targets_comparison_summary <- tar_plan(
tar_map(
values = list(
subsets = syms(c(
"az", "forest", "wilderness", "grazing", "pima"
))
),
tar_target(
summary_stdev,
summarize_stdev(stdev, subsets)
)
)
)
targets_comparison_summary_combine <- tar_plan(
tar_combine(
summary_stdev,
targets_comparison_summary, #combine all the targets defined above
command = dplyr::bind_rows(!!!.x) |>
arrange(subset, product)
),
tar_file(
summary_stdev_csv,
tar_write_csv(summary_stdev, "output/comparison/stdev_summary.csv"),
packages = c("readr")
)
)
targets_plot_subsets <- tar_plan(
tar_file(
subset_map,
plot_subsets(az, pima, grazing, wilderness, forest),
packages = c("ggplot2", "patchwork", "tidyterra", "forcats", "dplyr", "gtable")
)
)
# # Render .Qmd documents
targets_render <- tar_plan(
tar_quarto(readme, "README.qmd"),
# tar_quarto(report, "docs/index.qmd")
)
#_targets.R must end with a list of targets. They can be arbitrarily nested
list(
targets_inputs,
target_yearly,
target_yearly_summary,
target_averages,
target_avg_summary,
target_avg_summary_combine,
target_avg_maps,
targets_slopes,
targets_slope_plots,
targets_slope_summary,
targets_slope_summary_combine,
targets_slope_summary_plot,
targets_comparison,
targets_comparison_summary,
targets_comparison_summary_combine,
targets_render,
targets_plot_subsets
)