Skip to content

Commit

Permalink
Refactor in accordance with mrdrivers update
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoch94 committed Nov 6, 2024
1 parent 8c6c708 commit 6789790
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 64 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ importFrom(madrat,toolGetMapping)
importFrom(madrat,toolNAreplace)
importFrom(madrat,toolSplitSubtype)
importFrom(madrat,toolSubtypeSelect)
importFrom(madrat,vcat)
importFrom(magclass,"getCells<-")
importFrom(magclass,"getComment<-")
importFrom(magclass,"getItems<-")
importFrom(magclass,"getNames<-")
importFrom(magclass,"getRegions<-")
importFrom(magclass,"getSets<-")
importFrom(magclass,"getYears<-")
importFrom(magclass,add_columns)
importFrom(magclass,add_dimension)
Expand Down
21 changes: 8 additions & 13 deletions R/calcValidIncome.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
#' Returns historical development of income and future projections of income dynamics
#'
#'
#' @param datasource To switch between historical values of James et al (default) or
#' with future projections for different scenarios (James_OECD_Nakicenovic)
#' @param datasource "WDI_SSPs", the only one option at the moment.
#' @return list of magpie object with data and weight. Since intensive and extensive variables
#' are mixed please keep the mixed_aggregation
#' @importFrom madrat vcat calcOutput
#' @importFrom magclass mbind add_dimension getSets getSets<- collapseNames getYears
#' @author Florian Humpenoeder, Abhijeet Mishra, Kristine Karstens
calcValidIncome <- function(datasource = "WDI_SSPs") {

calcValidIncome <- function(datasource = "WDI-MI_SSPs-MI") {

if (datasource == "WDI-MI_SSPs-MI") {
if (datasource == "WDI_SSPs") {

.tmp <- function(x, nm) {
getSets(x)[3] <- "scenario"
Expand Down Expand Up @@ -64,10 +60,9 @@ calcValidIncome <- function(datasource = "WDI-MI_SSPs-MI") {
stop("Given datasource currently not supported!")
}

return(list(x = out,
weight = weight,
mixed_aggregation = TRUE,
unit = "(million) US Dollar 2017 equivalents in MER/yr, MER/cap/yr, PPP/yr, PPP/cap/yr",
description = "Income")
)
list(x = out,
weight = weight,
mixed_aggregation = TRUE,
unit = "(million) US Dollar 2017 equivalents in MER/yr, MER/cap/yr, PPP/yr, PPP/cap/yr",
description = "Income")
}
56 changes: 17 additions & 39 deletions R/calcValidPopulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,31 @@
#'
#' Returns historical development of population and future projections of population dynamics
#'
#'
#' @param PopulationPast population past data source
#' @param PopulationFuture population future data source
#' @param TimeFromFindSet boolean deciding something
#' @param pastData population past data source
#' @param futureData population future data source
#' @return list of magpie object with data and weight
#' @author Florian Humpenoeder, Jan Philipp Dietrich, David Chen
#' @importFrom magclass getRegions
#' @import mrdrivers
calcValidPopulation <- function(PopulationPast = "WDI-UN_PopDiv-MI",
PopulationFuture = c("SSPs-UN_PopDiv-MI",
"SDPs-UN_PopDiv-MI",
"SSP2EU-UN_PopDiv-MI"),
TimeFromFindSet = TRUE) {

past <- calcOutput("PopulationPast",
PopulationPast = PopulationPast,
aggregate = FALSE)
getNames(past) <- paste("historical", paste0(PopulationPast), "Population (million people)", sep = ".")
getSets(past)[3] <- "scenario.model.variable"
calcValidPopulation <- function(pastData = "WDI-UN_PopDiv-MI", futureData = "SSPs-UN_PopDiv") {

future <- NULL
for (i in 1:length(PopulationFuture)) {
tmp <- calcOutput("PopulationFuture",
PopulationFuture = PopulationFuture[i],
extension2150 = "bezier",
aggregate = FALSE)
future <- mbind(future, tmp)
}
past <- calcOutput("PopulationPast", pastData = pastData, aggregate = FALSE)
future <- calcOutput("PopulationFuture", futureData = futureData, aggregate = FALSE)


getNames(future) <- paste(sub("^pop_", "", getNames(future)),
paste0(PopulationFuture, collapse = "_and_"),
"Population (million people)",
sep = ".")
getNames(past) <- paste("historical", pastData, "Population (million people)", sep = ".")
getNames(future) <- paste(getNames(future), futureData, "Population (million people)", sep = ".")
getSets(past)[3] <- "scenario.model.variable"
getSets(future)[3] <- "scenario.model.variable"

out <- new.magpie(getRegions(past),
union(getYears(past), getYears(future)),
c(getNames(past), getNames(future)),
out <- new.magpie(cells_and_regions = getItems(past, 1),
years = union(getYears(past), getYears(future)),
names = c(getNames(past), getNames(future)),
sets = c("region", "year", "scenario.model.variable"),
fill = NA)
out[, getYears(past), getNames(past)] <- past
out[, getYears(future), getNames(future)] <- future

getSets(out, fulldim = FALSE)[3] <- "scenario.model.variable"
return(list(x = out, weight = NULL, unit = "million",
description = paste0("Population data. Datasource for the Past: ",
PopulationPast,
". Datasource for the Future: ",
paste0(PopulationFuture, collapse = ", "))))
list(x = out,
weight = NULL,
unit = "million",
description = paste0("Population data. Datasource for the Past: ", pastData, ". Datasource for the Future: ",
futureData))
}
5 changes: 2 additions & 3 deletions man/calcValidIncome.Rd

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

11 changes: 4 additions & 7 deletions man/calcValidPopulation.Rd

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

0 comments on commit 6789790

Please sign in to comment.