From e20f8dd3706ec0c4282dbf02181bd34bd11e1475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Tue, 4 Jul 2023 11:01:28 +0200 Subject: [PATCH] as.SpatRaster sets terra::time --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/as.SpatRaster.R | 3 +++ README.md | 6 +++--- tests/testthat/test-raster.R | 10 +++++++++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 6361745c..3dca94e9 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '119151300' +ValidationKey: '119225742' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 720d13a1..7928e029 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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.10.0 -date-released: '2023-06-25' +version: 6.10.1 +date-released: '2023-07-04' 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 diff --git a/DESCRIPTION b/DESCRIPTION index 78344c60..d94c55a6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: magclass Title: Data Class and Tools for Handling Spatial-Temporal Data -Version: 6.10.0 -Date: 2023-06-25 +Version: 6.10.1 +Date: 2023-07-04 Authors@R: c( person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre")), person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = "aut"), diff --git a/R/as.SpatRaster.R b/R/as.SpatRaster.R index fd5a1fec..0021ee72 100644 --- a/R/as.SpatRaster.R +++ b/R/as.SpatRaster.R @@ -54,5 +54,8 @@ as.SpatRaster <- function(x, res = NULL) { # nolint: object_name_linter target <- terra::rast(ncols = 360 / res, nrows = 180 / res, nlyrs = nyears(x) * ndata(x)) out <- terra::rast(xyz, crs = terra::crs(target)) names(out) <- colnames(m) + if (all(grepl("^y[0-9]+\\.\\.", names(out)))) { + terra::time(out, tstep = "years") <- as.integer(substr(names(out), 2, 5)) + } return(out) } diff --git a/README.md b/README.md index 07fcbf97..34b91027 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Data Class and Tools for Handling Spatial-Temporal Data -R package **magclass**, version **6.10.0** +R package **magclass**, version **6.10.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) @@ -56,7 +56,7 @@ In case of questions / problems please contact Jan Philipp Dietrich . +Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D (2023). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi:10.5281/zenodo.1158580 , R package version 6.10.1, . A BibTeX entry for LaTeX users is @@ -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}, year = {2023}, - note = {R package version 6.10.0}, + note = {R package version 6.10.1}, doi = {10.5281/zenodo.1158580}, url = {https://github.com/pik-piam/magclass}, } diff --git a/tests/testthat/test-raster.R b/tests/testthat/test-raster.R index 7dfa5bb6..57bf17a7 100644 --- a/tests/testthat/test-raster.R +++ b/tests/testthat/test-raster.R @@ -18,7 +18,15 @@ test_that("terra convertion does not alter data", { expect_equal(ndata(m), j) expect_equal(nyears(m), t) r2 <- as.SpatRaster(m) - expect_equal(r[[names(r)]], terra::extend(r2[[names(r)]], terra::ext(r))) + noTime <- function(a) { + terra::time(a) <- NULL + return(a) + } + # comparing times does not work here, so compare separately + expect_equal(noTime(r), noTime(terra::extend(r2[[names(r)]], terra::ext(r)))) + if (t > 1) { + expect_equal(terra::time(r2), rep_len(1900 + 1:t, terra::nlyr(r))) + } m2 <- as.magpie(r2) expect_identical(m, m2) v <- terra::as.polygons(r)