Skip to content

Commit

Permalink
add expandMagclass helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Mar 4, 2024
1 parent 4b1f4a1 commit 735d778
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions R/expandMagclass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
expandMagclass <- function(x, ref, fill = 0) {
r <- new.magpie(
cells_and_regions = union(getRegions(x), getRegions(ref)),
years = union(getYears(x), getYears(ref)),
names = union(getNames(x), getNames(ref)),
fill = fill,
sets = names(dimnames(ref))
)
r[getRegions(x), getYears(x), getNames(x)] <- x
return(r)
}
38 changes: 38 additions & 0 deletions tests/testthat/test-expandMagclass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("expandMagclass works", {
region <- c("AAA", "BBB", "CCC")
t <- paste0("y", c(2000, 2005, 2010))
name <- c("foo", "bar", "bazz")

A <- new.magpie(
cells_and_regions = region, years = t, names = name, fill = 1,
sets = c("region", "t", "name")
)

# spatial ----

## x smaller than ref ----
B <- expandMagclass(A[region[-2], , ], A)
expect_true(all(region %in% getRegions(B)))

## x larger than ref ----
expect_equal(expandMagclass(A, A[region[-2], , ]), A)

# temporal ----

## x smaller than ref ----
B <- expandMagclass(A[, t[-2], ], A)
expect_true(all(t %in% getYears(B)))

## x larger than ref ----
expect_equal(expandMagclass(x = A, ref = A[, t[-2], ]), A)

# names ----

## x smaller than ref ----
B <- expandMagclass(A[,,name[-2]], A)
expect_true(all(name %in% getNames(B)))

## x larger than ref ----
expect_equal(expandMagclass(A, A[,,name[-2]]), A)

})

0 comments on commit 735d778

Please sign in to comment.