-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c0be2e
commit 23b6dc7
Showing
8 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' match object dimensions of a magclass object to the dimensions of a reference object | ||
#' | ||
#' A helper that restricts and expands a magclass object x to the size of a magclass object ref. | ||
#' Dimension names not present in x are added and set to the value provided by 'fill'. | ||
#' Dimension names not present in ref are cropped. | ||
#' | ||
#' @param x a magclass object to be modified | ||
#' @param ref a magclass object used as a reference for the modification | ||
#' @param fill value to be set in new dimensions | ||
#' @param dim subset of dimensions for which the matching should be done. Either number or | ||
#' name of dimension or a vector of these. See \code{\link{dimCode}} for more details. | ||
#' Set to NULL (default) for matching all dimensions. | ||
#' @author Falk Benke | ||
#' | ||
#' @export | ||
matchDim <- function(x, ref, dim = NULL, fill = NA) { | ||
|
||
if (is.null(dim)) { | ||
dim <- c(1, 2, 3) | ||
} else { | ||
dim <- dimCode(dim, x, missing = "stop") | ||
} | ||
|
||
if (3 %in% dim && ndim(x, dim = 3) != ndim(ref, dim = 3)) { | ||
stop( | ||
"Unsupported case: magclass objects x and ref have different number of ", | ||
"subdimensions in third dimension." | ||
) | ||
} | ||
|
||
# extend new object to the union of both objects | ||
d1 <- if (1 %in% dim) union(getItems(x, dim = 1), getItems(ref, dim = 1)) else getItems(x, dim = 1) | ||
d2 <- if (2 %in% dim) union(getItems(x, dim = 2), getItems(ref, dim = 2)) else getItems(x, dim = 2) | ||
d3 <- if (3 %in% dim) union(getItems(x, dim = 3), getItems(ref, dim = 3)) else getItems(x, dim = 3) | ||
|
||
r <- new.magpie( | ||
cells_and_regions = d1, | ||
years = d2, | ||
names = d3, | ||
fill = fill, | ||
sets = names(dimnames(ref)) | ||
) | ||
|
||
# copy over values from x | ||
r[getItems(x, dim = 1), getItems(x, dim = 2), getItems(x, dim = 3)] <- x | ||
|
||
# restrict object to dimensions of ref | ||
if (1 %in% dim) r <- r[getItems(ref, dim = 1), , ] | ||
if (2 %in% dim) r <- r[, getItems(ref, dim = 2), ] | ||
if (3 %in% dim) r <- r[, , getItems(ref, dim = 3)] | ||
|
||
return(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
test_that("matchDim works", { | ||
|
||
mp <- new.magpie( | ||
cells_and_regions = c("AAA", "BBB", "CCC"), | ||
years = paste0("y", c(2000, 2005, 2010)), | ||
names = c("foo", "bar", "baz"), | ||
sets = c("region", "t", "name") | ||
) | ||
|
||
mp2 <- new.magpie( | ||
cells_and_regions = c("BBB", "CCC", "DDD"), | ||
years = paste0("y", c(2000, 2010, 2020)), | ||
names = c("foo", "bar", "qux"), | ||
sets = c("region", "t", "name") | ||
) | ||
|
||
# match all dimensions | ||
x <- matchDim(mp, mp2) | ||
expect_equal(getItems(x, dim = 1), c("BBB", "CCC", "DDD")) | ||
expect_equal(getItems(x, dim = 2), c("y2000", "y2010", "y2020")) | ||
expect_equal(getItems(x, dim = 3), c("foo", "bar", "qux")) | ||
|
||
# match only spatial | ||
x <- matchDim(mp, mp2, dim = 1) | ||
expect_equal(getItems(x, dim = 1), c("BBB", "CCC", "DDD")) | ||
expect_equal(getItems(x, dim = 2), c("y2000", "y2005", "y2010")) | ||
expect_equal(getItems(x, dim = 3), c("foo", "bar", "baz")) | ||
|
||
# match only temporal | ||
x <- matchDim(mp, mp2, dim = 2) | ||
expect_equal(getItems(x, dim = 1), c("AAA", "BBB", "CCC")) | ||
expect_equal(getItems(x, dim = 2), c("y2000", "y2010", "y2020")) | ||
expect_equal(getItems(x, dim = 3), c("foo", "bar", "baz")) | ||
|
||
# match only third | ||
x <- matchDim(mp, mp2, dim = 3) | ||
expect_equal(getItems(x, dim = 1), c("AAA", "BBB", "CCC")) | ||
expect_equal(getItems(x, dim = 2), c("y2000", "y2005", "y2010")) | ||
expect_equal(getItems(x, dim = 3), c("foo", "bar", "qux")) | ||
|
||
}) |