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

Refactor calcCapital #608

Merged
merged 2 commits into from
Jan 9, 2025
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
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '40962870'
ValidationKey: '41196800'
AcceptedWarnings:
- Invalid URL: .*
- 'Warning: package ''.*'' was built under R version'
Expand All @@ -10,3 +10,4 @@ AutocreateReadme: yes
allowLinterWarnings: yes
enforceVersionUpdate: yes
skipCoverage: yes
AutocreateCITATION: yes
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: 'mrremind: MadRat REMIND Input Data Package'
version: 0.204.1
date-released: '2024-12-13'
version: 0.205.0
date-released: '2025-01-08'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
- family-names: Baumstark
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: mrremind
Title: MadRat REMIND Input Data Package
Version: 0.204.1
Date: 2024-12-13
Version: 0.205.0
Date: 2025-01-08
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(calcAGEB)
export(calcBP)
export(calcCCScapacity)
export(calcCapital)
export(calcEDGAR7Fgases)
export(calcEEAGHGProjections)
export(calcEmber)
Expand Down
128 changes: 56 additions & 72 deletions R/calcCapital.R
Original file line number Diff line number Diff line change
@@ -1,87 +1,71 @@
#' Calculate macroeconomic capital stock
#'
#' Compute macroeconomic capital stock based on capital intensities from PWT and GDP scenarios from [mrdrivers].
#' The PWT capital intensities are used up until 2010. After that, the capital intensities converge towards
#' that of Japan in 2010, at speeds that vary across scenarios. The final capital stocks are the product
#' of the capital intensities and the gdp scenarios from [mrdrivers].
#'
#' @export
#' @seealso \itemize{
#' \item See the vignette \code{vignette("scenarios", "mrdrivers")} for information on the GDP scenarios.
#' \item [readPWT()] for information on the PWT version used.
#' }
#' @inherit madrat::calcOutput return
calcCapital <- function() {

# compute macroeconomic capital stock based on capital intensities from PWT and ssp scenarios
# t.b.d.: correct for capital stock part that enters energy sectors
capital <- readSource("PWT")[, , "rkna"]
gdppwt <- readSource("PWT")[, , "rgdpna"]
getNames(capital) <- "kap"
cap_intensity <- capital / setNames(gdppwt, NULL)
# Get capital intensities (capital over GDP) from PWT
kPWT <- readSource("PWT")[, , "rkna"]
gdpPWT <- readSource("PWT")[, , "rgdpna"]
kIntPWT <- kPWT / setNames(gdpPWT, NULL)

# use initial gdp as in REMIND which differs from PWT
gdpppp_hist <- calcOutput("GDP", aggregate = FALSE, years = c("y1995", "y2000", "y2005"))
gdpppp <- calcOutput("GDP", aggregate = FALSE, years = seq(2005, 2150, 5))
my_scen <- c("gdp_SSP1", "gdp_SSP2", "gdp_SSP3", "gdp_SSP4", "gdp_SSP5", "gdp_SSP2EU",
"gdp_SDP", "gdp_SDP_EI", "gdp_SDP_RC", "gdp_SDP_MC")
gdpppp <- mselect(gdpppp, variable = my_scen)
# Get GDP from mrdrivers (which differs from GDP in PWT)
gdp <- calcOutput("GDP", naming = "scenario", aggregate = FALSE, years = seq(1995, 2150, 5))

p41 <- setYears(cap_intensity[, rep(1, 32), ], seq(1995, 2150, 5))
p41 <- add_dimension(p41, dim = 3.1, add = "ssp", nm = my_scen)
cap_intensity_future <- p41
convtime <- p41
gdp_weight <- p41
# Define reference capital intensity, and the convergence time in years, of the countries capital intensities towards
# that reference, for the different GDP scenarios. The convergence assumptions should follow the SSP narratives.
# Convergence starts after 2010.
kIntRef <- kIntPWT["JPN", 2010, ] %>% as.numeric()
convTime <- c("SSP1" = 150, "SSP2" = 250, "SSP3" = 500, "SSP4" = 300, "SSP5" = 150,
"SDP" = 150, "SDP_EI" = 150, "SDP_RC" = 150, "SDP_MC" = 150, "SSP2EU" = 250)

# ssp variation
convtime[, , "gdp_SSP1"] <- 150
convtime[, , "gdp_SSP2"] <- 250
convtime[, , "gdp_SSP3"] <- 500
convtime[, , "gdp_SSP4"] <- 300
convtime[, , "gdp_SSP5"] <- 150
convtime[, , "gdp_SDP"] <- 150
convtime[, , "gdp_SDP_EI"] <- 150
convtime[, , "gdp_SDP_RC"] <- 150
convtime[, , "gdp_SDP_MC"] <- 150
convtime[, , "gdp_SSP2EU"] <- 250

for (t in c("y1995", "y2000", "y2005")) {
cap_intensity_future[, t, ] <- cap_intensity[, t, ]
gdp_weight[, t, ] <- gdpppp_hist[, t, ]
}
cap_intensity_ref <- cap_intensity["JPN", "y2010"]
getRegions(cap_intensity_ref) <- "GLO"
lambda <- 0
for (t in getYears(gdpppp)) {
cap_intensity_future[, t, ] <- ((convtime[, t, ] - lambda) * collapseNames(setYears(cap_intensity[, "y2010", ])) +
lambda * setNames(setYears(cap_intensity_ref, NULL), NULL)) / convtime[, t, ]
lambda <- lambda + 5
gdp_weight[, t, ] <- gdpppp[, t, ]
}
cap_intensity_future[is.na(cap_intensity_future)] <- 0
cap_int_new <- cap_intensity_future
for (t in getYears(cap_intensity_future)) {
for (r in getRegions(cap_intensity_future)) {
if (cap_intensity_future[r, t, "gdp_SSP2"] == 0) {
# get current mapping
map <- toolGetMapping(type = "regional", name = getConfig("regionmapping"), where = "mappingfolder")
# get list of countries that belong to the same region as r
regi <- map$RegionCode[map$CountryCode == r]
c_regi <- map$CountryCode[map$RegionCode == regi]
# filter out the regions that are 0
c_regi <- c_regi[!cap_intensity_future[c_regi, t, "gdp_SSP2"] == 0]
# generate mapping for the aggregation
mapping <- map[which(map$CountryCode %in% c_regi), ]
mapping$RegionCode <- r
# store calculated data in separate file
cap_int_new[r, t, ] <- toolAggregate(cap_intensity_future[c_regi, t, ],
mapping,
weight = gdp_weight[c_regi, t, ])
}
# Create kInt magpie object with the same dimension as gdp, and assign the PWT capital intensities for the
# historic years until 2010.
kInt <- gdp * NaN
hy <- c(1995, 2000, 2005, 2010)
kInt[, hy, ] <- kIntPWT[, hy, ]
# For future years (after 2010), linearly converge towards kIntRef
fy <- setdiff(getYears(gdp, as.integer = TRUE), hy)
for (t in fy) {
for (s in getNames(kInt)) {
kInt[, t, s] <- kInt[, 2010, s] + (kIntRef - kInt[, 2010, s]) * (t - 2010) / convTime[s]
}
}

cap_future <- cap_int_new * gdp_weight * 1e-6
# Use regional average for countries missing data
## To get regional aggregate values for all countries: first aggregate, using only non-missing countries and
## partrel = TRUE, then disaggregate again.
nmc <- getItems(kInt, dim = 1)[!is.nan(kInt[, 2010, "SSP2"])]
map <- toolGetMapping("regionmappingH12.csv")
kIntReg <- toolAggregate(kInt[nmc, , ], rel = map, weight = gdp[nmc, , ], partrel = TRUE) %>%
toolAggregate(rel = map, from = "RegionCode", to = "CountryCode")
mc <- setdiff(getItems(kInt, dim = 1), nmc)
kInt[mc, , ] <- kIntReg[mc, , ]

# add industry subsectors energy efficiency capital stocks ----
# Compute capital stock
k <- kInt * gdp * 1e-6

kap <- cap_future %>%
`[`(, 2015, "gdp_SSP2EU.kap") %>%
quitte::magclass_to_tibble(c("iso3c", NA, NA, NA, "kap")) %>%
dplyr::select("iso3c", "kap")
# Add industry energy efficiency capital stocks (passing 2015 SSP2 capital stock as "kap")
kap <- k[, 2015, "SSP2"] %>% tibble::as_tibble() %>% dplyr::select("iso3c", "kap" = "value")
EEK <- calcOutput("Industry_EEK", kap = kap, aggregate = FALSE, years = getYears(k))

EEK <- calcOutput("Industry_EEK", kap = kap, supplementary = FALSE, aggregate = FALSE, years = getYears(cap_future))
# Modify names to match "all_demScen" in remind ("gdp_" prefix), and differentiate the macroeconomic capital "kap"
# from EEK capital stocks.
getNames(k) <- paste0("gdp_", getNames(k), ".kap")
getSets(k) <- c("iso3c", "year", "ssp", "variable")
x <- mbind(k, EEK)

# tie outputs together ----
list(x = mbind(cap_future, EEK),
list(x = x,
weight = NULL,
unit = "trillion US$2017",
description = "Capital stock, computed using the capital to GDP ratio from PWT, and GDP scenarios from mrdrivers.")
description = "Capital stock computed using the capital/GDP ratio from PWT, and GDP scenarios from mrdrivers.")
}
3 changes: 1 addition & 2 deletions R/readGlobalCCSinstitute.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#' @md
#' @param subtype Project Database version to read, one of
#' - `'08-09-2017'`: Data apparently from June 2017.
#' - `'2023-11'`: Data from the
#' [Global Status of CCS 2023](zotero://select/items/3_E5GNNPZ8) report.
#' - `'2023-11'`: Data from the Global Status of CCS 2023 report.
#'
#' @return A [`magpie`][magclass::magclass] object.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/readPWT.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Read PWT
#'
#' Read-in PWT data as magclass object
#' Read-in PWT data (version 8.0) as magclass object
#'
#' @order 1
#' @inherit madrat::readSource return
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRat REMIND Input Data Package

R package **mrremind**, version **0.204.1**
R package **mrremind**, version **0.205.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrremind)](https://cran.r-project.org/package=mrremind) [![R build status](https://github.com/pik-piam/mrremind/workflows/check/badge.svg)](https://github.com/pik-piam/mrremind/actions) [![codecov](https://codecov.io/gh/pik-piam/mrremind/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrremind) [![r-universe](https://pik-piam.r-universe.dev/badges/mrremind)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -39,16 +39,17 @@ In case of questions / problems please contact Lavinia Baumstark <lavinia@pik-po

To cite package **mrremind** in publications use:

Baumstark L, Rodrigues R, Levesque A, Oeser J, Bertram C, Mouratiadou I, Malik A, Schreyer F, Soergel B, Rottoli M, Mishra A, Dirnaichner A, Pehl M, Giannousakis A, Klein D, Strefler J, Feldhaus L, Brecha R, Rauner S, Dietrich J, Bi S, Benke F, Weigmann P, Richters O, Hasse R, Fuchs S, Mandaroux R, Koch J (2024). _mrremind: MadRat REMIND Input Data Package_. R package version 0.204.1, <https://github.com/pik-piam/mrremind>.
Baumstark L, Rodrigues R, Levesque A, Oeser J, Bertram C, Mouratiadou I, Malik A, Schreyer F, Soergel B, Rottoli M, Mishra A, Dirnaichner A, Pehl M, Giannousakis A, Klein D, Strefler J, Feldhaus L, Brecha R, Rauner S, Dietrich J, Bi S, Benke F, Weigmann P, Richters O, Hasse R, Fuchs S, Mandaroux R, Koch J (2025). "mrremind: MadRat REMIND Input Data Package." Version: 0.205.0, <https://github.com/pik-piam/mrremind>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
@Misc{,
title = {mrremind: MadRat REMIND Input Data Package},
author = {Lavinia Baumstark and Renato Rodrigues and Antoine Levesque and Julian Oeser and Christoph Bertram and Ioanna Mouratiadou and Aman Malik and Felix Schreyer and Bjoern Soergel and Marianna Rottoli and Abhijeet Mishra and Alois Dirnaichner and Michaja Pehl and Anastasis Giannousakis and David Klein and Jessica Strefler and Lukas Feldhaus and Regina Brecha and Sebastian Rauner and Jan Philipp Dietrich and Stephen Bi and Falk Benke and Pascal Weigmann and Oliver Richters and Robin Hasse and Sophie Fuchs and Rahel Mandaroux and Johannes Koch},
year = {2024},
note = {R package version 0.204.1},
date = {2025-01-08},
year = {2025},
url = {https://github.com/pik-piam/mrremind},
note = {Version: 0.205.0},
}
```
25 changes: 25 additions & 0 deletions man/calcCapital.Rd

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

3 changes: 1 addition & 2 deletions man/convertGlobalCCSinstitute.Rd

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

3 changes: 1 addition & 2 deletions man/readGlobalCCSinstitute.Rd

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

2 changes: 1 addition & 1 deletion man/readPWT.Rd

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

Loading