From 5d2a29cb16d7f081f98ec121371d28a6e8c5c860 Mon Sep 17 00:00:00 2001 From: Johannes Koch Date: Wed, 29 Jan 2025 22:02:03 +0100 Subject: [PATCH] Refactor fullRemind in line with new gdp and pop GAMS sets Add SSP2IndiaDEA scenarios to mrdriver calls in fullREMIND and remove SSP2EU scenario. Move readIEA_WEIO_2014 to mrindustry, fixes #624. Add scenario specification for all calcGDP, calcGDPpc and calcPopulation calls, in anticipation of mrdrivers changes. Clean up NAMESPACE file by removing importFrom statements and adding :: calls for select functions. --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 6 +- NAMESPACE | 77 +---------- R/calcAGEB.R | 3 +- R/calcBP.R | 4 +- R/calcBiomassPrices.R | 12 +- R/calcCapacity.R | 5 +- R/calcCapacityFactorHist.R | 125 +++++++++--------- R/calcCapital.R | 15 ++- R/calcCostsTradePeFinancial.R | 22 ++-- R/calcEDGETransport.R | 28 ++-- R/calcEEAGHGProjections.R | 3 +- R/calcEmiPollutantExo.R | 197 ++++++++++++---------------- R/calcEmiTarget.R | 6 +- R/calcEmissionFactors.R | 14 +- R/calcEmissions.R | 5 +- R/calcEuropeanEnergyDatasheets.R | 6 +- R/calcFE.R | 6 +- R/calcFEShares.R | 2 +- R/calcFEdemand.R | 7 +- R/calcFeDemandBuildings.R | 34 ++--- R/calcFloorspace.R | 8 +- R/calcFossilExtraction.R | 7 +- R/calcGAINS.R | 134 ++++++++++--------- R/calcHistorical.R | 10 +- R/calcIEA_ETP.R | 29 ++-- R/calcIO.R | 21 ++- R/calcIntertempElastSubst.R | 30 ++--- R/calcJRC_IDEES.R | 7 +- R/calcMacroInvestments.R | 27 ++-- R/calcPE.R | 4 +- R/calcPlasticsEoL.R | 11 +- R/calcProjectPipelines.R | 2 +- R/calcRiskPremium.R | 4 +- R/calcSolar.R | 6 - R/calcTheil.R | 31 +++-- R/calcTimePref.R | 18 +-- R/calcTransportGDPshare.R | 12 +- R/calcUBA.R | 4 +- R/calcUNFCCC.R | 3 +- R/convertCDIAC.R | 2 +- R/convertECLIPSE.R | 23 ++-- R/convertEDGETransport.R | 31 +++-- R/convertEdgeBuildings.R | 42 +++--- R/convertEuropeanEnergyDatasheets.R | 2 +- R/convertGini.R | 53 -------- R/convertGlobalCCSinstitute.R | 7 +- R/convertIEA_ETP.R | 11 +- R/convertIEA_WEO.R | 1 - R/convertLIMITS.R | 48 ++----- R/convertNREL.R | 10 +- R/convertProdShares.R | 8 +- R/convertRCP.R | 2 +- R/convertREMIND_11Regi.R | 4 +- R/convertREN21.R | 3 +- R/convertStationary.R | 28 ++-- R/convertUNFCCC_NDC.R | 4 +- R/downloadNIR.R | 22 ++-- R/fullREMIND.R | 65 ++++++--- R/fullTHRESHOLDS.R | 6 +- R/readAGEB.R | 3 +- R/readAriadneDB.R | 8 +- R/readDLR.R | 6 +- R/readECLIPSE.R | 24 ++-- R/readEDGAR7Fgases.R | 22 +--- R/readEDGETransport.R | 39 +++--- R/readEdgeBuildings.R | 14 +- R/readEuropeanEnergyDatasheets.R | 15 +-- R/readEurostat_EffortSharing.R | 6 +- R/readExpertGuess.R | 21 ++- R/readGCPT.R | 47 ++++--- R/readGEA2012.R | 9 +- R/readGGDC10.R | 17 +-- R/readGini.R | 72 ++++++++-- R/readGlobalCCSinstitute.R | 10 +- R/readGlobalEnergyMonitor.R | 9 +- R/readHRE.R | 18 +-- R/readIAEA_PRIS.R | 4 +- R/readIEA_CCUS.R | 20 +-- R/readIEA_ETP.R | 20 ++- R/readIEA_EVOutlook.R | 6 +- R/readIEA_HSMR.R | 8 +- R/readIEA_WEIO_2014.R | 130 ------------------ R/readIEA_WEO.R | 47 ++++--- R/readIEA_WorldEnergyOutlook.R | 14 +- R/readIRENA.R | 5 +- R/readOpenmod.R | 2 +- R/readRCP.R | 86 ++++++------ R/readStationary.R | 9 +- R/readUBA.R | 1 - R/readUNFCCC.R | 17 +-- R/toolAggregateTimeSteps.R | 4 +- R/toolBiomassSupplyAggregate.R | 12 +- R/toolSolarFunctionAggregate.R | 50 ++++--- README.md | 8 +- man/calcCapacityFactorHist.Rd | 2 +- man/calcEmiPollutantExo.Rd | 2 - man/calcMacroInvestments.Rd | 23 ++++ man/calcSolar.Rd | 3 - man/convertGini.Rd | 2 +- man/convertGlobalCCSinstitute.Rd | 3 +- man/fullREMIND.Rd | 5 +- man/readGini.Rd | 14 +- man/readIEA_WEIO_2014.Rd | 19 --- man/readRCP.Rd | 1 - man/toolBiomassSupplyAggregate.Rd | 3 +- 107 files changed, 908 insertions(+), 1275 deletions(-) delete mode 100644 R/convertGini.R delete mode 100644 R/readIEA_WEIO_2014.R create mode 100644 man/calcMacroInvestments.Rd delete mode 100644 man/readIEA_WEIO_2014.Rd diff --git a/.buildlibrary b/.buildlibrary index 9e465403..15b38f19 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '43050380' +ValidationKey: '430792199830' AcceptedWarnings: - Invalid URL: .* - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index ae9d324a..f6766094 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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.214.0 -date-released: '2025-01-29' +version: 0.214.0.9015 +date-released: '2025-02-03' abstract: The mrremind packages contains data preprocessing for the REMIND model. authors: - family-names: Baumstark diff --git a/DESCRIPTION b/DESCRIPTION index 0a3bddf9..3a0d2b71 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrremind Title: MadRat REMIND Input Data Package -Version: 0.214.0 -Date: 2025-01-29 +Version: 0.214.0.9015 +Date: 2025-02-03 Authors@R: c( person("Lavinia", "Baumstark", , "lavinia@pik-potsdam.de", role = c("aut", "cre")), person("Renato", "Rodrigues", role = "aut"), @@ -40,7 +40,6 @@ URL: https://github.com/pik-piam/mrremind Depends: R (>= 2.10.0) Imports: - assertr, countrycode, data.table, dplyr, @@ -53,6 +52,7 @@ Imports: magrittr, mrcommons (>= 1.44.12), mrdrivers (>= 6.0.0), + mrfaocore, mrindustry (>= 0.4.0), nnls, purrr, diff --git a/NAMESPACE b/NAMESPACE index 4a214b2b..f44955c2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(calcIEA_ETP) export(calcIEA_EVOutlook) export(calcIEA_WorldEnergyOutlook) export(calcIRENA) +export(calcMacroInvestments) export(calcOtherFossilInElectricity) export(calcPlasticsEoL) export(calcProjectPipelines) @@ -32,6 +33,7 @@ export(convertIEA_CCUS) export(convertIEA_ETP) export(convertUBA) export(convertUNFCCC) +export(fullREMIND) export(fullTHRESHOLDS) export(readAGEB) export(readAR6GWP) @@ -43,7 +45,6 @@ export(readHRE) export(readIAEA_PRIS) export(readIEA_CCUS) export(readIEA_HSMR) -export(readIEA_WEIO_2014) export(readUBA) export(readUNFCCC) export(toolAggregateTimeSteps) @@ -54,105 +55,31 @@ export(toolFillEU34Countries) export(toolSolarFunctionAggregate) import(madrat) import(magclass) -importFrom(assertr,verify) importFrom(data.table,":=") -importFrom(data.table,data.table) -importFrom(data.table,rbindlist) -importFrom(data.table,setDT) -importFrom(data.table,setkeyv) -importFrom(data.table,setnames) -importFrom(dplyr,"%>%") -importFrom(dplyr,across) -importFrom(dplyr,all_of) -importFrom(dplyr,anti_join) importFrom(dplyr,arrange) -importFrom(dplyr,bind_cols) importFrom(dplyr,bind_rows) importFrom(dplyr,desc) importFrom(dplyr,distinct) -importFrom(dplyr,everything) importFrom(dplyr,filter) importFrom(dplyr,full_join) importFrom(dplyr,group_by) -importFrom(dplyr,if_any) -importFrom(dplyr,lag) importFrom(dplyr,left_join) importFrom(dplyr,matches) -importFrom(dplyr,min_rank) importFrom(dplyr,mutate) -importFrom(dplyr,n) importFrom(dplyr,pull) -importFrom(dplyr,recode) -importFrom(dplyr,relocate) importFrom(dplyr,rename) -importFrom(dplyr,rename_all) importFrom(dplyr,select) -importFrom(dplyr,starts_with) importFrom(dplyr,summarise) -importFrom(dplyr,summarize) -importFrom(dplyr,tribble) importFrom(dplyr,ungroup) -importFrom(madrat,calcOutput) -importFrom(madrat,madratAttach) -importFrom(madrat,readSource) -importFrom(madrat,toolAggregate) -importFrom(madrat,toolGetMapping) -importFrom(magclass,"getItems<-") -importFrom(magclass,collapseDim) -importFrom(magclass,dimSums) -importFrom(magclass,getItems) -importFrom(magclass,matchDim) -importFrom(magclass,mbind) -importFrom(magclass,mselect) -importFrom(magclass,time_interpolate) importFrom(magrittr,"%>%") -importFrom(quitte,add_countrycode_) -importFrom(quitte,as.quitte) -importFrom(quitte,calc_addVariable) -importFrom(quitte,cartesian) -importFrom(quitte,inline.data.frame) -importFrom(quitte,interpolate_missing_periods) -importFrom(quitte,madrat_mule) -importFrom(quitte,removeColNa) -importFrom(readr,read_csv) importFrom(readxl,excel_sheets) importFrom(readxl,read_excel) importFrom(readxl,read_xlsx) importFrom(rlang,"!!!") importFrom(rlang,"!!") importFrom(rlang,.data) -importFrom(rlang,is_empty) importFrom(rlang,sym) importFrom(rlang,syms) -importFrom(stats,aggregate) -importFrom(stats,coef) -importFrom(stats,complete.cases) -importFrom(stats,lm) -importFrom(stats,na.exclude) -importFrom(stats,na.omit) -importFrom(stats,na.pass) -importFrom(stats,weighted.mean) importFrom(tibble,as_tibble) importFrom(tibble,tibble) -importFrom(tidyr,complete) -importFrom(tidyr,drop_na) -importFrom(tidyr,expand_grid) -importFrom(tidyr,extract) -importFrom(tidyr,gather) -importFrom(tidyr,nest) -importFrom(tidyr,nesting) -importFrom(tidyr,pivot_longer) -importFrom(tidyr,pivot_wider) -importFrom(tidyr,separate) -importFrom(tidyr,spread) -importFrom(tidyr,unite) -importFrom(tidyr,unnest) -importFrom(tidyselect,all_of) -importFrom(utils,download.file) -importFrom(utils,head) -importFrom(utils,packageVersion) importFrom(utils,read.csv) -importFrom(utils,read.csv2) -importFrom(utils,read.table) -importFrom(utils,tail) -importFrom(utils,unzip) diff --git a/R/calcAGEB.R b/R/calcAGEB.R index b6f95011..17f1362d 100644 --- a/R/calcAGEB.R +++ b/R/calcAGEB.R @@ -8,7 +8,6 @@ #' @param subtype data subtype. Either "balances" ("Auswertungstabellen zur Energiebilanz Deutschland") or #' "electricity" ("Bruttostromerzeugung in Deutschland nach Energieträgern") #' @importFrom dplyr select mutate left_join -#' @importFrom stats aggregate #' @export calcAGEB <- function(subtype = "balances") { ageb <- readSource("AGEB", subtype = subtype) @@ -36,7 +35,7 @@ calcAGEB <- function(subtype = "balances") { ) %>% select("variable" = "REMIND_variable", "region", "year", "value") - x <- aggregate(value ~ variable + region + year, x, sum) %>% + x <- stats::aggregate(value ~ variable + region + year, x, sum) %>% as.magpie() %>% toolCountryFill(fill = NA, verbosity = 2) diff --git a/R/calcBP.R b/R/calcBP.R index 7100f8cf..bed7b895 100644 --- a/R/calcBP.R +++ b/R/calcBP.R @@ -1,8 +1,6 @@ #' Calculate REMIND variables from historical BP values #' #' @author Falk Benke -#' @importFrom magclass matchDim -#' @importFrom quitte interpolate_missing_periods #' #' @export #' @@ -36,7 +34,7 @@ calcBP <- function() { data.frame(period = 2050, value = 0.45) ) - efficiencyFactors <- interpolate_missing_periods(efficiencyFactors, method = "linear") %>% + efficiencyFactors <- quitte::interpolate_missing_periods(efficiencyFactors, method = "linear") %>% as.magpie() %>% dimReduce() diff --git a/R/calcBiomassPrices.R b/R/calcBiomassPrices.R index 97f92aba..a76db0be 100644 --- a/R/calcBiomassPrices.R +++ b/R/calcBiomassPrices.R @@ -6,7 +6,7 @@ calcBiomassPrices <- function() { x <- readSource("MAgPIE", subtype = "supplyCurve_magpie_40") # add supply curves for SDP-MC-SSP1-PkBudg1000 copying SDP-MC-SSP1-PkBudg650 - tmp <- x[, , "SDP-MC-SSP1-PkBudg650"] + tmp <- x[, , "SDP-MC-SSP1-PkBudg650"] getNames(tmp) <- gsub("SDP-MC-SSP1-PkBudg650", "SDP-MC-SSP1-PkBudg1000", getNames(tmp)) x <- mbind(x, tmp) @@ -37,9 +37,9 @@ calcBiomassPrices <- function() { x[, , "a"][is.na(x[, , "a"])] <- 1 x[, , "b"][is.na(x[, , "b"])] <- 0.1 - return(list(x = x, - weight = calcOutput("FAOLand", aggregate = FALSE)[, , "6610", pmatch = TRUE][, "y2010", ], - unit = "none", - description = "coefficients for the bioenergy supplycurve", - aggregationFunction = toolBiomassSupplyAggregate)) + list(x = x, + weight = calcOutput("FAOLand", aggregate = FALSE)[, , "6610", pmatch = TRUE][, "y2010", ], + unit = "none", + description = "coefficients for the bioenergy supplycurve", + aggregationFunction = toolBiomassSupplyAggregate) } diff --git a/R/calcCapacity.R b/R/calcCapacity.R index 745d0219..304af285 100644 --- a/R/calcCapacity.R +++ b/R/calcCapacity.R @@ -3,7 +3,6 @@ #' #' @param subtype data subtype. Either "capacityByTech" or "capacityByPE" #' @return magpie object of capacity data -#' @importFrom dplyr tribble #' @author Renato Rodrigues, Stephen Bi #' @examples #' \dontrun{ @@ -90,7 +89,7 @@ calcCapacity <- function(subtype) { # *** for 2018-2022, take 90GW, 90GW*0.5=50GW ngt, the rest is split between ngcc and gaschp 70:30 (from IEA EB energy output) CHA.2020.GasData <- as.magpie( - tribble( + tibble::tribble( ~region, ~year, ~data, ~value, "CHN", 2010, "gaschp", 0.004, "CHN", 2015, "gaschp", 0.011, @@ -107,7 +106,7 @@ calcCapacity <- function(subtype) { # so it should be roughly ok as upper bound. (don't use as lower bound!) USA.2025.PVData <- as.magpie( - tribble( + tibble::tribble( ~region, ~year, ~data, ~value, "USA", 2025, "spv", 0.265)) diff --git a/R/calcCapacityFactorHist.R b/R/calcCapacityFactorHist.R index 3fab8de5..ffce08d1 100644 --- a/R/calcCapacityFactorHist.R +++ b/R/calcCapacityFactorHist.R @@ -5,26 +5,23 @@ #' #' @return magpie object of the capacity factor data #' @author Renato Rodrigues, Stephen Bi -#' @importFrom rlang .data #' @examples -#' \dontrun{ +#' \dontrun{ #' calcOutput("CapacityFactor") #' } -#' - - +#' calcCapacityFactorHist <- function(subtype){ - + if (subtype == "wind") { - + #mapping of remind technology names to IRENA categories rem_Irena_map <- data.frame(rem=c("hydro","wind","spv","csp","bioigcc","geohdr"), irena= c("Renewable hydropower","Wind","Solar photovoltaic","Concentrated solar power", "Bioenergy","Geothermal")) # Read capacity factor inputs hist_cap <- readSource(type="IRENA",subtype="Capacity")/1000 # converting from MW to GW hist_gen <- readSource("IRENA", subtype = "Generation")# Units are GWh - # Calculate 2015 capacity factor for relevant technologies - cf_realworld <- hist_gen[,2015,rem_Irena_map$irena]/(8760*hist_cap[,2015,rem_Irena_map$irena]) + # Calculate 2015 capacity factor for relevant technologies + cf_realworld <- hist_gen[,2015,rem_Irena_map$irena]/(8760*hist_cap[,2015,rem_Irena_map$irena]) #rename getNames(cf_realworld) <- rem_Irena_map$rem #check data @@ -32,29 +29,29 @@ calcCapacityFactorHist <- function(subtype){ max(cf_realworld[,,"wind"],na.rm = T) #INF spm, >1 AZE max(cf_realworld[,,"spv"],na.rm = T) max(cf_realworld[,,"csp"],na.rm = T) - max(cf_realworld[,,"bioigcc"],na.rm = T) #>1 CHL, JPN, POL + max(cf_realworld[,,"bioigcc"],na.rm = T) #>1 CHL, JPN, POL max(cf_realworld[,,"geohdr"],na.rm = T) - + #correct SPM infinite value cf_realworld[is.infinite(cf_realworld)] <- 0.8 #correct AZE,CHL,JPN,POL >1 value cf_realworld[cf_realworld > 1] <- 0.8 #get rid of NAs cf_realworld[is.na(cf_realworld)] <- 0 - - + + #weight: historic generation hist_gen <- hist_gen[,2015,rem_Irena_map$irena] getNames(hist_gen) <- rem_Irena_map$rem hist_gen[is.na(cf_realworld)] <- 0 - + return(list(x=cf_realworld, weight=hist_gen, - unit="% of capacity", - description="Installed capacity availability in 2015 - capacity factor (fraction of the year that a plant is running)" + unit="% of capacity", + description="Installed capacity availability in 2015 - capacity factor (fraction of the year that a plant is running)" )) - + } else if (subtype == "windoff") { - + #mapping of remind technology names to IRENA categories tech_list = c("hydro","wind","windoff","spv","csp","bioigcc","geohdr") rem_Irena_map <- data.frame(rem=tech_list, @@ -65,107 +62,107 @@ calcCapacityFactorHist <- function(subtype){ # Calculate 2015 capacity factor for relevant technologies, using 2*generation(t) / (cap(t) + cap(t-1) ) # then average over 5 years for 2015 - + # load 5 years of generation - hist_gen2 = hist_gen[,seq(2013,2017,1), rem_Irena_map$irena]%>% - as.data.frame() + hist_gen2 = hist_gen[,seq(2013,2017,1), rem_Irena_map$irena]%>% + as.data.frame() # %>% # filter(.data$Data1 == "Offshore wind energy",.data$Region=="USA" ) - + # load 5 years of capacity - hist_cap2a = hist_cap[,seq(2012,2016,1), rem_Irena_map$irena] %>% - as.data.frame() %>% - mutate(Year = as.factor(as.integer(as.character(.data$Year)) + 1)) %>% + hist_cap2a = hist_cap[,seq(2012,2016,1), rem_Irena_map$irena] %>% + as.data.frame() %>% + mutate(Year = as.factor(as.integer(as.character(.data$Year)) + 1)) %>% select(.data$Year, .data$Region, .data$Data1, 'cap1' ='Value') - + # load 5 years of capacity - hist_cap2b = hist_cap[,seq(2013,2017,1), rem_Irena_map$irena]%>% - as.data.frame() %>% + hist_cap2b = hist_cap[,seq(2013,2017,1), rem_Irena_map$irena]%>% + as.data.frame() %>% select(.data$Year, .data$Region, .data$Data1, 'cap2' ='Value') - - hist_cap2 <- full_join(hist_cap2b, hist_cap2a) %>% + + hist_cap2 <- full_join(hist_cap2b, hist_cap2a) %>% # filter(.data$Data1 == "Offshore wind energy",.data$Region=="USA" ) %>% mutate(cap = (.data$cap1 + .data$cap2) / 2) %>% select(.data$Year, .data$Region, .data$Data1, .data$cap) - - cf_year <- full_join(hist_gen2, hist_cap2) %>% + + cf_year <- full_join(hist_gen2, hist_cap2) %>% mutate(Value = .data$Value / (8760 * .data$cap)) - + cf_year$Value[cf_year$cap < 0.2] <- 0 # remove those CFs if the installed capacity are too tiny - - cf_year <- cf_year %>% - select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% + + cf_year <- cf_year %>% + select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% as.magpie() - + cf_year[is.na(cf_year)] <- 0 - + # cf_year <- as.data.frame(cf_year) - + #rename getNames(cf_year) <- rem_Irena_map$rem #check data max(cf_year[,,"hydro"],na.rm = T) max(cf_year[,,"wind"],na.rm = T)#INF spm, >1 AZE - max(cf_year[,,"windoff"],na.rm = T) + max(cf_year[,,"windoff"],na.rm = T) max(cf_year[,,"spv"],na.rm = T) max(cf_year[,,"csp"],na.rm = T) - max(cf_year[,,"bioigcc"],na.rm = T) #>1 CHL, JPN, POL + max(cf_year[,,"bioigcc"],na.rm = T) #>1 CHL, JPN, POL max(cf_year[,,"geohdr"],na.rm = T) - + #correct SPM infinite value cf_year[is.infinite(cf_year)] <- 0.8 #correct AZE,CHL,JPN,POL >1 value cf_year[cf_year > 1] <- 0.8 #get rid of NAs cf_year[is.na(cf_year)] <- 0 - + # cf_year <- as.data.frame(cf_year) - + # averaging over 5 years for non-0 CFs cf_realworld_n0 <- cf_year %>% - as.data.frame() %>% - filter(.data$Value != 0) %>% + as.data.frame() %>% + filter(.data$Value != 0) %>% group_by(.data$Region, .data$Data1) %>% summarise(Value=mean(.data$Value)) %>% mutate(Year="2015") %>% select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% ungroup() - + # for regions and techs with 0 CFs for all 5 years cf_realworld_0 <- cf_year %>% - as.data.frame() %>% + as.data.frame() %>% group_by(.data$Region, .data$Data1) %>% summarise(Value=sum(.data$Value)) %>% - filter(.data$Value == 0) %>% + filter(.data$Value == 0) %>% mutate(Year="2015") %>% select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% ungroup() - - cf_realworld <- full_join(cf_realworld_n0, cf_realworld_0) %>% + + cf_realworld <- full_join(cf_realworld_n0, cf_realworld_0) %>% group_by(.data$Region, .data$Year) %>% - mutate(Data1 = factor(.data$Data1, levels=tech_list) ) %>% - ungroup() %>% + mutate(Data1 = factor(.data$Data1, levels=tech_list) ) %>% + ungroup() %>% as.magpie() - # cf_realworld <- cf_realworld %>% + # cf_realworld <- cf_realworld %>% # as.data.frame() - + #weight: historic generation hist_gen <- hist_gen2 %>% - select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% + select(.data$Year,.data$Region,.data$Data1,.data$Value) %>% group_by(.data$Region, .data$Data1) %>% - summarise(Value=sum(.data$Value)) %>% - mutate(Year="2015") %>% - ungroup() %>% + summarise(Value=sum(.data$Value)) %>% + mutate(Year="2015") %>% + ungroup() %>% as.magpie() - + # hist_gen <- as.data.frame(hist_gen) getNames(hist_gen) <- rem_Irena_map$rem hist_gen[is.na(cf_realworld)] <- 0 - + return(list(x=cf_realworld, weight=hist_gen, - unit="% of capacity", - description="Installed capacity availability in 2015 - capacity factor (fraction of the year that a plant is running)" + unit="% of capacity", + description="Installed capacity availability in 2015 - capacity factor (fraction of the year that a plant is running)" )) } - + } diff --git a/R/calcCapital.R b/R/calcCapital.R index 1d99abe7..87c07ae9 100644 --- a/R/calcCapital.R +++ b/R/calcCapital.R @@ -19,14 +19,18 @@ calcCapital <- function() { kIntPWT <- kPWT / setNames(gdpPWT, NULL) # Get GDP from mrdrivers (which differs from GDP in PWT) - gdp <- calcOutput("GDP", naming = "scenario", aggregate = FALSE, years = seq(1995, 2150, 5)) + gdp <- calcOutput("GDP", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + aggregate = FALSE, + years = seq(1995, 2150, 5)) # 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" = 150, "SSP4" = 300, "SSP5" = 150, - "SDP" = 150, "SDP_EI" = 150, "SDP_RC" = 150, "SDP_MC" = 150, "SSP2EU" = 250) + "SDP" = 150, "SDP_EI" = 150, "SDP_RC" = 150, "SDP_MC" = 150) # Create kInt magpie object with the same dimension as gdp, and assign the PWT capital intensities for the # historic years until 2010. @@ -71,14 +75,13 @@ calcCapital <- function() { kap <- k[, 2015, "SSP2"] %>% tibble::as_tibble() %>% dplyr::select("iso3c", "kap" = "value") EEK <- calcOutput("Industry_EEK", kap = kap, aggregate = FALSE, years = getYears(k)) - # 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") + # Modify names to differentiate the macroeconomic capital "kap" from EEK capital stocks. + getNames(k) <- paste0(getNames(k), ".kap") getSets(k) <- c("iso3c", "year", "ssp", "variable") x <- mbind(k, EEK) list(x = x, weight = NULL, - unit = "trillion US$2017", + unit = glue::glue("trillion US${mrdrivers::toolGetUnitDollar(returnOnlyBase = TRUE)}"), description = "Capital stock computed using the capital/GDP ratio from PWT, and GDP scenarios from mrdrivers.") } diff --git a/R/calcCostsTradePeFinancial.R b/R/calcCostsTradePeFinancial.R index c40877b8..e6214a36 100644 --- a/R/calcCostsTradePeFinancial.R +++ b/R/calcCostsTradePeFinancial.R @@ -12,19 +12,15 @@ calcCostsTradePeFinancial <- function() { data <- readSource("ExpertGuess", subtype = "costsTradePeFinancial") - data <- GDPuc::toolConvertGDP( - gdp = data, - unit_in = "constant 2005 US$MER", - unit_out = mrdrivers::toolGetUnitDollar(), - replace_NAs = "with_USA" - ) + data <- GDPuc::toolConvertGDP(gdp = data, + unit_in = "constant 2005 US$MER", + unit_out = mrdrivers::toolGetUnitDollar(), + replace_NAs = "with_USA") - w <- calcOutput("GDP", aggregate = FALSE)[, 2005, "gdp_SSP2"] + w <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE)[, 2005, ] - return(list( - x = data, - weight = w, - unit = "trillion US$2017/TWa", - description = "PE tradecosts (financial costs on import, export and use)" - )) + list(x = data, + weight = w, + unit = glue::glue("trillion US${mrdrivers::toolGetUnitDollar(returnOnlyBase = TRUE)}/TWa"), + description = "PE tradecosts (financial costs on import, export and use)") } diff --git a/R/calcEDGETransport.R b/R/calcEDGETransport.R index 1a8b45d9..57460fbb 100644 --- a/R/calcEDGETransport.R +++ b/R/calcEDGETransport.R @@ -22,7 +22,7 @@ calcEDGETransport <- function(subtype) { # if so, the values should just be aggregated equally in order to prevent zeros in the results weight <- rmndt::magpie2dt(weight) regMap <- toolGetMapping(getConfig("regionmapping"), type = "regional", where = "mappingfolder") - setnames(regMap, "CountryCode", "all_regi") + data.table::setnames(regMap, "CountryCode", "all_regi") weight <- merge(weight, regMap[, c("all_regi", "RegionCode")], by = "all_regi") weight[, weightSum := sum(value), by = c("RegionCode", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_teEs", "tall")] weight[weightSum == 0, value := 1] @@ -38,7 +38,7 @@ calcEDGETransport <- function(subtype) { # if so, the values should just be aggregated equally in order to prevent zeros in the results weight <- rmndt::magpie2dt(weight) regMap <- toolGetMapping(getConfig("regionmapping"), type = "regional", where = "mappingfolder") - setnames(regMap, "CountryCode", "all_regi") + data.table::setnames(regMap, "CountryCode", "all_regi") weight <- merge(weight, regMap[, c("all_regi", "RegionCode")], by = "all_regi") weight[, weightSum := sum(value), by = c("RegionCode", "GDP_scenario", "DEM_scenario", "EDGE_scenario", "all_enty", "all_in", "all_teEs", "tall")] weight[weightSum == 0, value := 1] @@ -58,53 +58,51 @@ calcEDGETransport <- function(subtype) { description <- "Energy service demand on CES level." }, "CAPEXandNonFuelOPEX" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "2017US$/(p|t)km" description <- "Capital cost (purchase) and non-fuel operational costs on technology level." }, "scenSpecPrefTrends" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "-" description <- "Scenario specific preference trends on technology level." }, "scenSpecLoadFactor" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "-" description <- "Scenario specific load factor on technology level." }, "scenSpecEnIntensity" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "MJ/vehkm" description <- "Scenario specific energy intensity on technology level." }, "initialIncoCosts" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "2017US$/(p|t)km" description <- "Initial inconvenience cost values." }, "annualMileage" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "vehkm/yr" description <- "Annual vehicle km traveled." }, "timeValueCosts" = { - gdp <- calcOutput("GDP", aggregate = FALSE)[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) weight <- gdp |> time_interpolate(getYears(x)) unit <- "2017US$/(p|t)km" description <- "Value of time cost equivalent." } ) - return(list( - x = x, - weight = weight, - unit = unit, - description = description - )) + list(x = x, + weight = weight, + unit = unit, + description = description) } diff --git a/R/calcEEAGHGProjections.R b/R/calcEEAGHGProjections.R index 5a15230e..6e5ac418 100644 --- a/R/calcEEAGHGProjections.R +++ b/R/calcEEAGHGProjections.R @@ -6,9 +6,8 @@ #' @author Falk Benke #' #' @importFrom dplyr select mutate left_join filter -#' @importFrom rlang sym #' @export - +#' calcEEAGHGProjections <- function() { mapping <- toolGetMapping(type = "sectoral", diff --git a/R/calcEmiPollutantExo.R b/R/calcEmiPollutantExo.R index 62cfdcbe..abe07724 100644 --- a/R/calcEmiPollutantExo.R +++ b/R/calcEmiPollutantExo.R @@ -1,135 +1,100 @@ #' calcEmiPollutantExo #' calculate EmiPollutantExo based on RCP data -#' +#' #' @return magpie object #' @author Julian Oeser #' @param subtype Either 'Waste' or 'AviationShipping' #' @param aviationshippingsource Defines source for aviation and shipping emissions. Either 'RCP' or 'LeeGAINS'. #' @seealso \code{\link{calcOutput}} #' @examples -#' #' \dontrun{ a <- calcOutput(type="EmiPollutantExo") #' } -#' #' +calcEmiPollutantExo <- function(subtype, aviationshippingsource = "RCP") { + if (!subtype %in% c("Waste", "AviationShipping")) { + stop("Invalid subtype. Must be 'Waste' or 'AviationShipping'") + } + if (!aviationshippingsource %in% c("RCP", "AviationShipping")) { + stop("Invalid aviationshippingsource. Must be 'RCP' or 'LeeGAINS'") + } -calcEmiPollutantExo <- function(subtype, aviationshippingsource="RCP"){ - - if (subtype=="Waste") { - - x <- readSource("RCP", subtype="Waste") - - # scale to EDGAR data - ch4 <- readSource("EDGAR", subtype="ch4_history")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - so2 <- readSource("EDGAR", subtype="SO2")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - bc <- setNames(collapseNames(readSource("CEDS", subtype="BC")[,2005,c("5A_Solid-waste-disposal", - "5C_Waste-incineration", - "5D_Wastewater-handling", - "5E_Other-waste-handling")]),c("6A","6B","6C","6D")) / 1000 # convert from kt to Tg - getSets(bc) <- getSets(ch4) - oc <- setNames(collapseNames(readSource("CEDS", subtype="OC")[,2005,c("5A_Solid-waste-disposal", - "5C_Waste-incineration", - "5D_Wastewater-handling", - "5E_Other-waste-handling")]),c("6A","6B","6C","6D")) / 1000 # convert from kt to Tg - getSets(oc) <- getSets(ch4) - co <- readSource("EDGAR", subtype="CO")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - voc <- readSource("EDGAR", subtype="VOC")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - #nh3 <- readSource("EDGAR", subtype="NH3")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - nox <- readSource("EDGAR", subtype="NOx")[,2005,c("6A","6B","6C","6D")] / 1000 # convert from Gg to Tg - - ch4 <- add_dimension(ch4, dim=3.1, "type", "ch4") - so2 <- add_dimension(so2, dim=3.1, "type", "so2") - bc <- add_dimension(bc, dim=3.1, "type", "bc") - oc <- add_dimension(oc, dim=3.1, "type", "oc") - co <- add_dimension(co, dim=3.1, "type", "CO") - voc <- add_dimension(voc, dim=3.1, "type", "VOC") - #nh3 <- add_dimension(nh3, dim=3.1, "type", "NH3") - nox <- add_dimension(nox, dim=3.1, "type", "NOx") - - scale_data <- collapseNames(mbind(ch4,so2,bc,oc,co,voc,nox)) - scale_data <- dimSums(scale_data,dim=3.2) - getYears(scale_data) <- NULL - getSets(scale_data) <- c("region","year","type") - x <- complete_magpie(x,fill=0) - scale_x <- x[,2005,] - getYears(scale_x) <- NULL - - x <- x * scale_data / scale_x - x[is.na(x)] <- 0 - - # interpolate time - y <- getYears(x, as.integer = TRUE) - required_year <- seq(2000, 2150, by=5) - interpolated_year <- required_year[!(required_year %in% y)] - out <- time_interpolate(x, interpolated_year, TRUE, extrapolation_type = "constant") - - getSets(out) <- getSets(x) - - - } else if (subtype=="AviationShipping") { - + if (subtype == "Waste") { + x <- readSource("RCP", subtype = "Waste") + # EDGAR and CEDS IDs of interest + eIds <- c("6A", "6B", "6C", "6D") + cIDs <- c("5A_Solid-waste-disposal", "5C_Waste-incineration", "5D_Wastewater-handling", "5E_Other-waste-handling") + } + + if (subtype == "AviationShipping") { if (aviationshippingsource == "RCP") { - x <- readSource("RCP", subtype="AviationShipping", convert=FALSE) - } else if (aviationshippingsource == "LeeGAINS") { - shipping <- readSource("ECLIPSE", subtype="shipping.emi", convert = FALSE) - avi <- readSource("Lee", subtype="emi") + x <- readSource("RCP", subtype = "AviationShipping", convert = FALSE) + } + if (aviationshippingsource == "LeeGAINS") { + shipping <- readSource("ECLIPSE", subtype = "shipping.emi", convert = FALSE) + avi <- readSource("Lee", subtype = "emi") x <- mbind(shipping, avi) getSets(x) <- c("region", "year", "source", "type", "scenario") - dimnames(x)[[3]] <- gsub(pattern = "CH4", replacement = "ch4", dimnames(x)[[3]]) - dimnames(x)[[3]] <- gsub(pattern = "BC", replacement = "bc", dimnames(x)[[3]]) - dimnames(x)[[3]] <- gsub(pattern = "SO2", replacement = "so2", dimnames(x)[[3]]) + dimnames(x)[[3]] <- gsub("CH4", "ch4", dimnames(x)[[3]]) + dimnames(x)[[3]] <- gsub("BC", "bc", dimnames(x)[[3]]) + dimnames(x)[[3]] <- gsub("SO2", "so2", dimnames(x)[[3]]) } - - # scale to EDGAR data - ch4 <- readSource("EDGAR", subtype="ch4_history")[,2005,c("1C1","1C2")] / 1000 # convert from Gg to Tg - so2 <- readSource("EDGAR", subtype="SO2")[,2005,c("1C1","1C2")] / 1000 # convert from Gg to Tg - bc <- setNames(collapseNames(readSource("CEDS", subtype="BC")[,2005,c("1A3ai_International-aviation","1A3di_International-shipping")]),c("1C1","1C2")) / 1000 # convert from kt to Tg - getSets(bc) <- getSets(ch4) - oc <- setNames(collapseNames(readSource("CEDS", subtype="OC")[,2005,c("1A3ai_International-aviation","1A3di_International-shipping")]),c("1C1","1C2")) / 1000 # convert from kt to Tg - getSets(oc) <- getSets(ch4) - co <- readSource("EDGAR", subtype="CO")[,2005,c("1C1","1C2")] / 1000 # convert from Gg to Tg - voc <- readSource("EDGAR", subtype="VOC")[,2005,c("1C1","1C2")] / 1000 # convert from Gg to Tg - nox <- readSource("EDGAR", subtype="NOx")[,2005,c("1C1","1C2")] / 1000 # convert from Gg to Tg - - ch4 <- add_dimension(ch4, dim=3.1, "type", "ch4") - so2 <- add_dimension(so2, dim=3.1, "type", "so2") - bc <- add_dimension(bc, dim=3.1, "type", "bc") - oc <- add_dimension(oc, dim=3.1, "type", "oc") - co <- add_dimension(co, dim=3.1, "type", "CO") - voc <- add_dimension(voc, dim=3.1, "type", "VOC") - nox <- add_dimension(nox, dim=3.1, "type", "NOx") - - scale_data <- collapseNames(mbind(ch4,so2,bc,oc,co,voc,nox)) - scale_data <- dimSums(scale_data,dim=1) - getNames(scale_data) <- gsub("1C1","Aviation",getNames(scale_data)) - getNames(scale_data) <- gsub("1C2","InternationalShipping",getNames(scale_data)) - getYears(scale_data) <- NULL - getSets(scale_data) <- c("region","year","type","source") - x <- complete_magpie(x,fill=0) - scale_x <- x[,2005,] - getYears(scale_x) <- NULL - - x <- x * scale_data / scale_x - x[is.na(x)] <- 0 - - # interpolate time - y <- getYears(x, as.integer = TRUE) - required_year <- seq(2000, 2150, by=5) - interpolated_year <- required_year[!(required_year %in% y)] - out <- time_interpolate(x, interpolated_year, TRUE, extrapolation_type = "constant") - - getSets(out) <- getSets(x) - - - } else { - stop("Invalid subtype. Must be 'Waste' or 'AviationShipping'") + # EDGAR and CEDS IDs of interest + eIds <- c("1C1", "1C2") + cIDs <- c("1A3ai_International-aviation", "1A3di_International-shipping") } - - - return(list(x=out, - weight=NULL, - unit="Tg/yr", - description="Emissions from Aviation and International Shipping from 2000 to 2150")) - + + # Get EDGAR and CEDS data used to scale + ch4 <- readSource("EDGAR", subtype = "ch4_history")[, 2005, eIds] + so2 <- readSource("EDGAR", subtype = "SO2")[, 2005, eIds] + co <- readSource("EDGAR", subtype = "CO")[, 2005, eIds] + voc <- readSource("EDGAR", subtype = "VOC")[, 2005, eIds] + nox <- readSource("EDGAR", subtype = "NOx")[, 2005, eIds] + bc <- readSource("CEDS", subtype = "BC")[, 2005, cIDs] %>% collapseNames() %>% setNames(eIds) + oc <- readSource("CEDS", subtype = "OC")[, 2005, cIDs] %>% collapseNames() %>% setNames(eIds) + getSets(bc) <- getSets(ch4) + getSets(oc) <- getSets(ch4) + + ch4 <- add_dimension(ch4, dim = 3.1, "type", "ch4") + so2 <- add_dimension(so2, dim = 3.1, "type", "so2") + bc <- add_dimension(bc, dim = 3.1, "type", "bc") + oc <- add_dimension(oc, dim = 3.1, "type", "oc") + co <- add_dimension(co, dim = 3.1, "type", "CO") + voc <- add_dimension(voc, dim = 3.1, "type", "VOC") + nox <- add_dimension(nox, dim = 3.1, "type", "NOx") + + # Convert from Gg or kt to Tg + scale_data <- collapseNames(mbind(ch4, so2, bc, oc, co, voc, nox)) / 1000 + + if (subtype == "Waste") { + scale_data <- dimSums(scale_data, dim = 3.2) + getSets(scale_data) <- c("region","year","type") + } + if (subtype == "AviationShipping") { + scale_data <- dimSums(scale_data, dim = 1) + getSets(scale_data) <- c("region", "year", "type", "source") + getNames(scale_data) <- gsub("1C1", "Aviation", getNames(scale_data)) + getNames(scale_data) <- gsub("1C2", "InternationalShipping", getNames(scale_data)) + } + + getYears(scale_data) <- NULL + x <- complete_magpie(x, fill = 0) + scale_x <- x[, 2005, ] + getYears(scale_x) <- NULL + + x <- x * scale_data / scale_x + x[is.na(x)] <- 0 + + # interpolate time + y <- getYears(x, as.integer = TRUE) + required_year <- seq(2000, 2150, by = 5) + interpolated_year <- required_year[!(required_year %in% y)] + out <- time_interpolate(x, interpolated_year, TRUE, extrapolation_type = "constant") + + getSets(out) <- getSets(x) + + list(x = out, + weight = NULL, + unit = "Tg/yr", + description = "Emissions from Aviation and International Shipping from 2000 to 2150") } diff --git a/R/calcEmiTarget.R b/R/calcEmiTarget.R index c987e0e6..20fcbef4 100644 --- a/R/calcEmiTarget.R +++ b/R/calcEmiTarget.R @@ -2,7 +2,7 @@ #' @author Aman Malik, Christoph Bertram, Oliver Richters #' @param sources currently only UNFCCC_NDC #' @param subtype "Ghgshare2005", "Ghgfactor", "Ghghistshare" - +#' calcEmiTarget <- function(sources, subtype) { ### Import historical data (gdp and emi) needed for the calculations @@ -29,7 +29,7 @@ calcEmiTarget <- function(sources, subtype) { ghg <- toolCountryFill(ghg, fill = 0, verbosity = 2) # Future GDP values - gdp <- calcOutput("GDP", aggregate = FALSE) + gdp <- calcOutput("GDP", scenario = c("SSPs", "SDPs"), naming = "scenario", aggregate = FALSE) convertNAto0 <- function(x) { x[is.na(x)] <- 0 @@ -67,7 +67,7 @@ calcEmiTarget <- function(sources, subtype) { # create 1/0 dummy for calculation of regional share covered by quantitative target, per TarYear. # Note that 0 implies no goal, net zero targets have ghgfactor of 0 but dummy of 1 - dummy1 <- 1 * (!is.na(ghgfactor[, , "gdp_SSP2", drop = TRUE])) + dummy1 <- 1 * (!is.na(ghgfactor[, , "SSP2", drop = TRUE])) if (grepl("Ghgfactor", subtype, fixed = TRUE)) { # p45_factor_targetyear.cs3r # in order to calculate the share of regional emissions coming from countries with quantitative target diff --git a/R/calcEmissionFactors.R b/R/calcEmissionFactors.R index c59407b4..cd620ac8 100644 --- a/R/calcEmissionFactors.R +++ b/R/calcEmissionFactors.R @@ -1,7 +1,3 @@ -#' @importFrom dplyr ungroup -#' @importFrom utils read.csv read.csv2 -#' @importFrom quitte as.quitte - calcEmissionFactors <- function(subtype = "emission_factors", sectoral_resolution = "aggregated") { if (!(subtype %in% c("emission_factors", "activities", "emissions"))) { stop('subtype must be in c("emission_factors", "activities", "emissions")') @@ -22,14 +18,14 @@ calcEmissionFactors <- function(subtype = "emission_factors", sectoral_resolutio dummy <- id_ef[ip_region, ip_year, ip_scenario] # Get minimum values across country group - tmp <- as.quitte(id_ef[ip_countryGroup, ip_year, ip_scenario]) %>% + tmp <- quitte::as.quitte(id_ef[ip_countryGroup, ip_year, ip_scenario]) %>% group_by(!!!syms(c("data1", "data2"))) %>% summarise(value = ifelse(all(.data$value == 0), 0, min(.data$value[.data$value > 0], na.rm = TRUE) )) %>% # a value 0 is often a sign for a NA that has been replaced with 0 for small countries - ungroup() %>% + dplyr::ungroup() %>% as.data.frame() %>% - as.quitte() %>% + quitte::as.quitte() %>% as.magpie() # Allocate minimum values to region @@ -100,7 +96,7 @@ calcEmissionFactors <- function(subtype = "emission_factors", sectoral_resolutio # read in regional map (select ISO and GAINS codes only). This is required for the construction of the SSPs - map_regions <- read.csv2( + map_regions <-utils::read.csv2( toolGetMapping(type = "regional", name = "regionmappingGAINS.csv", returnPathOnly = TRUE, where = "mrremind"), stringsAsFactors = TRUE )[, c(2, 3)] @@ -117,7 +113,7 @@ calcEmissionFactors <- function(subtype = "emission_factors", sectoral_resolutio )) %>% mutate(CountryCode = factor(.data$CountryCode)) - gdp_cap <- calcOutput("GDPpc", aggregate = FALSE)[, 2005, "gdppc_SSP2"] + gdp_cap <- calcOutput("GDPpc", scenario = "SSP2", aggregate = FALSE)[, 2005, ] #-- PROCESS DATA ------------------ vcat(2, ">> Process data... \n") diff --git a/R/calcEmissions.R b/R/calcEmissions.R index b8b38078..120462c4 100644 --- a/R/calcEmissions.R +++ b/R/calcEmissions.R @@ -6,7 +6,6 @@ #' #' @author Steve Smith, Pascal Weigmann #' -#' @importFrom utils read.csv2 calcEmissions <- function(datasource = "CEDS16") { ## ---- CEDS 16 ---- if (datasource == "CEDS16") { @@ -1002,7 +1001,7 @@ calcEmissions <- function(datasource = "CEDS16") { "NOx" map <- - read.csv2( + utils::read.csv2( toolGetMapping( type = "sectoral", name = "mappingLIMITSsectorstoREMINDsectors.csv", @@ -1041,7 +1040,7 @@ calcEmissions <- function(datasource = "CEDS16") { em_eclipse <- collapseNames(em_eclipse[, , "CLE"]) map <- - read.csv2( + utils::read.csv2( toolGetMapping( type = "sectoral", name = "mappingECLIPSEsectorstoREMINDsectors.csv", diff --git a/R/calcEuropeanEnergyDatasheets.R b/R/calcEuropeanEnergyDatasheets.R index d8ac018e..717e2014 100644 --- a/R/calcEuropeanEnergyDatasheets.R +++ b/R/calcEuropeanEnergyDatasheets.R @@ -7,9 +7,7 @@ #' @author Falk Benke #' #' @importFrom dplyr select mutate left_join -#' @importFrom rlang sym -#' @importFrom stats aggregate na.exclude - +#' calcEuropeanEnergyDatasheets <- function(subtype) { if (!subtype %in% c("EU27", "EU28")) { stop("Invalid subtype. Must be either EU27 or EU28") @@ -39,7 +37,7 @@ calcEuropeanEnergyDatasheets <- function(subtype) { mutate("value" = .data$value * .data$Conversion) %>% select("region", "year", "variable" = "REMIND", "value") - x <- aggregate(value ~ region + year + variable, x, sum, na.action = na.exclude) %>% + x <- stats::aggregate(value ~ region + year + variable, x, sum, na.action = stats::na.exclude) %>% as.magpie() %>% toolCountryFill(fill = NA, verbosity = 2) diff --git a/R/calcFE.R b/R/calcFE.R index 4860068c..26824c5e 100644 --- a/R/calcFE.R +++ b/R/calcFE.R @@ -1,8 +1,6 @@ #' Calculates FE historical from IEA energy balances, projections from EDGE, and historical #' values from IEA WEO 2019 #' @author Lavinia Baumstark, Aman Malik -#' @importFrom quitte inline.data.frame -#' @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) @@ -18,9 +16,9 @@ calcFE <- function(source = "IEA", scenario_proj = "SSP2", ieaVersion = "default name = "structuremappingIO_reporting.csv", where = "mrremind", returnPathOnly = TRUE) target <- c("output") - map <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") + map <- utils::read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") # delete NAs rows - map <- map[c("io", target)] %>% na.omit() + map <- map[c("io", target)] %>% stats::na.omit() # Change the column name of the mapping colnames(map) <- gsub("io", "names_in", colnames(map)) diff --git a/R/calcFEShares.R b/R/calcFEShares.R index f993adac..fcef0dcb 100644 --- a/R/calcFEShares.R +++ b/R/calcFEShares.R @@ -9,7 +9,7 @@ calcFEShares <- function(subtype) { edge_buildings <- calcOutput("IOEdgeBuildings", subtype = "output_EDGE_buildings", aggregate = FALSE) output <- calcOutput("IO", subtype = "output", aggregate = FALSE) - fe_demand <- calcOutput("FEdemand", aggregate = FALSE)[, , "gdp_SSP1"] + fe_demand <- calcOutput("FEdemand", aggregate = FALSE)[, , "SSP1"] fe_demand <- collapseNames(fe_demand) #---- Process Data ---- diff --git a/R/calcFEdemand.R b/R/calcFEdemand.R index 1c003a52..1703e128 100644 --- a/R/calcFEdemand.R +++ b/R/calcFEdemand.R @@ -6,10 +6,9 @@ calcFEdemand <- function() { feIndustry <- calcOutput("FeDemandIndustry", warnNA = FALSE, aggregate = FALSE) # duplicate scenarios ---- - # add Navigate and Campaigners scenarios to industry and transport to match buildings scenarios by duplication - duplicateScens <- "gdp_SSP2EU_NAV_all" - feIndustry <- mbind(feIndustry, setItems(feIndustry[, , "gdp_SSP2EU"], 3.1, duplicateScens)) + duplicateScens <- "SSP2_NAV_all" + feIndustry <- mbind(feIndustry, setItems(feIndustry[, , "SSP2"], 3.1, duplicateScens)) # add up industry and buildings contributions to stationary @@ -31,6 +30,6 @@ calcFEdemand <- function() { "ue_otherInd ($tn)" ), description = "demand pathways for final energy in buildings and industry", - structure.data = "^gdp_(SSP[1-5].*|SDP.*)\\.(fe|ue)" + structure.data = "^(SSP[1-5].*|SDP.*)\\.(fe|ue)" )) } diff --git a/R/calcFeDemandBuildings.R b/R/calcFeDemandBuildings.R index f09e516f..1c00e402 100644 --- a/R/calcFeDemandBuildings.R +++ b/R/calcFeDemandBuildings.R @@ -23,7 +23,7 @@ calcFeDemandBuildings <- function(subtype) { stationary <- toolAggregateTimeSteps(stationary) # add scenarios to stationary to match buildings scenarios by duplication - stationary <- mbind(stationary, setItems(stationary[, , "SSP2EU"], 3.1, "SSP2EU_NAV_all")) + stationary <- mbind(stationary, setItems(stationary[, , "SSP2"], 3.1, "SSP2_NAV_all")) if (subtype == "FE") { @@ -50,9 +50,7 @@ calcFeDemandBuildings <- function(subtype) { data <- mbind(stationary, buildings) # Prepare Mapping ---- - - mapping <- toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", - where = "mrcommons") + mapping <- toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", where = "mrcommons") # TODO: remove once this is in the mapping # add total buildings electricity demand: feelb = feelcb + feelhpb + feelrhb @@ -65,7 +63,7 @@ calcFeDemandBuildings <- function(subtype) { mapping <- mapping %>% select("EDGEitems", "REMINDitems_out", "weight_Fedemand") %>% - na.omit() %>% + stats::na.omit() %>% filter(.data$EDGEitems %in% getNames(data, dim = "item")) %>% distinct() @@ -80,7 +78,7 @@ calcFeDemandBuildings <- function(subtype) { filter(grepl("b$", .data$REMINDitems_out) | (grepl("s$", .data$REMINDitems_out)) & !grepl("fe(..i$|ind)", .data$EDGEitems)) remindVars <- unique(mapping$REMINDitems_out) - remindDims <- cartesian(getNames(data, dim = "scenario"), remindVars) + remindDims <- quitte::cartesian(getNames(data, dim = "scenario"), remindVars) } else { @@ -99,7 +97,7 @@ calcFeDemandBuildings <- function(subtype) { } scenarioRcp <- unique(gsub("^(.*\\..*)\\..*$", "\\1", getItems(data, dim = 3))) - remindDims <- cartesian(scenarioRcp, remindVars) + remindDims <- quitte::cartesian(scenarioRcp, remindVars) } # Apply Mapping ---- @@ -125,11 +123,6 @@ calcFeDemandBuildings <- function(subtype) { } # Prepare Output ---- - - # change the scenario names for consistency with REMIND sets - getNames(remind) <- gsub("^SSP", "gdp_SSP", getNames(remind)) - getNames(remind) <- gsub("SDP", "gdp_SDP", getNames(remind)) - # change item names back from UE to FE if (subtype == "UE_buildings") { getItems(remind, "item") <- gsub("^ue", "fe", getItems(remind, "item")) @@ -142,15 +135,14 @@ calcFeDemandBuildings <- function(subtype) { ) outputStructure <- switch(subtype, - FE = "^gdp_(SSP[1-5].*|SDP.*)\\.(fe|ue)", - FE_buildings = "^gdp_(SSP[1-5]|SDP).*\\..*\\.fe.*b$", - UE_buildings = "^gdp_(SSP[1-5]|SDP).*\\..*\\.fe.*b$" + FE = "^(SSP[1-5].*|SDP.*)\\.(fe|ue)", + FE_buildings = "^(SSP[1-5]|SDP).*\\..*\\.fe.*b$", + UE_buildings = "^(SSP[1-5]|SDP).*\\..*\\.fe.*b$" ) - return(list(x = remind, - weight = NULL, - unit = "EJ", - description = description, - structure.data = outputStructure)) - + list(x = remind, + weight = NULL, + unit = "EJ", + description = description, + structure.data = outputStructure) } diff --git a/R/calcFloorspace.R b/R/calcFloorspace.R index 307b01e4..739d8c4a 100644 --- a/R/calcFloorspace.R +++ b/R/calcFloorspace.R @@ -17,8 +17,8 @@ calcFloorspace <- function(onlyTotal = FALSE) { data <- collapseNames(data[, , "buildings"]) } - return(list(x = data, - weight = NULL, - unit = "million m2", - description = "Buildings floor space")) + list(x = data, + weight = NULL, + unit = "million m2", + description = "Buildings floor space") } diff --git a/R/calcFossilExtraction.R b/R/calcFossilExtraction.R index 4740b745..97acb124 100644 --- a/R/calcFossilExtraction.R +++ b/R/calcFossilExtraction.R @@ -9,7 +9,6 @@ #' calcOutput(type = "FossilExtraction", subtype = "FossilExtraction") #' } #' @importFrom dplyr mutate select rename arrange -#' @importFrom stats lm coef calcFossilExtraction <- function(subtype = "FossilExtraction") { if (!((subtype == "FossilExtraction") || (subtype == "UraniumExtraction"))) { @@ -74,11 +73,11 @@ calcFossilExtraction <- function(subtype = "FossilExtraction") { filter(CumRes < 6) # linear fit to extraction cost data - linfit <- lm(Price ~ poly(CumRes, 1, raw = TRUE), data = RegrData) + linfit <- stats::lm(Price ~ poly(CumRes, 1, raw = TRUE), data = RegrData) # substitute Australia gas extraction polcy coef for medium Scenario with linear fit to Dylan's data - output["AUS", , "medGas.0"] <- coef(linfit)[1] - output["AUS", , "medGas.1"] <- coef(linfit)[2] + output["AUS", , "medGas.0"] <- stats::coef(linfit)[1] + output["AUS", , "medGas.1"] <- stats::coef(linfit)[2] output["AUS", , "medGas.2"] <- 0 output["AUS", , "medGas.3"] <- 0 diff --git a/R/calcGAINS.R b/R/calcGAINS.R index 3cc506df..7ddb845f 100644 --- a/R/calcGAINS.R +++ b/R/calcGAINS.R @@ -8,37 +8,34 @@ #' #' @param subtype decides whether emissions or emission factors are returned #' @param sectoral_resolution aggreaged or extenden (uses different GAINS input data) -#' @importFrom dplyr ungroup -#' @importFrom utils read.csv read.csv2 -#' @importFrom quitte as.quitte calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "extended") { - if (!(subtype %in% c("emission_factors", "emissions"))) stop('subtype must be in c("emission_factors", "emissions")') + if (!(subtype %in% c("emission_factors", "emissions"))) { + stop('subtype must be in c("emission_factors", "emissions")') + } # local functions - - # country to region + ## country to region allocate_c2r_ef <- function(id_ef, ip_region, ip_country, ip_year, ip_scenario) { - dummy <- id_ef[ip_region, ip_year, ip_scenario] - dummy[, , ] <- setCells(id_ef[ip_country, ip_year, ip_scenario], "GLO") - return(dummy) + dummy <- id_ef[ip_region, ip_year, ip_scenario] + dummy[, , ] <- setCells(id_ef[ip_country, ip_year, ip_scenario], "GLO") + dummy } - allocate_min2r_ef <- function(id_ef, ip_region, ip_countryGroup, ip_year, ip_scenario) { dummy <- id_ef[ip_region, ip_year, ip_scenario] # Get minimum values across country group - tmp <- as.quitte(id_ef[ip_countryGroup, ip_year, ip_scenario]) %>% + tmp <- quitte::as.quitte(id_ef[ip_countryGroup, ip_year, ip_scenario]) %>% group_by(!!!syms(c("data1", "data2"))) %>% summarise(value = ifelse(all(.data$value == 0), 0, min(.data$value[.data$value > 0], na.rm = TRUE)) ) %>% # a value 0 is often a sign for a NA that has been replaced with 0 for small countries - ungroup() %>% + dplyr::ungroup() %>% as.data.frame() %>% - as.quitte() %>% + quitte::as.quitte() %>% as.magpie() # Allocate minimum values to region dummy[ip_region, ip_year, ip_scenario] <- setYears(tmp) - return(dummy) + dummy } # conversion factors @@ -46,27 +43,28 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten # user-defined parameters time <- c(seq(2005, 2055, 5), seq(2060, 2110, 10), 2130, 2150) - scenario <- c("SSP1", "SSP2", "SSP5", "FLE", "MFR", "CLE") # ,"SSP3","SSP4", "MFR_Transports", "GlobalEURO6", "FLE_building_transport", "SLCF_building_transport") + # ,"SSP3","SSP4", "MFR_Transports", "GlobalEURO6", "FLE_building_transport", "SLCF_building_transport") + scenario <- c("SSP1", "SSP2", "SSP5", "FLE", "MFR", "CLE") - p_dagg_year <- 2005 - p_dagg_pop <- "pop_SSP2" - p_dagg_gdp <- "gdp_SSP2" - - p_countryCategories <- "useGAINSregions" # "perCountry" + # "perCountry" + p_countryCategories <- "useGAINSregions" # list of OECD countries - r_oecd <- c("AUS", "AUT", "BEL", "CAN", "CHL", "CZE", "DNK", "EST", "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", - "JPN", "KOR", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT", "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA") + r_oecd <- c("AUS", "AUT", "BEL", "CAN", "CHL", "CZE", "DNK", "EST", "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", + "ISR", "ITA", "JPN", "KOR", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT", "SVK", "SVN", "ESP", "SWE", + "CHE", "TUR", "GBR", "USA") - # set of sectors for which no emission factor will be computed (because there is no activity reported, or not in terms of energy) + # set of sectors for which no emission factor will be computed (because there is no activity reported, or not in + # terms of energy) dimSector_skipEF <- c("AACID", "CEMENT", "CHEM", "CHEMBULK", "CUSM", "NACID", "PAPER", "STEEL", - "Losses_Coal", "Losses_Distribution_Use", - "Transformations_Coal", "Transformations_HLF", "Transformations_HLF_Refinery", "Transformations_NatGas") + "Losses_Coal", "Losses_Distribution_Use", "Transformations_Coal", "Transformations_HLF", + "Transformations_HLF_Refinery", "Transformations_NatGas") - dimSector_skipEF_edge <- c("End_Use_Industry_Bio_Trad", "End_Use_Industry_Coal", "End_Use_Industry_HLF", "End_Use_Industry_LLF", - "End_Use_Industry_NatGas", "End_Use_Residential_Bio_Mod", "End_Use_Residential_Bio_Trad", "End_Use_Residential_Coal", - "End_Use_Residential_HLF", "End_Use_Residential_LLF", "End_Use_Residential_NatGas", "End_Use_Services_Bio_Trad", - "End_Use_Services_Coal") + dimSector_skipEF_edge <- c("End_Use_Industry_Bio_Trad", "End_Use_Industry_Coal", "End_Use_Industry_HLF", + "End_Use_Industry_LLF", "End_Use_Industry_NatGas", "End_Use_Residential_Bio_Mod", + "End_Use_Residential_Bio_Trad", "End_Use_Residential_Coal", "End_Use_Residential_HLF", + "End_Use_Residential_LLF", "End_Use_Residential_NatGas", "End_Use_Services_Bio_Trad", + "End_Use_Services_Coal") dimSector_skipEF_edge <- c("") dimSector_skipEF <- c("") @@ -79,9 +77,9 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten emissions <- emissions[, c(2005, 2010, 2020, 2030, 2050), ] # read in regional map (select ISO and GAINS codes only). This is required for the construction of the SSPs - map_regions <- read.csv2(toolGetMapping(type = "regional", name = "regionmappingGAINS.csv", - returnPathOnly = TRUE, where = "mappingfolder"), - stringsAsFactors = TRUE)[, c(2, 3)] + map_regions <- utils::read.csv2(toolGetMapping(type = "regional", name = "regionmappingGAINS.csv", + returnPathOnly = TRUE, where = "mappingfolder"), + stringsAsFactors = TRUE)[, c(2, 3)] map_regions <- map_regions %>% filter(.data$CountryCode != "ANT") %>% # Remove Netherland Antilles (not in REMIND regional mapping) filter(.data$RegionCode != "") %>% @@ -90,18 +88,12 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten gsub("[0-9]", "", .data$RegionCode)))) %>% mutate(CountryCode = factor(.data$CountryCode)) - # read in population and GDP data. required to compute gdp per cap - pop <- calcOutput("Population", aggregate = FALSE)[, p_dagg_year, p_dagg_pop] - gdp <- calcOutput("GDP", aggregate = FALSE)[, p_dagg_year, p_dagg_gdp] - - #-- PROCESS DATA ------------------ # set of sectors for which emission factors are computed dimSector_EF <- getNames(activities)[!getNames(activities) %in% c(dimSector_skipEF, dimSector_skipEF_edge)] - # calculate gdp per capita - gdp_cap <- gdp / pop - gdp_cap[is.na(gdp_cap)] <- 0 # set NA to 0 + # Get gdp per capita + gdp_cap <- calcOutput("GDPpc", scenario = "SSP2", aggregate = FALSE)[, 2005, ] # Regional selections # select one country pertaining to WEU (all WEU countries should have the same EF). Used for SSP scenario rules @@ -109,7 +101,8 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten # Retrieve Transport names - # define missing SLE scenario (assumed to be 3/4 of the distance between CLE and MFR, according to discussion with Zig Klimont on 18th Feb 2016) + # define missing SLE scenario (assumed to be 3/4 of the distance between CLE and MFR, according to discussion with + # Zig Klimont on 18th Feb 2016) cle <- emissions[, , "CLE"] getNames(cle) <- gsub("CLE", "MFR", getNames(cle)) sle <- cle - (cle - emissions[, , "MFR"]) * 0.75 @@ -117,44 +110,54 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten emissions <- mbind(emissions, sle) rm(cle, sle) - # calculate emission factors (only for power and end-use sectors, and not empty activities) and convert from kt/PJ to Tg/Twa + # calculate emission factors (only for power and end-use sectors, and not empty activities) and convert from kt/PJ + # to Tg/Twa ef_eclipse <- emissions[, , dimSector_EF] / activities[, , dimSector_EF] * conv_kt_per_PJ_to_Tg_per_TWa getSets(ef_eclipse) <- c("region", "year", "data1", "data2", "data3") # DK: NAs in ef_eclipse: There are two potential reasons for NAs in ef_eclipse: - # 1) ef = emi / activitiy => if the activity is zero the ef gets NA. The activity is disaggregated from 24 GAINS regions to the 249 ISO - # countries using population as weight. If there is no population data for a country the activity (and also the emissions) of this country - # are zero. In this case ef for this country is NA for ALL sectors and species => Jerome's command below that collects regions in NAregions - # which have only NAs works! The NAs are replaced with ef from countries that belong to the same GAINS region and thus have identical ef. - # After replacing these kind of NAs there may remain NAs for another reason: - # 2) There is no activity data for a particular sector and GAINS region in the source data => activity will be zero for this sector and all - # ISO countries belonging to this GAINS region => ef will be NA for those sectors and countries. Set those NAs to zero. When ef is reaggregated - # to REMIND regions this is done using the activites as weight. And the activity is zero for the countries and sectors that have zeros due to zero - # activity and thus have no effect on the result. - - # some regions/countries have NA values everywhere (pop data is zero). Allocate EF of another country that belongs to the same GAINS region (except for Antartica) + # 1) ef = emi / activitiy => if the activity is zero the ef gets NA. The activity is disaggregated from 24 GAINS + # regions to the 249 ISO countries using population as weight. If there is no population data for a country the + # activity (and also the emissions) of this country are zero. In this case ef for this country is NA for ALL sectors + # and species => Jerome's command below that collects regions in NAregions which have only NAs works! The NAs are + # replaced with ef from countries that belong to the same GAINS region and thus have identical ef. After replacing + # these kind of NAs there may remain NAs for another reason: 2) There is no activity data for a particular sector and + # GAINS region in the source data => activity will be zero for this sector and all ISO countries belonging to this + # GAINS region => ef will be NA for those sectors and countries. Set those NAs to zero. When ef is reaggregated to + # REMIND regions this is done using the activites as weight. And the activity is zero for the countries and sectors + # that have zeros due to zero activity and thus have no effect on the result. + + # some regions/countries have NA values everywhere (pop data is zero). Allocate EF of another country that belongs to + # the same GAINS region (except for Antartica) # Find countries that have NA for all sectors and species (then probably due to missing population data, see above) - ef_eclipse["ATA", , ] <- 0 # Antartica -> 0 - NAregions <- c("AIA", "ALA", "ATF", "BES", "BLM", "BVT", "CCK", "COK", "CXR", "ESH", "FLK", "GGY", "GIB", "GLP", "GUF", "HMD", "IOT", "JEY", "MSR", "MTQ", "MYT", "NFK", - "NIU", "NRU", "PCN", "REU", "SGS", "SHN", "SJM", "SPM", "TKL", "TWN", "UMI", "VAT", "VGB", "WLF") + # Antartica -> 0 + ef_eclipse["ATA", , ] <- 0 + NAregions <- c("AIA", "ALA", "ATF", "BES", "BLM", "BVT", "CCK", "COK", "CXR", "ESH", "FLK", "GGY", "GIB", "GLP", + "GUF", "HMD", "IOT", "JEY", "MSR", "MTQ", "MYT", "NFK", "NIU", "NRU", "PCN", "REU", "SGS", "SHN", + "SJM", "SPM", "TKL", "TWN", "UMI", "VAT", "VGB", "WLF") MissingRegions <- c("ALA", "BES", "BLM", "CUW", "GGY", "IMN", "JEY", "MAF", "PSE", "SSD", "SXM") - # for countries that have NAs everywhere (missing population, see reason 1 above) replace NA with ef of country that belongs to same GAINS region + # for countries that have NAs everywhere (missing population, see reason 1 above) replace NA with ef of country + # that belongs to same GAINS region for (kregi in NAregions) { - # suche die countrycodes aller countries, die zu der region gehören, zu der auch kregi gehört, lasse die Regionen aus NAregions und missingRegions weg - # take the value of the first country since all countries that belong to the same GAINS region have the same ef - subsitute_region <- map_regions$CountryCode[map_regions$RegionCode == map_regions$RegionCode[map_regions$CountryCode == kregi] & - !map_regions$CountryCode %in% c(NAregions, MissingRegions)][1] + # suche die countrycodes aller countries, die zu der region gehören, zu der auch kregi gehört, lasse die Regionen + # aus NAregions und missingRegions weg take the value of the first country since all countries that belong to the + # same GAINS region have the same ef + subsitute_region <- map_regions$CountryCode[ + map_regions$RegionCode == map_regions$RegionCode[map_regions$CountryCode == kregi] & + !map_regions$CountryCode %in% c(NAregions, MissingRegions) + ][1] tmp <- ef_eclipse[subsitute_region, , ] getItems(tmp, dim = 1) <- kregi ef_eclipse[kregi, , ] <- tmp } - # for the remaining NAs (0/0 = NaN) and Infs (1/0 = Inf ) just set EF to 0 (activity levels are 0, although in some cases emissions exist) + # for the remaining NAs (0/0 = NaN) and Infs (1/0 = Inf ) just set EF to 0 (activity levels are 0, although in + # some cases emissions exist) ef_eclipse[is.na(ef_eclipse)] <- 0 ef_eclipse[is.infinite(ef_eclipse)] <- 0 rm(NAregions, MissingRegions) @@ -208,11 +211,14 @@ calcGAINS <- function(subtype = "emission_factors", sectoral_resolution = "exten }) # low income countries (using World Bank definition < 2750 US$(2010)/Cap) - r_L <- levels(map_regions$CountryCode[map_regions$RegionCode %in% names(regionMean_gdppcap[regionMean_gdppcap <= 2750])]) + r_L <- levels(map_regions$CountryCode[ + map_regions$RegionCode %in% names(regionMean_gdppcap[regionMean_gdppcap <= 2750]) + ]) # high and medium income countries r_HM <- setdiff(getRegions(ef), r_L) # High-Medium income countries with strong pollution policies in place - r_HMStrong <- map_regions$CountryCode[map_regions$RegionCode %in% c("Western Europe", "Japan")] # FIXME definition taken from JeS matlab script + # FIXME definition taken from JeS matlab script + r_HMStrong <- map_regions$CountryCode[map_regions$RegionCode %in% c("Western Europe", "Japan")] # High-Medium income countries with lower emissions goals r_HMRest <- setdiff(r_HM, r_HMStrong) } else { diff --git a/R/calcHistorical.R b/R/calcHistorical.R index 82e7154b..096f26b6 100644 --- a/R/calcHistorical.R +++ b/R/calcHistorical.R @@ -1,7 +1,5 @@ #' Gather reference data from various sources. #' @importFrom dplyr filter group_by mutate select ungroup -#' @importFrom rlang syms -#' @importFrom tidyr complete nesting pivot_wider calcHistorical <- function() { # Final Energy @@ -105,7 +103,7 @@ calcHistorical <- function() { variable = "Production|Industry|Cement (Mt/yr)" ) %>% select("iso3c", "year", "model", "variable", "value") %>% - complete( + tidyr::complete( iso3c = unname(getISOlist()), year = unique(.data$year), fill = list( @@ -134,7 +132,7 @@ calcHistorical <- function() { ) %>% # kt/year * 1e-3 Mt/kt = Mt/year mutate(value = .data$value * 1e-3) %>% - pivot_wider(values_fill = 0) %>% + tidyr::pivot_wider(values_fill = 0) %>% mutate( `Production|Industry|Steel (Mt/yr)` = .data$`Production in Oxygen-Blown Converters` + .data$`Production in Open Hearth Furnaces` @@ -159,12 +157,12 @@ calcHistorical <- function() { "Production|Industry|Steel|Primary (Mt/yr)", "Production|Industry|Steel|Secondary (Mt/yr)" ) %>% - pivot_longer(c( + tidyr::pivot_longer(c( "Production|Industry|Steel (Mt/yr)", "Production|Industry|Steel|Primary (Mt/yr)", "Production|Industry|Steel|Secondary (Mt/yr)" )) %>% - complete(nesting(!!!syms(c("year", "source", "name"))), + tidyr::complete(tidyr::nesting(!!!syms(c("year", "source", "name"))), iso3c = toolGetMapping( name = getConfig("regionmapping"), type = "regional", where = "mappingfolder" diff --git a/R/calcIEA_ETP.R b/R/calcIEA_ETP.R index 7394e1b9..1c66945f 100644 --- a/R/calcIEA_ETP.R +++ b/R/calcIEA_ETP.R @@ -5,11 +5,9 @@ #' #' @author Falk Benke #' -#' @importFrom dplyr select mutate left_join %>% if_any filter all_of -#' @importFrom madrat toolGetMapping -#' @importFrom utils read.csv2 +#' @importFrom dplyr select mutate left_join filter #' @export - +#' calcIEA_ETP <- function() { .map <- function(x, mapping) { @@ -40,7 +38,7 @@ calcIEA_ETP <- function() { ) %>% select("region", "year", "model", "variable" = "REMIND", "value") - x <- aggregate(value ~ region + year + model + variable, data, sum) %>% + x <- stats::aggregate(value ~ region + year + model + variable, data, sum) %>% as.magpie() return(x) @@ -51,9 +49,9 @@ calcIEA_ETP <- function() { map <- toolGetMapping("regionmappingIEA_ETP.csv", where = "mrremind", type = "regional", returnPathOnly = TRUE) %>% - read.csv2(check.names = FALSE) + utils::read.csv2(check.names = FALSE) keepCountries <- map %>% - filter(if_any(all_of(c("individual", keepRegions)))) %>% + filter(dplyr::if_any(tidyselect::all_of(c("individual", keepRegions)))) %>% getElement("CountryCode") x[keepCountries, , ] } @@ -117,15 +115,10 @@ calcIEA_ETP <- function() { } } - - - - return(list( - x = dataReg, - weight = NULL, - aggregationFunction = .customAggregate, - aggregationArguments = list(glo = dataGlo), - unit = c("EJ/yr", "Mt CO2/yr", "Mt/yr", "bn pkm/yr", "bn tkm/yr"), - description = "IEA ETP projections as REMIND variables" - )) + list(x = dataReg, + weight = NULL, + aggregationFunction = .customAggregate, + aggregationArguments = list(glo = dataGlo), + unit = c("EJ/yr", "Mt CO2/yr", "Mt/yr", "bn pkm/yr", "bn tkm/yr"), + description = "IEA ETP projections as REMIND variables") } diff --git a/R/calcIO.R b/R/calcIO.R index 776302bc..ea907ff4 100644 --- a/R/calcIO.R +++ b/R/calcIO.R @@ -21,10 +21,7 @@ #' a <- calcOutput("IO", subtype = "output") #' } #' -#' @importFrom rlang .data is_empty #' @importFrom dplyr filter mutate -#' @importFrom tidyr unite -#' @importFrom tidyselect all_of calcIO <- function(subtype = c("input", "output", "output_biomass", "trade", "input_Industry_subsectors", "output_Industry_subsectors"), ieaVersion = "default") { @@ -85,7 +82,7 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade", # read in data and convert from ktoe to EJ data <- readSource("IEA", subtype = ieaSubtype) * 4.1868e-5 - ieamatch <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") + ieamatch <- utils::read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") # add total buildings electricity demand (feelb = feelcb + feelhpb + feelrhb) if (subtype == "output") { @@ -104,18 +101,18 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade", new_product_flows <- tibble( text = setdiff(getNames(data), names_data_before) ) %>% - separate("text", c("product", "flow"), sep = "\\.") %>% - anti_join( + tidyr::separate("text", c("product", "flow"), sep = "\\.") %>% + dplyr::anti_join( ieamatch %>% as_tibble() %>% select(product = "iea_product", flow = "iea_flows"), c("product", "flow") ) %>% - unite("text", c("product", "flow"), sep = ".") %>% + tidyr::unite("text", c("product", "flow"), sep = ".") %>% pull("text") - if (!is_empty(new_product_flows)) { + if (!rlang::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") @@ -133,10 +130,10 @@ calcIO <- function(subtype = c("input", "output", "output_biomass", "trade", # delete NAs rows ieamatch <- ieamatch %>% as_tibble() %>% - 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 = ".") %>% + select(tidyselect::all_of(c("iea_product", "iea_flows", "Weight", target))) %>% + stats::na.omit() %>% + tidyr::unite("target", tidyselect::all_of(target), sep = ".", remove = FALSE) %>% + tidyr::unite("product.flow", c("iea_product", "iea_flows"), sep = ".") %>% filter(!!sym("product.flow") %in% getNames(data)) magpieNames <- ieamatch[["target"]] %>% unique() diff --git a/R/calcIntertempElastSubst.R b/R/calcIntertempElastSubst.R index 759979fc..8bb00df6 100644 --- a/R/calcIntertempElastSubst.R +++ b/R/calcIntertempElastSubst.R @@ -1,22 +1,16 @@ - - - calcIntertempElastSubst <- function() { - - # read data - x <- readSource("ExpertGuess",subtype="ies") + + x <- readSource("ExpertGuess", subtype = "ies") getNames(x) <- NULL getYears(x) <- NULL - - # read data used for weight - pop <- calcOutput("Population",aggregate=FALSE)[,2005,"pop_SSP2"] - gdp <- calcOutput("GDP",aggregate=FALSE)[,2005,"gdp_SSP2"] - # use half pop and half gdp - w <- (pop / dimSums(pop,dim=1)) + setNames(gdp / dimSums(gdp,dim=1),NULL) - - - return(list(x = x, - weight = w, - unit = "dimensionless", - description = "intertemporal elasticity of substitution")) + + # Use half pop and half gdp for weight + pop <- calcOutput("Population", scenario = "SSP2", naming = "scenario", aggregate = FALSE)[, 2005, ] + gdp <- calcOutput("GDP", scenario = "SSP2", naming = "scenario", aggregate = FALSE)[, 2005, ] + w <- pop / dimSums(pop, dim = 1) + gdp / dimSums(gdp, dim = 1) + + list(x = x, + weight = w, + unit = "dimensionless", + description = "Expert guesses for the intertemporal elasticity of substitution") } diff --git a/R/calcJRC_IDEES.R b/R/calcJRC_IDEES.R index b21e132c..16069f2a 100644 --- a/R/calcJRC_IDEES.R +++ b/R/calcJRC_IDEES.R @@ -10,11 +10,8 @@ #' #' @author Falk Benke #' -#' #' @importFrom dplyr select mutate left_join -#' @importFrom rlang sym -#' @importFrom stats aggregate - +#' calcJRC_IDEES <- function(subtype) { subtypes <- c("Industry", "Transport", "ResCom") @@ -68,7 +65,7 @@ calcJRC_IDEES <- function(subtype) { "REMIND_variable" = paste0(!!sym("REMIND_variable"), " (", !!sym("Unit_REMIND"), ")")) %>% select("variable" = "REMIND_variable", "region", "year", "value") - x <- aggregate(value ~ variable + region + year, x, sum) %>% + x <- stats::aggregate(value ~ variable + region + year, x, sum) %>% as.magpie() %>% toolCountryFill(fill = NA, verbosity = 2) %>% toolFillEU34Countries() diff --git a/R/calcMacroInvestments.R b/R/calcMacroInvestments.R index d17a2642..5eeb3613 100644 --- a/R/calcMacroInvestments.R +++ b/R/calcMacroInvestments.R @@ -1,9 +1,19 @@ +#' Calculate 2005 macroeconomic capital investments +#' +#' Compute macroeconomic capital investments based on investments shares from the PWT and GDP scenarios from +#' [mrdrivers]. The final investments are the product of the two. +#' +#' @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 calcMacroInvestments <- function() { - # Read in investment share and SSP2 GDP in 2005 + # Read-in 2005 investment share and SSP2 GDP shInv <- readSource("PWT")[, 2005, "csh_i"] - gdp <- calcOutput("GDP", scenario = "SSP2", years = 2005, aggregate = FALSE) - getItems(gdp, dim = 2) <- "y2005" + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE)[, 2005, ] shInv_new <- toolFillWithRegionAvg(shInv, valueToReplace = 0, weight = gdp) @@ -16,10 +26,9 @@ calcMacroInvestments <- function() { getYears(data) <- NULL getNames(data) <- NULL - return(list( - x = data, - weight = NULL, - unit = "trillion US$2017", - description = "Investments in the macro-economic capital stock in constant 2017 US$MER" - )) + list(x = data, + weight = NULL, + unit = glue::glue("trillion US${mrdrivers::toolGetUnitDollar(returnOnlyBase = TRUE)}"), + description = glue::glue("Investments in the macro-economic capital stock computed using the investment/GDP \\ + ratio from the PWT, and GDP scenarios from mrdrivers.")) } diff --git a/R/calcPE.R b/R/calcPE.R index 81c817e9..67e48df1 100644 --- a/R/calcPE.R +++ b/R/calcPE.R @@ -16,9 +16,9 @@ calcPE <- function(subtype = "IEA", ieaVersion = "default") { target <- c("input") ### calculate data - map <- read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") + map <- utils::read.csv2(mapping, stringsAsFactors = FALSE, na.strings = "") # delete NAs rows - map <- map[c("io", target)] %>% na.omit() + map <- map[c("io", target)] %>% stats::na.omit() # select data that have names map <- map[map$io %in% getNames(data), ] diff --git a/R/calcPlasticsEoL.R b/R/calcPlasticsEoL.R index 969fc775..cccf085c 100644 --- a/R/calcPlasticsEoL.R +++ b/R/calcPlasticsEoL.R @@ -10,10 +10,7 @@ #' @seealso [`calcOutput()`] #' #' @importFrom dplyr filter pull select -#' @importFrom rlang .data #' @importFrom tibble as_tibble -#' @importFrom quitte calc_addVariable -#' #' @export #' calcPlasticsEoL <- function() { @@ -37,13 +34,13 @@ calcPlasticsEoL <- function() { x <- x[, , selection, pmatch = TRUE] - y <- as.quitte(x) + y <- quitte::as.quitte(x) # calculate total plastic waste plasticsEoL <- y %>% # we could have switches to include a circular scenario but it would probably # require a different scenario to calibrate filter(!!sym("scenario") %in% c("SSP2")) %>% - calc_addVariable( + quitte::calc_addVariable( "Plastics|Waste|Total" = " `Plastics|Waste|Buildings & Construction` + `Plastics|Waste|Consumer Products` + @@ -59,7 +56,7 @@ calcPlasticsEoL <- function() { # calculate share (0 to 1) of waste that gets incinerated incinerationShares <- plasticsEoL %>% - calc_addVariable( + quitte::calc_addVariable( "Plastics|End of Life|Incineration share" = " `Plastics|End of Life|Waste to Energy`/ `Plastics|Waste|Total` @@ -68,7 +65,7 @@ calcPlasticsEoL <- function() { ) %>% # remove unused dimensions select(-"model", -"scenario", -"variable", -"unit") %>% - interpolate_missing_periods(seq(2050, 2060, 5), method = "linear") %>% + quitte::interpolate_missing_periods(seq(2050, 2060, 5), method = "linear") %>% suppressWarnings() # as magpie diff --git a/R/calcProjectPipelines.R b/R/calcProjectPipelines.R index 1a458ea7..b299c21a 100644 --- a/R/calcProjectPipelines.R +++ b/R/calcProjectPipelines.R @@ -34,7 +34,7 @@ calcProjectPipelines <- function(subtype) { getItems(eu27Pool, dim = 1) <- "EU27" # distribute EU27 pool to the countries according to GDP - gdp <- calcOutput("GDP", aggregate = FALSE)[eu27, 2020, "gdp_SSP2EU"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE)[eu27, 2020, ] eu27Pool <- toolAggregate(eu27Pool, rel = mapping, weight = gdp, from = "EU27_map", to = "CountryCode") diff --git a/R/calcRiskPremium.R b/R/calcRiskPremium.R index 8cd4a219..1e9c777c 100644 --- a/R/calcRiskPremium.R +++ b/R/calcRiskPremium.R @@ -9,8 +9,8 @@ calcRiskPremium <- function() { getNames(x) <- NULL getYears(x) <- NULL - # Use GDP as weight - w <- calcOutput("GDP", aggregate = FALSE)[, 2005, "gdp_SSP2"] + # Use GDP in 2005 as weight + w <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE)[, 2005, ] list(x = x, weight = w, diff --git a/R/calcSolar.R b/R/calcSolar.R index 5605f728..5c866f54 100644 --- a/R/calcSolar.R +++ b/R/calcSolar.R @@ -6,14 +6,8 @@ #' @author Julian Oeser, modified by Renato Rodrigues #' @seealso \code{\link{calcOutput}} #' @examples -#' #' \dontrun{ a <- calcOutput(type="Solar") #' } -#' -#' -#' @importFrom utils head - - calcSolar <- function() { x <- readSource("DLR") diff --git a/R/calcTheil.R b/R/calcTheil.R index e8dba0c8..cc7536b9 100644 --- a/R/calcTheil.R +++ b/R/calcTheil.R @@ -34,10 +34,28 @@ calcTheil <- function() { # later, as it is used in the calculations of the Theil contribution and weights. s <- getNames(Gini) y <- getYears(Gini) - gdp <- calcOutput("GDP", naming = "scenario", extension2150 = "constant", years = y, aggregate = FALSE)[, , s] - gdpReg <- calcOutput("GDP", naming = "scenario", extension2150 = "constant", years = y)[, , s] - gdppc <- calcOutput("GDPpc", naming = "scenario", extension2150 = "constant", years = y, aggregate = FALSE)[, , s] - gdppcReg <- calcOutput("GDPpc", naming = "scenario", extension2150 = "constant", years = y)[, , s] + gdp <- calcOutput("GDP", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + extension2150 = "constant", + years = y, + aggregate = FALSE)[, , s] + gdpReg <- calcOutput("GDP", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + extension2150 = "constant", + years = y)[, , s] + gdppc <- calcOutput("GDPpc", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + extension2150 = "constant", + years = y, + aggregate = FALSE)[, , s] + gdppcReg <- calcOutput("GDPpc", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + extension2150 = "constant", + years = y)[, , s] # Allocate empty objects for storing Theil contribution and weights contribTheilT <- TheilT @@ -57,11 +75,6 @@ calcTheil <- function() { stopifnot(max(abs(dimSums(weight[rrCountries, , ], dim = 1) - 1)) < 1e-10) } - # For easier REMIND integration use same names as GDP scenarios for Theil - # Change this if we later want to test effect of per capita income growth vs. inequality - getNames(contribTheilT) <- paste0("gdp_", getNames(contribTheilT)) - getNames(weight) <- paste0("gdp_", getNames(weight)) - list(x = contribTheilT, weight = weight, unit = "-", diff --git a/R/calcTimePref.R b/R/calcTimePref.R index 026604b8..136d3ead 100644 --- a/R/calcTimePref.R +++ b/R/calcTimePref.R @@ -1,19 +1,13 @@ calcTimePref <- function() { - # read data + x <- readSource("ExpertGuess", subtype = "prtp") getNames(x) <- NULL getYears(x) <- NULL - # read data used for weight - pop <- calcOutput("Population", aggregate = FALSE)[, 2005, "pop_SSP2"] - gdp <- calcOutput("GDP", aggregate = FALSE)[, 2005, "gdp_SSP2"] - # use half pop and half gdp - w <- (pop / dimSums(pop, dim = 1)) + setNames(gdp / dimSums(gdp, dim = 1), NULL) + # Use half pop and half gdp for weight + pop <- calcOutput("Population", scenario = "SSP2", naming = "scenario", aggregate = FALSE)[, 2005, ] + gdp <- calcOutput("GDP", scenario = "SSP2", naming = "scenario", aggregate = FALSE)[, 2005, ] + w <- pop / dimSums(pop, dim = 1) + gdp / dimSums(gdp, dim = 1) - return(list( - x = x, - weight = w, - unit = "dimensionless", - description = "pure rate of time preference" - )) + list(x = x, weight = w, unit = "dimensionless", description = "pure rate of time preference") } diff --git a/R/calcTransportGDPshare.R b/R/calcTransportGDPshare.R index 35f6c271..447c13b0 100644 --- a/R/calcTransportGDPshare.R +++ b/R/calcTransportGDPshare.R @@ -51,12 +51,12 @@ calcTransportGDPshare <- function() { share <- collapseDim(share) - weight <- calcOutput("GDP", aggregate = FALSE, years = getYears(share, as.integer = TRUE))[, , "gdp_SSP2"] + weight <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE, years = getYears(share, as.integer = TRUE)) weight <- collapseDim(weight) - #Returning capacity values - return(list(x = share, weight = weight, - unit = "percentage", - description = "transport share in GDP" - )) + # Returning capacity values + list(x = share, + weight = weight, + unit = "percentage", + description = "transport share in GDP") } diff --git a/R/calcUBA.R b/R/calcUBA.R index adbaa15e..114796cb 100644 --- a/R/calcUBA.R +++ b/R/calcUBA.R @@ -6,8 +6,6 @@ #' @author Falk Benke #' #' @importFrom dplyr select mutate left_join filter -#' @importFrom rlang sym -#' @importFrom stats aggregate #' @export calcUBA <- function() { uba <- readSource("UBA") @@ -31,7 +29,7 @@ calcUBA <- function() { mutate("REMIND_variable" = paste0(!!sym("REMIND_variable"), " (", !!sym("Unit_REMIND"), ")")) %>% select("variable" = "REMIND_variable", "region", "year", "value") - x <- aggregate(value ~ variable + region + year, x, sum) %>% + x <- stats::aggregate(value ~ variable + region + year, x, sum) %>% as.magpie() %>% toolCountryFill(fill = NA, verbosity = 2) diff --git a/R/calcUNFCCC.R b/R/calcUNFCCC.R index c68bb22a..c114b972 100644 --- a/R/calcUNFCCC.R +++ b/R/calcUNFCCC.R @@ -5,7 +5,6 @@ #' #' @author Falk Benke, Pascal Weigmann #' @importFrom dplyr select mutate left_join -#' @importFrom stats aggregate #' @export calcUNFCCC <- function() { @@ -38,7 +37,7 @@ calcUNFCCC <- function() { ) %>% select("variable" = "REMIND", "region", "year", "value") - x <- aggregate(value ~ variable + region + year, x, sum) %>% + x <- stats::aggregate(value ~ variable + region + year, x, sum) %>% as.magpie() %>% toolCountryFill(fill = NA, verbosity = 2) diff --git a/R/convertCDIAC.R b/R/convertCDIAC.R index ce49e84a..2853ef82 100644 --- a/R/convertCDIAC.R +++ b/R/convertCDIAC.R @@ -11,7 +11,7 @@ convertCDIAC <- function(x) { # CDIAC(XIT) -> ISO(ITA + SMR) # CDIAC(XFR) -> ISO(FRA + MCO) m <- matrix(c(c("XIT", "XIT", "XFR", "XFR"), c("ITA", "SMR", "FRA", "MCO")), 4) - w <- calcOutput("Population", years = 2005, aggregate = FALSE)[c("ITA", "SMR", "FRA", "MCO"), , "pop_SSP2"] + w <- calcOutput("Population", scenario = "SSP2", years = 2005, aggregate = FALSE)[c("ITA", "SMR", "FRA", "MCO"), , ] x_split <- toolAggregate(x[c("XIT", "XFR"), , ], m, weight = w) # delete XIT and XFR from x diff --git a/R/convertECLIPSE.R b/R/convertECLIPSE.R index fd651bd9..b11380aa 100755 --- a/R/convertECLIPSE.R +++ b/R/convertECLIPSE.R @@ -1,15 +1,8 @@ -#' @importFrom dplyr min_rank -#' @importFrom quitte as.quitte - convertECLIPSE <- function(x, subtype) { # Parameter definitions downscaling <- TRUE - # TODO: Switch to EDGAR data - p_dagg_year <- 2005 - p_dagg_pop <- "pop_SSP2" - # Initialisation # Local functions closest <- function(xv, xv2, fun.ties = min, place = "all") { @@ -57,19 +50,19 @@ convertECLIPSE <- function(x, subtype) { keepNames <- getSets(mdata)[3:length(getSets(mdata))] tmp <- mdata %>% - as.quitte() + quitte::as.quitte() tmp <- tmp[, colSums(is.na(tmp)) < nrow(tmp)] grp_cols <- setdiff(colnames(tmp), c("value", "period")) output <- tmp %>% - as.quitte() %>% + quitte::as.quitte() %>% # mutate(period = as.integer(format(period,"%Y"))) %>% mutate(value = ifelse(.data$value == 0, NA, .data$value)) %>% # group_by(region, sector,variable,scenario) %>% group_by(!!!syms(grp_cols)) %>% - mutate(rank = min_rank(.data$period), + mutate(rank = dplyr::min_rank(.data$period), value = ifelse( all(is.na(.data$value)) | !any(is.na(.data$value)) | !is.na(.data$value), @@ -84,7 +77,7 @@ convertECLIPSE <- function(x, subtype) { ungroup() %>% select(-'rank') %>% as.data.frame() %>% - as.quitte() + quitte::as.quitte() output <- output[c(setdiff(colnames(output), c(keepNames, "value")), keepNames, "value")] # ensure that the order of the names is the same as in mdata @@ -108,7 +101,7 @@ convertECLIPSE <- function(x, subtype) { returnPathOnly = TRUE, where = "mappingfolder") # Get GAINS regional mapping - map <- read.csv2(m) + map <- utils::read.csv2(m) map <- map[!(map$RegionCode == "" | map$CountryCode == "ANT"), c(2, 3)] map <- map %>% mutate(RegionCode = gsub("\\ \\+", "\\+", gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$RegionCode)))) @@ -116,7 +109,7 @@ convertECLIPSE <- function(x, subtype) { map$RegionCode <- factor(map$RegionCode) # TODO: Use EDGAR data as weight - w <- calcOutput("Population", aggregate = FALSE)[levels(map$CountryCode), p_dagg_year, p_dagg_pop] + w <- calcOutput("Population", scenario = "SSP2", aggregate = FALSE)[levels(map$CountryCode), 2005, ] x <- toolAggregate(x[, , ], map, weight = w) # fill all missing countries with 0 (add Antarctica) @@ -135,7 +128,7 @@ convertECLIPSE <- function(x, subtype) { returnPathOnly = TRUE, where = "mappingfolder") # Get GAINS regional mapping - map <- read.csv2(m) + map <- utils::read.csv2(m) map <- map[!(map$RegionCode == "" | map$CountryCode == "ANT"), c(2, 3)] map <- map %>% mutate(RegionCode = gsub("\\ \\+", "\\+", gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$RegionCode)))) @@ -143,7 +136,7 @@ convertECLIPSE <- function(x, subtype) { map$RegionCode <- factor(map$RegionCode) # TODO: Use EDGAR data as weight - w <- calcOutput("Population", aggregate = FALSE)[levels(map$CountryCode), p_dagg_year, p_dagg_pop] + w <- calcOutput("Population", scenario = "SSP2", aggregate = FALSE)[levels(map$CountryCode), 2005, ] x <- toolAggregate(x[, , ], map, weight = w) # fill all missing countries with 0 (add Antarctica) diff --git a/R/convertEDGETransport.R b/R/convertEDGETransport.R index 4aac2792..ab0c03b3 100644 --- a/R/convertEDGETransport.R +++ b/R/convertEDGETransport.R @@ -3,33 +3,44 @@ #' @param subtype REMIND/iterative EDGE-T input data subtypes #' @param x MAgPIE object containing EDGE-T values in 21 region resolution #' @return REMIND/iterative EDGE-T input data as MAgPIE object disaggregated to ISO level -#' @importFrom data.table setDT -#' @importFrom madrat toolAggregate #' @author Johanna Hoppe - +#' convertEDGETransport <- function(x, subtype) { + # TMP: changes in edgeTransport required. + getNames(x) <- gsub("gdp_", "", getNames(x)) + RegionCode <- CountryCode <- . <- NULL - mappingfile <- setDT(toolGetMapping("regionmapping_21_EU11.csv", type = "regional", - where = "mappingfolder"))[, .(iso = CountryCode, region = RegionCode)] + mappingfile <- data.table::setDT(toolGetMapping("regionmapping_21_EU11.csv", type = "regional", + where = "mappingfolder"))[, .(iso = CountryCode, region = RegionCode)] if (subtype %in% c("f35_demByTech", "f29_trpdemand", "weightESdemand")) { - gdp <- calcOutput("GDP", aggregate = FALSE) |> time_interpolate(getYears(x), extrapolation_type = "constant") - gdp <- gdp[, , "gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) |> + time_interpolate(getYears(x), extrapolation_type = "constant") result <- toolAggregate(x = x, weight = gdp, rel = mappingfile, from = "region", to = "iso") + } else if (!subtype == "shares_LDV_transport") { result <- toolAggregate(x = x, rel = mappingfile, weight = NULL, from = "region", to = "iso") + } else if (subtype %in% c("shares_LDV_transport")) { ## only the first EDGE-T scenario for SSP2 is used as a proxy for the LDV shares - x <- x[, , "gdp_SSP2.Mix1.gdp_SSP2.share_LDV_totliq.shares_LDV_transport"] + x <- x[, , "SSP2.Mix1.SSP2.share_LDV_totliq.shares_LDV_transport"] for (year in getYears(x, as.integer = TRUE)) { x[, year, ] <- as.vector(x[, c(2010), ]) + ((0.55 - as.vector(x[, c(2010), ])) / (2100 - 2010)) * (year - 2010) } + # extending values - x <- time_interpolate(x, integrate_interpolated_years = TRUE, interpolated_year = seq(from = 1990, to = 2100), extrapolation_type = "linear") - x <- time_interpolate(x, integrate_interpolated_years = TRUE, interpolated_year = c(seq(from = 1970, to = 1989), seq(from = 2101, to = 2150)), extrapolation_type = "constant") + x <- time_interpolate(x, + integrate_interpolated_years = TRUE, + interpolated_year = seq(from = 1990, to = 2100), + extrapolation_type = "linear") + x <- time_interpolate(x, + integrate_interpolated_years = TRUE, + interpolated_year = c(seq(from = 1970, to = 1989), seq(from = 2101, to = 2150)), + extrapolation_type = "constant") result <- x } + return(result) } diff --git a/R/convertEdgeBuildings.R b/R/convertEdgeBuildings.R index c6a2ef1e..371b25ec 100644 --- a/R/convertEdgeBuildings.R +++ b/R/convertEdgeBuildings.R @@ -5,13 +5,12 @@ #' @return EDGE data as MAgPIE object aggregated to country level #' @author Antoine Levesque, Robin Hasse #' - convertEdgeBuildings <- function(x, subtype = "FE") { #---- Functions ------------- noYearDim <- function(x) setYears(x, NULL) addSSPnames <- function(x) { - do.call("mbind", lapply(c(paste0("SSP", c(1:5, "2EU", "2_lowEn", "2_highDemDEU", "2EU_NAV_all")), + do.call("mbind", lapply(c(paste0("SSP", c(1:5, "2_lowEn", "2_highDemDEU", "2_NAV_all")), paste0("SDP", c("", "_EI", "_RC", "_MC"))), function(s) setNames(x, paste(s, getNames(x), sep = ".")) )) @@ -20,8 +19,7 @@ convertEdgeBuildings <- function(x, subtype = "FE") { duplScens <- function(x, scens = NULL) { if (is.null(scens)) { scens <- list( - gdp_SSP2EU = "gdp_SSP2EU_NAV_all", - gdp_SSP2 = c("gdp_SSP2_lowEn", "gdp_SSP2_highDemDEU") + SSP2 = c("SSP2_lowEn", "SSP2_highDemDEU", "SSP2_NAV_all") ) } mbind(x, do.call(mbind, lapply(names(scens), function(from) { @@ -48,7 +46,7 @@ convertEdgeBuildings <- function(x, subtype = "FE") { exceeding_years_before <- exceeding_years_vec[exceeding_years_vec <= threshold] exceeding_years_after <- exceeding_years_vec[exceeding_years_vec > threshold] lambda <- c(rep(0, length(previous_years)), - tail(seq(0, 1, length.out = length(exceeding_years_before) + 1), -1), + utils::tail(seq(0, 1, length.out = length(exceeding_years_before) + 1), -1), rep(1, length(exceeding_years_after))) names(lambda) <- as.character(c(previous_years, exceeding_years_vec)) return(as.magpie(lambda)) @@ -60,7 +58,7 @@ convertEdgeBuildings <- function(x, subtype = "FE") { struct_mapping_path <- toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", returnPathOnly = TRUE, where = "mrcommons") - struct_mapping <- read.csv2(struct_mapping_path, na.strings = "") + struct_mapping <- utils::read.csv2(struct_mapping_path, na.strings = "") # Select the relevant part of the mapping struct_mapping <- struct_mapping[!is.na(struct_mapping$weight_convertEDGE), ] @@ -81,17 +79,16 @@ convertEdgeBuildings <- function(x, subtype = "FE") { mappingfile <- toolGetMapping(type = "regional", name = "regionmappingEDGE.csv", returnPathOnly = TRUE, where = "mappingfolder") - mapping <- read.csv2(mappingfile) + mapping <- utils::read.csv2(mappingfile) region_col <- which(names(mapping) == "RegionCodeEUR_ETP") iso_col <- which(names(mapping) == "CountryCode") #--- Load the Weights #--- First load the GDP data. Set average2020 to False to get yearly data as far as possible. - wg <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) + wg <- calcOutput("GDP", scenario = c("SSPs", "SDPs"), naming = "scenario", average2020 = FALSE, aggregate = FALSE) # duplicate SSP2 for SSP2_lowEn and SSP2_highDemDEU for Navigate and Campaigners scenarios wg <- duplScens(wg) - getNames(wg) <- gsub("gdp_", "", getNames(wg)) #--- Then load the final energy data hist_fe_stationary <- calcOutput("IOEdgeBuildings", subtype = "output_EDGE", aggregate = FALSE) @@ -110,7 +107,8 @@ convertEdgeBuildings <- function(x, subtype = "FE") { } # Select last year of X available in the historical data set - maxYear_X_in_FE <- max(getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) %in% getYears(wfe, as.integer = TRUE)]) + maxYear_X_in_FE <- max(getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) %in% + getYears(wfe, as.integer = TRUE)]) # Deduce the scenario periods exceeding_years <- getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) > maxYear_X_in_FE] @@ -161,7 +159,7 @@ convertEdgeBuildings <- function(x, subtype = "FE") { # Attribute the growth in water heating demand of the EDGE Region OCD to TUR, # and retrieve it from AUS, CAN, CHE (Swiss), NOR, NZL # For SSP1, SSP2 and SDP - names_2_change <- grep("(SSP1|SSP2|SDP|SDP_EI|SDP_RC|SDP_MC|SSP2EU).*water_heating", getNames(result), value = TRUE) + names_2_change <- grep("(SSP1|SSP2|SDP|SDP_EI|SDP_RC|SDP_MC).*water_heating", getNames(result), value = TRUE) names_2_change_elec <- grep("elec", names_2_change, value = TRUE) regs_OCD <- c("AUS", "CAN", "CHE", "NOR", "NZL") @@ -169,25 +167,30 @@ convertEdgeBuildings <- function(x, subtype = "FE") { end_of_history <- 2015 scenario_time <- getYears(result, TRUE)[getYears(result, TRUE) > end_of_history] - WH_growth <- result[regs_OCD, scenario_time, names_2_change] - dimReduce(result[regs_OCD, end_of_history, names_2_change]) + WH_growth <- result[regs_OCD, scenario_time, names_2_change] - + dimReduce(result[regs_OCD, end_of_history, names_2_change]) WH_growth[, , names_2_change_elec] <- WH_growth[, , names_2_change_elec] * 0.5 WH_growth[WH_growth < 0] <- 0 WH_growth_agg <- dimSums(WH_growth, dim = 1) - result[getRegions(WH_growth), getYears(WH_growth), getNames(WH_growth)] <- result[getRegions(WH_growth), getYears(WH_growth), getNames(WH_growth)] - WH_growth - result[reg_TUR, getYears(WH_growth), getNames(WH_growth)] <- result[reg_TUR, getYears(WH_growth), getNames(WH_growth)] + WH_growth_agg + result[getRegions(WH_growth), getYears(WH_growth), getNames(WH_growth)] <- + result[getRegions(WH_growth), getYears(WH_growth), getNames(WH_growth)] - WH_growth + result[reg_TUR, getYears(WH_growth), getNames(WH_growth)] <- + result[reg_TUR, getYears(WH_growth), getNames(WH_growth)] + WH_growth_agg } else if (subtype == "Floorspace") { mappingfile <- toolGetMapping(type = "regional", name = "regionmappingEDGE.csv", returnPathOnly = TRUE, where = "mappingfolder") - mapping <- read.csv2(mappingfile) + mapping <- utils::read.csv2(mappingfile) region_col <- which(names(mapping) == "RegionCodeEUR_ETP") iso_col <- which(names(mapping) == "CountryCode") - getNames(x) <- paste0("gdp_", getNames(x)) - wp <- calcOutput("Population", years = rem_years_hist, aggregate = FALSE) + wp <- calcOutput("Population", + scenario = c("SSPs", "SDPs"), + naming = "scenario", + years = rem_years_hist, + aggregate = FALSE) getSets(wp) <- gsub("variable", "scenario", getSets(wp)) - getItems(wp, "scenario") <- gsub("pop_", "gdp_", getItems(wp, "scenario")) # duplicate SSP2 for SSP2_lowEn and SSP2_highDemDEU for Navigate and Campaigners scenarios wp <- duplScens(wp) @@ -197,5 +200,6 @@ convertEdgeBuildings <- function(x, subtype = "FE") { from = region_col, to = iso_col) result <- x } - return(result) + + result } diff --git a/R/convertEuropeanEnergyDatasheets.R b/R/convertEuropeanEnergyDatasheets.R index 9633ea4b..76f5c8b4 100644 --- a/R/convertEuropeanEnergyDatasheets.R +++ b/R/convertEuropeanEnergyDatasheets.R @@ -13,7 +13,7 @@ #' } #' convertEuropeanEnergyDatasheets <- function(x, subtype) { - iso3 <- read.csv2("isotwo2iso3Mapping.csv", stringsAsFactors = FALSE) + iso3 <-utils::read.csv2("isotwo2iso3Mapping.csv", stringsAsFactors = FALSE) getItems(x, dim = 1) <- sapply(getRegions(x), function(y) iso3[which(iso3[, 1] == y), 2]) # fill up zero countries diff --git a/R/convertGini.R b/R/convertGini.R deleted file mode 100644 index c47808b4..00000000 --- a/R/convertGini.R +++ /dev/null @@ -1,53 +0,0 @@ -#' Convert Gini -#' -#' Converts Gini data from readGini() to ISO country level. -#' Countries missing in the original data set will have their Gini set to zero ( -#' a very small number for numerical reasons to be precise). -#' The original data range is 2011-2100 in one-year steps, -#' here we extend it to 2000-2150 in 5-year steps. -#' Values before (after) the original range are held fixed at 2011 (2100) levels. -#' Gini values for the SDP scenario are taken from the SSP1 scenario -#' -#' -#' @param x MAgPIE object containing Gini data with World Bank codes, 2011-2100, in percent (range 0-100) -#' @return MAgPIE object of the Gini data in ISO countries, range 0-1 -#' @author Bjoern Soergel -#' @seealso \code{\link{readSource}} \code{\link{readGini}} -#' @examples -#' \dontrun{ -#' a <- convertGini(x) -#' } -convertGini <- function(x) { - # add SDP scenario, with same data as SSP1 - xSDP <- x[, , "SSP1"] - getNames(xSDP) <- "SDP" - x <- mbind(x, xSDP) - - # add SSP2EU, same data as SSP2 - xSSP2EU <- x[, , "SSP2"] - getNames(xSSP2EU) <- "SSP2EU" - x <- mbind(x, xSSP2EU) - - # conversion to iso3c codes, match Somalia by hand because countrycode fails - getItems(x, dim = 1) <- countrycode::countrycode(getRegions(x), - origin = "wb", - destination = "iso3c", - custom_match = c("SOM" = "SOM")) - getSets(x)[1] <- "iso3c" - - # fill missing countries with a small number (not zero because this might cause numerical problems in Gini aggregation later) - x <- toolCountryFill(x, fill = 1e-8, verbosity = 2) - - # fill years outside original range - # NB: time_interpolate breaks set names, so restore them explicitly - xsets <- getSets(x) - x <- time_interpolate(x, interpolated_year = c(seq(2000, 2010, by = 5), seq(2105, 2150, by = 5)), - integrate_interpolated_years = TRUE, extrapolation_type = "constant") - x <- x[, seq(2000, 2150, by = 5), ] - getSets(x) <- xsets - - # convert to 0..1 range - x <- x / 100 - - return(x) -} diff --git a/R/convertGlobalCCSinstitute.R b/R/convertGlobalCCSinstitute.R index 6373603b..71d1631c 100644 --- a/R/convertGlobalCCSinstitute.R +++ b/R/convertGlobalCCSinstitute.R @@ -1,14 +1,9 @@ #' Convert Global CCS Institute Project Database #' #' @md -#' @param x A [`magpie`][magclass::magclass] object returned by -#' [readGlobalCCSinstitute()]. +#' @param x A [`magpie`][magclass::magclass] object returned by [readGlobalCCSinstitute()]. #' @inherit readGlobalCCSinstitute -#' #' @return A [`magpie`][magclass::magclass] object. -#' -#' @importFrom quitte add_countrycode_ madrat_mule -#' #' @export convertGlobalCCSinstitute <- function(x, subtype = '08-09-2017') { if ('08-09-2017' == subtype) { # 08-09-2017 ---- diff --git a/R/convertIEA_ETP.R b/R/convertIEA_ETP.R index d6ea60bd..5e243fc3 100644 --- a/R/convertIEA_ETP.R +++ b/R/convertIEA_ETP.R @@ -2,17 +2,10 @@ #' #' @author Falk Benke, Robin Hasse #' - #' @param x IEA ETP projection magpie object derived from readIEA_ETP function #' @param subtype data subtype. Either "industry", "buildings", "summary", or "transport" -#' -#' @importFrom magclass mselect time_interpolate collapseDim dimSums getItems -#' getItems<- mbind -#' @importFrom dplyr %>% -#' @importFrom madrat calcOutput readSource toolGetMapping -#' @importFrom utils read.csv2 #' @export - +#' convertIEA_ETP <- function(x, subtype) { # nolint: object_name_linter. # FUNCTIONS ------------------------------------------------------------------ @@ -63,7 +56,7 @@ convertIEA_ETP <- function(x, subtype) { # nolint: object_name_linter. map <- toolGetMapping("regionmappingIEA_ETP.csv", where = "mrremind", type = "regional", returnPathOnly = TRUE) %>% - read.csv2(check.names = FALSE) + utils::read.csv2(check.names = FALSE) fe <- calcOutput("FE", source = "IEA", aggregate = FALSE) %>% mselect(d3 = switch(subtype, diff --git a/R/convertIEA_WEO.R b/R/convertIEA_WEO.R index df687c48..bdca0e6b 100644 --- a/R/convertIEA_WEO.R +++ b/R/convertIEA_WEO.R @@ -12,7 +12,6 @@ #' a <- convertWEO(x, subtype = "Capacity") #' } #' -#' @importFrom tidyr gather #' @importFrom readxl read_excel convertIEA_WEO <- function(x, subtype) { diff --git a/R/convertLIMITS.R b/R/convertLIMITS.R index a7bb3066..0e4d32f7 100755 --- a/R/convertLIMITS.R +++ b/R/convertLIMITS.R @@ -1,45 +1,21 @@ convertLIMITS <- function(x, subtype) { - - # Parameter definitions - # TODO: Is it consistent to disaggregate activity and emission data using 2005 SSP2 pop data? - p_dagg_year <- 2005 - p_dagg_pop <- "pop_SSP2" - # Check errors - if (is.null(subtype)) stop("Please provide one of the following subtypes: 'activities' or 'emissions'") - - # For now, this is useless as the processing for activities and emissions is the same - if (subtype == "activities") { - - m <- toolGetMapping(type = "regional", name = "regionmappingTIMER.csv", - returnPathOnly = TRUE, where = "mappingfolder") - - # Get TIMER regional mapping - map <- read.csv2(m) - map <- map[!(map$RegionCode == "" | map$CountryCode == "ANT"), c(2, 3)] - map$CountryCode <- factor(map$CountryCode) - map$RegionCode <- factor(map$RegionCode) - - w <- calcOutput("Population", aggregate = FALSE)[levels(map$CountryCode), p_dagg_year, p_dagg_pop] - x <- toolAggregate(x[, , ], map, weight = w) + if (is.null(subtype)) { + stop("Please provide one of the following subtypes: 'activities' or 'emissions'") } - if (subtype == "emissions") { - - m <- toolGetMapping(type = "regional", name = "regionmappingTIMER.csv", - returnPathOnly = TRUE, where = "mappingfolder") + m <- toolGetMapping(type = "regional", name = "regionmappingTIMER.csv", returnPathOnly = TRUE, where = "mappingfolder") - map <- read.csv2(m) - map <- map[!(map$RegionCode == "" | map$CountryCode == "ANT"), c(2, 3)] - map$CountryCode <- factor(map$CountryCode) - map$RegionCode <- factor(map$RegionCode) + # Get TIMER regional mapping + map <- utils::read.csv2(m) + map <- map[!(map$RegionCode == "" | map$CountryCode == "ANT"), c(2, 3)] + map$CountryCode <- factor(map$CountryCode) + map$RegionCode <- factor(map$RegionCode) - w <- calcOutput("Population", aggregate = FALSE)[levels(map$CountryCode), p_dagg_year, p_dagg_pop] - x <- toolAggregate(x[, , ], map, weight = w) - } + # TODO: Is it consistent to disaggregate activity and emission data using 2005 SSP2 pop data? + w <- calcOutput("Population", scenario = "SSP2", aggregate = FALSE)[levels(map$CountryCode), 2005, ] + x <- toolAggregate(x[, , ], map, weight = w) # fill all missing countries with 0 - x <- toolCountryFill(x, fill = 0, verbosity = 2) - - return(x) + toolCountryFill(x, fill = 0, verbosity = 2) } diff --git a/R/convertNREL.R b/R/convertNREL.R index 462fbf8d..18418de4 100644 --- a/R/convertNREL.R +++ b/R/convertNREL.R @@ -14,23 +14,19 @@ convertNREL <- function(x) { # rename countries with ISO-code getItems(x, dim = 1) <- toolCountry2isocode(getItems(x, dim = 1)) - #-------------------- allocation of aggregations ------------------------------- + # Allocation of aggregations # "ANT" -> "SXM", "CUW", "BES" # "YUG" -> "SRB", "MNE" # YUG seems to be only these two, "SVN", "HRV", "MKD", "BIH" m <- matrix(c( c("ANT", "ANT", "ANT", "YUG", "YUG"), # ,"YUG","YUG","YUG","YUG"), c("SXM", "CUW", "BES", "SRB", "MNE") ), 5) # ,"SVN","HRV","MKD","BIH")),9) - w <- calcOutput("Population", aggregate = FALSE)[m[, 2], 2010, "pop_SSP2"] + w <- calcOutput("Population", scenario = "SSP2", aggregate = FALSE)[m[, 2], 2010, ] x_split <- toolAggregate(x[c("ANT", "YUG"), , ], m, weight = w) # delete ANT and YUG from x x <- x[c("ANT", "YUG"), , invert = TRUE] x <- mbind(x, x_split) - #-------------------------------------------------------------------------------- - # fill all missing countries with 0 - x <- toolCountryFill(x, fill = 0, verbosity = 2) - - return(x) + toolCountryFill(x, fill = 0, verbosity = 2) } diff --git a/R/convertProdShares.R b/R/convertProdShares.R index ae0c92a3..86de94f5 100644 --- a/R/convertProdShares.R +++ b/R/convertProdShares.R @@ -10,13 +10,13 @@ convertProdShares <- function(x) { mapping <- toolGetMapping("regionmappingH12.csv", type = "regional", where = "mappingfolder") # weight needed for disaggregation - wt <- calcOutput(type = "GDP", aggregate = F) + wt <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) # Disaggregating "Europe" in spv mapping_eur <- mapping[mapping$RegionCode == "EUR", ] mapping_eur$RegionCode <- "Europe" - wt_eur <- wt[mapping_eur$CountryCode, 2020, "gdp_SSP2"] + wt_eur <- wt[mapping_eur$CountryCode, 2020, ] x2 <- toolAggregate(x = x["Europe", "y2018", "spv"], rel = mapping_eur, weight = wt_eur, from = "CountryCode", to = "RegionCode") # Disaggregating "Other" in spv @@ -24,7 +24,7 @@ convertProdShares <- function(x) { other_cont <- data.frame(setdiff(mapping[mapping$RegionCode != "EUR", ]$CountryCode, toolCountry2isocode(getRegions(spv[which(spv > 0)])))) other_cont$Region <- "Other" colnames(other_cont)[1] <- "Country" - wt_other <- wt[other_cont$Country, "y2020", "gdp_SSP2"] + wt_other <- wt[other_cont$Country, "y2020", ] x3 <- toolAggregate(x = spv["Other", , ], rel = other_cont, from = "Country", to = "Region", weight = wt_other) # Disaggregating "Other" in wind @@ -35,7 +35,7 @@ convertProdShares <- function(x) { other_cont <- data.frame(setdiff(mapping$CountryCode, toolCountry2isocode(getRegions(wind[which(wind > 0)])))) other_cont$Region <- "Other" colnames(other_cont)[1] <- "Country" - wt_other <- wt[other_cont$Country, "y2020", "gdp_SSP2"] + wt_other <- wt[other_cont$Country, "y2020", ] x4 <- toolAggregate(x = wind["Other", , ], rel = other_cont, from = "Country", to = "Region", weight = wt_other) # creating copy of x without aggregated regions and countries as ISO and not NA diff --git a/R/convertRCP.R b/R/convertRCP.R index ad22847e..1167ee4e 100644 --- a/R/convertRCP.R +++ b/R/convertRCP.R @@ -34,7 +34,7 @@ convertRCP <- function(x, subtype) { returnPathOnly = TRUE, where = "mappingfolder" ) - mapping <- read.csv2(mappingfile, stringsAsFactors = FALSE) + mapping <- utils::read.csv2(mappingfile, stringsAsFactors = FALSE) countries <- mapping$CountryCode weights <- weights[countries, , ] diff --git a/R/convertREMIND_11Regi.R b/R/convertREMIND_11Regi.R index a9eef327..4ae83685 100644 --- a/R/convertREMIND_11Regi.R +++ b/R/convertREMIND_11Regi.R @@ -18,13 +18,13 @@ convertREMIND_11Regi <- function(x,subtype) { # No weighting for spatial aggregation y <- toolAggregate(x, "regionmappingREMIND.csv", weight=NULL) } else if (subtype == "AP_starting_values") { - pop <- calcOutput("Population",years=2005,aggregate=FALSE)[,,"pop_SSP2"] + pop <- calcOutput("Population", scenario = "SSP2", years = 2005, aggregate = FALSE) y <- toolAggregate(x,"regionmappingREMIND.csv",weight=pop) } else if (subtype == "deltacapoffset") { fe <- dimSums(calcOutput("IO",subtype="output",aggregate=FALSE)[,2010,c("feelb","feeli")],dim=3) y <- toolAggregate(x,"regionmappingREMIND.csv",weight=fe) } else if (subtype == "nashWeight") { - gdp <- calcOutput("GDP",years=2005,aggregate=FALSE)[,,"gdp_SSP2"] + gdp <- calcOutput("GDP", scenario = "SSP2", years = 2005, aggregate = FALSE) y <- toolAggregate(x,"regionmappingREMIND.csv",weight=gdp) } else if (subtype=="capacityFactorRules" | subtype == "taxConvergence" | subtype == "maxFeSubsidy" | subtype == "maxPeSubsidy" | subtype == "propFeSubsidy") { diff --git a/R/convertREN21.R b/R/convertREN21.R index 1d39dea8..bd779b30 100644 --- a/R/convertREN21.R +++ b/R/convertREN21.R @@ -6,7 +6,8 @@ #' @param subtype Only "Capacity" asof now #' @return Magpie object with Total Installed Capacity targets. The target years differ depending upon the database. #' @author Aman Malik - +#' @importFrom utils read.csv +#' convertREN21 <- function(x, subtype) { if (subtype == "Capacity") { x <- magpiesort(x) # sorting years chronologically and region names alphabetically diff --git a/R/convertStationary.R b/R/convertStationary.R index 936af846..4e5156ed 100644 --- a/R/convertStationary.R +++ b/R/convertStationary.R @@ -8,7 +8,7 @@ convertStationary <- function(x) { noYearDim <- function(x) setYears(x, NULL) addSSPnames <- function(x) { - do.call("mbind", lapply(c(paste0("SSP", c(1:5, "2EU", "2_lowEn", "2_highDemDEU")), + do.call("mbind", lapply(c(paste0("SSP", c(1:5, "2_lowEn", "2_highDemDEU")), paste0("SDP", c("", "_EI", "_RC", "_MC"))), function(s) setNames(x, paste(s, getNames(x), sep = ".")) )) @@ -31,7 +31,7 @@ convertStationary <- function(x) { exceeding_years_before <- exceeding_years_vec[exceeding_years_vec <= threshold] exceeding_years_after <- exceeding_years_vec[exceeding_years_vec > threshold] lambda <- c(rep(0, length(previous_years)), - tail(seq(0, 1, length.out = length(exceeding_years_before) + 1), -1), + utils::tail(seq(0, 1, length.out = length(exceeding_years_before) + 1), -1), rep(1, length(exceeding_years_after))) names(lambda) <- as.character(c(previous_years, exceeding_years_vec)) return(as.magpie(lambda)) @@ -42,7 +42,7 @@ convertStationary <- function(x) { struct_mapping_path <- toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", returnPathOnly = TRUE, where = "mrcommons") - struct_mapping <- read.csv2(struct_mapping_path, na.strings = "") + struct_mapping <- utils::read.csv2(struct_mapping_path, na.strings = "") # Select the relevant part of the mapping struct_mapping <- struct_mapping[!is.na(struct_mapping$weight_convertEDGE), ] @@ -59,14 +59,13 @@ convertStationary <- function(x) { mappingfile <- toolGetMapping(type = "regional", name = "regionmappingREMIND.csv", returnPathOnly = TRUE, where = "mappingfolder") - mapping <- read.csv2(mappingfile) + mapping <- utils::read.csv2(mappingfile) region_col <- which(names(mapping) == "RegionCode") iso_col <- which(names(mapping) == "CountryCode") #--- Load the Weights #--- First load the GDP data. Set average2020 to False to get yearly data as far as possible. - wg <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) - getNames(wg) <- gsub("gdp_", "", getNames(wg)) + wg <- calcOutput("GDP", scenario = c("SSPs", "SDPs"), naming = "scenario", average2020 = FALSE, aggregate = FALSE) # duplicate SSP2 for SSP2_lowEn and SSP2_highDemDEU wg <- mbind( @@ -92,7 +91,8 @@ convertStationary <- function(x) { } # Select last year of X available in the historical data set - maxYear_X_in_FE <- max(getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) %in% getYears(wfe, as.integer = TRUE)]) + maxYear_X_in_FE <- max(getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) %in% + getYears(wfe, as.integer = TRUE)]) # Deduce the scenario periods exceeding_years <- getYears(x, as.integer = TRUE)[getYears(x, as.integer = TRUE) > maxYear_X_in_FE] @@ -101,7 +101,9 @@ convertStationary <- function(x) { # For historical years, the data is substituted. For projections years, there is first a transition period, # before the FE_stationary projections are fully taken up - fe_stationary <- time_interpolate(hist_fe_stationary[, getYears(hist_fe_stationary)[getYears(hist_fe_stationary, TRUE) <= maxYear_X_in_FE], ], # The years exceeding maxYear might not be meaningful. Therefore we exclude them + # The years exceeding maxYear might not be meaningful. Therefore we exclude them + helper <- getYears(hist_fe_stationary)[getYears(hist_fe_stationary, TRUE) <= maxYear_X_in_FE] + fe_stationary <- time_interpolate(hist_fe_stationary[, helper, ], interpolated_year = c(maxYear_X_in_FE, exceeding_years), integrate_interpolated_years = TRUE, extrapolation_type = "constant") @@ -114,14 +116,16 @@ convertStationary <- function(x) { # Item names differ slightly for the input of EDGE_stationary (fe_stationary) and the output # The main issue concerns transport. We therefore restrict to the variables of interest in each data set of # historical data - stationary_items <- grep("^(fenon|feagr|feind|feoth)", getNames(x, TRUE)[[2]], value = TRUE) # Stationary, non-buildings names + # Stationary, non-buildings names + stationary_items <- grep("^(fenon|feagr|feind|feoth)", getNames(x, TRUE)[[2]], value = TRUE) # create lambda vector that gives 0 to the historical data and 1 after 2030 lambda <- calcLambda(exceeding_years, 2030, getYears(x)[getYears(x, TRUE) <= maxYear_X_in_FE]) # Replace - x[, , stationary_items] <- fe_stationary[, getYears(x), stationary_items] * (1 - lambda) + x[, , stationary_items] * lambda + x[, , stationary_items] <- fe_stationary[, getYears(x), stationary_items] * (1 - lambda) + + x[, , stationary_items] * lambda # Scale GDP and FE weights so that they can be added wg <- wg / dimSums(wg, dim = 1, na.rm = TRUE) @@ -153,9 +157,7 @@ convertStationary <- function(x) { wfe <- wfe[, getYears(x), getNames(x, dim = "item")] # Disaggregate and fill the gaps - xadd <- toolAggregate(x, mappingfile, weight = wfe, - from = region_col, - to = iso_col) + xadd <- toolAggregate(x, mappingfile, weight = wfe, from = region_col, to = iso_col) result <- toolCountryFill(xadd, 0, verbosity = 2) return(result) diff --git a/R/convertUNFCCC_NDC.R b/R/convertUNFCCC_NDC.R index b62bd4f1..82c1074f 100644 --- a/R/convertUNFCCC_NDC.R +++ b/R/convertUNFCCC_NDC.R @@ -226,7 +226,7 @@ convertUNFCCC_NDC <- function(x, subtype) { # nol # # Making sure that targets in subsequent years are always same or greater than the proceeding year for (r in getItems(x_mod5, dim = "region")) { for (t in techList) { - for (i in head(targetYears, -1)) { + for (i in utils::head(targetYears, -1)) { if (x_capacity[r, i + 5, t] < setYears(x_capacity[r, i, t])) { x_capacity[r, i + 5, t] <- setYears(x_capacity[r, i, t]) } else { @@ -273,7 +273,7 @@ convertUNFCCC_NDC <- function(x, subtype) { # nol "Emi|CH4|Waste (Mt CH4/yr)")], dim = 3) # Future GDP values - gdp <- calcOutput("GDP", aggregate = FALSE) + gdp <- calcOutput("GDP", scenario = c("SSPs", "SDPs"), naming = "scenario", aggregate = FALSE) # Define EU countries + croatia for special treatment because of joint NDC EUR_NDC_countries <- c("POL", "CZE", "ROU", "BGR", "HUN", "SVK", "LTU", "EST", "SVN", diff --git a/R/downloadNIR.R b/R/downloadNIR.R index 1d7aa9a1..4a05cec8 100644 --- a/R/downloadNIR.R +++ b/R/downloadNIR.R @@ -1,7 +1,5 @@ -#' @importFrom utils download.file tail unzip - downloadNIR <- function(subtype=NULL) { - + links <- c("http://unfccc.int/files/national_reports/annex_i_ghg_inventories/national_inventories_submissions/application/zip/aus-2015-crf-27may.zip", "http://unfccc.int/files/national_reports/annex_i_ghg_inventories/national_inventories_submissions/application/zip/aut-2015-crf-5nov15.zip", "http://unfccc.int/files/national_reports/annex_i_ghg_inventories/national_inventories_submissions/application/zip/blr-2015-crf-7jul16.zip", @@ -42,21 +40,21 @@ downloadNIR <- function(subtype=NULL) { "http://unfccc.int/files/national_reports/annex_i_natcom/status_of_submission_of_natcom_under_the_kp/application/zip/tur-2015-crf_regen-12nov15.zip", "http://unfccc.int/files/national_reports/annex_i_ghg_inventories/national_inventories_submissions/application/zip/ukr-2015-crf-14jun16.zip", "http://unfccc.int/files/national_reports/annex_i_ghg_inventories/national_inventories_submissions/application/zip/gbr-2015-crf-30oct15.zip") - - + + ### download files - fnames <- sapply(links, function(x){tail(strsplit(x, split = "/")[[1]], 1)}) - - lapply(1:length(links), FUN = function(x){ download.file(links[x], destfile=fnames[x], mode="wb")}) - + fnames <- sapply(links, function(x){utils::tail(strsplit(x, split = "/")[[1]], 1)}) + + lapply(1:length(links), FUN = function(x){ utils::download.file(links[x], destfile=fnames[x], mode="wb")}) + ### unzip files zipfiles <- list.files(pattern=".zip$") - lapply(zipfiles, unzip) + lapply(zipfiles, utils::unzip) lapply(zipfiles, unlink) - + ### delete unwanted files outfiles <- list.files(pattern="^[A-Z]{3}_[0-9]{4}_[0-9]{4}_.*\\.xlsx") allfiles <- list.files() unlink(allfiles[which(!(allfiles %in% outfiles))]) - + } diff --git a/R/fullREMIND.R b/R/fullREMIND.R index 9d243404..a745acbb 100644 --- a/R/fullREMIND.R +++ b/R/fullREMIND.R @@ -1,13 +1,11 @@ #' fullREMIND #' -#' Function that produces the complete regional data set required for the -#' REMIND model. -#' @importFrom madrat madratAttach -#' @importFrom magrittr %>% -#' @importFrom quitte cartesian madrat_mule +#' Function that produces the complete regional data set required for the REMIND model. +#' #' @author Lavinia Baumstark #' @seealso -#' \code{\link{readSource}},\code{\link{getCalculations}},\code{\link{calcOutput}} +#' \code{\link{readSource}}, \code{\link{getCalculations}}, \code{\link{calcOutput}} +#' @export #' @examples #' \dontrun{ #' fullREMIND() @@ -17,26 +15,61 @@ fullREMIND <- function() { rem_years <- seq(2005, 2150, 5) rem_years_hist <- seq(1990, 2150, 5) + gdpPopScen <- c("SSPs", "SDPs", "SSP2IndiaDEAs") #-------------- macro-economic parameters ----------------------------------------------------------- - calcOutput("Population", years = rem_years_hist, round = 8, file = "f_pop.cs3r") - calcOutput("Labour", years = rem_years_hist, round = 8, file = "f_lab.cs3r") - calcOutput("GDP", years = rem_years_hist, round = 8, file = "f_gdp.cs3r") + calcOutput("Population", + scenario = gdpPopScen, + naming = "scenario", + years = rem_years_hist, + round = 8, + file = "f_pop.cs3r") + + calcOutput("Population", + scenario = gdpPopScen, + naming = "scenario", + aggregate = FALSE, + years = rem_years_hist, + round = 8, + file = "f50_pop.cs3r") + + calcOutput("Labour", + scenario = gdpPopScen, + naming = "scenario", + years = rem_years_hist, + round = 8, + file = "f_lab.cs3r") + + calcOutput("GDP", + scenario = gdpPopScen, + naming = "scenario", + years = rem_years_hist, + round = 8, + file = "f_gdp.cs3r") + + calcOutput("GDP", + scenario = gdpPopScen, + naming = "scenario", + aggregate = FALSE, + years = rem_years_hist, + round = 8, + file = "f50_gdp.cs3r") + calcOutput("RatioPPP2MER", round = 8, file = "pm_shPPPMER.cs4r") calcOutput("MacroInvestments", round = 8, file = "p01_boundInvMacro.cs4r") calcOutput("FETaxes", subtype = "taxes", round = 2, file = "f21_tau_fe_tax.cs4r") calcOutput("FETaxes", subtype = "subsidies", round = 2, file = "f21_tau_fe_sub.cs4r") - - calcOutput("TaxConvergence", subtype = "taxConvergence", round = 2, - file = "f21_tax_convergence.cs4r") - calcOutput("TaxConvergence", subtype = "taxConvergenceRollback", round = 2, + calcOutput("TaxConvergence", subtype = "taxConvergence", round = 2, file = "f21_tax_convergence.cs4r") + calcOutput("TaxConvergence", + subtype = "taxConvergenceRollback", + round = 2, file = "f21_tax_convergence_rollback.cs4r") - calcOutput("TaxLimits", subtype = "maxFeSubsidy", round = 2, file = "f21_max_fe_sub.cs4r") calcOutput("TaxLimits", subtype = "maxPeSubsidy", round = 2, file = "f21_max_pe_sub.cs4r") calcOutput("TaxLimits", subtype = "propFeSubsidy", round = 2, file = "f21_prop_fe_sub.cs4r") calcOutput("PETaxes", subtype = "subsidies", round = 2, file = "f21_tau_pe_sub.cs4r") calcOutput("Capital", signif = 4, file = "f29_capitalQuantity.cs4r") + calcOutput("ExogDemScen", round = 8, file = "p47_exogDemScen.cs4r") # exogenous demand scenarios activated by cm_exogDem_scen calcOutput( type = "Steel_Projections", subtype = "secondary.steel.max.share", @@ -45,7 +78,7 @@ fullREMIND <- function() { China_Production = readSource(type = "ExpertGuess", subtype = "Chinese_Steel_Production", convert = FALSE) %>% - madrat_mule()) + quitte::madrat_mule()) calcOutput("FEdemand", signif = 4, file = "f_fedemand.cs4r") @@ -63,8 +96,6 @@ fullREMIND <- function() { calcOutput("NetForeignAsset", round = 6, file = "pm_nfa_start.cs4r") calcOutput("Theil", round = 8, file = "f_ineqTheil.cs4r") calcOutput("DevelopmentState", round = 4, file = "f_developmentState.cs3r") - calcOutput("Population", years = rem_years_hist, round = 8, file = "f50_pop.cs3r", aggregate = FALSE) - calcOutput("GDP", years = rem_years_hist, round = 8, file = "f50_gdp.cs3r", aggregate = FALSE) calcOutput("TCdamage", subtype = "const", round = 8, file = "f50_TC_df_const.cs4r", aggregate = FALSE) calcOutput("TCdamage", subtype = "tasK", round = 8, file = "f50_TC_df_tasK.cs4r", aggregate = FALSE) calcOutput("KLWdamage", subtype = "beta1", round = 8, file = "f50_KLW_df_beta1.cs4r", aggregate = FALSE) diff --git a/R/fullTHRESHOLDS.R b/R/fullTHRESHOLDS.R index 0501f7d3..37f6c87f 100644 --- a/R/fullTHRESHOLDS.R +++ b/R/fullTHRESHOLDS.R @@ -169,7 +169,7 @@ fullTHRESHOLDS <- function(type = "config") { # write report containing all available data, including all statuses and # thresholds attached to "variable" outfile <- "pipelines.mif" - as.quitte(out) %>% + quitte::as.quitte(out) %>% mutate("variable" = paste(.data$variable, .data$status, sep = "|")) %>% select(-"scenario", -"status") %>% as.magpie() %>% @@ -180,8 +180,8 @@ fullTHRESHOLDS <- function(type = "config") { # (as used in a validationConfig) outfile <- "thresholds.mif" out <- out[, , c("min_", "max_"), pmatch = TRUE] %>% - as.quitte() %>% - pivot_wider(names_from = "status") %>% + quitte::as.quitte() %>% + tidyr::pivot_wider(names_from = "status") %>% select(-"scenario") # exclude rows without any threshold out[!(is.na(out$min_red) diff --git a/R/readAGEB.R b/R/readAGEB.R index 795a7c86..1e8415f2 100644 --- a/R/readAGEB.R +++ b/R/readAGEB.R @@ -10,9 +10,8 @@ #' @importFrom readxl read_xlsx #' @importFrom tibble tibble #' @importFrom dplyr bind_rows select filter mutate -#' @importFrom rlang sym -#' #' @export +#' readAGEB <- function(subtype = "balances") { if (!subtype %in% c("balances", "electricity")) { stop("Invalid subtype. Must be either balances or electricity") diff --git a/R/readAriadneDB.R b/R/readAriadneDB.R index 2fb5e7dd..5e11f1e8 100644 --- a/R/readAriadneDB.R +++ b/R/readAriadneDB.R @@ -4,14 +4,12 @@ #' #' @return A [`magpie`][magclass::magclass] object. #' @author Felix Schreyer -#' @importFrom tidyr gather #' @importFrom dplyr filter mutate -#' @importFrom readxl read_excel - +#' readAriadneDB <- function() { # read in file - data <- read_excel( + data <- readxl::read_excel( "IIASA_DB_Complete_06_03_2024.xlsx", sheet = "data", col_types = c( @@ -22,7 +20,7 @@ readAriadneDB <- function() { # rearrange and convert to magclass object out <- data %>% - gather("period", "value", -"model", -"scenario", -"region", -"variable", -"unit") %>% + tidyr::gather("period", "value", -"model", -"scenario", -"region", -"variable", -"unit") %>% filter(!is.na(.data$value)) %>% as.magpie(temporal = 6, spatial = 3, datacol = 7) diff --git a/R/readDLR.R b/R/readDLR.R index 4bb290b0..f8af409f 100644 --- a/R/readDLR.R +++ b/R/readDLR.R @@ -1,5 +1,3 @@ -#' @importFrom utils read.table - readDLR <- function() { countries <- c( "Afghanistan", @@ -222,7 +220,7 @@ readDLR <- function() { year <- "y2010" ### quality bin - bins <- read.table("FLh_bins_TriebFormula_2013_11_26.txt", col.names = c("PV", "CSP")) + bins <- utils::read.table("FLh_bins_TriebFormula_2013_11_26.txt", col.names = c("PV", "CSP")) ### define which columns to read, psv: odd columns, csp: even columns (exclude first two and last two) colClasses.df <- data.frame(PV = character(428), CSP = character(428)) @@ -250,7 +248,7 @@ readDLR <- function() { bin <- bins[, technology] - out <- read.csv2(x, header = FALSE, colClasses = colClasses.df[, technology]) + out <- utils::read.csv2(x, header = FALSE, colClasses = colClasses.df[, technology]) colnames(out) <- iso.countries out$type <- type out$technology <- technology diff --git a/R/readECLIPSE.R b/R/readECLIPSE.R index f6e0cdb0..7ec3e7d1 100755 --- a/R/readECLIPSE.R +++ b/R/readECLIPSE.R @@ -17,8 +17,9 @@ readECLIPSE <- function(subtype) { ap <- ap[!is.na(ap[[1]]),] ap <- ap %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% rename(region = 'Region', sector = 'Sector', unit = 'Unit') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% filter(!.data$sector %in% c(rm_sectors, rm_unwanted), @@ -39,8 +40,9 @@ readECLIPSE <- function(subtype) { ap <- ap[!is.na(ap[[1]]),] ap <- ap %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% rename(region = 'Region', sector = 'Sector', unit = 'Unit') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% filter(!.data$sector %in% c(rm_sectors, rm_unwanted), @@ -63,8 +65,9 @@ readECLIPSE <- function(subtype) { sheet = s) out <- out[!is.na(out[[1]]), ] out <- out %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% rename(region = 'Region', sector = 'Sector') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% @@ -83,8 +86,9 @@ readECLIPSE <- function(subtype) { out <- read_excel("EMISSIONS_EMF30_aggregated_Ev5a_MFR_Nov2015.xlsx", sheet=s) out <- out[!is.na(out[[1]]),] out <- out %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% rename(region = 'Region', sector = 'Sector') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% mutate(variable = s) %>% @@ -117,7 +121,7 @@ readECLIPSE <- function(subtype) { out <- read_excel("EMISSIONS_EMF30_extended_Ev5a_CLE_Nov2015.xlsx", sheet=s) out <- out[!is.na(out[[1]]),] out <- out %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year') %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year') %>% rename(region = 'Region', sector = 'Sector') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% mutate(variable = s) %>% @@ -133,7 +137,7 @@ readECLIPSE <- function(subtype) { out <- read_excel("EMISSIONS_EMF30_extended_Ev5a_MFR_Nov2015.xlsx", sheet=s) out <- out[!is.na(out[[1]]),] out <- out %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year') %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year') %>% rename(region = 'Region', sector = 'Sector') %>% mutate(region = gsub("^\\s+|\\s+$", "", gsub("[0-9]", "", .data$region))) %>% mutate(variable = s) %>% diff --git a/R/readEDGAR7Fgases.R b/R/readEDGAR7Fgases.R index 0b4f8717..b088c7f4 100644 --- a/R/readEDGAR7Fgases.R +++ b/R/readEDGAR7Fgases.R @@ -2,28 +2,18 @@ #' @md #' #' @return A magpie object with F-gases emissions per gas species and per country -#' #' @author Gabriel Abrahao -#' -#' @importFrom readxl read_xlsx -#' @importFrom tidyr pivot_longer -#' @importFrom dplyr starts_with #' @export readEDGAR7Fgases <- function() { - intable <- read_excel( - "EDGAR_F-gases_1990-2021.xlsx", - sheet = "TOTALS BY COUNTRY", skip = 10 - ) + intable <- readxl::read_excel("EDGAR_F-gases_1990-2021.xlsx", sheet = "TOTALS BY COUNTRY", skip = 10) intable <- intable[, -c(1, 2, 4)] colnames(intable)[1:2] <- c("CountryCode", "Gas") longtable <- tidyr::pivot_longer(intable, - cols = starts_with("Y_"), - names_prefix = "Y_", - names_to = "period", - values_to = "value" - ) + cols = tidyselect::starts_with("Y_"), + names_prefix = "Y_", + names_to = "period", + values_to = "value") - x <- as.magpie(longtable) - return(x) + as.magpie(longtable) } diff --git a/R/readEDGETransport.R b/R/readEDGETransport.R index c2189b1d..b4a46b62 100644 --- a/R/readEDGETransport.R +++ b/R/readEDGETransport.R @@ -12,9 +12,8 @@ #' \dontrun{ #' a <- readSource(type = "EDGETransport") #' } -#' @importFrom tidyr expand_grid #' @importFrom dplyr bind_rows -#' @importFrom data.table data.table rbindlist := setnames setkeyv +#' @importFrom data.table := readEDGETransport <- function(subtype) { @@ -27,13 +26,13 @@ readEDGETransport <- function(subtype) { allScens <- bind_rows( ## for all "default" SSP variants we ship the whole zoo of standard ## EDGE-T scenarios - expand_grid( + tidyr::expand_grid( SSPscen = c("SSP1", "SSP2", "SSP3", "SSP5", "SDP"), transportPolScen = c("Mix1", "Mix2", "Mix3", "Mix4"), isICEban = c(TRUE, FALSE), demScen = c("default")), # Specific project scenarios - tribble( + tibble::tribble( ~SSPscen, ~transportPolScen, ~isICEban, ~demScen, "SSP2", "Mix1", FALSE, "SSP2_demRedStrong", "SSP2", "Mix2", FALSE, "SSP2_demRedStrong", @@ -83,7 +82,7 @@ readEDGETransport <- function(subtype) { # Bind rows of equally named subtypes EdgeTransportSAdata <- lapply(types, function(type, outerList) { listOfDataTables <- lapply(outerList, function(innerList) innerList[[type]]) - rbindlist(listOfDataTables) + data.table::rbindlist(listOfDataTables) }, EdgeTransportSAdata) EdgeTransportSAdata <- setNames(EdgeTransportSAdata, types) @@ -93,19 +92,19 @@ readEDGETransport <- function(subtype) { ## that are applied to all sectors simultaneously ############################################################# translateEdgeTransportDemScentoREMIND <- function(dt) { - dt[DEM_scenario == "gdp_SSP2_demDiffer" & EDGE_scenario == "Mix4ICEban", DEM_scenario := "gdp_SSP2_demDiffer_IKEA"] - dt[DEM_scenario == "gdp_SSP2_demDiffer" & EDGE_scenario == "Mix1", DEM_scenario := "gdp_SSP2_demDiffer_IKEA"] - dt[DEM_scenario == "gdp_SSP2" & EDGE_scenario == "NAV_ele", DEM_scenario := "gdp_SSP2EU_NAV_ele"] - dt[DEM_scenario == "gdp_SSP2" & EDGE_scenario == "NAV_tec", DEM_scenario := "gdp_SSP2EU_NAV_tec"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "NAV_act", DEM_scenario := "gdp_SSP2EU_NAV_act"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "NAV_all", DEM_scenario := "gdp_SSP2EU_NAV_all"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "NAV_lce", DEM_scenario := "gdp_SSP2EU_NAV_lce"] - dt[DEM_scenario == "gdp_SSP2_demRedWeak" & EDGE_scenario == "CAMP_lscWeak", DEM_scenario := "gdp_SSP2EU_CAMP_weak"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "CAMP_lscStrong", DEM_scenario := "gdp_SSP2EU_CAMP_strong"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "Mix1", DEM_scenario := "gdp_SSP2_lowEn"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "Mix2", DEM_scenario := "gdp_SSP2_lowEn"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "Mix3ICEban", DEM_scenario := "gdp_SSP2_lowEn"] - dt[DEM_scenario == "gdp_SSP2_demRedStrong" & EDGE_scenario == "Mix4ICEban", DEM_scenario := "gdp_SSP2_lowEn"] + dt[DEM_scenario == "SSP2_demDiffer" & EDGE_scenario == "Mix4ICEban", DEM_scenario := "SSP2_demDiffer_IKEA"] + dt[DEM_scenario == "SSP2_demDiffer" & EDGE_scenario == "Mix1", DEM_scenario := "SSP2_demDiffer_IKEA"] + dt[DEM_scenario == "SSP2" & EDGE_scenario == "NAV_ele", DEM_scenario := "SSP2_NAV_ele"] + dt[DEM_scenario == "SSP2" & EDGE_scenario == "NAV_tec", DEM_scenario := "SSP2_NAV_tec"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "NAV_act", DEM_scenario := "SSP2_NAV_act"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "NAV_all", DEM_scenario := "SSP2_NAV_all"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "NAV_lce", DEM_scenario := "SSP2_NAV_lce"] + dt[DEM_scenario == "SSP2_demRedWeak" & EDGE_scenario == "CAMP_lscWeak", DEM_scenario := "SSP2_CAMP_weak"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "CAMP_lscStrong", DEM_scenario := "SSP2_CAMP_strong"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "Mix1", DEM_scenario := "SSP2_lowEn"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "Mix2", DEM_scenario := "SSP2_lowEn"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "Mix3ICEban", DEM_scenario := "SSP2_lowEn"] + dt[DEM_scenario == "SSP2_demRedStrong" & EDGE_scenario == "Mix4ICEban", DEM_scenario := "SSP2_lowEn"] return(dt) } EdgeTransportSAdata <- lapply(EdgeTransportSAdata, translateEdgeTransportDemScentoREMIND) @@ -116,7 +115,5 @@ readEDGETransport <- function(subtype) { validSubtypes <- names(EdgeTransportSAdata) if (!subtype %in% validSubtypes) stop(sprintf("Subtype %s is not valid for EDGETransport.", subtype)) - magpieobj <- as.magpie(EdgeTransportSAdata[[subtype]]) - - return(magpieobj) + as.magpie(EdgeTransportSAdata[[subtype]]) } diff --git a/R/readEdgeBuildings.R b/R/readEdgeBuildings.R index 2b96c9d5..d59bae00 100644 --- a/R/readEdgeBuildings.R +++ b/R/readEdgeBuildings.R @@ -11,14 +11,14 @@ readEdgeBuildings <- function(subtype = c("FE", "Floorspace")) { # input data version ver <- "2.2" - scenarios <- list( - SSPs = paste0("SSP", 1:5), - SSP2s = paste0("SSP2", c("EU", "_lowEn", - "EU_NAV_all")), - SDPs = paste0("SDP", c("", "_EI", "_MC", "_RC")) - ) + scenarios <- list(SSPs = paste0("SSP", 1:5), + SSP2s = paste0("SSP2", c("_lowEn", "_NAV_all")), + SDPs = paste0("SDP", c("", "_EI", "_MC", "_RC"))) data <- read.csv(file.path(ver, "EDGE_buildings.csv")) data <- as.magpie(data) + #TMP: as long as ver = 2.2. Rename SSP2EU_NAV_all to SSP2_NAV_all and drop SSP2EU. + getNames(data) <- sub("SSP2EU_NAV_all", "SSP2_NAV_all", getNames(data)) + data <- data[, , grep("SSP2EU", getNames(data), invert = TRUE)] switch(subtype, FE = { @@ -61,5 +61,5 @@ readEdgeBuildings <- function(subtype = c("FE", "Floorspace")) { data <- mselect(data, scenario = Reduce(c, scenarios)) } - return(data) + data } diff --git a/R/readEuropeanEnergyDatasheets.R b/R/readEuropeanEnergyDatasheets.R index 3bb72574..bf6fa148 100644 --- a/R/readEuropeanEnergyDatasheets.R +++ b/R/readEuropeanEnergyDatasheets.R @@ -13,9 +13,6 @@ #' @importFrom readxl excel_sheets read_excel #' @param subtype data subtype. Either "EU28" (data from June 20 including GBR) #' or "EU27" (latest data from August 23 without GBR) -#' @importFrom tidyr drop_na extract -#' @importFrom readxl excel_sheets read_excel -#' @importFrom stats aggregate na.pass #' readEuropeanEnergyDatasheets <- function(subtype) { if (!subtype %in% c("EU27", "EU28")) { @@ -56,7 +53,7 @@ readEuropeanEnergyDatasheets <- function(subtype) { countrySheet_1[, -c(1, 2, 3)] <- countrySheet_1[, -c(1, 2, 3)] * countrySheet_1[, c("factor")] countrySheet_1 <- countrySheet_1[, -c(2, 3)] # removing extra columns countrySheet_1 <- cbind(region, countrySheet_1) # adding region column - countrySheet_1 <- aggregate(. ~ REMIND + region, data = countrySheet_1, FUN = sum, na.action = na.pass) + countrySheet_1 <- stats::aggregate(. ~ REMIND + region, data = countrySheet_1, FUN = sum, na.action = stats::na.pass) colnames(countrySheet_1) <- c("variable", "region", 1990:(1990 + ncol(countrySheet_1) - 3)) # replace with remind_2 mapping countrySheet_2 <- countrySheet[indices_2, nameColumn:ncol(countrySheet)] @@ -68,7 +65,7 @@ readEuropeanEnergyDatasheets <- function(subtype) { countrySheet_2 <- countrySheet_2[, -c(2, 3)] # removing extra columns countrySheet_2 <- cbind(region, countrySheet_2) # adding region column # merge repeated items - countrySheet_2 <- aggregate(. ~ REMIND_2 + region, data = countrySheet_2, FUN = sum, na.action = na.pass) + countrySheet_2 <- stats::aggregate(. ~ REMIND_2 + region, data = countrySheet_2, FUN = sum, na.action = stats::na.pass) colnames(countrySheet_2) <- c("variable", "region", 1990:(1990 + ncol(countrySheet_2) - 3)) # replace with remind_3 mapping countrySheet_3 <- countrySheet[indices_3, nameColumn:ncol(countrySheet)] @@ -80,7 +77,7 @@ readEuropeanEnergyDatasheets <- function(subtype) { countrySheet_3 <- countrySheet_3[, -c(2, 3)] # removing extra columns countrySheet_3 <- cbind(region, countrySheet_3) # adding region column # merge repeated items - countrySheet_3 <- aggregate(. ~ REMIND_3 + region, data = countrySheet_3, FUN = sum, na.action = na.pass) + countrySheet_3 <- stats::aggregate(. ~ REMIND_3 + region, data = countrySheet_3, FUN = sum, na.action = stats::na.pass) colnames(countrySheet_3) <- c("variable", "region", 1990:(1990 + ncol(countrySheet_3) - 3)) # merge both REMIND mappings countrySheet <- rbind(countrySheet_1, countrySheet_2, countrySheet_3) @@ -637,7 +634,7 @@ readEuropeanEnergyDatasheets <- function(subtype) { ) } ) %>% - extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") + tidyr::extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") # nolint end file <- "energy_statistical_countrydatasheets_aug23.xlsx" @@ -649,8 +646,8 @@ readEuropeanEnergyDatasheets <- function(subtype) { tmp <- rbind( tmp, suppressMessages(read_xlsx(path = file, sheet = sheet, range = "C8:AI543", )) %>% - bind_cols(rows) %>% - drop_na("variable", "unit") %>% + dplyr::bind_cols(rows) %>% + tidyr::drop_na("variable", "unit") %>% select(-1) %>% reshape2::melt(id.vars = c("variable", "unit"), variable.name = "year") %>% mutate( diff --git a/R/readEurostat_EffortSharing.R b/R/readEurostat_EffortSharing.R index 0356fd13..08fbe87d 100644 --- a/R/readEurostat_EffortSharing.R +++ b/R/readEurostat_EffortSharing.R @@ -17,7 +17,7 @@ readEurostat_EffortSharing <- function(subtype) { data <- read.csv("effort_sharing_targets.csv", sep = ";", skip = 5) colnames(data) <- c("iso2", "country.name", "iso3", "2020", "2030", "flexibility_from_ETS_in_2030", "flexibility_from_land_use_in_2030") - data <- na.omit(data[, c("iso3", "2020", "2030")]) + data <- stats::na.omit(data[, c("iso3", "2020", "2030")]) } else if (subtype == "emissions") { # Reading EU Effort Sharing country historical emissions data <- read.csv("t2020_35.csv", sep = ";") @@ -32,8 +32,8 @@ readEurostat_EffortSharing <- function(subtype) { "LV" = "LVA", "RO" = "ROU", "BG" = "BGR" ) data$iso3 <- iso2toiso3[as.character(data$iso2)] - data <- na.omit(data[, c("iso3", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", - "2013", "2014", "2015", "2016")]) + data <- stats::na.omit(data[, c("iso3", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", + "2013", "2014", "2015", "2016")]) } else { stop("Not a valid subtype!") } diff --git a/R/readExpertGuess.R b/R/readExpertGuess.R index 9d90e3ed..4faa164d 100644 --- a/R/readExpertGuess.R +++ b/R/readExpertGuess.R @@ -27,10 +27,7 @@ #' } #' #' @importFrom dplyr bind_rows filter pull select -#' @importFrom quitte madrat_mule -#' @importFrom readr read_csv -#' @importFrom tidyr expand_grid - +#' readExpertGuess <- function(subtype) { if (subtype == "ies") { a <- read.csv("ies.csv", sep = ";") @@ -65,21 +62,21 @@ readExpertGuess <- function(subtype) { } if ("Chinese_Steel_Production" == subtype) { - out <- read_csv( + out <- readr::read_csv( file = "Chinese_Steel_Production.csv", comment = "#", show_col_types = FALSE ) %>% - madrat_mule() + quitte::madrat_mule() } else if ("industry_max_secondary_steel_share" == subtype) { - out <- read_csv( + out <- readr::read_csv( file = "industry_max_secondary_steel_share.csv", comment = "#", show_col_types = FALSE ) %>% - madrat_mule() + quitte::madrat_mule() } else if ("cement_production_convergence_parameters" == subtype) { - out <- read_csv( + out <- readr::read_csv( file = "cement_production_convergence_parameters.csv", col_types = "cdi", comment = "#" @@ -89,10 +86,10 @@ readExpertGuess <- function(subtype) { out %>% filter(!is.na(.data$region)), out %>% - head(n = 1) %>% + utils::head(n = 1) %>% filter(is.na(.data$region)) %>% select(-"region") %>% - expand_grid(region = toolGetMapping( + tidyr::expand_grid(region = toolGetMapping( name = "regionmapping_21_EU11.csv", type = "regional", where = "mappingfolder" ) %>% @@ -101,7 +98,7 @@ readExpertGuess <- function(subtype) { sort() %>% setdiff(out$region)) ) %>% - madrat_mule() + quitte::madrat_mule() } else if (subtype == "tradeConstraints") { a <- read.csv("tradeConstraints.csv", sep = ";") out <- as.magpie(a) diff --git a/R/readGCPT.R b/R/readGCPT.R index 3a737991..bbe24844 100644 --- a/R/readGCPT.R +++ b/R/readGCPT.R @@ -4,11 +4,9 @@ #' @param subtype Options are status, historical, future, lifespans, comp_rates and emissions #' @author Stephen Bi #' @importFrom readxl read_excel -#' @importFrom dplyr filter select mutate summarize group_by left_join across everything starts_with -#' @importFrom quitte removeColNa +#' @importFrom dplyr filter select mutate summarise group_by left_join #' @aliases readEndCoal - - +#' readGCPT <- function(subtype) { if (!(subtype %in% c("early_retire", "historical", "status", "future", @@ -56,10 +54,10 @@ readGCPT <- function(subtype) { sep <- ";" latest_col <- 3 - plant_status <- read.csv2(status_changes, sep = sep, stringsAsFactors = FALSE, fileEncoding = "ISO-8859-13") + plant_status <- utils::read.csv2(status_changes, sep = sep, stringsAsFactors = FALSE, fileEncoding = "ISO-8859-13") plant_status <- plant_status %>% - select(Country, MW, starts_with("H2")) %>% + select(Country, MW, tidyselect::starts_with("H2")) %>% mutate("Country" = ifelse(grepl("rkiye", Country), "Turkey", Country), "Country" = ifelse(grepl("Ivoire", Country), "Cote d'Ivoire", Country), "MW" = as.numeric(MW)) @@ -100,8 +98,8 @@ readGCPT <- function(subtype) { # Filter for all plants that were mothballed during this period mothballed <- plant_status %>% - filter(rowSums(across(everything(), ~ grepl("Moth", .))) > 0) %>% - filter(rowSums(across(everything(), ~ grepl("Oper", .))) > 0) + filter(rowSums(dplyr::across(tidyselect::everything(), ~ grepl("Moth", .))) > 0) %>% + filter(rowSums(dplyr::across(tidyselect::everything(), ~ grepl("Oper", .))) > 0) # Calculate capacity that was mothballed or restarted each year since 2014 # (to be fed into back-calculation of annual capacity below) @@ -115,7 +113,7 @@ readGCPT <- function(subtype) { oper_moth <- mothballed %>% filter(mothballed[, j] == "Operating" & mothballed[, 3] == "Mothballed") %>% group_by(Country) %>% - summarize(sum = sum(MW)) + summarise(sum = sum(MW)) oper_moth$Country <- toolCountry2isocode(mapping = c("DR Congo" = "COD"), country = oper_moth$Country) @@ -128,7 +126,7 @@ readGCPT <- function(subtype) { moth_ret <- mothballed %>% filter(mothballed[, j] == "Mothballed" & mothballed[, 3] == "Retired") %>% group_by(Country) %>% - summarize(sum = sum(MW)) + summarise(sum = sum(MW)) moth_ret$Country <- toolCountry2isocode(mapping = c("DR Congo" = "COD"), country = moth_ret$Country) moth_ret <- suppressWarnings( toolCountryFill(as.magpie(moth_ret, spatial = 1, temporal = as.numeric(colnames(mothballed)[j]), datacol = 2), verbosity = 2, fill = 0, no_remove_warning = "KOS") @@ -137,7 +135,7 @@ readGCPT <- function(subtype) { moth_oper <- mothballed %>% filter(mothballed[, j] == "Mothballed" & mothballed[, 3] == "Operating") %>% group_by(Country) %>% - summarize(sum = sum(MW)) + summarise(sum = sum(MW)) moth_oper$Country <- toolCountry2isocode(mapping = c("DR Congo" = "COD"), country = moth_oper$Country) moth_oper <- suppressWarnings( toolCountryFill(as.magpie(moth_oper, spatial = 1, temporal = as.numeric(colnames(mothballed)[j]), datacol = 2), verbosity = 2, fill = 0, no_remove_warning = "KOS") @@ -146,7 +144,7 @@ readGCPT <- function(subtype) { constr_moth <- mothballed %>% filter(mothballed[, 3] == "Mothballed" & mothballed[, j] != "Mothballed" & mothballed[, j] != "Operating") %>% group_by(Country) %>% - summarize(sum = sum(MW)) + summarise(sum = sum(MW)) constr_moth$Country <- toolCountry2isocode(mapping = c("DR Congo" = "COD"), country = constr_moth$Country) constr_moth <- suppressWarnings( toolCountryFill(as.magpie(constr_moth, spatial = 1, temporal = as.numeric(colnames(mothballed)[j]), datacol = 2), verbosity = 2, fill = 0, no_remove_warning = "KOS") @@ -160,7 +158,7 @@ readGCPT <- function(subtype) { moth_moth <- mothballed %>% filter(mothballed[, 3] == "Mothballed" & mothballed[, j] == "Mothballed") %>% group_by(Country) %>% - summarize(sum = sum(MW)) + summarise(sum = sum(MW)) moth_moth$Country <- toolCountry2isocode(mapping = c("DR Congo" = "COD"), country = moth_moth$Country) moth_moth <- suppressWarnings( @@ -230,7 +228,7 @@ readGCPT <- function(subtype) { avgRetAge <- retAge %>% group_by(Country) %>% - summarise(Avg_Ret_Age = weighted.mean(`Plant Age`, `Capacity (MW)`)) + summarise(Avg_Ret_Age = stats::weighted.mean(`Plant Age`, `Capacity (MW)`)) retCap <- retAge %>% group_by(Country) %>% summarise(Retired_Cap = sum(`Capacity (MW)`)) @@ -253,7 +251,7 @@ readGCPT <- function(subtype) { filter(Status %in% c("Operating", "operating") & !is.na(`Capacity (MW)`) & !is.na(`Plant Age`)) meanAge_c <- meanAge %>% group_by(Country) %>% - summarise(meanAge = weighted.mean(`Plant Age`, `Capacity (MW)`)) + summarise(meanAge = stats::weighted.mean(`Plant Age`, `Capacity (MW)`)) meanAge_c <- toolCountryFill(as.magpie(meanAge_c, spatial = 1), fill = 0, verbosity = 2) capWeight <- meanAge %>% group_by(Country) %>% @@ -507,7 +505,7 @@ readGCPT <- function(subtype) { she_rate[, , phase] <- mtran[, , "she2she"] / mtran[, , "shelved"] can_rate[nonzero_countries, , phase] <- mtran[nonzero_countries, , "she2can"] / mtran[nonzero_countries, , "shelved"] # Calculate global mean rates - glo_can_rate <- weighted.mean(can_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) + glo_can_rate <- stats::weighted.mean(can_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) # Calculate regional rates tmp <- toolAggregate(can_rate[nonzero_countries, , phase], rel = map[which(map$CountryCode %in% nonzero_countries), ], mtran[nonzero_countries, , phase]) # For regions which had zero projects in a certain phase, assign the global weighted mean @@ -520,8 +518,8 @@ readGCPT <- function(subtype) { she_rate[, , phase] <- mtran[, , paste0(phase, "2she")] / mtran[, , phase] } # Calculate global mean rates - glo_can_she_rate <- weighted.mean(can_she_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) - glo_she_rate <- weighted.mean(she_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) + glo_can_she_rate <- stats::weighted.mean(can_she_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) + glo_she_rate <- stats::weighted.mean(she_rate[nonzero_countries, , phase], mtran[nonzero_countries, , phase]) # Calculate regional rates tmp_can_she <- toolAggregate(can_she_rate[nonzero_countries, , phase], rel = map[which(map$CountryCode %in% nonzero_countries), ], mtran[nonzero_countries, , phase]) @@ -565,8 +563,9 @@ readGCPT <- function(subtype) { comp_rate[, , status] <- 1 - can_she_rate[, , status] comp_rate_brown[, , status] <- 1 - 0.5 * can_she_rate[, , status] } - glo_comp_rate[, , status] <- weighted.mean(comp_rate[nonzero_countries, , status], mtran[nonzero_countries, , status]) - glo_comp_rate_brown[, , status] <- weighted.mean(comp_rate_brown[nonzero_countries, , status], mtran[nonzero_countries, , status]) + glo_comp_rate[, , status] <- stats::weighted.mean(comp_rate[nonzero_countries, , status], + mtran[nonzero_countries, , status]) + glo_comp_rate_brown[, , status] <- stats::weighted.mean(comp_rate_brown[nonzero_countries, , status], mtran[nonzero_countries, , status]) if (length(nonzero_countries)) { # Calculate regional rates @@ -990,7 +989,7 @@ readGCPT <- function(subtype) { } # Read in national average capacity factor assumption for each 5-year time-step capFac <- calcOutput("CapacityFactor", aggregate = FALSE)[, seq(2020, 2100, 5), "pc"] - capFac <- removeColNa(as.data.frame(capFac))[, -3] + capFac <- quitte::removeColNa(as.data.frame(capFac))[, -3] colnames(capFac) <- c("Country", "Period", "Cap_Factor") capFac$Period <- as.numeric(as.character(capFac$Period)) @@ -1042,18 +1041,18 @@ readGCPT <- function(subtype) { bau_emi <- left_join(bau_emi, df_comp, by = c("Country", "Status")) bau_emi$Value[which(bau_emi$Status == "Operating")] <- 1 bau_emi$Value[which(bau_emi$Status == "Mothballed")] <- 0.5 - bau_emi <- bau_emi %>% group_by(Country) %>% summarize(BAU_total_emi = sum(BAU_emi * Value) / 1000) + bau_emi <- bau_emi %>% group_by(Country) %>% summarise(BAU_total_emi = sum(BAU_emi * Value) / 1000) green_emi <- left_join(green_emi, df_comp, by = c("Country", "Status")) green_emi$Value <- 0.5 * green_emi$Value green_emi$Value[which(green_emi$Status == "Operating")] <- 1 green_emi$Value[which(green_emi$Status == "Mothballed")] <- 0 - green_emi <- green_emi %>% group_by(Country) %>% summarize(green_total_emi = sum(green_emi * Value) / 1000) + green_emi <- green_emi %>% group_by(Country) %>% summarise(green_total_emi = sum(green_emi * Value) / 1000) brown_emi <- left_join(brown_emi, df_comp_brown, by = c("Country", "Status")) brown_emi$Value[which(brown_emi$Status == "Operating")] <- 1 brown_emi$Value[which(brown_emi$Status == "Mothballed")] <- 0.5 - brown_emi <- brown_emi %>% group_by(Country) %>% summarize(brown_total_emi = sum(brown_emi * Value) / 1000) + brown_emi <- brown_emi %>% group_by(Country) %>% summarise(brown_total_emi = sum(brown_emi * Value) / 1000) bau_emi <- toolCountryFill(as.magpie(bau_emi), fill = 0, verbosity = 2) green_emi <- toolCountryFill(as.magpie(green_emi), fill = 0, verbosity = 2) diff --git a/R/readGEA2012.R b/R/readGEA2012.R index 2df3bfe7..ce303b37 100644 --- a/R/readGEA2012.R +++ b/R/readGEA2012.R @@ -9,9 +9,6 @@ #' a <- readSource("GEA2012", "coal") #' } #' -#' @importFrom readxl read_excel -#' @importFrom dplyr relocate mutate - readGEA2012 <- function(subtype) { EJ_2_TWyr <- 1 / 31.536 ts1 <- 5 @@ -37,12 +34,12 @@ readGEA2012 <- function(subtype) { # 'TAO-rv','SHO-rv','EHO-rv','COO-rv','TAO-rs','SHO-rs','EHO-rs','COO-rs','HAC','LIC') if ("coal" %in% subtype) { - rawData <- read.csv2("Scenario Data HAC_LIC.csv", header = TRUE, as.is = T) + rawData <-utils::read.csv2("Scenario Data HAC_LIC.csv", header = TRUE, as.is = T) rawData$grade <- as.factor(rawData$grade) rawData$value <- as.numeric(rawData$value) rawData <- rawData %>% - mutate(enty = "pecoal") %>% - relocate(enty, .before = scenario) + dplyr::mutate(enty = "pecoal") %>% + dplyr::relocate(enty, .before = scenario) out <- setYears(as.magpie(rawData), ttot[1]) tmp <- out for (rlf in 2:length(unique(rawData$grade))) { diff --git a/R/readGGDC10.R b/R/readGGDC10.R index f24f8968..1ddf9e61 100644 --- a/R/readGGDC10.R +++ b/R/readGGDC10.R @@ -5,19 +5,8 @@ #' a <- readSource("GGDC10",convert=F) #' } #' -#' @importFrom readxl read_excel -#' @importFrom tidyr gather -#' - readGGDC10 <- function() { - - data <- suppressWarnings(read_xlsx(path = "ggdc10.xlsx", sheet = "dataset")) - - dataLong <- data[, -c(2, 3)] %>% - gather("Sector", "Value", -c(1:3)) - - x <- as.magpie(dataLong, spatial = 1, temporal = 3, datacol = 5) - - return(x) - + data <- suppressWarnings(readxl::read_xlsx(path = "ggdc10.xlsx", sheet = "dataset")) + dataLong <- data[, -c(2, 3)] %>% tidyr::gather("Sector", "Value", -c(1:3)) + as.magpie(dataLong, spatial = 1, temporal = 3, datacol = 5) } diff --git a/R/readGini.R b/R/readGini.R index 36331cb6..e90cd4ed 100644 --- a/R/readGini.R +++ b/R/readGini.R @@ -1,23 +1,67 @@ #' Read Gini -#' -#' Read Gini coefficients for SSP scenarios from Rao et al., Futures, 2018. Data has been provided by the authors, but will be made publicly available as well. -#' This contains data for 184 countries and from 2011 onwards. -#' +#' +#' Read Gini coefficients for SSP scenarios from Rao et al., Futures, 2018. Data has been provided by the authors, but +#' will be made publicly available as well. This contains data for 184 countries and from 2011 onwards. +#' #' Copied from the documentation provided by the authors: -#' This sheet contains the original Gini projections for 43 countries from the underlying empirical model (See reference to RSP 2016 in the main paper) -#' and the extrapolations to all countries using the methodology described in the article. The country codes are the World Bank codes. -#' +#' This sheet contains the original Gini projections for 43 countries from the underlying empirical model +#' (See reference to RSP 2016 in the main paper) and the extrapolations to all countries using the methodology +#' described in the article. The country codes are the World Bank codes. +#' #' @return magpie object of the Gini data #' @author Bjoern Soergel #' @seealso \code{\link{readSource}} \code{\link{convertGini}} #' @examples -#' -#' \dontrun{ a <- readSource(type="Gini") +#' \dontrun{ +#' a <- readSource(type="Gini") #' } -#' -#' @importFrom readxl read_excel - readGini <- function(){ - x <- read_excel("Gini_projections_SSPs.xlsx", sheet = 2) - return(as.magpie(x)) + as.magpie(readxl::read_excel("Gini_projections_SSPs.xlsx", sheet = 2)) +} + +#' Convert Gini +#' +#' Converts Gini data from readGini() to ISO country level. +#' Countries missing in the original data set will have their Gini set to zero ( +#' a very small number for numerical reasons to be precise). +#' The original data range is 2011-2100 in one-year steps, +#' here we extend it to 2000-2150 in 5-year steps. +#' Values before (after) the original range are held fixed at 2011 (2100) levels. +#' Gini values for the SDP scenario are taken from the SSP1 scenario +#' +#' @param x MAgPIE object containing Gini data with World Bank codes, 2011-2100, in percent (range 0-100) +#' @return MAgPIE object of the Gini data in ISO countries, range 0-1 +#' @author Bjoern Soergel +#' @seealso \code{\link{readSource}} \code{\link{readGini}} +#' @examples +#' \dontrun{ +#' a <- convertGini(x) +#' } +convertGini <- function(x) { + # add SDP scenario, with same data as SSP1 + xSDP <- x[, , "SSP1"] + getNames(xSDP) <- "SDP" + x <- mbind(x, xSDP) + + # conversion to iso3c codes, match Somalia by hand because countrycode fails + getItems(x, dim = 1) <- countrycode::countrycode(getRegions(x), + origin = "wb", + destination = "iso3c", + custom_match = c("SOM" = "SOM")) + getSets(x)[1] <- "iso3c" + + # fill missing countries with a small number (not zero because this might cause numerical problems in Gini aggregation later) + x <- toolCountryFill(x, fill = 1e-8, verbosity = 2) + + # fill years outside original range + # NB: time_interpolate breaks set names, so restore them explicitly + xsets <- getSets(x) + x <- time_interpolate(x, interpolated_year = c(seq(2000, 2010, by = 5), seq(2105, 2150, by = 5)), + integrate_interpolated_years = TRUE, extrapolation_type = "constant") + x <- x[, seq(2000, 2150, by = 5), ] + getSets(x) <- xsets + + # convert to 0..1 range + x <- x / 100 + x } diff --git a/R/readGlobalCCSinstitute.R b/R/readGlobalCCSinstitute.R index 644b678a..dc6b9f5f 100644 --- a/R/readGlobalCCSinstitute.R +++ b/R/readGlobalCCSinstitute.R @@ -8,8 +8,6 @@ #' @return A [`magpie`][magclass::magclass] object. #' #' @importFrom dplyr mutate select -#' @importFrom quitte madrat_mule -#' @importFrom readr read_csv #' @importFrom readxl read_excel #' #' @export @@ -29,10 +27,10 @@ readGlobalCCSinstitute <- function(subtype = '08-09-2017') { return() } else if ('2023-11' == subtype) { # 2023-11 ---- - read_csv(file = 'Global_Status_of_CCS_2023-11.csv', col_types = 'cccicnc', - na = 'Under Evaluation', comment = '#', trim_ws = TRUE) %>% - madrat_mule() %>% - return() + readr::read_csv(file = 'Global_Status_of_CCS_2023-11.csv', col_types = 'cccicnc', + na = 'Under Evaluation', comment = '#', trim_ws = TRUE) %>% + quitte::madrat_mule() %>% + return() } else { stop('Unsupported version argument.') diff --git a/R/readGlobalEnergyMonitor.R b/R/readGlobalEnergyMonitor.R index 8f005536..a08be9c6 100644 --- a/R/readGlobalEnergyMonitor.R +++ b/R/readGlobalEnergyMonitor.R @@ -6,17 +6,16 @@ #' #' @importFrom dplyr filter mutate select #' @importFrom readxl read_xlsx -#' @importFrom rlang sym -#' #' @export +#' readGlobalEnergyMonitor <- function() { # GEM GIPT 2024 # file available after filling out questionnaire: # https://globalenergymonitor.org/projects/global-integrated-power-tracker/download-data/ d <- readxl::read_excel("Global-Integrated-Power-June-2024.xlsx", - sheet = "Power facilities", - trim_ws = TRUE, - col_types = "text") %>% + sheet = "Power facilities", + trim_ws = TRUE, + col_types = "text") %>% select(variable = "Type", tech = "Technology", region = "Country/area", diff --git a/R/readHRE.R b/R/readHRE.R index fe59cd66..1432b1dd 100644 --- a/R/readHRE.R +++ b/R/readHRE.R @@ -6,29 +6,25 @@ #' #' @seealso [`readSource()`] #' @source https://heatroadmap.eu/roadmaps/ -#' #' @importFrom dplyr select -#' @importFrom tidyr separate -#' @importFrom readxl read_xlsx -#' #' @export - +#' readHRE <- function() { filename <- "HRE4-summary-tables-and-figures-web.xlsx" - df <- read_excel(filename, sheet = "2. Consolidated Results", range = "A3:H9537") - + df <- readxl::read_excel(filename, sheet = "2. Consolidated Results", range = "A3:H9537") + # only use data from these 4 scenarios df <- df[df$Scenario %in% c("BL 2015", "BL 2050", "CD 2050", "HRE 2050"), ] - - df <- separate(df, "Scenario", c("model", "year"), sep=" ") - + + df <- tidyr::separate(df, "Scenario", c("model", "year"), sep=" ") + # combine category columns to one variable column df$variable <- paste(df$Category, df$SourceType, df$Source, sep = "|") df <- select(df, c("Country", "year", "model", "DataType", "variable", "Unit", "Value")) # convert to magpie object x <- as.magpie(df, spatial="Country", temporal="year", tidy = TRUE) - + # change country code to iso3 getItems(x, dim=1) <- countrycode::countrycode(getItems(x, dim=1), "eurostat", "iso3c") getSets(x)[1] <- "region" diff --git a/R/readIAEA_PRIS.R b/R/readIAEA_PRIS.R index 45ddfd33..6b4a59fc 100644 --- a/R/readIAEA_PRIS.R +++ b/R/readIAEA_PRIS.R @@ -16,8 +16,8 @@ readIAEA_PRIS <- function() { "inactive" = .data$inactive, # only relevant for Japan variable = "Cap|Electricity|Nuclear") %>% mutate(year = "y2020") %>% # technically, its 2024 - pivot_longer(cols = c("operational", "construction", "inactive"), - names_to = "status") %>% + tidyr::pivot_longer(cols = c("operational", "construction", "inactive"), + names_to = "status") %>% select("country", "year", "variable", "status", "unit", "value") %>% as.magpie(spatial = "country", temporal = "year") %>% add_dimension(dim = 3.1, add = "model", nm = "IAEA_PRIS") diff --git a/R/readIEA_CCUS.R b/R/readIEA_CCUS.R index d6ebbc87..3cfb4882 100644 --- a/R/readIEA_CCUS.R +++ b/R/readIEA_CCUS.R @@ -39,10 +39,10 @@ readIEA_CCUS <- function(subtype) { data[data$country == "Lybia", "country"] <- "Libya" # share capacities of projects by multiple countries according to their GDP - gdp <- calcOutput("GDP", aggregate = FALSE) - gdp_JPN <- as.numeric(gdp["JPN", 2020, "gdp_SSP2EU"]) - gdp_AUS <- as.numeric(gdp["AUS", 2020, "gdp_SSP2EU"]) - gdp_MYS <- as.numeric(gdp["MYS", 2020, "gdp_SSP2EU"]) + gdp <- calcOutput("GDP", scenario = "SSP2", aggregate = FALSE) + gdp_JPN <- as.numeric(gdp["JPN", 2020, ]) + gdp_AUS <- as.numeric(gdp["AUS", 2020, ]) + gdp_MYS <- as.numeric(gdp["MYS", 2020, ]) JM <- data[data$country == "Japan-Malaysia", ] AJ <- data[data$country == "Australia-Japan", ] @@ -76,7 +76,7 @@ readIEA_CCUS <- function(subtype) { ) } - cap <- aggregate(value ~ country + period, data = tmp, FUN = sum) %>% + cap <- stats::aggregate(value ~ country + period, data = tmp, FUN = sum) %>% filter(.data$period <= 2023) %>% mutate(variable = "Carbon Management|Storage") @@ -114,13 +114,13 @@ readIEA_CCUS <- function(subtype) { ) } - capLow <- aggregate(value ~ country + period, + capLow <- stats::aggregate(value ~ country + period, data = filter(tmp, .data$status %in% statusLow), FUN = sum ) %>% mutate(variable = "low") - capHigh <- aggregate(value ~ country + period, + capHigh <- stats::aggregate(value ~ country + period, data = filter(tmp, .data$status %in% statusHigh), FUN = sum ) %>% @@ -156,19 +156,19 @@ readIEA_CCUS <- function(subtype) { } # aggregate by country and period, new variables depending on status - capOp <- aggregate(value ~ country + period, + capOp <- stats::aggregate(value ~ country + period, data = filter(tmp, .data$status %in% "Operational"), FUN = sum ) %>% mutate(status = "operational") - capCon <- aggregate(value ~ country + period, + capCon <- stats::aggregate(value ~ country + period, data = filter(tmp, .data$status %in% "Under construction"), FUN = sum ) %>% mutate(status = "construction") - capPlan <- aggregate(value ~ country + period, + capPlan <- stats::aggregate(value ~ country + period, data = filter(tmp, .data$status %in% "Planned"), FUN = sum ) %>% diff --git a/R/readIEA_ETP.R b/R/readIEA_ETP.R index 2f07b1b4..cd316714 100644 --- a/R/readIEA_ETP.R +++ b/R/readIEA_ETP.R @@ -3,11 +3,9 @@ #' @author Falk Benke #' @param subtype data subtype. Either "industry", "buildings", "summary", or "transport" #' @importFrom tibble tibble -#' @importFrom dplyr bind_rows bind_cols select mutate +#' @importFrom dplyr bind_rows select mutate #' @importFrom readxl read_xlsx -#' @importFrom tidyr drop_na pivot_longer extract -#' @importFrom rlang sym - +#' readIEA_ETP <- function(subtype) { # nolint start @@ -139,7 +137,7 @@ readIEA_ETP <- function(subtype) { ) } ) %>% - extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") + tidyr::extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") ) }, buildings = { @@ -268,7 +266,7 @@ readIEA_ETP <- function(subtype) { ) } ) %>% - extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") + tidyr::extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") ) }, summary = { @@ -442,7 +440,7 @@ readIEA_ETP <- function(subtype) { ) } ) %>% - extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") + tidyr::extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") ) }, transport = { @@ -511,7 +509,7 @@ readIEA_ETP <- function(subtype) { ) } ) %>% - extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") + tidyr::extract("name", c("variable", "unit"), "^(.*) \\((.*)\\)$") ) } ) @@ -538,14 +536,14 @@ readIEA_ETP <- function(subtype) { col_types = col_types, range = subtypes[[subtype]]$scenarios[[scenario]] ) %>% # add variable and unit columns - bind_cols(subtypes[[subtype]]$rows) %>% + dplyr::bind_cols(subtypes[[subtype]]$rows) %>% # drop unneeded rows - drop_na("variable", "unit") %>% + tidyr::drop_na("variable", "unit") %>% # drop rownames from worksheet select(-1) %>% # add variable prefix mutate("variable" = paste0(subtypes[[subtype]]$prefix, "|", !!sym("variable"))) %>% - pivot_longer( + tidyr::pivot_longer( cols = c(-"variable", -"unit"), names_to = "year", names_transform = list("year" = as.integer) ) %>% diff --git a/R/readIEA_EVOutlook.R b/R/readIEA_EVOutlook.R index 1f0baced..ee696de7 100644 --- a/R/readIEA_EVOutlook.R +++ b/R/readIEA_EVOutlook.R @@ -2,7 +2,7 @@ #' #' @author Falk Benke readIEA_EVOutlook <- function() { - data <- read.csv2(file.path("data", "2024", "IEA Global EV Data 2024.csv"), sep = ",") + data <- utils::read.csv2(file.path("data", "2024", "IEA Global EV Data 2024.csv"), sep = ",") data <- data %>% mutate( @@ -12,7 +12,5 @@ readIEA_EVOutlook <- function() { ) %>% select("region", "year", "scenario" = "category", "variable", "unit", "value") - x <- as.magpie(data, spatial = 1) - - return(x) + as.magpie(data, spatial = 1) } diff --git a/R/readIEA_HSMR.R b/R/readIEA_HSMR.R index 122a58c6..114d0602 100644 --- a/R/readIEA_HSMR.R +++ b/R/readIEA_HSMR.R @@ -18,11 +18,11 @@ readIEA_HSMR <- function() { "2020_pumped" = .data$cap_2020_pumped, "2030_pumped" = .data$cap_2020_pumped + .data$of_that_pumped, variable = "Cap|Electricity|Hydro") %>% - pivot_longer(cols = c("2020_operational", "2030_expected", - "2030_accelerated", "2020_pumped", "2030_pumped"), - names_to = "year_scen") %>% + tidyr::pivot_longer(cols = c("2020_operational", "2030_expected", + "2030_accelerated", "2020_pumped", "2030_pumped"), + names_to = "year_scen") %>% select("country", "variable", "year_scen", "unit", "value") %>% - separate(.data$year_scen, c("year", "status")) %>% + tidyr::separate(.data$year_scen, c("year", "status")) %>% as.magpie(spatial = "country") x[, 2020, "accelerated"] <- x[, 2020, "operational"] diff --git a/R/readIEA_WEIO_2014.R b/R/readIEA_WEIO_2014.R deleted file mode 100644 index 25eaf316..00000000 --- a/R/readIEA_WEIO_2014.R +++ /dev/null @@ -1,130 +0,0 @@ -#' IEA World Energy Investment Outlook (2014) -#' -#' Read projected 2014-20 investments into industry energy efficiency from the -#' [IEA World Energy Investment Outlook (2014)](http://www.iea.org/publications/freepublications/publication/weo-2014-special-report---investment.html) -#' -#' @return A [madrat_mule()] with a list containing the [tibble] `data` with -#' 2014–20 average annual investments into `Energy intensive` and -#' `Non-energy intensive` industry, in $bn 2012, and the [tibble] -#' `country_groups` with `IEA region`s and corresponding `iso3c` country -#' codes. -#' -#' @importFrom dplyr anti_join group_by left_join mutate pull select summarise n -#' @importFrom quitte madrat_mule -#' @importFrom readr read_csv -#' @importFrom readxl excel_sheets read_excel -#' @importFrom tidyr nest unnest -#' @importFrom assertr verify -#' @export -readIEA_WEIO_2014 <- function() { - # define file paths ---- - # (for easier debugging) - # path <- '~/PIK/swap/inputdata/sources/IEA_WEIO_2014/' - path <- './' - - file_country_groups <- file.path(path, 'IEA_WEO_country_groups.csv') - file_data <- file.path(path, 'WEIO2014AnnexA.xls') - - # read country groups ---- - country_groups <- read_csv(file = file_country_groups, - show_col_types = FALSE) %>% - nest(data = .data$Countries) %>% - mutate(result = purrr::map(.data$data, function(x) { - tibble(iso3c = unlist(strsplit(x$Countries, ', ', fixed = TRUE))) - })) %>% - select(-'data') %>% - unnest(.data$result) - - # read data ---- - d <- tibble() - for (sheet in setdiff(excel_sheets(file_data), 'Contents')) { - d <- bind_rows( - d, - - tibble( - `IEA region` = read_excel(path = file_data, sheet = sheet, range = 'C2', - col_names = 'IEA region', - col_types = 'text') %>% - pull('IEA region'), - - read_excel(path = file_data, sheet = sheet, range = 'C39:F40', - col_names = c('name', 'value'), - col_types = c('text', 'skip', 'skip', 'numeric'), - progress = FALSE) - ) - ) - } - - # calculate additional regions ---- - # For regions which have sub-regions, calculate a separate region without the - # sub-regions. For example, split 'OECD Americas' into "United States' and - # 'OECD Americas w/o United States'. - region_modifications <- tribble( - ~superset, ~subsets, - 'OECD Americas', 'United States', - 'OECD Europe', 'European Union', - 'OECD Asia Oceania', 'Japan', - 'E. Europe/Eurasia', 'Russia', - 'Non-OECD Asia', 'China, India, Southeast Asia', - 'Latin America', 'Brazil' - ) %>% - mutate(`IEA region` = paste(.data$superset, 'w/o', .data$subsets)) %>% - nest(data = .data$subsets) %>% - mutate(result = purrr::map(.data$data, function(x) { - tibble(subsets = unlist(strsplit(x$subsets, ', ', fixed = TRUE))) - })) %>% - select(-'data') %>% - unnest(.data$result) %>% - select('IEA region', 'superset', 'subsets') - - ## calculate region mappings ---- - country_groups <- bind_rows( - country_groups %>% - anti_join(region_modifications, c('IEA region' = 'superset')), - - region_modifications %>% - distinct(!!!syms(c('IEA region', 'superset'))) %>% - left_join(country_groups, c('superset' = 'IEA region')) %>% - select('IEA region', 'iso3c') %>% - anti_join( - region_modifications %>% - group_by(!!sym('IEA region')) %>% - left_join(country_groups, c('subsets' = 'IEA region')) %>% - distinct(!!!syms(c('IEA region', 'iso3c'))) %>% - rename(remove_iso3c = 'iso3c'), - - c('iso3c' = 'remove_iso3c') - ) - ) %>% - group_by(.data$iso3c) %>% - mutate(count = n()) %>% - ungroup() %>% - verify(expr = 1 == .data$count, - description = 'duplicate countries in country groups') %>% - select(-'count') - - ## calculate region data ---- - d <- bind_rows( - d %>% - anti_join(region_modifications, c('IEA region' = 'superset')), - - bind_rows( - region_modifications %>% - left_join(d, c('superset' = 'IEA region')) %>% - select('IEA region', 'name', 'value'), - - region_modifications %>% - left_join(d, c('subsets' = 'IEA region')) %>% - group_by(!!!syms(c('IEA region', 'name'))) %>% - summarise(value = sum(.data$value) * -1, .groups = 'drop') - ) %>% - group_by(!!!syms(c('IEA region', 'name'))) %>% - summarise(value = sum(.data$value), .groups = 'drop') - ) - - # return data and country groups ---- - list(data = d, - country_groups = country_groups) %>% - madrat_mule() %>% - return() -} diff --git a/R/readIEA_WEO.R b/R/readIEA_WEO.R index 39367b1e..905bafd8 100644 --- a/R/readIEA_WEO.R +++ b/R/readIEA_WEO.R @@ -15,7 +15,6 @@ #' a <- readSource(type = "WEO", subtype = "Capacity") #' } #' -#' @importFrom tidyr gather #' @importFrom readxl read_excel readIEA_WEO <- function(subtype) { @@ -62,14 +61,14 @@ readIEA_WEO <- function(subtype) { input <- input_all[, c(15, 1:6)] colnames(input)[4:7] <- c(2015, 2020, 2030, 2040) input <- input %>% - gather(4:7, key = "Year", value = "costs") + tidyr::gather(4:7, key = "Year", value = "costs") x <- as.magpie(input, spatial = 3, temporal = 4, datacol = 5) } else if (subtype == "O&M_Costs") { input <- input_all[, c(15, 1:2, 7:10)] colnames(input)[4:7] <- c(2015, 2020, 2030, 2040) input <- input %>% - gather(4:7, key = "Year", value = "costs") + tidyr::gather(4:7, key = "Year", value = "costs") x <- as.magpie(input, spatial = 3, temporal = 4, datacol = 5) } @@ -110,7 +109,7 @@ readIEA_WEO <- function(subtype) { # Primary energy demand tmp1 <- tmp_sps[which(tmp_sps[, 1] == "Total primary demand"):(which(tmp_sps[, 1] == "Power sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -129,7 +128,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Total primary demand"):(which(tmp_sps[, 1] == "Power sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -145,7 +144,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Total primary demand"):(which(tmp_sps[, 1] == "Power sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -159,7 +158,7 @@ readIEA_WEO <- function(subtype) { # Power sector tmp2 <- tmp_sps[which(tmp_sps[, 1] == "Power sector"):(which(tmp_sps[, 1] == "Other energy sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -177,7 +176,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Power sector"):(which(tmp_sps[, 1] == "Other energy sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -193,7 +192,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Power sector"):(which(tmp_sps[, 1] == "Other energy sector") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -208,7 +207,7 @@ readIEA_WEO <- function(subtype) { # Final energy consumption tmp3 <- tmp_sps[which(tmp_sps[, 1] == "Total final consumption"):(which(tmp_sps[, 1] == "Industry") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -226,7 +225,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Total final consumption"):(which(tmp_sps[, 1] == "Industry") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -242,7 +241,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Total final consumption"):(which(tmp_sps[, 1] == "Industry") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -256,7 +255,7 @@ readIEA_WEO <- function(subtype) { # Final energy consumption (Industry) tmp4 <- tmp_sps[which(tmp_sps[, 1] == "Industry"):(which(tmp_sps[, 1] == "Transport") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -274,7 +273,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Industry"):(which(tmp_sps[, 1] == "Transport") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -290,7 +289,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Industry"):(which(tmp_sps[, 1] == "Transport") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -304,7 +303,7 @@ readIEA_WEO <- function(subtype) { # Final energy consumption (Transport) tmp5 <- tmp_sps[which(tmp_sps[, 1] == "Transport"):(which(tmp_sps[, 1] == "Buildings") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -322,7 +321,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Transport"):(which(tmp_sps[, 1] == "Buildings") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -337,7 +336,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Transport"):(which(tmp_sps[, 1] == "Buildings") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -351,7 +350,7 @@ readIEA_WEO <- function(subtype) { # Final energy consumption (Buildings) tmp6 <- tmp_sps[which(tmp_sps[, 1] == "Buildings"):(which(tmp_sps[, 1] == "Other") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -369,7 +368,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Buildings"):(which(tmp_sps[, 1] == "Other") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -384,7 +383,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Buildings"):(which(tmp_sps[, 1] == "Other") - 1), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% @@ -397,7 +396,7 @@ readIEA_WEO <- function(subtype) { # Final energy consumption (Other) tmp7 <- tmp_sps[which(tmp_sps[, 1] == "Other"):which(tmp_sps[, 1] == "Petrochem. feedstock"), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Stated Policies Scenario") %>% @@ -413,7 +412,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Current Policies Scenario"), tmp_cps[which(tmp_sps[, 1] == "Other"):which(tmp_sps[, 1] == "Petrochem. feedstock"), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Current Policies Scenario") %>% @@ -427,7 +426,7 @@ readIEA_WEO <- function(subtype) { filter(period < 2025) %>% mutate(scenario = "Sustainable Development Scenario"), tmp_sds[which(tmp_sps[, 1] == "Other"):which(tmp_sps[, 1] == "Petrochem. feedstock"), ] %>% - gather(period, value, -variable) %>% + tidyr::gather(period, value, -variable) %>% mutate(period = as.numeric(period)) %>% mutate(model = "IEA-WEM2019") %>% mutate(scenario = "Sustainable Development Scenario") %>% diff --git a/R/readIEA_WorldEnergyOutlook.R b/R/readIEA_WorldEnergyOutlook.R index 7f4c623b..f7049779 100644 --- a/R/readIEA_WorldEnergyOutlook.R +++ b/R/readIEA_WorldEnergyOutlook.R @@ -1,27 +1,27 @@ #' Read in IEA World Energy Outlook Data from 2023 #' #' @author Falk Benke -#' @importFrom dplyr filter distinct group_by ungroup rename_all +#' @importFrom dplyr filter distinct group_by ungroup #' readIEA_WorldEnergyOutlook <- function() { # nolint data <- rbind( - read.csv2( + utils::read.csv2( file = "2023/complete/WEO2023_Extended_Data_Regions.csv", sep = "," - ) %>% rename_all(tolower), - read.csv2( + ) %>% dplyr::rename_all(tolower), + utils::read.csv2( file = "2023/complete/WEO2023_Extended_Data_Supply_Refining_H2_Trade_Prices.csv", sep = "," - ) %>% rename_all(tolower), + ) %>% dplyr::rename_all(tolower), read.csv( file = "2023/complete/WEO2023_Extended_Data_World.csv", sep = "," - ) %>% rename_all(tolower), + ) %>% dplyr::rename_all(tolower), read.csv( file = "2023/complete/WEO2023_Extended_Data_Investment.csv", sep = "," - ) %>% rename_all(tolower) + ) %>% dplyr::rename_all(tolower) ) %>% mutate( "value" = ifelse(.data$unit == "PJ", as.numeric(.data$value) / 1000, as.numeric(.data$value)), diff --git a/R/readIRENA.R b/R/readIRENA.R index 3c12b00a..b901e190 100644 --- a/R/readIRENA.R +++ b/R/readIRENA.R @@ -13,15 +13,14 @@ #' } #' #' @importFrom dplyr mutate rename -#' @importFrom readr read_csv readIRENA <- function(subtype) { if (subtype == "Capacity") { # Reading renewables electricity capacity values in MW from csv - data <- read.csv2("2022/Capacity.csv", sep = ";", skip = 2) + data <- utils::read.csv2("2022/Capacity.csv", sep = ";", skip = 2) } else if (subtype == "Generation") { # Reading renewables electricity generation values in GWh from csv - data <- read.csv2("2022/Generation.csv", sep = ";", skip = 2) + data <- utils::read.csv2("2022/Generation.csv", sep = ";", skip = 2) } else { stop("Not a valid subtype!") } diff --git a/R/readOpenmod.R b/R/readOpenmod.R index a91c97ae..dda78793 100644 --- a/R/readOpenmod.R +++ b/R/readOpenmod.R @@ -11,7 +11,7 @@ #' readOpenmod <- function() { # Read 2010 capacity data - data2010 <- read.csv2("LIMES_gencap_2010.csv", stringsAsFactors = FALSE) + data2010 <- utils::read.csv2("LIMES_gencap_2010.csv", stringsAsFactors = FALSE) data2010$period <- 2010 data2010 <- reshape2::melt(data2010, id.vars = c("dummy", "period"), variable.name = "tech", value.name = "value") data2010$value <- as.numeric(data2010$value) diff --git a/R/readRCP.R b/R/readRCP.R index 8eaaa4e6..77a6b320 100644 --- a/R/readRCP.R +++ b/R/readRCP.R @@ -1,74 +1,62 @@ #' Read RCP #' Read in RCP data #' +#' @param subtype Either 'Waste' or 'AviationShipping' #' @return magpie object of the RCP data #' @author Julian Oeser #' @seealso \code{\link{readSource}} #' @examples -#' #' \dontrun{ a <- readSource(type="RCP", subtype="Waste") #' } #' -#' @param subtype Either 'Waste' or 'AviationShipping' -#' -#' @importFrom readxl read_excel -#' @importFrom dplyr recode -#' @importFrom stats complete.cases - readRCP <- function(subtype) { - - dat <- as.data.frame(do.call(rbind, lapply(list.files(pattern = ".xls$"), read_excel))) + dat <- as.data.frame(do.call(rbind, lapply(list.files(pattern = ".xls$"), readxl::read_excel))) if (subtype == "Waste") { dat <- dat %>% - filter(.data$Region %in% c("R5ASIA", "R5LAM", "R5MAF", "R5OECD", "R5REF"), - grepl("Waste", .data$Variable)) %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% - separate("Variable", c("type", "source"), sep = " - ") %>% - select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source', - 'value') %>% + filter(.data$Region %in% c("R5ASIA", "R5LAM", "R5MAF", "R5OECD", "R5REF"), grepl("Waste", .data$Variable)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% + tidyr::separate("Variable", c("type", "source"), sep = " - ") %>% + select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source', 'value') %>% mutate(region = sub('^R5', '', .data$region)) } else if (subtype == "AviationShipping") { dat <- dat %>% - filter(.data$Region == "World", - grepl("Aviation|Shipping", .data$Variable)) %>% - pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year', - names_transform = list(year = as.numeric)) %>% + filter(.data$Region == "World", grepl("Aviation|Shipping", .data$Variable)) %>% + tidyr::pivot_longer(cols = matches('^[0-9]*$'), + names_to = 'year', + names_transform = list(year = as.numeric)) %>% mutate(Region = "GLO") %>% - separate("Variable", c("type", "source"), sep = " - ") %>% - select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source', - 'value') + tidyr::separate("Variable", c("type", "source"), sep = " - ") %>% + select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source', 'value') } else { stop("Invalid subtype. Must be 'Waste' or 'AviationShipping'") } - dat$scenario <- recode(dat$scenario, - `IMAGE - RCP3-PD (2.6)`="rcp26", - `MiniCAM - RCP 4.5` = "rcp45", - `AIM - RCP 6.0` = "rcp60", - `MESSAGE - RCP 8.5` = "rcp85") - - dat$source <- recode(dat$source, - `Waste (landfills, wastewater, non-energy incineration)`="Waste", - `International Shipping`="InternationalShipping") - - dat$type <- recode(dat$type, - `CH4 emissions`="ch4", - `Sulfur emissions`="so2", - `Black Carbon emissions`="bc", - `Organic Carbon Emissions`="oc", - `CO emissions`="CO", - `NOx emissions`="NOx", - `VOC emissions`="VOC", - `NH3 emissions`="nh3") - - dat <- dat[complete.cases(dat),] - - out <- as.magpie(dat, spatial=1 ,datacol=6) - - return(out) - + dat$scenario <- dplyr::recode(dat$scenario, + `IMAGE - RCP3-PD (2.6)`="rcp26", + `MiniCAM - RCP 4.5` = "rcp45", + `AIM - RCP 6.0` = "rcp60", + `MESSAGE - RCP 8.5` = "rcp85") + + dat$source <- dplyr::recode(dat$source, + `Waste (landfills, wastewater, non-energy incineration)`="Waste", + `International Shipping`="InternationalShipping") + + dat$type <- dplyr::recode(dat$type, + `CH4 emissions`="ch4", + `Sulfur emissions`="so2", + `Black Carbon emissions`="bc", + `Organic Carbon Emissions`="oc", + `CO emissions`="CO", + `NOx emissions`="NOx", + `VOC emissions`="VOC", + `NH3 emissions`="nh3") + + dat <- dat[stats::complete.cases(dat),] + + as.magpie(dat, spatial = 1 , datacol = 6) } diff --git a/R/readStationary.R b/R/readStationary.R index 5111a524..0cbc25d7 100644 --- a/R/readStationary.R +++ b/R/readStationary.R @@ -5,10 +5,9 @@ #' @author Antoine Levesque, Robin Hasse readStationary <- function() { - scenarios <- list( - SSPs = paste0("SSP", 1:5), - SSP2s = paste0("SSP2", c("EU", "_lowEn", "_highDemDEU")), - SDPs = paste0("SDP", c("", "_EI", "_MC", "_RC"))) + scenarios <- list(SSPs = paste0("SSP", 1:5), + SSP2s = paste0("SSP2", c("_lowEn", "_highDemDEU")), + SDPs = paste0("SDP", c("", "_EI", "_MC", "_RC"))) data <- read.magpie("EDGE_TradMod.cs4r") data[is.na(data)] <- 0 @@ -34,5 +33,5 @@ readStationary <- function() { data <- mselect(data, scenario = Reduce(c, scenarios)) } - return(data) + data } diff --git a/R/readUBA.R b/R/readUBA.R index dd9acd32..3d7f6205 100644 --- a/R/readUBA.R +++ b/R/readUBA.R @@ -8,7 +8,6 @@ #' @importFrom readxl read_xlsx #' @importFrom tibble tibble #' @importFrom dplyr select mutate -#' @importFrom rlang sym #' #' @export readUBA <- function() { diff --git a/R/readUNFCCC.R b/R/readUNFCCC.R index 33314ab8..b2b5d561 100644 --- a/R/readUNFCCC.R +++ b/R/readUNFCCC.R @@ -6,19 +6,10 @@ #' #' @seealso [`readSource()`] #' -#' @importFrom dplyr bind_rows bind_cols mutate select -#' +#' @importFrom dplyr bind_rows mutate select #' @importFrom tibble tibble -#' @importFrom tidyr drop_na -#' @importFrom readxl read_xlsx -#' @importFrom rlang sym -#' -#' -#' #' @export #' -#' -#' readUNFCCC <- function() { # structural definition of the source ---- @@ -457,7 +448,7 @@ readUNFCCC <- function() { for (i in intersect(names(sheets), availableSheets)) { s <- suppressMessages( - read_xlsx( + readxl::read_xlsx( path = file.path("2023", dir, file), sheet = i, range = sheets[[i]][["range"]], col_names = c("variable", sheets[[i]][["colnames"]]) @@ -466,7 +457,7 @@ readUNFCCC <- function() { if (!is.null(sheets[[i]][["extraVariables"]])) { extra <- suppressMessages( - read_xlsx(path = file.path("2023", dir, file), sheet = i) %>% + readxl::read_xlsx(path = file.path("2023", dir, file), sheet = i) %>% select(seq(1:4)) ) colnames(extra) <- c("variable", sheets[[i]][["colnames"]]) @@ -480,7 +471,7 @@ readUNFCCC <- function() { suppressMessages( suppressWarnings( s %>% - bind_cols(sheets[[i]]$rows, year = year, region = region) %>% + dplyr::bind_cols(sheets[[i]]$rows, year = year, region = region) %>% select(-1) %>% select(-which(is.na(sheets[[i]][["colnames"]]))) %>% filter(!is.na(!!sym("name"))) %>% diff --git a/R/toolAggregateTimeSteps.R b/R/toolAggregateTimeSteps.R index b1841eb0..31ee29c8 100644 --- a/R/toolAggregateTimeSteps.R +++ b/R/toolAggregateTimeSteps.R @@ -16,8 +16,8 @@ toolAggregateTimeSteps <- function(x, nYears = 5) { periodsMissing <- setdiff(periodsTarget, periods) # periods with difference smaller or greater to next period - periodsSubN <- sort(union(head(periods, -1)[diff(periods) != nYears], - tail(periods, -1)[diff(periods) != nYears])) + periodsSubN <- sort(union(utils::head(periods, -1)[diff(periods) != nYears], + utils::tail(periods, -1)[diff(periods) != nYears])) # periods that need to be aggregated periodsFill <- intersect(periodsTarget, union(periodsSubN, periodsMissing)) diff --git a/R/toolBiomassSupplyAggregate.R b/R/toolBiomassSupplyAggregate.R index 07bbc9f1..b1848bbf 100644 --- a/R/toolBiomassSupplyAggregate.R +++ b/R/toolBiomassSupplyAggregate.R @@ -11,9 +11,11 @@ #' @author Felix Schreyer #' @export #' @importFrom dplyr mutate select rename left_join -#' @importFrom quitte as.quitte - -toolBiomassSupplyAggregate <- function(x, rel=NULL, weight = calcOutput("FAOLand", aggregate = F)[,,"6610",pmatch=TRUE][,"y2010",]){ +#' +toolBiomassSupplyAggregate <- function(x, + rel = NULL, + weight = calcOutput("FAOLand", + aggregate = FALSE)[, , "6610", pmatch = TRUE][, "y2010", ]){ # variable definitions needed for data.frame operations below region <- NULL @@ -34,11 +36,11 @@ toolBiomassSupplyAggregate <- function(x, rel=NULL, weight = calcOutput("FAOLand AgrLandReg <- toolAggregate(AgrLandIso, rel) # calculate share of agricultural land area for each iso-country relative to the MAgPIE region it is in - AgrLandShare <- as.quitte(AgrLandIso) %>% + AgrLandShare <- quitte::as.quitte(AgrLandIso) %>% select(region, value) %>% rename(country = region) %>% left_join(rel) %>% - left_join((as.quitte(AgrLandReg) %>% + left_join((quitte::as.quitte(AgrLandReg) %>% select(region, value) %>% rename(Total = value))) %>% mutate(value = value / Total) %>% diff --git a/R/toolSolarFunctionAggregate.R b/R/toolSolarFunctionAggregate.R index 8dac04f5..58ccecde 100644 --- a/R/toolSolarFunctionAggregate.R +++ b/R/toolSolarFunctionAggregate.R @@ -12,13 +12,7 @@ #' @author Felix Schreyer, Renato Rodrigues, Julian Oeser #' @export #' @importFrom dplyr mutate select rename filter left_join group_by ungroup arrange summarise desc -#' lag full_join -#' @importFrom tidyr spread gather complete -#' @importFrom quitte as.quitte -#' @importFrom stats weighted.mean -#' @importFrom utils packageVersion - - +#' full_join toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", aggregate = FALSE)[,"y2015","FE|Electricity (EJ/yr)"]){ # old part by Julian Oeser @@ -38,7 +32,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag area.csp <- dimSums(x[,,"area"][,,"CSP"][,,c("0-50", "50-100")], dim=c(3.4, 3.3)) # share of area if PV installed only where no csp can be installed - if(packageVersion("magclass") < 6) { + if(utils::packageVersion("magclass") < 6) { area.only.pv.share <- ((area.pv+1)-(area.csp+1)) / (area.pv+1) x <- add_columns(x, c("PVcomp", "PVonly"), 3.2) x[,,"PVonly"] <- x[,,"PV"]*area.only.pv.share[,,"PV"] @@ -65,11 +59,11 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag offset.pv <- 4 offset.csp <- 2 - bins.pv.d2 <- c(rep(head(bins.pv, 1), offset.pv), bins.pv[1:(length(bins.pv)-offset.pv)]) + bins.pv.d2 <- c(rep(utils::head(bins.pv, 1), offset.pv), bins.pv[1:(length(bins.pv)-offset.pv)]) bins.pv.agg <- cbind(bins.pv, bins.pv.d2) colnames(bins.pv.agg) <- c("d1", "d2") - bins.csp.d2 <- c(rep(head(bins.csp, 1), offset.csp), bins.csp[1:(length(bins.csp)-offset.csp)]) + bins.csp.d2 <- c(rep(utils::head(bins.csp, 1), offset.csp), bins.csp[1:(length(bins.csp)-offset.csp)]) bins.csp.agg <- cbind(bins.csp, bins.csp.d2) colnames(bins.csp.agg) <- c("d1", "d2") @@ -156,7 +150,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag MaxProd.Norm[MaxProd.Norm == 0] <- 0.01 # convert data to quitte format because more convenient for the following operations - df.x <- as.quitte(x[,,techs][,,dist]) %>% + df.x <- quitte::as.quitte(x[,,techs][,,dist]) %>% # drop 0 and NA potentials #filter(value > 0 , !is.na(value)) %>% # rename Bin to Full load hours, convert to numerical @@ -166,11 +160,11 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag # calculate maxprod (potential/maximum possible production in grade) in GWh by: capacity * FLH (Full Load Hours) / 1000 df.pot <- df.x %>% - spread(Type, value) %>% + tidyr::pivot_wider(names_from = "Type") %>% mutate( maxprod = capacity * FLH / 1000) %>% # remove those locations with NA or 0 area or potential filter( maxprod > 0, area > 0) %>% - gather(Type, value, capacity, area, maxprod) + tidyr::gather(Type, value, capacity, area, maxprod) # calculate cumulated capacity, maxprod (potential) and area in descending grade order # to obtain how much can be produced in total at this FLH or higher FLH @@ -181,7 +175,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag ungroup() # process 2015 IEA electricity production - df.prod.2015 <- as.quitte(MaxProd.Norm) %>% + df.prod.2015 <- quitte::as.quitte(MaxProd.Norm) %>% # FE 2015 in GWh/yr mutate( production = value / 3.6 * 1e6) %>% select(region, production) @@ -198,7 +192,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag ## join normalized cumulative maxprod agin to df with cumulative maxprod, area, capacity #to be able to interplolate all three measures df.cuml <- df.cuml %>% - spread(Type, value) %>% + tidyr::pivot_wider(names_from = "Type") %>% left_join(df.maxprod.norm) # get total potential per region, technology, distance, @@ -232,10 +226,10 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag # order by descending FLH arrange(desc(FLH)) %>% # recompute potential as difference between normalized potential of neighboring FLH bins - mutate( diff = maxprod.norm - lag(maxprod.norm)) %>% + mutate( diff = maxprod.norm - dplyr::lag(maxprod.norm)) %>% mutate( diff = ifelse(is.na(diff), maxprod.norm, diff)) %>% # expand by n.intp equally spaced values between the given maxprod.norm values - complete(maxprod.norm = seq.between(maxprod.norm, diff, n.intp)) %>% + tidyr::complete(maxprod.norm = seq.between(maxprod.norm, diff, n.intp)) %>% # interpolate maxprod, area, capacity linearly at new data points between given data points mutate(FLH = zoo::na.approx(FLH, na.rm = F), maxprod = zoo::na.approx(maxprod, na.rm = F), area = zoo::na.approx(area, na.rm = F), capacity = zoo::na.approx(capacity, na.rm = F)) %>% @@ -246,11 +240,11 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag # by performing gradient operation df.fine.grades <- df.interpolate %>% select(region, Technology, Distance, FLH, maxprod.norm, maxprod, capacity, area) %>% - gather(Type, value, maxprod, capacity, area) %>% + tidyr::gather(Type, value, maxprod, capacity, area) %>% group_by(region, Type, Technology, Distance) %>% arrange(desc(FLH)) %>% # calculate value per fine grade by gradient of cumulative value - mutate( diff = value - lag(value)) %>% + mutate( diff = value - dplyr::lag(value)) %>% # highest FLH has no predecessor, so take diff to 0, i.e. value itself mutate( diff = ifelse(is.na(diff), value, diff)) %>% ungroup() %>% @@ -347,9 +341,9 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag # spread maxprod column to have weight for FLH aggregation in next step df.map.REMIND <- df.map.REMIND %>% - spread(Type, value) %>% + tidyr::pivot_wider(names_from = "Type") %>% mutate(maxprod.weight = maxprod) %>% - gather(Type, value, maxprod, capacity, area) + tidyr::gather(Type, value, maxprod, capacity, area) @@ -359,11 +353,11 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag group_by(region, Type, Technology, Distance, grade) %>% # aggregate fine grade to REMIND grades: FLH <- FLHs weighted by maxprod of fine grade # potential, area, capacity <- sum over all values potential/area/capacity REMIND grade - summarise( FLH = weighted.mean(FLH, maxprod.weight), value = sum(value)) %>% + summarise( FLH = stats::weighted.mean(FLH, maxprod.weight), value = sum(value)) %>% ungroup() %>% # add FLH to Type column to make it another dimension like maxprod, area etc. - spread(Type, value) %>% - gather(Type, value, capacity, area, maxprod, FLH) %>% + tidyr::pivot_wider(names_from = "Type") %>% + tidyr::gather(Type, value, capacity, area, maxprod, FLH) %>% select(region, Type, Technology, Distance, grade, value) # get overlap of first data point in last grade with second last grade @@ -378,7 +372,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag ungroup() %>% filter( maxprod.norm == Max) %>% select(-Max) %>% - gather(Type, Seclast, maxprod, area, capacity) %>% + tidyr::gather(Type, Seclast, maxprod, area, capacity) %>% select(region, Technology, Distance, Type, Seclast) # calculate maxprod, area, capacity difference between last data point before last grade @@ -386,7 +380,7 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag df.lastgrade.overlap <- df.interpolate %>% # filter for maxprod, area, capacity at lower bound of last grade (threshold) filter(maxprod.norm < grade.breaks[length(grade.breaks)]) %>% - gather(Type, value, maxprod, area, capacity) %>% + tidyr::gather(Type, value, maxprod, area, capacity) %>% group_by(region, Technology, Distance) %>% mutate( Max = max(maxprod.norm)) %>% ungroup() %>% @@ -412,8 +406,8 @@ toolSolarFunctionAggregate <- function(x, rel=NULL, weight = calcOutput("FE", ag summarise( value = sum(value), FLH = mean(FLH)) %>% ungroup() %>% mutate( grade = length(grade.breaks)+1) %>% - spread(Type, value) %>% - gather(Type, value, capacity, area, maxprod, FLH) %>% + tidyr::pivot_wider(names_from = "Type") %>% + tidyr::gather(Type, value, capacity, area, maxprod, FLH) %>% # join overlap of first data point in last grade with second last grade from last grade left_join(df.lastgrade.overlap) %>% # subtract overlap from maxprod, area, capacity of last grade diff --git a/README.md b/README.md index 85721802..ea1783eb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MadRat REMIND Input Data Package -R package **mrremind**, version **0.214.0** +R package **mrremind**, version **0.214.0.9015** [![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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Lavinia Baumstark . +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.214.0.9015, . A BibTeX entry for LaTeX users is @@ -47,9 +47,9 @@ A BibTeX entry for LaTeX users is @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}, - date = {2025-01-29}, + date = {2025-02-03}, year = {2025}, url = {https://github.com/pik-piam/mrremind}, - note = {Version: 0.214.0}, + note = {Version: 0.214.0.9015}, } ``` diff --git a/man/calcCapacityFactorHist.Rd b/man/calcCapacityFactorHist.Rd index 06b8894c..f31d1f77 100644 --- a/man/calcCapacityFactorHist.Rd +++ b/man/calcCapacityFactorHist.Rd @@ -16,7 +16,7 @@ magpie object of the capacity factor data provides capacity factor values } \examples{ -\dontrun{ +\dontrun{ calcOutput("CapacityFactor") } diff --git a/man/calcEmiPollutantExo.Rd b/man/calcEmiPollutantExo.Rd index 04cc1aa5..4c894039 100644 --- a/man/calcEmiPollutantExo.Rd +++ b/man/calcEmiPollutantExo.Rd @@ -20,11 +20,9 @@ calcEmiPollutantExo calculate EmiPollutantExo based on RCP data } \examples{ - \dontrun{ a <- calcOutput(type="EmiPollutantExo") } - } \seealso{ \code{\link{calcOutput}} diff --git a/man/calcMacroInvestments.Rd b/man/calcMacroInvestments.Rd new file mode 100644 index 00000000..5fcf89ed --- /dev/null +++ b/man/calcMacroInvestments.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcMacroInvestments.R +\name{calcMacroInvestments} +\alias{calcMacroInvestments} +\title{Calculate 2005 macroeconomic capital investments} +\usage{ +calcMacroInvestments() +} +\value{ +magpie object with the requested output data either on country or on +regional level depending on the choice of argument "aggregate" or a list of information +if supplementary is set to TRUE. +} +\description{ +Compute macroeconomic capital investments based on investments shares from the PWT and GDP scenarios from +\link{mrdrivers}. The final investments are the product of the two. +} +\seealso{ +\itemize{ +\item See the vignette \code{vignette("scenarios", "mrdrivers")} for information on the GDP scenarios. +\item \code{\link[=readPWT]{readPWT()}} for information on the PWT version used. +} +} diff --git a/man/calcSolar.Rd b/man/calcSolar.Rd index fed540c9..d1038b30 100644 --- a/man/calcSolar.Rd +++ b/man/calcSolar.Rd @@ -15,11 +15,8 @@ calcSolar calculate Area, Capacity and Energy for photovoltaics (PV) and contentrated solar power (CSP) } \examples{ - \dontrun{ a <- calcOutput(type="Solar") } - - } \seealso{ \code{\link{calcOutput}} diff --git a/man/convertGini.Rd b/man/convertGini.Rd index d16ed6cc..c5fb2444 100644 --- a/man/convertGini.Rd +++ b/man/convertGini.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/convertGini.R +% Please edit documentation in R/readGini.R \name{convertGini} \alias{convertGini} \title{Convert Gini} diff --git a/man/convertGlobalCCSinstitute.Rd b/man/convertGlobalCCSinstitute.Rd index 076ef2e2..b5486846 100644 --- a/man/convertGlobalCCSinstitute.Rd +++ b/man/convertGlobalCCSinstitute.Rd @@ -7,8 +7,7 @@ convertGlobalCCSinstitute(x, subtype = "08-09-2017") } \arguments{ -\item{x}{A \code{\link[magclass:magclass-package]{magpie}} object returned by -\code{\link[=readGlobalCCSinstitute]{readGlobalCCSinstitute()}}.} +\item{x}{A \code{\link[magclass:magclass-package]{magpie}} object returned by \code{\link[=readGlobalCCSinstitute]{readGlobalCCSinstitute()}}.} \item{subtype}{Project Database version to read, one of - \code{'08-09-2017'}: Data apparently from June 2017. diff --git a/man/fullREMIND.Rd b/man/fullREMIND.Rd index a90ef5c7..fad36fff 100644 --- a/man/fullREMIND.Rd +++ b/man/fullREMIND.Rd @@ -7,8 +7,7 @@ fullREMIND() } \description{ -Function that produces the complete regional data set required for the -REMIND model. +Function that produces the complete regional data set required for the REMIND model. } \examples{ \dontrun{ @@ -17,7 +16,7 @@ fullREMIND() } \seealso{ -\code{\link{readSource}},\code{\link{getCalculations}},\code{\link{calcOutput}} +\code{\link{readSource}}, \code{\link{getCalculations}}, \code{\link{calcOutput}} } \author{ Lavinia Baumstark diff --git a/man/readGini.Rd b/man/readGini.Rd index 8d417cb9..952bb644 100644 --- a/man/readGini.Rd +++ b/man/readGini.Rd @@ -10,19 +10,19 @@ readGini() magpie object of the Gini data } \description{ -Read Gini coefficients for SSP scenarios from Rao et al., Futures, 2018. Data has been provided by the authors, but will be made publicly available as well. -This contains data for 184 countries and from 2011 onwards. +Read Gini coefficients for SSP scenarios from Rao et al., Futures, 2018. Data has been provided by the authors, but +will be made publicly available as well. This contains data for 184 countries and from 2011 onwards. } \details{ Copied from the documentation provided by the authors: -This sheet contains the original Gini projections for 43 countries from the underlying empirical model (See reference to RSP 2016 in the main paper) -and the extrapolations to all countries using the methodology described in the article. The country codes are the World Bank codes. +This sheet contains the original Gini projections for 43 countries from the underlying empirical model +(See reference to RSP 2016 in the main paper) and the extrapolations to all countries using the methodology +described in the article. The country codes are the World Bank codes. } \examples{ - -\dontrun{ a <- readSource(type="Gini") +\dontrun{ +a <- readSource(type="Gini") } - } \seealso{ \code{\link{readSource}} \code{\link{convertGini}} diff --git a/man/readIEA_WEIO_2014.Rd b/man/readIEA_WEIO_2014.Rd deleted file mode 100644 index 9d6676ca..00000000 --- a/man/readIEA_WEIO_2014.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readIEA_WEIO_2014.R -\name{readIEA_WEIO_2014} -\alias{readIEA_WEIO_2014} -\title{IEA World Energy Investment Outlook (2014)} -\usage{ -readIEA_WEIO_2014() -} -\value{ -A \code{\link[=madrat_mule]{madrat_mule()}} with a list containing the \link{tibble} \code{data} with -2014–20 average annual investments into \verb{Energy intensive} and -\verb{Non-energy intensive} industry, in $bn 2012, and the \link{tibble} -\code{country_groups} with \verb{IEA region}s and corresponding \code{iso3c} country -codes. -} -\description{ -Read projected 2014-20 investments into industry energy efficiency from the -\href{http://www.iea.org/publications/freepublications/publication/weo-2014-special-report---investment.html}{IEA World Energy Investment Outlook (2014)} -} diff --git a/man/readRCP.Rd b/man/readRCP.Rd index f1dcb895..08dfd469 100644 --- a/man/readRCP.Rd +++ b/man/readRCP.Rd @@ -18,7 +18,6 @@ Read RCP Read in RCP data } \examples{ - \dontrun{ a <- readSource(type="RCP", subtype="Waste") } diff --git a/man/toolBiomassSupplyAggregate.Rd b/man/toolBiomassSupplyAggregate.Rd index 841391d4..cb89a17d 100644 --- a/man/toolBiomassSupplyAggregate.Rd +++ b/man/toolBiomassSupplyAggregate.Rd @@ -10,7 +10,8 @@ The slope parameter (b) is multiplied by a weight. The weight is the inverse of toolBiomassSupplyAggregate( x, rel = NULL, - weight = calcOutput("FAOLand", aggregate = F)[, , "6610", pmatch = TRUE][, "y2010", ] + weight = calcOutput("FAOLand", aggregate = FALSE)[, , "6610", pmatch = TRUE][, "y2010", + ] ) } \arguments{