Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfuehrlich-pik committed Jan 11, 2024
1 parent 910805e commit c1dcdf6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions R/read.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ read.magpie <- function(file_name, file_folder = "", file_type = NULL, # nolint:
}

readMagpie <- clean_magpie(as.magpie(x))
attr(readMagpie, "crs") <- NULL
} else {
if (!requireNamespace("raster", quietly = TRUE)) {
stop("The package \"raster\" is required!")
Expand Down
3 changes: 3 additions & 0 deletions R/write.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ write.magpie <- function(x, # nolint: object_name_linter, cyclocomp_linter.
if (!requireNamespace("ncdf4", quietly = TRUE) || !requireNamespace("terra", quietly = TRUE)) {
stop("The packages \"ncdf4\" and \"terra\" are required!")
}
if (is.null(getItems(x, 3))) {
getItems(x, 3) <- "Variable"
}
spatRasterDataset <- as.SpatRasterDataset(x)

if (zname != "time") {
Expand Down
21 changes: 10 additions & 11 deletions tests/testthat/test-readwritemagpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,28 @@ test_that("handling of spatial data works", {
expect_no_warning(write.magpie(m10, file.path(td, "test.nc")))
expect_silent(write.magpie(m05, file.path(td, "test.nc")))
expect_silent(m05in <- read.magpie(file.path(td, "test.nc")))

getCoords(m05) <- magclass:::magclassdata$half_deg[c("lon", "lat")]
m05 <- collapseDim(m05, dim = c(1.1, 1.2))
m05 <- m05[getItems(m05in, dim = 1), , ]
getNames(m05in) <- NULL
getSets(m05in, fulldim = FALSE)[3] <- "data"
attr(m05in, ".internal.selfref") <- NULL # nolint
expect_identical(m05, m05in)

a <- maxample("animal")
a <- dimSums(a, dim = c(1.3, 1.4, 2, 3))
write.magpie(a, file.path(td, "animal.asc"))
copy.magpie(file.path(td, "animal.asc"), file.path(td, "animal.nc"))
asc <- read.magpie(file.path(td, "animal.asc"))
expect_silent(anc <- read.magpie(file.path(td, "animal.nc")))
attr(asc, ".internal.selfref") <- NULL # nolint
getItems(asc, dim = 2) <- NULL
getItems(asc, dim = 3) <- NULL
expect_identical(asc, a)

.clean <- function(x) {
attr(x, ".internal.selfref") <- NULL # nolint
getItems(x, dim = 2) <- NULL
getItems(x, dim = 3) <- NULL
return(x)
}
expect_identical(.clean(asc), a)
expect_identical(.clean(anc), a)
write.magpie(a, file.path(td, "animal.nc"))
expect_silent(anc <- read.magpie(file.path(td, "animal.nc")))
getItems(anc, dim = 3) <- NULL
getSets(anc) <- c("x", "y", "d2", "d3")
expect_identical(anc, a)
})


Expand Down

0 comments on commit c1dcdf6

Please sign in to comment.