Skip to content

Commit

Permalink
Merge pull request #43 from pascal-sauer/master
Browse files Browse the repository at this point in the history
numeric_version
  • Loading branch information
pascal-sauer authored Feb 15, 2024
2 parents c58ad61 + dc3568b commit 0274298
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '10554510'
ValidationKey: '10575880'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrland: MadRaT land data package'
version: 0.53.4
date-released: '2024-02-12'
version: 0.53.5
date-released: '2024-02-15'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrland
Title: MadRaT land data package
Version: 0.53.4
Date: 2024-02-12
Version: 0.53.5
Date: 2024-02-15
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down
173 changes: 89 additions & 84 deletions R/calc2ndBioDem.R
Original file line number Diff line number Diff line change
@@ -1,122 +1,127 @@
#' @title calc2ndBioDem
#' @description calculates 2nd generation bioenergy demand
#' @return magpie object with results on country level, weight on country level, unit and description.
#'
#'
#' @param datasource source to be used
#' @param rev data revision the output will be produced for (positive numeric)
#'
#' @examples
#'
#' \dontrun{
#' @param rev data revision the output will be produced for (numeric_version)
#'
#' @examples
#' \dontrun{
#' calcOutput("2ndBioDem")
#' }
#' @import magclass
#' @importFrom madrat readSource calcOutput
#' @importFrom magclass collapseNames time_interpolate mbind lowpass

calc2ndBioDem <- function(datasource, rev = 0.1) {
calc2ndBioDem <- function(datasource, rev = numeric_version("0.1")) {

if (datasource == "REMIND") {
x <- readSource("REMIND", subtype = paste0("extensive_",rev))
x <- x[,,"Primary Energy Production|Biomass|Energy Crops (EJ/yr)"]*10^3
x <- readSource("REMIND", subtype = paste0("extensive_", rev))
x <- x[, , "Primary Energy Production|Biomass|Energy Crops (EJ/yr)"] * 10^3
x <- collapseNames(x)
first_remind_year <- sort(getYears(x))[1]
x <- time_interpolate(x,seq(1995,2150,5),extrapolation_type = "constant")
firstRemindYear <- sort(getYears(x))[1]
x <- time_interpolate(x, seq(1995, 2150, 5), extrapolation_type = "constant")

# set values in initial years that are not existing in REMIND data to zero
x[,getYears(x)<first_remind_year,]<-0
x[, getYears(x) < firstRemindYear, ] <- 0

description <- "2nd generation bioenergy demand for different scenarios taken from R2M41 coupled runs"

} else if (datasource == "Strefler2021") {
x <- readSource("Strefler2021", subtype = paste0("extensive_",rev))
x <- x[,,"Primary Energy Production|Biomass|Energy Crops (EJ/yr)"]*10^3
x <- readSource("Strefler2021", subtype = paste0("extensive_", rev))
x <- x[, , "Primary Energy Production|Biomass|Energy Crops (EJ/yr)"] * 10^3
x <- collapseNames(x)
first_remind_year <- sort(getYears(x))[1]
x <- time_interpolate(x,seq(1995,2150,5),extrapolation_type = "constant")
firstRemindYear <- sort(getYears(x))[1]
x <- time_interpolate(x, seq(1995, 2150, 5), extrapolation_type = "constant")

# set values in initial years that are not existing in REMIND data to zero
x[,getYears(x)<first_remind_year,]<-0

description <- "2nd generation bioenergy demand for different scenarios taken from Strefler et al 2021 (DOI 10.1038/s41467-021-22211-2)"

x[, getYears(x) < firstRemindYear, ] <- 0

description <- paste("2nd generation bioenergy demand for different scenarios taken",
"from Strefler et al 2021 (DOI 10.1038/s41467-021-22211-2)")

} else if (datasource == "REMMAG") {
x <- readSource("REMMAG","biodem")
#harmonize historic period
x[,c(1995,2000,2005,2010),] <- collapseNames(x[,c(1995,2000,2005,2010),"SSP2-Ref-SPA0"])
x <- readSource("REMMAG", "biodem")
# harmonize historic period
x[, c(1995, 2000, 2005, 2010), ] <- collapseNames(x[, c(1995, 2000, 2005, 2010), "SSP2-Ref-SPA0"])
description <- "2nd generation bioenergy demand for different scenarios taken from R17M3 coupled runs"

} else if (datasource == "SSPResults") {
x<-readSource("SSPResults")
x<- collapseNames(x[,,"Primary Energy|Biomass|Energy Crops (EJ/yr)"])*10^3
x <- readSource("SSPResults")
x <- collapseNames(x[, , "Primary Energy|Biomass|Energy Crops (EJ/yr)"]) * 10^3
description <- "2nd generation bioenergy demand for different scenarios taken from IIASA SSP database"

} else if (datasource == "S4N_project") {
# Total bioenergy demand (including 1st genation, 2nd generation and residues) at country level from IMAGE for 2 different SSP2 scenarios starting in 2005 (in EJ per year)
image_be <- readSource("S4Nproject_input", subtype="bioenergy", convert="onlycorrect")
# Total bioenergy demand (including 1st genation, 2nd generation and residues)
# at country level from IMAGE for 2 different SSP2 scenarios starting in 2005 (in EJ per year)
imageBe <- readSource("S4Nproject_input", subtype = "bioenergy", convert = "onlycorrect")

# Transform units: from EJ to PJ
image_be <- image_be*1e3
imageBe <- imageBe * 1e3

# 1st gen BE demand in MAgPIE in scenario selected for Sim4Nexus (in PJ/yr)
BE_1st <- calcOutput("1stBioDem", years=seq(2005, 2100,by=5), aggregate=FALSE)
BE_1st <- collapseNames(BE_1st[,,"const2030"])
BE_1st <- dimSums(BE_1st, dim=3)
be1st <- calcOutput("1stBioDem", years = seq(2005, 2100, by = 5), aggregate = FALSE)
be1st <- collapseNames(be1st[, , "const2030"])
be1st <- dimSums(be1st, dim = 3)
# 2nd gen residues in MAgPIE in scenario selected for Sim4Nexus (in PJ/yr)
res <- calcOutput("ResFor2ndBioengery", products="kres", product_aggr=TRUE, add_off=TRUE, years=seq(2005, 2100,by=5), aggregate=FALSE)
res <- collapseNames(res[,,"ssp2"])

res <- calcOutput("ResFor2ndBioengery", products = "kres", product_aggr = TRUE,
add_off = TRUE, years = seq(2005, 2100, by = 5), aggregate = FALSE)
res <- collapseNames(res[, , "ssp2"])

# 2nd generation bioenergy demand: Total BE (IMAGE) - 1st BE (MAgPIE) - residues (MAgPIE)
image_be <- image_be - BE_1st - res
imageBe <- imageBe - be1st - res

# Correct negative values
image_be[image_be<0] <- 0
imageBe[imageBe < 0] <- 0

# Fill missing years (1995, 2000) with 2nd generation bioenergy demand from REMMAG data
remmag_be <- readSource("REMMAG", subtype="biodem")
remmag_be <- collapseNames(remmag_be[,c("y1995","y2000"),"SSP2-26-SPA2"])
x <- new.magpie(getCells(image_be), paste0("y",seq(1995, 2100,by=5)), getNames(image_be))
x[,getYears(remmag_be),"SSP2"] <- remmag_be
x[,getYears(remmag_be),"SSP2_SPA2_26I_D"] <- remmag_be
x[,getYears(image_be),] <- image_be
remmagBE <- readSource("REMMAG", subtype = "biodem")
remmagBE <- collapseNames(remmagBE[, c("y1995", "y2000"), "SSP2-26-SPA2"])
x <- new.magpie(getCells(imageBe), paste0("y", seq(1995, 2100, by = 5)), getNames(imageBe))

x[, getYears(remmagBE), "SSP2"] <- remmagBE
x[, getYears(remmagBE), "SSP2_SPA2_26I_D"] <- remmagBE
x[, getYears(imageBe), ] <- imageBe

# fill missing years in the future
x <- time_interpolate(x, seq(1995,2150,5), extrapolation_type="constant")
x <- time_interpolate(x, seq(1995, 2150, 5), extrapolation_type = "constant")

description <- "2nd generation bioenergy demand for different scenarios provided by IMAGE"

} else if (datasource == "SSP_and_REM") {
ssp <- calcOutput("2ndBioDem",datasource="SSPResults",aggregate = FALSE, rev = rev)
rem <- calcOutput("2ndBioDem",datasource="REMIND",aggregate = FALSE, rev = rev)
if (rev > 4.58) {
strefler <- calcOutput("2ndBioDem",datasource="Strefler2021",aggregate = FALSE, rev = rev)
} else strefler <- NULL

ssp <- time_interpolate(ssp,getYears(rem),extrapolation_type = "constant")
x <- mbind(ssp,rem,strefler)

#years
years <- getYears(x,as.integer = T)
yr_hist <- years[years > 1995 & years <= 2020]
yr_fut <- years[years >= 2020]

#apply lowpass filter (not applied on 1st time step, applied separately on historic and future period)
ssp <- calcOutput("2ndBioDem", datasource = "SSPResults", aggregate = FALSE, rev = rev)
rem <- calcOutput("2ndBioDem", datasource = "REMIND", aggregate = FALSE, rev = rev)
if (rev > numeric_version("4.58")) {
strefler <- calcOutput("2ndBioDem", datasource = "Strefler2021", aggregate = FALSE, rev = rev)
} else {
strefler <- NULL
}

ssp <- time_interpolate(ssp, getYears(rem), extrapolation_type = "constant")
x <- mbind(ssp, rem, strefler)

# years
years <- getYears(x, as.integer = TRUE)
yrHist <- years[years > 1995 & years <= 2020]
yrFut <- years[years >= 2020]

# apply lowpass filter (not applied on 1st time step, applied separately on historic and future period)
iter <- 3
x <- mbind(x[,1995,],lowpass(x[,yr_hist,],i=iter),lowpass(x[,yr_fut,],i=iter)[,-1,])
x <- mbind(x[, 1995, ], lowpass(x[, yrHist, ], i = iter), lowpass(x[, yrFut, ], i = iter)[, -1, ])

# sort scenarios alphabetically
x <- x[,,sort(getNames(x))]

description <- "2nd generation bioenergy demand for different scenarios taken from R2M41 coupled runs and from IIASA SSP database"

} else {
stop("Unknown datasource",datasource)
x <- x[, , sort(getNames(x))]

description <- paste("2nd generation bioenergy demand for different scenarios",
"taken from R2M41 coupled runs and from IIASA SSP database")

} else {
stop("Unknown datasource", datasource)
}
return(list(x=x, weight=NULL,
description=description,
unit="PJ per year",
note="bioenergy is demanded in the country which is expected to produce the bioenergy (demand after trade)"))


return(list(x = x, weight = NULL,
description = description,
unit = "PJ per year",
note = paste("bioenergy is demanded in the country which is expected",
"to produce the bioenergy (demand after trade)")))
}
Loading

0 comments on commit 0274298

Please sign in to comment.