getData_raw<-function(collections,level="glo",historical=NULL,datasources=NULL, query=NULL){
###############################################################
#Check if data for the specified collection sis available
###############################################################
tmp<-collections
for(i in 1:length(collections)){
if(!collections[i] %in% names(valdata$data)){
tmp[i]<-NA
}
}
tmp<-tmp[which(!is.na(tmp))]
if(length(tmp)==0) stop("No data for any of the collections ",paste(collections)," found.")
collections<-tmp
###############################################################
#Determine what data_levels can be used for the level specified in the add_data call
###############################################################
levels<-c("cell","country","reg","glo")
data_levels<-levels[1:grep(level,levels)]
if(length(data_levels)==0) stop("No appropriate data for level=",level,"found.")
#Rename func_level for groupAggregate
if(level=="country") level<-"iso"
###############################################################
#Check if historical data, projections or both are requested
###############################################################
if(is.null(historical)){
types<-c("historical","projection")
} else if(historical==TRUE){
types<-"historical"
} else if(historical==FALSE){
types<-"projection"
} else {
stop("'historical argument must be either TRUE, FALSE or NULL.")
}
#Create an output object
out<-list()
###############################################################
#Get the requested data, aggregate it to the desired level and save it in the out list
###############################################################
for(collection in collections){
out[[collection]]<-list()
out[[collection]][["unit"]]<-valdata$data[[collection]][["unit"]]
out[[collection]][["data"]]<-list()
for(data_level in data_levels){
if(!is.null(valdata$data[[collection]][["data"]][[data_level]][["data"]])){
for(type in types){
if(!is.null(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]])){
if(is.null(out[[collection]][["data"]][[type]])) out[[collection]][["data"]][[type]]<-list()
if(is.null(datasources)) {
sources<-names(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]])
} else {
sources<-datasources
}
for(sourc in sources){
if(!is.null(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]])){
message(sourc," DATA for ",collection," found")
comment<-attr(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]][["data"]],"comment")
if(data_level=="cell" & !is.null(query)){
#convert query (if provided) from country2reg to cell2reg
cell <- valdata$other$iso_cell
a <- merge(query,cell,by.x="iso",by.y="country")
a <- a[order(a$magpiecell),]
a$iso <- paste(a$iso,a$magpiecell,sep=".")
a$magpiecell <- NULL
query_final <- a
getCells(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]][["data"]]) <- query_final$iso
} else {
query_final <- query
}
#Define all the arguments for groupAggregate in a list
args<-list(data=clean_magpie(valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]][["data"]]),
vectorfunction=valdata$aggr_types[[valdata$data[[collection]][["aggr_type"]]]][["func"]],
dim=1,
from=NULL,
to=level,
query=query_final)
for(i in valdata$aggr_types[[valdata$data[[collection]][["aggr_type"]]]][["add_args"]]){
args[[i]]<-valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]][[i]]
if(is.magpie(args[[i]])) args[[i]] <- clean_magpie(args[[i]])
}
if(level!="cell") {
tmp<-do.call(groupAggregate,args)
} else {
tmp<-valdata$data[[collection]][["data"]][[data_level]][["data"]][[type]][[sourc]][["data"]]
}
tmp<-tmp[which(!getCells(tmp)%in%c("XXX.11","XXX")),,]
if(!is.null(out[[collection]][[type]][[sourc]])){
warning("More than one dataset is present for the combination: ",type," ",sourc,". Only last one will be returned.")
}
attr(tmp,"comment")<-comment
out[[collection]][["data"]][[type]][[sourc]]<-as.magpie(tmp)
}
}
}
}
}
}
if(is.null(out[[collection]][["data"]])) out[[collection]]<-NULL
}
if(length(out)==0) {
out<-NULL
warning("No Data found. NULL returned")
}
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.