From dc9b16cfee93e08cd339a97166280a6c19fbd820 Mon Sep 17 00:00:00 2001 From: Carlos Delgado Date: Wed, 22 Feb 2023 12:25:08 +0100 Subject: [PATCH 1/2] included paralelisation but does not reduce the computational time --- R/CDORemap.R | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/R/CDORemap.R b/R/CDORemap.R index 927b107..9e16011 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -77,6 +77,8 @@ #' files for CDO to work. By default, the R session temporary directory is #' used (\code{tempdir()}). #' +#'@param ncores Number of theats used for interpolation. +#' #'@return A list with the following components: #' \item{'data_array'}{The interpolated data array (if an input array #' is provided at all, NULL otherwise).} @@ -223,7 +225,8 @@ #'@export CDORemap <- function(data_array = NULL, lons, lats, grid, method, avoid_writes = TRUE, crop = TRUE, - force_remap = FALSE, write_dir = tempdir()) { #, mask = NULL) { + force_remap = FALSE, write_dir = tempdir(), + ncores = NULL) { #, mask = NULL) { .isRegularVector <- function(x, tol = 0.1) { if (length(x) < 2) { #stop("The provided vector must be of length 2 or greater.") @@ -564,6 +567,12 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, if (!dir.exists(write_dir)) { stop("Parameter 'write_dir' must point to an existing directory.") } + # Check ncores + if (is.null(ncores)) { + ncores <- 1 + } else if (!is.integer(ncores) | ncores < 1) + stop("Parameter 'ncores' must be NULL or an integer equal or greater than 1") + } # if (!is.null(mask)) { # if (!is.numeric(mask) || !is.array(mask)) { # stop("Parameter 'mask' must be a numeric array.") @@ -814,7 +823,7 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, ',', format(lat_extremes[2], scientific = FALSE), ' -') } err <- try({ - system(paste0("cdo -s ", sellonlatbox, "remap", method, ",", grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) + system(paste0("cdo -P ", ncores," -s ", sellonlatbox, "remap", method, ",", grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) }) file.remove(tmp_file) if (is(err, 'try-error') || err > 0) { -- GitLab From fa323427879dcd6d7b7b142f259b237d82d8c0ea Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 22 May 2023 09:33:21 +0200 Subject: [PATCH 2/2] Refine ncores --- R/CDORemap.R | 28 +++++++++++++++++++--------- man/CDORemap.Rd | 7 ++++++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/R/CDORemap.R b/R/CDORemap.R index f67c70f..4ea14fd 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -76,8 +76,9 @@ #'@param write_dir Path to the directory where to create the intermediate #' files for CDO to work. By default, the R session temporary directory is #' used (\code{tempdir()}). -#' -#'@param ncores Number of theats used for interpolation. +#'@param ncores An integer indicating the number of theads used for +#' interpolation (i.e., \code{-P} in cdo command.) The default value is NULL +#' and \code{-P} is not used. #' #'@return A list with the following components: #' \item{'data_array'}{The interpolated data array (if an input array @@ -568,10 +569,11 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, stop("Parameter 'write_dir' must point to an existing directory.") } # Check ncores - if (is.null(ncores)) { - ncores <- 1 - } else if (!is.integer(ncores) | ncores < 1) - stop("Parameter 'ncores' must be NULL or an integer equal or greater than 1") + if (!is.null(ncores)) { + if (!is.numeric(ncores) | any(ncores %% 1 != 0) | any(ncores < 0) | + length(ncores) > 1) { + stop("Parameter 'ncores' must be a positive integer.") + } } # if (!is.null(mask)) { # if (!is.numeric(mask) || !is.array(mask)) { @@ -823,9 +825,17 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, ',', format(lat_extremes[1], scientific = FALSE), ',', format(lat_extremes[2], scientific = FALSE), ' -') } - err <- try({ - system(paste0("cdo -P ", ncores," -s ", sellonlatbox, "remap", method, ",", grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) - }) + if (is.null(ncores)) { + err <- try({ + system(paste0("cdo -s ", sellonlatbox, "remap", method, ",", grid, " ", + tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) + }) + } else { + err <- try({ + system(paste0("cdo -P ", ncores," -s ", sellonlatbox, "remap", method, ",", + grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) + }) + } file.remove(tmp_file) if (is(err, 'try-error') || err > 0) { stop("CDO remap failed. Possible problem: parameter 'grid'.") diff --git a/man/CDORemap.Rd b/man/CDORemap.Rd index 61f6101..d7eee21 100644 --- a/man/CDORemap.Rd +++ b/man/CDORemap.Rd @@ -13,7 +13,8 @@ CDORemap( avoid_writes = TRUE, crop = TRUE, force_remap = FALSE, - write_dir = tempdir() + write_dir = tempdir(), + ncores = NULL ) } \arguments{ @@ -85,6 +86,10 @@ is already on the target grid.} \item{write_dir}{Path to the directory where to create the intermediate files for CDO to work. By default, the R session temporary directory is used (\code{tempdir()}).} + +\item{ncores}{An integer indicating the number of theads used for +interpolation (i.e., \code{-P} in cdo command.) The default value is NULL +and \code{-P} is not used.} } \value{ A list with the following components: -- GitLab