From 3b872436d47451a94fc4f171fabecf5a105a43a2 Mon Sep 17 00:00:00 2001 From: Falk Benke Date: Mon, 21 Oct 2024 12:28:32 +0200 Subject: [PATCH] handle differing years in modifyInvestmentVariables --- R/convGDX2MIF_LCOE.R | 2 +- R/modifyInvestmentVariables.R | 5 ++--- R/reportCapacity.R | 10 ++++++---- R/reportEnergyInvestment.R | 4 ++-- man/modifyInvestmentVariables.Rd | 6 +++++- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/R/convGDX2MIF_LCOE.R b/R/convGDX2MIF_LCOE.R index b238bf81..86da3487 100644 --- a/R/convGDX2MIF_LCOE.R +++ b/R/convGDX2MIF_LCOE.R @@ -27,7 +27,7 @@ convGDX2MIF_LCOE <- function(gdx, gdx_ref, file = NULL, scenario = "default", t = c(seq(2005, 2060, 5), seq(2070, 2110, 10), 2130, 2150)) { # make the reporting output <- NULL - output <- mbind(output, reportLCOE(gdx = gdx, gdx_ref = gdx_ref)[, t, ],) + output <- mbind(output, reportLCOE(gdx = gdx, gdx_ref = gdx_ref)[, t, ]) # write the LCOE.mif or give back the magpie object output if (!is.null(file)) { diff --git a/R/modifyInvestmentVariables.R b/R/modifyInvestmentVariables.R index d68066f9..779ef577 100644 --- a/R/modifyInvestmentVariables.R +++ b/R/modifyInvestmentVariables.R @@ -26,12 +26,11 @@ modifyInvestmentVariables <- function(x, ref = NULL, startYear = NULL) { x <- (x + tmp) / 2 if (!is.null(ref)) { - fixedYears <- getYears(x)[getYears(x, as.integer = TRUE) < startYear] - + joinedYears <- intersect(getYears(x, as.integer = TRUE), getYears(ref, as.integer = TRUE)) + fixedYears <- joinedYears[joinedYears < startYear] if (length(fixedYears) == 0) { return(x) } - ref <- modifyInvestmentVariables(ref) joinedNames <- intersect(getNames(x), getNames(ref)) joinedRegions <- intersect(getItems(ref, dim = 1), getItems(x, dim = 1)) diff --git a/R/reportCapacity.R b/R/reportCapacity.R index 02cb00e7..0f8faa97 100644 --- a/R/reportCapacity.R +++ b/R/reportCapacity.R @@ -49,15 +49,18 @@ reportCapacity <- function(gdx, regionSubsetList = NULL, # data preparation ttot <- as.numeric(as.vector(ttot)) - vm_cap <- vm_cap[teall2rlf] - vm_cap <- vm_cap[, ttot, ] + vm_cap <- vm_cap[teall2rlf] + vm_cap <- vm_cap[, ttot, ] + vm_deltaCap <- vm_deltaCap[teall2rlf] vm_deltaCap <- vm_deltaCap[, ttot, ] if (!is.null(gdx_ref)) { cm_startyear <- as.integer(readGDX(gdx, name = "cm_startyear", format = "simplest")) vm_deltaCapRef <- readGDX(gdx_ref, name = c("vm_deltaCap"), field = "l", format = "first_found") * 1000 - vm_deltaCap <- modifyInvestmentVariables(vm_deltaCap, vm_deltaCapRef, gdx_ref) + vm_deltaCapRef <- vm_deltaCapRef[teall2rlf] + vm_deltaCapRef <- vm_deltaCapRef[, ttot, ] + vm_deltaCap <- modifyInvestmentVariables(vm_deltaCap, vm_deltaCapRef, cm_startyear) } else { vm_deltaCap <- modifyInvestmentVariables(vm_deltaCap) } @@ -168,7 +171,6 @@ reportCapacity <- function(gdx, regionSubsetList = NULL, if ("dac" %in% magclass::getNames(vm_cap, dim = 1)) { tmp <- mbind(tmp, setNames(dimSums(vm_cap[, , c("dac")], dim = 3) * sm_c_2_co2, "Cap|Carbon Management|DAC (Mt CO2/yr)")) } - # Newly built capacities electricity (Should all go into tmp2, so that this can be used for calculating cumulated values in tmp5 below) tmp2 <- NULL tmp2 <- mbind(tmp2, setNames(dimSums(vm_deltaCap[, , c("ngcc", "ngt", "gaschp", "ngccc")], dim = 3), "New Cap|Electricity|Gas (GW/yr)")) diff --git a/R/reportEnergyInvestment.R b/R/reportEnergyInvestment.R index a537f483..edd5fa78 100644 --- a/R/reportEnergyInvestment.R +++ b/R/reportEnergyInvestment.R @@ -78,8 +78,8 @@ reportEnergyInvestment <- function(gdx, regionSubsetList = NULL, v_adjustteinv_ref <- readGDX(gdx_ref, name = c("v_costInvTeAdj", "vm_costInvTeAdj", "v_adjustteinv"), field = "l", format = "first_found") cm_startyear <- as.integer(readGDX(gdx, name = "cm_startyear", format = "simplest")) - v_directteinv <- modifyInvestmentVariables(v_directteinv[, ttot, ], v_directteinv_ref, cm_startyear) - v_adjustteinv <- modifyInvestmentVariables(v_adjustteinv[, ttot, ], v_adjustteinv_ref, cm_startyear) + v_directteinv <- modifyInvestmentVariables(v_directteinv[, ttot, ], v_directteinv_ref[, ttot, ], cm_startyear) + v_adjustteinv <- modifyInvestmentVariables(v_adjustteinv[, ttot, ], v_adjustteinv_ref[, ttot, ], cm_startyear) } else { v_directteinv <- modifyInvestmentVariables(v_directteinv[, ttot, ]) v_adjustteinv <- modifyInvestmentVariables(v_adjustteinv[, ttot, ]) diff --git a/man/modifyInvestmentVariables.Rd b/man/modifyInvestmentVariables.Rd index 0bac751f..d504ca31 100644 --- a/man/modifyInvestmentVariables.Rd +++ b/man/modifyInvestmentVariables.Rd @@ -4,10 +4,14 @@ \alias{modifyInvestmentVariables} \title{Modify Investment Variables} \usage{ -modifyInvestmentVariables(x) +modifyInvestmentVariables(x, ref = NULL, startYear = NULL) } \arguments{ \item{x}{a magclass object to be manipulated} + +\item{ref}{an optional magclass object to be used for fixing values before 'startYear'} + +\item{startYear}{years before will be overwritten with values from 'ref'} } \description{ a helper to adapt investment variables in reporting after changes in