From e29937724dc26889ddfd894dfb0da9f30d0b7c2e Mon Sep 17 00:00:00 2001 From: Stephen Bi Date: Tue, 16 Jan 2018 13:44:44 +0100 Subject: [PATCH] Minor revisions to metadata handling --- DESCRIPTION | 2 +- R/getMetadata.R | 36 +++++++++++++++++++++++++++--------- R/updateMetadata.R | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f05dad4c..2f58eee3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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, diff --git a/R/getMetadata.R b/R/getMetadata.R index ab6fc4eb..c3a400ff 100644 --- a/R/getMetadata.R +++ b/R/getMetadata.R @@ -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)){ @@ -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) diff --git a/R/updateMetadata.R b/R/updateMetadata.R index 035a610c..55cd83dd 100644 --- a/R/updateMetadata.R +++ b/R/updateMetadata.R @@ -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)){