diff --git a/R/Clim.R b/R/Clim.R index 1040e368f0c61b15a966639e5cfe2aeb52871396..95701191a5741c36104a4e98491520ba0b24ddf2 100644 --- a/R/Clim.R +++ b/R/Clim.R @@ -26,11 +26,11 @@ #' options include 'clim', 'kharin', and 'NDV'. The default value is 'clim'. #'@param ftime_dim A character string indicating the name of forecast time #' dimension. Only used when method = 'NDV'. The default value is 'ftime'. +#'@param memb A logical value indicating whether to remain 'memb_dim' dimension +#' (TRUE) or do ensemble mean over 'memb_dim' (FALSE). The default value is TRUE. #'@param memb_dim A character string indicating the name of the member #' dimension. Only used when parameter 'memb' is FALSE. It must be one element #' in 'dat_dim'. The default value is 'member'. -#'@param memb A logical value indicating whether to remain 'memb_dim' dimension -#' (TRUE) or do ensemble mean over 'memb_dim' (FALSE). The default value is TRUE. #'@param na.rm A logical value indicating whether to remove NA values along #' 'time_dim' when calculating climatology (TRUE) or return NA if there is NA #' along 'time_dim' (FALSE). The default value is TRUE. @@ -67,8 +67,8 @@ #'@import multiApply #'@export Clim <- function(exp, obs, time_dim = 'sdate', dat_dim = c('dataset', 'member'), - method = 'clim', ftime_dim = 'ftime', memb_dim = 'member', - memb = TRUE, na.rm = TRUE, ncores = NULL) { + method = 'clim', ftime_dim = 'ftime', memb = TRUE, + memb_dim = 'member', na.rm = TRUE, ncores = NULL) { # Check inputs ## exp and obs (1) @@ -109,23 +109,27 @@ Clim <- function(exp, obs, time_dim = 'sdate', dat_dim = c('dataset', 'member'), stop("Parameter 'method' must be one of 'clim', 'kharin' or 'NDV'.") } ## ftime_dim - if (!is.character(ftime_dim) | length(ftime_dim) > 1) { - stop("Parameter 'ftime_dim' must be a character string.") - } - if (!ftime_dim %in% names(dim(exp)) | !ftime_dim %in% names(dim(obs))) { - stop("Parameter 'ftime_dim' is not found in 'exp' or 'obs' dimension.") - } - ## memb_dim - if (!is.character(memb_dim) | length(memb_dim) > 1) { - stop("Parameter 'memb_dim' must be a character string.") - } - if (!memb_dim %in% names(dim(exp)) | !memb_dim %in% names(dim(obs))) { - stop("Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension.") + if (method == "NDV") { + if (!is.character(ftime_dim) | length(ftime_dim) > 1) { + stop("Parameter 'ftime_dim' must be a character string.") + } + if (!ftime_dim %in% names(dim(exp)) | !ftime_dim %in% names(dim(obs))) { + stop("Parameter 'ftime_dim' is not found in 'exp' or 'obs' dimension.") + } } ## memb if (!is.logical(memb) | length(memb) > 1) { stop("Parameter 'memb' must be one logical value.") } + ## memb_dim + if (!memb) { + if (!is.character(memb_dim) | length(memb_dim) > 1) { + stop("Parameter 'memb_dim' must be a character string.") + } + if (!memb_dim %in% names(dim(exp)) | !memb_dim %in% names(dim(obs))) { + stop("Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension.") + } + } ## na.rm if (!is.logical(na.rm) | length(na.rm) > 1) { stop("Parameter 'na.rm' must be one logical value.") @@ -186,7 +190,7 @@ Clim <- function(exp, obs, time_dim = 'sdate', dat_dim = c('dataset', 'member'), target_dims = c(time_dim, dat_dim), fun = .Clim, method = method, time_dim = time_dim, - dat_dim = dat_dim, ftime_dim = ftime_dim, memb_dim = memb_dim, + dat_dim = dat_dim, memb_dim = memb_dim, memb = memb, na.rm = na.rm, ncores = ncores) # Add member dimension name back diff --git a/man/Clim.Rd b/man/Clim.Rd index 8bb93f1e07c0c91073a4463b1b4bef42c398910e..a997a7f1db2a33a5d63a4fe7219fe70af176b08f 100644 --- a/man/Clim.Rd +++ b/man/Clim.Rd @@ -5,8 +5,8 @@ \title{Compute Bias Corrected Climatologies} \usage{ Clim(exp, obs, time_dim = "sdate", dat_dim = c("dataset", "member"), - method = "clim", ftime_dim = "ftime", memb_dim = "member", - memb = TRUE, na.rm = TRUE, ncores = NULL) + method = "clim", ftime_dim = "ftime", memb = TRUE, + memb_dim = "member", na.rm = TRUE, ncores = NULL) } \arguments{ \item{exp}{A named numeric array of experimental data, with at least two @@ -29,13 +29,13 @@ options include 'clim', 'kharin', and 'NDV'. The default value is 'clim'.} \item{ftime_dim}{A character string indicating the name of forecast time dimension. Only used when method = 'NDV'. The default value is 'ftime'.} +\item{memb}{A logical value indicating whether to remain 'memb_dim' dimension +(TRUE) or do ensemble mean over 'memb_dim' (FALSE). The default value is TRUE.} + \item{memb_dim}{A character string indicating the name of the member dimension. Only used when parameter 'memb' is FALSE. It must be one element in 'dat_dim'. The default value is 'member'.} -\item{memb}{A logical value indicating whether to remain 'memb_dim' dimension -(TRUE) or do ensemble mean over 'memb_dim' (FALSE). The default value is TRUE.} - \item{na.rm}{A logical value indicating whether to remove NA values along 'time_dim' when calculating climatology (TRUE) or return NA if there is NA along 'time_dim' (FALSE). The default value is TRUE.} diff --git a/tests/testthat/test-Clim.R b/tests/testthat/test-Clim.R index 835f59f2aaef362617f1083243d4558fa762182a..1d2ff3e6b013cdefd1cd3375a34193d2bbc0545d 100644 --- a/tests/testthat/test-Clim.R +++ b/tests/testthat/test-Clim.R @@ -18,6 +18,14 @@ context("s2dv::Clim tests") na <- floor(runif(30, min = 1, max = 120)) obs2[na] <- NA + # dat3 + set.seed(1) + exp3 <- array(rnorm(120), dim = c(dat = 1, ensemble = 3, date = 5, + lon = 2, lat = 4)) + set.seed(2) + obs3 <- array(rnorm(40), dim = c(dat = 1, ensemble = 1, + lon = 2, lat = 4, date = 5)) + ############################################## test_that("1. Input checks", { @@ -63,24 +71,24 @@ test_that("1. Input checks", { "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension." ) expect_error( - Clim(exp1, obs1, ftime_dim = 4), + Clim(exp1, obs1, method = 'NDV', ftime_dim = 4), "Parameter 'ftime_dim' must be a character string." ) expect_error( - Clim(exp1, obs1, ftime_dim = 'f'), + Clim(exp3, obs3, time_dim = "date", dat_dim = c("dat", "ensemble"), method = 'NDV'), "Parameter 'ftime_dim' is not found in 'exp' or 'obs' dimension." ) expect_error( - Clim(exp1, obs1, memb_dim = c('dataset', 'member')), - "Parameter 'memb_dim' must be a character string." + Clim(exp1, obs1, memb = 'member'), + "Parameter 'memb' must be one logical value." ) expect_error( - Clim(exp1, obs1, memb_dim = 'memb'), - "Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension." + Clim(exp1, obs1, memb = FALSE, memb_dim = c('dataset', 'member')), + "Parameter 'memb_dim' must be a character string." ) expect_error( - Clim(exp1, obs1, memb = 'member'), - "Parameter 'memb' must be one logical value." + Clim(exp1, obs1, memb = FALSE, memb_dim = 'memb'), + "Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension." ) expect_error( Clim(exp1, obs1, na.rm = na.omit), @@ -191,4 +199,22 @@ test_that("3. Output checks: dat2", { }) ############################################## +test_that("4. Output checks: dat3", { + + expect_equal( + (Clim(exp3, obs3, time_dim = 'date', dat_dim = c('dat', 'ensemble'))$clim_obs)[1:5], + c(0.6094532, -0.5307990, 0.5437774, -0.1817153, 0.1368191), + tolerance = 0.00001 + ) + expect_equal( + (Clim(exp3, obs3, time_dim = 'date', dat_dim = c('dat', 'ensemble'))$clim_exp)[1:5], + c(0.10592542, 0.10971142, 0.08689170, 0.14149945, 0.02359945), + tolerance = 0.00001 + ) + + + + + +})