-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaleoclim.R
More file actions
202 lines (179 loc) · 6.36 KB
/
Copy pathpaleoclim.R
File metadata and controls
202 lines (179 loc) · 6.36 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
#' Retrieve data from PaleoClim
#'
#' Downloads data from PaleoClim (<http://www.paleoclim.org>) and loads it into R
#' as a `SpatRaster` object.
#'
#' @param period Character. Time period to retrieve.
#' @param resolution Character. Resolution to retrieve.
#' @param region `SpatExtent` object or object that can be coerced to
#' `SpatExtent` (see [terra::ext()]), describing the
#' region to be retrieved. If `NULL`, defaults to the whole
#' globe.
#' @param as Character. `as = "raster"` returns a `RasterStack` object
#' (see [raster::stack()]) instead of the default raster from
#' the `terra` package. It is provided for backwards
#' compatibility and will be removed in future versions.
#' Requires the `raster` package.
#' @param skip_cache Logical. If `TRUE`, cached data will be ignored.
#' @param cache_path Logical. Path to directory where downloaded files should
#' be saved. Defaults to R's temporary directory.
#' @param quiet Logical. If `TRUE`, suppresses messages and download
#' progress information.
#'
#' @details
#' See <http://www.paleoclim.org> for details of the datasets and codings.
#' Data at 30s resolution is only available for 'cur' and 'lgm'.
#'
#' By default, `paleoclim()` will read previously downloaded files in R's
#' temporary directory if available. Use `skip_cache = TRUE` to override this.
#' `cache_path` can also be set to another directory. This can be useful if you
#' want to reuse downloaded data between sessions.
#'
#' @return
#' `SpatRaster` object (see [terra::rast()]) with each bioclimatic variable
#' as a separate named layer.
#'
#' @export
#'
#' @examplesIf interactive() && curl::has_internet()
#' paleoclim("lh", "10m")
paleoclim <- function(period = c("lh", "mh", "eh", "yds", "ba", "hs1",
"lig", "mis19", "mpwp", "m2", "cur", "lgm"),
resolution = c("10m", "5m", "2_5m", "30s"),
region = NULL,
as = c("terra", "raster"),
skip_cache = FALSE,
cache_path = fs::path_temp(),
quiet = FALSE) {
period <- rlang::arg_match(period)
resolution <- rlang::arg_match(resolution)
as <- rlang::arg_match(as)
if (resolution == "30s" & !period %in% c("cur", "lgm")) {
rlang::abort("Data at 30s resolution is only available for 'cur' and 'lgm'")
}
url <- construct_paleoclim_url(period, resolution)
tmpfile <- fs::path(cache_path, fs::path_file(url))
if (isFALSE(interactive())) quiet <- TRUE
# Download
if (!fs::file_exists(tmpfile) | isTRUE(skip_cache)) {
raster <- download_paleoclim(url, tmpfile, as, quiet)
}
# Or read from cache
else {
if (!isTRUE(quiet)) {
rlang::inform(
paste0("Reading cached PaleoClim data from ", tmpfile),
body = c(
i = "Use `skip_cache = TRUE` to force redownload."
)
)
}
raster <- load_paleoclim(tmpfile, as)
}
if (!is.null(region)) {
raster <- terra::crop(raster, region)
}
return(raster)
}
#' Construct PaleoClim URL
#'
#' @param period Character. PaleoClim period code.
#' @param resolution Character. PaleoClim resolution.
#'
#' @noRd
#' @keywords internal
construct_paleoclim_url <- function(period, resolution) {
base_url <- ("http://sdmtoolbox.org/paleoclim.org/data/")
if (period %in% c("mpwp", "m2")) {
subdir <- paste0(period, "/")
}
else if (period == "cur") {
subdir <- "chelsa_cur/"
}
else if (period == "lgm") {
subdir <- "chelsa_LGM/"
}
else {
subdir <- paste0(toupper(period), "/")
}
if (period %in% c("mis19", "m2")) {
file <- paste0(toupper(period), "_v1_r", resolution, ".zip")
}
else if (period == "mpwp") {
file <- paste0("mPWP_v1_r", resolution, ".zip")
}
else if (period == "cur") {
file <- paste0("CHELSA_cur_V1_2B_r", resolution, ".zip")
}
else if (period == "lgm") {
file <- paste0("chelsa_LGM_v1_2B_r", resolution, ".zip")
}
else {
file <- paste0(toupper(period), "_v1_", resolution, ".zip")
}
url <- paste0(base_url, subdir, file)
return(url)
}
#' Download file from PaleoClim
#'
#' If possible; otherwise 'fail gracefully' by returning NA with a warning.
#'
#' @noRd
#' @keywords internal
download_paleoclim <- function(url, tmpfile, as, quiet = FALSE) {
res <- curl::curl_fetch_disk(
url,
tmpfile,
handle = curl::new_handle(timeout = max(3600, getOption("timeout")))
)
httr::warn_for_status(res$status_code)
if (res$status_code == 200) load_paleoclim(tmpfile, as)
else NA
}
#' Load data from PaleoClim
#'
#' Loads a PaleoClim data file (`.zip` format) into R as a `SpatRaster`.
#'
#' @param file Character. Path to a *.zip file downloaded from PaleoClim.
#' @param as Character. `as = "raster"` returns a `RasterStack` object
#' (see [raster::stack()]) instead of the default raster from
#' the `terra` package. It is provided for backwards
#' compatibility and will be removed in future versions.
#' Requires the `raster` package.
#'
#' @return
#' `SpatRaster` object (see [terra::rast()]) with each bioclimatic variable
#' as a separate named layer.
#'
#' @export
#'
#' @examples
#' file <- system.file("testdata", "LH_v1_10m_cropped.zip",
#' package = "rpaleoclim")
#' load_paleoclim(file)
load_paleoclim <- function(file, as = c("terra", "raster")) {
as <- rlang::arg_match(as)
tmpdir <- fs::file_temp("paleoclim_")
utils::unzip(file, exdir = tmpdir)
tifs <- fs::dir_ls(tmpdir, recurse = TRUE, glob = "*.tif")
names(tifs) <- fs::path_ext_remove(fs::path_file(tifs))
if (length(tifs) > 0) raster <- terra::rast(tifs)
else raster <- terra::rast()
if (as == "raster" ) {
if (!requireNamespace("raster", quietly = TRUE)) {
rlang::abort(
'`as = "raster"` requires package `raster`',
class = "rpaleoclim_missing_package"
)
}
rlang::warn(
'`as = "raster"` is deprecated and will be removed in future versions of rpaleoclim',
"rpaleoclim_raster_deprecation",
.frequency = "once",
.frequency_id = "rpaleoclim_raster_deprecation"
)
raster <- lapply(as.list(raster), raster::raster)
raster <- raster::stack(raster)
}
return(raster)
}