diff --git a/NAMESPACE b/NAMESPACE index bafe27bad1f0612d7521b11d0ab8a8e715c722a2..bca5e7302fb4bb6e8b0a5ecf102e1335f1816f9f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,8 +5,13 @@ export(CST_AccumulationExceedingThreshold) export(CST_PeriodAccumulation) export(CST_PeriodMean) export(CST_SelectPeriodOnData) +export(CST_Threshold) +export(CST_TotalSpellTimeExceedingThreshold) export(PeriodAccumulation) export(PeriodMean) export(SelectPeriodOnData) export(SelectPeriodOnDates) +export(Threshold) +export(TotalSpellTimeExceedingThreshold) import(multiApply) +importFrom(stats,quantile) diff --git a/R/Threshold.R b/R/Threshold.R new file mode 100644 index 0000000000000000000000000000000000000000..c09ebe004fe6424f41edcf1623f058ca24ebb225 --- /dev/null +++ b/R/Threshold.R @@ -0,0 +1,143 @@ +#'Absolute value of a relative threshold (percentile) +#' +#'Frequently, thresholds are defined by a percentile that may correspond to a different absolute value depending on the variable, gridpoint and also julian day (time). +#' This function calculates the corresponding value of a percentile given a dataset. +#' +#'@param data an 's2dv_cube' object as provided function \code{CST_Load} in package CSTools. +#'@param threshold a single scalar or vector indicating the relative threshold(s). +#'@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 temporal dimension. 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. This dimension is required to subset the data in a requested period. +#'@param memb_dim a character string indicating the name of the dimension in which the ensemble members are stored. When set it to NULL, threshold is computed for individual members. +#'@param sdate_dim a character string indicating the name of the dimension in which the initialization dates are stored. +#'@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 probabilites in the element \code{data}. +#' +#'@import multiApply +#' +#'@examples +#'threshold <- 0.9 +#'exp <- CSTools::lonlat_prec +#'exp_probs <- CST_Threshold(exp, threshold) +#'exp$data <- array(rnorm(5 * 3 * 214 * 2), +#' c(member = 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')) +#'exp_probs <- CST_Threshold(exp, threshold, start = list(21, 4), end = list(21, 6)) +#'@export +CST_Threshold <- function(data, threshold, start = NULL, end = NULL, + time_dim = 'ftime', memb_dim = 'member', sdate_dim = 'sdate', + 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.") + } + } + } + thres <- Threshold(data$data, threshold, data$Dates[[1]], start, end, + time_dim = time_dim, memb_dim = memb_dim, + sdate_dim = sdate_dim, na.rm = na.rm, ncores = ncores) + data$data <- thres + 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) +} +#'Absolute value of a relative threshold (percentile) +#' +#'Frequently, thresholds are defined by a percentile that may correspond to a different absolute value depending on the variable, gridpoint and also julian day (time). +#' This function calculates the corresponding value of a percentile given a dataset. +#' +#'@param data a multidimensional array with named dimensions. +#'@param threshold a single scalar or vector indicating the relative threshold(s). +#'@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 temporal dimension. 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. This dimension is required to subset the data in a requested period. +#'@param memb_dim a character string indicating the name of the dimension in which the ensemble members are stored. When set it to NULL, threshold is computed for individual members. +#'@param sdate_dim a character string indicating the name of the dimension in which the initialization dates are stored. +#'@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 +#'@importFrom stats quantile +#' +#'@examples +#'threshold <- 0.9 +#'data <- array(rnorm(25 * 3 * 214 * 2, mean = 26), +#' c(member = 25, sdate = 3, time = 214, lon = 2)) +#'thres_q <- Threshold(data, threshold) +#'data <- array(rnorm(1 * 3 * 214 * 2), c(member = 1, sdate = 3, time = 214, lon = 2)) +#'res <- Threshold(data, threshold) +#'@export +Threshold <- function(data, threshold, dates = NULL, start = NULL, end = NULL, + time_dim = 'time', memb_dim = 'member', sdate_dim = 'sdate', + 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) <- c(length(data), 1) + names(dim(data)) <- c(memb_dim, sdate_dim) + } + if (is.null(threshold)) { + stop("Parameter 'threshold' cannot be NULL.") + } + if (!is.numeric(threshold)) { + stop("Parameter 'threshold' must be numeric.") + } + if (is.null(names(dim(data)))) { + stop("Parameter 'data' must have named dimensions.") + } + if (!is.null(dates)) { + 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) + } + } + if (!is.null(memb_dim)) { + dimensions <- c(sdate_dim, memb_dim) + } else { + dimensions <- sdate_dim + } + if (length(threshold) == 1) { + thres <- Apply(data, target_dims = dimensions, + fun = function(x) {quantile(as.vector(x), threshold, na.rm)})$output1 + } else { + thres <- Apply(data, target_dims = dimensions, + fun = function(x) {quantile(as.vector(x), threshold, na.rm)}, + output_dims = 'probs')$output1 + } + return(thres) +} diff --git a/R/TotalSpellTimeExceedingThreshold.R b/R/TotalSpellTimeExceedingThreshold.R new file mode 100644 index 0000000000000000000000000000000000000000..45c3203efe4e0fb4f617bcc2f8091fe07c0e3d4b --- /dev/null +++ b/R/TotalSpellTimeExceedingThreshold.R @@ -0,0 +1,187 @@ +#'Total Spell Time Exceeding Threshold +#' +#'The number of days (when daily data is provided) that are part of a spell (defined by its minimum length e.g. 6 consecutive days) that exceed (or not exceed) a threshold are calculated with \code{TotalSpellTimeExceedingThreshold}. +#'This function allows to compute indicators widely used in Climate Services, such as: +#'\itemize{ +#' \code{WSDI}{Warm Spell Duration Index that count the total number of days with at least 6 consecutive days when the daily temperature maximum exceeds its 90th percentile.}} +#'This function requires the data and the threshold to be in the same units. The 90th percentile can be translate into absolute values given a reference dataset using function \code{Threshold} or the data can be transform into probabilites by using function \code{AbsToProbs}. See section @examples. +#'@seealso [Threshold()] and [AbsToProbs()]. +#' +#'@param data an 's2dv_cube' object as provided by function \code{CST_Load} in package CSTools. +#'@param threshold an 's2dv_cube' object as output of a 'CST_' function in the same units as parameter 'data' and with the common dimensions of the element 'data' of the same length. A single scalar is also possible. If \code{timd_dim} is in the dimension (with the same length as \code{data}), the comparison will be done day by day. +#'@param spell a scalar indicating the minimum length of the spell. +#'@param op a opartor '>' (by default), '<', '>=' or '<='. +#'@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 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 +#'TTSET <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = 283, spell = 3) +#'exp$data <- array(rnorm(5 * 3 * 214 * 2, mean = 25, sd = 3), +#' c(member = 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')) +#'# compare with the scalar fixed threshold +#'WSDI <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = 35, spell = 6) +#'# compare with percentile +#'thresholdP <- CST_Threshold(exp, threshold = 0.9) +#'WSDI_P <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = thresholdP, spell = 6) +#'@export +CST_TotalSpellTimeExceedingThreshold <- function(data, threshold, spell, op = '>', + start = NULL, end = NULL, + time_dim = 'ftime', + 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.") + } + } + } + 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) + data$data <- total + if (!is.null(start) && !is.null(end)) { + data$Dates <- SelectPeriodOnDates(dates = data$Dates$start, + start = start, end = end, + time_dim = time_dim, ncores = ncores) + } + return(data) +} +#'Total Spell Time Exceeding Threshold +#' +#'The number of days (when daily data is provided) that are part of a spell (defined by its minimum length e.g. 6 consecutive days) that exceed (or not exceed) a threshold are calculated with \code{TotalSpellTimeExceedingThreshold}. +#'This function allows to compute indicators widely used in Climate Services, such as: +#'\itemize{ +#' \code{WSDI}{Warm Spell Duration Index that count the total number of days with at least 6 consecutive days when the daily temperature maximum exceeds its 90th percentile.}} +#'This function requires the data and the threshold to be in the same units. The 90th percentile can be translate into absolute values given a reference dataset using function \code{Threshold} or the data can be transform into probabilites by using function \code{AbsToProbs}. See section @examples. +#'@seealso [Threshold()] and [AbsToProbs()]. +#' +#'@param data a multidimensional array with named dimensions. +#'@param threshold a multidimensional array with named dimensions in the same units as parameter 'data' and with the common dimensions of the element 'data' of the same length. If \code{timd_dim} is in the dimension (with the same length as \code{data}), the comparison will be done day by day. +#'@param spell a scalar indicating the minimum length of the spell. +#'@param op a opartor '>' (by default), '<', '>=' or '<='. +#'@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 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(120), c(member = 1, sdate = 2, time = 20, lat = 4)) +#'threshold <- array(rnorm(4), c(lat = 4)) +#'total <- TotalSpellTimeExceedingThreshold(data, threshold, spell = 6) +#'# compare with percentile +#'thresholdP <- Threshold(data, threshold = 0.9) +#'total_P <- TotalSpellTimeExceedingThreshold(data, threshold = thresholdP, spell = 6) +#'@export +TotalSpellTimeExceedingThreshold <- function(data, threshold, spell, op = '>', dates = NULL, + start = NULL, end = NULL, time_dim = 'time', + 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(dim(data)) <- time_dim + } + if (is.null(threshold)) { + stop("Parameter 'threshold' cannot be NULL.") + } + if (!is.numeric(threshold)) { + stop("Parameter 'threshold' must be numeric.") + } + if (is.null(names(dim(data)))) { + stop("Parameter 'data' must have named dimensions.") + } + if (!is.null(dates)) { + 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.") + } + if (time_dim %in% names(dim(threshold))) { + if (dim(threshold)[time_dim] == dim(data)[time_dim]) { + threshold <- SelectPeriodOnData(threshold, dates, start, end, + time_dim = time_dim, ncores = ncores) + } + } + data <- SelectPeriodOnData(data, dates, start, end, + time_dim = time_dim, ncores = ncores) + } + } + if (is.null(dim(threshold))) { + total <- Apply(list(data), target_dims = time_dim, + fun = .totalspellthres, + threshold = threshold, spell = spell, op = op, + ncores = ncores)$output1 + } else if (time_dim %in% names(dim(threshold))) { + total <- Apply(list(data, threshold), target_dims = list(time_dim, time_dim), + fun = .totalspellthres, spell = spell, op = op)$output1 + + } else { + total <- Apply(list(data, threshold), target_dims = list(time_dim, NULL), + fun = .totalspellthres, spell = spell, op = op)$output1 + } + return(total) +} +#data <- c(1,1,3,3,3,3,1,1,3,1,1,3,3,3,3,3) +#spell <- 3 +#threshold <- 2 +.totalspellthres <- function(data, threshold, spell, op = '>') { + # data a time serie, threshold single value: + if (op == '>') { + exceed <- data > threshold + } else if (op == '<') { + exceed <- data < threshold + } else if (op == '<=') { + exceed <- data <= threshold + } else { + exceed <- data >= threshold + } + spells_exceed <- sequence(rle(as.character(exceed))$lengths) + spells_exceed[exceed == FALSE] <- NA + pos_spells <- which(spells_exceed == spell) + total <- sum(unlist(lapply(pos_spells, function(y) { + last_days <- x <- y + while (!is.na(x)) { + x <- spells_exceed[last_days + 1] + last_days <- last_days + 1 + } + days <- length((y - spell + 1): (last_days - 1)) + return(days) + }))) + return(total) +} diff --git a/man/CST_Threshold.Rd b/man/CST_Threshold.Rd new file mode 100644 index 0000000000000000000000000000000000000000..bff93fa02f40b70111feb0785de243bb378d5d93 --- /dev/null +++ b/man/CST_Threshold.Rd @@ -0,0 +1,58 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Threshold.R +\name{CST_Threshold} +\alias{CST_Threshold} +\title{Absolute value of a relative threshold (percentile)} +\usage{ +CST_Threshold( + data, + threshold, + start = NULL, + end = NULL, + time_dim = "ftime", + memb_dim = "member", + sdate_dim = "sdate", + na.rm = FALSE, + ncores = NULL +) +} +\arguments{ +\item{data}{an 's2dv_cube' object as provided function \code{CST_Load} in package CSTools.} + +\item{threshold}{a single scalar or vector indicating the relative threshold(s).} + +\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 temporal dimension. 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. This dimension is required to subset the data in a requested period.} + +\item{memb_dim}{a character string indicating the name of the dimension in which the ensemble members are stored. When set it to NULL, threshold is computed for individual members.} + +\item{sdate_dim}{a character string indicating the name of the dimension in which the initialization dates are stored.} + +\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 probabilites in the element \code{data}. +} +\description{ +Frequently, thresholds are defined by a percentile that may correspond to a different absolute value depending on the variable, gridpoint and also julian day (time). +This function calculates the corresponding value of a percentile given a dataset. +} +\examples{ +threshold <- 0.9 +exp <- CSTools::lonlat_prec +exp_probs <- CST_Threshold(exp, threshold) +exp$data <- array(rnorm(5 * 3 * 214 * 2), + c(member = 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')) +exp_probs <- CST_Threshold(exp, threshold, start = list(21, 4), end = list(21, 6)) +} diff --git a/man/CST_TotalSpellTimeExceedingThreshold.Rd b/man/CST_TotalSpellTimeExceedingThreshold.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0038d367b280726c75a32adf546902de09e98ec1 --- /dev/null +++ b/man/CST_TotalSpellTimeExceedingThreshold.Rd @@ -0,0 +1,64 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/TotalSpellTimeExceedingThreshold.R +\name{CST_TotalSpellTimeExceedingThreshold} +\alias{CST_TotalSpellTimeExceedingThreshold} +\title{Total Spell Time Exceeding Threshold} +\usage{ +CST_TotalSpellTimeExceedingThreshold( + data, + threshold, + spell, + op = ">", + start = NULL, + end = NULL, + time_dim = "ftime", + ncores = NULL +) +} +\arguments{ +\item{data}{an 's2dv_cube' object as provided by function \code{CST_Load} in package CSTools.} + +\item{threshold}{an 's2dv_cube' object as output of a 'CST_' function in the same units as parameter 'data' and with the common dimensions of the element 'data' of the same length. A single scalar is also possible. If \code{timd_dim} is in the dimension (with the same length as \code{data}), the comparison will be done day by day.} + +\item{spell}{a scalar indicating the minimum length of the spell.} + +\item{op}{a opartor '>' (by default), '<', '>=' or '<='.} + +\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{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{ +The number of days (when daily data is provided) that are part of a spell (defined by its minimum length e.g. 6 consecutive days) that exceed (or not exceed) a threshold are calculated with \code{TotalSpellTimeExceedingThreshold}. +This function allows to compute indicators widely used in Climate Services, such as: +\itemize{ +\code{WSDI}{Warm Spell Duration Index that count the total number of days with at least 6 consecutive days when the daily temperature maximum exceeds its 90th percentile.}} +This function requires the data and the threshold to be in the same units. The 90th percentile can be translate into absolute values given a reference dataset using function \code{Threshold} or the data can be transform into probabilites by using function \code{AbsToProbs}. See section @examples. +} +\examples{ +exp <- CSTools::lonlat_data$exp +TTSET <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = 283, spell = 3) +exp$data <- array(rnorm(5 * 3 * 214 * 2, mean = 25, sd = 3), + c(member = 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')) +# compare with the scalar fixed threshold +WSDI <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = 35, spell = 6) +# compare with percentile +thresholdP <- CST_Threshold(exp, threshold = 0.9) +WSDI_P <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = thresholdP, spell = 6) +} +\seealso{ +[Threshold()] and [AbsToProbs()]. +} diff --git a/man/Threshold.Rd b/man/Threshold.Rd new file mode 100644 index 0000000000000000000000000000000000000000..c2f3872c25d77bd4f7a6a1a1ddd6fd30ac7d229d --- /dev/null +++ b/man/Threshold.Rd @@ -0,0 +1,55 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Threshold.R +\name{Threshold} +\alias{Threshold} +\title{Absolute value of a relative threshold (percentile)} +\usage{ +Threshold( + data, + threshold, + dates = NULL, + start = NULL, + end = NULL, + time_dim = "time", + memb_dim = "member", + sdate_dim = "sdate", + na.rm = FALSE, + ncores = NULL +) +} +\arguments{ +\item{data}{a multidimensional array with named dimensions.} + +\item{threshold}{a single scalar or vector indicating the relative threshold(s).} + +\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 temporal dimension. 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. This dimension is required to subset the data in a requested period.} + +\item{memb_dim}{a character string indicating the name of the dimension in which the ensemble members are stored. When set it to NULL, threshold is computed for individual members.} + +\item{sdate_dim}{a character string indicating the name of the dimension in which the initialization dates are stored.} + +\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{ +Frequently, thresholds are defined by a percentile that may correspond to a different absolute value depending on the variable, gridpoint and also julian day (time). +This function calculates the corresponding value of a percentile given a dataset. +} +\examples{ +threshold <- 0.9 +data <- array(rnorm(25 * 3 * 214 * 2, mean = 26), + c(member = 25, sdate = 3, time = 214, lon = 2)) +thres_q <- Threshold(data, threshold) +data <- array(rnorm(1 * 3 * 214 * 2), c(member = 1, sdate = 3, time = 214, lon = 2)) +res <- Threshold(data, threshold) +} diff --git a/man/TotalSpellTimeExceedingThreshold.Rd b/man/TotalSpellTimeExceedingThreshold.Rd new file mode 100644 index 0000000000000000000000000000000000000000..39ba110b75f14b8cc3d4465049e8e9e2787b1830 --- /dev/null +++ b/man/TotalSpellTimeExceedingThreshold.Rd @@ -0,0 +1,58 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/TotalSpellTimeExceedingThreshold.R +\name{TotalSpellTimeExceedingThreshold} +\alias{TotalSpellTimeExceedingThreshold} +\title{Total Spell Time Exceeding Threshold} +\usage{ +TotalSpellTimeExceedingThreshold( + data, + threshold, + spell, + op = ">", + dates = NULL, + start = NULL, + end = NULL, + time_dim = "time", + ncores = NULL +) +} +\arguments{ +\item{data}{a multidimensional array with named dimensions.} + +\item{threshold}{a multidimensional array with named dimensions in the same units as parameter 'data' and with the common dimensions of the element 'data' of the same length. If \code{timd_dim} is in the dimension (with the same length as \code{data}), the comparison will be done day by day.} + +\item{spell}{a scalar indicating the minimum length of the spell.} + +\item{op}{a opartor '>' (by default), '<', '>=' or '<='.} + +\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{ncores}{an integer indicating the number of cores to use in parallel computation.} +} +\value{ +A multidimensional array with named dimensions. +} +\description{ +The number of days (when daily data is provided) that are part of a spell (defined by its minimum length e.g. 6 consecutive days) that exceed (or not exceed) a threshold are calculated with \code{TotalSpellTimeExceedingThreshold}. +This function allows to compute indicators widely used in Climate Services, such as: +\itemize{ +\code{WSDI}{Warm Spell Duration Index that count the total number of days with at least 6 consecutive days when the daily temperature maximum exceeds its 90th percentile.}} +This function requires the data and the threshold to be in the same units. The 90th percentile can be translate into absolute values given a reference dataset using function \code{Threshold} or the data can be transform into probabilites by using function \code{AbsToProbs}. See section @examples. +} +\examples{ +data <- array(rnorm(120), c(member = 1, sdate = 2, time = 20, lat = 4)) +threshold <- array(rnorm(4), c(lat = 4)) +total <- TotalSpellTimeExceedingThreshold(data, threshold, spell = 6) +# compare with percentile +thresholdP <- Threshold(data, threshold = 0.9) +total_P <- TotalSpellTimeExceedingThreshold(data, threshold = thresholdP, spell = 6) +} +\seealso{ +[Threshold()] and [AbsToProbs()]. +} diff --git a/tests/testthat/test-Threshold.R b/tests/testthat/test-Threshold.R new file mode 100644 index 0000000000000000000000000000000000000000..44f12914d3beee5647760f4af6af16d39c68453a --- /dev/null +++ b/tests/testthat/test-Threshold.R @@ -0,0 +1,51 @@ +context("Generic tests") +test_that("Sanity checks", { + #source("csindicators/R/Threshold.R") + expect_error(Threshold(NULL), + "Parameter 'data' cannot be NULL.") + expect_error(Threshold('x'), + "Parameter 'data' must be numeric.") + data <- 1:20 + expect_error(Threshold(data, NULL), + "Parameter 'threshold' cannot be NULL.") + expect_error(Threshold(data, 'x'), + "Parameter 'threshold' must be numeric.") + threshold <- 0.9 + expect_equal(Threshold(data, threshold), 18.1) + dim(data) <- c(2, 10) + expect_error(Threshold(data, threshold), + "Parameter 'data' must have named dimensions.") + names(dim(data)) <- c('lat', 'sdate') + expect_error(Threshold(data, threshold), + "Could not find dimension 'member' in 1th object provided in 'data'.") + expect_equal(Threshold(data, threshold, memb_dim = NULL), + array(c(17.2, 18.2), c(lat = 2))) + threshold <- c(0.1, 0.2) + expect_equal(Threshold(data, threshold, memb_dim = NULL), + array(c(2.8, 4.6, 3.8, 5.6), c(probs = 2, lat = 2))) + data <- array(1:40, c(x = 2, ftime = 20)) + expect_error(Threshold(data, threshold), + "Could not find dimension 'sdate' in 1th object provided in 'data'.") + expect_equal(dim(Threshold(data, threshold, sdate_dim = 'ftime', memb_dim = NULL)), + c(probs = 2, x = 2)) + # threshold with dimensions ? + dim(threshold) <- c(member = 2, ftime = 1) + expect_equal(dim(Threshold(data, threshold, sdate_dim = 'ftime', memb_dim = NULL)), + c(probs = 2, x = 2)) + expect_equal(dim(Threshold(data, threshold, memb_dim = 'x', sdate_dim = 'ftime')), + c(probs = 2)) +}) + +test_that("Seasonal forecasts", { + + exp <- CSTools::lonlat_data$exp$data + thresholdP <- Threshold(exp, threshold = 0.9) + expect_equal(dim(exp)[4:6], dim(thresholdP)[2:4]) + expect_equal(round(thresholdP[1, , 2, 2]), c(283, 281, 280)) + exp1 <- exp[1, 1, 1, , , ] # no member + library(s2dv) # 1 member and 1 sdate + exp1 <- InsertDim(InsertDim(exp1, 1, 1, name = 'sdate'), 1, 1, name = 'member') + exp1_thresholdP <- Threshold(exp1, threshold = 0.9) + expect_equal(round(exp1_thresholdP[, 2, 2]), c(281, 279, 276)) + +}) diff --git a/tests/testthat/test-TotalSpellTimeExceedingThreshold.R b/tests/testthat/test-TotalSpellTimeExceedingThreshold.R new file mode 100644 index 0000000000000000000000000000000000000000..e09fb155ce5e0312f0d64a460331b71822e42ba0 --- /dev/null +++ b/tests/testthat/test-TotalSpellTimeExceedingThreshold.R @@ -0,0 +1,55 @@ +context("Generic tests") +test_that("Sanity checks", { + #source("csindicators/R/TotalSpellTimeExceedingThreshold.R") + expect_error(TotalSpellTimeExceedingThreshold(NULL), + "Parameter 'data' cannot be NULL.") + expect_error(TotalSpellTimeExceedingThreshold('x'), + "Parameter 'data' must be numeric.") + data <- 1:20 + expect_error(TotalSpellTimeExceedingThreshold(data, NULL), + "Parameter 'threshold' cannot be NULL.") + expect_error(TotalSpellTimeExceedingThreshold(data, 'x'), + "Parameter 'threshold' must be numeric.") + threshold <- 10 + expect_error(TotalSpellTimeExceedingThreshold(data, threshold), + paste("argument", '"spell"', "is missing, with no default")) + dim(data) <- c(2, 10) + expect_error(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2), + "Parameter 'data' must have named dimensions.") + names(dim(data)) <- c('time', 'lat') + threshold <- array(1:2, 2) + dim(threshold) <- c(time = 2) + expect_equal(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2), + array(c(0,rep(2,9)), c(lat = 10))) + data <- array(1:40, c(x = 2, ftime = 20)) + expect_error(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2), + "Could not find dimension 'time' in 1th object provided in 'data'.") + threshold <- 10 + expect_equal(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2, time_dim = 'ftime'), + array(c(15, 15), c(x = 2))) + threshold <- rep(10, 20) + dim(threshold) <- c(member = 1, ftime = 20) + expect_equal(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2, time_dim = 'ftime'), + array(c(15, 15), c(x = 2, member = 1))) + expect_error(TotalSpellTimeExceedingThreshold(data, threshold, spell = 2, time_dim = 'y'), + paste("Could not find dimension 'y' in 1th object provided in 'data'")) + +}) + +test_that("Seasonal Forecasts", { + + exp <- CSTools::lonlat_data$exp + exp$data <- exp$data[1,1:3,1:3,,,] + res <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = 260, spell = 2) + expect_equal(res$data[,,1,1], + array(c(3,3,3,3,3,3,3,3,3), c(member = 3, sdate = 3))) + # compare with percentile + thresholdP <- Threshold(exp$data, threshold = 0.9) + WSDI <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = thresholdP, spell = 2) + expect_equal(WSDI$data[3,3,3,], + c(rep(0, 6), rep(2, 2), 0, 2, 0, 2, rep(0, 29), 2, rep(0, 11))) + thresholdP1 <- thresholdP[1,,] + WSDI1 <- CST_TotalSpellTimeExceedingThreshold(exp, threshold = thresholdP1, spell = 2) + expect_equal(WSDI1$data[3,3,3,], + c(rep(0, 53))) +})