Skip to content

Commit

Permalink
throw error on unexpected args, allow verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-sauer committed Feb 26, 2024
1 parent bf73304 commit 2056c18
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
19 changes: 15 additions & 4 deletions R/write.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
#' existing data can be combined with the new data using the mbind function
#' @param comment Vector of strings: Optional comment giving additional
#' information about the data. If different to NULL this will overwrite the
#' content of attr(x,"comment")
#' content of attr(x,"comment"). For nc files the unit can be passed
#' with e.g. 'comment = "unit: kg"'.
#' @param comment.char character: a character vector of length one containing a
#' single character or an empty string. Use "" to turn off the interpretation
#' of comments altogether.
Expand All @@ -62,7 +63,6 @@
#' Set to NULL system defaults will be used. Access codes are identical to the
#' codes used in unix function chmod.
#' @param zname name of the time variable for raster files like nc, asc, grd and tif
#' @param unit unit of the data to be written, only used for nc files
#' @param ... additional arguments passed to specific write functions
#' @note
#'
Expand Down Expand Up @@ -99,7 +99,7 @@ write.magpie <- function(x, # nolint: object_name_linter, cyclocomp_linter.
file_name, file_folder = "", file_type = NULL, # nolint: object_name_linter.
append = FALSE, comment = NULL,
comment.char = "*", # nolint: object_name_linter.
mode = NULL, zname = "time", unit = "", ...) {
mode = NULL, zname = "time", ...) {
umask <- Sys.umask()
if (!is.null(mode)) {
umaskMode <- as.character(777 - as.integer(mode))
Expand Down Expand Up @@ -218,7 +218,18 @@ write.magpie <- function(x, # nolint: object_name_linter, cyclocomp_linter.
getCoords(x) <- magclassdata$half_deg[c("lon", "lat")]
}
}
writeNC(x, file_name, unit = unit, zname = zname)
if (is.null(comment)) {
unit <- ""
} else {
indicators <- sub(":.*$", "", comment)
units <- sub("^.*: ", "", comment)
if (any(grepl("unit", indicators))) {
unit <- units[grep("unit", indicators)]
} else {
unit <- ""
}
}
writeNC(x, file_name, unit = unit, zname = zname, ...)
} else if (file_type == "rds") {
saveRDS(object = x, file = filePath, ...)
} else if (file_type == "cs3" || file_type == "cs3r") {
Expand Down
5 changes: 4 additions & 1 deletion R/writeNC.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ writeNC <- function(x, filename, unit, ..., compression = 2, missval = NA,
stop("The ncdf4 package is required to write netCDF files, please install it.")
}
# fail immediately if arguments are not set
stopifnot(is.character(filename), is.character(unit), ...length() == 0)
stopifnot(is.character(filename), is.character(unit))
if (!(...length() == 0 || all(...names() == "verbose"))) {
stop("Unknown argument passed to writeNC: ", paste(...names(), collapse = ", "))
}

# magclass objects are sparse, fill gaps with NA
coords <- getCoords(x)
Expand Down
6 changes: 2 additions & 4 deletions man/write.magpie.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tests/testthat/test-readwritemagpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ test_that("handling of spatial data works", {
getItems(asc, dim = 3) <- NULL
expect_identical(asc, a)

write.magpie(a, file.path(td, "animal.nc"))
expect_error({
write.magpie(a, file.path(td, "animal.nc"), unknownArg = TRUE, blabla = 1)
}, "Unknown argument passed to writeNC: unknownArg, blabla")

write.magpie(a, file.path(td, "animal.nc"), verbose = FALSE)
expect_silent(anc <- read.magpie(file.path(td, "animal.nc")))
getItems(anc, dim = 3) <- NULL
getSets(anc) <- c("x", "y", "d2", "d3")
Expand Down

0 comments on commit 2056c18

Please sign in to comment.