Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help page #37

Closed
wants to merge 104 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
dc562e8
preliminary internal functions
dramanica Dec 7, 2022
4ba037e
download reliefs
dramanica Dec 7, 2022
30b4bf3
remove pdf
dramanica Dec 7, 2022
8518c59
fix for terra bug on cran
dramanica Dec 7, 2022
74d5844
ideas for monthly vars
dramanica Dec 7, 2022
269bfa1
First implementation of bioclim_vars
dramanica Dec 9, 2022
5b8720d
biovars for sds
dramanica Dec 9, 2022
d2a1266
improve docs for methods
dramanica Dec 9, 2022
e58c898
Fix possible problems with relief extent
dramanica Dec 9, 2022
7eb94f3
new vignette for downscaling
dramanica Dec 9, 2022
40189e8
worldclim helper functions
dramanica Dec 10, 2022
898d9b9
initial files for ds
dramanica Dec 12, 2022
7b2a3a3
Merge branch 'cran_fixes' into ds_files
dramanica Dec 14, 2022
58dd456
move ds draft vignette
dramanica Dec 14, 2022
cb41d83
fix rounding bug for resolution
dramanica Dec 14, 2022
58d9ba8
silence idw messages
dramanica Dec 14, 2022
33ba3b7
Merge branch 'cran_fixes' into ds_files
dramanica Dec 14, 2022
3c21ca8
Update namespace
dramanica Dec 14, 2022
0ef8270
direct casting from bathy to spatraster
dramanica Dec 15, 2022
fc62ee0
Update pdf vignette
dramanica Dec 21, 2022
49c55e9
clean up old files
dramanica Dec 21, 2022
890daa2
Merge remote-tracking branch 'origin/dev' into ds_files
dramanica Mar 21, 2023
315f906
Merge remote-tracking branch 'origin/dev' into ds_files
dramanica Mar 31, 2023
dc30746
Update ds files
dramanica Apr 1, 2023
7b90a17
Clean up for ds
dramanica Apr 1, 2023
163d7f0
Merge branch 'dev' into ds_files
dramanica Apr 2, 2023
768464b
Merge branch 'dev' into ds_files
dramanica Apr 20, 2023
bd4d91f
Spell out limitations for land mask
dramanica Apr 24, 2023
62a3eed
Merge branch 'dev' into ds_files
dramanica Apr 26, 2023
c6e3dd9
Update docs
dramanica Apr 26, 2023
705f0f4
Small docs updates
dramanica Apr 26, 2023
d6e21cf
minor doc tweaks
dramanica Apr 26, 2023
db54801
Merge branch 'ds_files' of github.com:EvolEcolGroup/pastclim into ds_…
dramanica Apr 27, 2023
5540b0e
Get time_bp for region_series
dramanica Apr 27, 2023
6a9cf7d
Use dev terra
dramanica Apr 27, 2023
4af2f4d
fix time_bp<- for SpatRasterDataset
dramanica Apr 27, 2023
34613cc
fix docs
dramanica Apr 27, 2023
d12bd35
make some more functions work with series
dramanica Apr 28, 2023
b34455b
Small edits to docs
dramanica Apr 28, 2023
1bb193f
Merge pull request #27 from EvolEcolGroup/time_bp_series
dramanica Apr 28, 2023
bd2f778
bump version
dramanica Apr 28, 2023
c8c69c6
minor wording improvement
dramanica Apr 28, 2023
4576f82
Linting of vignette
dramanica Apr 28, 2023
16cef89
Readme fixes
dramanica Apr 28, 2023
2879558
Update news
dramanica Apr 28, 2023
6a6afd8
Merge branch 'dev' into ds_files
dramanica Apr 28, 2023
92ee8b1
typo
dramanica Apr 29, 2023
0013472
Prepare update_dataset_list to work on CRAN
dramanica May 1, 2023
864d0ec
clean readme
dramanica May 1, 2023
20f8877
clean readme
dramanica May 1, 2023
ea936bf
Merge branch 'dev' into ds_files
dramanica May 1, 2023
aa85766
Notes about parallelising idw
dramanica May 7, 2023
2e7597b
Some more ds functionality
dramanica May 9, 2023
9c39f13
more ds infrastructure
dramanica May 10, 2023
06db7d5
Fix temporary path problem for set_data_path
dramanica May 11, 2023
6f6b698
spelling fixes
dramanica May 11, 2023
00426ab
clean up scripts to prepare Beyer2020
dramanica May 17, 2023
dad3aba
fix missing value
dramanica May 19, 2023
c69263d
Update script for Beyer2020 packing
dramanica May 19, 2023
1d0b103
fix monthly prep
dramanica May 19, 2023
e04d552
clean up biomes
dramanica May 19, 2023
d12ed46
Updated repackage scripts for Beyer
dramanica May 20, 2023
a0f06f7
A few minor tweaks
dramanica May 20, 2023
7259a54
small fix to load_etopo
dramanica May 24, 2023
2c5f560
Clean up
dramanica Jun 5, 2023
f097996
Try to fix windows warnings
dramanica Jun 5, 2023
bf26dcf
fix error in Windows
dramanica Jun 5, 2023
abace31
Merge pull request #28 from EvolEcolGroup/clean_up
dramanica Jun 5, 2023
bfc3020
show only annual vars by default.
dramanica Jun 5, 2023
345b12f
Add worldclim present day datasets
dramanica Jun 23, 2023
32193c0
Merge pull request #29 from EvolEcolGroup/present
dramanica Jun 23, 2023
4598a07
future worldclim
dramanica Jun 23, 2023
6f47b7e
full future datasets
dramanica Jun 23, 2023
9b55657
fix vignette
dramanica Jun 23, 2023
68ab88c
future reconstructions
dramanica Jun 23, 2023
2d7b035
Update docs
dramanica Jun 24, 2023
b926082
Clean up WorldClim implementation
dramanica Jun 24, 2023
3a314c2
Merge pull request #30 from EvolEcolGroup/future
dramanica Jun 24, 2023
e2fb443
add option to use time_ce
dramanica Jun 28, 2023
fb26981
ce implementation
dramanica Jun 28, 2023
e7617ce
Minor doc edits
dramanica Jun 28, 2023
fab79ac
more ce changes
dramanica Jun 28, 2023
0c1054c
fix get_land_mask for ce
dramanica Jun 28, 2023
f5cb453
fix ce error
dramanica Jun 29, 2023
824b0fb
Small var fixes
dramanica Jun 29, 2023
feba578
Fix download future projections
dramanica Jun 29, 2023
a4cf89b
temporary fix for reloading sf if needed.
dramanica Jun 29, 2023
4709a1d
bump version number
dramanica Jun 29, 2023
3963c76
Merge pull request #32 from EvolEcolGroup/times_coords
dramanica Jun 29, 2023
c4afd02
Update vignette
dramanica Jul 2, 2023
e302a64
minor vignette update
dramanica Jul 2, 2023
d7e439f
start new vignette
dramanica Jul 9, 2023
3712d90
Avoid unloading sf
dramanica Jul 9, 2023
e748d6f
Fix warnings for WorldClim
dramanica Jul 9, 2023
ff39674
Merge pull request #34 from EvolEcolGroup/present_future
dramanica Jul 9, 2023
ab8de3d
refurbish location workings
dramanica Jul 12, 2023
0b0abfb
custom coords for location_series
dramanica Jul 12, 2023
ed1dc20
Merge branch 'location_rewrite' into dev
dramanica Jul 12, 2023
23be888
update docs for location functions
dramanica Jul 12, 2023
3939a59
For biome, use the mode when interpolating
dramanica Jul 12, 2023
144dd41
add compression to custom example
dramanica Jul 12, 2023
969b198
Fix lack of a pastclim-package alias
dramanica Aug 30, 2023
3499c4b
Fix help for cran
dramanica Aug 30, 2023
0daeafb
Bump version
dramanica Aug 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ jobs:
extra-packages: any::rcmdcheck
needs: check

# - name: Get dev terra for dev pastclim
# if: matrix.config.os == 'macos-latest' || ${{ github.base_ref == 'dev' }} || ${{ github.ref_name == 'dev' }}
# run: install.packages('terra', repos='https://rspatial.r-universe.dev')
# shell: Rscript {0}
- name: Get dev terra for dev pastclim
if: matrix.config.os == 'macos-latest' || ${{ github.base_ref == 'dev' }} || ${{ github.ref_name == 'dev' }}
run: install.packages('terra', repos='https://rspatial.r-universe.dev')
shell: Rscript {0}

- uses: r-lib/actions/check-r-package@v2
with:
Expand Down
16 changes: 8 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: pastclim
Type: Package
Title: Manipulate Time Series of Palaeoclimate Reconstructions
Version: 1.2.4
Version: 1.2.4.9005
Authors@R: c(
person("Michela", "Leonardi", role = "aut"),
person(c("Emily","Y."), "Hallet", role = "ctb"),
Expand All @@ -26,18 +26,18 @@ RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Depends:
R (>= 4.0.0),
methods,
terra (>= 1.7.18)
Imports:
curl,
methods,
ncdf4,
utils
Suggests:
rmarkdown,
knitr,
sf,
ggplot2,
testthat (>= 3.0.0),
spelling
rmarkdown,
knitr,
ggplot2,
sf,
testthat (>= 3.0.0),
spelling
VignetteBuilder: knitr
Config/testthat/edition: 3
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export("time_bp<-")
export(bioclim_vars)
export(clean_data_path)
export(climate_for_locations)
export(climate_for_time_slice)
Expand All @@ -15,24 +16,29 @@ export(get_downloaded_datasets)
export(get_ice_mask)
export(get_land_mask)
export(get_mis_time_steps)
export(get_time_bp_steps)
export(get_time_ce_steps)
export(get_time_steps)
export(get_vars_for_dataset)
export(is_region_series)
export(list_available_datasets)
export(location_series)
export(location_slice)
export(location_slice_from_region_series)
export(region_series)
export(region_slice)
export(sample_region_series)
export(sample_region_slice)
export(set_data_path)
export(set_data_path_for_CRAN)
export(slice_region_series)
export(time_bp)
export(time_series_for_locations)
export(update_dataset_list)
export(validate_nc)
export(var_labels)
exportMethods("time_bp<-")
exportMethods(bioclim_vars)
exportMethods(time_bp)
import(methods)
import(terra)
importFrom(methods,setGeneric)
22 changes: 13 additions & 9 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# development
* Improve `get_ice_mask()`, `get_land_mask()`, and `distance_from_sea()` to work
on series rather than just on slices.

# pastclim 1.2.4
* Updates on how time is handled to stay in sync with changes in `terra`.

# pastclim 1.2.3
* Added lai to Krapp2021 (the variable is now also present in the original OSF
* Added *lai* to *Krapp2021* (the variable is now also present in the original OSF
repository for that dataset).
* Change column names in data.frame returned by location_series to match
location_slice
* Allow for interpolation of nearest neighbours in location_series, and allow
for a buffer on estimates returned by the location_* functions.
* Change column names in `data.frame` returned by `location_series()` to match
`location_slice()`
* Allow for interpolation of nearest neighbours in `location_series()`, and allow
for a buffer on estimates returned by the `location_*()` functions.

# pastclim 1.2.2
* Update of Krapp2021 files to make them compatible with how terra now handles
* Update of *Krapp2021* files to make them compatible with how `terra` now handles
time. Users will have to re-download datasets. Old files can be removed with
'clean_data_path()'
`clean_data_path()`

# pastclim 1.2.1
* Small updates for CRAN submission.
Expand All @@ -34,9 +38,9 @@
still work, but will raise a warning that the functions are deprecated.

* Remove the need for `pastclimData`, we now put any data in the user dir returned
by R>=4.0.0. This removes the need of re-downloading the data when upgrading R.
by `R>=4.0.0`. This removes the need of re-downloading the data when upgrading `R`.

* Add monthly variables to Beyer2020 and Krapp2021.
* Add monthly variables to *Beyer2020* and *Krapp2021*.

# pastclim 1.0.1

Expand Down
243 changes: 243 additions & 0 deletions R/bioclim_vars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#' Compute the BIOCLIM variables
#'
#' To compute BIOCLIM variables from
#' monthly average temperature and precipitation data. For modern data,
#' this variables are generally computed using min and maximum temperature,
#' but for many palaeoclimatic reconstructions only average temperature is
#' available. Most variables, with the exception of BIO02 and BIO03, can
#' be rephrased meaningfully in terms of mean temperature.
#' This function is a modified version of \code{predicts::bcvars}.
#'
#' The variables are:
#' BIO01 = Annual Mean Temperature
#' BIO04 = Temperature Seasonality (standard deviation *100)
#' BIO05 = Max Temperature of Warmest Month
#' BIO06 = Min Temperature of Coldest Month
#' BIO07 = Temperature Annual Range (P5-P6)
#' BIO08 = Mean Temperature of Wettest Quarter
#' BIO09 = Mean Temperature of Driest Quarter
#' BIO10 = Mean Temperature of Warmest Quarter
#' BIO11 = Mean Temperature of Coldest Quarter
#' BIO12 = Annual Precipitation
#' BIO13 = Precipitation of Wettest Month
#' BIO14 = Precipitation of Driest Month
#' BIO15 = Precipitation Seasonality (Coefficient of Variation)
#' BIO16 = Precipitation of Wettest Quarter
#' BIO17 = Precipitation of Driest Quarter
#' BIO18 = Precipitation of Warmest Quarter
#' BIO19 = Precipitation of Coldest Quarter
#'
#' These summary Bioclimatic variables are after:
#' Nix, 1986. A biogeographic analysis of Australian elapid snakes. In: R. Longmore (ed.).
#' Atlas of elapid snakes of Australia. Australian Flora and Fauna Series 7.
#' Australian Government Publishing Service, Canberra.
#'
# and Expanded following the ANUCLIM manual
#'
#'
#' @param tavg monthly average temperatures
#' @param prec monthly precipitation
#' @param ... additional variables for specific methods
#' @returns the bioclim variables
#' @docType methods
#' @rdname bioclim_vars-methods
#' @importFrom methods setGeneric
#' @export

#if (!isGeneric("bioclim_vars")) {
methods::setGeneric("bioclim_vars", function(prec, tavg, ...){
methods::standardGeneric("bioclim_vars")
})

#' @rdname bioclim_vars-methods
#' @export
methods::setMethod("bioclim_vars", signature(prec="numeric", tavg="numeric"),
function(prec, tavg) {
bioclim_vars(t(as.matrix(prec)), t(as.matrix(tavg)))
}
)

#' @param filename filename where the raster can be stored.
#' @rdname bioclim_vars-methods
#' @export
methods::setMethod("bioclim_vars", signature(prec="SpatRaster", tavg="SpatRaster"),
function(prec, tavg, filename="", ...) {

if (nlyr(prec) != 12) stop("nlyr(prec) is not 12")
if (nlyr(tavg) != 12) stop("nlyr(tavg) is not 12")


x <- c(prec, tavg)
readStart(x)
on.exit(readStop(x))
nc <- ncol(x)
out <- rast(prec, nlyr=17)
names(out) = c("bio01",paste0("bio0", 4:9),
paste0("bio",10:19))
b <- writeStart(out, filename, ...)
for (i in 1:b$n) {
d <- readValues(x, b$row[i], b$nrows[i], 1, nc, TRUE, FALSE)
p <- bioclim_vars(d[,1:12], d[,13:24])
writeValues(out, p, b$row[i], b$nrows[i])
}
writeStop(out)
return(out)
}
)


#' @param filename filename where the raster can be stored.
#' @rdname bioclim_vars-methods
#' @export
methods::setMethod("bioclim_vars", signature(prec="SpatRasterDataset", tavg="SpatRasterDataset"),
function(prec, tavg, filename="", ...) {
if (!all(is_region_series(prec),is_region_series(tavg))){
"prec and tavg should be generated with region_series"
}
if (!all((nlyr(prec)[1]==nlyr(tavg)[1]),
(time_bp(prec[[1]])==time_bp(tavg[[1]])))){
stop("prec and tavg should have the same time steps")
}
time_slices <- time_bp(prec[[1]])
# loop over the time slices
for (i in 1:length(time_slices)){
prec_slice <- slice_region_series(prec, time_bp = time_slices[i])
tavg_slice <- slice_region_series(tavg, time_bp = time_slices[i])
biovars_slice<-bioclim_vars(prec_slice, tavg_slice)
# set times in years BP
time_bp(biovars_slice)<-rep(time_slices[i],terra::nlyr(biovars_slice))
if (i==1){
biovars_list<-split(biovars_slice, f=1:17)
} else {
for (x in 1:(terra::nlyr(biovars_slice))){
biovars_list[[x]] <- c(biovars_list[[x]], biovars_slice[[x]])
}
}
}
# return the variables as a SpatRasterDataset
bioclim_sds <- terra::sds(biovars_list)
names(bioclim_sds) <- c("bio01",paste0("bio0", 4:9),
paste0("bio",10:19))
varnames(bioclim_sds) <- c("bio01",paste0("bio0", 4:9),
paste0("bio",10:19))
return(bioclim_sds)
})


#' @rdname bioclim_vars-methods
#' @export
methods::setMethod("bioclim_vars", signature(prec="matrix", tavg="matrix"),
function(prec, tavg) {

if (nrow(prec) != nrow(tavg) ) {
stop("prec and tavg should have same length")
}

if (ncol(prec) != ncol(tavg)) {
stop("prec and tavg should have same number of variables (columns)")
}

# can"t have missing values in a row
nas <- apply(prec, 1, function(x){ any(is.na(x)) } )
nas <- nas | apply(tavg, 1, function(x){ any(is.na(x)) } )
p <- matrix(nrow=nrow(prec), ncol=17)
colnames(p) = c("bio01",paste0("bio0", 4:9),
paste0("bio",10:19))
if (all(nas)) { return(p) }

prec[nas,] <- NA
tavg[nas,] <- NA

window <- function(x) {
lng <- length(x)
x <- c(x, x[1:3])
m <- matrix(ncol=3, nrow=lng)
for (i in 1:3) { m[,i] <- x[i:(lng+i-1)] }
apply(m, MARGIN=1, FUN=sum)
}

# P1. Annual Mean Temperature
p[,"bio01"] <- apply(tavg,1,mean)
# P4. Temperature Seasonality (standard deviation)
p[,"bio04"] <- 100 * apply(tavg, 1, stats::sd)
# P5. Max Temperature of Warmest Period
p[,"bio05"] <- apply(tavg,1, max)
# P6. Min Temperature of Coldest Period
p[,"bio06"] <- apply(tavg, 1, min)
# P7. Temperature Annual Range (P5-P6)
p[,"bio07"] <- p[,"bio05"] - p[,"bio06"]
# P12. Annual Precipitation
p[,"bio12"] <- apply(prec, 1, sum)
# P13. Precipitation of Wettest Period
p[,"bio13"] <- apply(prec, 1, max)
# P14. Precipitation of Driest Period
p[,"bio14"] <- apply(prec, 1, min)
# P15. Precipitation Seasonality(Coefficient of Variation)
# the "1 +" is to avoid strange CVs for areas where mean rainfaill is < 1)
p[,"bio15"] <- apply(prec+1, 1, .cv)

# precip by quarter (3 months)
wet <- t(apply(prec, 1, window))
# P16. Precipitation of Wettest Quarter
p[,"bio16"] <- apply(wet, 1, max)
# P17. Precipitation of Driest Quarter
p[,"bio17"] <- apply(wet, 1, min)
tmp <- t(apply(tavg, 1, window)) / 3

if (all(is.na(wet))) {
p[,"bio08"] <- NA
p[,"bio09"] <- NA
} else {
# P8. Mean Temperature of Wettest Quarter
wetqrt <- cbind(1:nrow(p), as.integer(apply(wet, 1, which.max)))
p[,"bio08"] <- tmp[wetqrt]
# P9. Mean Temperature of Driest Quarter
dryqrt <- cbind(1:nrow(p), as.integer(apply(wet, 1, which.min)))
p[,"bio09"] <- tmp[dryqrt]
}
# P10 Mean Temperature of Warmest Quarter
p[,"bio10"] <- apply(tmp, 1, max)

# P11 Mean Temperature of Coldest Quarter
p[,"bio11"] <- apply(tmp, 1, min)

if (all(is.na(tmp))) {
p[,"bio18"] <- NA
p[,"bio19"] <- NA
} else {
# P18. Precipitation of Warmest Quarter
hot <- cbind(1:nrow(p), as.integer(apply(tmp, 1, which.max)))
p[,"bio18"] <- wet[hot]
# P19. Precipitation of Coldest Quarter
cold <- cbind(1:nrow(p), as.integer(apply(tmp, 1, which.min)))
p[,"bio19"] <- wet[cold]
}

return(p)
}
)

#' Coefficient of variables (expressed as percentage)
#'
#' R function to compute the coefficient of variation
#' (expressed as a percentage). If there is only a single value, stats::sd = NA.
#' However, one could argue that cv =0; and NA may break the code that
#' receives it. The function returns 0 if the mean is close to zero.
#'
#' This is ODD: abs to avoid very small (or zero) mean with e.g. -5:5
#'
#' @param x a vector of values
#' @returns the cv
#' @keywords internal

.cv <- function(x) {
m <- mean(abs(x))
if (is.na(m)){return(NA)}
if (m == 0) {
return(0)
} else {
return(100 * stats::sd(x) / m)
}
}


2 changes: 1 addition & 1 deletion R/check_available_datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


check_available_dataset <- function(dataset, include_custom = FALSE) {
available_datasets <- get_available_datasets()
available_datasets <- list_available_datasets()
if (include_custom) {
available_datasets <- c(available_datasets, "custom")
}
Expand Down
Loading
Loading