diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000000000000000000000000000000000000..fa596e707601da63df8c53cf4f087a70a953dbea --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,8 @@ +.*\.git$ +.*\.gitignore$ +.*\.tar.gz$ +.*\.pdf$ +./.nc$ +.*^(?!data)\.RData$ +.*\.gitlab-ci.yml$ +^tests$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35a09175a4a4e587947985914ec082148bc51880 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +.*.sw* +./*.nc +*.tar.gz +*.Rcheck +.Rd2pdf* +*~ +*.Rhistory +\#*\# +build_output.txt +check_output.txt +checkout_output.txt +merge_output.txt +master_pull.txt +*.eps +*.ps +Rplots.pdf +.nfs* +*.RData +!data/*.RData + diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..4db1f228e5b3890e93423d8c9200cf3617c8b51c --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,9 @@ +stages: + - build +build: + stage: build + script: + - module load R/3.6.1-foss-2015a-bare + - R CMD build --resave-data . + - R CMD check --as-cran --no-manual --run-donttest CSIndicators_*.tar.gz + - R -e 'covr::package_coverage()' diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000000000000000000000000000000000000..45093eef3112c7f74ca9914f6d9df7dbb0eec301 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,19 @@ +Package: CSIndicators +Title: Sectorial Indicators for Climate Services from Sub-Seasonal Forecast to Decadal Predictions +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. +Depends: + R (>= 3.6.1) +Imports: + multiApply (>= 2.1.1) +Suggests: + testthat, + CSTools +License: Apache License 2.0 +URL: https://earth.bsc.es/gitlab/es/csindicators/ +BugReports: https://earth.bsc.es/gitlab/es/csindicators/-/issues +LazyData: true +Encoding: UTF-8 +RoxygenNote: 7.0.1 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000000000000000000000000000000000000..80f1ec626b0f5352041b5a97cf65fc7dd2ee8a0e --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,7 @@ +# Generated by roxygen2: do not edit by hand + +export(CST_PeriodAccumulation) +export(PeriodAccumulation) +export(SelectPeriodOnData) +export(SelectPeriodOnDates) +import(multiApply) diff --git a/R/PeriodAccumulation.R b/R/PeriodAccumulation.R new file mode 100644 index 0000000000000000000000000000000000000000..083bb7f35b65de4331360e9024e8a2cb02b24196 --- /dev/null +++ b/R/PeriodAccumulation.R @@ -0,0 +1,137 @@ +#'Period Accumulation on 's2dv_cube' objects +#' +#'Period Accumulation computes the sum (accumulation) of a given variable in a period. +#'Providing precipitation data, two agriculture indices can be obtained by using this function: +#'\itemize{ +#' \item\code{SprR}{Spring Total Precipitation: The total precipitation from April 21th to June 21st} +#' \item\code{HarR}{Harvest Total Precipitation: The total precipitation from August 21st to October 21st}} +#' +#'@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_prec +#'TP <- CST_PeriodAccumulation(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-05-2000", format = "%d-%m-%Y"), +#' as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2001", format = "%d-%m-%Y"), +#' as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2002", format = "%d-%m-%Y"), +#' as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day')) +#'SprR <- CST_PeriodAccumulation(exp, start = list(21, 4), end = list(21, 6)) +#'dim(SprR$data) +#'head(SprR$Dates) +#'HarR <- CST_PeriodAccumulation(exp, start = list(21, 8), end = list(21, 10)) +#'dim(HarR$data) +#'head(HarR$Dates) +#'@export +CST_PeriodAccumulation <- function(data, start = NULL, end = NULL, + time_dim = 'ftime', na.rm = FALSE, + ncores = NULL) { + 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 and", + "all data would be used.") + } + } + } + total <- PeriodAccumulation(data$data, 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 Accumulation on multidimensional array objects +#' +#'Period Accumulation computes the sum (accumulation) of a given variable in a period. +#'Providing precipitation data, two agriculture indices can be obtained by using this function: +#'\itemize{ +#' \item\code{SprR}{Spring Total Precipitation: The total precipitation from April 21th to June 21st} +#' \item\code{HarR}{Harvest Total Precipitation: The total precipitation from August 21st to October 21st}} +#' +#'@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 +#'TP <- PeriodAccumulation(exp) +#'data <- array(rnorm(5 * 3 * 214 * 2), +#' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +#'Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), +#' as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2001", format = "%d-%m-%Y"), +#' as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2002", format = "%d-%m-%Y"), +#' as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day')) +#'SprR <- PeriodAccumulation(exp, dates = Dates, start = list(21, 4), end = list(21, 6)) +#'dim(SprR) +#'head(SprR) +#'HarR <- PeriodAccumulation(exp, dates = Dates, start = list(21, 8), end = list(21, 10)) +#'dim(HarR) +#'head(HarR) +#'@export +PeriodAccumulation <- 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 Total Accumulation 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 = sum, + na.rm = na.rm, ncores = ncores)$output1 +} + diff --git a/R/SelectPeriodOnData.R b/R/SelectPeriodOnData.R new file mode 100644 index 0000000000000000000000000000000000000000..d0dbb6023debe423403cd54a314ac0f347b3a486 --- /dev/null +++ b/R/SelectPeriodOnData.R @@ -0,0 +1,67 @@ +#' Select a period on Dates +#' +#' Auxiliary function to subset data for a specific period. +#' +#'@param data a multidimensional array with named dimensions. +#'@param dates a vector of dates or a multidimensional array of dates with named dimensions. +#'@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. +#'@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. +#'@param time_dim a character string indicating the name of the dimension to compute select the dates. 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 ncores an integer indicating the number of cores to use in parallel computation. +#' +#'@return A multidimensional array with named dimensions. +#' +#'@import multiApply +#' +#'@examples +#'data <- array(rnorm(5 * 3 * 214 * 2), +#' c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +#'Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), +#' as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2001", format = "%d-%m-%Y"), +#' as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2002", format = "%d-%m-%Y"), +#' as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day')) +#'Period <- SelectPeriodOnDates(Dates, start = list(21, 6), end = list(21, 9)) +#'@export +SelectPeriodOnData <- function(data, dates, start, end, + time_dim = 'ftime', ncores = NULL) { + if (is.null(dim(dates))) { + dim(dates) <- length(dates) + names(dim(dates)) <- time_dim + } + if (is.null(dim(data))) { + dim(data) <- length(data) + names(dim(data)) <- time_dim + } + res <- Apply(list(dates), target_dims = time_dim, + fun = .position, + ini_day = start[[1]], ini_month = start[[2]], + end_day = end[[1]], end_month = end[[2]], + ncores = ncores)$output1 + # when 29Feb is included the length of the output changes: + regular <- Apply(list(res), target_dims = time_dim, + fun = sum, ncores = ncores)$output1 + dims <- dim(data) + dims[names(dims) == time_dim] <- max(regular) + if (any(regular != max(regular))) { + res <- Apply(list(data, res), target_dims = time_dim, + fun = function(x, y) { + if (sum(y) != max(regular)) { + result <- c(x[y], NA) + } else { + result <- x[y] + } + dim(result) <- length(result) + names(dim(result)) <- names(dim(x)) + return(result) + }, ncores = ncores)$output1 + } else { + res <- Apply(list(data, res), target_dims = time_dim, + fun = function(x, y) { + res <- x[y] + }, ncores = ncores)$output1 + } + return(res) +} + diff --git a/R/SelectPeriodOnDates.R b/R/SelectPeriodOnDates.R new file mode 100644 index 0000000000000000000000000000000000000000..46ae7ff2b05d8547989dfe9d91867a28d0b266ae --- /dev/null +++ b/R/SelectPeriodOnDates.R @@ -0,0 +1,59 @@ +#' Select a period on Dates +#' +#' Auxiliary function to subset dates for a specific period. +#' +#'@param dates a vector of dates or a multidimensional array of dates with named dimensions. +#'@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. +#'@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. +#'@param time_dim a character string indicating the name of the dimension to compute select the dates. 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 ncores an integer indicating the number of cores to use in parallel computation. +#' +#'@return A multidimensional array with named dimensions. +#' +#'@import multiApply +#' +#'@examples +#'Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), +#' as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2001", format = "%d-%m-%Y"), +#' as.Date("30-11-2001", format = "%d-%m-%Y"), by = 'day'), +#' seq(as.Date("01-05-2002", format = "%d-%m-%Y"), +#' as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day')) +#'Period <- SelectPeriodOnDates(Dates, start = list(21, 6), end = list(21, 9)) +#'@export +SelectPeriodOnDates <- function(dates, start, end, + time_dim = 'ftime', ncores = NULL) { + # TODO: consider NAs + if (is.null(dim(dates))) { + dim(dates) <- length(dates) + names(dim(dates)) <- time_dim + } + res <- Apply(list(dates), target_dims = time_dim, + fun = .position, + ini_day = start[[1]], ini_month = start[[2]], + end_day = end[[1]], end_month = end[[2]], + ncores = ncores)$output1 + # when 29Feb is included the length of the output changes: + regular <- Apply(list(res), target_dims = time_dim, + fun = sum, ncores = ncores)$output1 + dims <- dim(dates) + dims[names(dims) == time_dim] <- max(regular) + if (any(regular != max(regular))) { + res <- Apply(list(dates, res), target_dims = time_dim, + fun = function(x, y) { + if (sum(y) != max(regular)) { + result <- c(x[y], NA) + } else { + result <- x[y] + } + dim(result) <- length(result) + names(dim(result)) <- names(dim(x)) + return(result) + }, ncores = ncores)$output1 + res <- as.POSIXct(res, origin = '1970-01-01', tz = 'UTC') + } else { + res <- dates[res] + dim(res) <- dims + } + return(res) +} diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000000000000000000000000000000000000..33660aa50e7013f6219bfeccabe8053a6a61fbd7 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,25 @@ +.position <- function(dates, ini_day, ini_month, end_day, end_month) { + days <- as.numeric(format(dates, "%d")) + months <- as.numeric(format(dates, "%m")) + pos <- 1:length(dates) + position <- logical(length(dates)) + if (ini_month != end_month) { + pos <- sort(unique(c(pos[months == ini_month & days >= ini_day], + pos[months < end_month & months > ini_month], + pos[months == end_month & days <= end_day]))) + position[pos] <- TRUE + position[-pos] <- FALSE + } else { + pos <- sort(unique(c(pos[months == ini_month & + days >= ini_day & days <= end_day]))) + position[pos] <- TRUE + position[-pos] <- FALSE + } + if (!is.null(dim(dates))) { + dim(position) <- length(position) + if(!is.null(names(dim(dates)))) { + names(dim(position)) <- names(dim(dates)) + } + } + return(position) +} diff --git a/man/CST_PeriodAccumulation.Rd b/man/CST_PeriodAccumulation.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0337a136b7d2c5e5c063666f0fb58a22a0ed2c72 --- /dev/null +++ b/man/CST_PeriodAccumulation.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PeriodAccumulation.R +\name{CST_PeriodAccumulation} +\alias{CST_PeriodAccumulation} +\title{Period Accumulation on 's2dv_cube' objects} +\usage{ +CST_PeriodAccumulation( + 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 Accumulation computes the sum (accumulation) of a given variable in a period. +Providing precipitation data, two agriculture indices can be obtained by using this function: +\itemize{ + \item\code{SprR}{Spring Total Precipitation: The total precipitation from April 21th to June 21st} + \item\code{HarR}{Harvest Total Precipitation: The total precipitation from August 21st to October 21st}} +} +\examples{ +exp <- CSTools::lonlat_prec +TP <- CST_PeriodAccumulation(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-05-2000", format = "\%d-\%m-\%Y"), + as.Date("30-11-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2001", format = "\%d-\%m-\%Y"), + as.Date("30-11-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2002", format = "\%d-\%m-\%Y"), + as.Date("30-11-2002", format = "\%d-\%m-\%Y"), by = 'day')) +SprR <- CST_PeriodAccumulation(exp, start = list(21, 4), end = list(21, 6)) +dim(SprR$data) +head(SprR$Dates) +HarR <- CST_PeriodAccumulation(exp, start = list(21, 8), end = list(21, 10)) +dim(HarR$data) +head(HarR$Dates) +} diff --git a/man/PeriodAccumulation.Rd b/man/PeriodAccumulation.Rd new file mode 100644 index 0000000000000000000000000000000000000000..2998170d2b2d12f04d6e98408082a609d1e61a6b --- /dev/null +++ b/man/PeriodAccumulation.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PeriodAccumulation.R +\name{PeriodAccumulation} +\alias{PeriodAccumulation} +\title{Period Accumulation on multidimensional array objects} +\usage{ +PeriodAccumulation( + 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 Accumulation computes the sum (accumulation) of a given variable in a period. +Providing precipitation data, two agriculture indices can be obtained by using this function: +\itemize{ + \item\code{SprR}{Spring Total Precipitation: The total precipitation from April 21th to June 21st} + \item\code{HarR}{Harvest Total Precipitation: The total precipitation from August 21st to October 21st}} +} +\examples{ +exp <- CSTools::lonlat_prec$data +TP <- PeriodAccumulation(exp) +data <- array(rnorm(5 * 3 * 214 * 2), + c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +Dates <- c(seq(as.Date("01-05-2000", format = "\%d-\%m-\%Y"), + as.Date("30-11-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2001", format = "\%d-\%m-\%Y"), + as.Date("30-11-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2002", format = "\%d-\%m-\%Y"), + as.Date("30-11-2002", format = "\%d-\%m-\%Y"), by = 'day')) +SprR <- PeriodAccumulation(exp, dates = Dates, start = list(21, 4), end = list(21, 6)) +dim(SprR) +head(SprR) +HarR <- PeriodAccumulation(exp, dates = Dates, start = list(21, 8), end = list(21, 10)) +dim(HarR) +head(HarR) +} diff --git a/man/SelectPeriodOnData.Rd b/man/SelectPeriodOnData.Rd new file mode 100644 index 0000000000000000000000000000000000000000..7c83f69830088c1948bfb5f152869b0b772fdbb9 --- /dev/null +++ b/man/SelectPeriodOnData.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/SelectPeriodOnData.R +\name{SelectPeriodOnData} +\alias{SelectPeriodOnData} +\title{Select a period on Dates} +\usage{ +SelectPeriodOnData(data, dates, start, end, time_dim = "ftime", 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.} + +\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.} + +\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.} + +\item{time_dim}{a character string indicating the name of the dimension to compute select the dates. 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{ncores}{an integer indicating the number of cores to use in parallel computation.} +} +\value{ +A multidimensional array with named dimensions. +} +\description{ +Auxiliary function to subset data for a specific period. +} +\examples{ +data <- array(rnorm(5 * 3 * 214 * 2), + c(memb = 5, sdate = 3, ftime = 214, lon = 2)) +Dates <- c(seq(as.Date("01-05-2000", format = "\%d-\%m-\%Y"), + as.Date("30-11-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2001", format = "\%d-\%m-\%Y"), + as.Date("30-11-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2002", format = "\%d-\%m-\%Y"), + as.Date("30-11-2002", format = "\%d-\%m-\%Y"), by = 'day')) +Period <- SelectPeriodOnDates(Dates, start = list(21, 6), end = list(21, 9)) +} diff --git a/man/SelectPeriodOnDates.Rd b/man/SelectPeriodOnDates.Rd new file mode 100644 index 0000000000000000000000000000000000000000..2cd553ac529bbea5bbe6f140e4a4f13fd346fdc3 --- /dev/null +++ b/man/SelectPeriodOnDates.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/SelectPeriodOnDates.R +\name{SelectPeriodOnDates} +\alias{SelectPeriodOnDates} +\title{Select a period on Dates} +\usage{ +SelectPeriodOnDates(dates, start, end, time_dim = "ftime", ncores = NULL) +} +\arguments{ +\item{dates}{a vector of dates or a multidimensional array of dates with named dimensions.} + +\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.} + +\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.} + +\item{time_dim}{a character string indicating the name of the dimension to compute select the dates. 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{ncores}{an integer indicating the number of cores to use in parallel computation.} +} +\value{ +A multidimensional array with named dimensions. +} +\description{ +Auxiliary function to subset dates for a specific period. +} +\examples{ +Dates <- c(seq(as.Date("01-05-2000", format = "\%d-\%m-\%Y"), + as.Date("30-11-2000", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2001", format = "\%d-\%m-\%Y"), + as.Date("30-11-2001", format = "\%d-\%m-\%Y"), by = 'day'), + seq(as.Date("01-05-2002", format = "\%d-\%m-\%Y"), + as.Date("30-11-2002", format = "\%d-\%m-\%Y"), by = 'day')) +Period <- SelectPeriodOnDates(Dates, start = list(21, 6), end = list(21, 9)) +}