diff --git a/R/CST_QuantileMapping.R b/R/CST_QuantileMapping.R index 0ac443ba519c32dac843f588ab1bf20ae3900ee3..92b7ac038161d037ab3b9409bf42c97dd3376aa1 100644 --- a/R/CST_QuantileMapping.R +++ b/R/CST_QuantileMapping.R @@ -14,8 +14,9 @@ #'@param sdate_dim A character string indicating the dimension name in which #' cross-validation would be applied when exp_cor is not provided. 'sdate' by #' default. -#'@param memb_dim A character string indicating the dimension name where -#' ensemble members are stored in the experimental arrays. 'member' by default. +#'@param memb_dim A character string indicating the dimension name where +#' ensemble members are stored in the experimental arrays. It can be NULL if +#' there is no ensemble member dimension. It is set as 'member' by default. #'@param window_dim A character string indicating the dimension name where #' samples have been stored. It can be NULL (default) in case all samples are #' used. @@ -106,8 +107,8 @@ CST_QuantileMapping <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', #' cross-validation would be applied when exp_cor is not provided. 'sdate' by #' default. #'@param memb_dim A character string indicating the dimension name where -#' ensemble members are stored in the experimental arrays. 'member' by -#' default. +#' ensemble members are stored in the experimental arrays. It can be NULL if +#' there is no ensemble member dimension. It is set as 'member' by default. #'@param window_dim A character string indicating the dimension name where #' samples have been stored. It can be NULL (default) in case all samples are #' used. @@ -183,30 +184,28 @@ QuantileMapping <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', "'PTF', 'DIST', 'RQUANT', 'QUANT', 'SSPLIN'.") } # memb_dim - if (!all(memb_dim %in% obsdims)) { - obs <- InsertDim(obs, posdim = 1, lendim = 1, - name = memb_dim[!(memb_dim %in% obsdims)]) - } - if (any(!memb_dim %in% expdims)) { - stop("Parameter 'memb_dim' is not found in 'exp' dimensions.") + if (is.null(memb_dim)) { + remove_member <- TRUE + memb_dim <- "temp_memb_dim" + exp <- InsertDim(exp, posdim = 1, lendim = 1, name = "temp_memb_dim") + obs <- InsertDim(obs, posdim = 1, lendim = 1, name = "temp_memb_dim") + obsdims <- names(dim(obs)) + expdims <- names(dim(exp)) + if (!is.null(exp_cor)) { + exp_cor <- InsertDim(exp_cor, posdim = 1, lendim = 1, name = "temp_memb_dim") + } + } else { + remove_member <- FALSE + if (!all(memb_dim %in% obsdims)) { + obs <- InsertDim(obs, posdim = 1, lendim = 1, + name = memb_dim[!(memb_dim %in% obsdims)]) + obsdims <- names(dim(obs)) + } + if (any(!memb_dim %in% expdims)) { + stop(paste0("Parameter 'memb_dim' is not found in 'exp' dimensions. ", + "Set it as NULL if there is no member dimension.")) + } } - - # if (is.null(memb_dim)) { - # remove_member <- TRUE - # exp <- InsertDim(exp, posdim = 1, lendim = 1, name = 'member') - # obs <- InsertDim(obs, posdim = 1, lendim = 1, name = 'member') - # memb_dim <- 'member' - # } else { - # remove_member <- FALSE - # if (!all(memb_dim %in% obsdims)) { - # obs <- InsertDim(obs, posdim = 1, lendim = 1, - # name = memb_dim[!(memb_dim %in% obsdims)]) - # } - # if (any(!memb_dim %in% expdims)) { - # stop("Parameter 'memb_dim' is not found in 'exp' dimensions.") - # } - # } - sample_dims <- c(memb_dim, sdate_dim) # window_dim if (!is.null(window_dim)) { @@ -232,7 +231,6 @@ QuantileMapping <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', } ############################### - if (!is.null(exp_cor)) { qmaped <- Apply(list(exp, obs, exp_cor), target_dims = sample_dims, fun = .qmapcor, method = method, sdate_dim = sdate_dim, @@ -244,14 +242,16 @@ QuantileMapping <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', sdate_dim = sdate_dim, na.rm = na.rm, ..., ncores = ncores)$output1 } - # if (remove_member) { - # dim(qmaped) <- dim(qmaped)[-which(names(dim(qmaped)) == 'member')] - # } + # remove added 'temp_memb_dim' + if (remove_member) { + dim(qmaped) <- dim(qmaped)[-which(names(dim(qmaped)) == "temp_memb_dim")] + } + return(qmaped) } -.qmapcor <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', method = 'QUANT', - na.rm = FALSE, ...) { +.qmapcor <- function(exp, obs, exp_cor = NULL, sdate_dim = 'sdate', + method = 'QUANT', na.rm = FALSE, ...) { # exp: [memb (+ window), sdate] # obs: [memb (+ window), sdate] diff --git a/man/CST_QuantileMapping.Rd b/man/CST_QuantileMapping.Rd index fc77d3bfac261bf56994330a8045a3a934e284ad..0ca3423f9d64002f088417482ca195593cb8bdff 100644 --- a/man/CST_QuantileMapping.Rd +++ b/man/CST_QuantileMapping.Rd @@ -30,8 +30,9 @@ is applied in object 'exp'.} cross-validation would be applied when exp_cor is not provided. 'sdate' by default.} -\item{memb_dim}{A character string indicating the dimension name where -ensemble members are stored in the experimental arrays. 'member' by default.} +\item{memb_dim}{A character string indicating the dimension name where +ensemble members are stored in the experimental arrays. It can be NULL if +there is no ensemble member dimension. It is set as 'member' by default.} \item{window_dim}{A character string indicating the dimension name where samples have been stored. It can be NULL (default) in case all samples are diff --git a/man/QuantileMapping.Rd b/man/QuantileMapping.Rd index c4c29e7f47d2121b36f65d6e47cc33a20457da9f..a1ff178de2c0170e6b2a675bf7a9747beb0b4ff2 100644 --- a/man/QuantileMapping.Rd +++ b/man/QuantileMapping.Rd @@ -33,8 +33,8 @@ cross-validation would be applied when exp_cor is not provided. 'sdate' by default.} \item{memb_dim}{A character string indicating the dimension name where -ensemble members are stored in the experimental arrays. 'member' by -default.} +ensemble members are stored in the experimental arrays. It can be NULL if +there is no ensemble member dimension. It is set as 'member' by default.} \item{window_dim}{A character string indicating the dimension name where samples have been stored. It can be NULL (default) in case all samples are diff --git a/tests/testthat/test-CST_QuantileMapping.R b/tests/testthat/test-CST_QuantileMapping.R index 0b2890f3eb90d370d381ad629416f82826640ff7..a086ab93d3de7f2f6a4187bec54b0c5e95596e9b 100644 --- a/tests/testthat/test-CST_QuantileMapping.R +++ b/tests/testthat/test-CST_QuantileMapping.R @@ -99,14 +99,15 @@ exp_cor6_1$data <- ClimProjDiags::Subset(exp_cor6_1$data, 'sdate', 1) exp_cor6_2 <- exp6 exp_cor6_2$data <- ClimProjDiags::Subset(exp_cor6_2$data, 'member', 1:2) -# # dat7 -# exp7 <- 1 : c(1 * 1 * 6 * 3 * 8 * 8) -# dim(exp7) <- c(dataset = 1, sdate = 6, ftime = 3, -# lat = 8, lon = 8) +# dat7 +exp7 <- 1 : c(1 * 1 * 6 * 3 * 4 * 4 * 2) +dim(exp7) <- c(dataset = 1, sdate = 6, ftime = 3, + lat = 4, lon = 4, member = 2) -# obs7 <- 101 : c(100 + 1 * 1 * 6 * 3 * 8 * 8) -# dim(obs7) <- c(dataset = 1, sdate = 6, ftime = 3, -# lat = 8, lon = 8) +obs7 <- 101 : c(100 + 1 * 1 * 6 * 3 * 4 * 4 * 2) +dim(obs7) <- c(dataset = 1, sdate = 6, ftime = 3, + lat = 4, lon = 4, window = 2) +exp_cor7 <- exp7 + 1 ############################################## @@ -129,7 +130,7 @@ test_that("1. Sanity checks", { expect_error( CST_QuantileMapping(exp = exp1, obs = obs1, exp_cor = 1), paste0("Parameter 'exp_cor' must be of the class 's2dv_cube', as output ", - "by CSTools::CST_Load.") + "by CSTools::CST_Load.") ) # exp and obs expect_error( @@ -235,9 +236,24 @@ test_that("4. dat6", { ############################################## -# test_that("5. dat7", { -# expect_equal( -# dim(QuantileMapping(exp7, obs7, memb_dim = NULL)), -# c(sdate = 6, dataset = 1, ftime = 3, lat = 8, lon = 8) -# ) -# }) \ No newline at end of file +test_that("5. dat7", { + expect_equal( + dim(QuantileMapping(exp7, obs7, memb_dim = NULL)), + c(sdate = 6, dataset = 1, ftime = 3, lat = 4, lon = 4, member = 2, window = 2) + ) + # window_dim + expect_equal( + dim(QuantileMapping(exp7, obs7, memb_dim = NULL, window_dim = 'window')), + c(sdate = 6, dataset = 1, ftime = 3, lat = 4, lon = 4, member = 2) + ) + # exp_cor + expect_equal( + dim(QuantileMapping(exp7, obs7, exp_cor7, memb_dim = NULL)), + c(sdate = 6, dataset = 1, ftime = 3, lat = 4, lon = 4, member = 2, window = 2) + ) + expect_equal( + dim(QuantileMapping(exp7, obs7, exp_cor7, memb_dim = NULL, + window_dim = 'window')), + c(sdate = 6, dataset = 1, ftime = 3, lat = 4, lon = 4, member = 2) + ) +})