Skip to content

Commit

Permalink
cran checks
Browse files Browse the repository at this point in the history
  • Loading branch information
VeruGHub committed Jul 10, 2023
1 parent 37254cf commit 3e06ffa
Show file tree
Hide file tree
Showing 26 changed files with 187 additions and 159 deletions.
3 changes: 2 additions & 1 deletion R/build_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#
# Build the url to download climatic data from ftp://palantir.boku.ac.at/Public/ClimateData/
#
# @param climatic_var Character. Climatic variable to be downloaded. One of 'Tmax', 'Tmin' or 'Prcp'.
# @param climatic_var Character. Climatic variable to be downloaded. One of 'Tmax',
# 'Tmin' or 'Prcp'.
# @param year Numeric. Year to download climatic information
#
# @return text string with the url
Expand Down
7 changes: 4 additions & 3 deletions R/check_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#'
#' @return TRUE if the server seems available, FALSE otherwise.
#'
#' @details This function checks access to the latest version of the climatic dataset
#' (version 4).
#' @details This function checks access to the latest version of the climatic
#' dataset (version 4).
#'
#' @export
#'
Expand Down Expand Up @@ -53,7 +53,8 @@ check_server <- function(climatic_var = NULL,
data.ok <- try(
R.utils::withTimeout({
suppressMessages(
get_daily_climate_single(coords, climatic_var, paste0(year, "-01-01"), check_conn = FALSE))
get_daily_climate_single(coords, climatic_var, paste0(year, "-01-01"),
check_conn = FALSE))
},
timeout = 30, # allow 30 seconds to download this single data point
onTimeout = "silent"), # if time out, return NULL
Expand Down
41 changes: 23 additions & 18 deletions R/get_daily_climate.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@

#' Get daily data for multiple climatic variables
#'
#' Extract daily climate data (temperature and precipitation) for a given set of points or polygons within Europe.
#' Extract daily climate data (temperature and precipitation) for a given set of
#' points or polygons within Europe.
#'
#'
#' @param coords A [matrix], [data.frame], [tibble::tbl_df-class], [sf::sf()], or [terra::SpatVector()] object
#' containing point or polygon coordinates in decimal degrees (lonlat/geographic format).
#' Longitude must fall between -40.5 and 75.5 degrees, and latitude between 25.5 and 75.5 degrees.
#' @param coords A [matrix], [data.frame], [tibble::tbl_df-class], [sf::sf()], or
#' [terra::SpatVector()] object containing point or polygon coordinates in decimal
#' degrees (lonlat/geographic format). Longitude must fall between -40.5 and 75.5
#' degrees, and latitude between 25.5 and 75.5 degrees.
#' If `coords` is a matrix, it must have only two columns: the first with longitude
#' and the second with latitude data.
#' If `coords` is a data.frame or a tbl_df, it must contain at least two columns called `lon` and `lat`
#' with longitude and latitude coordinates, respectively.
#' @param climatic_var Character. Climatic variables to be downloaded ('Tmax', 'Tmin' or 'Prcp').
#' Various elements can be concatenated in the vector.
#' If `coords` is a data.frame or a tbl_df, it must contain at least two columns
#' called `lon` and `lat` with longitude and latitude coordinates, respectively.
#' @param climatic_var Character. Climatic variables to be downloaded ('Tmax',
#' 'Tmin' or 'Prcp'). Various elements can be concatenated in the vector.
#' @param period Either numbers (representing years between 1950 and 2022),
#' or dates in "YYYY-MM-DD" format (to obtain data for specific days).
#' To specify a sequence of years or dates use the format 'start:end' (e.g. YYYY:YYYY or "YYYY-MM-DD:YYYY-MM-DD", see examples).
#' Various elements can be concatenated in the vector
#' (e.g. c(2000:2005, 2010:2015, 2020), c("2000-01-01:2000-01-15", "2000-02-01"))
#' @param output Character. Either "df", which returns a dataframe with daily climatic values
#' for each point/polygon, or "raster", which returns [terra::SpatRaster()] objects (within a list when more than one
#' climatic variable is downloaded).
#' @param version Numeric. Version of the climate data. It uses the latest version (4) by default.
#' The former version (3) is also available, for the sake of reproducibility.
#' To specify a sequence of years or dates use the format 'start:end'
#' (e.g. YYYY:YYYY or "YYYY-MM-DD:YYYY-MM-DD", see examples). Various elements
#' can be concatenated in the vector (e.g. c(2000:2005, 2010:2015, 2020),
#' c("2000-01-01:2000-01-15", "2000-02-01"))
#' @param output Character. Either "df", which returns a dataframe with daily
#' climatic values for each point/polygon, or "raster", which returns [terra::SpatRaster()]
#' objects (within a list when more than one climatic variable is downloaded).
#' @param version Numeric. Version of the climate data. It uses the latest version (4)
#' by default. The former version (3) is also available, for the sake of reproducibility.
#' See 'references' for details on the climatic data sets.
#' @param check_connection Logical. Check the connection to the server before attempting data download?
#' @param check_connection Logical. Check the connection to the server before
#' attempting data download?
#'
#' @return Either:
#' - A data.frame (if output = "df")
#' - A [terra::SpatRaster()] object (if output = "raster")
#' - A list of [terra::SpatRaster()] objects (if output = "raster" and there is more than one `climatic_var`).
#' - A list of [terra::SpatRaster()] objects (if output = "raster" and there is
#' more than one `climatic_var`).
#'
#' @export
#'
Expand Down
35 changes: 23 additions & 12 deletions R/get_daily_climate_single.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

#' Get daily data for one climatic variable
#'
#' Extract daily climate data (temperature or precipitation) for a given set of points or polygons within Europe.
#' Extract daily climate data (temperature or precipitation) for a given set of
#' points or polygons within Europe.
#'
#' @param climatic_var_single Character. Climatic variable to be downloaded. One of 'Tmax', 'Tmin' or 'Prcp'.
#' @param output Character. Either "df", which returns a dataframe with daily climatic values
#' for each point/polygon, or "raster", which returns a [terra::SpatRaster()] object.
#' @param check_conn Logical. Check the data connection before attempting to download the data?
#' @param climatic_var_single Character. Climatic variable to be downloaded.
#' One of 'Tmax', 'Tmin' or 'Prcp'.
#' @param output Character. Either "df", which returns a dataframe with daily
#' climatic values for each point/polygon, or "raster", which returns a
#' [terra::SpatRaster()] object.
#' @param check_conn Logical. Check the connection to the server before
#' attempting data download?
#' @inheritParams get_daily_climate
#'
#' @return A data.frame (if output = "df") or a [terra::SpatRaster()] object (if output = "raster").
#' @return A data.frame (if output = "df") or a [terra::SpatRaster()] object
#' (if output = "raster").
#'
#' @keywords internal
#' @noRd
Expand Down Expand Up @@ -49,7 +54,8 @@ get_daily_climate_single <- function(coords = NULL,
}

if (climatic_var_single %in% names(coords)) {
stop("Coords cannot have a column with the same name as ", climatic_var_single, ". Please change it.")
stop("Coords cannot have a column with the same name as ", climatic_var_single,
". Please change it.")
}

## coords
Expand Down Expand Up @@ -96,7 +102,8 @@ get_daily_climate_single <- function(coords = NULL,

if (terra::geomtype(coords.spatvec) == "polygons") {

if (sum(suppressWarnings(terra::expanse(coords.spatvec, unit = "km"))) > 10000) { # change limits if needed
if (sum(suppressWarnings(terra::expanse(coords.spatvec, unit = "km"))) >
10000) { # change limits if needed
stop("Asking for climate data for too large area. Please reduce the polygon area or download original rasters directly from ftp://palantir.boku.ac.at/Public/ClimateData/ so as not to saturate the server")
}

Expand Down Expand Up @@ -172,7 +179,8 @@ get_daily_climate_single <- function(coords = NULL,

#### Extract ####

message(paste0("\nDownloading ", climatic_var_single, " data... This process might take several minutes"))
message(paste0("\nDownloading ", climatic_var_single,
" data... This process might take several minutes"))

if (output == "df") {

Expand All @@ -190,14 +198,16 @@ get_daily_climate_single <- function(coords = NULL,
out <- subset(out, select = -c(lon, lat))
spatvec.coords <- terra::crds(coords.spatvec, df = TRUE)
names(spatvec.coords) <- c("lon", "lat")
coords.spatvec.df <- data.frame(terra::as.data.frame(coords.spatvec), spatvec.coords)
coords.spatvec.df <- data.frame(terra::as.data.frame(coords.spatvec),
spatvec.coords)
}

out <- merge(coords.spatvec.df, out, by.x = "ID_coords", by.y = "ID", all = TRUE)

## Rasters codify NA as very negative values (-32768).
# So, if value <-9000, it is NA
out[, climatic_var_single] <- ifelse(out[, climatic_var_single] < -9000, NA, out[, climatic_var_single])
out[, climatic_var_single] <- ifelse(out[, climatic_var_single] < -9000, NA,
out[, climatic_var_single])

## Real climatic values
out[,climatic_var_single] <- out[,climatic_var_single]/100
Expand Down Expand Up @@ -307,7 +317,8 @@ reshape_terra_extract <- function(df.wide, climvar) {

df.long <- stats::reshape(df.wide, direction = "long",
idvar = c("ID", "lon", "lat"),
varying = names(df.wide)[!names(df.wide) %in% c("ID", "lon", "lat")],
varying = names(df.wide)[!names(df.wide) %in%
c("ID", "lon", "lat")],
timevar = "date")

df.long <- df.long[order(df.long$ID, df.long$date), ]
Expand Down
5 changes: 1 addition & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->

[![Project Status: Active -- The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![R-CMD-check](https://github.com/VeruGHub/easyclimate/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/VeruGHub/easyclimate/actions/workflows/R-CMD-check.yaml)
[![HitCount](https://hits.dwyl.com/VeruGHub/easyclimate.svg?style=flat-square)](https://hits.dwyl.com/VeruGHub/easyclimate)
[![HitCount: unique users](https://hits.dwyl.com/VeruGHub/easyclimate.svg?style=flat-square&show=unique)](https://hits.dwyl.com/VeruGHub/easyclimate)
<!-- [![Codecov test coverage](https://codecov.io/gh/VeruGHub/easyclimate/branch/master/graph/badge.svg)](https://codecov.io/gh/VeruGHub/easyclimate?branch=master) -->
[![Project Status: Active -- The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![R-CMD-check](https://github.com/VeruGHub/easyclimate/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/VeruGHub/easyclimate/actions/workflows/R-CMD-check.yaml) [![HitCount](https://hits.dwyl.com/VeruGHub/easyclimate.svg?style=flat-square)](https://hits.dwyl.com/VeruGHub/easyclimate) [![HitCount: unique users](https://hits.dwyl.com/VeruGHub/easyclimate.svg?style=flat-square&show=unique)](https://hits.dwyl.com/VeruGHub/easyclimate) <!-- [![Codecov test coverage](https://codecov.io/gh/VeruGHub/easyclimate/branch/master/graph/badge.svg)](https://codecov.io/gh/VeruGHub/easyclimate?branch=master) -->

<!-- badges: end -->

Expand Down
38 changes: 19 additions & 19 deletions docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3e06ffa

Please sign in to comment.