Skip to content

Commit

Permalink
styler clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Oct 11, 2023
1 parent 8e0b6e3 commit f11962e
Show file tree
Hide file tree
Showing 107 changed files with 2,684 additions and 2,210 deletions.
355 changes: 188 additions & 167 deletions R/bioclim_vars.R

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions R/check_coords_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
#' @return A vector of length 2 with the valid names, in the correct order
#' @keywords internal

check_coords_names <-function(data, coords){
if (is.null(coords)){
valid_names <- list(c("x","y"), c("longitude", "latitude"), c("lon","lat"),
c("X","Y"))
check_coords_names <- function(data, coords) {
if (is.null(coords)) {
valid_names <- list(
c("x", "y"), c("longitude", "latitude"), c("lon", "lat"),
c("X", "Y")
)
} else {
valid_names <- list(coords)
}
# internal function to check that both x and y names are present
check_pair <- function(valid_names, var_names){
check_pair <- function(valid_names, var_names) {
all(!is.na(match(valid_names, var_names)))
}
# find if we have any pair
valid_pair <- which(unlist(lapply(valid_names, check_pair, names(data))))
if (length(valid_pair)!=1){
if (length(valid_pair) != 1) {
stop("There are no recognised coordinate columns, set their names with 'coords'")
}

Expand Down
23 changes: 11 additions & 12 deletions R/check_times.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
#' Check multiple time variables
#'
#' Check that we only have one set of times
#' Check that we only have one set of times
#'
#' @param time_bp times in bp
#' @param time_ce times in ce
#' @param allow_null boolean whether both can be NULL
#' @returns times in bp
#' @keywords internal

check_time_vars <- function(time_bp, time_ce, allow_null=TRUE) {
if (all(!is.null(time_bp), !is.null(time_ce))){
check_time_vars <- function(time_bp, time_ce, allow_null = TRUE) {
if (all(!is.null(time_bp), !is.null(time_ce))) {
stop("both time_bp and time_ce were provide, the function can only accept one of the two")
}
if (all(is.null(time_bp), is.null(time_ce))){
if (allow_null){
if (all(is.null(time_bp), is.null(time_ce))) {
if (allow_null) {
return(NULL)
} else {
stop("either time_bp or time_ce should be provided")
}

}
if (is.null(time_ce)){
if (is.null(time_ce)) {
return(time_bp)
} else {
# convert it to bp
if (inherits(time_ce, "list")){
return(lapply(time_ce,function(x){x-1950}))
if (inherits(time_ce, "list")) {
return(lapply(time_ce, function(x) {
x - 1950
}))
} else {
return(time_ce-1950)
return(time_ce - 1950)
}


}
}
36 changes: 20 additions & 16 deletions R/clean_data_path.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Clean the data path
#' Clean the data path
#'
#' This function deletes old reconstructions that have been superseded in the
#' data_path. It assumes that the only files in data_path are part of `pastclim`
Expand All @@ -8,8 +8,8 @@
#' @returns TRUE if files are deleted successfully
#' @export

clean_data_path <- function(ask=TRUE) {
if (is.null(get_data_path(silent=TRUE))){
clean_data_path <- function(ask = TRUE) {
if (is.null(get_data_path(silent = TRUE))) {
message("The data path has not been set yet; use set_data_path() first!")
return(FALSE)
}
Expand All @@ -18,26 +18,30 @@ clean_data_path <- function(ask=TRUE) {
# current or whether it has become obsolete)
files_now <- files_now[!files_now %in% c("pastclim_dataset_list.csv")]
# keep worldclim datasets
files_now <- files_now[!substr(files_now,1,2)=="wc"]
files_now <- files_now[!substr(files_now, 1, 2) == "wc"]
# keep etopo datasets
files_now <- files_now[!substr(files_now,1,5)=="etopo"]
files_now <- files_now[!substr(files_now, 1, 5) == "etopo"]
# keep chelsa datasets
files_now <- files_now[!substr(files_now,1,6)=="chelsa"]
files_now <- files_now[!substr(files_now, 1, 6) == "chelsa"]


possible_files <- unique(getOption("pastclim.dataset_list")$file_name)
files_to_remove <- files_now[!files_now %in% possible_files]
if (length(files_to_remove)>0){
if (ask){
this_answer <- utils::menu(choices = c("yes","no"),
title = paste("The following files are obsolete:\n",
paste(files_to_remove,collapse = ", "),
"\n Do you want to delete them?"))
if (length(files_to_remove) > 0) {
if (ask) {
this_answer <- utils::menu(
choices = c("yes", "no"),
title = paste(
"The following files are obsolete:\n",
paste(files_to_remove, collapse = ", "),
"\n Do you want to delete them?"
)
)
} else { # default to delete if we are not asking
this_answer <- 1
}
if (this_answer==1){
file.remove(file.path(get_data_path(),files_to_remove))
if (this_answer == 1) {
file.remove(file.path(get_data_path(), files_to_remove))
}
} else {
message("Everything is up-to-date; no files need removing.")
Expand Down
84 changes: 41 additions & 43 deletions R/datasets_docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ NULL

#' Documentation for the Beyer2020 dataset
#'
#' This dataset covers the last 120k years, at intervals of 1/2 k years, and a
#' This dataset covers the last 120k years, at intervals of 1/2 k years, and a
#' resolution of 0.5 degrees in latitude and longitude.
#'
#'
#' If you use this dataset, make sure to cite the original publication:
#'
#' Beyer, R.M.,
Expand All @@ -35,12 +35,12 @@ NULL
#' \url{https://pubs.usgs.gov/ds/691/ds691.pdf}
#'
#' Changelog
#'
#' v1.1.0 Added monthly variables. Files can be downloaded from:
#'
#' v1.1.0 Added monthly variables. Files can be downloaded from:
#' \url{https://zenodo.org/deposit/7062281}
#'
#'
#' v1.0.0 Remove ice sheets and internal seas, and use correct formula for bio15.
#' Files can be downloaded from:
#' Files can be downloaded from:
#' \doi{doi.org/10.6084/m9.figshare.19723405.v1}
#'
#'
Expand All @@ -50,9 +50,9 @@ NULL

#' Documentation for the Krapp2021 dataset
#'
#' This dataset covers the last 800k years, at intervals of 1k years, and a
#' This dataset covers the last 800k years, at intervals of 1k years, and a
#' resolution of 0.5 degrees in latitude and longitude.
#'
#'
#' The units of several variables have been changed to match what is used
#' in WorldClim.
#'
Expand All @@ -69,14 +69,14 @@ NULL
#' \url{https://pubs.usgs.gov/ds/691/ds691.pdf}
#'
#' Changelog
#'
#'
#' v1.4.0 Change units to match WorldClim. Fix variable duplication found
#' on earlier versions of the dataset.
#'
#'
#' v1.1.0 Added monthly variables. Files can be downloaded from:
#' \url{https://zenodo.org/record/7065055}
#'
#' v1.0.0 Remove ice sheets and use correct formula for bio15.
#'
#' v1.0.0 Remove ice sheets and use correct formula for bio15.
#' Files can be downloaded from:
#' \doi{doi.org/10.6084/m9.figshare.19733680.v1}
#'
Expand All @@ -86,47 +86,47 @@ NULL

#' Documentation for the WorldClim datasets
#'
#' WorldClim version 2.1 is a database of high spatial resolution global weather and
#' WorldClim version 2.1 is a database of high spatial resolution global weather and
#' climate data, covering both the present and future projections. If you use this
#' dataset, make sure to cite the original publication:
#'
#' Fick, S.E. and R.J. Hijmans, 2017. WorldClim 2: new 1km spatial resolution
#' climate surfaces for global land areas. International Journal of Climatology 37 (12): 4302-4315.
#' Fick, S.E. and R.J. Hijmans, 2017. WorldClim 2: new 1km spatial resolution
#' climate surfaces for global land areas. International Journal of Climatology 37 (12): 4302-4315.
#' \doi{doi.org/10.1002/joc.5086}
#'
#' **Present-day reconstructions** are based on the mean for the period 1970-2000,
#'
#' **Present-day reconstructions** are based on the mean for the period 1970-2000,
#' and are available at multiple resolutions of
#' 10 arc-minutes, 5 arc-minutes, 2.5 arc-minute and 0.5 arc-minutes. The resolution
#' of interest can be obtained by changing the ending of the dataset name
#' of interest can be obtained by changing the ending of the dataset name
#' *WorldClim_2.1_RESm*, e.g. *WorldClim_2.1_10m* or *WorldClim_2.1_5m*
#' (currently, only 10m and 5m are currently available in `pastclim`). In `pastclim`, the datasets are given
#' a date of 1985 CE (the mid-point of the period of interest), corresponding to
#' a time_bp of 35. There are 19 “bioclimatic” variables, as well as monthly
#' a time_bp of 35. There are 19 “bioclimatic” variables, as well as monthly
#' estimates for minimum, mean, and maximum temperature, and precipitation.
#'
#'
#' **Future projections** are based on the models in CMIP6, downscaled and de-biased
#' using WorldClim 2.1 for the present as a baseline. Monthly values of minimum
#' temperature, maximum temperature, and precipitation, as well as 19 bioclimatic
#' variables were processed for
#' 23 global climate models (GCMs), and for four
#' 23 global climate models (GCMs), and for four
#' Shared Socio-economic Pathways (SSPs): 126, 245, 370 and 585. Model and
#' SSP can be chosen by changing the ending of the dataset name
#' *WorldClim_2.1_GCM_SSP_RESm*.
#'
#' Available values for GCM are: "ACCESS-CM2",
#' *WorldClim_2.1_GCM_SSP_RESm*.
#'
#' Available values for GCM are: "ACCESS-CM2",
#' "BCC-CSM2-MR", "CMCC-ESM2", "EC-Earth3-Veg", "FIO-ESM-2-0",
#' "GFDL-ESM4", "GISS-E2-1-G", "HadGEM3-GC31-LL", "INM-CM5-0", "IPSL-CM6A-LR",
#' "MIROC6", "MPI-ESM1-2-HR", "MRI-ESM2-0", and "UKESM1-0-LL". For SSP, use: "ssp126",
#' "ssp245", "ssp370", and "ssp585". RES takes the same values as for present reconstructions
#' (i.e. "10m", "5m", "2.5m", and "0.5m"). Example dataset names are
#' (i.e. "10m", "5m", "2.5m", and "0.5m"). Example dataset names are
#' *WorldClim_2.1_ACCESS-CM2_ssp245_10m* and *WorldClim_2.1_MRI-ESM2-0_ssp370_5m*
#'
#' The dataset are averages over 20 year
#'
#' The dataset are averages over 20 year
#' periods (2021-2040, 2041-2060, 2061-2080, 2081-2100).
#' In `pastclim`, the midpoints of the periods (2030, 2050, 2070, 2090) are used as the time stamps. All 4 periods
#' are automatically downloaded for each combination of GCM model and SSP, and are selected
#' as usual by defining the time in functions such as [region_slice()].
#'
#'
#'
#' @name WorldClim_2.1
NULL
Expand All @@ -152,33 +152,33 @@ NULL
#' derived bioclimatic variables covering the last 5 Ma (Pliocene–Pleistocene),
#' at intervals of 1,000 years, and a spatial resolution of 1
#' degrees (see Barreto et al., 2023 for details).
#'
#' PALEO-PGEM-Series is downscaled to 1° × 1° spatial resolution from the
#' outputs of the PALEO-PGEM emulator (Holden et al., 2019), which emulates
#' reasonable and extensively validated global estimates of monthly temperature
#' and precipitation for the Plio-Pleistocene every 1 kyr at a spatial
#'
#' PALEO-PGEM-Series is downscaled to 1° × 1° spatial resolution from the
#' outputs of the PALEO-PGEM emulator (Holden et al., 2019), which emulates
#' reasonable and extensively validated global estimates of monthly temperature
#' and precipitation for the Plio-Pleistocene every 1 kyr at a spatial
#' resolution of ~5° × 5° (Holden et al., 2016, 2019).
#'
#'
#' PALEO-PGEM-Series includes the mean and the standard deviation (i.e.,
#' standard error) of the emulated climate over 10 stochastic GCM emulations
#' to accommodate aspects of model uncertainty. This allows users to estimate
#' the robustness of their results in the face of the stochastic aspects of
#' to accommodate aspects of model uncertainty. This allows users to estimate
#' the robustness of their results in the face of the stochastic aspects of
#' the emulations. For more details, see Section 2.4 in Barreto et al. (2023).
#'
#'
#' Note that this is a very large dataset, with 5001 time slices. It takes
#' approximately 1 minute to set up each variable when creating a region_slice or
#' region_series. However, once the object has been created, other operations tend
#' to be much faster (especially if you subset the dataset to a small number
#' of time steps of interest).
#'
#'
#' If you use this dataset, make sure to cite the original publications:
#'
#' Barreto, E., Holden, P. B., Edwards, N. R., & Rangel, T. F. (2023).
#'
#' Barreto, E., Holden, P. B., Edwards, N. R., & Rangel, T. F. (2023).
#' PALEO-PGEM-Series: A spatial time series of the global climate over the
#' last 5 million years (Plio-Pleistocene). Global Ecology and
#' Biogeography, 00, 1– 12.
#' \doi{doi.org/10.1111/geb.13683}
#'
#'
#' Holden, P. B., Edwards, N. R., Rangel, T. F., Pereira, E. B., Tran, G. T.,
#' and Wilkinson, R. D. (2019): PALEO-PGEM v1.0: a statistical emulator of
#' Pliocene–Pleistocene climate, Geosci. Model Dev., 12, 5137–5155,
Expand All @@ -187,5 +187,3 @@ NULL
#' @name Barreto2023
NULL
#> NULL


18 changes: 9 additions & 9 deletions R/df_from_region_series.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#' Extract data frame from a region series
#'
#'
#' Extract the climatic information from a region series and organise them as
#' a data frame.
#'
#'
#' To extract a data frame from a region slice, see [df_from_region_slice()].
#'
#' @param x climate time series generated with [region_series()]
#' @param xy a boolean whether x and y coordinates should be added
#' @param xy a boolean whether x and y coordinates should be added
#' to the dataframe (default to TRUE)
#' @returns a data.frame where each cell each raster layer (i.e. timestep) is a
#' row, and the available variables are columns.
#'
#' @export

df_from_region_series <- function(x ,xy=TRUE) {
if (!is_region_series(x)){
df_from_region_series <- function(x, xy = TRUE) {
if (!is_region_series(x)) {
stop("x is not a valid object generated by region_series")
}
times_all <- time_bp(x[[1]])
for (i in seq_len(terra::nlyr(x[1]))){
x_slice <- slice_region_series(x,times_all[i])
slice_df <- terra::as.data.frame(x_slice,xy=xy)
for (i in seq_len(terra::nlyr(x[1]))) {
x_slice <- slice_region_series(x, times_all[i])
slice_df <- terra::as.data.frame(x_slice, xy = xy)
slice_df$time_bp <- times_all[i]
if (i==1){
if (i == 1) {
region_df <- slice_df
} else {
region_df <- rbind(region_df, slice_df)
Expand Down
12 changes: 6 additions & 6 deletions R/df_from_region_slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
#'
#' Extract the climatic information from a region slice and organise it as
#' a data frame. This is just a wrapper around [terra::as.data.frame()].
#'
#'
#' To extract a data frame from a region series, see [df_from_region_series()].
#'
#' @param x climate time slice (i.e. a [`terra::SpatRaster`])
#' @param x climate time slice (i.e. a [`terra::SpatRaster`])
#' generated with [region_slice()]
#' @param xy a boolean whether x and y coordinates should be added
#' @param xy a boolean whether x and y coordinates should be added
#' to the dataframe (default to TRUE)
#' @returns a data.frame where each cell the raster is a
#' row, and the available variables are columns.
#'
#' @export

df_from_region_slice <- function(x ,xy=TRUE) {
if (!inherits(x,"SpatRaster")){
df_from_region_slice <- function(x, xy = TRUE) {
if (!inherits(x, "SpatRaster")) {
stop("x is not a valid SpatRaster generated by region_slice")
}
slice_df <- terra::as.data.frame(x,xy=xy)
slice_df <- terra::as.data.frame(x, xy = xy)
return(slice_df)
}
Loading

0 comments on commit f11962e

Please sign in to comment.