From df500f387dd5f49b0bb2c5678e404a84738d5059 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 15 Oct 2020 10:43:02 +0200 Subject: [PATCH 1/5] Reordering indices and adding parameter new_dim_name --- R/CST_SplitDim.R | 24 ++++++++++++++++++++---- man/CST_SplitDim.Rd | 10 +++++++++- man/SplitDim.Rd | 10 +++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/R/CST_SplitDim.R b/R/CST_SplitDim.R index 46cd97cc..e9e6eeed 100644 --- a/R/CST_SplitDim.R +++ b/R/CST_SplitDim.R @@ -8,6 +8,7 @@ #'@param split_dim a character string indicating the name of the dimension to split #'@param indices a vector of numeric indices or dates. If left at NULL, the dates provided in the s2dv_cube object (element Dates) will be used. #'@param freq a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independently of the year they belong to, while 'monthly' differenciates months from different years. +#'@param new_dim_name a character string indicating the name of the new dimension. #' #'@import abind #'@import s2dverification @@ -34,7 +35,7 @@ #'dim(new_data$data) #'@export CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, - freq = 'monthly') { + freq = 'monthly', new_dim_name = NULL) { if (!inherits(data, 's2dv_cube')) { stop("Parameter 'data' must be of the class 's2dv_cube', ", "as output by CSTools::CST_Load.") @@ -68,7 +69,7 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, #'@param split_dim a character string indicating the name of the dimension to split #'@param indices a vector of numeric indices or dates #'@param freq a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension -#' +#'@param new_dim_name a character string indicating the name of the new dimension. #'@import abind #'@import s2dverification #'@examples @@ -85,7 +86,8 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, #'new_data <- SplitDim(data, indices = time, freq = 'month') #'new_data <- SplitDim(data, indices = time, freq = 'year') #'@export -SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly') { +SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', + new_dim_name = NULL) { # check data if (is.null(data)) { stop("Parameter 'data' cannot be NULL.") @@ -173,7 +175,18 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly') { "distinguishable month).") } } - repited <- unique(indices) + # check new_dim_name + if (!is.null(new_dim_name)) { + if (!is.character(new_dim_name)) { + stop("Parameter 'new_dim_name' must be character string") + } + if (length(new_dim_name) > 1) { + new_dim_name <- new_dim_name[1] + warning("Parameter 'new_dim_name' has length greater than 1 ", + "and only the first elemenst is used.") + } + } + repited <- sort(unique(indices)) max_times <- max(unlist(lapply(repited, function(x){sum(indices == x)}))) data <- lapply(repited, function(x) {rebuild(x, data, along = split_dim, @@ -184,6 +197,9 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly') { } else { names(dim(data)) <- c(names(dims), 'index') } + if (!is.null(new_dim_name)) { + names(dim(data)) <- c(names(dims), new_dim_name) + } return(data) } diff --git a/man/CST_SplitDim.Rd b/man/CST_SplitDim.Rd index ee93aedc..11aca488 100644 --- a/man/CST_SplitDim.Rd +++ b/man/CST_SplitDim.Rd @@ -4,7 +4,13 @@ \alias{CST_SplitDim} \title{Function to Split Dimension} \usage{ -CST_SplitDim(data, split_dim = "time", indices = NULL, freq = "monthly") +CST_SplitDim( + data, + split_dim = "time", + indices = NULL, + freq = "monthly", + new_dim_name = NULL +) } \arguments{ \item{data}{a 's2dv_cube' object} @@ -14,6 +20,8 @@ CST_SplitDim(data, split_dim = "time", indices = NULL, freq = "monthly") \item{indices}{a vector of numeric indices or dates. If left at NULL, the dates provided in the s2dv_cube object (element Dates) will be used.} \item{freq}{a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independently of the year they belong to, while 'monthly' differenciates months from different years.} + +\item{new_dim_name}{a character string indicating the name of the new dimension.} } \description{ This function split a dimension in two. The user can select the dimension to split and provide indices indicating how to split that dimension or dates and the frequency expected (monthly or by day, month and year). The user can also provide a numeric frequency indicating the length of each division. diff --git a/man/SplitDim.Rd b/man/SplitDim.Rd index f07e4756..e172bdd4 100644 --- a/man/SplitDim.Rd +++ b/man/SplitDim.Rd @@ -4,7 +4,13 @@ \alias{SplitDim} \title{Function to Split Dimension} \usage{ -SplitDim(data, split_dim = "time", indices, freq = "monthly") +SplitDim( + data, + split_dim = "time", + indices, + freq = "monthly", + new_dim_name = NULL +) } \arguments{ \item{data}{an n-dimensional array with named dimensions} @@ -14,6 +20,8 @@ SplitDim(data, split_dim = "time", indices, freq = "monthly") \item{indices}{a vector of numeric indices or dates} \item{freq}{a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension} + +\item{new_dim_name}{a character string indicating the name of the new dimension.} } \description{ This function split a dimension in two. The user can select the dimension to split and provide indices indicating how to split that dimension or dates and the frequency expected (monthly or by day, month and year). The user can also provide a numeric frequency indicating the length of each division. -- GitLab From 245ac2b47f9a32b3de2cc1e9aeee3ce538dc9f03 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 15 Oct 2020 12:05:28 +0200 Subject: [PATCH 2/5] only sorting when indices are numeric in SplitDims --- R/CST_SplitDim.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/CST_SplitDim.R b/R/CST_SplitDim.R index e9e6eeed..4d6350ea 100644 --- a/R/CST_SplitDim.R +++ b/R/CST_SplitDim.R @@ -69,6 +69,7 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, #'@param split_dim a character string indicating the name of the dimension to split #'@param indices a vector of numeric indices or dates #'@param freq a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension +nthly' #'@param new_dim_name a character string indicating the name of the new dimension. #'@import abind #'@import s2dverification @@ -132,6 +133,7 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', warning("Parameter 'freq' is not being used since ", "parameter 'indices' is numeric.") } + repited <- sort(unique(indices)) } } else { # Indices should be Dates and freq character @@ -163,12 +165,16 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', if (!is.numeric(indices)) { if (freq == 'day') { indices <- as.numeric(strftime(indices, format = "%d")) + repited <- unique(indices) } else if (freq == 'month') { indices <- as.numeric(strftime(indices, format = "%m")) + repited <- unique(indices) } else if (freq == 'year') { indices <- as.numeric(strftime(indices, format = "%Y")) + repited <- unique(indices) } else if (freq == 'monthly' ) { indices <- as.numeric(strftime(indices, format = "%m%Y")) + repited <- unique(indices) } else { stop("Parameter 'freq' must be numeric or a character: ", "by 'day', 'month', 'year' or 'monthly' (for ", @@ -186,7 +192,6 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', "and only the first elemenst is used.") } } - repited <- sort(unique(indices)) max_times <- max(unlist(lapply(repited, function(x){sum(indices == x)}))) data <- lapply(repited, function(x) {rebuild(x, data, along = split_dim, -- GitLab From 851548403835176efbb2f7ddd7106db894b4c7df Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 15 Oct 2020 12:09:38 +0200 Subject: [PATCH 3/5] incorrect end of line --- R/CST_SplitDim.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/CST_SplitDim.R b/R/CST_SplitDim.R index 4d6350ea..a68b91f1 100644 --- a/R/CST_SplitDim.R +++ b/R/CST_SplitDim.R @@ -68,8 +68,7 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, #'@param data an n-dimensional array with named dimensions #'@param split_dim a character string indicating the name of the dimension to split #'@param indices a vector of numeric indices or dates -#'@param freq a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension -nthly' +#'@param freq a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension. #'@param new_dim_name a character string indicating the name of the new dimension. #'@import abind #'@import s2dverification -- GitLab From 1adb0735600cbcf7f198004bb3879757889a8739 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 15 Oct 2020 17:17:12 +0200 Subject: [PATCH 4/5] Fixes and new tests added for SplitDims --- R/CST_SplitDim.R | 7 +++++-- tests/testthat/test-CST_SplitDim.R | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/R/CST_SplitDim.R b/R/CST_SplitDim.R index a68b91f1..3326d435 100644 --- a/R/CST_SplitDim.R +++ b/R/CST_SplitDim.R @@ -41,6 +41,7 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, "as output by CSTools::CST_Load.") } if (is.null(indices)) { + if (any(split_dim %in% c('ftime', 'time', 'sdate'))) { if (is.list(data$Dates)) { indices <- data$Dates[[1]] } else { @@ -54,9 +55,10 @@ CST_SplitDim <- function(data, split_dim = 'time', indices = NULL, indices <- indices[1 : dim(data$data)[which(names(dim(data$data)) == split_dim)]] } + } } data$data <- SplitDim(data$data, split_dim = split_dim, indices = indices, - freq = freq) + freq = freq, new_dim_name = new_dim_name) return(data) } #'Function to Split Dimension @@ -125,6 +127,7 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', } indices <- rep(1 : (dims[pos_split] / freq), freq) indices <- sort(indices) + repited <- sort(unique(indices)) } } else if (is.numeric(indices)) { if (!is.null(freq)) { @@ -132,8 +135,8 @@ SplitDim <- function(data, split_dim = 'time', indices, freq = 'monthly', warning("Parameter 'freq' is not being used since ", "parameter 'indices' is numeric.") } - repited <- sort(unique(indices)) } + repited <- sort(unique(indices)) } else { # Indices should be Dates and freq character if (!is.character(freq)) { diff --git a/tests/testthat/test-CST_SplitDim.R b/tests/testthat/test-CST_SplitDim.R index 800344c1..d595b76f 100644 --- a/tests/testthat/test-CST_SplitDim.R +++ b/tests/testthat/test-CST_SplitDim.R @@ -73,4 +73,20 @@ library(CSTools) result$data <- output expect_equal(CST_SplitDim(data = lonlat_data$exp, split_dim = 'ftime'), result) + + expect_equal(dim(CST_SplitDim(data = lonlat_data$exp, split_dim = 'member', + freq = 5)$data), + c(dataset = 1, member = 5, sdate = 6, ftime = 3, + lat = 22, lon = 53, index = 3)) + expect_warning(CST_SplitDim(data = lonlat_data$exp, split_dim = 'member', + freq = 5, new_dim_name = c('a', 'b')), + paste0("Parameter 'new_dim_name' has length greater than 1 ", + "and only the first elemenst is used.")) + expect_error(CST_SplitDim(data = lonlat_data$exp, split_dim = 'member', + freq = 5, new_dim_name = 3), + "Parameter 'new_dim_name' must be character string") + expect_equal(dim(CST_SplitDim(data = lonlat_data$exp, split_dim = 'member', + freq = 5, new_dim_name = 'wt')$data), + c(dataset = 1, member = 5, sdate = 6, ftime = 3, + lat = 22, lon = 53, wt = 3)) }) -- GitLab From d39d7f241bdec2804af1eaa95fe6efca4d7662e1 Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 28 Oct 2020 17:31:20 +0100 Subject: [PATCH 5/5] NEWS and doc updated --- NEWS.md | 1 + man/SplitDim.Rd | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 92ad49b1..e57b8358 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ - Fixes: + PlotForecastPDF correctly displays terciles labels + CST_SaveExp correctly save time units + + CST_SplitDims returns ordered output following ascending order provided in indices when it is numeric ### CSTools 3.1.0 **Submission date to CRAN: 02-07-2020** diff --git a/man/SplitDim.Rd b/man/SplitDim.Rd index e172bdd4..a4904306 100644 --- a/man/SplitDim.Rd +++ b/man/SplitDim.Rd @@ -19,7 +19,7 @@ SplitDim( \item{indices}{a vector of numeric indices or dates} -\item{freq}{a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension} +\item{freq}{a character string indicating the frequency: by 'day', 'month' and 'year' or 'monthly' (by default). 'month' identifies months between 1 and 12 independetly of the year they belong to, while 'monthly' differenciates months from different years. Parameter 'freq' can also be numeric indicating the length in which to subset the dimension.} \item{new_dim_name}{a character string indicating the name of the new dimension.} } -- GitLab