diff --git a/R/CST_InsertDim.R b/R/CST_InsertDim.R new file mode 100644 index 0000000000000000000000000000000000000000..f765b15d64743c023d697afa9878b699fc4f2141 --- /dev/null +++ b/R/CST_InsertDim.R @@ -0,0 +1,71 @@ +#'Add a named dimension to an object of class s2dv_cube +#' +#'Insert an extra dimension into an array at position 'posdim' with length +#''lendim'. The array in \code{data} repeats along the new dimension. +#'The dimensions, coordinates and attributes are modified accordingly. +#' +#'@author Agudetse Roures Victoria, \email{victoria.agudetse@bsc.es} +#' +#'@param data An object of class \code{s2dv_cube} to which the additional +#' dimension should be added. +#'@param posdim An integer indicating the position of the new dimension. +#'@param lendim An integer indicating the length of the new dimension. +#'@param name A character string indicating the name for the new dimension. +#'@param values A vector containing the values of the new dimension and any +#' relevant attributes. If NULL, a sequence of integers from 1 to lendim will +#' be added. +#' +#'@return An object of class \code{s2dv_cube} with similar data, coordinates and +#'attributes as the \code{data} input, but with an additional dimension. +#' +#'@examples +#'#Example with sample data: +#'# Check original dimensions and coordinates +#'lonlat_temp$exp$dims +#'names(lonlat_temp$exp$coords) +#'# Add 'variable' dimension +#'exp <- CST_InsertDim(lonlat_temp$exp, +#' posdim = 2, +#' lendim = 1, +#' name = "variable", +#' values = c("tas")) +#'# Check new dimensions and coordinates +#'exp$dims +#'exp$coords$variable +#' +#'@seealso \link[s2dv]{InsertDim} +#' +#'@importFrom s2dv InsertDim +#'@export +CST_InsertDim <- function(data, posdim, lendim, name, values = NULL) { + # Check inputs + # Check 's2dv_cube' + if (!inherits(data, 's2dv_cube')) { + stop("Parameter 'data' must be of the class 's2dv_cube'.") + } + # Check name + if (!is.character(name) || length(name) > 1) { + stop("Parameter 'name' must be a character string") + } + # Check values + if (is.null(values)) { + warning(paste0("Parameter 'values' is not provided. Adding a sequence of ", + "integers from 1 to 'lendim' as the values for the new dimension.")) + values <- 1:lendim + } else { + if (!(length(values) == lendim)) { + stop(paste0("The length of the parameter 'values' must be consistent", + "with the parameter 'lendim'.")) + } + } + + # Insert dim in data + data$data <- s2dv::InsertDim(data$data, posdim = posdim, lendim = lendim, + name = name) + # Adjust dimensions + data$dims <- dim(data$data) + # Adjust coordinates + data$coords[[name]] <- values + data$coords <- data$coords[names(data$dims)] + return(data) +} diff --git a/tests/testthat/test-CST_InsertDim.R b/tests/testthat/test-CST_InsertDim.R new file mode 100644 index 0000000000000000000000000000000000000000..39e5ade86562f5aaf1f00c61a10d54f479bd34c2 --- /dev/null +++ b/tests/testthat/test-CST_InsertDim.R @@ -0,0 +1,47 @@ +context("CSTools::CST_InsertDim tests") + +############################################## +exp <- lonlat_temp$exp +############################################## + +test_that("1. Input checks", { + expect_error( + CST_InsertDim(1), + "Parameter 'data' must be of the class 's2dv_cube'." + ) + expect_error( + CST_InsertDim(exp, name = 1), + "Parameter 'name' must be a character string" + ) + expect_warning( + CST_InsertDim(exp, posdim = 1, lendim = 1, name = "variable"), + paste0("Parameter 'values' is not provided. Adding a sequence of ", + "integers from 1 to 'lendim' as the values for the new dimension.") + ) + expect_error( + CST_InsertDim(exp, posdim = 1, lendim = 1, name = "variable", values = 1:2), + paste0("The length of the parameter 'values' must be consistent", + "with the parameter 'lendim'.") + ) +}) + +############################################## + +test_that("2. Output checks", { + exp <- CST_InsertDim(exp, posdim = 2, lendim = 1, name = "variable", + values = c("tas")) + expect_equal( + dim(exp$data), + c(dataset = 1, variable = 1, member = 15, sdate = 6, ftime = 3, lat = 22, + lon = 53) + ) + expect_equal( + exp$dims, + c(dataset = 1, variable = 1, member = 15, sdate = 6, ftime = 3, lat = 22, + lon = 53) + ) + expect_equal( + exp$coords$variable, + c("tas") + ) +})