forked from pik-piam/madrat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adapted metadata handling from calcOutput in readSource (metadata w…
…ill now be added as comment to the returned data objects) - added helper function getMetadataComment to more easily extract metadata from a data object
- Loading branch information
1 parent
da201b8
commit e9db236
Showing
18 changed files
with
295 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
ValidationKey: '62859660' | ||
ValidationKey: '63066150' | ||
AcceptedWarnings: | ||
- 'Warning: package ''.*'' was built under R version' | ||
- 'Warning: namespace ''.*'' is not available and has been replaced' | ||
AcceptedNotes: unable to verify current time | ||
AutocreateReadme: yes | ||
allowLinterWarnings: no | ||
enforceVersionUpdate: no | ||
skipCoverage: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#' cleanComment | ||
#' | ||
#' Helper function to clean a comment from additional metadata information | ||
#' | ||
#' @md | ||
#' @param x magclass object the comment shold be read from | ||
#' @param remove Vector of categories to be removed | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' x <- maxample("animal") | ||
#' getComment(x) <- c("unit: bla","comment: hallo","blub: ble") | ||
#' madrat:::cleanComment(x) | ||
|
||
|
||
cleanComment <- function(x, remove = c("unit", "description", "comment", "origin", "creation date", "note")) { | ||
# remove old descriptors | ||
x <- getComment(x) | ||
out <- grep(paste0("^ *(", paste(remove, collapse = "|"), "):"), x, value = TRUE, invert = TRUE) | ||
if (length(out) == 0) return(NULL) | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#' getMetadataComment | ||
#' | ||
#' Helper function extract a metadata comment | ||
#' | ||
#' @md | ||
#' @param x object the metadata should be extracted from | ||
#' @param name name of the metadata to be extracted (e.g. unit) | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' x <- as.magpie(1) | ||
#' getComment(x) <- c(" description: example description", " unit: kg") | ||
#' getMetadataComment(x, "unit") | ||
#' getMetadataComment(x, "description") | ||
#' @export | ||
|
||
getMetadataComment <- function(x, name) { | ||
comment <- attr(x, "comment") | ||
key <- paste0("^ ", name, ": ") | ||
entry <- grep(key, comment) | ||
if (length(entry) == 0) return(NULL) | ||
if (length(entry) > 1) stop("duplicate metadata entries found!") | ||
return(sub(key, "", comment[entry])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' prepComment | ||
#' | ||
#' Helper function to properly format a metadata comment entry | ||
#' | ||
#' @md | ||
#' @param x content to be added as metadata comment | ||
#' @param name Name of the metadata entry | ||
#' @param warning Either NULL (no warning) or a warning text that should be | ||
#' returned if x is NULL | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' madrat:::prepComment("example comment", "example") | ||
|
||
prepComment <- function(x, name, warning = NULL) { | ||
if (!is.null(x)) { | ||
x[1] <- paste0(" ", name, ": ", x[1]) | ||
if (length(x) > 1) { | ||
x[2:length(x)] <- paste0(paste(rep(" ", 3 + nchar(name)), collapse = ""), x[2:length(x)]) | ||
} | ||
} else { | ||
if (!is.null(warning)) { | ||
vcat(0, warning) | ||
x <- paste0(" ", name, ": not provided") | ||
} | ||
} | ||
return(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' prepExtendedComment | ||
#' | ||
#' Helper function condense metadata information into | ||
#' an extended comment entry | ||
#' | ||
#' @md | ||
#' @param x list containing the metadata to be condensed | ||
#' @param type output type, e.g. "TauTotal" | ||
#' @param warn boolean indicating whether warnings should be triggered | ||
#' if entries are missing, or not. | ||
#' @param n the number of generations to go back | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' test <- function(a = 1) { | ||
#' return(madrat:::prepExtendedComment(list(unit = "m", description = "example", package = "blub"))) | ||
#' } | ||
#' test(a = 42) | ||
#' | ||
prepExtendedComment <- function(x, type = "#undefined", warn = TRUE, n = 1) { | ||
|
||
cl <- sys.call(-n) | ||
f <- get(as.character(cl[[1]]), mode = "function", sys.frame(-n - 1)) | ||
cl <- match.call(definition = f, call = cl) | ||
|
||
if (isTRUE(warn)) { | ||
unitWarning <- paste0('Missing unit information for data set "', type, '"!') | ||
descriptionWarning <- paste0('Missing description for data set "', type, | ||
'"! Please add a description in the corresponding calc function!') | ||
} else { | ||
unitWarning <- descriptionWarning <- NULL | ||
} | ||
|
||
unit <- prepComment(x$unit, "unit", unitWarning) | ||
description <- prepComment(x$description, "description", descriptionWarning) | ||
comment <- prepComment(cleanComment(x$x), "comment") | ||
origin <- prepComment(paste0(gsub("\\s{2,}", " ", paste(deparse(cl), collapse = "")), | ||
" (madrat ", unname(getNamespaceVersion("madrat")), " | ", x$package, ")"), | ||
"origin") | ||
date <- prepComment(date(), "creation date") | ||
note <- prepComment(x$note, "note") | ||
|
||
extendedComment <- c(description, | ||
unit, | ||
note, | ||
comment, | ||
origin, | ||
date) | ||
return(extendedComment) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.