-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathd_qa_stack.R
More file actions
345 lines (301 loc) · 12 KB
/
d_qa_stack.R
File metadata and controls
345 lines (301 loc) · 12 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
# Targets list to gather Landsat stack at WQP site locations
# Source targets functions ------------------------------------------------
tar_source(files = "d_qa_stack/src/")
# Define {targets} workflow -----------------------------------------------
# target objects in workflow
d_qa_stack <- list(
tar_target(
name = d_check_dir_structure,
command = {
# make directories if needed
directories = c("d_qa_stack/qa/",
"d_qa_stack/out/",
"d_qa_stack/export/")
walk(directories, function(dir) {
if(!dir.exists(dir)){
dir.create(dir)
}
})
},
cue = tar_cue("always"),
deployment = "main",
),
tar_target(
name = d_mission_identifiers,
command = tibble(mission_id = c("LT04", "LT05", "LE07", "LC08", "LC09"),
mission_names = c("Landsat 4", "Landsat 5", "Landsat 7", "Landsat 8", "Landsat 9"))
),
# set dswe types
tar_target(
name = d_dswe_types,
command = {
dswe = NULL
if (grepl("1", b_yml$DSWE_setting)) {
dswe = c(dswe, "DSWE1")
}
if (grepl("1a", b_yml$DSWE_setting)) {
dswe = c(dswe, "DSWE1a")
}
if (grepl("3", b_yml$DSWE_setting)) {
dswe = c(dswe, "DSWE3")
}
dswe
}
),
tar_files(
name = d_metadata_files,
command = c_collated_siteSR_files %>%
.[grepl("metadata", .)]
),
# qa the siteSR stacks like we do with lakeSR
tar_target(
name = d_qa_Landsat_files,
command = {
d_check_dir_structure
qa_and_document_LS(mission_info = d_mission_identifiers,
dswe = d_dswe_types,
metadata_files = d_metadata_files,
collated_files = c_collated_siteSR_files)
},
packages = c("arrow", "data.table", "tidyverse", "ggrepel", "viridis", "stringi"),
pattern = cross(d_mission_identifiers, d_dswe_types),
deployment = "main" # can not be run multi-core
),
# now add siteSR id, and necessary information for data storage, save as .csv
tar_files(
name = d_qa_files_list,
command = {
d_qa_Landsat_files
list.files("d_qa_stack/qa/",
full.names = TRUE,
pattern = ".feather") %>%
# make sure gee version is right
.[grepl(paste0(b_yml$run_date, "_filtered"), .)]
}
),
# create a list of HUC2's to map over
tar_target(
name = d_unique_huc2,
command = {
a_sites_with_NHD_info %>%
filter(siteSR_id %in% b_visible_sites$id) %>%
distinct(huc2 = str_sub(assigned_HUC, 1, 2)) %>%
pull(huc2)
}
),
# Landsat 4 is small enough for a single file
tar_target(
name = d_Landsat4_collated_data,
command = sort_qa_Landsat_data(qa_files = d_qa_files_list,
gee_identifier = b_yml$run_date,
mission_info = d_mission_identifiers %>%
filter(mission_names == "Landsat 4"),
site_info = a_sites_with_NHD_info,
dswe = d_dswe_types),
pattern = map(d_dswe_types),
packages = c("data.table", "tidyverse", "arrow", "stringi")
),
# Landsat 5, 7, 8 need to be separated by HUC2
tar_target(
name = d_collated_Landsat5_by_huc2,
command = sort_qa_Landsat_data(qa_files = d_qa_files_list,
gee_identifier = b_yml$run_date,
mission_info = d_mission_identifiers %>%
filter(mission_names == "Landsat 5"),
site_info = a_sites_with_NHD_info,
dswe = d_dswe_types,
HUC2 = d_unique_huc2),
pattern = cross(d_dswe_types, d_unique_huc2),
packages = c("data.table", "tidyverse", "arrow", "stringi"),
deployment = "main" # too big for multicore
),
tar_target(
name = d_collated_Landsat7_by_huc2,
command = sort_qa_Landsat_data(qa_files = d_qa_files_list,
gee_identifier = b_yml$run_date,
mission_info = d_mission_identifiers %>%
filter(mission_names == "Landsat 7"),
site_info = a_sites_with_NHD_info,
dswe = d_dswe_types,
HUC2 = d_unique_huc2),
pattern = cross(d_dswe_types, d_unique_huc2),
packages = c("data.table", "tidyverse", "arrow", "stringi"),
deployment = "main" # too big for multicore
),
tar_target(
name = d_collated_Landsat8_by_huc2,
command = sort_qa_Landsat_data(qa_files = d_qa_files_list,
gee_identifier = b_yml$run_date,
mission_info = d_mission_identifiers %>%
filter(mission_names == "Landsat 8"),
site_info = a_sites_with_NHD_info,
dswe = d_dswe_types,
HUC2 = d_unique_huc2),
pattern = cross(d_dswe_types, d_unique_huc2),
packages = c("data.table", "tidyverse", "arrow", "stringi"),
deployment = "main" # too big for multicore
),
# Landsat 9 is small enough to be a single file.
tar_target(
name = d_Landsat9_collated_data,
command = sort_qa_Landsat_data(qa_files = d_qa_files_list,
gee_identifier = b_yml$run_date,
mission_info = d_mission_identifiers %>%
filter(mission_names == "Landsat 9"),
site_info = a_sites_with_NHD_info,
dswe = d_dswe_types),
pattern = map(d_dswe_types),
packages = c("data.table", "tidyverse", "arrow", "stringi")
),
# metadata
tar_target(
name = d_Landsat_metadata_formatted,
command = prep_LS_metadata_for_export(file = d_metadata_files,
file_type = "csv",
gee_identifier = b_yml$run_date,
out_path = "d_qa_stack/export"),
pattern = map(d_metadata_files),
packages = c("data.table", "tidyverse", "arrow", "stringi")
),
# make a list of the collated and sorted files created
tar_files(
name = d_all_sorted_Landsat_files,
command = as.vector(c(d_Landsat4_collated_data, d_collated_Landsat5_by_huc2,
d_collated_Landsat7_by_huc2, d_collated_Landsat8_by_huc2,
d_Landsat9_collated_data, d_Landsat_metadata_formatted))
),
tar_target(
name = d_make_Landsat_feather_files,
command = {
# create version folder for output
if (!dir.exists(file.path("d_qa_stack/out/", b_yml$run_date))) {
dir.create(file.path("d_qa_stack/out/", b_yml$run_date))
}
# filter for identifier/dswe
fns <- d_all_sorted_Landsat_files[grepl(gsub(" ", "", d_mission_identifiers$mission_names),
d_all_sorted_Landsat_files)]
fns_dswe <- fns[grepl(paste0(d_dswe_types, "_"), fns)]
# create the output filepath
out_fp <- paste0("d_qa_stack/out/",
b_yml$run_date,
"/siteSR_",
str_replace(d_mission_identifiers$mission_names," ", ""),
"_", d_dswe_types, "_",
b_yml$run_date, ".feather")
# check to see if this is a single file, or multiple and needs additional
# data handling
if (length(fns_dswe > 1)) {
# create a temp directory for the temporary Arrow dataset
temp_dataset_dir <- tempfile("arrow_ds_")
dir.create(temp_dataset_dir)
# these files need to be processed by chunk to deal with memory issues
walk(fns_dswe, function(fn) {
# read chunk
chunk <- fread(fn)
setDT(chunk)
# add source_file column to partition by
chunk[, source_file := tools::file_path_sans_ext(basename(fn))]
# write chunk using partitioning (otherwise we hit memory issues)
write_dataset(chunk,
path = temp_dataset_dir,
format = "feather",
partitioning = "source_file",
existing_data_behavior = "delete_matching")
})
# connect to the arrow-partitioned file
ds <- open_dataset(temp_dataset_dir, format = "feather")
# and grab all the data and write the feather file
ds %>%
collect() %>%
select(-source_file) %>%
write_feather(., out_fp, compression = "lz4")
# housekeeping
unlink(temp_dataset_dir, recursive = TRUE)
gc()
Sys.sleep(5)
} else {
data <- fread(fn)
write_feather(data, out_fp, compression = "lz4")
}
# return filepath
out_fp
},
pattern = cross(d_dswe_types, d_mission_identifiers),
packages = c("arrow", "data.table", "tidyverse"),
deployment = "main" # these are huge, so make sure this runs solo
)
)
# if configuration is to update and share on Drive, add these targets to the d
# list
if (config::get(config = general_config)$update_and_share) {
d_qa_stack <- list(
d_qa_stack,
tar_target(
name = d_check_Drive_siteSR_folder,
command = {
check_drive_parent_folder
tryCatch({
drive_auth(siteSR_config$google_email)
parent_folder <- siteSR_config$drive_project_folder
version_path <- paste0(siteSR_config$drive_project_folder,
paste0("siteSR_qa_v", b_yml$run_date, "/"))
drive_ls(version_path)
}, error = function(e) {
# if there is an error, check both the 'collated_raw' folder and the 'version'
# folder
drive_mkdir(path = parent_folder, name = paste0("siteSR_qa_v", b_yml$run_date))
})
return(version_path)
},
packages = "googledrive",
cue = tar_cue("always")
),
tar_target(
name = d_send_siteSR_files_to_drive,
command = export_single_file(file_path = d_all_sorted_Landsat_files,
drive_path = d_check_Drive_siteSR_folder,
google_email = siteSR_config$google_email),
packages = c("tidyverse", "googledrive"),
pattern = map(d_all_sorted_Landsat_files),
cue = tar_cue("always")
),
tar_target(
name = d_save_siteSR_drive_info,
command = {
drive_ids <- d_send_siteSR_files_to_drive %>%
select(name, id)
write_csv(drive_ids,
paste0("d_qa_stack/out/siteSR_qa_files_drive_ids_v",
b_yml$run_date,
".csv"))
drive_ids
},
packages = c("tidyverse", "googledrive"),
cue = tar_cue("always")
),
tar_target(
name = d_send_feather_files_to_Drive,
command = export_single_file(file_path = d_make_Landsat_feather_files,
drive_path = d_check_Drive_siteSR_folder,
google_email = siteSR_config$google_email),
packages = c("tidyverse", "googledrive"),
pattern = map(d_make_Landsat_feather_files),
cue = tar_cue("always")
),
tar_target(
name = d_save_feather_drive_info,
command = {
d_check_dir_structure
drive_ids <- d_send_feather_files_to_Drive %>%
select(name, id)
write_csv(drive_ids,
paste0("d_qa_stack/out/siteSR_Landsat_QA_feather_files_drive_ids_v",
b_yml$run_date,
".csv"))
drive_ids
},
packages = c("tidyverse", "googledrive"),
cue = tar_cue("always")
)
)
}