From ad541e0b225bd76a7f645c4ba6c6c0400f84bb0d Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Thu, 30 Mar 2023 09:39:22 +0200 Subject: [PATCH 1/4] Add new function CST_InsertDim --- R/CST_InsertDim.R | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 R/CST_InsertDim.R diff --git a/R/CST_InsertDim.R b/R/CST_InsertDim.R new file mode 100644 index 00000000..fa89f179 --- /dev/null +++ b/R/CST_InsertDim.R @@ -0,0 +1,69 @@ +#'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{x} 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 that x is s2dv_cube + if (!inherits(data, 's2dv_cube')) { + stop("Parameter 'x' 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)) { + values <- 1:lendim + } else { + if (!(length(values) == lendim)) { + stop("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) +} -- GitLab From 892e3607ed375a9cb8ba6300303334fdd5971420 Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Thu, 30 Mar 2023 09:43:40 +0200 Subject: [PATCH 2/4] Add warning when 'values' are not provided --- R/CST_InsertDim.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/CST_InsertDim.R b/R/CST_InsertDim.R index fa89f179..62b9bdcb 100644 --- a/R/CST_InsertDim.R +++ b/R/CST_InsertDim.R @@ -48,6 +48,8 @@ CST_InsertDim <- function(data, posdim, lendim, name, values = NULL) { } # Check values if (is.null(values)) { + warning("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)) { -- GitLab From 98bcda4714e693ac9da35bd0a78a69a4cedfbc9a Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 30 Mar 2023 17:20:33 +0200 Subject: [PATCH 3/4] Minor code format correction and add test file --- R/CST_InsertDim.R | 26 ++++++++--------- tests/testthat/test-CST_InsertDim.R | 43 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 tests/testthat/test-CST_InsertDim.R diff --git a/R/CST_InsertDim.R b/R/CST_InsertDim.R index 62b9bdcb..d33eef6a 100644 --- a/R/CST_InsertDim.R +++ b/R/CST_InsertDim.R @@ -16,7 +16,7 @@ #' be added. #' #'@return An object of class \code{s2dv_cube} with similar data, coordinates and -#' attributes as the \code{x} input, but with an additional dimension. +#'attributes as the \code{data} input, but with an additional dimension. #' #'@examples #'#Example with sample data: @@ -37,31 +37,27 @@ #' #'@importFrom s2dv InsertDim #'@export -CST_InsertDim <- function(data, posdim, lendim, name, values = NULL) { - # Check that x is s2dv_cube +CST_InsertDim <- function(data, posdim, lendim, name = NULL, values = NULL) { + # Check inputs + # Check 's2dv_cube' if (!inherits(data, 's2dv_cube')) { - stop("Parameter 'x' must be of the class 's2dv_cube'.") - } - # Check name - if (!is.character(name) || length(name) > 1) { - stop("Parameter 'name' must be a character string") + stop("Parameter 'data' must be of the class 's2dv_cube'.") } # Check values if (is.null(values)) { - warning("Parameter 'values' is not provided. Adding a sequence of integers - from 1 to 'lendim' as the values for the new dimension.") + 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("The length of the parameter 'values' must be consistent - with the parameter '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) + data$data <- s2dv::InsertDim(data$data, posdim = posdim, lendim = lendim, + name = name) # Adjust dimensions data$dims <- dim(data$data) # Adjust coordinates diff --git a/tests/testthat/test-CST_InsertDim.R b/tests/testthat/test-CST_InsertDim.R new file mode 100644 index 00000000..29f8ca47 --- /dev/null +++ b/tests/testthat/test-CST_InsertDim.R @@ -0,0 +1,43 @@ +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_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") + ) +}) \ No newline at end of file -- GitLab From 351e1a3443e7a1276ced5b3d7b3030720fb4b18d Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Fri, 31 Mar 2023 10:16:15 +0200 Subject: [PATCH 4/4] Remove default value for parameter name --- R/CST_InsertDim.R | 6 +++++- tests/testthat/test-CST_InsertDim.R | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/R/CST_InsertDim.R b/R/CST_InsertDim.R index d33eef6a..f765b15d 100644 --- a/R/CST_InsertDim.R +++ b/R/CST_InsertDim.R @@ -37,12 +37,16 @@ #' #'@importFrom s2dv InsertDim #'@export -CST_InsertDim <- function(data, posdim, lendim, name = NULL, values = NULL) { +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 ", diff --git a/tests/testthat/test-CST_InsertDim.R b/tests/testthat/test-CST_InsertDim.R index 29f8ca47..39e5ade8 100644 --- a/tests/testthat/test-CST_InsertDim.R +++ b/tests/testthat/test-CST_InsertDim.R @@ -9,6 +9,10 @@ test_that("1. Input checks", { 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 ", @@ -40,4 +44,4 @@ test_that("2. Output checks", { exp$coords$variable, c("tas") ) -}) \ No newline at end of file +}) -- GitLab