Skip to content

Commit

Permalink
Minor revisions to metadata handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenbi committed Jan 16, 2018
1 parent 051c7a0 commit e299377
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: magclass
Type: Package
Title: Data Class and Tools for Handling Spatial-Temporal Data
Version: 4.72.4
Version: 4.72.5
Author: Jan Philipp Dietrich,
Benjamin Bodirsky,
Misko Stevanovic,
Expand Down
36 changes: 27 additions & 9 deletions R/getMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ getMetadata <- function(x, type=NULL) {
else{
if (length(value$unit)>1){
warning(value$unit," is an invalid argument for unit")
value$unit <- 1
}
value$unit <- "1"
}else if (is.null(value$unit)) value$unit <- "1"
if (!is.null(value$source)){
if (is.list(value$source)){
for (i in 1:(length(value$source)-1)){
Expand Down Expand Up @@ -108,27 +108,45 @@ getMetadata <- function(x, type=NULL) {
}
M <- value
}else if (type=="unit"){
if (length(value)<=1) M[[type]] <- value
if (is.character(value) & length(value)==1) M[[type]] <- value
else if (is.null(value)) M$unit <- '1'
else warning(value," is an invalid argument for unit!")
}else if (type=="source"){
if (is.null(value) || is.list(value)) M[[type]] <- value
else warning("Source field must be a list! Please include at least author, title, date, and journal. DOI, ISSN, URL, etc are also encouraged")
if (is.null(value)) M[[type]] <- value
else if (is.list(value)){
for (i in 1:(length(value)-1)){
if (is.list(value[[i]])){
if (!is.null(value[[i+1]]) & !is.list(value[[i+1]])){
warning("Source [",i+1,"] is not a list! Please include at least author, title, date, and journal. Also DOI, ISSN, URL, etc")
value[[i+1]] <- NULL
}else{
M$source[[i]] <- value[[i]]
M$source[[i+1]] <- value[[i+1]]
}
}else if (!is.null(value[[i]]) & is.list(value[[i+1]])){
warning("Source [",i,"] is not a list! Please include at least author, title, date, and journal. Also DOI, ISSN, URL, etc")
value[[i]] <- NULL
}
}
}else{
warning("Source must be a formatted as a list! Please include at least author, title, date, and journal. Also DOI, ISSN, URL, etc")
}
}else if (type == "calcHistory"){
if (is.character(value)){
if (is.list(M$calcHistory)) M$calcHistory[[length(M$calcHistory)]] <- append(M$calcHistory[[length(M$calcHistory)]],value)
else if (is.null(M[[type]])) M[[type]] <- value
else M[[type]] <- list(M[[type]],value)
}else if (is.null(value)) M[[type]] <- value
else warning(value," is an invalid argument for calcHistory! Please use getMetadata.R to provide the most recent function executed on, ",x)
else warning(value," is an invalid argument for calcHistory! Please use getMetadata.R to append the most recent function executed to calcHistory.")
}else if (type=="date"){
if ((is.character(value) & length(value)==1)) M[[type]] <- value
else warning(value," is an invalid argument for date! Please use getMetadata.R or updateMetadata.R to provide a date for ",x)
else warning(value," is an invalid argument for date! Please use getMetadata.R or updateMetadata.R to provide a date.")
}else if (type=="user"){
if ((is.character(value) & length(value)==1)) M[[type]] <- value
else warning(value," is an invalid argument for user! Please use getMetadata.R or updateMetadata.R to provide a user for ",x)
else warning(value," is an invalid argument for user! Please use getMetadata.R or updateMetadata.R to provide a user")
}else if (type=="description"){
if(is.null(value) || is.character(value)) M[[type]] <- value
else warning(value," is an invalid argument for description! Please use getMetadata.R to provide a description for ",x)
else warning(value," is an invalid argument for description! Please use getMetadata.R to provide a description")
}else warning(type," is not a valid metadata field!")
attr(x, "Metadata") <- M
return(x)
Expand Down
4 changes: 2 additions & 2 deletions R/updateMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ updateMetadata <- function(x, y=NULL, unit="keep", source="keep", calcHistory="k
if (!is.null(y)) Mx$unit <- My$unit
else warning("Units cannot be copied without a second magpie argument provided!")
}else if (unit=="clear") Mx$unit <- NULL
else if (unit=="update") warning("Update is an invalid argument for unit!")
else if (unit!="keep") if(length(unit)==1) Mx$unit <- unit else warning("Invalid argument ",unit," for unit!")
else if (unit=="update") warning("Update is an invalid argument for unit! Valid arguments include: copy, clear, keep, or a desired unit character")
else if (unit!="keep") Mx$unit <- unit

if (source=="copy"){
if (!is.null(y)){
Expand Down

0 comments on commit e299377

Please sign in to comment.