From 34772a6a95a3009225153494e77b61b8054d151f Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 11 Jul 2019 17:39:10 +0200 Subject: [PATCH 1/3] Threshold function works with NA values --- R/Threshold.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/R/Threshold.R b/R/Threshold.R index aa43b3a..2ba75e6 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -29,7 +29,7 @@ #'a <- Threshold(data, dates = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL) #'str(a) #'@export -Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL) { +Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL, na.rm = FALSE) { if (is.null(data)) { stop("Parameter 'data' cannot be NULL.") } @@ -93,6 +93,14 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt if (stop_error) { stop("Parameter 'dates' must be of the same length as the 'time' dimension of the parameter 'data'.") } + if (!is.logical(na.rm)) { + stop("Parameter 'na.rm' must be logical.") + } + if (length(na.rm) > 1) { + na.rm <- na.rm[1] + warning("Parameter 'na.rm' has length > 1 and only the first ", + "element will be used.") + } dates <- as.PCICt(dates, cal = calendar) dates = as.character(dates) jdays <- as.numeric(strftime(dates, format = "%j")) @@ -170,7 +178,7 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt if (length(dim(data)) > 1) { result <- Apply(data = data, margins = margins, fun = .Threshold, indices = jdays, qtiles = qtiles, - ncores = ncores) + ncores = ncores, na.rm = na.rm) names(dim(result$output1)) <- c("jdays", dim_names[-time_dim]) } else { result <- list() @@ -179,6 +187,6 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt } return(result$output1) } -.Threshold <- function(data, indices, qtiles) { - tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles) +.Threshold <- function(data, indices, qtiles, na.rm) { + tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles, na.rm = na.rm) } -- GitLab From a2001677c4293c36acedbe0c53f3c6114dcbd00a Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 12 Jul 2019 10:11:31 +0200 Subject: [PATCH 2/3] na.rm set default --- R/Threshold.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Threshold.R b/R/Threshold.R index 2ba75e6..7b94021 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -187,6 +187,6 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt } return(result$output1) } -.Threshold <- function(data, indices, qtiles, na.rm) { +.Threshold <- function(data, indices, qtiles, na.rm = FALSE) { tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles, na.rm = na.rm) } -- GitLab From 051ff8069ae68b54a2033e5706152d92b9978bb5 Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 12 Jul 2019 15:26:18 +0200 Subject: [PATCH 3/3] Bump version and update doc --- DESCRIPTION | 19 +++++++++++++------ R/Threshold.R | 1 + man/AnoAgree.Rd | 1 + man/Climdex.Rd | 1 + man/CombineIndices.Rd | 1 + man/DTRIndicator.Rd | 1 + man/DTRRef.Rd | 1 + man/DailyAno.Rd | 1 + man/Extremes.Rd | 1 + man/SeasonSelect.Rd | 1 + man/SelBox.Rd | 1 + man/Subset.Rd | 1 + man/Threshold.Rd | 5 ++++- man/WaveDuration.Rd | 1 + man/WeightedMean.Rd | 1 + 15 files changed, 30 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f8e69b1..d7691f7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,14 +1,21 @@ Package: ClimProjDiags Title: Set of Tools to Compute Various Climate Indices -Version: 0.0.3 +Version: 0.0.4 Authors@R: c( person("BSC-CNS", role = c("aut", "cph")), person("Alasdair", "Hunter", , "alasdair.hunter@bsc.es", role = "aut"), person("Nuria", "Perez-Zanon", , "nuria.perez@bsc.es", role = c("aut", "cre")), person("Nicolau", "Manubens", , "nicolau.manubens@bsc.es", role = "ctb"), person("Louis-Philippe", "Caron", , "louis-philippe.caron@bsc.es", role = "ctb")) -Description: Set of tools to compute metrics and indices for climate analysis. The package provides functions to compute extreme indices, evaluate the agreement between models and combine theses models into an ensemble. Multi-model time series of climate indices can be computed either after averaging the 2-D fields from different models provided they share a common grid or by combining time series computed on the model native grid. Indices can be assigned weights and/or combined to construct new indices. -Depends: R (>= 3.2.0) +Description: Set of tools to compute metrics and indices for climate analysis. + The package provides functions to compute extreme indices, evaluate the + agreement between models and combine theses models into an ensemble. Multi-model + time series of climate indices can be computed either after averaging the 2-D + fields from different models provided they share a common grid or by combining + time series computed on the model native grid. Indices can be assigned weights + and/or combined to construct new indices. +Depends: + R (>= 3.2.0) Imports: multiApply (>= 2.0.0), climdex.pcic, @@ -20,8 +27,8 @@ URL: https://earth.bsc.es/gitlab/es/ClimProjDiags BugReports: https://earth.bsc.es/gitlab/es/ClimProjDiags/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 6.0.1.9000 -Suggests: +RoxygenNote: 5.0.0 +Suggests: knitr, rmarkdown -VignetteBuilder: knitr +VignetteBuilder: knitr diff --git a/R/Threshold.R b/R/Threshold.R index 7b94021..311163a 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -8,6 +8,7 @@ #'@param base.range The years used for computing the threshold. #'@param qtiles Numeric vector with values between 0 and 1 indicating the quantiles to be computed. #'@param ncores The number of cores to be used when computing the threshold. +#'@param na.rm A logical value. If TRUE, any NA and NaN's are removed before the quantiles are computed (default as FALSE). #' #'@return An array with similar dimensions as the \code{data} input, but without 'time' dimension, and a new 'jdays' dimension. #' diff --git a/man/AnoAgree.Rd b/man/AnoAgree.Rd index 63c1450..c929352 100644 --- a/man/AnoAgree.Rd +++ b/man/AnoAgree.Rd @@ -34,3 +34,4 @@ a <- rnorm(6) agree <- AnoAgree(ano = a, membersdim = 1, na.rm = TRUE, ncores = NULL) print(agree) } + diff --git a/man/Climdex.Rd b/man/Climdex.Rd index 12ebac5..3ff6bb7 100644 --- a/man/Climdex.Rd +++ b/man/Climdex.Rd @@ -63,3 +63,4 @@ str(thres) clim <- Climdex(data, metric = "t90p", threshold = thres) str(clim) } + diff --git a/man/CombineIndices.Rd b/man/CombineIndices.Rd index 6d032cd..95abc5e 100644 --- a/man/CombineIndices.Rd +++ b/man/CombineIndices.Rd @@ -33,3 +33,4 @@ dim(b) <- c(lon = 2, lat = 3, mod = 4) comb_ind <- CombineIndices(indices = list(a, b), weights = c(2, 1), operation = "add") print(comb_ind) } + diff --git a/man/DTRIndicator.Rd b/man/DTRIndicator.Rd index f46d34d..33e7f50 100644 --- a/man/DTRIndicator.Rd +++ b/man/DTRIndicator.Rd @@ -60,3 +60,4 @@ aa <- DTRIndicator(tmax, tmin, ref = a, by.seasons = FALSE, ncores = NULL) str(aa) dim(aa$indicator) } + diff --git a/man/DTRRef.Rd b/man/DTRRef.Rd index 1239eea..6e32e31 100644 --- a/man/DTRRef.Rd +++ b/man/DTRRef.Rd @@ -68,3 +68,4 @@ dim(tmin) <- c(2, 3, 365) a <- DTRRef(tmax, tmin, by.seasons = FALSE, dates = time, timedim = 3, ncores = NULL) str(a) } + diff --git a/man/DailyAno.Rd b/man/DailyAno.Rd index dcd53c3..d4033f7 100644 --- a/man/DailyAno.Rd +++ b/man/DailyAno.Rd @@ -31,3 +31,4 @@ jdays <- c(rep(1, 5), rep(2, 5)) daily_anomaly <- DailyAno(data = data, jdays = jdays, na.rm = TRUE) print(daily_anomaly) } + diff --git a/man/Extremes.Rd b/man/Extremes.Rd index 4ce49cf..0939fa6 100644 --- a/man/Extremes.Rd +++ b/man/Extremes.Rd @@ -58,3 +58,4 @@ a <- Extremes(data, threshold = threshold, op = ">", min.length = 6, spells.can. max.missing.days = 5, ncores = NULL) str(a) } + diff --git a/man/SeasonSelect.Rd b/man/SeasonSelect.Rd index 33066ee..a71fd24 100644 --- a/man/SeasonSelect.Rd +++ b/man/SeasonSelect.Rd @@ -45,3 +45,4 @@ attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- time a <- SeasonSelect(data = data, season = 'JJA') str(a) } + diff --git a/man/SelBox.Rd b/man/SelBox.Rd index 38e3547..07e92b2 100644 --- a/man/SelBox.Rd +++ b/man/SelBox.Rd @@ -43,3 +43,4 @@ a <- SelBox(data = data, lon = lon, lat = lat, region = c(2, 20, 1, 5), londim = 1, latdim = 2, mask = NULL) str(a) } + diff --git a/man/Subset.Rd b/man/Subset.Rd index 5d24310..634cfe2 100644 --- a/man/Subset.Rd +++ b/man/Subset.Rd @@ -31,3 +31,4 @@ data_subset <- Subset(data, c('time', 'model'), dim(data_subset) } + diff --git a/man/Threshold.Rd b/man/Threshold.Rd index bcd6300..cec12e6 100644 --- a/man/Threshold.Rd +++ b/man/Threshold.Rd @@ -5,7 +5,7 @@ \title{Daily thresholds based on quantiles for n-dimensional arrays} \usage{ Threshold(data, dates = NULL, calendar = NULL, base.range = NULL, - qtiles = 0.9, ncores = NULL) + qtiles = 0.9, ncores = NULL, na.rm = FALSE) } \arguments{ \item{data}{A numeric n-dimensional array containing daily data.} @@ -19,6 +19,8 @@ Threshold(data, dates = NULL, calendar = NULL, base.range = NULL, \item{qtiles}{Numeric vector with values between 0 and 1 indicating the quantiles to be computed.} \item{ncores}{The number of cores to be used when computing the threshold.} + +\item{na.rm}{A logical value. If TRUE, any NA and NaN's are removed before the quantiles are computed (default as FALSE).} } \value{ An array with similar dimensions as the \code{data} input, but without 'time' dimension, and a new 'jdays' dimension. @@ -40,3 +42,4 @@ attr(data, 'Variables')$dat1$time <- time a <- Threshold(data, dates = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL) str(a) } + diff --git a/man/WaveDuration.Rd b/man/WaveDuration.Rd index ba5ace2..b068252 100644 --- a/man/WaveDuration.Rd +++ b/man/WaveDuration.Rd @@ -48,3 +48,4 @@ threshold <- rep(40, 31) a <- WaveDuration(data, threshold, op = ">", spell.length = 6, by.seasons = TRUE, ncores = NULL) str(a) } + diff --git a/man/WeightedMean.Rd b/man/WeightedMean.Rd index 0141c70..23f31c6 100644 --- a/man/WeightedMean.Rd +++ b/man/WeightedMean.Rd @@ -60,3 +60,4 @@ a <- WeightedMean(data = data, lon = lon, lat = lat, region = NULL, mask = NULL, londim = 1, latdim = 2) str(a) } + -- GitLab