diff --git a/R/CST_SaveExp.R b/R/CST_SaveExp.R index d6e98f384cde4dad9e41d48e88a9d754c609594c..2c363e46b1ae076a3bf50d1b2b3fd0e57f256c7b 100644 --- a/R/CST_SaveExp.R +++ b/R/CST_SaveExp.R @@ -28,7 +28,14 @@ #' dimension. #'@param memb_dim A character string indicating the name of the member dimension. #' By default, it is set to 'member'. It can be NULL if there is no member -#' dimension. +#' dimension. +#'@param startdates A vector of dates that will be used for the filenames +#' when saving the data in multiple files. It must be a vector of the same +#' length as the start date dimension of data. It must be a vector of class +#' \code{Dates}, \code{'POSIXct'} or character with lenghts between 1 and 10. +#' If it is NULL, the coordinate corresponding the the start date dimension or +#' the first Date of each time step will be used as the name of the files. +#' It is NULL by default. #'@param single_file A logical value indicating if all object is saved in a #' single file (TRUE) or in multiple files (FALSE). When it is FALSE, #' the array is separated for Datasets, variable and start date. It is FALSE @@ -76,7 +83,8 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', ftime_dim = 'time', dat_dim = 'dataset', var_dim = 'var', memb_dim = 'member', - single_file = FALSE, extra_string = NULL) { + startdates = NULL, single_file = FALSE, + extra_string = NULL) { # Check 's2dv_cube' if (!inherits(data, 's2dv_cube')) { stop("Parameter 'data' must be of the class 's2dv_cube', ", @@ -124,13 +132,28 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', "only the first element will be used.") sdate_dim <- sdate_dim[1] } + } else if (length(dim(data$attrs$Dates)) == 1) { + sdate_dim <- 'sdate' + dim(data$data) <- c(sdate = 1, dim(data$data)) + data$dims <- dim(data$data) + dim(data$attrs$Dates) <- c(sdate = 1, dim(data$attrs$Dates)) + data$coords[[sdate_dim]] <- data$attrs$Dates[1] + } + # startdates + if (is.null(startdates)) { + startdates <- data$coords[[sdate_dim]] } else { - if (length(dim(data$attrs$Dates)) == 1) { - sdate_dim <- 'sdate' - dim(data$data) <- c(sdate = 1, dim(data$data)) - data$dims <- dim(data$data) - dim(data$attrs$Dates) <- c(sdate = 1, dim(data$attrs$Dates)) - data$coords[[sdate_dim]] <- data$attrs$Dates[1] + if (!is.character(startdates)) { + warning(paste0("Parameter 'startdates' is not a character string, ", + "it will not be used.")) + startdates <- data$coords[[sdate_dim]] + } + if (!is.null(sdate_dim)) { + if (dim(data$data)[sdate_dim] != length(startdates)) { + warning(paste0("Parameter 'startdates' doesn't have the same length ", + "as dimension '", sdate_dim,"', it will not be used.")) + startdates <- data$coords[[sdate_dim]] + } } } @@ -141,7 +164,7 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', varname = data$attrs$Variable$varName, metadata = data$attrs$Variable$metadata, Datasets = data$attrs$Datasets, - startdates = data$coords[[sdate_dim]], + startdates = startdates, dat_dim = dat_dim, sdate_dim = sdate_dim, ftime_dim = ftime_dim, var_dim = var_dim, memb_dim = memb_dim, @@ -173,8 +196,12 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', #' lists for each variable. #'@param Datasets A vector of character string indicating the names of the #' datasets. -#'@param startdates A vector of dates indicating the initialization date of each -#' simulations. +#'@param startdates A vector of dates that will be used for the filenames +#' when saving the data in multiple files. It must be a vector of the same +#' length as the start date dimension of data. It must be a vector of class +#' \code{Dates}, \code{'POSIXct'} or character with lenghts between 1 and 10. +#' If it is NULL, the first Date of each time step will be used as the name of +#' the files. It is NULL by default. #'@param sdate_dim A character string indicating the name of the start date #' dimension. By default, it is set to 'sdate'. It can be NULL if there is no #' start date dimension. @@ -432,9 +459,8 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, all(names(dim(Dates)) == c(sdate_dim, ftime_dim))) { if (is.null(startdates)) { startdates <- Subset(Dates, along = ftime_dim, 1, drop = 'selected') - } else if ((!inherits(startdates, "POSIXct") & !inherits(startdates, "Date")) && - (!is.character(startdates) | (all(nchar(startdates) != 10) & - all(nchar(startdates) != 8) & all(nchar(startdates) != 6)))) { + } else if ((!inherits(startdates, "POSIXct") & !inherits(startdates, "Date")) && + (!is.character(startdates) | (any(nchar(startdates) > 10) | any(nchar(startdates) < 1)))) { warning("Parameter 'startdates' should be a character string containing ", "the start dates in the format 'yyyy-mm-dd', 'yyyymmdd', 'yyyymm', ", "'POSIXct' or 'Dates' class. Files will be named with Dates instead.") diff --git a/man/CST_SaveExp.Rd b/man/CST_SaveExp.Rd index 032f01c4ca3b3ee0b6c46bd0c09ee9bd2652e5bd..f8918eb9aa88ca8fae91b62951a31590378b8f27 100644 --- a/man/CST_SaveExp.Rd +++ b/man/CST_SaveExp.Rd @@ -12,6 +12,7 @@ CST_SaveExp( dat_dim = "dataset", var_dim = "var", memb_dim = "member", + startdates = NULL, single_file = FALSE, extra_string = NULL ) @@ -45,6 +46,14 @@ dimension.} By default, it is set to 'member'. It can be NULL if there is no member dimension.} +\item{startdates}{A vector of dates that will be used for the filenames +when saving the data in multiple files. It must be a vector of the same +length as the start date dimension of data. It must be a vector of class +\code{Dates}, \code{'POSIXct'} or character with lenghts between 1 and 10. +If it is NULL, the coordinate corresponding the the start date dimension or +the first Date of each time step will be used as the name of the files. +It is NULL by default.} + \item{single_file}{A logical value indicating if all object is saved in a single file (TRUE) or in multiple files (FALSE). When it is FALSE, the array is separated for Datasets, variable and start date. It is FALSE diff --git a/man/SaveExp.Rd b/man/SaveExp.Rd index d0418131c288acd2626fb90b3b76c58cbca8a0ec..a9c0ac36d9e65636fcb938c60b9fb88404555bcb 100644 --- a/man/SaveExp.Rd +++ b/man/SaveExp.Rd @@ -47,8 +47,12 @@ lists for each variable.} \item{Datasets}{A vector of character string indicating the names of the datasets.} -\item{startdates}{A vector of dates indicating the initialization date of each -simulations.} +\item{startdates}{A vector of dates that will be used for the filenames +when saving the data in multiple files. It must be a vector of the same +length as the start date dimension of data. It must be a vector of class +\code{Dates}, \code{'POSIXct'} or character with lenghts between 1 and 10. +If it is NULL, the first Date of each time step will be used as the name of +the files. It is NULL by default.} \item{dat_dim}{A character string indicating the name of dataset dimension. By default, it is set to 'dataset'. It can be NULL if there is no dataset diff --git a/tests/testthat/test-CST_SaveExp.R b/tests/testthat/test-CST_SaveExp.R index a05c5bd64d8b4d16b0130d8c5ccf06ec0ded4a86..00630d61b2da03e788f5a233fc893105c2af7f19 100644 --- a/tests/testthat/test-CST_SaveExp.R +++ b/tests/testthat/test-CST_SaveExp.R @@ -94,6 +94,18 @@ test_that("1. Input checks: CST_SaveExp", { paste0("Parameter 'sdate_dim' is not found in 'data' dimension.") ) ) + # # startdates + # expect_warning( + # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, + # dat_dim = NULL, var_dim = NULL, startdates = 1), + # "Parameter 'startdates' is not a character string, it will not be used." + # ) + # expect_warning( + # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, + # dat_dim = NULL, var_dim = NULL, startdates = '20100101'), + # paste0("Parameter 'startdates' doesn't have the same length ", + # "as dimension '", sdate_dim,"', it will not be used.") + # ) # # metadata # expect_warning( # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,