Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pik-piam/mrremind
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Feb 13, 2024
2 parents fb425b7 + 7ca633d commit 10e68d3
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '34720077'
ValidationKey: '34746870'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9025
rev: v0.4.0
hooks:
- id: parsable-R
- id: deps-in-desc
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: 'mrremind: MadRat REMIND Input Data Package'
version: 0.175.7
date-released: '2024-02-08'
version: 0.175.8
date-released: '2024-02-12'
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.175.7
Date: 2024-02-08
Version: 0.175.8
Date: 2024-02-12
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
20 changes: 8 additions & 12 deletions R/calcFE.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
#' @importFrom stats na.omit
#' @param source "IEA" or "IEA_WEO"
#' @param scenario_proj "SSP2" by default unless overwritten
#' @param ieaVersion Release version of IEA data, either 'default' (vetted and used in REMIND)
#' or 'latest'.


calcFE <- function(source = "IEA", scenario_proj = "SSP2") {
calcFE <- function(source = "IEA", scenario_proj = "SSP2", ieaVersion = "default") {
#------ READ-IN DATA----------------------------------------
if (source == "IEA") {
data <- calcOutput("IO", subtype = "output", aggregate = FALSE)

# remove period where only 0s appear
data <- data[, 2021, , invert = T]
data <- calcOutput("IO", subtype = "output", ieaVersion = ieaVersion,
aggregate = FALSE)

mapping <- toolGetMapping(type = "sectoral",
name = "structuremappingIO_reporting.csv",
Expand All @@ -28,7 +27,7 @@ calcFE <- function(source = "IEA", scenario_proj = "SSP2") {
colnames(map) <- gsub("io", "names_in", colnames(map))

# Give description
descript <- "IEA Final Energy Data based on 2017 version of IEA Energy Balances"
descript <- "IEA Final Energy Data based on 2022 version of IEA Energy Balances"

#------ PROCESS DATA ------------------------------------------
# select data that have names
Expand Down Expand Up @@ -129,7 +128,6 @@ calcFE <- function(source = "IEA", scenario_proj = "SSP2") {
} else if (source == "IEA_WEO") {
data <- readSource(type = "IEA_WEO", subtype = "FE")
regions <- toolGetMapping(getConfig()[1], where = "mappingfolder", type = "regional")
# regions <- unique(regions$RegionCode)

# gdp of all countries in 2015
gdp <- calcOutput("GDPPast", years = 2015, aggregate = FALSE)
Expand All @@ -140,7 +138,7 @@ calcFE <- function(source = "IEA", scenario_proj = "SSP2") {
var <- getNames(data)[1]
data_new <- new.magpie(getRegions(data), years = getYears(data), names = getNames(data), fill = NA)
for (i in regions$CountryCode) {
if (!is.na(data[i, "y2010", var]) & gdp[i, , ] > 0.9 * dimSums(gdp[regions[regions$RegionCode == regions[regions$CountryCode == i, ]$RegionCode, ]$CountryCode, , ], dim = 1)) {
if (!is.na(data[i, "y2010", var]) && gdp[i, , ] > 0.9 * dimSums(gdp[regions[regions$RegionCode == regions[regions$CountryCode == i, ]$RegionCode, ]$CountryCode, , ], dim = 1)) {
data_new[i, , ] <- data[i, , ]
countries <- regions[regions$RegionCode == regions[regions$CountryCode == i, ]$RegionCode, ]$CountryCode
data_new[setdiff(countries, i), , ] <- 0 # countries other than the "main" country
Expand All @@ -151,9 +149,7 @@ calcFE <- function(source = "IEA", scenario_proj = "SSP2") {
data <- data_new

data <- data[, , ] * 4.1868e-2 # Mtoe to EJ
# data <- collapseNames(data)
# vars <- c("Coal","Oil","Gas")
# data <- data[,,vars,pmatch=T]

# converting to remind convention
getNames(data) <- gsub(pattern = "Final Energy", replacement = "FE", x = getNames(data))
getNames(data) <- paste0(getNames(data), " (EJ/yr)")
Expand Down
4 changes: 2 additions & 2 deletions R/calcHistorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
calcHistorical <- function() {

# Final Energy
fe_iea <- calcOutput("FE", source = "IEA", aggregate = FALSE)
fe_iea <- calcOutput("FE", source = "IEA", ieaVersion = "latest", aggregate = FALSE)
fe_iea <- add_dimension(fe_iea, dim = 3.1, add = "model", nm = "IEA")

fe_weo <- calcOutput("FE", source = "IEA_WEO", aggregate = FALSE)
Expand All @@ -15,7 +15,7 @@ calcHistorical <- function() {
fe_weo <- add_dimension(fe_weo, dim = 3.1, add = "model", nm = "IEA_WEO")

# Primary Energy
pe_iea <- calcOutput("PE", subtype = "IEA", aggregate = FALSE)
pe_iea <- calcOutput("PE", subtype = "IEA", ieaVersion = "latest", aggregate = FALSE)
pe_iea <- add_dimension(pe_iea, dim = 3.1, add = "model", nm = "IEA")

pe_weo <- calcOutput("PE", subtype = "IEA_WEO", aggregate = FALSE)
Expand Down
52 changes: 32 additions & 20 deletions R/calcIO.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#'
#' @md
#' @param subtype Data subtype. See default argument for possible values.
#' @param ieaVersion Release version of IEA data, either 'default'
#' (vetted and used in REMIND) or 'latest'.
#' @return IEA data as MAgPIE object aggregated to country level
#' @author Anastasis Giannousakis
#' @seealso \code{\link{calcOutput}}
Expand All @@ -24,7 +26,9 @@
#' @importFrom tidyr unite
#' @importFrom tidyselect all_of
calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
"input_Industry_subsectors", "output_Industry_subsectors", "IEA_output", "IEA_input")) {
"input_Industry_subsectors", "output_Industry_subsectors",
"IEA_output", "IEA_input"),
ieaVersion = "default") {
subtype <- match.arg(subtype)
switch(
subtype,
Expand Down Expand Up @@ -74,8 +78,14 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
mapping <- toolGetMapping(type = "sectoral", name = mapping, where = where,
returnPathOnly = TRUE)

if (!(ieaVersion %in% c("default", "latest"))) {
stop("Invalid parameter `ieaVersion`. Must be either 'default' or 'latest'")
}

ieaSubtype <- if (ieaVersion == "default") "EnergyBalances" else "EnergyBalances-latest"

# read in data and convert from ktoe to EJ
data <- readSource("IEA", subtype = "EnergyBalances") * 4.1868e-5
data <- readSource("IEA", subtype = ieaSubtype) * 4.1868e-5

ieamatch <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "")

Expand All @@ -94,29 +104,31 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
threshold = 1e-2)
# warn if dimensions not present in the mapping have been added to the data
new_product_flows <- tibble(
text = setdiff(getNames(data), names_data_before)) %>%
separate('text', c('product', 'flow'), sep = '\\.') %>%
text = setdiff(getNames(data), names_data_before)
) %>%
separate("text", c("product", "flow"), sep = "\\.") %>%
anti_join(
ieamatch %>%
as_tibble() %>%
select(product = 'iea_product', flow = 'iea_flows'),
select(product = "iea_product", flow = "iea_flows"),

c('product', 'flow')
c("product", "flow")
) %>%
unite('text', c('product', 'flow'), sep = '.') %>%
pull('text')
unite("text", c("product", "flow"), sep = ".") %>%
pull("text")

if (!is_empty(new_product_flows)) {
warning('Product/flow combinations not present in mapping added by ',
'fix_IEA_data_for_Industry_subsectors():\n',
paste(new_product_flows, collapse = '\n'))
warning("Product/flow combinations not present in mapping added by ",
"fix_IEA_data_for_Industry_subsectors():\n",
paste(new_product_flows, collapse = "\n")
)
}

# FIXME remove product/flow combinations from the mapping that have been
# removed from the data while replacing coke oven and blast furnace outputs
ieamatch <- ieamatch %>%
as_tibble() %>%
filter(paste(.data$iea_product, .data$iea_flows, sep = '.')
filter(paste(.data$iea_product, .data$iea_flows, sep = ".")
%in% getNames(data))
}

Expand All @@ -126,7 +138,7 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
select(all_of(c("iea_product", "iea_flows", "Weight", target))) %>%
na.omit() %>%
unite("target", all_of(target), sep = ".", remove = FALSE) %>%
unite('product.flow', c('iea_product', 'iea_flows'),sep = '.') %>%
unite("product.flow", c("iea_product", "iea_flows"), sep = ".") %>%
filter(!!sym("product.flow") %in% getNames(data))
magpieNames <- ieamatch[["target"]] %>% unique()

Expand All @@ -144,29 +156,29 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
function(item) {
product_flow <- ieamatch %>%
filter(item == .data$target) %>%
pull('product.flow')
pull("product.flow")

weights <- ieamatch %>%
filter(item == .data$target) %>%
pull('Weight') %>%
pull("Weight") %>%
as.numeric()

tmp <- dimSums( data[,,product_flow]
tmp <- dimSums(data[, , product_flow]
* setNames(as.magpie(weights), product_flow),
dim = 3, na.rm = TRUE)
getNames(tmp) <- item

return(tmp)
})
})
)

# Split residential Biomass into traditional and modern biomass depending upon the income per capita
if (subtype %in% c("output", "input", "output_Industry_subsectors")) {
# In order to split the REMIND technology biotr between biotr and biotrmod,
# We use the traditional biomass split for EDGE buildings and divide by the total quantity of FE biomass

edgeBio <- calcOutput("IOEdgeBuildings", subtype = "output_EDGE_buildings", aggregate = FALSE)
feBio <- calcOutput("IO", subtype = "output_biomass", aggregate = FALSE)
edgeBio <- calcOutput("IOEdgeBuildings", subtype = "output_EDGE_buildings", ieaVersion = ieaVersion, aggregate = FALSE)
feBio <- calcOutput("IO", subtype = "output_biomass", ieaVersion = ieaVersion, aggregate = FALSE)
shareBiotrad <- edgeBio[, , "biotrad"] / (feBio[, , "sesobio.fesob.tdbiosob"] + feBio[, , "sesobio.fesoi.tdbiosoi"])
shareBiotrad[is.na(shareBiotrad)] <- 0
reminditems <- mbind(reminditems,
Expand Down Expand Up @@ -225,5 +237,5 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade",
}

return(list(x = reminditems, weight = NULL, unit = "EJ",
description = "IEA SE Output Data based on 2017 edition of IEA World Energy Balances"))
description = "IEA SE Output Data based on 2022 edition of IEA World Energy Balances"))
}
Loading

0 comments on commit 10e68d3

Please sign in to comment.