From 6cf8054771d5c494e43d8e68b8b9c5ee3f06017a Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Wed, 27 Jan 2021 09:20:30 +0100 Subject: [PATCH 01/11] PeriodMean.R added --- R/PeriodMean.R | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 R/PeriodMean.R diff --git a/R/PeriodMean.R b/R/PeriodMean.R new file mode 100644 index 0000000..4c02f39 --- /dev/null +++ b/R/PeriodMean.R @@ -0,0 +1,145 @@ +#'Period Mean on 's2dv_cube' objects +#' +#'Period Mean computes the average (mean) of a given variable in a period. +#'Providing temperature data, two agriculture indices can be obtain by using this function: +#'\itemize{ +#' \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} +#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st} +#' +#'@param data an 's2dv_cube' object as provided function \code{CST_Load} in package CSTools. +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param na.rm a logical value indicating whether to ignore NA values (TRUE) or not (FALSE). +#'@param ncores an integer indicating the number of cores to use in parallel computation. +#' +#'@return A 's2dv_cube' object containing the indicator in the element \code{data}. +#' +#'@import multiApply +#' +#'@examples +#'exp <- CSTools::lonlat_data$exp +#'SA <- CST_PeriodMean(exp) +#'exp$data <- array(rnorm(5 * 3 * 214 * 2), +#' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +#'exp$Dates[[1]] <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), +#' as.Date("31-10-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-04-2001", format = "%d-%m-%Y"), +#' as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-04-2002", format = "%d-%m-%Y"), +#' as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) +#'SprTX <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 5)) +#'dim(SprTX$data) +#'head(SprTX$Dates) +#'mean(exp$data[1,1,c(1:(30+31)),1]) # April 1st - May 31st +#'SprTX$data[1,1,1] +#'GST <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 10)) +#'dim(GST$data) +#'head(GST$Dates) +#'mean(exp$data[1,1,,1]) +#'GST$data[1,1,1] +#'@export +CST_PeriodMean <- function(data, start = NULL, end = NULL, + time_dim = 'ftime', na.rm = FALSE, + ncores = NULL) { +# Consider to add an option for providing tx and tn in data + if (!inherits(data, 's2dv_cube')) { + stop("Parameter 'data' must be of the class 's2dv_cube', ", + "as output by CSTools::CST_Load.") + } + # when subsetting is needed, dimensions are also needed: + 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) == + 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.") + } + } + } + total <- PeriodMean(data = data$data, dates = data$Dates[[1]], start, 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[[1]], + start = start, end = end, + time_dim = time_dim, ncores = ncores) + } + return(data) +} + +#'Period Mean on multidimensional array objects +#' +#'Period Mean computes the average (mean) of a given variable in a period. +#'Providing temperature data, two agriculture indices can be obtain by using this function: +#'\itemize{ +#' \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} +#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st} +#' +#'@param data a multidimensional array with named dimensions. +#'@param dates a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided. +#'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param end an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. +#'@param time_dim a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified. +#'@param na.rm a logical value indicating whether to ignore NA values (TRUE) or not (FALSE). +#'@param ncores an integer indicating the number of cores to use in parallel computation. +#' +#'@return A multidimensional array with named dimensions. +#' +#'@import multiApply +#' +#'@examples +#'exp <- CSTools::lonlat_prec$data +#'SA <- PeriodMean(exp) +#'data <- array(rnorm(5 * 3 * 214 * 2), +#' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +#'Dates <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), +#' as.Date("31-10-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-04-2001", format = "%d-%m-%Y"), +#' as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-04-2002", format = "%d-%m-%Y"), +#' as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) +#'SprTX <- PeriodMean(data, dates = Dates, start = list(1, 4), end = list(31, 5)) +#'dim(SprTX) +#'head(SprTX) +#' +#'GST <- PeriodMean(data, dates = Dates, start = list(1, 4), end = list(31, 10)) +#'dim(GST) +#'head(GST) +#' +#'@export +PeriodMean <- function(data, dates = NULL, start = NULL, end = NULL, + time_dim = 'ftime', na.rm = FALSE, ncores = NULL) { + + if (is.null(data)) { + stop("Parameter 'data' cannot be NULL.") + } + if (!is.numeric(data)) { + stop("Parameter 'data' must be numeric.") + } + if (!is.array(data)) { + dim(data) <- length(data) + names(data) <- time_dim + } + if (is.null(dates)) { + warning("Parameter 'dates' is NULL and the Average of the ", + "full data provided in 'data' is computed.") + } else { + if (!is.null(start) && !is.null(end)) { + if (!any(c(is.list(start), is.list(end)))) { + stop("Parameter 'start' and 'end' must be lists indicating the ", + "day and the month of the period start and end.") + } + data <- SelectPeriodOnData(data, dates, start, end, + time_dim = time_dim, ncores = ncores) + } + } + total <- Apply(list(data), target_dims = time_dim, fun = mean, + na.rm = na.rm, ncores = ncores)$output1 +} + + -- GitLab From aa97e6758589d0f21efb7827d2fe9d56e67a4940 Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Thu, 28 Jan 2021 07:45:58 +0100 Subject: [PATCH 02/11] examples updated --- R/PeriodMean.R | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/R/PeriodMean.R b/R/PeriodMean.R index 4c02f39..438236b 100644 --- a/R/PeriodMean.R +++ b/R/PeriodMean.R @@ -20,6 +20,8 @@ #'@examples #'exp <- CSTools::lonlat_data$exp #'SA <- CST_PeriodMean(exp) +#'all.equal(mean(exp$data[1,1,1,,1,1]), SA$data[1,1,1,1,1]) +#' #'exp$data <- array(rnorm(5 * 3 * 214 * 2), #' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) #'exp$Dates[[1]] <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), @@ -31,13 +33,9 @@ #'SprTX <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 5)) #'dim(SprTX$data) #'head(SprTX$Dates) -#'mean(exp$data[1,1,c(1:(30+31)),1]) # April 1st - May 31st -#'SprTX$data[1,1,1] -#'GST <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 10)) -#'dim(GST$data) -#'head(GST$Dates) -#'mean(exp$data[1,1,,1]) -#'GST$data[1,1,1] +#'all.equal(mean(exp$data[1,1,1:61,1]), SprTX$data[1,1,1]) +#'all.equal(mean(exp$data[1,2,1:61,1]), SprTX$data[1,2,1]) +#'all.equal(mean(exp$data[1,3,1:61,1]), SprTX$data[1,3,1]) #'@export CST_PeriodMean <- function(data, start = NULL, end = NULL, time_dim = 'ftime', na.rm = FALSE, @@ -95,6 +93,8 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #'@examples #'exp <- CSTools::lonlat_prec$data #'SA <- PeriodMean(exp) +#'all.equal(mean(exp[1,1,1,,1,1]), SA[1,1,1,1,1]) +#' #'data <- array(rnorm(5 * 3 * 214 * 2), #' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) #'Dates <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), @@ -103,13 +103,11 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #' as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'), #' seq(as.Date("01-04-2002", format = "%d-%m-%Y"), #' as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) -#'SprTX <- PeriodMean(data, dates = Dates, start = list(1, 4), end = list(31, 5)) -#'dim(SprTX) -#'head(SprTX) -#' -#'GST <- PeriodMean(data, dates = Dates, start = list(1, 4), end = list(31, 10)) -#'dim(GST) -#'head(GST) +#'dim(Dates) <- c(ftime = 214, sdate = 3) +#'SprTX <- PeriodMean(data, Dates, start = list(1, 4), end = list(31, 5)) +#'all.equal(mean(data[1,1,1:61,1]), SprTX[1,1,1]) +#'all.equal(mean(data[1,2,1:61,1]), SprTX[1,2,1]) +#'all.equal(mean(data[1,3,1:61,1]), SprTX[1,3,1]) #' #'@export PeriodMean <- function(data, dates = NULL, start = NULL, end = NULL, -- GitLab From 60792a7c1b8bf5367739aac2b96d8fe492bb1b88 Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Thu, 28 Jan 2021 15:25:20 +0100 Subject: [PATCH 03/11] a test for seasonal forecasts added --- tests/testthat.R | 4 ++++ tests/testthat/test-PeriodMean.R | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-PeriodMean.R diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..79cf4a7 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(CSIndicators) + +test_check("CSIndicators") diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R new file mode 100644 index 0000000..fbcddd3 --- /dev/null +++ b/tests/testthat/test-PeriodMean.R @@ -0,0 +1,38 @@ +context("Generic tests") +test_that("Sanity Checks", { + #source("csindicators/R/PeriodMean.R") + expect_error(PeriodMean('x'), "Parameter 'data' must be numeric.") + expect_equal(PeriodMean(1), 1) + expect_equal(PeriodMean(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(1: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))) +}) + +test_that("seasonal", { + + exp <- CSTools::lonlat_prec + exp$data <- array(1:(1 * 3 * 214 * 2), + c(memb = 1, sdate = 3, ftime = 214, lon = 2)) + exp$Dates[[1]] <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), + as.Date("31-10-2000", format = "%d-%m-%Y"), by = 'day'), + seq(as.Date("01-04-2001", format = "%d-%m-%Y"), + as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'), + seq(as.Date("01-04-2002", format = "%d-%m-%Y"), + as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) + output <- exp + output$data <- array(c(mean(exp$data[1,1,21:82,1]), mean(exp$data[1,2,21:82,1]), + mean(exp$data[1,3,21:82,1]), mean(exp$data[1,1,21:82,2]), + mean(exp$data[1,2,21:82,2]), mean(exp$data[1,3,21:82,2])), + c(memb = 1, sdate = 3, lon = 2)) + expect_equal( + CST_PeriodMean(exp, start = list(21, 4), end = list(21, 6))$data, + output$data) + + + +}) -- GitLab From 4ba8c79fb72dc9f1c6147dfd875c26b087b6a78d Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Thu, 28 Jan 2021 15:30:18 +0100 Subject: [PATCH 04/11] bug fix --- tests/testthat/test-PeriodMean.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R index fbcddd3..a7ed289 100644 --- a/tests/testthat/test-PeriodMean.R +++ b/tests/testthat/test-PeriodMean.R @@ -8,7 +8,7 @@ test_that("Sanity Checks", { 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(1:10), 5.5) - data <- array(1:24, c(sdate = 2, time = 3, lon = 4)) + data <- array(1:24, c(sdate = 2, ftime = 3, lon = 4)) expect_equal(PeriodMean(data), array(c(3,4,9,10,15,16,21,22), c(sdate = 2, lon = 4))) }) -- GitLab From 89a3b4f5bda88edf59ee2c57fccedb372cd3ee02 Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Thu, 28 Jan 2021 15:43:05 +0100 Subject: [PATCH 05/11] bug fix --- tests/testthat/test-PeriodMean.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R index a7ed289..6202d46 100644 --- a/tests/testthat/test-PeriodMean.R +++ b/tests/testthat/test-PeriodMean.R @@ -7,7 +7,8 @@ test_that("Sanity Checks", { 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(1:10), 5.5) + + expect_equal(PeriodMean(array(1:10, c(ftime = 10))), 5.5) data <- array(1:24, c(sdate = 2, ftime = 3, lon = 4)) expect_equal(PeriodMean(data), array(c(3,4,9,10,15,16,21,22), c(sdate = 2, lon = 4))) -- GitLab From c8ef0d1a9ca381af993157b910799b3d87d3fcd2 Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Thu, 28 Jan 2021 16:11:28 +0100 Subject: [PATCH 06/11] bug fix for test-PeriodMean.R --- tests/testthat/test-PeriodMean.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R index 6202d46..a77f55f 100644 --- a/tests/testthat/test-PeriodMean.R +++ b/tests/testthat/test-PeriodMean.R @@ -2,8 +2,7 @@ context("Generic tests") test_that("Sanity Checks", { #source("csindicators/R/PeriodMean.R") expect_error(PeriodMean('x'), "Parameter 'data' must be numeric.") - expect_equal(PeriodMean(1), 1) - expect_equal(PeriodMean(1, time_dim = 'x'), 1) + 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.") -- GitLab From dd4266f3f152873538fbb5678ebccf29cc44fd2f Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Fri, 29 Jan 2021 04:32:18 +0100 Subject: [PATCH 07/11] Description updated --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 45093ee..a60be75 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Title: Sectorial Indicators for Climate Services from Sub-Seasonal Forecast to D Version: 0.0.1 Authors@R: c( person("Nuria", "Perez-Zanon", , "nuria.perez@bsc.es", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-8568-3071"))) -Description: To be added. +Description: The package contains the definition-based computation of the sectoral indicators for the Climate Service. Depends: R (>= 3.6.1) Imports: -- GitLab From cf48894d4363a9fa0f9dcd51fcba1161cce3a312 Mon Sep 17 00:00:00 2001 From: Chihchung Chou Date: Wed, 3 Feb 2021 15:54:54 +0100 Subject: [PATCH 08/11] documentation revised --- R/PeriodMean.R | 9 --------- 1 file changed, 9 deletions(-) diff --git a/R/PeriodMean.R b/R/PeriodMean.R index 438236b..672334f 100644 --- a/R/PeriodMean.R +++ b/R/PeriodMean.R @@ -20,7 +20,6 @@ #'@examples #'exp <- CSTools::lonlat_data$exp #'SA <- CST_PeriodMean(exp) -#'all.equal(mean(exp$data[1,1,1,,1,1]), SA$data[1,1,1,1,1]) #' #'exp$data <- array(rnorm(5 * 3 * 214 * 2), #' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) @@ -33,9 +32,6 @@ #'SprTX <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 5)) #'dim(SprTX$data) #'head(SprTX$Dates) -#'all.equal(mean(exp$data[1,1,1:61,1]), SprTX$data[1,1,1]) -#'all.equal(mean(exp$data[1,2,1:61,1]), SprTX$data[1,2,1]) -#'all.equal(mean(exp$data[1,3,1:61,1]), SprTX$data[1,3,1]) #'@export CST_PeriodMean <- function(data, start = NULL, end = NULL, time_dim = 'ftime', na.rm = FALSE, @@ -93,7 +89,6 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #'@examples #'exp <- CSTools::lonlat_prec$data #'SA <- PeriodMean(exp) -#'all.equal(mean(exp[1,1,1,,1,1]), SA[1,1,1,1,1]) #' #'data <- array(rnorm(5 * 3 * 214 * 2), #' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) @@ -105,10 +100,6 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #' as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) #'dim(Dates) <- c(ftime = 214, sdate = 3) #'SprTX <- PeriodMean(data, Dates, start = list(1, 4), end = list(31, 5)) -#'all.equal(mean(data[1,1,1:61,1]), SprTX[1,1,1]) -#'all.equal(mean(data[1,2,1:61,1]), SprTX[1,2,1]) -#'all.equal(mean(data[1,3,1:61,1]), SprTX[1,3,1]) -#' #'@export PeriodMean <- function(data, dates = NULL, start = NULL, end = NULL, time_dim = 'ftime', na.rm = FALSE, ncores = NULL) { -- GitLab From 20480af0abe0940ac11f0ec0c9b2c51333cc6b40 Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 3 Feb 2021 17:10:16 +0100 Subject: [PATCH 09/11] Automatic docu and fixes --- NAMESPACE | 2 ++ R/PeriodMean.R | 4 ++-- man/CST_PeriodMean.Rd | 54 +++++++++++++++++++++++++++++++++++++++++ man/PeriodMean.Rd | 56 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 man/CST_PeriodMean.Rd create mode 100644 man/PeriodMean.Rd diff --git a/NAMESPACE b/NAMESPACE index 80f1ec6..cac4862 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,9 @@ # Generated by roxygen2: do not edit by hand export(CST_PeriodAccumulation) +export(CST_PeriodMean) export(PeriodAccumulation) +export(PeriodMean) export(SelectPeriodOnData) export(SelectPeriodOnDates) import(multiApply) diff --git a/R/PeriodMean.R b/R/PeriodMean.R index 672334f..5b27b2f 100644 --- a/R/PeriodMean.R +++ b/R/PeriodMean.R @@ -4,7 +4,7 @@ #'Providing temperature data, two agriculture indices can be obtain by using this function: #'\itemize{ #' \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} -#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st} +#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st}} #' #'@param data an 's2dv_cube' object as provided function \code{CST_Load} in package CSTools. #'@param start an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}. @@ -72,7 +72,7 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #'Providing temperature data, two agriculture indices can be obtain by using this function: #'\itemize{ #' \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} -#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st} +#' \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st}} #' #'@param data a multidimensional array with named dimensions. #'@param dates a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided. diff --git a/man/CST_PeriodMean.Rd b/man/CST_PeriodMean.Rd new file mode 100644 index 0000000..bbc1fe3 --- /dev/null +++ b/man/CST_PeriodMean.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PeriodMean.R +\name{CST_PeriodMean} +\alias{CST_PeriodMean} +\title{Period Mean on 's2dv_cube' objects} +\usage{ +CST_PeriodMean( + data, + start = NULL, + end = NULL, + time_dim = "ftime", + na.rm = FALSE, + ncores = NULL +) +} +\arguments{ +\item{data}{an 's2dv_cube' object as provided function \code{CST_Load} in package CSTools.} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{na.rm}{a logical value indicating whether to ignore NA values (TRUE) or not (FALSE).} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation.} +} +\value{ +A 's2dv_cube' object containing the indicator in the element \code{data}. +} +\description{ +Period Mean computes the average (mean) of a given variable in a period. +Providing temperature data, two agriculture indices can be obtain by using this function: +\itemize{ + \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} + \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st}} +} +\examples{ +exp <- CSTools::lonlat_data$exp +SA <- CST_PeriodMean(exp) + +exp$data <- array(rnorm(5 * 3 * 214 * 2), + c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +exp$Dates[[1]] <- c(seq(as.Date("01-04-2000", format = "\%d-\%m-\%Y"), + as.Date("31-10-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-04-2001", format = "\%d-\%m-\%Y"), + as.Date("31-10-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-04-2002", format = "\%d-\%m-\%Y"), + as.Date("31-10-2002", format = "\%d-\%m-\%Y"), by = 'day')) +SprTX <- CST_PeriodMean(exp, start = list(1, 4), end = list(31, 5)) +dim(SprTX$data) +head(SprTX$Dates) +} diff --git a/man/PeriodMean.Rd b/man/PeriodMean.Rd new file mode 100644 index 0000000..e9d915a --- /dev/null +++ b/man/PeriodMean.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PeriodMean.R +\name{PeriodMean} +\alias{PeriodMean} +\title{Period Mean on multidimensional array objects} +\usage{ +PeriodMean( + data, + dates = NULL, + start = NULL, + end = NULL, + time_dim = "ftime", + na.rm = FALSE, + ncores = NULL +) +} +\arguments{ +\item{data}{a multidimensional array with named dimensions.} + +\item{dates}{a vector of dates or a multidimensional array of dates with named dimensions matching the dimensions on parameter 'data'. By default it is NULL, to select a period this parameter must be provided.} + +\item{start}{an optional parameter to defined the initial date of the period to select from the data by providing a list of two elements: the initial date of the period and the initial month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{end}{an optional parameter to defined the final date of the period to select from the data by providing a list of two elements: the final day of the period and the final month of the period. By default it is set to NULL and the indicator is computed using all the data provided in \code{data}.} + +\item{time_dim}{a character string indicating the name of the function to compute the indicator. By default, it is set to 'ftime'. More than one dimension name matching the dimensions provided in the object \code{data$data} can be specified.} + +\item{na.rm}{a logical value indicating whether to ignore NA values (TRUE) or not (FALSE).} + +\item{ncores}{an integer indicating the number of cores to use in parallel computation.} +} +\value{ +A multidimensional array with named dimensions. +} +\description{ +Period Mean computes the average (mean) of a given variable in a period. +Providing temperature data, two agriculture indices can be obtain by using this function: +\itemize{ + \item\code{GST}{Growing Season average Temperature: The average temperature from April 1st to Octobe 31st} + \item\code{SprTX}{Spring Average Maximum Temperature: The average daily maximum temperature from April 1st to May 31st}} +} +\examples{ +exp <- CSTools::lonlat_prec$data +SA <- PeriodMean(exp) + +data <- array(rnorm(5 * 3 * 214 * 2), + c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +Dates <- c(seq(as.Date("01-04-2000", format = "\%d-\%m-\%Y"), + as.Date("31-10-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-04-2001", format = "\%d-\%m-\%Y"), + as.Date("31-10-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-04-2002", format = "\%d-\%m-\%Y"), + as.Date("31-10-2002", format = "\%d-\%m-\%Y"), by = 'day')) +dim(Dates) <- c(ftime = 214, sdate = 3) +SprTX <- PeriodMean(data, Dates, start = list(1, 4), end = list(31, 5)) +} -- GitLab From 05179dcb9dd817c39ea2c901b3ddaa3e6e567e9e Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 3 Feb 2021 17:55:59 +0100 Subject: [PATCH 10/11] standarization of time_dim among functions --- R/PeriodMean.R | 8 ++++---- man/PeriodMean.Rd | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/PeriodMean.R b/R/PeriodMean.R index 5b27b2f..fa1b478 100644 --- a/R/PeriodMean.R +++ b/R/PeriodMean.R @@ -88,21 +88,21 @@ CST_PeriodMean <- function(data, start = NULL, end = NULL, #' #'@examples #'exp <- CSTools::lonlat_prec$data -#'SA <- PeriodMean(exp) +#'SA <- PeriodMean(exp, time_dim = 'ftime') #' #'data <- array(rnorm(5 * 3 * 214 * 2), -#' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +#' c(memb = 5, sdate = 3, time = 214, lon = 2)) #'Dates <- c(seq(as.Date("01-04-2000", format = "%d-%m-%Y"), #' as.Date("31-10-2000", format = "%d-%m-%Y"), by = 'day'), #' seq(as.Date("01-04-2001", format = "%d-%m-%Y"), #' as.Date("31-10-2001", format = "%d-%m-%Y"), by = 'day'), #' seq(as.Date("01-04-2002", format = "%d-%m-%Y"), #' as.Date("31-10-2002", format = "%d-%m-%Y"), by = 'day')) -#'dim(Dates) <- c(ftime = 214, sdate = 3) +#'dim(Dates) <- c(time = 214, sdate = 3) #'SprTX <- PeriodMean(data, Dates, start = list(1, 4), end = list(31, 5)) #'@export PeriodMean <- function(data, dates = NULL, start = NULL, end = NULL, - time_dim = 'ftime', na.rm = FALSE, ncores = NULL) { + time_dim = 'time', na.rm = FALSE, ncores = NULL) { if (is.null(data)) { stop("Parameter 'data' cannot be NULL.") diff --git a/man/PeriodMean.Rd b/man/PeriodMean.Rd index e9d915a..b0fd0c3 100644 --- a/man/PeriodMean.Rd +++ b/man/PeriodMean.Rd @@ -9,7 +9,7 @@ PeriodMean( dates = NULL, start = NULL, end = NULL, - time_dim = "ftime", + time_dim = "time", na.rm = FALSE, ncores = NULL ) @@ -41,16 +41,16 @@ Providing temperature data, two agriculture indices can be obtain by using this } \examples{ exp <- CSTools::lonlat_prec$data -SA <- PeriodMean(exp) +SA <- PeriodMean(exp, time_dim = 'ftime') data <- array(rnorm(5 * 3 * 214 * 2), - c(memb = 5, sdate = 3, ftime = 214, lon = 2)) + c(memb = 5, sdate = 3, time = 214, lon = 2)) Dates <- c(seq(as.Date("01-04-2000", format = "\%d-\%m-\%Y"), as.Date("31-10-2000", format = "\%d-\%m-\%Y"), by = 'day'), seq(as.Date("01-04-2001", format = "\%d-\%m-\%Y"), as.Date("31-10-2001", format = "\%d-\%m-\%Y"), by = 'day'), seq(as.Date("01-04-2002", format = "\%d-\%m-\%Y"), as.Date("31-10-2002", format = "\%d-\%m-\%Y"), by = 'day')) -dim(Dates) <- c(ftime = 214, sdate = 3) +dim(Dates) <- c(time = 214, sdate = 3) SprTX <- PeriodMean(data, Dates, start = list(1, 4), end = list(31, 5)) } -- GitLab From 42babe8b26d13f100603b2946186c0aed663ec0f Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 3 Feb 2021 18:23:51 +0100 Subject: [PATCH 11/11] fixing tests --- .Rbuildignore | 1 - tests/testthat/test-PeriodMean.R | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index fa596e7..62a63bb 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,4 +5,3 @@ ./.nc$ .*^(?!data)\.RData$ .*\.gitlab-ci.yml$ -^tests$ diff --git a/tests/testthat/test-PeriodMean.R b/tests/testthat/test-PeriodMean.R index a77f55f..c9a0a37 100644 --- a/tests/testthat/test-PeriodMean.R +++ b/tests/testthat/test-PeriodMean.R @@ -7,8 +7,8 @@ test_that("Sanity Checks", { 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(ftime = 10))), 5.5) - data <- array(1:24, c(sdate = 2, ftime = 3, lon = 4)) + 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))) }) -- GitLab