diff --git a/.Rbuildignore b/.Rbuildignore index fa596e707601da63df8c53cf4f087a70a953dbea..b2d8e5fcebca62bff5e5380a881580283874cd54 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,4 +5,4 @@ ./.nc$ .*^(?!data)\.RData$ .*\.gitlab-ci.yml$ -^tests$ +#^tests$ diff --git a/NEWS.md b/NEWS.md index 6d66f0a29c3e7b22226d91d6ca505b8e9a462961..887db24ed55d6fee9dc4d5252264565129aac7e0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ +### CSTools X.X.X +**Submission date to CRAN: DD-MM-YYYY** + +- Enhancements:รง + + CST_BiasCorrection new parameters 'memb_dim', 'sdate_dim', 'ncores' + ### CSTools 4.0.1 -**Submission date to CRAN: XX-06-2021** +**Submission date to CRAN: 05-10-2021** - New features: + Dynamical Bias Correction method: `CST_ProxiesAttractors` and `CST_DynBiasCorrection` diff --git a/R/CST_BiasCorrection.R b/R/CST_BiasCorrection.R index 0b8375130cb170345fd7e356214f577b3703662b..263128aa1681a7d5f4133e1364fdf7f1d66cb562 100644 --- a/R/CST_BiasCorrection.R +++ b/R/CST_BiasCorrection.R @@ -8,6 +8,9 @@ #'@param exp_cor an object of class \code{s2dv_cube} as returned by \code{CST_Load} function, containing the seasonl forecast experiment to be corrected. If it is NULL, the 'exp' forecast will be corrected. #'@param na.rm a logical value indicating whether missing values should be stripped before the computation proceeds, by default it is set to FALSE. #' +#'@param memb_dim is a character string indicating the name of the member dimension. By default, it is set to 'member'. +#'@param sdate_dim is a character string indicating the name of the start date dimension. By default, it is set to 'sdate'. +#'@param ncores is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one. #'@return an object of class \code{s2dv_cube} containing the bias corrected forecasts in the element called \code{$data} with the same dimensions of the experimental data. #' #'@references Torralba, V., F.J. Doblas-Reyes, D. MacLeod, I. Christel and M. Davis (2017). Seasonal climate prediction: a new source of information for the management of wind energy resources. Journal of Applied Meteorology and Climatology, 56, 1231-1247, doi:10.1175/JAMC-D-16-0204.1. (CLIM4ENERGY, EUPORIAS, NEWA, RESILIENCE, SPECS) @@ -31,7 +34,9 @@ #'a <- CST_BiasCorrection(exp = exp, obs = obs) #'str(a) #'@export -CST_BiasCorrection <- function(exp, obs, exp_cor = NULL, na.rm = FALSE) { +CST_BiasCorrection <- function(exp, obs, exp_cor = NULL, na.rm = FALSE, + memb_dim = 'member', sdate_dim = 'sdate', + ncores = 1) { if (!inherits(exp, 's2dv_cube') || !inherits(obs, 's2dv_cube')) { stop("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", "as output by CSTools::CST_Load.") @@ -42,7 +47,9 @@ CST_BiasCorrection <- function(exp, obs, exp_cor = NULL, na.rm = FALSE) { } if (is.null(exp_cor)) { dimnames <- names(dim(exp$data)) - BiasCorrected <- BiasCorrection(exp = exp$data, obs = obs$data, na.rm = na.rm) + BiasCorrected <- BiasCorrection(exp = exp$data, obs = obs$data, na.rm = na.rm, + memb_dim = memb_dim, sdate_dim = sdate_dim, + ncores = ncores) pos <- match(dimnames, names(dim(BiasCorrected))) BiasCorrected <- aperm(BiasCorrected, pos) names(dim(BiasCorrected)) <- dimnames @@ -76,6 +83,9 @@ CST_BiasCorrection <- function(exp, obs, exp_cor = NULL, na.rm = FALSE) { #'@param obs a multidimensional array with named dimensions containing the observed data with at least 'sdate' dimension. #'@param exp_cor a multidimensional array with named dimensions containing the seasonl forecast experiment to be corrected. If it is NULL, the 'exp' forecast will be corrected. #'@param na.rm a logical value indicating whether missing values should be stripped before the computation proceeds, by default it is set to FALSE. +#'@param memb_dim is a character string indicating the name of the member dimension. By default, it is set to 'member'. +#'@param sdate_dim is a character string indicating the name of the start date dimension. By default, it is set to 'sdate'. +#'@param ncores is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one. #' #'@return an object of class \code{s2dv_cube} containing the bias corrected forecasts in the element called \code{$data} with the same dimensions of the experimental data. #' @@ -94,14 +104,17 @@ CST_BiasCorrection <- function(exp, obs, exp_cor = NULL, na.rm = FALSE) { #'a <- BiasCorrection(exp = mod1, obs = obs1) #'str(a) #'@export -BiasCorrection <- function (exp, obs, exp_cor = NULL, na.rm = FALSE) { +BiasCorrection <- function (exp, obs, exp_cor = NULL, na.rm = FALSE, + memb_dim = 'member', sdate_dim = 'sdate', + ncores = 1) { - if (!all(c('member', 'sdate') %in% names(dim(exp)))) { - stop("Parameter 'exp' must have the dimensions 'member' and 'sdate'.") + if (!all(c(memb_dim, sdate_dim) %in% names(dim(exp)))) { + stop(paste("Parameter 'exp' must have the dimensions set up in 'memb_dim'", + "and 'sdate_dim' parameters.")) } - if (!all(c('sdate') %in% names(dim(obs)))) { - stop("Parameter 'obs' must have the dimension 'sdate'.") + if (!all(sdate_dim %in% names(dim(obs)))) { + stop("Parameter 'obs' must have the dimension specified in 'sdate_dim'.") } if (any(is.na(exp))) { @@ -122,34 +135,31 @@ BiasCorrection <- function (exp, obs, exp_cor = NULL, na.rm = FALSE) { warning("Paramter 'na.rm' has length greater than 1, and only the fist element is used.") } - target_dims_obs <- 'sdate' - if ('member' %in% names(dim(obs))) { - target_dims_obs <- c('member', target_dims_obs) + target_dims_obs <- sdate_dim + if (memb_dim %in% names(dim(obs))) { + target_dims_obs <- c(memb_dim, target_dims_obs) } if (is.null(exp_cor)) { BiasCorrected <- Apply(data = list(var_obs = obs, var_exp = exp), target_dims = list(target_dims_obs, - c('member', 'sdate')), - fun = .sbc , na.rm = na.rm)$output1 + c(memb_dim, sdate_dim)), + fun = .sbc , na.rm = na.rm, ncores = ncores)$output1 } else { BiasCorrected <- Apply(data = list(var_obs = obs, var_exp = exp, var_cor = exp_cor), target_dims = list(target_dims_obs, - c('member', 'sdate'), - c('member', 'sdate')), - fun = .sbc , output_dims = c('member','sdate'), - na.rm = na.rm)$output1 + c(memb_dim, sdate_dim), + c(memb_dim, sdate_dim)), + fun = .sbc , output_dims = c(memb_dim,sdate_dim), + na.rm = na.rm, ncores = ncores)$output1 } return(BiasCorrected) } .sbc <- function(var_obs, var_exp , var_cor = NULL, na.rm = FALSE) { - nmembers <- dim(var_exp)['member'][] - ntime <- dim(var_exp)['sdate'][] - #if (all(names(dim(var_exp)) != c('member','sdate'))) { - # var_exp <- t(var_exp) - #} + nmembers <- dim(var_exp)[1] + ntime <- dim(var_exp)[2][] corrected <- NA * var_exp diff --git a/man/BiasCorrection.Rd b/man/BiasCorrection.Rd index 8b60a6de655fd4261a51bf72ad02f99ac2756498..3f8d72ce279759be58c6a7fc2b3fff12b615fe62 100644 --- a/man/BiasCorrection.Rd +++ b/man/BiasCorrection.Rd @@ -4,7 +4,15 @@ \alias{BiasCorrection} \title{Bias Correction based on the mean and standard deviation adjustment} \usage{ -BiasCorrection(exp, obs, exp_cor = NULL, na.rm = FALSE) +BiasCorrection( + exp, + obs, + exp_cor = NULL, + na.rm = FALSE, + memb_dim = "member", + sdate_dim = "sdate", + ncores = 1 +) } \arguments{ \item{exp}{a multidimensional array with named dimensions containing the seasonal forecast experiment data with at least 'member' and 'sdate' dimensions.} @@ -14,6 +22,12 @@ BiasCorrection(exp, obs, exp_cor = NULL, na.rm = FALSE) \item{exp_cor}{a multidimensional array with named dimensions containing the seasonl forecast experiment to be corrected. If it is NULL, the 'exp' forecast will be corrected.} \item{na.rm}{a logical value indicating whether missing values should be stripped before the computation proceeds, by default it is set to FALSE.} + +\item{memb_dim}{is a character string indicating the name of the member dimension. By default, it is set to 'member'.} + +\item{sdate_dim}{is a character string indicating the name of the start date dimension. By default, it is set to 'sdate'.} + +\item{ncores}{is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one.} } \value{ an object of class \code{s2dv_cube} containing the bias corrected forecasts in the element called \code{$data} with the same dimensions of the experimental data. diff --git a/man/CST_BiasCorrection.Rd b/man/CST_BiasCorrection.Rd index adfc2798ac9c9ddfa3cb942a0c2faf69233bfaed..202e7651d661797db14514aa5e69835e4e6c39fd 100644 --- a/man/CST_BiasCorrection.Rd +++ b/man/CST_BiasCorrection.Rd @@ -4,7 +4,15 @@ \alias{CST_BiasCorrection} \title{Bias Correction based on the mean and standard deviation adjustment} \usage{ -CST_BiasCorrection(exp, obs, exp_cor = NULL, na.rm = FALSE) +CST_BiasCorrection( + exp, + obs, + exp_cor = NULL, + na.rm = FALSE, + memb_dim = "member", + sdate_dim = "sdate", + ncores = 1 +) } \arguments{ \item{exp}{an object of class \code{s2dv_cube} as returned by \code{CST_Load} function, containing the seasonal forecast experiment data in the element named \code{$data}} @@ -14,6 +22,12 @@ CST_BiasCorrection(exp, obs, exp_cor = NULL, na.rm = FALSE) \item{exp_cor}{an object of class \code{s2dv_cube} as returned by \code{CST_Load} function, containing the seasonl forecast experiment to be corrected. If it is NULL, the 'exp' forecast will be corrected.} \item{na.rm}{a logical value indicating whether missing values should be stripped before the computation proceeds, by default it is set to FALSE.} + +\item{memb_dim}{is a character string indicating the name of the member dimension. By default, it is set to 'member'.} + +\item{sdate_dim}{is a character string indicating the name of the start date dimension. By default, it is set to 'sdate'.} + +\item{ncores}{is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one.} } \value{ an object of class \code{s2dv_cube} containing the bias corrected forecasts in the element called \code{$data} with the same dimensions of the experimental data. diff --git a/tests/testthat/test-CST_BiasCorrection.R b/tests/testthat/test-CST_BiasCorrection.R index fc6405ce7f9ee002c70aa7d27fbb8417bd854e35..205aef445b17bcbab8457e209961e98e776a7720 100644 --- a/tests/testthat/test-CST_BiasCorrection.R +++ b/tests/testthat/test-CST_BiasCorrection.R @@ -47,4 +47,14 @@ test_that("Sanity checks", { obs <- array(3:6, c(sdate = 3, member = 1)) res <- round(BiasCorrection(exp = hinc, obs = obs, exp_cor = hinc), 2) expect_equal(res, array(c(2.66, 4.27, 3.2, 4.8, 3.73, 5.34), c(member = 2, sdate = 3))) + + # if obs doesn't have memb_dim it works the same: + hinc <- array(1:6, c(sdate = 3, member = 2)) + obs <- array(3:6, c(sdate = 3)) + res <- round(BiasCorrection(exp = hinc, obs = obs, exp_cor = hinc), 2) + expect_equal(res, array(c(2.66, 4.27, 3.2, 4.8, 3.73, 5.34), c(member = 2, sdate = 3))) + + + + })