Skip to content

Commit 37762a8

Browse files
authored
Merge pull request #175 from fbenke-pik/master
add function matchDim
2 parents 190e84f + 6746b70 commit 37762a8

File tree

8 files changed

+141
-9
lines changed

8 files changed

+141
-9
lines changed

.buildlibrary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ValidationKey: '122405360'
1+
ValidationKey: '122616222'
22
AcceptedWarnings:
33
- 'Warning: package ''.*'' was built under R version'
44
- 'Warning: namespace ''.*'' is not available and has been replaced'

CITATION.cff

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ cff-version: 1.2.0
22
message: If you use this software, please cite it using the metadata from this file.
33
type: software
44
title: 'magclass: Data Class and Tools for Handling Spatial-Temporal Data'
5-
version: 6.16.0
6-
date-released: '2024-05-28'
5+
version: 6.16.1
6+
date-released: '2024-06-28'
77
abstract: Data class for increased interoperability working with spatial-temporal
88
data together with corresponding functions and methods (conversions, basic calculations
99
and basic data manipulation). The class distinguishes between spatial, temporal
@@ -79,7 +79,7 @@ authors:
7979
given-names: Oliver
8080
affiliation: Potsdam Institute for Climate Impact Research
8181
license: LGPL-3.0
82-
keywords:
82+
keywords:
8383
- tool
8484
repository-code: https://github.com/pik-piam/magclass
8585
doi: 10.5281/zenodo.1158580

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Type: Package
22
Package: magclass
33
Title: Data Class and Tools for Handling Spatial-Temporal Data
4-
Version: 6.16.0
5-
Date: 2024-05-28
4+
Version: 6.16.1
5+
Date: 2024-06-28
66
Authors@R: c(
77
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de",
88
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431"), role = c("aut", "cre")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export(lowpass)
5858
export(magpie_expand)
5959
export(magpiesort)
6060
export(magpply)
61+
export(matchDim)
6162
export(maxample)
6263
export(mbind)
6364
export(mcalc)

R/matchDim.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#' match object dimensions of a magclass object to the dimensions of a reference object
2+
#'
3+
#' A helper that restricts and expands a magclass object x to the size of a magclass object ref.
4+
#' Dimension names not present in x are added and set to the value provided by 'fill'.
5+
#' Dimension names not present in ref are cropped.
6+
#'
7+
#' @param x a magclass object to be modified
8+
#' @param ref a magclass object used as a reference for the modification
9+
#' @param fill value to be set in new dimensions
10+
#' @param dim subset of dimensions for which the matching should be done.
11+
#' Can be either a number between 1 and 3 or a vector of these.
12+
#' Set to NULL (default) for matching all dimensions.
13+
#' @author Falk Benke
14+
#'
15+
#' @export
16+
matchDim <- function(x, ref, dim = NULL, fill = NA) {
17+
18+
if (is.null(dim)) {
19+
dim <- c(1, 2, 3)
20+
} else if (length(setdiff(dim, c(1, 2, 3))) > 0) {
21+
stop("argument 'dim' can only contain numbers between 1 and 3")
22+
}
23+
24+
for (i in dim) {
25+
if (ndim(x, dim = i) != ndim(ref, dim = i)) {
26+
stop(
27+
paste0(
28+
"Unsupported case: magclass objects x and ref have different number of ",
29+
"subdimensions in dimension ", i
30+
)
31+
)
32+
}
33+
}
34+
35+
# extend new object to the union of both objects
36+
d1 <- if (1 %in% dim) union(getItems(x, dim = 1), getItems(ref, dim = 1)) else getItems(x, dim = 1)
37+
d2 <- if (2 %in% dim) union(getItems(x, dim = 2), getItems(ref, dim = 2)) else getItems(x, dim = 2)
38+
d3 <- if (3 %in% dim) union(getItems(x, dim = 3), getItems(ref, dim = 3)) else getItems(x, dim = 3)
39+
40+
r <- new.magpie(
41+
cells_and_regions = d1,
42+
years = d2,
43+
names = d3,
44+
fill = fill,
45+
sets = names(dimnames(ref))
46+
)
47+
48+
# copy over values from x
49+
r[getItems(x, dim = 1), getItems(x, dim = 2), getItems(x, dim = 3)] <- x
50+
51+
# restrict object to dimensions of ref
52+
if (1 %in% dim) r <- r[getItems(ref, dim = 1), , ]
53+
if (2 %in% dim) r <- r[, getItems(ref, dim = 2), ]
54+
if (3 %in% dim) r <- r[, , getItems(ref, dim = 3)]
55+
56+
return(r)
57+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Data Class and Tools for Handling Spatial-Temporal Data
22

3-
R package **magclass**, version **6.16.0**
3+
R package **magclass**, version **6.16.1**
44

55
[![CRAN status](https://www.r-pkg.org/badges/version/magclass)](https://cran.r-project.org/package=magclass) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158580.svg)](https://doi.org/10.5281/zenodo.1158580) [![R build status](https://github.com/pik-piam/magclass/workflows/check/badge.svg)](https://github.com/pik-piam/magclass/actions) [![codecov](https://codecov.io/gh/pik-piam/magclass/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/magclass) [![r-universe](https://pik-piam.r-universe.dev/badges/magclass)](https://pik-piam.r-universe.dev/builds)
66

@@ -56,7 +56,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi
5656

5757
To cite package **magclass** in publications use:
5858

59-
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D, Sauer P (2024). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi:10.5281/zenodo.1158580 <https://doi.org/10.5281/zenodo.1158580>, R package version 6.16.0, <https://github.com/pik-piam/magclass>.
59+
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D, Sauer P (2024). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi:10.5281/zenodo.1158580 <https://doi.org/10.5281/zenodo.1158580>, R package version 6.16.1, <https://github.com/pik-piam/magclass>.
6060

6161
A BibTeX entry for LaTeX users is
6262

@@ -65,7 +65,7 @@ A BibTeX entry for LaTeX users is
6565
title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data},
6666
author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip and Pascal Sauer},
6767
year = {2024},
68-
note = {R package version 6.16.0},
68+
note = {R package version 6.16.1},
6969
doi = {10.5281/zenodo.1158580},
7070
url = {https://github.com/pik-piam/magclass},
7171
}

man/matchDim.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-matchDim.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
test_that("matchDim works", {
2+
3+
mp <- new.magpie(
4+
cells_and_regions = c("AAA", "BBB", "CCC"),
5+
years = paste0("y", c(2000, 2005, 2010)),
6+
names = c("foo", "bar", "baz"),
7+
sets = c("region", "t", "name")
8+
)
9+
10+
mp2 <- new.magpie(
11+
cells_and_regions = c("BBB", "CCC", "DDD"),
12+
years = paste0("y", c(2000, 2010, 2020)),
13+
names = c("foo", "bar", "qux"),
14+
sets = c("region", "t", "name")
15+
)
16+
17+
# match all dimensions
18+
x <- matchDim(mp, mp2)
19+
expect_equal(getItems(x, dim = 1), c("BBB", "CCC", "DDD"))
20+
expect_equal(getItems(x, dim = 2), c("y2000", "y2010", "y2020"))
21+
expect_equal(getItems(x, dim = 3), c("foo", "bar", "qux"))
22+
23+
# match only spatial
24+
x <- matchDim(mp, mp2, dim = 1)
25+
expect_equal(getItems(x, dim = 1), c("BBB", "CCC", "DDD"))
26+
expect_equal(getItems(x, dim = 2), c("y2000", "y2005", "y2010"))
27+
expect_equal(getItems(x, dim = 3), c("foo", "bar", "baz"))
28+
29+
# match only temporal
30+
x <- matchDim(mp, mp2, dim = 2)
31+
expect_equal(getItems(x, dim = 1), c("AAA", "BBB", "CCC"))
32+
expect_equal(getItems(x, dim = 2), c("y2000", "y2010", "y2020"))
33+
expect_equal(getItems(x, dim = 3), c("foo", "bar", "baz"))
34+
35+
# match only third
36+
x <- matchDim(mp, mp2, dim = 3)
37+
expect_equal(getItems(x, dim = 1), c("AAA", "BBB", "CCC"))
38+
expect_equal(getItems(x, dim = 2), c("y2000", "y2005", "y2010"))
39+
expect_equal(getItems(x, dim = 3), c("foo", "bar", "qux"))
40+
41+
# reject objects with varying subdimensions
42+
mp3 <- add_dimension(mp2, dim = 1.2, add = "subregion", "yyy")
43+
expect_error(matchDim(mp, mp3, dim = 1),
44+
regexp = "magclass objects x and ref have different number of subdimensions in dimension 1")
45+
expect_no_error(matchDim(mp, mp3, dim = 2))
46+
47+
})

0 commit comments

Comments
 (0)