diff --git a/NEWS.md b/NEWS.md index 2a9cffb623360ea12ba8874d7bef2853324db8f6..f108f5db59e4223372e2c4917de88e00ab23618d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ + EnsClustering vignette + EnsClustering has a new parameter 'time_dim' + CST_BiasCorrection has na.rm paramter + + CST_Anomaly allows to smooth the climatology with filter.span parameter - Fixes + CST_Anomaly handles exp, obs or both + PlotForecastPDF vignette displays figures correctly diff --git a/R/CST_Anomaly.R b/R/CST_Anomaly.R index c6de2ad1a697f9a54adba5254563835d2fe9de70..6e33c3411d1000abb0c15e4994aa9d7fbd473b23 100644 --- a/R/CST_Anomaly.R +++ b/R/CST_Anomaly.R @@ -12,7 +12,7 @@ #'@param cross A logical value indicating whether cross-validation should be applied or not. Default = FALSE. #'@param memb A logical value indicating whether Clim() computes one climatology for each experimental data #'product member(TRUE) or it computes one sole climatology for all members (FALSE). Default = TRUE. -#' +#'@param filter_span a numeric value indicating the degree of smoothing. This option is only available if parameter \code{cross} is set to FALSE. #'@param dim_anom An integer indicating the dimension along which the climatology will be computed. It #'usually corresponds to 3 (sdates) or 4 (ftime). Default = 3. #' @@ -52,7 +52,8 @@ #' #' #'@export -CST_Anomaly <- function(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, dim_anom = 3) { +CST_Anomaly <- function(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, + filter_span = NULL, dim_anom = 3) { if (!inherits(exp, 's2dv_cube') & !is.null(exp) || !inherits(obs, 's2dv_cube') & !is.null(obs)) { @@ -144,7 +145,22 @@ CST_Anomaly <- function(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, dim_ # Without cross-validation } else { tmp <- Clim(var_exp = exp$data, var_obs = obs$data, memb = memb) - + if (!is.null(filter_span)) { + if (is.numeric(filter_span)) { + pos_dims <- names(dim(tmp$clim_exp)) + reorder <- match(pos_dims, c('ftime', + pos_dims[-which(pos_dims == 'ftime')])) + tmp$clim_obs <- aperm(apply(tmp$clim_obs, c(1 : + length(dim(tmp$clim_obs)))[-which(names(dim(tmp$clim_obs)) == 'ftime')], + .Loess, loess_span = filter_span), reorder) + tmp$clim_exp <- aperm(apply(tmp$clim_exp, c(1 : + length(dim(tmp$clim_exp)))[-which(names(dim(tmp$clim_exp)) == 'ftime')], + .Loess, loess_span = filter_span), reorder) + } else { + warning("Paramater 'filter_span' is not numeric and any filter", + " is being applied.") + } + } if (memb) { clim_exp <- tmp$clim_exp clim_obs <- tmp$clim_obs @@ -193,3 +209,10 @@ CST_Anomaly <- function(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, dim_ return(list(exp = exp, obs = obs)) } } +.Loess <- function(clim, loess_span) { + data <- data.frame(ensmean = clim, day = 1 : length(clim)) + loess_filt <- loess(ensmean ~ day, data, span = loess_span) + output <- predict(loess_filt) + return(output) +} + diff --git a/man/CST_Anomaly.Rd b/man/CST_Anomaly.Rd index 365cbf4615fe69382625ac7b25a94975024cd498..1157416852e78b063b6c4db1606e2d3e2b8401cf 100644 --- a/man/CST_Anomaly.Rd +++ b/man/CST_Anomaly.Rd @@ -4,7 +4,14 @@ \alias{CST_Anomaly} \title{Anomalies relative to a climatology along selected dimension with or without cross-validation} \usage{ -CST_Anomaly(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, dim_anom = 3) +CST_Anomaly( + exp = NULL, + obs = NULL, + cross = FALSE, + memb = TRUE, + filter_span = NULL, + dim_anom = 3 +) } \arguments{ \item{exp}{an object of class \code{s2dv_cube} as returned by \code{CST_Load} function, containing the seasonal forecast experiment data in the element named \code{$data}.} @@ -16,6 +23,8 @@ CST_Anomaly(exp = NULL, obs = NULL, cross = FALSE, memb = TRUE, dim_anom = 3) \item{memb}{A logical value indicating whether Clim() computes one climatology for each experimental data product member(TRUE) or it computes one sole climatology for all members (FALSE). Default = TRUE.} +\item{filter_span}{a numeric value indicating the degree of smoothing. This option is only available if parameter \code{cross} is set to FALSE.} + \item{dim_anom}{An integer indicating the dimension along which the climatology will be computed. It usually corresponds to 3 (sdates) or 4 (ftime). Default = 3.} }