From c15daf413e91243e3208cf1d3ec842094f09d1f1 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 8 Nov 2023 16:05:29 +0100 Subject: [PATCH 1/2] Add new param print_sys_msg to show cdo system message if needed --- R/CDORemap.R | 9 ++++++--- man/CDORemap.Rd | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/R/CDORemap.R b/R/CDORemap.R index 4ea14fd..a401bab 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -76,6 +76,8 @@ #'@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 print_sys_msg A logical value indicating to print the messages from +#' system CDO commands. The default is FALSE to keep function using clean. #'@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. @@ -227,6 +229,7 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, avoid_writes = TRUE, crop = TRUE, force_remap = FALSE, write_dir = tempdir(), + print_sys_msg = FALSE, ncores = NULL) { #, mask = NULL) { .isRegularVector <- function(x, tol = 0.1) { if (length(x) < 2) { @@ -828,17 +831,17 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, if (is.null(ncores)) { err <- try({ system(paste0("cdo -s ", sellonlatbox, "remap", method, ",", grid, " ", - tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) + tmp_file, " ", tmp_file2), ignore.stdout = print_sys_msg, ignore.stderr = print_sys_msg) }) } else { err <- try({ system(paste0("cdo -P ", ncores," -s ", sellonlatbox, "remap", method, ",", - grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = T, ignore.stderr = T) + grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = print_sys_msg, ignore.stderr = print_sys_msg) }) } file.remove(tmp_file) if (is(err, 'try-error') || err > 0) { - stop("CDO remap failed. Possible problem: parameter 'grid'.") + stop("CDO remap failed. Set 'print_sys_msg' to TRUE to see CDO system message..") } ncdf_remapped <- nc_open(tmp_file2) if (!lons_lats_taken) { diff --git a/man/CDORemap.Rd b/man/CDORemap.Rd index d7eee21..9bfeaa5 100644 --- a/man/CDORemap.Rd +++ b/man/CDORemap.Rd @@ -14,6 +14,7 @@ CDORemap( crop = TRUE, force_remap = FALSE, write_dir = tempdir(), + print_sys_msg = FALSE, ncores = NULL ) } @@ -87,6 +88,9 @@ is already on the target grid.} files for CDO to work. By default, the R session temporary directory is used (\code{tempdir()}).} +\item{print_sys_msg}{A logical value indicating to print the messages from +system CDO commands. The default is FALSE to keep function using clean.} + \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.} -- GitLab From 42a30c152544d2791be17190d69918a5fddf15a1 Mon Sep 17 00:00:00 2001 From: AN CHI HO Date: Wed, 8 Nov 2023 16:20:46 +0100 Subject: [PATCH 2/2] Correct the logic --- R/CDORemap.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/CDORemap.R b/R/CDORemap.R index a401bab..b4f1545 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -831,12 +831,12 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, if (is.null(ncores)) { err <- try({ system(paste0("cdo -s ", sellonlatbox, "remap", method, ",", grid, " ", - tmp_file, " ", tmp_file2), ignore.stdout = print_sys_msg, ignore.stderr = print_sys_msg) + tmp_file, " ", tmp_file2), ignore.stdout = !print_sys_msg, ignore.stderr = !print_sys_msg) }) } else { err <- try({ system(paste0("cdo -P ", ncores," -s ", sellonlatbox, "remap", method, ",", - grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = print_sys_msg, ignore.stderr = print_sys_msg) + grid, " ", tmp_file, " ", tmp_file2), ignore.stdout = !print_sys_msg, ignore.stderr = !print_sys_msg) }) } file.remove(tmp_file) -- GitLab