diff --git a/DESCRIPTION b/DESCRIPTION index e9aba15850e79489f17d740591779741ddc89ee0..22f14303bc10b08f81f80d0e3046e0609c1bfb63 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ClimDatDownloadR Type: Package Title: Downloads Climate Data from Chelsa and WorldClim -Version: 0.1.7 +Version: 0.1.7-2 Authors@R: c(person("Helge", "Jentsch", email = "helge.marc.ole.jentsch@uni-hamburg.de", role = c("aut", "cre"), @@ -38,14 +38,14 @@ License: MIT + file LICENSE Encoding: UTF-8 Depends: R (>= 3.6), terra (>= 1.7-18) Imports: curl (>= 4.3.2), httr (>= 1.4.1), ncdf4 (>= 1.17), RCurl (>= - 1.98), raster (>= 3.1-5), RefManageR (>= 1.2.12), stringr (>= - 1.4.0), sf (>= 0.9-4), sp (>= 1.4-1), utils + 1.98), RefManageR (>= 1.2.12), stringr (>= 1.4.0), sf (>= + 0.9-4), sp (>= 1.4-1), utils RoxygenNote: 7.2.3 Suggests: knitr, testthat, usethis, rmarkdown VignetteBuilder: knitr SystemRequirements: Tested with at least 4 GB RAM. NeedsCompilation: no -Packaged: 2023-06-23 12:34:01 UTC; helge +Packaged: 2023-07-05 14:07:57 UTC; helge Author: Helge Jentsch [aut, cre], Maria Bobrowski [aut], Johannes Weidinger [aut], diff --git a/NAMESPACE b/NAMESPACE index 6753522662b39ada7c5efa2c2581bacb8bab2bab..0976af3b3f3c6c701b307c758584d102cdf68bc8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,6 @@ export(Chelsa.CMIP_5.download) export(Chelsa.CMIP_6.download) export(Chelsa.CRUts.download) export(Chelsa.Clim.download) -export(Chelsa.Clim.download.deprecated) export(Chelsa.lgm.download) export(Chelsa.timeseries.download) export(WorldClim.CMIP_5.download) @@ -21,7 +20,6 @@ export(stacking.downloaded.data) import(RCurl) import(httr) import(ncdf4) -import(raster) import(sf) import(sp) import(stringr) diff --git a/R/Chelsa_Download_functions.R b/R/Chelsa_Download_functions.R index 65863267f0b1e0f9aecd7eed3c819c5fab5f6b20..f4224b54df61dd985dc7759cb05290e99d161927 100644 --- a/R/Chelsa_Download_functions.R +++ b/R/Chelsa_Download_functions.R @@ -34,11 +34,10 @@ #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' #' -#'@export Chelsa.Clim.download.deprecated<- function(save.location = "./", parameter = c("prec", "temp", "tmax", "tmin", "bio"), bio.var = c(1:19), @@ -181,15 +180,14 @@ Chelsa.Clim.download.deprecated<- function(save.location = "./", quiet = FALSE, cacheOK = FALSE) if(i != "prec"){ - raster.temp <- raster(dest.temp) - + raster.temp <- terra::rast(dest.temp) gc() - raster.temp <- clamp(raster.temp, lower = -1000, + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) gc() - writeRaster(raster.temp, + terra::writeRaster(raster.temp, dest.temp, overwrite = TRUE) rm(raster.temp) @@ -313,29 +311,29 @@ Chelsa.Clim.download.deprecated<- function(save.location = "./", quiet = FALSE) if(bio <= 11){ - raster.temp <- raster(dest.temp) + raster.temp <- terra::rast(dest.temp) # raster.values <- values(raster.temp) # raster.values[raster.values==-32768] <- NA # values(raster.temp) <- raster.values # rm(raster.values) gc() - raster.temp <- clamp(raster.temp, lower = -1000, + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) - gain(raster.temp) <- 0.1 - writeRaster(raster.temp, + raster.temp <- process.raster.int.doub(raster.temp) + terra::writeRaster(raster.temp, dest.temp, overwrite = TRUE) rm(raster.temp) gc() }else{ - raster.temp <- raster(paste0(temp.temp.save.location, "CHELSA_", + raster.temp <- terra::rast(paste0(temp.temp.save.location, "CHELSA_", i, "_", bio, vers, ".tif")) gc() - raster.temp <- clamp(raster.temp, lower = -1000, + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) gc() - writeRaster(raster.temp, + terra::writeRaster(raster.temp, paste0(temp.temp.save.location, "CHELSA_", i, "_", bio, vers, ".tif"), overwrite = TRUE) @@ -471,7 +469,7 @@ Chelsa.Clim.download.deprecated<- function(save.location = "./", #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@import httr #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' @@ -596,15 +594,15 @@ Chelsa.CMIP_5.download <- function(save.location = "./", quiet = FALSE) if(i != "prec"){ gc() - raster.temp <- raster(dest.temp) - raster.temp <- clamp(raster.temp, lower = -1000, + raster.temp <- terra::rast(dest.temp) + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) gc() - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(dest.temp) gc() - writeRaster(raster.temp, - dest.temp, - overwrite = TRUE) + terra::writeRaster(x = raster.temp, + filename = dest.temp, + overwrite = TRUE) rm(raster.temp) gc() } @@ -675,22 +673,22 @@ Chelsa.CMIP_5.download <- function(save.location = "./", mode = 'wb', quiet = FALSE) - raster.temp <- raster(dest.temp) + raster.temp <- terra::rast(dest.temp) # raster.values <- values(raster.temp) # raster.values[raster.values==-32768] <- NA # values(raster.temp) <- raster.values # rm(raster.values); gc() - raster.temp <- clamp(raster.temp, + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) gc() if(bio <= 11){ gc() - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) } - writeRaster(raster.temp, - dest.temp, - overwrite = TRUE) + terra::writeRaster(x = raster.temp, + filename = dest.temp, + overwrite = TRUE) rm(raster.temp) gc() }else{ @@ -806,7 +804,7 @@ Chelsa.CMIP_5.download <- function(save.location = "./", #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' #' @@ -937,30 +935,30 @@ Chelsa.lgm.download <- function(save.location = "./", mode = 'wb', quiet = FALSE) if(i != "prec"){ - raster.temp <- raster(dest.temp) + raster.temp <- terra::rast(dest.temp) - raster.temp <- clamp(raster.temp, lower = -1000, useValues= FALSE) + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues= FALSE) gc() # Conversion Float - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) # umrechnung Kelvin - Celsius gc() - offs(raster.temp) <- -273.15 - - writeRaster(raster.temp, - dest.temp, + raster.temp <- process.raster.offset(raster.layer= raster.temp) + terra::writeRaster(x = raster.temp, + filename = dest.temp, overwrite = TRUE) rm(raster.temp) gc() }else{ # for precipitation as http://chelsa-climate.org/last-glacial-maximum-climate/ says - raster.temp <- raster(dest.temp) - raster.temp <- clamp(raster.temp, upper = 30000, useValues= FALSE) + raster.temp <- terra::rast(dest.temp) + raster.temp <- terra::clamp(raster.temp, upper = 30000, useValues= FALSE) gc() - gain(raster.temp) <- 0.1 - writeRaster(raster.temp, - dest.temp, + raster.temp <- process.raster.int.doub(raster.temp) + + terra::writeRaster(x = raster.temp, + filename = dest.temp, overwrite = TRUE) rm(raster.temp) gc() @@ -1033,17 +1031,17 @@ Chelsa.lgm.download <- function(save.location = "./", mode = 'wb', quiet = FALSE) # Casting into floats and deleting NA values - raster.temp <- raster(dest.temp) - raster.temp <- clamp(raster.temp, lower = -1000, useValues= FALSE) + raster.temp <- terra::rast(dest.temp) + raster.temp <- terra::clamp(raster.temp, upper = -1000, useValues= FALSE) gc() if(bio <= 11){ # values(raster.temp) <- as.numeric(values(raster.temp)/10) - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) } - writeRaster(raster.temp, - dest.temp, - overwrite = TRUE) + terra::writeRaster(x = raster.temp, + filename = dest.temp, + overwrite = TRUE) rm(raster.temp) gc() }else{ @@ -1163,7 +1161,7 @@ Chelsa.lgm.download <- function(save.location = "./", #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@import httr #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' @@ -1303,22 +1301,16 @@ Chelsa.timeseries.download <- function(save.location = "./", if(i != "prec"){ - raster.temp <- raster(dest.file) - # raster.values <- values(raster.temp) - # raster.values[raster.values==-32768] <- NA - # values(raster.temp) <- raster.values - # rm(raster.values) + raster.temp <- terra::rast(dest.file) - raster.temp <- clamp(raster.temp, lower = -1000, useValues = FALSE) + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) gc() - # values(raster.temp) <- as.numeric(values(raster.temp)/10) - # values(raster.temp) <- as.numeric(values(raster.temp)-273.15) - gain(raster.temp) <- 0.1 - offs(raster.temp) <- -273.15 + raster.temp <- process.raster.int.doub(raster.temp) + raster.temp <- process.raster.offset(raster.layer = raster.temp) - writeRaster(raster.temp, - dest.file, + terra::writeRaster(x = raster.temp, + filename = dest.file, overwrite = TRUE) rm(raster.temp) gc() @@ -1432,7 +1424,7 @@ Chelsa.timeseries.download <- function(save.location = "./", #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@import httr #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' @@ -1592,11 +1584,11 @@ Chelsa.CRUts.download <- function(save.location = "./", mustWork = T), "/temp_gdalwarped.tif") # gdalUtils::gdalwarp(dest.temp, raster.temp.file.dest) - raster.temp <- raster(raster.temp.file.dest) - raster.temp <- clamp(raster.temp, lower = -1000, useValues = FALSE) - gain(raster.temp) <- 0.1 - writeRaster(raster.temp, - dest.temp, + raster.temp <- terra::rast(raster.temp.file.dest) + raster.temp <- terra::clamp(raster.temp, lower = -1000, useValues = FALSE) + raster.temp <- process.raster.int.doub(raster.temp) + terra::writeRaster(x = raster.temp, + filename = dest.temp, overwrite = TRUE) rm(raster.temp) gc() diff --git a/R/WorldClim_download_functions.R b/R/WorldClim_download_functions.R index 4389b860de7c771ad6234c2418425e87c38f0eae..d2272a0e5e1eaac108cab81569b496b2f38ea5d0 100644 --- a/R/WorldClim_download_functions.R +++ b/R/WorldClim_download_functions.R @@ -288,43 +288,42 @@ WorldClim.HistClim.download <- function(save.location = "./", for (month in month.var) { if(parm.temp != "prec"){ if(res.temp != "30s"){ - raster.temp <- raster( + raster.temp <- terra::rast( list.files(path = temp.temp.save.location, pattern = paste0(parm.temp, month,".bil"), full.names = T) ) }else{ - raster.temp <- raster( + raster.temp <- terra::rast( list.files(path = temp.temp.save.location, pattern = paste0(parm.temp, "_", month,".bil"), full.names = T) ) } gc() - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) gc() }else{ if(res.temp != "30s"){ - raster.temp <- raster( + raster.temp <- terra::rast( list.files(path = temp.temp.save.location, pattern = paste0(parm.temp, month,".bil"), full.names = T) ) }else{ - raster.temp <- raster( + raster.temp <- terra::rast( list.files(path = temp.temp.save.location, pattern = paste0(parm.temp, "_", month,".bil"), full.names = T) ) } } - writeRaster(raster.temp, + terra::writeRaster(raster.temp, filename = paste0(temp.temp.save.location, "WC_",vers, "_", res, "_", parm.temp, "_", str_pad(c(1:12)[month], 2, 'left', pad = "0"), ".tif"), - format = "GTiff", overwrite = TRUE) } unlink(c(list.files(temp.temp.save.location, pattern = ".bil", @@ -333,7 +332,7 @@ WorldClim.HistClim.download <- function(save.location = "./", full.names = TRUE))) }else{ for (bio in bio.var) { - raster.temp <- raster( + raster.temp <- terra::rast( list.files(temp.temp.save.location, pattern = paste0(parm.temp, bio, ".bil"), full.names = T) @@ -341,22 +340,21 @@ WorldClim.HistClim.download <- function(save.location = "./", if(bio <= 11){ if(res.temp != "30s"){ gc() - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) gc() }else{ # crop gc() - gain(raster.temp) <- 0.1 + raster.temp <- process.raster.int.doub(raster.temp) gc() } } - writeRaster(raster.temp, + terra::writeRaster(raster.temp, filename = paste0(temp.temp.save.location, "WC_",vers, "_", res, "_", parm.temp, "_", str_pad(c(1:19)[bio], 2, 'left', pad = "0"), ".tif"), - format = "GTiff", overwrite = TRUE) } unlink(c(list.files(temp.temp.save.location, pattern = ".bil", @@ -623,7 +621,7 @@ WorldClim.HistClim.download <- function(save.location = "./", #'@importFrom curl curl_fetch_memory #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@importFrom utils unzip download.file setTxtProgressBar txtProgressBar #' #'@export @@ -895,22 +893,22 @@ WorldClim.CMIP_5.download <- function(save.location = "./", if(parm.temp != "prec"){ for (file.conversion.num in 1:length(keep.files)) { if(res.temp != "30s"){ - raster.temp <- raster(keep.files[file.conversion.num]) + raster.temp <- terra::rast(keep.files[file.conversion.num]) gc() - gain(raster.temp) <- .1 - writeRaster(x = raster.temp, + raster.temp <- process.raster.int.doub(raster.temp) + terra::writeRaster(x = raster.temp, filename = keep.files[file.conversion.num], overwrite = TRUE) rm(raster.temp) gc() }else{ - raster.temp <- raster(keep.files[file.conversion.num]) + raster.temp <- terra::rast(keep.files[file.conversion.num]) # crop gc() - gain(raster.temp) <- .1 + raster.temp <- process.raster.int.doub(raster.temp) gc() # save - writeRaster(x = raster.temp, + terra::writeRaster(x = raster.temp, filename = keep.files[file.conversion.num], overwrite = TRUE) rm(raster.temp) @@ -960,22 +958,19 @@ WorldClim.CMIP_5.download <- function(save.location = "./", print(file.conversion.temp) if(file.conversion.temp <= 11){ if(res.temp != "30s"){ + raster.temp <- terra::rast(keep.files[file.conversion.num]) gc() - raster.temp <- raster(keep.files[file.conversion.num]) - gain(raster.temp) <- .1 - writeRaster(x = raster.temp, + raster.temp <- process.raster.int.doub(raster.temp) + terra::writeRaster(x = raster.temp, filename = keep.files[file.conversion.num], overwrite = TRUE) rm(raster.temp) gc() }else{ - raster.temp <- raster(keep.files[file.conversion.num]) - # crop - gc() - gain(raster.temp) <- .1 + raster.temp <- terra::rast(keep.files[file.conversion.num]) gc() - # save - writeRaster(x = raster.temp, + raster.temp <- process.raster.int.doub(raster.temp) + terra::writeRaster(x = raster.temp, filename = keep.files[file.conversion.num], overwrite = TRUE) rm(raster.temp) @@ -1123,7 +1118,7 @@ WorldClim.CMIP_5.download <- function(save.location = "./", #'@import stringr #'@import RCurl #'@import ncdf4 -#'@import raster +#'@import terra #'@importFrom utils download.file unzip setTxtProgressBar txtProgressBar #'@importFrom curl curl_fetch_memory @@ -1197,11 +1192,6 @@ WorldClim.CMIP_6.download <- function(save.location = "./", # if(!is.numeric(bio.var)) stop() # bio.var <- str_pad(bio.var, 2, 'left', pad = "0") } - if(is.element("30s", resolution)){ - warning("Data at 30-seconds spatial resolution is expected to be available soon.", call. = TRUE, immediate. = TRUE) - } - # analog the elevation can be regarded. - # First: Set URL Root -------------------------------------------------------- # https://geodata.ucdavis.edu/cmip6/30s/ACCESS-CM2/ssp126/wc2.1_30s_tmin_ACCESS-CM2_ssp126_2021-2040.tif @@ -1242,14 +1232,6 @@ WorldClim.CMIP_6.download <- function(save.location = "./", "30s" = "30s", next ) - # if(res.temp == "30s") { - # warning( - # # "Processing might take a while since 30s resolution is downloaded for the whole world", - # "30 second data is not yet available. Please check WorldClim website. Skipping 30s data!", - # call. = TRUE, - # immediate. = TRUE) - # next - # } URL.2 <- paste0(res.temp, "/") # Forth: Global Circulation Model -------------------------------------- for(gcm in model.var){ @@ -1282,9 +1264,18 @@ WorldClim.CMIP_6.download <- function(save.location = "./", URL.3, ".tif") # check if URL is available # if(url.exists(URL.temp)){ - if(RCurl::url.exists(url = URL.temp)){ + # if(RCurl::urlExists(url = URL.temp)){ + URLstatus <- curl::curl_fetch_memory(url = URL.temp)$status + if(URLstatus == 200){ # clear up the temporary directory unlink(list.files(tempdir(), recursive = T, full.names=T)) + if(res.temp == "30s"){ + options(timeout = max(2400, getOption("timeout"))) + }else{ + if(getOption("timeout") != 60){ + options(timeout = max(60, getOption("timeout"))) + } + } # download file to save location download.file(url = URL.temp, destfile = dest.temp, diff --git a/build/vignette.rds b/build/vignette.rds index d21735aa51ea825212c5d138535de4e413ab1afa..5cf6f94285e0877160cddd496bcf89146c0336f4 100644 Binary files a/build/vignette.rds and b/build/vignette.rds differ diff --git a/inst/doc/ClimDatDownloadR.html b/inst/doc/ClimDatDownloadR.html index 011fb535ead0bf281c9c079ff052d4f4e9e95bbd..feb20ea7dbf307b224299f2782994594ab36431b 100644 --- a/inst/doc/ClimDatDownloadR.html +++ b/inst/doc/ClimDatDownloadR.html @@ -353,7 +353,15 @@ first, if not already done. Then you can activate the package with the <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># install.packages("ClimDatDownloadR", dependencies = TRUE)</span></span> <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ClimDatDownloadR)</span> <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> Loading required package: terra</span></span> -<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> terra 1.7.23</span></span></code></pre></div> +<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> terra 1.7.39</span></span> +<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,</span></span> +<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> which was just loaded, will retire in October 2023.</span></span> +<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Please refer to R-spatial evolution reports for details, especially</span></span> +<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> https://r-spatial.org/r/2023/05/15/evolution4.html.</span></span> +<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> It may be desirable to make the sf package available;</span></span> +<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> package maintainers should consider adding sf to Suggests:.</span></span> +<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> The sp package is now running under evolution status 2</span></span> +<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> (status 2 uses the sf package in place of rgdal)</span></span></code></pre></div> <p>Very well, now that you have the package installed and attached, let’s start with the data sets of the climatologies of Chelsa and WorldClim.</p>