generated from bluegreen-labs/R_project_template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
03_download_modis_data_spatial.R
115 lines (101 loc) · 2.22 KB
/
03_download_modis_data_spatial.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
# Download all remote sensing data using
# appeears, takes about 8h to complete
library(appeears)
library(dplyr)
library(rnaturalearth)
# grab country polygons from world map
# restrict to selected country
country <- ne_countries(
scale = 110,
returnclass = "sf"
) |>
dplyr::filter(
sovereignt %in% c("Switzerland","Germany","Austria")
) |>
sf::st_union() |>
sf::st_as_sf()
# list products to download
product_subset <- c(
"MOD09GA.061",
"MOD11A1.061",
"MYD11A1.061",
"MCD43A4.061"
)
# list appeears meta-data and subset
# only the above products
products <- appeears::rs_products() |>
filter(
ProductAndVersion %in% product_subset
)
# loop over all products and create
# basic query data
full_queries <- lapply(
products$ProductAndVersion,
function(product){
# specify the products / bands required
# grab all non-QA bands
bands <- rs_layers(product) |>
filter(
IsQA == FALSE
)
if(product != "MOD11A1.061" ) {
bands <- bands |>
filter(
grepl("refl", Layer, ignore.case = TRUE)
)
} else {
bands <- bands |>
filter(
grepl("LST_Day", Layer)
)
}
bands <- bands |>
select(
Layer
)
base_query <- bands |>
rowwise() |>
do({
data.frame(
subtask = product,
task = "spatial",
start = "2018-05-01",
end = "2018-11-01",
product = product,
layer = as.character(.$Layer)
)
})
})
tasks <- full_queries |>
bind_rows() |>
group_by(task, subtask) |>
group_split()
tasks <- lapply(
tasks, function(task){
appeears::rs_build_task(
task,
roi = country
)
})
# request the task to be executed
# don't download, just return
# the task ID / request calls
requests <- rs_request_batch(
request = tasks,
user = "khufkens",
workers = 10,
path = "data-raw/modis_data_spatial/"
)
# clean up the files which are not required
# first list all files, then select the
# ones to remove (kick those out)
downloaded_files <- list.files(
"data-raw/modis_data_spatial/",
"*",
full.names = TRUE,
recursive = TRUE
)
# files_to_remove <- downloaded_files[!grepl("results", downloaded_files)]
#
# # remove files
# file.remove(files_to_remove)