Skip to content

Commit

Permalink
add function matchDim
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Jun 27, 2024
1 parent 6c0be2e commit 23b6dc7
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '122405360'
ValidationKey: '122610061'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
9 changes: 4 additions & 5 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'magclass: Data Class and Tools for Handling Spatial-Temporal Data'
version: 6.16.0
date-released: '2024-05-28'
version: 6.16.1
date-released: '2024-06-27'
abstract: Data class for increased interoperability working with spatial-temporal
data together with corresponding functions and methods (conversions, basic calculations
and basic data manipulation). The class distinguishes between spatial, temporal
Expand All @@ -14,7 +14,7 @@ authors:
- family-names: Dietrich
given-names: Jan Philipp
email: dietrich@pik-potsdam.de
orcid: https://orcid.org/0000-0002-4309-6431
orcid: 0000-0002-4309-6431
affiliation: Potsdam Institute for Climate Impact Research
- family-names: Bodirsky
given-names: Benjamin Leon
Expand Down Expand Up @@ -79,8 +79,7 @@ authors:
given-names: Oliver
affiliation: Potsdam Institute for Climate Impact Research
license: LGPL-3.0
keywords:
- tool
keywords: tool
repository-code: https://github.com/pik-piam/magclass
doi: 10.5281/zenodo.1158580

4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: magclass
Title: Data Class and Tools for Handling Spatial-Temporal Data
Version: 6.16.0
Date: 2024-05-28
Version: 6.16.1
Date: 2024-06-27
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de",
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431"), role = c("aut", "cre")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export(lowpass)
export(magpie_expand)
export(magpiesort)
export(magpply)
export(matchDim)
export(maxample)
export(mbind)
export(mcalc)
Expand Down
53 changes: 53 additions & 0 deletions R/matchDim.R
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)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Class and Tools for Handling Spatial-Temporal Data

R package **magclass**, version **6.16.0**
R package **magclass**, version **6.16.1**

[![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)

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

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

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>.
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>.

A BibTeX entry for LaTeX users is

Expand All @@ -65,7 +65,7 @@ A BibTeX entry for LaTeX users is
title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data},
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},
year = {2024},
note = {R package version 6.16.0},
note = {R package version 6.16.1},
doi = {10.5281/zenodo.1158580},
url = {https://github.com/pik-piam/magclass},
}
Expand Down
27 changes: 27 additions & 0 deletions man/matchDim.Rd

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

41 changes: 41 additions & 0 deletions tests/testthat/test-matchDim.R
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"))

})

0 comments on commit 23b6dc7

Please sign in to comment.