From f776e17cefeefa047dd2880c347b6ef4a923bcea Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 19 Oct 2022 13:48:52 +0200 Subject: [PATCH 1/2] Correct warning in CST_ Dates initial checks for all functions --- R/AbsToProbs.R | 10 +++++----- R/AccumulationExceedingThreshold.R | 10 +++++----- R/MergeRefToExp.R | 14 +++++++------- R/PeriodAccumulation.R | 6 +++--- R/PeriodMean.R | 4 ++-- R/QThreshold.R | 10 +++++----- R/SelectPeriodOnData.R | 6 +++--- R/Threshold.R | 10 +++++----- R/TotalSpellTimeExceedingThreshold.R | 18 +++++++++--------- R/TotalTimeExceedingThreshold.R | 18 +++++++++--------- R/WindCapacityFactor.R | 4 ++-- R/WindPowerDensity.R | 8 ++++---- tests/testthat/test-PeriodMean.R | 16 +++++++++++----- 13 files changed, 70 insertions(+), 64 deletions(-) diff --git a/R/AbsToProbs.R b/R/AbsToProbs.R index ce5f01a..89a493b 100644 --- a/R/AbsToProbs.R +++ b/R/AbsToProbs.R @@ -58,13 +58,13 @@ CST_AbsToProbs <- function(data, start = NULL, end = NULL, if (is.null(dim(data$Dates$start))) { if (length(data$Dates$start) != dim(data$data)[time_dim]) { if (length(data$Dates$start) == - prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { + prod(dim(data$data)[time_dim] * dim(data$data)[sdate_dim])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], - dim(data$data)['sdate']) + dim(data$data)[sdate_dim]) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } } diff --git a/R/AccumulationExceedingThreshold.R b/R/AccumulationExceedingThreshold.R index 62a4bc4..74a24a9 100644 --- a/R/AccumulationExceedingThreshold.R +++ b/R/AccumulationExceedingThreshold.R @@ -60,17 +60,17 @@ CST_AccumulationExceedingThreshold <- function(data, threshold, op = '>', if (!is.null(start) && !is.null(end)) { if (is.null(dim(data$Dates$start))) { if (length(data$Dates$start) != dim(data$data)[time_dim]) { - if (length(data$Dates$start) == + if (length(data$Dates$start) == prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } - } + } if (inherits(threshold, 's2dv_cube')) { threshold <- threshold$data } diff --git a/R/MergeRefToExp.R b/R/MergeRefToExp.R index 837d304..ad5bb06 100644 --- a/R/MergeRefToExp.R +++ b/R/MergeRefToExp.R @@ -77,11 +77,11 @@ CST_MergeRefToExp <- function(data1, data2, start1, end1, start2, end2, if (length(data1$Dates$start) == prod(dim(data1$data)[time_dim] * dim(data1$data)['sdate'])) { dim(data1$Dates$start) <- c(dim(data1$data)[time_dim], - dim(data1$data)['sdate']) + dim(data1$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'data$Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'data$Dates$start' are missed and", - "all data would be used.") } } # when subsetting is needed, dimensions are also needed: @@ -91,10 +91,10 @@ CST_MergeRefToExp <- function(data1, data2, start1, end1, start2, end2, prod(dim(data2$data)[time_dim] * dim(data2$data)['sdate'])) { dim(data2$Dates$start) <- c(dim(data2$data)[time_dim], dim(data2$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'data$Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'data$Dates$start' are missed and", - "all data would be used.") } } data1$data <- MergeRefToExp(data1 = data1$data, dates1 = data1$Dates[[1]], diff --git a/R/PeriodAccumulation.R b/R/PeriodAccumulation.R index 92ec947..0fcf315 100644 --- a/R/PeriodAccumulation.R +++ b/R/PeriodAccumulation.R @@ -68,10 +68,10 @@ CST_PeriodAccumulation <- function(data, start = NULL, end = NULL, prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } } diff --git a/R/PeriodMean.R b/R/PeriodMean.R index c75cee3..73700fd 100644 --- a/R/PeriodMean.R +++ b/R/PeriodMean.R @@ -56,9 +56,9 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") } } } diff --git a/R/QThreshold.R b/R/QThreshold.R index 6b4359d..2b9702a 100644 --- a/R/QThreshold.R +++ b/R/QThreshold.R @@ -80,13 +80,13 @@ CST_QThreshold <- function(data, threshold, start = NULL, end = NULL, if (is.null(dim(data$Dates$start))) { if (length(data$Dates$start) != dim(data$data)[time_dim]) { if (length(data$Dates$start) == - prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { + prod(dim(data$data)[time_dim] * dim(data$data)[sdate_dim])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], - dim(data$data)['sdate']) + dim(data$data)[sdate_dim]) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } } diff --git a/R/SelectPeriodOnData.R b/R/SelectPeriodOnData.R index f15c60e..5bd9bb6 100644 --- a/R/SelectPeriodOnData.R +++ b/R/SelectPeriodOnData.R @@ -47,10 +47,10 @@ CST_SelectPeriodOnData <- function(data, start, end, time_dim = 'ftime', ncores prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } } diff --git a/R/Threshold.R b/R/Threshold.R index 755c2e6..933b3fd 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -64,13 +64,13 @@ CST_Threshold <- function(data, threshold, start = NULL, end = NULL, if (is.null(dim(data$Dates$start))) { if (length(data$Dates$start) != dim(data$data)[time_dim]) { if (length(data$Dates$start) == - prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { + prod(dim(data$data)[time_dim] * dim(data$data)[sdate_dim])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], - dim(data$data)['sdate']) + dim(data$data)[sdate_dim]) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } } diff --git a/R/TotalSpellTimeExceedingThreshold.R b/R/TotalSpellTimeExceedingThreshold.R index 97981ee..2e56d67 100644 --- a/R/TotalSpellTimeExceedingThreshold.R +++ b/R/TotalSpellTimeExceedingThreshold.R @@ -68,20 +68,20 @@ CST_TotalSpellTimeExceedingThreshold <- function(data, threshold, spell, op = '> prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } - } - if (inherits(threshold, 's2dv_cube')) { + } + if (inherits(threshold, 's2dv_cube')) { threshold <- threshold$data } - total <- TotalSpellTimeExceedingThreshold(data$data, data$Dates[[1]], - threshold = threshold, spell = spell, op = op, - start = start, end = end, time_dim = time_dim, - ncores = ncores) + total <- TotalSpellTimeExceedingThreshold(data$data, data$Dates[[1]], + threshold = threshold, spell = spell, op = op, + start = start, end = end, time_dim = time_dim, + ncores = ncores) data$data <- total if (!is.null(start) && !is.null(end)) { data$Dates <- SelectPeriodOnDates(dates = data$Dates$start, diff --git a/R/TotalTimeExceedingThreshold.R b/R/TotalTimeExceedingThreshold.R index ee0d8ac..136fd35 100644 --- a/R/TotalTimeExceedingThreshold.R +++ b/R/TotalTimeExceedingThreshold.R @@ -72,20 +72,20 @@ CST_TotalTimeExceedingThreshold <- function(data, threshold, op = '>', prod(dim(data$data)[time_dim] * dim(data$data)['sdate'])) { dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed and", + "all data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", - "all data would be used.") } } - } - if (inherits(threshold, 's2dv_cube')) { + } + if (inherits(threshold, 's2dv_cube')) { threshold <- threshold$data } - total <- TotalTimeExceedingThreshold(data$data, data$Dates[[1]], - threshold = threshold, op = op, - start = start, end = end, time_dim = time_dim, - na.rm = na.rm, ncores = ncores) + total <- TotalTimeExceedingThreshold(data$data, data$Dates[[1]], + threshold = threshold, op = op, + start = start, end = end, time_dim = time_dim, + na.rm = na.rm, ncores = ncores) data$data <- total if (!is.null(start) && !is.null(end)) { data$Dates <- SelectPeriodOnDates(dates = data$Dates$start, diff --git a/R/WindCapacityFactor.R b/R/WindCapacityFactor.R index 61565b0..3b59da7 100644 --- a/R/WindCapacityFactor.R +++ b/R/WindCapacityFactor.R @@ -63,9 +63,9 @@ CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/II prod(dim(wind$data)[time_dim] * dim(wind$data)['sdate'])) { dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], dim(wind$data)['sdate']) + } else { + warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") } - } else { - warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") } } } diff --git a/R/WindPowerDensity.R b/R/WindPowerDensity.R index 99d5d80..15aacf4 100644 --- a/R/WindPowerDensity.R +++ b/R/WindPowerDensity.R @@ -53,9 +53,9 @@ CST_WindPowerDensity <- function(wind, ro = 1.225, start = NULL, end = NULL, prod(dim(wind$data)[time_dim] * dim(wind$data)['sdate'])) { dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], dim(wind$data)['sdate']) + } else { + warning("Dimensions in 'wind' element 'Dates$start' are missed/unmatched. All data would be used.") } - } else { - warning("Dimensions in 'wind' element 'Dates$start' are missed/unmatched. All data would be used.") } } } @@ -68,8 +68,8 @@ CST_WindPowerDensity <- function(wind, ro = 1.225, start = NULL, end = NULL, } if (!is.null(start) && !is.null(end)) { wind$Dates <- SelectPeriodOnDates(dates = wind$Dates[[1]], - start = start, end = end, - time_dim = time_dim, ncores = ncores) + start = start, end = end, + time_dim = time_dim, ncores = ncores) } return(wind) } diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R index c9a0a37..75b6d57 100644 --- a/tests/testthat/test-PeriodMean.R +++ b/tests/testthat/test-PeriodMean.R @@ -2,15 +2,21 @@ context("Generic tests") test_that("Sanity Checks", { #source("csindicators/R/PeriodMean.R") expect_error(PeriodMean('x'), "Parameter 'data' must be numeric.") - expect_equal(PeriodMean(array(1, c(x = 1)), time_dim = 'x'), 1) + suppressWarnings( + expect_equal(PeriodMean(array(1, c(x = 1)), time_dim = 'x'), 1) + ) + expect_error(PeriodMean(data = NULL), "Parameter 'data' cannot be NULL.") expect_error(PeriodMean(1, dates = '2000-01-01', end = 3, start = 4), "Parameter 'start' and 'end' must be lists indicating the day and the month of the period start and end.") - - expect_equal(PeriodMean(array(1:10, c(time = 10))), 5.5) + suppressWarnings( + expect_equal(PeriodMean(array(1:10, c(time = 10))), 5.5) + ) data <- array(1:24, c(sdate = 2, time = 3, lon = 4)) - expect_equal(PeriodMean(data), - array(c(3,4,9,10,15,16,21,22), c(sdate = 2, lon = 4))) + suppressWarnings( + expect_equal(PeriodMean(data), + array(c(3,4,9,10,15,16,21,22), c(sdate = 2, lon = 4))) + ) }) test_that("seasonal", { -- GitLab From cea4f4a1f5927bac650b2ee5045bd0cae7647ae0 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 19 Oct 2022 17:17:35 +0200 Subject: [PATCH 2/2] Add space in warning texts for all functions --- R/AbsToProbs.R | 2 +- R/AccumulationExceedingThreshold.R | 2 +- R/MergeRefToExp.R | 2 +- R/PeriodAccumulation.R | 2 +- R/QThreshold.R | 2 +- R/SelectPeriodOnData.R | 2 +- R/Threshold.R | 2 +- R/TotalSpellTimeExceedingThreshold.R | 2 +- R/TotalTimeExceedingThreshold.R | 2 +- R/WindCapacityFactor.R | 3 ++- R/WindPowerDensity.R | 3 ++- 11 files changed, 13 insertions(+), 11 deletions(-) diff --git a/R/AbsToProbs.R b/R/AbsToProbs.R index 89a493b..89c72c9 100644 --- a/R/AbsToProbs.R +++ b/R/AbsToProbs.R @@ -62,7 +62,7 @@ CST_AbsToProbs <- function(data, start = NULL, end = NULL, dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)[sdate_dim]) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/AccumulationExceedingThreshold.R b/R/AccumulationExceedingThreshold.R index 74a24a9..62845ba 100644 --- a/R/AccumulationExceedingThreshold.R +++ b/R/AccumulationExceedingThreshold.R @@ -65,7 +65,7 @@ CST_AccumulationExceedingThreshold <- function(data, threshold, op = '>', dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/MergeRefToExp.R b/R/MergeRefToExp.R index ad5bb06..068acfe 100644 --- a/R/MergeRefToExp.R +++ b/R/MergeRefToExp.R @@ -92,7 +92,7 @@ CST_MergeRefToExp <- function(data1, data2, start1, end1, start2, end2, dim(data2$Dates$start) <- c(dim(data2$data)[time_dim], dim(data2$data)['sdate']) } else { - warning("Dimensions in 'data' element 'data$Dates$start' are missed and", + warning("Dimensions in 'data' element 'data$Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/PeriodAccumulation.R b/R/PeriodAccumulation.R index 0fcf315..5a672e0 100644 --- a/R/PeriodAccumulation.R +++ b/R/PeriodAccumulation.R @@ -69,7 +69,7 @@ CST_PeriodAccumulation <- function(data, start = NULL, end = NULL, dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/QThreshold.R b/R/QThreshold.R index 2b9702a..4663411 100644 --- a/R/QThreshold.R +++ b/R/QThreshold.R @@ -84,7 +84,7 @@ CST_QThreshold <- function(data, threshold, start = NULL, end = NULL, dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)[sdate_dim]) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/SelectPeriodOnData.R b/R/SelectPeriodOnData.R index 5bd9bb6..49575a2 100644 --- a/R/SelectPeriodOnData.R +++ b/R/SelectPeriodOnData.R @@ -48,7 +48,7 @@ CST_SelectPeriodOnData <- function(data, start, end, time_dim = 'ftime', ncores dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/Threshold.R b/R/Threshold.R index 933b3fd..18b8dea 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -68,7 +68,7 @@ CST_Threshold <- function(data, threshold, start = NULL, end = NULL, dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)[sdate_dim]) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/TotalSpellTimeExceedingThreshold.R b/R/TotalSpellTimeExceedingThreshold.R index 2e56d67..9a5de43 100644 --- a/R/TotalSpellTimeExceedingThreshold.R +++ b/R/TotalSpellTimeExceedingThreshold.R @@ -69,7 +69,7 @@ CST_TotalSpellTimeExceedingThreshold <- function(data, threshold, spell, op = '> dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/TotalTimeExceedingThreshold.R b/R/TotalTimeExceedingThreshold.R index 136fd35..5e800f0 100644 --- a/R/TotalTimeExceedingThreshold.R +++ b/R/TotalTimeExceedingThreshold.R @@ -73,7 +73,7 @@ CST_TotalTimeExceedingThreshold <- function(data, threshold, op = '>', dim(data$Dates$start) <- c(dim(data$data)[time_dim], dim(data$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed and", + warning("Dimensions in 'data' element 'Dates$start' are missed and ", "all data would be used.") } } diff --git a/R/WindCapacityFactor.R b/R/WindCapacityFactor.R index 3b59da7..659e6d4 100644 --- a/R/WindCapacityFactor.R +++ b/R/WindCapacityFactor.R @@ -64,7 +64,8 @@ CST_WindCapacityFactor <- function(wind, IEC_class = c("I", "I/II", "II", "II/II dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], dim(wind$data)['sdate']) } else { - warning("Dimensions in 'data' element 'Dates$start' are missed/unmatched. All data would be used.") + warning("Dimensions in 'data' element 'Dates$start' are missed and ", + "all data would be used.") } } } diff --git a/R/WindPowerDensity.R b/R/WindPowerDensity.R index 15aacf4..6bbc597 100644 --- a/R/WindPowerDensity.R +++ b/R/WindPowerDensity.R @@ -54,7 +54,8 @@ CST_WindPowerDensity <- function(wind, ro = 1.225, start = NULL, end = NULL, dim(wind$Dates$start) <- c(dim(wind$data)[time_dim], dim(wind$data)['sdate']) } else { - warning("Dimensions in 'wind' element 'Dates$start' are missed/unmatched. All data would be used.") + warning("Dimensions in 'data' element 'Dates$start' are missed and ", + "all data would be used.") } } } -- GitLab