Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numeric_version #43

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 20 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L19-L20

Added lines #L19 - L20 were not covered by tests
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")

Check warning on line 23 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L22-L23

Added lines #L22 - L23 were not covered by tests

# 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

Check warning on line 26 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L26

Added line #L26 was not covered by tests

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

Check warning on line 32 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L31-L32

Added lines #L31 - L32 were not covered by tests
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")

Check warning on line 35 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L34-L35

Added lines #L34 - L35 were not covered by tests

# 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

Check warning on line 38 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L38

Added line #L38 was not covered by tests

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

Check warning on line 41 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L40-L41

Added lines #L40 - L41 were not covered by tests

} 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")

Check warning on line 44 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L44

Added line #L44 was not covered by tests
# harmonize historic period
x[, c(1995, 2000, 2005, 2010), ] <- collapseNames(x[, c(1995, 2000, 2005, 2010), "SSP2-Ref-SPA0"])

Check warning on line 46 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L46

Added line #L46 was not covered by tests
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

Check warning on line 51 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L50-L51

Added lines #L50 - L51 were not covered by tests
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")

Check warning on line 57 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L57

Added line #L57 was not covered by tests

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

Check warning on line 60 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L60

Added line #L60 was not covered by tests

# 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)

Check warning on line 65 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L63-L65

Added lines #L63 - L65 were not covered by tests
# 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"])

Check warning on line 69 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L67-L69

Added lines #L67 - L69 were not covered by tests

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

Check warning on line 72 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L72

Added line #L72 was not covered by tests

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

Check warning on line 75 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L75

Added line #L75 was not covered by tests

# 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))

Check warning on line 80 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L78-L80

Added lines #L78 - L80 were not covered by tests

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

Check warning on line 84 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L82-L84

Added lines #L82 - L84 were not covered by tests

# 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")

Check warning on line 87 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L87

Added line #L87 was not covered by tests

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)

Check warning on line 95 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L92-L95

Added lines #L92 - L95 were not covered by tests
} else {
strefler <- NULL

Check warning on line 97 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L97

Added line #L97 was not covered by tests
}

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

Check warning on line 101 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L100-L101

Added lines #L100 - L101 were not covered by tests

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

Check warning on line 106 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L104-L106

Added lines #L104 - L106 were not covered by tests

# 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, ])

Check warning on line 110 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L110

Added line #L110 was not covered by tests

# 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))]

Check warning on line 113 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L113

Added line #L113 was not covered by tests

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

Check warning on line 116 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L115-L116

Added lines #L115 - L116 were not covered by tests

} else {
stop("Unknown datasource", datasource)

Check warning on line 119 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L119

Added line #L119 was not covered by tests
}
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)")))

Check warning on line 126 in R/calc2ndBioDem.R

View check run for this annotation

Codecov / codecov/patch

R/calc2ndBioDem.R#L122-L126

Added lines #L122 - L126 were not covered by tests
}
Loading
Loading