diff --git a/NAMESPACE b/NAMESPACE index af1faf4be6a91ba005ad601f00317ddea291ead4..8908f0edc2d3ef1e7818e92ca245f468ef390517 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -52,7 +52,9 @@ import(rainfarmr) import(s2dverification) import(stats) importFrom(ClimProjDiags,SelBox) +importFrom(ClimProjDiags,Subset) importFrom(RColorBrewer,brewer.pal) +importFrom(abind,abind) importFrom(data.table,CJ) importFrom(data.table,data.table) importFrom(data.table,setkey) diff --git a/NEWS.md b/NEWS.md index cf9eae22058c01a73c340eec2157ec536967da04..7e18054c35af3a206a714b06fba694b7185cc1dd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ + CST_RFSlope handless missing values in the temporal dimension and new 'ncores' parameter allows parallel computation + CST_RFWeights accepts s2dv_cube objects as input and new 'ncores' paramenter allows parallel computation + RFWeights is exposed to users + + CST_RainFARM accepts multi-dimensional slopes and weights and handless missing values in sample dimensions. + - Fixes: + PlotForecastPDF correctly displays terciles labels diff --git a/R/CST_RFWeights.R b/R/CST_RFWeights.R index 56d943da4f8bf1a63801c68d1d4a31339fa1e54e..fa7eec75045717fdd08918dcc1b1d0b19cbd77f6 100644 --- a/R/CST_RFWeights.R +++ b/R/CST_RFWeights.R @@ -124,7 +124,6 @@ CST_RFWeights <- function(climfile, nf, lon, lat, varname = NULL, #' @param lat Vector of latitudes. #' The number of longitudes and latitudes is expected to be even and the same. If not #' the function will perform a subsetting to ensure this condition. -#' @param varname Name of the variable to be read from \code{climfile}. #' @param fsmooth Logical to use smooth conservation (default) or large-scale box-average conservation. #' @param lonname a character string indicating the name of the longitudinal dimension set as 'lon' by default. #' @param latname a character string indicating the name of the latitudinal dimension set as 'lat' by default. @@ -143,7 +142,7 @@ CST_RFWeights <- function(climfile, nf, lon, lat, varname = NULL, #' @export RF_Weights <- function(zclim, latin, lonin, nf, lat, lon, fsmooth = TRUE, lonname = 'lon', latname = 'lat', ncores = NULL) { - x <- Apply(list(zclim), target = c(lonname, latname), fun = rf_weights, + x <- Apply(list(zclim), target_dims = c(lonname, latname), fun = rf_weights, latin = latin, lonin = lonin, nf = nf, lat = lat, lon = lon, fsmooth = fsmooth, ncores = ncores)$output1 grid <- lon_lat_fine(lon, lat, nf) diff --git a/R/CST_RainFARM.R b/R/CST_RainFARM.R index 49065a974711aa9732778ee46e8ca9717ee667b9..73b075c4b0dcca97842d874e721ca5fd2bfce22f 100644 --- a/R/CST_RainFARM.R +++ b/R/CST_RainFARM.R @@ -21,11 +21,10 @@ #' the function will perform a subsetting to ensure this condition. #' @param weights Matrix with climatological weights which can be obtained using #' the \code{CST_RFWeights} function. If \code{weights=1.} (default) no weights are used. -#' The matrix should have dimensions (lon, lat) in this order. -#' The names of these dimensions are not checked. +#' The names of these dimensions must be at least 'lon' and 'lat'. #' @param nf Refinement factor for downscaling (the output resolution is increased by this factor). #' @param slope Prescribed spectral slope. The default is \code{slope=0.} -#' meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. +#' meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. A 1D array with named dimension can be provided (see details and examples) #' @param kmin First wavenumber for spectral slope (default: \code{kmin=1}). #' @param nens Number of ensemble members to produce (default: \code{nens=1}). #' @param fglob Logical to conserve global precipitation over the domain (default: FALSE). @@ -51,10 +50,11 @@ #' #' @return CST_RainFARM() returns a downscaled CSTools object (i.e., of the #' class 's2dv_cube'). -#' If \code{nens>1} an additional dimension named "realization" is added to the +#' If \code{nens>1} an additional dimension named "realizatio"n is added to the #' \code{$data} array after the "member" dimension (unless #' \code{drop_realization_dim=TRUE} is specified). #' The ordering of the remaining dimensions in the \code{$data} element of the input object is maintained. +#' @details Wether parameter 'slope' and 'weights' presents seasonality dependency, a dimension name should match between these parameters and the input data in parameter 'data'. See example 2 below where weights and slope vary with 'sdate' dimension. #' @import multiApply #' @import rainfarmr #' @examples @@ -68,8 +68,8 @@ #' dim(lat) <- c(lat = length(lat)) #' data <- list(data = exp, lon = lon, lat = lat) #' # Create a test array of weights -#' ww <- array(1., dim = c(8 * nf, 8 * nf)) -#' res <- CST_RainFARM(data, nf, ww, nens=3) +#' ww <- array(1., dim = c(lon = 8 * nf, lat = 8 * nf)) +#' res <- CST_RainFARM(data, nf = nf, weights = ww, nens=3) #' str(res) #' #List of 3 #' # $ data: num [1, 1:2, 1:3, 1:3, 1:4, 1:64, 1:64] 260 553 281 278 143 ... @@ -79,20 +79,28 @@ #' # dataset member realization sdate ftime lat lon #' # 1 2 3 3 4 64 64 #' +#' # Example 2: +#' slo <- array(c(0.1, 0.5, 0.7), c(sdate= 3)) +#' wei <- array(rnorm(8 * 8 * 3), c(lon = 8, lat = 8, sdate = 3)) +#' res <- CST_RainFARM(lonlat_prec, +#' weights = wei, slope = slo, nf = 2) #' @export -CST_RainFARM <- function(data, nf, weights = 1., slope = 0, kmin = 1, +CST_RainFARM <- function(data, weights = 1., slope = 0, nf, kmin = 1, nens = 1, fglob = FALSE, fsmooth = TRUE, nprocs = 1, time_dim = NULL, verbose = FALSE, drop_realization_dim = FALSE) { res <- RainFARM(data$data, data$lon, data$lat, - nf, weights, nens, slope, kmin, fglob, fsmooth, + nf = nf, weights = weights, nens, slope, kmin, fglob, fsmooth, nprocs, time_dim, lon_dim = "lon", lat_dim = "lat", drop_realization_dim, verbose) - + att_lon <- attributes(data$lon)[-1] + att_lat <- attributes(data$lat)[-1] data$data <- res$data data$lon <- res$lon + attributes(data$lon) <- att_lon data$lat <- res$lat + attributes(data$lat) <- att_lat return(data) } @@ -117,13 +125,12 @@ CST_RainFARM <- function(data, nf, weights = 1., slope = 0, kmin = 1, #' the function will perform a subsetting to ensure this condition. #' @param lon Vector or array of longitudes. #' @param lat Vector or array of latitudes. -#' @param weights Matrix with climatological weights which can be obtained using +#' @param weights multi-dimensional array with climatological weights which can be obtained using #' the \code{CST_RFWeights} function. If \code{weights=1.} (default) no weights are used. -#' The matrix should have dimensions (lon, lat) in this order. -#' The names of these dimensions are not checked. -#' @param nf Refinement factor for downscaling (the output resolution is increased by this factor). +#' The names of these dimensions must be at least 'lon' and 'lat'. +#' @param nf Refinement factor for downscaling (the output resolution is increased by this factor). #' @param slope Prescribed spectral slope. The default is \code{slope=0.} -#' meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. +#' meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. A 1D array with named dimension can be provided (see details and examples) #' @param kmin First wavenumber for spectral slope (default: \code{kmin=1}). #' @param nens Number of ensemble members to produce (default: \code{nens=1}). #' @param fglob Logical to conseve global precipitation over the domain (default: FALSE) @@ -153,7 +160,10 @@ CST_RainFARM <- function(data, nf, weights = 1., slope = 0, kmin = 1, #' If \code{nens>1} an additional dimension named "realization" is added to the output array #' after the "member" dimension (if it exists and unless \code{drop_realization_dim=TRUE} is specified). #' The ordering of the remaining dimensions in the \code{exp} element of the input object is maintained. +#' @details Wether parameter 'slope' and 'weights' presents seasonality dependency, a dimension name should match between these parameters and the input data in parameter 'data'. See example 2 below where weights and slope vary with 'sdate' dimension. #' @import multiApply +#' @importFrom s2dverification Subset +#' @importFrom abind abind #' @export #' @examples #' # Example for the 'reduced' RainFARM function @@ -166,7 +176,7 @@ CST_RainFARM <- function(data, nf, weights = 1., slope = 0, kmin = 1, #' lon_mat <- seq(10, 13.5, 0.5) # could also be a 2d matrix #' lat_mat <- seq(40, 43.5, 0.5) #' # Create a test array of weights -#' ww <- array(1., dim = c(8 * nf, 8 * nf)) +#' ww <- array(1., dim = c(lon = 8 * nf, lat = 8 * nf)) #' # or create proper weights using an external fine-scale climatology file #' # Specify a weightsfn filename if you wish to save the weights #' \dontrun{ @@ -185,7 +195,11 @@ CST_RainFARM <- function(data, nf, weights = 1., slope = 0, kmin = 1, #' dim(res$data) #' # lon lat ftime realization #' # 64 64 20 2 -#' +#' # Example 2: +#' slo <- array(c(0.1, 0.5, 0.7), c(sdate= 3)) +#' wei <- array(rnorm(8*8*3), c(lon = 8, lat = 8, sdate = 3)) +#' res <- RainFARM(lonlat_prec$data, lon = lonlat_prec$lon, +#' lat = lonlat_prec$lat, weights = wei, slope = slo, nf = 2) RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, slope = 0, kmin = 1, fglob = FALSE, fsmooth = TRUE, nprocs = 1, time_dim = NULL, lon_dim = "lon", lat_dim = "lat", @@ -211,13 +225,28 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, warning(paste0("lon: [", lon[1], ", ", lon[length(lon)], "] ", " lat: [", lat[1], ", ", lat[length(lat)], "]")) } - if (!(length(dim(weights)) == 0) & - !(all(dim(weights) == c(dim(data)[lon_dim] * nf, - dim(data)[lat_dim] * nf)))) { + if (length(dim(weights)) > 0) { + if (length(names(dim(weights))) == 0) { + stop("Parameter 'weights' must have dimension names when it is not a scalar.") + } else { + if (length(which(names(dim(weights)) == 'lon')) > 0 & + length(which(names(dim(weights)) == 'lat')) > 0) { + lonposw <- which(names(dim(weights)) == 'lon') + latposw <- which(names(dim(weights)) == 'lat') + } else { + stop("Parameter 'weights' must have dimension names 'lon' and 'lat' when", + " it is not a scalar.") + } + } + } + if (!(length(dim(weights)) == 0)) { + if (!(dim(weights)[lonposw] == dim(data)[lon_dim] * nf) & + !(dim(weights)[latposw] == dim(data)[lat_dim] * nf)) { stop(paste("The dimensions of the weights matrix (", dim(weights)[1], "x", dim(weights)[2] , ") are not consistent with the size of the data (", dim(data)[lon_dim], ") and the refinement factor (", nf, ")")) + } } # Check/detect time_dim if (is.null(time_dim)) { @@ -238,11 +267,11 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, warning(paste("Selected time dim:", time_dim)) } # Check if slope is an array - if (length(slope) > 1) { - warning("Parameter 'slope' has length > 1 and only the first ", - "element will be used.") - slope <- as.numeric(slope[1]) - } + #if (length(slope) > 1) { + # warning("Parameter 'slope' has length > 1 and only the first ", + # "element will be used.") + # slope <- as.numeric(slope[1]) + #} # Perform common calls r <- lon_lat_fine(lon, lat, nf) @@ -259,14 +288,33 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, dim(data) <- c(cdim[ind], rainfarm_samples = prod(cdim[-ind])) # Repeatedly apply .RainFARM - result <- Apply(data, c(lon_dim, lat_dim, "rainfarm_samples"), .RainFARM, - weights, nf, nens, slope, kmin, - fglob, fsmooth, ncores = nprocs, verbose, - split_factor = "greatest")$output1 + if (length(weights) == 1 & length(slope) == 1) { + result <- Apply(data, c(lon_dim, lat_dim, "rainfarm_samples"), .RainFARM, + weights, slope, nf, nens, kmin, + fglob, fsmooth, ncores = nprocs, verbose, + split_factor = "greatest")$output1 + } else if (length(slope) == 1 & length(weights) > 1 ) { + result <- Apply(list(data, weights), + list(c(lon_dim, lat_dim, "rainfarm_samples"), + c(lonposw, latposw)), + .RainFARM, slope = slope, + nf = nf, nens = nens, kmin = kmin, + fglob = fglob, fsmooth = fsmooth, ncores = nprocs, + verbose = verbose, + split_factor = "greatest")$output1 + } else { + result <- Apply(list(data, weights, slope), + list(c(lon_dim, lat_dim, "rainfarm_samples"), + c(lonposw, latposw), NULL), + fun = .RainFARM, + nf = nf, nens = nens, kmin = kmin, + fglob = fglob, fsmooth = fsmooth, ncores = nprocs, + verbose = verbose, + split_factor = "greatest")$output1 + } # result has dims: lon, lat, rainfarm_samples, realization, other dims # Expand back rainfarm_samples to compacted dims dim(result) <- c(dim(result)[1:2], cdim[-ind], dim(result)[-(1:3)]) - # Reorder as it was in original data # + realization dim after member if it exists ienspos <- which(names(cdim0) == "member") @@ -282,7 +330,6 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, if (drop_realization_dim) { cdim <- dim(result) if (nens == 1) { - # just drop it if only one member dim(result) <- cdim[-which(names(cdim) == "realization")[1]] } else if ("member" %in% names(cdim)) { # compact member and realization dimension if member dim exists, @@ -295,7 +342,6 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, names(dim(result))[ind] <- "member" } } - return(list(data = result, lon = lon_f, lat = lat_f)) } @@ -303,8 +349,8 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, #' @param pr Precipitation array to downscale with dimensions (lon, lat, time). #' @param weights Matrix with climatological weights which can be obtained using #' the \code{CST_RFWeights} function (default: \code{weights=1.} i.e. no weights). -#' @param nf Refinement factor for downscaling (the output resolution is increased by this factor). #' @param slope Prescribed spectral slope (default: \code{slope=0.} +#' @param nf Refinement factor for downscaling (the output resolution is increased by this factor). #' meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. #' @param kmin First wavenumber for spectral slope (default: \code{kmin=1}). #' @param nens Number of ensemble members to produce (default: \code{nens=1}). @@ -313,24 +359,67 @@ RainFARM <- function(data, lon, lat, nf, weights = 1., nens = 1, #' @param verbose Logical for verbose output (default: FALSE). #' @return .RainFARM returns a downscaled array with dimensions (lon, lat, time, realization) #' @noRd -.RainFARM <- function(pr, weights, nf, nens, slope, kmin, +.RainFARM <- function(pr, weights, slope, nf, nens, kmin, fglob, fsmooth, verbose) { - + posna <- NULL + 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) + } if (slope == 0) { fxp <- fft2d(pr) sx <- fitslope(fxp, kmin = kmin) } else { sx <- slope } - result_dims <- c(dim(pr)[1] * nf, dim(pr)[2] * nf, dim(pr)[3], realization = nens) r <- array(dim = result_dims) - for (i in 1:nens) { r[, , , i] <- rainfarm(pr, sx, nf, weights, fglob = fglob, fsmooth = fsmooth, verbose = verbose) } + # restoring NA values in their position: + if (!is.null(posna)) { + pos <- which(posna == FALSE) + if (pos[1] == 1) { + result_dims['rainfarm_samples'] <- 1 + x <- array(rep(NA,prod(result_dims)), result_dims) + r <- abind(x, r, along = 3) + names(dim(r)) <- names(result_dims) + pos <- pos[-1] + } + result_dims['rainfarm_samples'] <- 1 + if ((pos[1] - 1) < dim(r)['rainfarm_samples']) { + x <- max(which((pos - 1) < dim(r)['rainfarm_samples'])) + pos_internal <- pos[1:x] + if (pos[length(pos)] > pos[x]) { + pos <- pos[(x + 1) : length(pos)] + } else { + pos <- NULL + } + } else { + pos_internal <- NULL + } + if (!is.null(pos_internal)) { + r <- lapply(pos_internal, + function(x) { + rrr <- Subset(r, along = 'rainfarm_samples', indices = 1:(x-1)) + rrrr <- Subset(r, along = 'rainfarm_samples', indices = x:dim(r)['rainfarm_samples']) + r <- abind(rrr, array(NA, result_dims), rrrr, along = 3) + names(dim(r)) <- names(result_dims) + return(r) + })[[length(pos)]] + } + if (!is.null(pos)) { + result_dims['rainfarm_samples'] <- length(pos) + x <- array(rep(NA,prod(result_dims)), result_dims) + r <- abind(r, x, along = 3) + } + names(dim(r)) <- names(result_dims) + warning("Missing values found in the samples.") + } return(r) } diff --git a/man/CST_RainFARM.Rd b/man/CST_RainFARM.Rd index 1c609e084ca509c1343802604dddf9899088da3e..f86ab89cb65487fd36c22bd40e6ccddcf6cfc289 100644 --- a/man/CST_RainFARM.Rd +++ b/man/CST_RainFARM.Rd @@ -6,9 +6,9 @@ \usage{ CST_RainFARM( data, - nf, weights = 1, slope = 0, + nf, kmin = 1, nens = 1, fglob = FALSE, @@ -29,15 +29,14 @@ which can be specified by parameter \code{time_dim}. The number of longitudes and latitudes in the input data is expected to be even and the same. If not the function will perform a subsetting to ensure this condition.} -\item{nf}{Refinement factor for downscaling (the output resolution is increased by this factor).} - \item{weights}{Matrix with climatological weights which can be obtained using the \code{CST_RFWeights} function. If \code{weights=1.} (default) no weights are used. -The matrix should have dimensions (lon, lat) in this order. -The names of these dimensions are not checked.} +The names of these dimensions must be at least 'lon' and 'lat'.} \item{slope}{Prescribed spectral slope. The default is \code{slope=0.} -meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}.} +meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. A 1D array with named dimension can be provided (see details and examples)} + +\item{nf}{Refinement factor for downscaling (the output resolution is increased by this factor).} \item{kmin}{First wavenumber for spectral slope (default: \code{kmin=1}).} @@ -72,7 +71,7 @@ with the following behaviour if set to TRUE: \value{ CST_RainFARM() returns a downscaled CSTools object (i.e., of the class 's2dv_cube'). -If \code{nens>1} an additional dimension named "realization" is added to the +If \code{nens>1} an additional dimension named "realizatio"n is added to the \code{$data} array after the "member" dimension (unless \code{drop_realization_dim=TRUE} is specified). The ordering of the remaining dimensions in the \code{$data} element of the input object is maintained. @@ -84,6 +83,9 @@ downscaling method and accepts a CSTools object (an object of the class Adapted for climate downscaling and including orographic correction as described in Terzago et al. 2018. } +\details{ +Wether parameter 'slope' and 'weights' presents seasonality dependency, a dimension name should match between these parameters and the input data in parameter 'data'. See example 2 below where weights and slope vary with 'sdate' dimension. +} \examples{ #Example 1: using CST_RainFARM for a CSTools object nf <- 8 # Choose a downscaling by factor 8 @@ -95,8 +97,8 @@ lat <- seq(40, 43.5, 0.5) dim(lat) <- c(lat = length(lat)) data <- list(data = exp, lon = lon, lat = lat) # Create a test array of weights -ww <- array(1., dim = c(8 * nf, 8 * nf)) -res <- CST_RainFARM(data, nf, ww, nens=3) +ww <- array(1., dim = c(lon = 8 * nf, lat = 8 * nf)) +res <- CST_RainFARM(data, nf = nf, weights = ww, nens=3) str(res) #List of 3 # $ data: num [1, 1:2, 1:3, 1:3, 1:4, 1:64, 1:64] 260 553 281 278 143 ... @@ -106,6 +108,11 @@ dim(res$data) # dataset member realization sdate ftime lat lon # 1 2 3 3 4 64 64 +# Example 2: +slo <- array(c(0.1, 0.5, 0.7), c(sdate= 3)) +wei <- array(rnorm(8 * 8 * 3), c(lon = 8, lat = 8, sdate = 3)) +res <- CST_RainFARM(lonlat_prec, + weights = wei, slope = slo, nf = 2) } \references{ Terzago, S. et al. (2018). NHESS 18(11), 2825-2840. diff --git a/man/RF_Weights.Rd b/man/RF_Weights.Rd index 7e8d735dedd35bc36e3d16a6eb0f3466099c9a04..66e1ac5153c2a41605867e6cbbcd35c8b3120bd7 100644 --- a/man/RF_Weights.Rd +++ b/man/RF_Weights.Rd @@ -39,8 +39,6 @@ the function will perform a subsetting to ensure this condition.} \item{latname}{a character string indicating the name of the latitudinal dimension set as 'lat' by default.} \item{ncores}{an integer that indicates the number of cores for parallel computations using multiApply function. The default value is one.} - -\item{varname}{Name of the variable to be read from \code{climfile}.} } \value{ An object of class 's2dv_cube' containing in matrix \code{data} the weights with dimensions (lon, lat). diff --git a/man/RainFARM.Rd b/man/RainFARM.Rd index 0db8467926923fbed0f9da2f9625e67d5fe06a7e..ef4485c94b1c290236206f97e2b0dcbc1ff06153 100644 --- a/man/RainFARM.Rd +++ b/man/RainFARM.Rd @@ -38,15 +38,14 @@ the function will perform a subsetting to ensure this condition.} \item{nf}{Refinement factor for downscaling (the output resolution is increased by this factor).} -\item{weights}{Matrix with climatological weights which can be obtained using +\item{weights}{multi-dimensional array with climatological weights which can be obtained using the \code{CST_RFWeights} function. If \code{weights=1.} (default) no weights are used. -The matrix should have dimensions (lon, lat) in this order. -The names of these dimensions are not checked.} +The names of these dimensions must be at least 'lon' and 'lat'.} \item{nens}{Number of ensemble members to produce (default: \code{nens=1}).} \item{slope}{Prescribed spectral slope. The default is \code{slope=0.} -meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}.} +meaning that the slope is determined automatically over the dimensions specified by \code{time_dim}. A 1D array with named dimension can be provided (see details and examples)} \item{kmin}{First wavenumber for spectral slope (default: \code{kmin=1}).} @@ -96,6 +95,9 @@ References: Terzago, S. et al. (2018). NHESS 18(11), 2825-2840. http://doi.org/10.5194/nhess-18-2825-2018, D'Onofrio et al. (2014), J of Hydrometeorology 15, 830-843; Rebora et. al. (2006), JHM 7, 724. } +\details{ +Wether parameter 'slope' and 'weights' presents seasonality dependency, a dimension name should match between these parameters and the input data in parameter 'data'. See example 2 below where weights and slope vary with 'sdate' dimension. +} \examples{ # Example for the 'reduced' RainFARM function nf <- 8 # Choose a downscaling by factor 8 @@ -107,7 +109,7 @@ dim(pr) <- c(lon = 8, lat = 8, ftime = 20) lon_mat <- seq(10, 13.5, 0.5) # could also be a 2d matrix lat_mat <- seq(40, 43.5, 0.5) # Create a test array of weights -ww <- array(1., dim = c(8 * nf, 8 * nf)) +ww <- array(1., dim = c(lon = 8 * nf, lat = 8 * nf)) # or create proper weights using an external fine-scale climatology file # Specify a weightsfn filename if you wish to save the weights \dontrun{ @@ -126,7 +128,11 @@ str(res) dim(res$data) # lon lat ftime realization # 64 64 20 2 - +# Example 2: +slo <- array(c(0.1, 0.5, 0.7), c(sdate= 3)) +wei <- array(rnorm(8*8*3), c(lon = 8, lat = 8, sdate = 3)) +res <- RainFARM(lonlat_prec$data, lon = lonlat_prec$lon, + lat = lonlat_prec$lat, weights = wei, slope = slo, nf = 2) } \author{ Jost von Hardenberg - ISAC-CNR, \email{j.vonhardenberg@isac.cnr.it} diff --git a/tests/testthat/test-CST_RainFARM.R b/tests/testthat/test-CST_RainFARM.R index 3608d4e8c3cd3347243289dd4b5d89cc4a9d0c41..d9414925dc4a4d1a48060022e02c93e9a9f430ed 100644 --- a/tests/testthat/test-CST_RainFARM.R +++ b/tests/testthat/test-CST_RainFARM.R @@ -26,7 +26,7 @@ test_that("Sanity checks and simple use cases", { ) expect_error( res <- CST_RainFARM(exp, nf=8, weights=array(0,dim=c(2,2))), - "The dimensions of the weights matrix" + "Parameter 'weights' must have dimension names when it is not a scalar." ) dimexp=dim(exp$data) @@ -92,7 +92,7 @@ test_that("Sanity checks and simple use cases", { # Use climatological mean of PF precipitation to generate sythetic weights w <- rfweights(rpfm, res$lon, res$lat, exp$lon, exp$lat, 8, fsmooth=FALSE ) - + names(dim(w)) <- c('lon', 'lat') res <- CST_RainFARM(exppf, nf=8, time_dim=c("ftime", "sdate", "member"), nens=2, nproc=2, fsmooth=FALSE) resw <- CST_RainFARM(exppf, nf=8, time_dim=c("ftime", "sdate", "member"),