From 7e3d9d65d58a0dfd272c2070b6091878c8201417 Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 18 Sep 2020 17:27:41 +0200 Subject: [PATCH 1/4] fix in CST_RFSlope to handle NAs in time_dim --- NAMESPACE | 1 + NEWS.md | 4 ++++ R/CST_RFSlope.R | 8 +++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 9f9a0e34..8ba1c4cd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -51,6 +51,7 @@ import(rainfarmr) import(s2dverification) import(stats) importFrom(ClimProjDiags,SelBox) +importFrom(ClimProjDiags,Subset) importFrom(RColorBrewer,brewer.pal) importFrom(data.table,CJ) importFrom(data.table,data.table) diff --git a/NEWS.md b/NEWS.md index fab70c70..68d09c8e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +### CSTools 3.x.x + +- New features: + + CST_RFSlope handless missing values in the temporal dimension. ### CSTools 3.1.0 **Submission date to CRAN: XX-06-2020** diff --git a/R/CST_RFSlope.R b/R/CST_RFSlope.R index 6bc3dc2d..fa7131b1 100644 --- a/R/CST_RFSlope.R +++ b/R/CST_RFSlope.R @@ -73,6 +73,8 @@ CST_RFSlope <- function(data, kmin = 1, time_dim = NULL) { #' The returned array has the same dimensions as the input array, #' minus the dimensions specified by \code{lon_dim}, \code{lat_dim} and \code{time_dim}. #' @import multiApply +#' @import rainfarmr +#' @importFrom ClimProjDiags Subset #' @export #' @examples #' # Example for the 'reduced' RFSlope function @@ -152,7 +154,11 @@ RFSlope <- function(data, kmin = 1, time_dim = NULL, #' @noRd .RFSlope <- function(pr, kmin) { - + if (any(is.na(pr))) { + posna <- unlist(lapply(1:dim(pr)['rainfarm_samples'], + function(x){!is.na(pr[1, 1, x])})) + pr <- Subset(pr, 'rainfarm_samples', posna) + } fxp <- fft2d(pr) sx <- fitslope(fxp, kmin = kmin) return(sx) -- GitLab From e410de29811d592dfd35baf47748aabdcccd952b Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 21 Sep 2020 16:23:50 +0200 Subject: [PATCH 2/4] Adding ncores parameter to RF_Slope --- R/CST_RFSlope.R | 20 ++++++++++++++++---- man/CST_RFSlope.Rd | 4 +++- man/RFSlope.Rd | 11 ++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/R/CST_RFSlope.R b/R/CST_RFSlope.R index fa7131b1..a8457742 100644 --- a/R/CST_RFSlope.R +++ b/R/CST_RFSlope.R @@ -15,6 +15,7 @@ #' over which to compute spectral slopes. If a character array of dimension names is provided, the spectral slopes #' will be computed as an average over all elements belonging to those dimensions. #' If omitted one of c("ftime", "sdate", "time") is searched and the first one with more than one element is chosen. +#' @param ncores is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one. #' @return CST_RFSlope() returns spectral slopes using the RainFARM convention #' (the logarithmic slope of k*|A(k)|^2 where A(k) are the spectral amplitudes). #' The returned array has the same dimensions as the \code{exp} element of the input object, @@ -38,7 +39,7 @@ #' #[1,] 1.893503 1.893503 1.893503 #' #[2,] 1.893503 1.893503 1.893503 #' @export -CST_RFSlope <- function(data, kmin = 1, time_dim = NULL) { +CST_RFSlope <- function(data, kmin = 1, time_dim = NULL, ncores = 1) { slopes <- RFSlope(data$data, kmin, time_dim, lon_dim = "lon", lat_dim = "lat") @@ -68,6 +69,8 @@ CST_RFSlope <- function(data, kmin = 1, time_dim = NULL) { #' with more than one element is chosen. #' @param lon_dim Name of lon dimension ("lon" by default). #' @param lat_dim Name of lat dimension ("lat" by default). +#' @param ncores is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one. +#' #' @return RFSlope() returns spectral slopes using the RainFARM convention #' (the logarithmic slope of k*|A(k)|^2 where A(k) are the spectral amplitudes). #' The returned array has the same dimensions as the input array, @@ -96,8 +99,17 @@ CST_RFSlope <- function(data, kmin = 1, time_dim = NULL) { #' #[3,] 1.893503 1.893503 1.893503 #' #[4,] 1.893503 1.893503 1.893503 RFSlope <- function(data, kmin = 1, time_dim = NULL, - lon_dim = "lon", lat_dim = "lat") { - + lon_dim = "lon", lat_dim = "lat", ncores = 1) { + if (length(ncores) > 1) { + ncores = ncores[1] + warning("Parameter 'ncores' has length > 1 and only the first element will be used.") + } + if (!is.null(ncores)) { + ncores <- round(ncores) + if (ncores == 0) { + ncores = NULL + } + } # Ensure input grid is square and with even dimensions if ( (dim(data)[lon_dim] != dim(data)[lat_dim]) | (dim(data)[lon_dim] %% 2 == 1)) { @@ -141,7 +153,7 @@ RFSlope <- function(data, kmin = 1, time_dim = NULL, # Repeatedly apply .RFSlope result <- Apply(data, c(lon_dim, lat_dim, "rainfarm_samples"), - .RFSlope, kmin)$output1 + .RFSlope, kmin, ncores = ncores)$output1 return(slopes = result) } diff --git a/man/CST_RFSlope.Rd b/man/CST_RFSlope.Rd index 0c4e1671..b76ac93e 100644 --- a/man/CST_RFSlope.Rd +++ b/man/CST_RFSlope.Rd @@ -4,7 +4,7 @@ \alias{CST_RFSlope} \title{RainFARM spectral slopes from a CSTools object} \usage{ -CST_RFSlope(data, kmin = 1, time_dim = NULL) +CST_RFSlope(data, kmin = 1, time_dim = NULL, ncores = 1) } \arguments{ \item{data}{An object of the class 's2dv_cube', containing the spatial precipitation fields to downscale. @@ -18,6 +18,8 @@ to average these slopes, which can be specified by parameter \code{time_dim}.} over which to compute spectral slopes. If a character array of dimension names is provided, the spectral slopes will be computed as an average over all elements belonging to those dimensions. If omitted one of c("ftime", "sdate", "time") is searched and the first one with more than one element is chosen.} + +\item{ncores}{is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one.} } \value{ CST_RFSlope() returns spectral slopes using the RainFARM convention diff --git a/man/RFSlope.Rd b/man/RFSlope.Rd index db3f0e10..5c0c1689 100644 --- a/man/RFSlope.Rd +++ b/man/RFSlope.Rd @@ -4,7 +4,14 @@ \alias{RFSlope} \title{RainFARM spectral slopes from an array (reduced version)} \usage{ -RFSlope(data, kmin = 1, time_dim = NULL, lon_dim = "lon", lat_dim = "lat") +RFSlope( + data, + kmin = 1, + time_dim = NULL, + lon_dim = "lon", + lat_dim = "lat", + ncores = 1 +) } \arguments{ \item{data}{Array containing the spatial precipitation fields to downscale. @@ -25,6 +32,8 @@ with more than one element is chosen.} \item{lon_dim}{Name of lon dimension ("lon" by default).} \item{lat_dim}{Name of lat dimension ("lat" by default).} + +\item{ncores}{is an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one.} } \value{ RFSlope() returns spectral slopes using the RainFARM convention -- GitLab From 617cdbdce0c255af7763657fd525392d82d28609 Mon Sep 17 00:00:00 2001 From: nperez Date: Tue, 27 Oct 2020 18:13:07 +0100 Subject: [PATCH 3/4] NEWs updated --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 558d41a8..c8dfc43f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ **Submission date to CRAN: XX-12-2020** - New features: + PlotPDFsOLE includes parameters to modify legend style - + CST_RFSlope handless missing values in the temporal dimension + + CST_RFSlope handless missing values in the temporal dimension and new ncores parameter for parallel computation - Fixes: + PlotForecastPDF correctly displays terciles labels -- GitLab From 5e9d61069ce3fe96bdf7ef9860d52dffbb6727b5 Mon Sep 17 00:00:00 2001 From: nperez Date: Tue, 27 Oct 2020 18:35:48 +0100 Subject: [PATCH 4/4] Subset to be imported from s2dverification --- NAMESPACE | 2 +- R/CST_RFSlope.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 8ba1c4cd..efc5c835 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -51,7 +51,6 @@ import(rainfarmr) import(s2dverification) import(stats) importFrom(ClimProjDiags,SelBox) -importFrom(ClimProjDiags,Subset) importFrom(RColorBrewer,brewer.pal) importFrom(data.table,CJ) importFrom(data.table,data.table) @@ -86,6 +85,7 @@ importFrom(plyr,.) importFrom(plyr,dlply) importFrom(reshape2,melt) importFrom(s2dv,Reorder) +importFrom(s2dverification,Subset) importFrom(utils,glob2rx) importFrom(utils,head) importFrom(utils,tail) diff --git a/R/CST_RFSlope.R b/R/CST_RFSlope.R index a8457742..75982037 100644 --- a/R/CST_RFSlope.R +++ b/R/CST_RFSlope.R @@ -77,7 +77,7 @@ CST_RFSlope <- function(data, kmin = 1, time_dim = NULL, ncores = 1) { #' minus the dimensions specified by \code{lon_dim}, \code{lat_dim} and \code{time_dim}. #' @import multiApply #' @import rainfarmr -#' @importFrom ClimProjDiags Subset +#' @importFrom s2dverification Subset #' @export #' @examples #' # Example for the 'reduced' RFSlope function -- GitLab