From a275e30256541851d727af2dc9cf216300853390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Tue, 16 Jan 2024 16:19:30 +0100 Subject: [PATCH 1/5] fix read.magpie nc --- R/as.magpie.R | 3 ++- R/magclass-package.R | 27 ++++++++------------------- R/read.magpie.R | 20 +++++++++++--------- man/magclass-package.Rd | 16 +++++++++++++--- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/R/as.magpie.R b/R/as.magpie.R index 599d9df2..70dcc33d 100644 --- a/R/as.magpie.R +++ b/R/as.magpie.R @@ -333,7 +333,8 @@ setMethod("as.magpie", crs <- NULL if (inherits(x, "SpatRaster")) { if (!requireNamespace("terra", quietly = TRUE)) stop("The package \"terra\" is required for raster conversions!") - df <- as.data.frame(x, na.rm = TRUE, xy = TRUE) + df <- as.data.frame(x, xy = TRUE) + df <- df[rowSums(is.na(df)) != ncol(df), ] # remove rows with all NAs df$x <- sub(".", "p", df$x, fixed = TRUE) df$y <- sub(".", "p", df$y, fixed = TRUE) idVars <- c("x", "y") diff --git a/R/magclass-package.R b/R/magclass-package.R index 3c5bb9ea..8803f235 100644 --- a/R/magclass-package.R +++ b/R/magclass-package.R @@ -1,26 +1,15 @@ - - #' Data Class and Tools for Handling Spatial-Temporal Data -#' -#' Data class for increased interoperability working with spatial-temporal -#' data together with corresponding functions and methods (conversions, +#' +#' 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 and other dimensions to facilitate the development #' and interoperability of tools build for it. Additional features are name-based -#' addressing of data and internal consistency checks (e.g. checking for the +#' addressing of data and internal consistency checks (e.g. checking for the #' right data order in calculations). -#' +#' #' @name magclass-package #' @aliases magclass-package magclass -#' @docType package -#' @author -#' Maintainer: Jan Philipp Dietrich -NULL - - - - - - - - +#' @author +#' Maintainer: Jan Philipp Dietrich +"_PACKAGE" diff --git a/R/read.magpie.R b/R/read.magpie.R index 7d1882c1..b036aa9e 100644 --- a/R/read.magpie.R +++ b/R/read.magpie.R @@ -119,15 +119,17 @@ read.magpie <- function(file_name, file_folder = "", file_type = NULL, # nolint: x <- terra::rast(fileName) if (all(grepl("Time=[0-9]+", names(x)))) { names(x) <- sub("(.+)_Time=([0-9]+)", "y\\2..\\1", names(x)) - } else if (all(grepl("_", names(x)))) { - names(x) <- vapply(names(x), function(n) { - parts <- strsplit(n, "_")[[1]] # e.g. "AFR_3" where 3 means the third entry in terra::time(x) - year <- terra::time(x)[as.integer(parts[2])] - if (is.na(year)) { - year <- as.integer(parts[2]) - } - return(paste0("y", year, "..", parts[1])) - }, character(1)) + } else { + parts <- strsplit(names(x), "_") + lastParts <- vapply(parts, function(p) p[length(p)], character(1)) + timeIndices <- suppressSpecificWarnings(as.numeric(lastParts), "NAs introduced by coercion") + + terraTime <- terra::time(x) + + if (all(!is.na(terraTime)) && all(timeIndices %in% seq_along(terraTime))) { + names(x) <- paste0("y", terraTime[timeIndices], "..", + vapply(parts, function(p) paste0(p[-length(p)], collapse = "_"), character(1))) + } } readMagpie <- clean_magpie(as.magpie(x)) diff --git a/man/magclass-package.Rd b/man/magclass-package.Rd index 672ffd7e..e353f0df 100644 --- a/man/magclass-package.Rd +++ b/man/magclass-package.Rd @@ -3,16 +3,26 @@ \docType{package} \name{magclass-package} \alias{magclass-package} +\alias{_PACKAGE} \alias{magclass} \title{Data Class and Tools for Handling Spatial-Temporal Data} \description{ -Data class for increased interoperability working with spatial-temporal -data together with corresponding functions and methods (conversions, +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 and other dimensions to facilitate the development and interoperability of tools build for it. Additional features are name-based -addressing of data and internal consistency checks (e.g. checking for the +addressing of data and internal consistency checks (e.g. checking for the right data order in calculations). +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/pik-piam/magclass} + \item \doi{10.5281/zenodo.1158580} + \item Report bugs at \url{https://github.com/pik-piam/magclass/issues} +} + } \author{ Maintainer: Jan Philipp Dietrich From 9dfa574923163de38b7c2b5001b240ea46ebd578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Wed, 17 Jan 2024 17:24:07 +0100 Subject: [PATCH 2/5] read nc time via ncdf4 if necessary --- R/read.magpie.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/R/read.magpie.R b/R/read.magpie.R index b036aa9e..b4f463b6 100644 --- a/R/read.magpie.R +++ b/R/read.magpie.R @@ -126,9 +126,28 @@ read.magpie <- function(file_name, file_folder = "", file_type = NULL, # nolint: terraTime <- terra::time(x) - if (all(!is.na(terraTime)) && all(timeIndices %in% seq_along(terraTime))) { + if (any(is.na(terraTime))) { + nc <- ncdf4::nc_open(fileName) + withr::defer(ncdf4::nc_close(nc)) + if ("time" %in% names(nc$dim)) { + terraTime <- rep_len(nc$dim$time$vals, terra::nlyr(x)) + if (terra::nlyr(x) %% nc$dim$time$len != 0) { + warning("Found ", terra::nlyr(x), " layers, but ", + nc$dim$time$len, " time steps. Now using ", + terraTime) + } + } + } + if (any(is.na(terraTime))) { + warning("Could not read time information from file. Falling back to enumerating.") + terraTime <- seq_along(terraTime) + } + + if (all(timeIndices %in% seq_along(terraTime))) { names(x) <- paste0("y", terraTime[timeIndices], "..", vapply(parts, function(p) paste0(p[-length(p)], collapse = "_"), character(1))) + } else { + names(x) <- paste0("y", terraTime, "..", names(x)) } } From f324298285f6a9a316cb4c9416f9139b977277f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Wed, 17 Jan 2024 17:38:14 +0100 Subject: [PATCH 3/5] remove enumerate fallback --- R/read.magpie.R | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/R/read.magpie.R b/R/read.magpie.R index b4f463b6..3ee62178 100644 --- a/R/read.magpie.R +++ b/R/read.magpie.R @@ -138,15 +138,11 @@ read.magpie <- function(file_name, file_folder = "", file_type = NULL, # nolint: } } } - if (any(is.na(terraTime))) { - warning("Could not read time information from file. Falling back to enumerating.") - terraTime <- seq_along(terraTime) - } if (all(timeIndices %in% seq_along(terraTime))) { names(x) <- paste0("y", terraTime[timeIndices], "..", vapply(parts, function(p) paste0(p[-length(p)], collapse = "_"), character(1))) - } else { + } else if (all(!is.na(terraTime))) { names(x) <- paste0("y", terraTime, "..", names(x)) } } From af2d5d1106510cc8f3b0c9b382c751f43d41273b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Wed, 17 Jan 2024 18:04:00 +0100 Subject: [PATCH 4/5] require ncdf4 --- R/read.magpie.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/read.magpie.R b/R/read.magpie.R index 3ee62178..c49c2a5b 100644 --- a/R/read.magpie.R +++ b/R/read.magpie.R @@ -126,7 +126,7 @@ read.magpie <- function(file_name, file_folder = "", file_type = NULL, # nolint: terraTime <- terra::time(x) - if (any(is.na(terraTime))) { + if (any(is.na(terraTime)) && requireNamespace("ncdf4", quietly = TRUE)) { nc <- ncdf4::nc_open(fileName) withr::defer(ncdf4::nc_close(nc)) if ("time" %in% names(nc$dim)) { From 84b56bfc4cb694018c3da15a78a10f56dde1ff04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20F=C3=BChrlich?= Date: Thu, 18 Jan 2024 11:40:06 +0100 Subject: [PATCH 5/5] build --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- README.md | 8 ++++---- man/magclass-package.Rd | 1 - 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index a718e711..6d0a554d 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '120989154' +ValidationKey: '121045680' 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 30e3e377..59502d12 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.13.1 -date-released: '2024-01-12' +version: 6.13.2 +date-released: '2024-01-18' 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 e4d409ed..29a87f34 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.13.1 -Date: 2024-01-12 +Version: 6.13.2 +Date: 2024-01-18 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/README.md b/README.md index 02c3bb8b..093c22f0 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.13.1** +R package **magclass**, version **6.13.2** [![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 , R package version 6.13.1, . +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 (URL: https://doi.org/10.5281/zenodo.1158580), R package version 6.13.2, . A BibTeX entry for LaTeX users is @@ -65,8 +65,8 @@ 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.13.1}, - url = {https://github.com/pik-piam/magclass}, + note = {R package version 6.13.2}, doi = {10.5281/zenodo.1158580}, + url = {https://github.com/pik-piam/magclass}, } ``` diff --git a/man/magclass-package.Rd b/man/magclass-package.Rd index e353f0df..086d3a71 100644 --- a/man/magclass-package.Rd +++ b/man/magclass-package.Rd @@ -3,7 +3,6 @@ \docType{package} \name{magclass-package} \alias{magclass-package} -\alias{_PACKAGE} \alias{magclass} \title{Data Class and Tools for Handling Spatial-Temporal Data} \description{