Skip to content

Commit

Permalink
Merge pull request #164 from pfuehrlich-pik/master
Browse files Browse the repository at this point in the history
fix read.magpie for nc files
  • Loading branch information
pfuehrlich-pik authored Jan 18, 2024
2 parents 757f3a9 + 84b56bf commit 3c385d6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 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.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
Expand Down
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.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"),
Expand Down
3 changes: 2 additions & 1 deletion R/as.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
27 changes: 8 additions & 19 deletions R/magclass-package.R
Original file line number Diff line number Diff line change
@@ -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 <dietrich@@pik-potsdam.de>
NULL








#' @author
#' Maintainer: Jan Philipp Dietrich <dietrich@pik-potsdam.de>
"_PACKAGE"
33 changes: 25 additions & 8 deletions R/read.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,32 @@ 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])
} 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 (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)) {
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)
}
}
return(paste0("y", year, "..", parts[1]))
}, character(1))
}

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 if (all(!is.na(terraTime))) {
names(x) <- paste0("y", terraTime, "..", names(x))
}
}

readMagpie <- clean_magpie(as.magpie(x))
Expand Down
8 changes: 4 additions & 4 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.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)

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.13.1, <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 (URL: https://doi.org/10.5281/zenodo.1158580), R package version 6.13.2, <URL: https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
```
6 changes: 3 additions & 3 deletions man/magclass-package.Rd

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

0 comments on commit 3c385d6

Please sign in to comment.