Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^\README.Rmd$
^dev_history\.R$
^\.github$
^data-raw$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ src/*.so
src/*.dll
README.html
README.Rmd
data-raw/*.RData
25 changes: 15 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: remote
Title: Empirical Orthogonal Teleconnections in R
Version: 1.2.3
Authors@R:
Version: 1.2.3.9004
Authors@R:
c(person(given = "Tim",
family = "Appelhans",
role = c("cre", "aut"),
Expand All @@ -25,24 +25,29 @@ Description: Empirical orthogonal teleconnections in R.
space and time, EOT analysis produces patterns that are orthogonal in
either space or time.
License: GPL (>= 3) | file LICENSE
Depends:
Depends:
Rcpp (>= 0.10.3),
raster,
methods
Imports:
methods,
R (>= 3.5)
Imports:
grDevices,
gridExtra,
latticeExtra,
mapdata,
scales,
stats,
terra,
utils
Suggests:
maps,
lattice,
Suggests:
checkmate,
grid,
sp
lattice,
maps,
sp,
tinytest
LinkingTo: Rcpp
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
Config/roxygen2/version: 8.0.0
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import(methods)
import(parallel)
import(raster)
import(scales)
import(terra)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,hcl)
importFrom(stats,cov.wt)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# remote 1.2.3.9004 (2026-07-15)

#### ✨ features and improvements

#### 🐛 bug fixes

#### 💬 documentation etc

#### 🍬 miscellaneous


# remote 1.2.3 (2025-04-12)

#### 💬 documentation etc
Expand Down
98 changes: 69 additions & 29 deletions R/anomalize.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,81 @@
#' Create an anomaly RasterStack
methods::setGeneric(
"anomalize"
, function(x, ...) {
standardGeneric("anomalize")
}
)

#' Create an anomaly raster series
#'
#' @description
#' The function creates an anomaly RasterStack either based on the
#' overall mean of the original stack, or a supplied reference RasterLayer.
#' For the creation of seasonal anomalies use [deseason()].
#' @description The function creates an anomaly raster series either based on
#' the overall mean of the original series, or a supplied reference raster.
#' For the creation of seasonal anomalies use [deseason()].
#'
#' @param x a RasterStack
#' @param reference an optional RasterLayer to be used as the reference
#' @param ... additional arguments passed to [raster::calc()] (and, in turn,
#' [raster::writeRaster()]) which is used under the hood
#' @param x A `SpatRaster` (or `Raster*`) series.
#' @param reference An optional `SpatRaster` (or `RasterLayer`) to be used as
#' the reference.
#' @param ... Additional arguments passed to [terra::app()] when 'reference' is
#' `NULL` (e.g. 'cores', 'filename'), or to the underlying `SpatRaster` method
#' for `Raster*` input.
#'
#' @return an anomaly RasterStack
#' @return An anomaly `SpatRaster` series.
#'
#' @seealso
#' [deseason()], [denoise()], [raster::calc()]
#' [deseason()], [denoise()]
#'
#' @export anomalize
#' @name anomalize
#'
#' @examples
#' data(australiaGPCP)
#'
#' aus_anom <- anomalize(australiaGPCP)
#' pcp = terra::unwrap(australiaGPCP)
#' pcp_anom = anomalize(pcp)
#'
#' opar <- par(mfrow = c(1,2))
#' plot(australiaGPCP[[10]], main = "original")
#' plot(aus_anom[[10]], main = "anomalized")
#' opar = par(mfrow = c(1,2))
#' plot(pcp[[10]], main = "original")
#' plot(pcp_anom[[10]], main = "anomalized")
#' par(opar)
anomalize <- function(x,
reference = NULL,
...) {

if (is.null(reference)) {
mn <- raster::calc(x, fun = mean, ...)
} else {
mn <- reference


################################################################################
### function using 'RasterStackBrick' ##########################################
#' @aliases anomalize,RasterStackBrick-method
#' @rdname anomalize
methods::setMethod(
"anomalize"
, signature(x = "RasterStackBrick")
, function(
x
, ...
) {
anomalize(
terra::rast(x)
, ...
)
}
)


################################################################################
### function using 'SpatRaster' ################################################
#' @aliases anomalize,SpatRaster-method
#' @rdname anomalize
methods::setMethod(
"anomalize"
, signature(x = "SpatRaster")
, function(
x
, reference = NULL
, ...
) {

if (is.null(reference)) {
reference = terra::app(
x
, fun = mean
, ...
)
}

return(x - reference)
}

return(x - mn)

}
)
85 changes: 61 additions & 24 deletions R/calcVar.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,70 @@
#' Calculate space-time variance of a RasterStack or RasterBrick
methods::setGeneric(
"calcVar"
, function(x, ...) {
standardGeneric("calcVar")
}
)

#' Calculate space-time variance of a raster series
#'
#' @description
#' The function calculates the (optionally standardised) space-time
#' variance of a RasterStack or RasterBrick.
#' @description The function calculates the (optionally standardised) space-time
#' variance of a raster series.
#'
#' @param x a RasterStack or RasterBrick
#' @param standardised logical.
#' @param ... currently not used
#' @param x A `SpatRaster` (or `Raster*`) series.
#' @param standardised `logical`, defaults to `FALSE`.
#' @param ... For `SpatRaster` input: currently not used. For `Raster*` input:
#' arguments passed to the underlying `SpatRaster` method.
#'
#' @return the mean (optionally standardised) space-time variance.
#' @return The mean (optionally standardised) space-time variance as `numeric`.
#'
#' @export calcVar
#' @name calcVar
#'
#' @examples
#' data("pacificSST")
#' @examples
#' sst = terra::unwrap(pacificSST)
#'
#' calcVar(pacificSST)
calcVar <- function(x, standardised = FALSE, ...) {

if (!standardised) {
t <- mean(apply(raster::getValues(x), 1, var, na.rm = TRUE),
na.rm = TRUE)
s <- mean(apply(raster::getValues(x), 2, var, na.rm = TRUE),
na.rm = TRUE)
vrnc <- t + s
} else {
vrnc <- var(as.vector(raster::getValues(x)), na.rm = TRUE)
#' calcVar(sst) # default non-standardised
#' calcVar(sst, standardised = TRUE)


################################################################################
### function using 'RasterStackBrick' ##########################################
#' @aliases calcVar,RasterStackBrick-method
#' @rdname calcVar
methods::setMethod(
"calcVar"
, signature(x = "RasterStackBrick")
, function(
x
, ...
) {
calcVar(
terra::rast(x)
, ...
)
}
)


################################################################################
### function using 'SpatRaster' ################################################
#' @aliases calcVar,SpatRaster-method
#' @rdname calcVar
methods::setMethod(
"calcVar"
, signature(x = "SpatRaster")
, function(x, standardised = FALSE, ...) {

return(vrnc)

}
if (!standardised) {
# compute variance across time and space, leveraging c++ functions for
# speed (see `?terra::app` for details)
t <- mean(terra::values(terra::app(x, "sd", na.rm = TRUE)^2)[, 1L])
s <- mean((terra::global(x, fun = "sd", na.rm = TRUE)[, 1L])^2)
vrnc <- t + s
} else {
vrnc <- var(as.vector(terra::values(x)), na.rm = TRUE)
}

return(vrnc)
}
)
Loading
Loading