diff --git a/.buildlibrary b/.buildlibrary index d1498702..436566eb 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,9 +1,11 @@ -ValidationKey: '38858820' +ValidationKey: '39260760' AcceptedWarnings: - Invalid URL: .* +- 'Warning: package ''.*'' was built under R version' AcceptedNotes: - Imports includes .* non-default packages. - All declared Imports should be used. +- unable to verify current time AutocreateReadme: yes allowLinterWarnings: yes enforceVersionUpdate: yes diff --git a/CITATION.cff b/CITATION.cff index e663861c..550ca0b6 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.194.1 -date-released: '2024-10-24' +version: 0.196.0 +date-released: '2024-11-04' abstract: The mrremind packages contains data preprocessing for the REMIND model. authors: - family-names: Baumstark diff --git a/DESCRIPTION b/DESCRIPTION index d798d626..2f92724b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrremind Title: MadRat REMIND Input Data Package -Version: 0.194.1 -Date: 2024-10-24 +Version: 0.196.0 +Date: 2024-11-04 Authors@R: c( person("Lavinia", "Baumstark", , "lavinia@pik-potsdam.de", role = c("aut", "cre")), person("Renato", "Rodrigues", role = "aut"), @@ -41,26 +41,23 @@ Depends: R (>= 2.10.0) Imports: assertr, - broom, - car, countrycode, data.table, dplyr, edgeTransport (>= 1.5.5), GDPuc (>= 1.3.0), ggplot2, - Hmisc, luscale, madrat (>= 3.7.1), magclass (>= 6.16.1), magrittr, mrcommons (>= 1.44.12), mrdrivers (>= 2.0.0), + mrindustry (>= 0.3.0), nnls, purrr, quitte (>= 0.3105.0), R.utils, - readODS, readr, readxl, reshape2, diff --git a/NAMESPACE b/NAMESPACE index 6ec30e17..3b623c32 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,14 +3,9 @@ export(calcAGEB) export(calcBP) export(calcCCScapacity) -export(calcCement) -export(calcCementShare) -export(calcChemicalFeedstocksShare) -export(calcClinker_to_cement_ratio) export(calcEDGAR7Fgases) export(calcEEAGHGProjections) export(calcEmber) -export(calcEmissionFactorsFeedstocks) export(calcExpertGuess) export(calcFloorspace) export(calcGlobalEnergyMonitor) @@ -18,23 +13,11 @@ export(calcHRE) export(calcIEA_ETP) export(calcIEA_EVOutlook) export(calcIEA_WorldEnergyOutlook) -export(calcIndustry_CCS_limits) -export(calcIndustry_EEK) -export(calcIndustry_Value_Added) -export(calcODYM_RECC) export(calcOtherFossilInElectricity) export(calcPlasticsEoL) export(calcProjectPipelines) -export(calcShareIndFE) -export(calcSteelStock) -export(calcSteel_Projections) export(calcUBA) export(calcUNFCCC) -export(calcUNIDO) -export(calcindustry_max_secondary_steel_share) -export(calcindustry_specific_FE_limits) -export(calcindustry_subsectors_specific) -export(convertADVANCE_WP2) export(convertAGEB) export(convertEDGAR7Fgases) export(convertEmber) @@ -42,13 +25,9 @@ export(convertGlobalCCSinstitute) export(convertGlobalEnergyMonitor) export(convertHRE) export(convertIEA_CCUS) -export(convertMueller) export(convertUBA) export(convertUNFCCC) -export(convertUNIDO) -export(convertUSGS) export(fullTHRESHOLDS) -export(readADVANCE_WP2) export(readAGEB) export(readAR6GWP) export(readEDGAR7Fgases) @@ -60,31 +39,19 @@ export(readIAEA_PRIS) export(readIEA_CCUS) export(readIEA_HSMR) export(readIEA_WEIO_2014) -export(readMueller) -export(readODYM_RECC) -export(readPauliuk) export(readUBA) export(readUNFCCC) -export(readUNIDO) -export(readUSGS) -export(readindustry_subsectors_specific) -export(readvanRuijven2016) -export(readworldsteel) export(toolAggregateTimeSteps) export(toolBiomassSupplyAggregate) export(toolCubicFunctionAggregate) export(toolCubicFunctionDisaggregate) export(toolFillEU34Countries) export(toolSolarFunctionAggregate) -export(tool_expand_tibble) import(madrat) import(magclass) -importFrom(GDPuc,convertGDP) importFrom(assertr,assert) -importFrom(assertr,in_set) importFrom(assertr,not_na) importFrom(assertr,verify) -importFrom(assertr,within_bounds) importFrom(data.table,":=") importFrom(data.table,data.table) importFrom(data.table,rbindlist) @@ -94,22 +61,16 @@ importFrom(data.table,setnames) importFrom(dplyr,across) importFrom(dplyr,anti_join) importFrom(dplyr,arrange) -importFrom(dplyr,as_tibble) -importFrom(dplyr,between) importFrom(dplyr,bind_cols) importFrom(dplyr,bind_rows) -importFrom(dplyr,case_when) importFrom(dplyr,desc) importFrom(dplyr,distinct) importFrom(dplyr,everything) importFrom(dplyr,filter) -importFrom(dplyr,first) importFrom(dplyr,full_join) importFrom(dplyr,group_by) importFrom(dplyr,inner_join) importFrom(dplyr,lag) -importFrom(dplyr,last) -importFrom(dplyr,lead) importFrom(dplyr,left_join) importFrom(dplyr,matches) importFrom(dplyr,min_rank) @@ -121,35 +82,13 @@ importFrom(dplyr,relocate) importFrom(dplyr,rename) importFrom(dplyr,rename_all) importFrom(dplyr,right_join) -importFrom(dplyr,row_number) importFrom(dplyr,select) importFrom(dplyr,semi_join) importFrom(dplyr,starts_with) importFrom(dplyr,summarise) importFrom(dplyr,summarize) -importFrom(dplyr,tibble) importFrom(dplyr,tribble) importFrom(dplyr,ungroup) -importFrom(dplyr,vars) -importFrom(ggplot2,aes) -importFrom(ggplot2,coord_cartesian) -importFrom(ggplot2,expand_limits) -importFrom(ggplot2,facet_wrap) -importFrom(ggplot2,geom_area) -importFrom(ggplot2,geom_line) -importFrom(ggplot2,geom_path) -importFrom(ggplot2,geom_point) -importFrom(ggplot2,ggplot) -importFrom(ggplot2,ggsave) -importFrom(ggplot2,guide_legend) -importFrom(ggplot2,labs) -importFrom(ggplot2,scale_colour_manual) -importFrom(ggplot2,scale_fill_discrete) -importFrom(ggplot2,scale_fill_manual) -importFrom(ggplot2,scale_linetype_manual) -importFrom(ggplot2,scale_shape_manual) -importFrom(ggplot2,theme) -importFrom(ggplot2,theme_minimal) importFrom(madrat,madratAttach) importFrom(madrat,toolAggregate) importFrom(magclass,matchDim) @@ -159,37 +98,27 @@ importFrom(quitte,as.quitte) importFrom(quitte,calc_addVariable) importFrom(quitte,cartesian) importFrom(quitte,character.data.frame) -importFrom(quitte,df_populate_range) -importFrom(quitte,duplicate) importFrom(quitte,inline.data.frame) importFrom(quitte,interpolate_missing_periods) importFrom(quitte,interpolate_missing_periods_) -importFrom(quitte,list_to_data_frame) importFrom(quitte,madrat_mule) -importFrom(quitte,magclass_to_tibble) -importFrom(quitte,order.levels) importFrom(quitte,overwrite) importFrom(quitte,removeColNa) -importFrom(quitte,seq_range) importFrom(quitte,sum_total_) importFrom(readr,col_character) importFrom(readr,col_skip) importFrom(readr,cols) importFrom(readr,read_csv) importFrom(readr,read_delim) -importFrom(readr,read_rds) -importFrom(readr,write_rds) importFrom(readxl,excel_sheets) importFrom(readxl,read_excel) importFrom(readxl,read_xlsx) importFrom(rlang,"!!!") importFrom(rlang,"!!") importFrom(rlang,.data) -importFrom(rlang,.env) importFrom(rlang,is_empty) importFrom(rlang,sym) importFrom(rlang,syms) -importFrom(stats,SSlogis) importFrom(stats,aggregate) importFrom(stats,coef) importFrom(stats,complete.cases) @@ -197,31 +126,23 @@ importFrom(stats,lm) importFrom(stats,na.exclude) importFrom(stats,na.omit) importFrom(stats,na.pass) -importFrom(stats,nls) -importFrom(stats,sd) importFrom(stats,weighted.mean) importFrom(tibble,as_tibble) importFrom(tibble,tibble) -importFrom(tibble,tribble) importFrom(tidyr,complete) -importFrom(tidyr,crossing) importFrom(tidyr,drop_na) -importFrom(tidyr,everything) importFrom(tidyr,expand_grid) importFrom(tidyr,extract) -importFrom(tidyr,fill) importFrom(tidyr,gather) importFrom(tidyr,nest) importFrom(tidyr,nesting) importFrom(tidyr,pivot_longer) importFrom(tidyr,pivot_wider) -importFrom(tidyr,replace_na) importFrom(tidyr,separate) importFrom(tidyr,spread) importFrom(tidyr,unite) importFrom(tidyr,unnest) importFrom(tidyselect,all_of) -importFrom(tidyselect,everything) importFrom(utils,download.file) importFrom(utils,head) importFrom(utils,packageVersion) @@ -230,6 +151,3 @@ importFrom(utils,read.csv2) importFrom(utils,read.table) importFrom(utils,tail) importFrom(utils,unzip) -importFrom(zoo,na.approx) -importFrom(zoo,na.fill) -importFrom(zoo,rollmean) diff --git a/R/EDGE-Industry.R b/R/EDGE-Industry.R deleted file mode 100644 index 490e3723..00000000 --- a/R/EDGE-Industry.R +++ /dev/null @@ -1,2653 +0,0 @@ -#' EDGE-Industry -#' -#' Functions for calculating industry activity trajectories. -#' -#' @md -#' @param subtype One of -#' - `production` Returns trajectories of primary and secondary steel -#' production (`calcSteel_Projections()`). -#' - `secondary.steel.max.share` Returns the maximum share of secondary steel -#' in total steel production (`calcSteel_Projections()`). -#' - `physical` Returns physical production trajectories for cement -#' (`calcIndustry_Value_Added()`). -#' - `economic` Returns value added trajectories for all subsectors -#' (`calcIndustry_Value_Added()`). -#' @param match.steel.historic.values Should steel production trajectories match -#' historic values? -#' @param match.steel.estimates Should steel production trajectories match -#' exogenous estimates? `NULL` or one of -#' - `IEA_ETP` IEA 2017 Energy Transition Pathways steel production totals for -#' OECD and Non-OECD countries from the _Reference Technologies Scenario_ -#' until 2060, and original growth rates after that. -#' @param save.plots `NULL` (default) if no plots are saved, or the path to save -#' directories to. -#' @param China_Production A data frame with columns `period` and -#' `total.production` prescribing total production for China to have, -#' disregarding results from the stock saturation model. -#' -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, `description`, `min`, and `max`. -#' -#' @author Michaja Pehl -#' -#' @seealso [`calcOutput()`] -#' -#' @importFrom assertr assert not_na verify within_bounds -#' @importFrom dplyr case_when bind_rows between distinct first last n -#' mutate pull right_join select semi_join vars -#' @importFrom ggplot2 aes coord_cartesian expand_limits facet_wrap geom_area -#' geom_line geom_path geom_point ggplot ggsave guide_legend labs -#' scale_colour_manual scale_fill_discrete scale_fill_manual -#' scale_linetype_manual scale_shape_manual theme theme_minimal -#' @importFrom quitte character.data.frame df_populate_range duplicate -#' list_to_data_frame madrat_mule magclass_to_tibble order.levels -#' seq_range sum_total_ -#' @importFrom readr write_rds -#' @importFrom stats nls SSlogis sd -#' @importFrom tibble as_tibble tibble tribble -#' @importFrom tidyr expand_grid pivot_longer pivot_wider replace_na -#' @importFrom zoo na.approx rollmean - -#' @rdname EDGE-Industry -#' @export -calcSteel_Projections <- function(subtype = 'production', - match.steel.historic.values = TRUE, - match.steel.estimates = 'none', - save.plots = NULL, - China_Production = NULL) { - - if (!is.null(save.plots)) { - if (!all(isTRUE(file.info(save.plots)$isdir), - 448L == bitwAnd(file.info(save.plots)$mode, 448L))) { - stop('No writable directory `save.plots`: ', save.plots) - } - } - - produce_plots_and_tables <- TRUE - - . <- NULL - - # get EDGE-Industry switches ---- - # FIXME: remove before deploying - EDGE_scenario_switches <- bind_rows( - tribble( - ~scenario, ~`EDGE-Industry_steel.stock.estimate`, - 'SDP', 'low', - 'SDP_EI', 'low', - 'SDP_MC', 'low', - 'SDP_RC', 'low', - 'SSP1', 'low', - 'SSP2', 'med', - 'SSP2EU', 'med', - 'SSP3', 'med', - 'SSP4', 'med', - 'SSP5', 'high') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - tribble( - ~scenario, ~`EDGE-Industry_scenario.mask.OECD`, - 'SSP4', 'SSP2') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - tribble( - ~scenario, ~`EDGE-Industry_scenario.mask.non-OECD`, - 'SSP4', 'SSP1') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - # steel stock lifetime convergence ---- - tribble( - ~scenario, ~`EDGE-Industry_steel.stock.lifetime.base.scenario`, - 'SDP', 'SSP2', - 'SDP_EI', 'SSP2', - 'SDP_MC', 'SSP2', - 'SDP_RC', 'SSP2', - 'SSP1', 'SSP2', - 'SSP2', 'SSP2', - 'SSP2EU', 'SSP2', - 'SSP3', 'SSP2', - 'SSP4', 'SSP4', - 'SSP5', 'SSP2') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - tribble( - ~scenario, ~`EDGE-Industry_steel.stock.lifetime.convergence.year`, - 'SDP', '2100', - 'SDP_EI', '2100', - 'SDP_MC', '2100', - 'SDP_RC', '2100', - 'SSP1', '2100', - 'SSP2', '2100', - 'SSP2EU', '2100', - 'SSP3', '2100', - 'SSP4', '2010', - 'SSP5', '2100') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - tribble( - ~scenario, ~`EDGE-Industry_steel.stock.lifetime.convergence.factor`, - 'SDP', '1.25', - 'SDP_EI', '1.25', - 'SDP_MC', '1.25', - 'SDP_RC', '1.25', - 'SSP1', '1.25', - 'SSP2', '1', - 'SSP2EU', '1', - 'SSP3', '1', - 'SSP4', '1', - 'SSP5', '0.75') %>% - pivot_longer(-'scenario', names_to = 'switch'), - - NULL) %>% - pivot_wider(names_from = 'switch') - - `EDGE-Industry_scenario_switches` <- EDGE_scenario_switches %>% - select( - 'scenario', - `steel.stock.estimate` = 'EDGE-Industry_steel.stock.estimate', - `scenario.mask.OECD` = - 'EDGE-Industry_scenario.mask.OECD', - `scenario.mask.non-OECD` = - 'EDGE-Industry_scenario.mask.non-OECD', - `steel.stock.lifetime.base.scenario` = - 'EDGE-Industry_steel.stock.lifetime.base.scenario', - `steel.stock.lifetime.convergence.year` = - 'EDGE-Industry_steel.stock.lifetime.convergence.year', - `steel.stock.lifetime.convergence.factor` = - 'EDGE-Industry_steel.stock.lifetime.convergence.factor') - - # load required data ---- - ## region mapping for aggregation ---- - region_mapping <- toolGetMapping(name = 'regionmapping_21_EU11.csv', - type = 'regional', - where = 'mappingfolder') %>% - as_tibble() %>% - select(region = 'RegionCode', iso3c = 'CountryCode') - - ### extra region mapping for Belgium-Luxembourg ---- - region_mapping__Belgium_Luxembourg <- region_mapping %>% - filter(.data$iso3c %in% c('BEL', 'LUX')) %>% - distinct(.data$region) %>% - verify(1 == length(.data$region)) %>% - mutate(iso3c = 'blx') - - ## country mapping for Müller data ---- - country_mapping <- readSource(type = 'Mueller', subtype = 'countries', - convert = FALSE) %>% - madrat_mule() - - ## steel stock lifetimes ---- - lifetime <- readSource(type = 'Pauliuk', subtype = 'lifetime', - convert = FALSE) %>% - madrat_mule() - - ### add iso3c codes ---- - lifetime <- inner_join( - lifetime, - - country_mapping %>% - mutate(country = ifelse(.data$iso3c %in% c('BEL', 'FRA', 'LUX', 'NLD'), - 'France+Benelux', .data$country)), - - 'country' - ) - - ## set of OECD countries ---- - OECD_iso3c <- toolGetMapping(name = 'regionmappingOECD.csv', - type = 'regional', - where = 'mappingfolder') %>% - as_tibble() %>% - select(iso3c = 'CountryCode', region = 'RegionCode') %>% - filter('OECD' == .data$region) %>% - pull('iso3c') - - - - ## historic per-capita steel stock estimates ---- - steel_stock_per_capita <- readSource(type = 'Mueller', subtype = 'stocks', - convert = FALSE) %>% - madrat_mule() %>% - # remove Netherlands Antilles, only use Bonaire, Sint Eustatius and Saba; - # Curaçao; and Sint Maarten (Dutch part) - filter('ANT' != .data$iso3c) - - ## historic per-capita GDP ---- - GDPpC_history <- readSource(type = 'James', subtype = 'IHME_USD05_PPP_pc', - convert = FALSE) %>% - as_tibble() %>% - select('iso3c' = 'ISO3', 'year' = 'Year', 'value') %>% - GDPuc::toolConvertGDP(unit_in = 'constant 2005 US$MER', - unit_out = mrdrivers::toolGetUnitDollar(), - replace_NAs = 'with_USA') %>% - rename(GDPpC = 'value') %>% - character.data.frame() - - ## historic population ---- - population_history <- calcOutput(type = 'PopulationPast', - PopulationPast = 'UN_PopDiv', - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = .data$Region, year = .data$Year, - population = .data$Value) %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year), - # million people * 1e6/million = people - population = .data$population * 1e6) - - ## GDP projections ---- - GDP <- calcOutput(type = 'GDP', average2020 = FALSE, naming = 'scenario', - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = .data$Data1, iso3c = .data$Region, year = .data$Year, - GDP = .data$Value) %>% - character.data.frame() %>% - mutate(scenario = sub('^gdp_', '', .data$scenario), - year = as.integer(.data$year), - # $m * 1e6 $/$m = $ - GDP = .data$GDP * 1e6) - - ## population ---- - population <- calcOutput('Population', naming = 'scenario', - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = .data$Data1, iso3c = .data$Region, year = .data$Year, - population = .data$Value) %>% - character.data.frame() %>% - mutate(scenario = sub('^pop_', '', .data$scenario), - year = as.integer(.data$year), - # million people * 1e6/million = people - population = .data$population * 1e6) - - # estimate steel stock distribution ---- - regression_data <- steel_stock_per_capita %>% - inner_join(GDPpC_history, c('iso3c', 'year')) %>% - inner_join(population_history, c('iso3c', 'year')) - - regression_parameters <- tibble() - for (.estimate in unique(regression_data$estimate)) { - - Asym <- regression_data %>% - filter(.estimate == .data$estimate) %>% - group_by(.data$year) %>% - summarise(Asym = 1.1 * Hmisc::wtd.quantile(x = .data$steel.stock.per.capita, - weights = .data$population, - probs = 0.99), - .groups = 'drop') %>% - pull('Asym') %>% - max() - - coefficients <- lm( - formula = car::logit(x, adjust = 0.025) ~ y, - data = regression_data %>% - filter(.estimate == .data$estimate, - between(.data$steel.stock.per.capita, 0, Asym)) %>% - mutate(x = .data$steel.stock.per.capita / Asym) %>% - select(.data$x, y = .data$GDPpC) - ) %>% - getElement('coefficients') %>% - setNames(NULL) - - xmid <- -coefficients[1] / coefficients[2] - scal <- 1 / coefficients[2] - - regression_parameters <- bind_rows( - regression_parameters, - - nls(formula = steel.stock.per.capita - ~ Asym / (1 + exp((xmid - GDPpC) / scal)), - weights = population, - data = regression_data %>% - filter(.estimate == .data$estimate), - start = list(Asym = Asym, xmid = xmid, scal = scal), - algorithm = 'port', - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(estimate = .estimate) - ) - } - - # estimate future steel stocks ---- - steel_stock_estimates <- full_join( - # GDP, population to calculate per-capita GDP - full_join(GDP, population, c('scenario', 'iso3c', 'year')) %>% - assert(not_na, everything()), - - # regression parameters mapped to GDP and population scenarios - full_join( - regression_parameters, - - `EDGE-Industry_scenario_switches` %>% - select('scenario', estimate = 'steel.stock.estimate'), - - 'estimate' - ), - - 'scenario' - ) %>% - # make sure all scenarios have associated regression parameters - assert( - not_na, .data$Asym, .data$scal, .data$xmid, - error_fun = function(errors, data) { - rows <- lapply(errors, function(x) { x$error_df$index }) %>% - unlist() %>% - unique() - message <- paste0('Unmatched estimates for steel projection regression', - 'parameters') - stop(paste(c(message, format(head(as.data.frame(data[rows,])))), - collapse = '\n'), - call. = FALSE) - }) %>% - # calculate steel stock estimates using logistic function - mutate( - value = SSlogis(input = .data$GDP / .data$population, - Asym = .data$Asym, xmid = .data$xmid, scal = .data$scal), - source = 'computation') %>% - select('scenario', 'iso3c', 'year', 'value', 'source') %>% - assert(not_na, everything()) - - steel_stock_estimates <- bind_rows( - steel_stock_estimates, - - steel_stock_per_capita %>% - filter(.data$year >= min(steel_stock_estimates$year)) %>% - full_join( - `EDGE-Industry_scenario_switches` %>% - select('scenario', estimate = 'steel.stock.estimate'), - - 'estimate' - ) %>% - select(.data$scenario, .data$iso3c, .data$year, - value = .data$steel.stock.per.capita) %>% - mutate(source = 'Pauliuk') - ) %>% - full_join(region_mapping, 'iso3c') %>% - pivot_wider(names_from = 'source') %>% - assert(not_na, .data$computation, - error_fun = function(errors, data) { - rows <- lapply(errors, function(x) { x$error_df$index }) %>% - unlist() %>% - unique() - message <- paste('Mismatch between Pauliuk and estimation', - 'regions') - stop(paste(c(message, format(head(as.data.frame(data[rows,])))), - collapse = '\n'), - call. = FALSE) - }) - - # TODO: harmonise estimates for historic time steps between scenarios, so as - # to having identical estimates between SSP1/2/5/... up to 2020 - - ## smooth transition ---- - # from Pauliuk data to per-capita GDP-based estimates over 30 years - fade_end <- max(steel_stock_per_capita$year) - fade_start <- fade_end - 30 - - steel_stock_estimates <- steel_stock_estimates %>% - mutate( - l = pmax(0, pmin(1, (.data$year - fade_start) / (fade_end - fade_start))), - mix = pmax(0, .data$l * .data$computation - + (1 - .data$l) * .data$Pauliuk), - steel.stock.per.capita = ifelse(is.na(.data$mix), - .data$computation, .data$mix)) %>% - select('scenario', 'iso3c', 'region', 'year', 'steel.stock.per.capita') %>% - assert(not_na, everything()) - - rm(list = c('fade_start', 'fade_end')) - - ## update SSP4 ---- - # SSP4 uses SSP2 estimates for OECD countries and SSP1 estimates for non-OECD - # countries - steel_stock_estimates <- bind_rows( - # non-masked scenarios - steel_stock_estimates %>% - anti_join( - `EDGE-Industry_scenario_switches` %>% - select(.data$scenario, - .data$scenario.mask.OECD, .data$`scenario.mask.non-OECD`) %>% - filter( !is.na(.data$scenario.mask.OECD) - & !is.na(.data$`scenario.mask.non-OECD`)) %>% - select(.data$scenario), - - 'scenario' - ) %>% - assert(not_na, everything()), - - # masked scenarios, OECD countries - left_join( - `EDGE-Industry_scenario_switches` %>% - select('scenario', 'scenario.mask.OECD') %>% - filter(!is.na(.data$scenario.mask.OECD)) %>% - rename(scenario.mask = 'scenario', - scenario = 'scenario.mask.OECD'), - - steel_stock_estimates %>% - filter(.data$iso3c %in% OECD_iso3c), - - 'scenario' - ) %>% - select(-'scenario', 'scenario' = 'scenario.mask') %>% - assert(not_na, everything()), - - # masked scenarios, non-OECD countries - left_join( - `EDGE-Industry_scenario_switches` %>% - select('scenario', 'scenario.mask.non-OECD') %>% - filter(!is.na(.data$`scenario.mask.non-OECD`)) %>% - rename(scenario.mask = 'scenario', - scenario = 'scenario.mask.non-OECD'), - - steel_stock_estimates %>% - filter(!.data$iso3c %in% OECD_iso3c), - - 'scenario' - ) %>% - select(-'scenario', 'scenario' = 'scenario.mask') %>% - assert(not_na, everything()) - ) %>% - assert(not_na, everything()) - - ## calculate regional and global totals, as well as absolute stocks ---- - steel_stock_estimates <- steel_stock_estimates %>% - assert(not_na, everything()) %>% - full_join(population, c('scenario', 'iso3c', 'year')) %>% - group_by(.data$scenario, .data$year, .data$region) %>% - sum_total_(group = 'iso3c', value = 'steel.stock.per.capita', - weight = 'population') %>% - ungroup(.data$region) %>% - sum_total_(group = 'iso3c', value = 'steel.stock.per.capita', - weight = 'population') %>% - ungroup() %>% - filter(!(.data$region == 'World' & .data$iso3c != 'Total')) %>% - # absolute stocks - mutate(steel.stock = .data$steel.stock.per.capita * .data$population) %>% - assert(not_na, everything()) - - if ('steel_stock_estimates' == subtype) { - return(list( - x = steel_stock_estimates %>% - madrat_mule(), - weight = NULL)) - } - - # calculate lifetime projections ---- - - # steel stock lifetimes are projected to converge from regional averages in - # 2010 towards the global average in 2100 - lifetime_regions <- lifetime %>% - select(.data$iso3c, .data$lifetime) %>% - full_join(filter(GDP, 2010 == .data$year), 'iso3c') %>% - inner_join(region_mapping, 'iso3c') %>% - filter(!is.na(.data$lifetime)) %>% - group_by(.data$scenario, .data$region) %>% - summarise( - lifetime = round(sum(.data$lifetime * .data$GDP) / sum(.data$GDP)), - .groups = 'drop') - - lifetime_global <- lifetime %>% - select(.data$iso3c, .data$lifetime) %>% - full_join(filter(GDP, 2010 == .data$year), 'iso3c') %>% - inner_join(region_mapping, 'iso3c') %>% - filter(!is.na(lifetime)) %>% - group_by(.data$scenario) %>% - summarise( - lifetime = round(sum(.data$lifetime * .data$GDP) / sum(.data$GDP)), - .groups = 'drop') - - lifetime_projections <- inner_join( - lifetime_regions %>% - rename(`2010` = .data$lifetime), - - lifetime_global %>% - mutate(region = 'World') %>% - complete(nesting(!!sym('scenario'), !!sym('lifetime')), - region = unique(region_mapping$region)) %>% - rename(`2100` = .data$lifetime), - - c('scenario', 'region') - ) %>% - pivot_longer(c(.data$`2010`, .data$`2100`), - names_to = 'year', names_transform = list(year = as.integer), - values_to = 'lifetime', - values_transform = list(lifetime = as.numeric)) - - # steel stock lifetimes for specific scenarios in 2100 can be defined relative - # to the lifetime of a in a , times a - # - lifetime_projections <- bind_rows( - lifetime_projections %>% - filter(2010 == .data$year), - - inner_join( - `EDGE-Industry_scenario_switches` %>% - select( - .data$scenario, - base.scenario = .data$steel.stock.lifetime.base.scenario, - convergence.year = .data$steel.stock.lifetime.convergence.year, - convergence.factor = .data$steel.stock.lifetime.convergence.factor - ) %>% - mutate(convergence.factor = as.numeric(.data$convergence.factor), - convergence.year = as.integer(.data$convergence.year)), - - lifetime_projections, - - c('base.scenario' = 'scenario', 'convergence.year' = 'year') - ) %>% - mutate(lifetime = round(.data$convergence.factor * .data$lifetime), - year = 2100) %>% - select('scenario', 'region', 'year', 'lifetime') - ) %>% - interpolate_missing_periods_(periods = list(year = 1950:2150), - value = 'lifetime', expand.values = TRUE) - - # calculate steel trade ---- - steel_yearbook_data <- madrat_mule(readSource('worldsteel', convert = FALSE)) - - ## compute historic steel values ---- - steel_historic <- bind_rows( - # combine Belgium and Luxembourg, because apparent steel use is reported - # for both together - steel_yearbook_data %>% - filter(!.data$iso3c %in% c('BEL', 'LUX')), - - steel_yearbook_data %>% - filter(.data$iso3c %in% c('BEL', 'LUX')) %>% - group_by(.data$name, .data$year) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - iso3c = 'blx', - .groups = 'drop') - ) %>% - # rename to shorter variable names - inner_join( - tribble( - ~name, ~variable, - 'Apparent Steel Use (Crude Steel Equivalent)', 'use', - 'Total Production of Crude Steel', 'production', - 'Production in Oxygen-Blown Converters', 'prod.BOF', - 'Production in Open Hearth Furnaces', 'prod.OHF', - 'Production in Electric Arc Furnaces', 'prod.EAF', - 'Pig Iron Production', 'prod.pig', - 'DRI Production', 'prod.DRI'), - - 'name' - ) %>% - select('iso3c', 'variable', 'year', 'value') %>% - # kt/year * 1e-3 Mt/kt = Mt/year - mutate(value = .data$value * 1e-3) %>% - pivot_wider(names_from = 'variable') %>% - - mutate(imports = pmax(0, .data$use - .data$production), - exports = pmin(0, .data$use - .data$production)) %>% - pivot_longer(cols = c(-'iso3c', -'year'), names_to = 'variable', - values_drop_na = TRUE) %>% - # add region mapping - inner_join( - bind_rows( - region_mapping, - region_mapping__Belgium_Luxembourg), - - 'iso3c') - - ## compute regional/global aggregates ---- - steel_historic <- bind_rows( - steel_historic, - - steel_historic %>% - group_by(.data$region, .data$year, .data$variable) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - iso3c = 'Total', - .groups = 'drop'), - - steel_historic %>% - group_by(.data$year, .data$variable) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - region = 'World', - .groups = 'drop') - ) - - ## compute trade shares ---- - # calculate regional trade shares - steel_trade_shares_regional <- steel_historic %>% - filter('Total' != .data$iso3c, - .data$variable %in% c('use', 'imports', 'exports')) %>% - # exclude regions that don't have valid import/export data - group_by(.data$iso3c, .data$year) %>% - filter(3 == n()) %>% - group_by(.data$region, .data$year, .data$variable) %>% - summarise(value = sum(.data$value), .groups = 'drop') %>% - pivot_wider(names_from = 'variable') %>% - mutate(import.share = .data$imports / .data$use, - export.share = .data$exports / .data$use) %>% - select('region', 'year', 'import.share', 'export.share') %>% - pivot_longer(c('import.share', 'export.share'), names_to = 'variable') - - # calculate country trade shares, defaulting to regional shares - steel_trade_shares <- steel_historic %>% - filter(.data$variable %in% c('imports', 'exports', 'use'), - !('Total' == .data$iso3c & 'use' != .data$variable)) %>% - pivot_wider(names_from = 'variable') %>% - full_join( - steel_trade_shares_regional %>% - mutate(variable = paste0(.data$variable, '.regional')) %>% - pivot_wider(names_from = 'variable') %>% - inner_join(region_mapping, 'region'), - - c('region', 'iso3c', 'year') - ) %>% - mutate( - import.share = ifelse(!is.na(.data$imports), - .data$imports / .data$use, - .data$import.share.regional), - export.share = ifelse(!is.na(.data$exports), - .data$exports / .data$use, - .data$export.share.regional), - imports = ifelse(!is.na(.data$imports), - .data$imports, - .data$use * .data$import.share), - exports = ifelse(!is.na(.data$exports), - .data$exports, - .data$use * .data$export.share), - trade = .data$imports + .data$exports, - trade.share = ifelse(!is.na(.data$use), - .data$trade / .data$use, - .data$import.share + .data$export.share)) %>% - select('iso3c', 'region', 'year', 'import.share', 'export.share', - 'trade.share') - - # calculate steel production ---- - steel_trade_share_2015 <- steel_trade_shares %>% - filter('Total' != .data$iso3c, - 2015 == .data$year) %>% - select('region', 'iso3c', 'trade.share') - - # duplicate Belgium and Luxembourg from Belgium-Luxembourg - steel_trade_share_2015 <- bind_rows( - steel_trade_share_2015 %>% - filter(!.data$iso3c %in% c('blx', 'BEL', 'LUX')), - - steel_trade_share_2015 %>% - filter('blx' == .data$iso3c) %>% - pivot_wider(names_from = 'iso3c', values_from = 'trade.share') %>% - mutate(LUX = .data$blx) %>% - rename(BEL = .data$blx) %>% - pivot_longer(-'region', names_to = 'iso3c', values_to = 'trade.share') - ) - - ## aggregate primary and secondary production ---- - steel_historic_prod <- steel_historic %>% - filter(!is.na(.data$iso3c), - .data$variable %in% c('production', 'prod.BOF', 'prod.OHF', - 'prod.EAF', 'prod.DRI')) %>% - pivot_wider(names_from = 'variable', values_fill = 0) %>% - mutate( - primary.production = .data$prod.BOF + .data$prod.OHF + .data$prod.DRI, - # TODO: for VEN & IRN DRI > EAF -- figure out what is going on - secondary.production = pmax(0, .data$prod.EAF - .data$prod.DRI), - primary.production = .data$primary.production - * .data$production - / ( .data$primary.production - + .data$secondary.production), - secondary.production = .data$secondary.production - * .data$production - / ( .data$primary.production - + .data$secondary.production)) %>% - select('iso3c', 'region', 'year', 'primary.production', - 'secondary.production') %>% - pivot_longer(cols = c('primary.production', 'secondary.production'), - names_to = 'variable') %>% - filter(0 != .data$value) - - ### split Belgium and Luxembourg by population ---- - steel_historic_prod <- bind_rows( - steel_historic_prod %>% - filter('blx' != .data$iso3c), - - steel_historic_prod %>% - filter('blx' == .data$iso3c) %>% - select(-'region') %>% - left_join( - population %>% - filter(.data$iso3c %in% c('BEL', 'LUX'), - .data$year %in% unique(steel_historic_prod$year)) %>% - group_by(.data$year, .data$scenario) %>% - summarise(population = sum(.data$population), - .groups = 'drop_last') %>% - summarise(population = mean(.data$population), .groups = 'drop'), - - 'year' - ) %>% - mutate(value = .data$value / .data$population) %>% - select('year', 'variable', 'value') %>% - left_join( - population %>% - filter(.data$iso3c %in% c('BEL', 'LUX'), - .data$year %in% unique(steel_historic_prod$year)) %>% - inner_join(region_mapping, 'iso3c') %>% - group_by(.data$region, .data$iso3c, .data$year) %>% - summarise(population = mean(.data$population), .groups = 'drop'), - - 'year' - ) %>% - mutate(value = .data$value * .data$population) %>% - select('region', 'iso3c', 'year', 'variable', 'value') - ) %>% - assert(not_na, everything()) - - ## calculate secondary steel max share ---- - secondary.steel.max.switches <- calcOutput( - type = 'industry_max_secondary_steel_share', - scenarios = unique(population$scenario), - regions = unique(region_mapping$region), - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = 'Data1', region = 'Data2', name = 'Data3', - value = 'Value') %>% - mutate(name = paste0('secondary.steel.max.share.', .data$name)) %>% - pivot_wider() %>% - character.data.frame() - - tmp <- full_join( - steel_historic_prod %>% - filter('Total' != .data$iso3c) %>% - mutate(match = TRUE), - - secondary.steel.max.switches %>% - select('scenario', 'secondary.steel.max.share.from') %>% - mutate(match = TRUE, - secondary.steel.max.share.from = - as.integer(.data$secondary.steel.max.share.from)), - - 'match' - ) %>% - select(-'match') %>% - group_by(!!!syms(c('scenario', 'region', 'iso3c', 'variable'))) %>% - filter(.data$year <= .data$secondary.steel.max.share.from) %>% - group_by(!!!syms(c('scenario', 'region', 'iso3c', 'year', 'variable'))) %>% - summarise(value = mean(.data$value), .groups = 'drop') %>% - sum_total_('iso3c') %>% - pivot_wider(names_from = 'variable', values_fill = list(value = 0)) %>% - mutate(share = .data$secondary.production - / (.data$primary.production + .data$secondary.production)) %>% - select('scenario', 'region', 'iso3c', 'year', 'share') - - secondary.steel.max.share <- bind_rows( - tmp, - - tmp %>% - distinct(.data$scenario, .data$region, .data$iso3c) %>% - full_join( - secondary.steel.max.switches %>% - select('scenario', 'region', year = 'secondary.steel.max.share.by', - share = 'secondary.steel.max.share.target') %>% - mutate(year = as.integer(.data$year), - share = as.numeric(.data$share)), - - c('scenario', 'region') - ) - ) %>% - interpolate_missing_periods_( - periods = list('year' = seq_range(range(steel_stock_estimates$year))), - value = 'share', expand.values = TRUE) - - # expand regional values to missing countries - secondary.steel.max.share <- bind_rows( - secondary.steel.max.share %>% - filter('Total' != .data$iso3c), - - secondary.steel.max.share %>% - filter('Total' == .data$iso3c) %>% - select(-'iso3c') %>% - right_join( - region_mapping %>% - anti_join(secondary.steel.max.share, c('region', 'iso3c')), - - 'region' - ) %>% - assert(not_na, everything()) - ) - - ## calculate primary and secondary production ---- - - # Imports (> 0 and exports (< 0) are scaled by factors m such that they - # balance globally. If imports are twice as large as exports, the imbalance - # is solved by scaling imports down by a factor twice as large as the factor - # with which exports are scaled up. E.g.: - # trade <- c(1, 2, -7) - # m <- (1 + sum(trade) / sum(abs(trade)) * -sign(trade)) - # adjusted.trade <- trade * m - # sum(adjusted.trade) == 0 - - production_estimates <- steel_stock_estimates %>% - filter('Total' != .data$iso3c) %>% - inner_join(steel_trade_share_2015 %>% select(-'region'), 'iso3c') %>% - left_join(lifetime_projections, c('scenario', 'region', 'year')) %>% - select(-'steel.stock.per.capita', -'population') %>% - assert(not_na, everything()) %>% - pivot_longer(c(-'scenario', -'iso3c', -'region', -'year')) %>% - interpolate_missing_periods_( - periods = list('year' = seq_range(range(.$year)))) %>% - pivot_wider() %>% - full_join( - secondary.steel.max.share %>% - rename(secondary.steel.max.share = 'share'), - - c('scenario', 'region', 'iso3c', 'year') - ) %>% - group_by(!!!syms(c('scenario', 'region', 'iso3c'))) %>% - mutate( - # stock additions: rolling average of stock changes (stocks might decrease - # with decreasing population, but still become obsolete and need - # replacement) over five years - stock.additions = rollmean( - pmax(0, - .data$steel.stock - lag(.data$steel.stock, order_by = .data$year, - default = first(.data$steel.stock))), - k = 5, fill = 'extend', na.rm = TRUE), - # depreciation: last years steel stock deprecated by 1/lifetime - depreciation = lag(x = .data$steel.stock, order_by = .data$year, - default = first(.data$steel.stock)) - / lag(.data$lifetime, order_by = .data$year, - default = first(.data$lifetime)), - # new stock: stock increases and replacements for deprecated old stock - new.stock = .data$stock.additions + .data$depreciation, - # recycable: 90 % of deprecated steel stock are assumed to be recycled - recyclable = 0.9 * .data$depreciation, # FIXME: pull parameter out - # trade: share of new stock serviced by trade - trade = .data$new.stock * .data$trade.share) %>% - group_by(.data$scenario, .data$year) %>% - mutate(m.factor = ( sum(.data$trade, na.rm = TRUE) - / sum(abs(.data$trade), na.rm = TRUE) - )) %>% - group_by(.data$scenario, .data$region, .data$iso3c) %>% - mutate( - adj.trade = ( .data$trade - * ifelse(0 < .data$trade, 1 - .data$m.factor, - 1 + .data$m.factor) - ), - adj.trade.share = .data$trade / .data$new.stock, - production = .data$new.stock - .data$adj.trade) %>% - ungroup() %>% - select('scenario', 'region', 'iso3c', 'year', 'production', 'recyclable', - 'steel.stock', 'secondary.steel.max.share', 'depreciation', - 'adj.trade') - - production_estimates <- production_estimates %>% - mutate( - secondary.production = pmin( - .data$secondary.steel.max.share * .data$production, - .data$recyclable), - primary.production = .data$production - .data$secondary.production) %>% - select('scenario', 'region', 'iso3c', 'year', 'steel.stock', 'depreciation', - 'primary.production', 'secondary.production', - trade = 'adj.trade') %>% - filter(min(.data$year) < .data$year) %>% - pivot_longer(c('steel.stock', 'depreciation', 'primary.production', - 'secondary.production', 'trade'), - names_to = 'variable') %>% - group_by(.data$scenario, .data$region, .data$year, .data$variable) %>% - sum_total_('iso3c') %>% - ungroup() - - ## calculate production limits of secondary steel---- - # FIXME: move to separate function - production_limits <- production_estimates %>% - filter('depreciation' == .data$variable) %>% - select(-'variable') - - ## construct output ---- - x <- production_estimates %>% - semi_join(region_mapping, c('region', 'iso3c')) %>% - filter(min(steel_historic$year) <= .data$year) %>% - right_join( - tribble( - ~variable, ~pf, - 'primary.production', 'ue_steel_primary', - 'secondary.production', 'ue_steel_secondary'), - - 'variable' - ) %>% - assert(not_na, everything()) %>% - # t/year * 1e-6 Gt/t = Gt/year - mutate(value = .data$value * 1e-9, - scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'pf', 'year', 'value') %>% - as.magpie(spatial = 2, temporal = 4, data = 5) - - # match historic values ---- - if (match.steel.historic.values) { - tmp <- full_join( - production_estimates %>% - filter(.data$variable %in% c('primary.production', - 'secondary.production')), - - steel_historic_prod %>% - filter(.data$variable %in% c('primary.production', - 'secondary.production')) %>% - rename(historic = 'value') %>% - # Mt/year * 1e6 t/Mt = t/year - mutate(historic = .data$historic * 1e6) %>% - expand_grid(scenario = unique(production_estimates$scenario)), - - c('scenario', 'region', 'iso3c', 'year', 'variable') - ) - - tmp_factor <- tmp %>% - group_by(.data$scenario, .data$region, .data$iso3c, .data$variable) %>% - arrange(.data$year) %>% - mutate( - factor = .data$historic / .data$value, - factor = case_when( - # countries w/o historic production fade production in over 20 years - all(is.na(.data$historic)) ~ - pmin(1, pmax(0, (.data$year - max(steel_historic_prod$year)) / 20)), - # shift country production to meet historic production in the - # first/last year for which historic data is available - .data$year < first(.data$year * as.integer(Inf != .data$historic), - order_by = .data$year, na_rm = TRUE) ~ - first(.data$factor, order_by = .data$year, na_rm = TRUE), - .data$year > last(.data$year * as.integer(Inf != .data$historic), - order_by = .data$year, na_rm = TRUE) ~ - last(.data$factor, order_by = .data$year, na_rm = TRUE), - TRUE ~ .data$factor), - # if value is 0, x/0 is Inf, and 0 * (x/0) is NaN - factor = ifelse(is.infinite(.data$factor), 0, .data$factor)) %>% - ungroup() %>% - select(-'value', -'historic') %>% - interpolate_missing_periods_(periods = list(year = unique(.$year)), - value = 'factor', - expand.values = TRUE) - - tmp <- full_join( - tmp, - tmp_factor, - - c('scenario', 'region', 'iso3c', 'year', 'variable') - ) %>% - mutate(value = .data$value * .data$factor) %>% - ungroup() %>% - select(-'historic', -'factor') %>% - assert(not_na, everything()) - - ## make zero values explicit ---- - tmp <- tmp %>% - semi_join(region_mapping, c('region', 'iso3c')) %>% - complete(.data$scenario, .data$variable, - nesting(!!sym('region'), !!sym('iso3c')), - year = unique(!!sym('year')), - fill = list(value = 0)) %>% - assert(not_na, everything()) - - ## update max secondary steel shares ---- - update.secondary.steel.max.share <- function(production, - secondary.steel.max.share) { - full_join( - secondary.steel.max.share %>% - rename(max.share = 'share'), - - production %>% - pivot_wider(names_from = 'variable') %>% - mutate(share = .data$secondary.production - / ( .data$primary.production - + .data$secondary.production)) %>% - replace_na(list(share = 0)), - - c('scenario', 'region', 'iso3c', 'year') - ) %>% - mutate(share = pmax(.data$share, .data$max.share, na.rm = TRUE)) %>% - select(all_of(colnames(secondary.steel.max.share))) %>% - assert(not_na, everything()) - } - - secondary.steel.max.share <- update.secondary.steel.max.share( - tmp, secondary.steel.max.share) - - ## construct output ---- - x <- tmp %>% - filter(min(steel_historic_prod$year) <= .data$year) %>% - semi_join(region_mapping, c('region', 'iso3c')) %>% - right_join( - tribble( - ~variable, ~pf, - 'primary.production', 'ue_steel_primary', - 'secondary.production', 'ue_steel_secondary'), - - 'variable' - ) %>% - assert(not_na, everything()) %>% - # t/year * 1e-9 Gt/t = Gt/year - mutate(value = .data$value * 1e-9, - scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'pf', 'year', 'value') %>% - as.magpie(spatial = 2, temporal = 4, data = 5) - } - - # match exogenous data for China ---- - if (is.data.frame(China_Production)) { - China_Production <- China_Production %>% - interpolate_missing_periods(period = seq_range(range(.$period)), - value = 'total.production', - method = 'spline') %>% - mutate(total.production = .data$total.production * 1e6) - - tmp <- tmp %>% - filter('SSP2EU' == .data$scenario, - 'CHN' == .data$iso3c, - max(steel_historic_prod$year) < .data$year, - .data$variable %in% c('primary.production', - 'secondary.production')) %>% - group_by(.data$scenario, .data$iso3c, .data$year) %>% - summarise(production = sum(.data$value), .groups = 'drop') %>% - left_join(China_Production, c('year' = 'period')) %>% - mutate(factor = .data$total.production / .data$production) %>% - select('iso3c', 'year', 'factor') %>% - expand_grid(scenario = unique(production_estimates$scenario)) %>% - complete(nesting(!!sym('scenario')), - iso3c = setdiff(unique(production_estimates$iso3c), 'Total'), - year = unique(production_estimates$year)) %>% - group_by(.data$scenario, .data$iso3c) %>% - mutate( - factor = case_when( - max(steel_historic_prod$year) >= .data$year ~ 1, - TRUE ~ .data$factor), - factor = case_when( - is.na(.data$factor) ~ last(na.omit(.data$factor)), - TRUE ~ .data$factor)) %>% - ungroup() %>% - left_join(tmp, c('scenario', 'iso3c', 'year')) %>% - mutate(value = .data$value * .data$factor) %>% - select(-'factor') %>% - assert(not_na, everything()) - - ## construct output ---- - x <- tmp %>% - filter(min(steel_historic_prod$year) <= .data$year) %>% - semi_join(region_mapping, c('region', 'iso3c')) %>% - right_join( - tribble( - ~variable, ~pf, - 'primary.production', 'ue_steel_primary', - 'secondary.production', 'ue_steel_secondary'), - - 'variable' - ) %>% - assert(not_na, everything()) %>% - # t/year * 1e-9 Gt/t = Gt/year - mutate(value = .data$value * 1e-9, - scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'pf', 'year', 'value') %>% - as.magpie(spatial = 2, temporal = 4, data = 5) - } - - # match exogenous estimates ---- - ## IEA ETP 2017 ---- - if ('IEA_ETP' == match.steel.estimates) { - # projected SSP2 production aggregated into OECD/Non-OECD regions - # w/o Chinese production if that is exogenously prescribed - if (!is.data.frame(China_Production)) { - projected_production <- tmp %>% - filter('SSP2' == .data$scenario) %>% - mutate( - region = ifelse(.data$iso3c %in% OECD_iso3c, 'OECD', 'Non-OECD')) %>% - group_by(.data$region, .data$year) %>% - summarise(value = sum(.data$value) * 1e-6, .groups = 'drop') - } else { - projected_production <- tmp %>% - filter('SSP2' == .data$scenario, - 'CHN' != .data$iso3c) %>% - mutate( - region = ifelse(.data$iso3c %in% OECD_iso3c, 'OECD', 'Non-OECD')) %>% - group_by(.data$region, .data$year) %>% - summarise(value = sum(.data$value) * 1e-6, .groups = 'drop') - } - - # IEA ETP RTS production, minus Chinese production if exogenously prescribed - ETP_production <- readSource('IEA_ETP', 'industry', convert = FALSE) %>% - `[`(,,'RTS.Industry|Materials production|Crude steel.Mt') %>% - as.data.frame() %>% - as_tibble() %>% - select(region = 'Region', year = 'Year', ETP = 'Value') %>% - filter(.data$region %in% c('OECD', 'Non-OECD')) %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year)) - - if (is.data.frame(China_Production)) { - ETP_production <- bind_rows( - ETP_production %>% - filter('OECD' == .data$region), - - ETP_production %>% - filter('Non-OECD' == .data$region) %>% - left_join( - China_Production %>% - mutate(total.production = .data$total.production * 1e-6), - - c('year' = 'period') - ) %>% - mutate(total.production = ifelse(!is.na(.data$total.production), - .data$total.production, - last(na.omit(.data$total.production))), - ETP = .data$ETP - .data$total.production) %>% - select(-'total.production') - ) %>% - verify(expr = .data$ETP > 0, - description = paste('exogenous Chinese production does not exceed', - 'IEA ETP Non-OECD production')) - } - - scaling_factor <- inner_join( - projected_production, - ETP_production, - - c('region', 'year') - ) %>% - group_by(.data$region) %>% - mutate( - factor = .data$ETP / .data$value, - factor = .data$factor / first(.data$factor, order_by = .data$year)) - - # If exogenous Chinese production trajectories gobble up all Non-OECD - # production, temper the scaling factor to only meet 2060 production exactly - if (is.data.frame(China_Production)) { - scaling_factor <- scaling_factor %>% - mutate(factor = ( .data$factor - + ( first(.data$factor, order_by = .data$year) - + ( ( last(.data$factor, order_by = .data$year) - - first(.data$factor, order_by = .data$year) - ) - / (max(.data$year) - min(.data$year)) - * (.data$year - min(.data$year)) - ) - ) - ) - / 2) - } - - scaling_factor <- scaling_factor %>% - select(-'value', -'ETP') %>% - bind_rows( - tibble( - year = c(max(steel_historic_prod$year), 2100), factor = 1)) %>% - complete(year = unique(tmp$year)) %>% - filter(!is.na(.data$region)) %>% - mutate(factor = na.approx(object = .data$factor, - x = .data$year, - yleft = first(na.omit(.data$factor)), - yright = last(na.omit(.data$factor)))) %>% - ungroup() %>% - assert(not_na, everything()) - - if (!is.data.frame(China_Production)) { - IEA_ETP_matched <- tmp %>% - mutate(OECD.region = ifelse(.data$iso3c %in% OECD_iso3c, - 'OECD', 'Non-OECD')) %>% - full_join(scaling_factor, c('year', 'OECD.region' = 'region')) %>% - mutate(value = .data$value * .data$factor) %>% - select(-'factor', -'OECD.region') %>% - complete(nesting(!!!syms(c('scenario', 'region', 'iso3c', 'variable'))), - year = unique(.$year), - fill = list(value = 0)) %>% - arrange('scenario', 'region', 'iso3c', 'year', 'variable') - } else { - IEA_ETP_matched <- tmp %>% - filter('CHN' != .data$iso3c) %>% - mutate(OECD.region = ifelse(.data$iso3c %in% OECD_iso3c, - 'OECD', 'Non-OECD')) %>% - full_join(scaling_factor, c('year', 'OECD.region' = 'region')) %>% - mutate(value = .data$value * .data$factor) %>% - select(-'factor', -'OECD.region') %>% - bind_rows( - tmp %>% - filter('CHN' == .data$iso3c) - ) %>% - complete(nesting(!!!syms(c('scenario', 'region', 'iso3c', 'variable'))), - year = unique(.$year), - fill = list(value = 0)) %>% - arrange('scenario', 'region', 'iso3c', 'year', 'variable') - } - - ## update max secondary steel share ---- - secondary.steel.max.share <- update.secondary.steel.max.share( - IEA_ETP_matched, secondary.steel.max.share) - - ## construct output ---- - x <- IEA_ETP_matched %>% - filter(min(steel_historic_prod$year) <= .data$year) %>% - semi_join(region_mapping, c('region', 'iso3c')) %>% - right_join( - tribble( - ~variable, ~pf, - 'primary.production', 'ue_steel_primary', - 'secondary.production', 'ue_steel_secondary'), - - 'variable' - ) %>% - assert(not_na, everything()) %>% - # t/year * 1e-9 Gt/t = Gt/year - mutate(value = .data$value * 1e-9, - scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'pf', 'year', 'value') %>% - as.magpie(spatial = 2, temporal = 4, data = 5) - } else if ('none' != match.steel.estimates) { - stop('Unknown setting \'', match.steel.estimates, - '\' for match.steel.estimates') - } - - ### return secondary steel max share ---- - if ('secondary.steel.max.share' == subtype) { - return( - list(x = secondary.steel.max.share %>% - filter(.data$year %in% unique(quitte::remind_timesteps$period), - 'Total' != .data$iso3c) %>% - mutate(scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'year', 'share') %>% - as.magpie(spatial = 2, temporal = 3, data = 4), - weight = calcOutput( - type = 'Steel_Projections', - match.steel.historic.values = match.steel.historic.values, - match.steel.estimates = match.steel.estimates, - aggregate = FALSE, years = unique(quitte::remind_timesteps$period), - supplementary = FALSE) %>% - dimSums(dim = 3.2), - unit = 'fraction', - description = 'maximum secondary steel production share' - ) - ) - } - - if (!is.null(save.plots)) { - - p <- ggplot() + - geom_area( - data = x %>% - as_tibble() %>% - filter('gdp_SSP2EU' == .data$scenario) %>% - left_join(region_mapping, 'iso3c') %>% - full_join( - tibble( - pf = c('ue_steel_primary', 'ue_steel_secondary'), - production = factor(c('Primary Production', - 'Secondary Production'), - rev(c('Primary Production', - 'Secondary Production')))), - - 'pf' - ) %>% - group_by(.data$region, .data$year, .data$production) %>% - summarise(value = sum(.data$value), .groups = 'drop') %>% - sum_total_('region', name = 'World'), - mapping = aes(x = !!sym('year'), y = !!sym('value') * 1e-3, - fill = !!sym('production'))) + - facet_wrap(~ region, scales = 'free_y') + - labs(x = NULL, y = 'Mt Steel/year') + - scale_fill_manual(values = c('Primary Production' = 'orange', - 'Secondary Production' = 'yellow'), - name = NULL) + - coord_cartesian(xlim = c(NA, 2100), expand = FALSE) + - theme_minimal() + - theme(legend.position = c(1, 0), - legend.justification = c(1, 0)) - - ggsave(plot = p, filename = '6_Steel_production.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '6_Steel_production.rds')) - } - - # return statement ---- - return(list(x = x, - weight = NULL, - unit = 'Gt steel/year', - description = 'primary and secondary steel production')) -} - -#' @rdname EDGE-Industry -#' @export -calcIndustry_Value_Added <- function(subtype = 'physical', - match.steel.historic.values = TRUE, - match.steel.estimates = 'none', - save.plots = NULL, - China_Production = NULL) { - if (!is.null(save.plots)) { - if (!all(isTRUE(file.info(save.plots)$isdir), - 448L == bitwAnd(file.info(save.plots)$mode, 448L))) { - stop('No writable directory `save.plots`: ', save.plots) - } - } - - linetype_scenarios <- c(regression = 'dashed', - # SSP1 = 'dotted', - SSP2 = 'solid', - # SSP5 = 'dashed' - NULL) - - # load required data ---- - ## region mapping for aggregation ---- - region_mapping <- toolGetMapping(name = 'regionmapping_21_EU11.csv', - type = 'regional', - where = 'mappingfolder') %>% - as_tibble() %>% - select(region = 'RegionCode', iso3c = 'CountryCode') - - ## UNIDO INSTATA2 data ---- - INDSTAT <- readSource('UNIDO', 'INDSTAT2') %>% - as_tibble() %>% - filter(!is.na(.data$value)) %>% - left_join(region_mapping, 'iso3c') - - - ## population data ---- - population <- calcOutput('Population', naming = 'scenario', - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = .data$Data1, iso3c = .data$Region, year = .data$Year, - population = .data$Value) %>% - character.data.frame() %>% - mutate(scenario = sub('^pop_', '', .data$scenario), - year = as.integer(.data$year), - # million people * 1e6/million = people - population = .data$population * 1e6) - - ## GDP data ---- - GDP <- calcOutput(type = 'GDP', average2020 = FALSE, naming = 'scenario', - aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = .data$Data1, iso3c = .data$Region, year = .data$Year, - GDP = .data$Value) %>% - character.data.frame() %>% - mutate(scenario = sub('^gdp_', '', .data$scenario), - year = as.integer(.data$year), - # $m * 1e6 $/$m = $ - GDP = .data$GDP * 1e6) - - ## ---- load cement production data ---- - data_cement_production <- calcOutput('Cement', aggregate = FALSE, - warnNA = FALSE) %>% - magclass_to_tibble() %>% - select(-'data') %>% - filter(!is.na(.data$value)) - - # calc manufacturing share in GDP ---- - manufacturing_share <- INDSTAT %>% - filter('manufacturing' == .data$subsector) %>% - pivot_wider(names_from = 'subsector') %>% - inner_join( - GDP %>% - filter(max(INDSTAT$year) >= .data$year) %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - inner_join( - population %>% - filter(max(INDSTAT$year) >= .data$year) %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) - - # regress per-capita industry value added ---- - regression_data <- manufacturing_share %>% - duplicate(region = 'World') %>% - pivot_longer(c('population', 'GDP', 'manufacturing')) %>% - group_by(.data$region, .data$iso3c, .data$year, .data$name) %>% - summarise(value = sum(.data$value), .groups = 'drop') %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - mutate( - # mfg.share = .data$manufacturing / .data$GDP, FIXME - GDPpC = .data$GDP / .data$population) - - regression_parameters <- tibble() - for (r in sort(unique(regression_data$region))) { - regression_parameters <- bind_rows( - regression_parameters, - - nls(formula = manufacturing / population ~ a * exp(b / GDPpC), - data = regression_data %>% - filter(.data$region == r, - 'Total' == .data$iso3c), - start = list(a = 1000, b = -2000), - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(region = r) - ) - } - - # FIXME add scenario differentiation of regression parameters ---- - - # project total manufacturing share ---- - ## calculate GDPpC projection scenarios ---- - GDPpC <- full_join(population, GDP, c('scenario', 'iso3c', 'year')) %>% - mutate(GDPpC = .data$GDP / .data$population) %>% - full_join(region_mapping, 'iso3c') - - ## converge regional towards global limit ---- - parameter_a_world <- regression_parameters %>% - filter('World' == .data$region) %>% - getElement('a') - - regression_parameters_converging <- regression_parameters %>% - filter('World' != .data$region) %>% - mutate(year = min(regression_data$year)) %>% - complete(nesting(!!!syms(c('region', 'a', 'b'))), - year = min(regression_data$year):2150) %>% - mutate(a = .data$a + ( (parameter_a_world - .data$a) - / (2200 - 2000) * (.data$year - 2000) - ) - * (.data$year >= 2000)) - - # calc projection ---- - projected_data <- inner_join( - regression_parameters_converging, - GDPpC, - c('region', 'year') - ) %>% - mutate(manufacturing = .data$a * exp(.data$b / .data$GDPpC) - * .data$population) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'manufacturing') - - projected_data_regions <- projected_data %>% - pivot_longer(c('GDP', 'population', 'manufacturing')) %>% - group_by(!!!syms(c('scenario', 'region', 'year', 'name'))) %>% - summarise(value = sum(.data$value), .groups = 'drop') %>% - sum_total_('region', name = 'World') %>% - pivot_wider() %>% - mutate(mfg.share = .data$manufacturing / .data$GDP, - GDPpC = .data$GDP / .data$population) - - # calc VA of steel production ---- - data_steel_production <- readSource('worldsteel', 'long', convert = FALSE) %>% - madrat_mule() - - regression_data_steel <- inner_join( - INDSTAT %>% - filter('steel' == .data$subsector) %>% - select('region', 'iso3c', 'year', steel.VA = 'value'), - - data_steel_production %>% - filter(0 != .data$value) %>% - rename(steel.production = 'value'), - - c('iso3c', 'year') - ) - - ## compute regional and World aggregates ---- - regression_data_steel <- regression_data_steel %>% - inner_join( - population %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - inner_join( - GDP %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - pivot_longer(c('population', 'steel.production', 'GDP', 'steel.VA'), - names_to = 'variable', - names_transform = list(variable = factor)) %>% - duplicate(region = 'World') - - regression_data_steel <- bind_rows( - regression_data_steel, - - regression_data_steel %>% - group_by(.data$region, .data$year, .data$variable) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - iso3c = 'Total', - .groups = 'drop') - ) %>% - group_by(!!!syms(c('region', 'iso3c', 'year', 'variable'))) %>% - pivot_wider(names_from = 'variable') - - ## compute regression parameters ---- - regression_parameters_steel <- tibble() - for (r in sort(unique(regression_data_steel$region))) { - regression_parameters_steel <- bind_rows( - regression_parameters_steel, - - nls(formula = steel.VApt ~ a * exp(b / GDPpC), - data = regression_data_steel %>% - filter(r == .data$region, - 'Total' == .data$iso3c) %>% - mutate(steel.VApt = .data$steel.VA / .data$steel.production, - GDPpC = .data$GDP / .data$population), - start = list(a = 500, b = 500), - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(region = r) - ) - } - - ### substitute World for AFR parameters ---- - if ('AFR' %in% region_mapping$region) { - replacement_region <- 'AFR' - } else if ('SSA' %in% region_mapping$region) { - replacement_region <- 'SSA' - } else { - replacement_region <- NA - } - - if (!is.na(replacement_region)) { - regression_parameters_steel <- bind_rows( - regression_parameters_steel %>% - filter(replacement_region != .data$region), - - regression_parameters_steel %>% - filter('World' == .data$region) %>% - mutate(region = replacement_region) - ) - rm(replacement_region) - } - - ## project steel VA per tonne of steel ---- - parameter_a_world_steel <- regression_parameters_steel %>% - filter('World' == .data$region) %>% - pull('a') - - regression_parameters_steel_converging <- regression_parameters_steel %>% - filter('World' != .data$region) %>% - mutate(year = as.integer(2000)) %>% - complete(nesting(!!!syms(c('region', 'a', 'b'))), year = 2000:2100) %>% - mutate(a = .data$a - + ( (parameter_a_world_steel - .data$a) - / (2200 - 2000) - * (.data$year - 2000) - )) - - projected_steel_data <- inner_join( - inner_join( - regression_parameters_steel_converging, - - GDPpC, - - c('region', 'year') - ) %>% - mutate(steel.VApt = .data$a * exp(.data$b / .data$GDPpC)) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'GDPpC', 'steel.VApt'), - - calcOutput(type = 'Steel_Projections', - match.steel.historic.values = match.steel.historic.values, - match.steel.estimates = match.steel.estimates, - China_Production = China_Production, - aggregate = FALSE, supplementary = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = 'Data1', iso3c = 'Region', variable = 'Data2', - year = 'Year', value = 'Value') %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year), - # Gt/year * 1e9 t/Gt = t/year - value = .data$value * 1e9, - variable = sub('^ue_steel_(primary|secondary)$', - '\\1.production', .data$variable), - scenario = sub('^gdp_', '', .data$scenario)) %>% - filter(between(.data$year, 2000, 2100)) %>% - full_join(region_mapping, 'iso3c') %>% - assert(not_na, everything()) %>% - group_by(!!!syms(c('scenario', 'region', 'iso3c', 'year'))) %>% - summarise(steel.production = sum(.data$value), .groups = 'drop'), - - c('scenario', 'region', 'iso3c', 'year') - ) %>% - mutate(steel.VA = .data$steel.VApt * .data$steel.production) %>% - select(-'steel.VApt', -'GDPpC') %>% - pivot_longer(c('population', 'GDP', 'steel.production', 'steel.VA')) %>% - duplicate(region = 'World') %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - mutate(GDPpC = .data$GDP / .data$population, - steel.VApt = .data$steel.VA / .data$steel.production) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'steel.production', 'steel.VA', 'GDPpC', 'steel.VApt') - - ## plot steel VA ============================================================= - if (!is.null(save.plots)) { - d_plot_region_totals <- regression_data_steel %>% - ungroup() %>% - filter('Total' == .data$iso3c) %>% - mutate(GDPpC = .data$GDP / .data$population, - steel.VA.pt = .data$steel.VA / .data$steel.production) %>% - # filter outliers - filter(2000 >= .data$steel.VA.pt) %>% - select('region', 'year', 'GDPpC', 'steel.VA.pt') - - d_plot_region_totals %>% - filter('SSA' == .data$region) %>% - select('region', 'steel.VA.pt') %>% - mutate(cuts = cut(x = .data$steel.VA.pt, - breaks = seq_range(range(.data$steel.VA.pt), - length.out = 31), - labels = 1:30, include.lowest = TRUE)) %>% - group_by(!!!syms(c('region', 'cuts'))) %>% - summarise(count = n(), .groups = 'drop_last') %>% - mutate(cuts = as.integer(.data$cuts)) %>% - ungroup() %>% - complete(nesting(!!sym('region')), cuts = 1:30) %>% - mutate(foo = cumsum(is.na(.data$count))) %>% - filter(cumsum(is.na(.data$count)) > 30 / 2) %>% - head(n = 1) %>% - select(-'count', -'foo') - - d_plot_regression <- full_join( - regression_parameters_steel, - - d_plot_region_totals %>% - select('region', 'GDPpC') %>% - df_populate_range('GDPpC'), - - 'region' - ) %>% - mutate(steel.VA.pt = .data$a * exp(.data$b / .data$GDPpC)) %>% - select('region', 'GDPpC', 'steel.VA.pt') - - d_plot_projections <- projected_steel_data %>% - filter(.data$scenario %in% names(linetype_scenarios), - 'Total' == .data$iso3c) %>% - select('scenario', 'region', 'year', 'GDPpC', steel.VA.pt = 'steel.VApt') - - d_plot_projections <- left_join( - d_plot_projections, - - d_plot_projections %>% - select('region', 'year', 'GDPpC') %>% - filter(max(.data$year) == .data$year) %>% - group_by(.data$region) %>% - filter(min(.data$GDPpC) == .data$GDPpC) %>% - select('region', max.GDPpC = 'GDPpC'), - - 'region' - ) %>% - filter(.data$GDPpC <= .data$max.GDPpC) %>% - select(-'max.GDPpC') - - p <- ggplot(mapping = aes(x = !!sym('GDPpC') / 1000, - y = !!sym('steel.VA.pt'))) + - geom_point(data = d_plot_region_totals, - mapping = aes(shape = 'region totals')) + - scale_shape_manual(values = c('region totals' = 'cross'), - name = NULL) + - geom_line(data = d_plot_projections %>% - filter(2050 >= .data$year), - mapping = aes(colour = !!sym('scenario'))) + - geom_line(data = d_plot_regression, - mapping = aes(colour = 'regression')) + - scale_colour_manual(values = c('regression' = 'red', - 'SSP2' = 'black'), - name = NULL, - guide = guide_legend(direction = 'horizontal')) + - facet_wrap(vars(!!sym('region')), scales = 'free') + - expand_limits(x = 0, y = 0) + - labs(x = 'per-capita GDP [1000$/yr]', - y = 'specific Steel Value Added [$/t]') + - theme_minimal() + - theme(legend.justification = c(1, 0), - legend.position = c(1, 0)) - - ggsave(plot = p, filename = '04_Steel_VA_regressions_projections.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '04_Steel_VA_regressions_projections.rds')) - } - - # ========================================================================== = - - # project cement production ---- - ## calculate regression data ---- - regression_data_cement <- full_join( - INDSTAT %>% - filter('cement' == .data$subsector) %>% - select('region', 'iso3c', 'year', cement.VA = 'value') %>% - filter(.data$year >= min(data_cement_production$year)), - - data_cement_production %>% - rename(cement.production = 'value') %>% - left_join(region_mapping, 'iso3c'), - - c('region', 'iso3c', 'year') - ) %>% - filter(!is.na(.data$cement.production)) - - ### censor nonsensical data ---- - cement_censor <- list_to_data_frame(list( - BDI = 1980:2010, # zero cement production - CIV = 1990:1993, # cement VA 100 times higher than before and after - NAM = 2007:2010, # zero cement production - HKG = 1973:1979, # no data for CHN prior to 1980 - IRQ = 1992:1997, # cement VA 100 times higher than before and after - RUS = 1970:1990, # exclude data from Soviet period which biases - # projections up - NULL), - 'iso3c', 'year') %>% - mutate(censored = TRUE) - - regression_data_cement <- regression_data_cement %>% - anti_join(cement_censor, c('iso3c', 'year')) - - ### compute regional and World aggregates ---- - regression_data_cement <- regression_data_cement %>% - inner_join( - population %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - inner_join( - GDP %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - pivot_longer(c('population', 'cement.production', 'GDP', 'cement.VA')) %>% - duplicate(region = 'World') - - regression_data_cement <- bind_rows( - regression_data_cement, - - regression_data_cement %>% - filter(# exclude CHA from global regression data, because it dominates - # the global regression - !('World' == .data$region & 'CHN' == .data$iso3c)) %>% - group_by(!!!syms(c('region', 'year', 'name'))) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - iso3c = 'Total', - .groups = 'drop') - ) %>% - pivot_wider() - - ## compute regression parameters ---- - regression_parameters_cement_production <- tibble() - for (r in sort(unique(regression_data_cement$region))) { - regression_parameters_cement_production <- bind_rows( - regression_parameters_cement_production, - - nls(formula = cement.PpC ~ a * exp(b / GDPpC), - data = regression_data_cement %>% - filter(r == .data$region, - 'Total' == .data$iso3c) %>% - mutate(cement.PpC = .data$cement.production / .data$population, - GDPpC = .data$GDP / .data$population), - start = list(a = 1, b = -1000), - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(region = r) - ) - } - - ## project cement production per capita ---- - param_a <- regression_parameters_cement_production %>% - filter('World' == .data$region) %>% - pull('a') - - regression_parameters_cement_production_converging <- - regression_parameters_cement_production %>% - filter('World' != .data$region) %>% - mutate(year = 2000L) %>% - complete(nesting(!!!syms(c('region', 'a', 'b'))), - year = 2000:2100) %>% - left_join( - readSource(type = 'ExpertGuess', - subtype = 'cement_production_convergence_parameters', - convert = FALSE) %>% - madrat_mule(), - - 'region' - ) %>% - mutate(a = .data$a - + ( (param_a * .data$convergence.level - .data$a) - / (.data$convergence.year - 2000) - * (.data$year - 2000) - )) %>% - select(-'convergence.level', -'convergence.year') - - projected_cement_data <- inner_join( - regression_parameters_cement_production_converging, - - GDPpC, - - c('region', 'year') - ) %>% - mutate(cement.production = .data$a * exp(.data$b / .data$GDPpC) - * .data$population) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'cement.production') %>% - pivot_longer(c('population', 'GDP', 'cement.production')) %>% - duplicate(region = 'World') %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - mutate(GDPpC = .data$GDP / .data$population, - cement.PpC = .data$cement.production / .data$population) - - last_cement_year <- max(data_cement_production$year) - - projected_cement_data <- left_join( - projected_cement_data %>% - select(-'GDPpC', -'cement.PpC') %>% - filter('Total' != .data$iso3c), - - data_cement_production %>% - rename(data = 'value'), - - c('iso3c', 'year') - ) %>% - group_by(!!!syms(c('scenario', 'region', 'iso3c'))) %>% - mutate( - shift.factor = .data$data / .data$cement.production, - shift.factor = ifelse( - between(.data$year, last_cement_year, last_cement_year + 14), - 1 + ( ( last(na.omit(.data$shift.factor)) - 1) - * ((last_cement_year + 14 - .data$year) / 14) ^ 2 - ), - ifelse(last_cement_year > .data$year, .data$shift.factor, 1)), - shift.factor = na.approx(object = .data$shift.factor, x = .data$year, - yleft = first(na.omit(.data$shift.factor)), - yright = last(na.omit(.data$shift.factor)), - na.rm = FALSE), - cement.production = .data$shift.factor * .data$cement.production) %>% - ungroup() %>% - select(-'data', -'shift.factor') %>% - pivot_longer(c('cement.production', 'GDP', 'population')) %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - mutate(GDPpC = .data$GDP / .data$population, - cement.PpC = .data$cement.production / .data$population) - - # project cement VA ---- - ## compute regression parameters ---- - regression_parameters_cement <- tibble() - for (r in sort(unique(regression_data_cement$region))) { - regression_parameters_cement <- bind_rows( - regression_parameters_cement, - - nls(formula = cement.VApt ~ a * exp(b / GDPpC), - data = regression_data_cement %>% - filter(r == .data$region, - 'Total' != .data$iso3c, - !is.na(.data$cement.VA)) %>% - pivot_longer(all_of(c('population', 'cement.production', 'GDP', - 'cement.VA'))) %>% - group_by(.data$region, .data$year, .data$name) %>% - summarise(value = sum(.data$value), - iso3c = 'Total', - .groups = 'drop') %>% - pivot_wider() %>% - mutate(cement.VApt = .data$cement.VA / .data$cement.production, - GDPpC = .data$GDP / .data$population), - start = list(a = 250, b = 1500), - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(region = r) - ) - } - - # project cement VA per tonne of cement ---- - parameter_a_world_cement <- regression_parameters_cement %>% - filter('World' == .data$region) %>% - pull('a') - - regression_parameters_cement_converging <- regression_parameters_cement %>% - filter('World' != .data$region) %>% - mutate(year = as.integer(2000)) %>% - complete(nesting(!!!syms(c('region', 'a', 'b'))), year = 2000:2100) %>% - mutate(a = .data$a - + ( (parameter_a_world_cement - .data$a) - / (2200 - 2000) - * (.data$year - 2000) - )) - - projected_cement_data <- inner_join( - regression_parameters_cement_converging, - - projected_cement_data, - - c('region', 'year') - ) %>% - filter('Total' != .data$iso3c) %>% - mutate(cement.VA = .data$a * exp(.data$b / (.data$GDP / .data$population)) - * .data$cement.production) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'cement.production', 'cement.VA') %>% - pivot_longer(c('population', 'GDP', 'cement.production', 'cement.VA')) %>% - sum_total_('iso3c') %>% - pivot_wider() - - ## plot cement regressions ==== - if (!is.null(save.plots)) { - d_plot_region_totals <- regression_data_cement %>% - filter('Total' == .data$iso3c) %>% - mutate(GDPpC = .data$GDP / .data$population) - - d_plot_countries <- regression_data_cement %>% - semi_join( - regression_data_cement %>% - filter('World' != .data$region, - 'Total' != .data$iso3c) %>% - distinct(.data$region, .data$iso3c) %>% - group_by(.data$region) %>% - filter(1 != n()) %>% - ungroup(), - - c('region', 'iso3c') - ) %>% - mutate(GDPpC = .data$GDP / .data$population) - - d_plot_projections <- projected_cement_data %>% - filter(.data$scenario %in% names(linetype_scenarios), - 'Total' == .data$iso3c, - between(.data$year, max(d_plot_region_totals$year), 2100)) %>% - mutate(GDPpC = .data$GDP / .data$population) %>% - inner_join( - regression_data_cement %>% - filter('Total' != .data$iso3c) %>% - group_by(.data$region) %>% - mutate(GDPpC = .data$GDP / .data$population) %>% - filter(max(.data$GDPpC) == .data$GDPpC) %>% - ungroup() %>% - select('region', max.GDPpC = 'GDPpC'), - - 'region' - ) %>% - filter(.data$GDPpC <= 2 * .data$max.GDPpC) - - d_plot_asymptote <- bind_cols( - regression_parameters_cement_production %>% - select('region', 'a') %>% - filter('World' != .data$region), - - regression_parameters_cement_production %>% - filter('World' == .data$region) %>% - select(World = 'a') - ) %>% - mutate(a.conv = (.data$a + .data$World) / 2) %>% - select(-'World') %>% - pivot_longer(c('a', 'a.conv')) %>% - full_join( - tribble( - ~name, ~year, - 'a', max(d_plot_region_totals$year), - 'a.conv', 2100), - - 'name' - ) %>% - select('region', 'year', 'value') - - d_plot_asymptote <- full_join( - bind_cols( - regression_parameters_cement_production %>% - select('region', 'a') %>% - filter('World' != .data$region), - - regression_parameters_cement_production %>% - filter('World' == .data$region) %>% - select(World = 'a') - ), - - d_plot_projections %>% - group_by(.data$scenario, .data$region) %>% - filter(.data$year %in% c(max(d_plot_region_totals$year), - max(.data$year))) %>% - select('scenario', 'region', 'year', 'GDPpC'), - - 'region' - ) %>% - mutate(value = .data$a - + ( (.data$World - .data$a) - / (2200 - 2000) - * (.data$year - 2000))) %>% - select('scenario', 'region', 'GDPpC', 'value') - - d_plot_regression <- full_join( - regression_parameters_cement_production, - - d_plot_region_totals %>% - select('region', 'GDPpC') %>% - df_populate_range('GDPpC'), - - 'region' - ) %>% - mutate(value = .data$a * exp(.data$b / .data$GDPpC)) - - d_plot_foo <- full_join( - regression_parameters_cement_production, - - d_plot_projections %>% - group_by(.data$scenario, .data$region) %>% - filter(.data$year %in% c(max(d_plot_region_totals$year), - max(.data$year))) %>% - select('scenario', 'region', 'year', 'GDPpC'), - - 'region' - ) %>% - mutate(value = .data$a * exp(.data$b / .data$GDPpC)) - - y_max <- d_plot_region_totals %>% - filter('World' == .data$region) %>% - mutate( - cement.production.pC = .data$cement.production / .data$population) %>% - filter(max(.data$cement.production.pC) == .data$cement.production.pC) %>% - pull('cement.production.pC') - - projection_points <- c(2015, 2030, 2050, 2075, 2100) - - p <- ggplot(mapping = aes(x = !!sym('GDPpC') / 1000, - y = !!sym('cement.production') - / !!sym('population'))) + - # plot region totals - geom_point( - data = d_plot_region_totals, - mapping = aes(shape = 'region totals'), - size = 2) + - # plot regression line - geom_path( - data = d_plot_regression, - mapping = aes(y = !!sym('value'), colour = 'regression')) + - # plot projections - geom_path( - data = d_plot_projections, - mapping = aes(colour = 'projection')) + - geom_point( - data = d_plot_projections %>% - filter(.data$year %in% projection_points), - mapping = aes(shape = as.character(!!sym('year'))), - size = 3) + - scale_shape_manual( - values = c('region totals' = 'o', - setNames(rep('x', length(projection_points)), - projection_points)), - name = NULL) + - scale_colour_manual(values = c('regression' = 'red', - 'projection' = 'black'), - name = NULL) + - facet_wrap(vars(!!sym('region')), scales = 'free') + - expand_limits(y = c(0, ceiling(y_max * 2) / 2)) + - labs(x = 'per-capita GDP [1000 $/year]', - y = 'per-capita Cement Production [tonnes/year]') + - theme_minimal() - - - ggsave(plot = p, filename = '01_Cement_regression_projection.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '01_Cement_regression_projection.rds')) - } - - # ========================================================================== = - - ## plot cement VA regressions ==== - if (!is.null(save.plots)) { - d_plot_region_totals <- regression_data_cement %>% - ungroup() %>% - filter('Total' == .data$iso3c) %>% - mutate(GDPpC = .data$GDP / .data$population, - cement.VA.pt = .data$cement.VA / .data$cement.production) %>% - # filter outliers - # filter(2000 >= .data$cement.VA.pt) %>% - select('region', 'year', 'GDPpC', 'cement.VA.pt') - - d_plot_regression <- full_join( - regression_parameters_cement, - - d_plot_region_totals %>% - select('region', 'GDPpC') %>% - df_populate_range('GDPpC'), - - 'region' - ) %>% - mutate(cement.VA.pt = .data$a * exp(.data$b / .data$GDPpC)) %>% - select('region', 'GDPpC', 'cement.VA.pt') - - d_plot_projections <- projected_cement_data %>% - filter(.data$scenario %in% names(linetype_scenarios), - 'Total' == .data$iso3c) %>% - mutate(GDPpC = .data$GDP / .data$population, - cement.VA.pt = .data$cement.VA / .data$cement.production) %>% - select('scenario', 'region', 'year', 'GDPpC', 'cement.VA.pt') - - d_plot_projections <- left_join( - d_plot_projections, - - d_plot_projections %>% - select('region', 'year', 'GDPpC') %>% - filter(max(.data$year) == .data$year) %>% - group_by(.data$region) %>% - filter(min(.data$GDPpC) == .data$GDPpC) %>% - select('region', max.GDPpC = 'GDPpC'), - - 'region' - ) %>% - filter(.data$GDPpC <= .data$max.GDPpC) %>% - select(-'max.GDPpC') - - p <- ggplot(mapping = aes(x = !!sym('GDPpC') / 1000, - y = !!sym('cement.VA.pt'))) + - geom_point(data = d_plot_region_totals, - mapping = aes(shape = 'region totals')) + - scale_shape_manual(values = c('region totals' = 'cross'), - name = NULL) + - geom_line(data = d_plot_regression, - mapping = aes(linetype = 'regression')) + - geom_line(data = d_plot_projections, - mapping = aes(linetype = !!sym('scenario'))) + - scale_linetype_manual(values = linetype_scenarios, name = NULL, - guide = guide_legend(direction = 'horizontal')) + - facet_wrap(vars(!!sym('region')), scales = 'free') + - expand_limits(x = 0, y = 0) + - labs(x = 'per-capita GDP [1000$/yr]', - y = 'specific Cement Value Added [$/t]') + - theme_minimal() + - theme(legend.justification = c(1, 0), - legend.position = c(1, 0)) - - - ggsave(plot = p, filename = '05a_Cement_VA_regressions_projections.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '05_Cement_VA_regressions_projections.rds')) - } - - # ========================================================================== = - - # project chemicals VA ---- - ## compile regression data ---- - regression_data_chemicals <- INDSTAT %>% - filter('chemicals' == .data$subsector) %>% - select('region', 'iso3c', 'year', chemicals.VA = 'value') %>% - inner_join( - population %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - inner_join( - GDP %>% - filter('SSP2' == .data$scenario) %>% # TODO: define default scenario - select(-'scenario'), - - c('iso3c', 'year') - ) %>% - duplicate(region = 'World') - - ### compute regional and World aggregates ---- - regression_data_chemicals <- bind_rows( - regression_data_chemicals, - - regression_data_chemicals %>% - pivot_longer(c('population', 'GDP', 'chemicals.VA')) %>% - group_by(!!!syms(c('region', 'year', 'name'))) %>% - summarise(value = sum(.data$value), - iso3c = 'Total', - .groups = 'drop') %>% - pivot_wider() - ) %>% - mutate(GDPpC = .data$GDP / .data$population) - - ## compute regression parameters ---- - regression_parameters_chemicals <- tibble() - for (r in sort(unique(regression_data_chemicals$region))) { - regression_parameters_chemicals <- bind_rows( - regression_parameters_chemicals, - - nls(formula = chemicals.VA / population ~ a * exp(b / GDPpC), - data = regression_data_chemicals %>% - filter(r == .data$region, - 'Total' == .data$iso3c), - start = list(a = 1000, b = -100), - trace = FALSE) %>% - broom::tidy() %>% - select('term', 'estimate') %>% - pivot_wider(names_from = 'term', values_from = 'estimate') %>% - mutate(region = r) - ) - } - - ## replace outliers with global parameters - outliers_chemicals <- regression_parameters_chemicals %>% - select('region', 'a') %>% - filter(abs((.data$a - mean(.data$a)) / sd(.data$a)) > 3) %>% - pull('region') - - regression_parameters_chemicals <- bind_rows( - regression_parameters_chemicals %>% - filter(!.data$region %in% outliers_chemicals), - - tibble( - regression_parameters_chemicals %>% - filter('World' == .data$region) %>% - select(-'region'), - - region = outliers_chemicals) - ) - - ## project chemicals VA and share ---- - param_a <- regression_parameters_chemicals %>% - filter('World' == .data$region) %>% - pull('a') - - regression_parameters_chemicals_converging <- - regression_parameters_chemicals %>% - filter('World' != .data$region) %>% - mutate(year = as.integer(2000)) %>% - complete(nesting(!!!syms(c('region', 'a', 'b'))), year = 2000:2100) %>% - mutate(a = .data$a - + ( (param_a - .data$a) - / (2200 - 2000) - * (.data$year - 2000) - )) - - projected_chemicals_data <- inner_join( - regression_parameters_chemicals_converging, - - projected_data, - - c('region', 'year') - ) %>% - mutate( - chemicals.VA = .data$a * exp(.data$b / (.data$GDP / .data$population)) - * .data$population) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - 'manufacturing', 'chemicals.VA') %>% - pivot_longer(c('population', 'GDP', 'manufacturing', 'chemicals.VA')) %>% - duplicate(region = 'World') %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - mutate(GDPpC = .data$GDP / .data$population, - chemicals.share = .data$chemicals.VA / .data$manufacturing) - - ## plot chemicals regressions ================================================ - if (!is.null(save.plots)) { - d_plot_region_totals <- regression_data_chemicals %>% - filter('Total' == .data$iso3c) - - d_plot_countries <- regression_data_chemicals %>% - semi_join( - regression_data_chemicals %>% - filter('World' != .data$region, - 'Total' != .data$iso3c) %>% - distinct(.data$region, .data$iso3c) %>% - group_by(.data$region) %>% - filter(1 != n()) %>% - ungroup(), - - c('region', 'iso3c') - ) %>% - mutate(GDPpC = .data$GDP / .data$population) - - d_plot_regression <- full_join( - regression_parameters_chemicals, - - d_plot_region_totals %>% - select('region', 'GDPpC') %>% - df_populate_range('GDPpC'), - - 'region' - ) %>% - mutate(value = .data$a * exp(.data$b / .data$GDPpC)) - - d_plot_projections <- projected_chemicals_data %>% - filter(.data$scenario %in% names(linetype_scenarios), - 'Total' == .data$iso3c, - between(.data$year, max(d_plot_region_totals$year), 2100)) %>% - select('scenario', 'region', 'year', 'GDPpC', 'chemicals.VA', - 'population') %>% - group_by(.data$region) %>% - filter(.data$GDPpC <= .data$GDPpC[ 'SSP2' == .data$scenario - & 2100 == .data$year]) %>% - ungroup() - - p <- ggplot( - mapping = aes(x = !!sym('GDPpC') / 1000, - y = !!sym('chemicals.VA') / !!sym('population'))) + - # plot region totals - geom_point( - data = d_plot_region_totals, - mapping = aes(shape = 'region totals')) + - # # plot regression line - geom_path( - data = d_plot_regression, - mapping = aes(y = !!sym('value'), colour = 'regression')) + - # # plot projections - geom_path( - data = d_plot_projections, - mapping = aes(colour = 'projection')) + - geom_point( - data = d_plot_projections %>% - filter(.data$year %in% projection_points), - mapping = aes(shape = as.character(!!sym('year'))), - size = 3) + - scale_shape_manual( - values = c('region totals' = 'o', - setNames(rep('x', length(projection_points)), - projection_points)), - name = NULL) + - scale_colour_manual(values = c('regression' = 'red', - 'projection' = 'black'), - name = NULL) + - facet_wrap(vars(!!sym('region')), scales = 'free') + - expand_limits(y = c(0, ceiling(y_max * 2) / 2)) + - labs(x = 'per-capita GDP [1000 $/year]', - y = 'per-capita Chemicals Value Added [$/year]') + - theme_minimal() - - ggsave(plot = p, filename = '02_Chemicals_regression_projection.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '02_Chemicals_regression_projection.rds')) - } - - # ======================================================================== === - - if (!is.null(save.plots)) { - d_plot_region_totals <- regression_data %>% - filter('Total' == .data$iso3c) - - d_plot_countries <- regression_data %>% - semi_join( - regression_data %>% - filter('World' != .data$region, - 'Total' != .data$iso3c) %>% - distinct(.data$region, .data$iso3c) %>% - group_by(.data$region) %>% - filter(1 != n()) %>% - ungroup(), - - c('region', 'iso3c') - ) %>% - mutate(GDPpC = .data$GDP / .data$population) - - d_plot_regression <- full_join( - regression_parameters, - - regression_data %>% - select('region', 'GDPpC') %>% - df_populate_range('GDPpC'), - - 'region' - ) %>% - mutate(value = .data$a * exp(.data$b / .data$GDPpC)) - - d_plot_projections <- projected_data %>% - pivot_longer(c('population', 'GDP', 'manufacturing')) %>% - sum_total_('iso3c') %>% - pivot_wider() %>% - filter(.data$scenario %in% names(linetype_scenarios), - 'Total' == .data$iso3c, - between(.data$year, max(d_plot_region_totals$year), 2100)) %>% - mutate(GDPpC = .data$GDP / .data$population) %>% - select('scenario', 'region', 'year', 'GDPpC', 'manufacturing', - 'population') %>% - group_by(.data$region) %>% - filter(.data$GDPpC <= .data$GDPpC[ 'SSP2' == .data$scenario - & 2100 == .data$year]) %>% - ungroup() - - p <- ggplot( - mapping = aes(x = !!sym('GDPpC') / 1000, - y = !!sym('manufacturing') / !!sym('population') / 1000)) + - # plot region totals - geom_point( - data = d_plot_region_totals, - mapping = aes(shape = 'region totals')) + - # # plot regression line - geom_path( - data = d_plot_regression, - mapping = aes(y = !!sym('value') / 1000, colour = 'regression')) + - # # plot projections - geom_path( - data = d_plot_projections, - mapping = aes(colour = 'projection')) + - geom_point( - data = d_plot_projections %>% - filter(.data$year %in% projection_points), - mapping = aes(shape = as.character(!!sym('year'))), - size = 3) + - scale_shape_manual( - values = c('region totals' = 'o', - setNames(rep('x', length(projection_points)), - projection_points)), - name = NULL) + - scale_colour_manual(values = c('regression' = 'red', - 'projection' = 'black'), - name = NULL) + - facet_wrap(vars(!!sym('region')), scales = 'free') + - expand_limits(y = c(0, ceiling(y_max * 2) / 2)) + - labs(x = 'per-capita GDP [1000 $/year]', - y = 'per-capita Industry Value Added [1000 $/year]') + - theme_minimal() - - ggsave(plot = p, filename = '03_Industry_regression_projection.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '03_Industry_regression_projection.rds')) - } - - # calculate other Industries Value Added projections ---- - projections <- bind_rows( - projected_data %>% - filter('Total' != .data$iso3c, 'World' != .data$region) %>% - select('scenario', 'region', 'iso3c', 'year', 'population', 'GDP', - manufacturing.VA = 'manufacturing') %>% - pivot_longer(c('population', 'GDP', 'manufacturing.VA')), - - projected_cement_data %>% - filter('Total' != .data$iso3c, 'World' != .data$region) %>% - select('scenario', 'region', 'iso3c', 'year', 'cement.production', - 'cement.VA') %>% - pivot_longer(c('cement.production', 'cement.VA')), - - projected_chemicals_data %>% - filter('Total' != .data$iso3c, 'World' != .data$region) %>% - select('scenario', 'region', 'iso3c', 'year', 'chemicals.VA') %>% - pivot_longer('chemicals.VA'), - - projected_steel_data %>% - filter('Total' != .data$iso3c, 'World' != .data$region) %>% - select('scenario', 'region', 'iso3c', 'year', 'steel.production', - 'steel.VA') %>% - pivot_longer(c('steel.production', 'steel.VA')) - ) %>% - pivot_wider() %>% - mutate(otherInd.VA = .data$manufacturing.VA - - .data$cement.VA - - .data$chemicals.VA - - .data$steel.VA) - - ## filter projections with negative VA for otherInd ---- - tmp_negative_otherInd <- projections %>% - select('scenario', 'region', 'iso3c', 'year', 'otherInd.VA') %>% - filter(0 > .data$otherInd.VA) %>% - select(-'otherInd.VA') - - # scenario/region/year combinations with negative values for all countries - tmp_all_negative_otherInd <- projections %>% - select('scenario', 'region', 'iso3c', 'year', 'otherInd.VA') %>% - group_by(!!!syms(c('scenario', 'region', 'year'))) %>% - filter(sum(0 > .data$otherInd.VA) == n()) %>% - ungroup() %>% - select('scenario', 'region', 'year') - - ## calculate regional shares ---- - # disregarding negative VA for otherInd - projections_regional_shares <- projections %>% - select('scenario', 'region', 'iso3c', 'year', matches('VA$')) %>% - anti_join( - tmp_negative_otherInd %>% - select(-'region'), - - c('scenario', 'iso3c', 'year') - ) %>% - pivot_longer(matches('VA$')) %>% - assert(within_bounds(0, Inf), .data$value) %>% - group_by(!!!syms(c('scenario', 'region', 'year', 'name'))) %>% - summarise(value = sum(.data$value), .groups = 'drop') %>% - pivot_wider() %>% - pivot_longer(matches('^(cement|chemicals|steel|otherInd)\\.VA$'), - names_to = 'variable') %>% - mutate(share = .data$value / .data$manufacturing.VA) %>% - select('scenario', 'region', 'year', 'variable', 'share') %>% - # fill temporal gaps arising when for some combination of - # scenario/region/year the otherInd.VA for all countries are negative - interpolate_missing_periods_( - periods = list('year' = unique(projections$year)), value = 'share', - expand.values = TRUE) - - ## replace VA projections with negative otherInd via regional shares ---- - projections <- bind_rows( - projections %>% - select('scenario', 'region', 'iso3c', 'year', 'manufacturing.VA') %>% - semi_join( - tmp_negative_otherInd, - - c('scenario', 'region', 'iso3c', 'year') - ) %>% - inner_join( - projections_regional_shares, - - c('scenario', 'region', 'year') - ) %>% - mutate(value = .data$manufacturing.VA * .data$share) %>% - select(-'manufacturing.VA', -'share'), - - projections %>% - pivot_longer(c('population', 'GDP', 'manufacturing.VA', - 'cement.production', 'cement.VA', 'chemicals.VA', - 'steel.production', 'steel.VA', 'otherInd.VA'), - names_to = 'variable') %>% - anti_join( - tmp_negative_otherInd %>% - mutate(variable = '') %>% - complete(nesting(!!!syms(c('scenario', 'region', 'iso3c', 'year'))), - variable = c('cement.VA', 'chemicals.VA', 'steel.VA', - 'otherInd.VA')), - - c('scenario', 'region', 'iso3c', 'year', 'variable') - ) - ) %>% - duplicate(region = 'World') %>% - sum_total_('iso3c') %>% - filter(!('World' == .data$region & 'Total' != .data$iso3c)) %>% - pivot_wider(names_from = 'variable') - - # plot otherInd VA =========================================================== - - if (!is.null(save.plots)) { - p <- ggplot() + - geom_area( - data = projections %>% - select('scenario', 'region', 'iso3c', 'year', 'cement.VA', - 'chemicals.VA', 'steel.VA', 'otherInd.VA') %>% - filter('SSP2' == .data$scenario, - 2000 <= .data$year, - 'Total' == .data$iso3c) %>% - select(-'scenario', -'iso3c') %>% - pivot_longer(matches('\\.VA$')) %>% - mutate(name = sub('\\.VA$', '', .data$name)) %>% - order.levels( - name = c('cement', 'chemicals', 'steel', 'otherInd')), - mapping = aes(x = !!sym('year'), y = !!sym('value') / 1e12, - fill = !!sym('name'))) + - scale_fill_discrete(name = NULL, - guide = guide_legend(direction = 'horizontal')) + - facet_wrap(vars(!!sym('region')), scales = 'free_y') + - labs(x = NULL, y = 'Value Added [$tn/year]') + - theme_minimal() + - theme(legend.justification = c(1, 0), legend.position = c(1, 0)) - - ggsave(plot = p, filename = '05b_Value_Added_projection.png', - device = 'png', path = save.plots, bg = 'white', - width = 18, height = 14, units = 'cm', scale = 1.73) - - write_rds(x = p, - file = file.path(save.plots, - '05_Value_Added_projection.rds')) - } - - # ========================================================================== = - - # construct output ---- - if ('physical' == subtype) { - x <- projections %>% - filter(1993 <= .data$year, - 'Total' != .data$iso3c, - 'World' != .data$region) %>% - select('scenario', 'iso3c', 'year', ue_cement = 'cement.production', - ue_chemicals = 'chemicals.VA', ue_otherInd = 'otherInd.VA') %>% - pivot_longer(matches('^ue_'), names_to = 'pf') %>% - verify(!(is.na(.data$value) & between(.data$year, 2000, 2100))) %>% - # t/year * 1e-9 Gt/t = Gt/year | cement - # $/year * 1e-12 $tn/$ = $tn/year | chemicals and other industry - mutate( - value = .data$value * case_when( - 'ue_cement' == pf ~ 1e-9, - 'ue_chemicals' == pf ~ 1e-12, - 'ue_otherInd' == pf ~ 1e-12), - scenario = paste0('gdp_', .data$scenario)) %>% - interpolate_missing_periods_(periods = list(year = 1993:2150), - expand.values = TRUE) %>% - select('scenario', 'iso3c', 'pf', 'year', 'value') - } - - if ('economic' == subtype) { - x <- projections %>% - filter(1993 <= .data$year, - 'Total' != .data$iso3c, - 'World' != .data$region) %>% - select('scenario', 'iso3c', 'year', 'cement.VA', 'chemicals.VA', - 'steel.VA', 'otherInd.VA') %>% - pivot_longer(matches('\\.VA$')) %>% - # $/yr * 1e12 $tn/$ = $tn/yr - mutate(value = .data$value * 1e-12, - scenario = paste0('gdp_', .data$scenario)) %>% - select('scenario', 'iso3c', 'name', 'year', 'value') - } - - # return statement ---- - return(list(x = x %>% - as.magpie(spatial = 2, temporal = 4, data = 5), - weight = NULL, - unit = 'trillion US$2017/year', - description = 'chemicals and other industry value added')) - -} diff --git a/R/ODYM_RECC.R b/R/ODYM_RECC.R deleted file mode 100644 index 50ab1a9e..00000000 --- a/R/ODYM_RECC.R +++ /dev/null @@ -1,108 +0,0 @@ -#' Read ODYM_RECC data from the SHAPE Project -#' -#' @md -#' @param subtype One of -#' - `'REMIND_industry_trends'`: Trends in per-capita production of industry -#' subsectors `cement`, `chemicals`, `steel_primary`, `steel_secondary`, -#' and `otherInd`. Trends for `chemicals` and `otherInd` are averages of -#' the other three trends, which are provided by NTNU. -#' @param smooth Smooth REMIND_industry_trends (default) or not. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr left_join mutate n group_by select ungroup -#' @importFrom quitte madrat_mule -#' @importFrom readr read_csv -#' @importFrom rlang syms -#' @importFrom tidyr pivot_longer pivot_wider -#' @importFrom tibble tribble -#' @importFrom zoo rollmean - -#' @export -#' @rdname ODYM_RECC -readODYM_RECC <- function(subtype, smooth = TRUE) { - # file path (for easier debugging) - path <- './' - # path <- '~/PIK/swap/inputdata/sources/ODYM_RECC/' - - # subtype switchboard ---- - switchboard <- list( - 'REMIND_industry_trends' = function() { - read_csv(file = file.path(path, 'REMIND_industry_trends.csv'), - col_names = c('scenario', 'iso3c', 'name', 'year', 'value'), - col_types = '--ccc-id', - skip = 1) %>% - madrat_mule() - }) - - - # check if the subtype called is available ---- - if (!subtype %in% names(switchboard)) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } - - # load data and do whatever ---- - return(switchboard[[subtype]]()) -} - -#' @export -#' @rdname ODYM_RECC -calcODYM_RECC <- function(subtype, smooth = TRUE) { - # subtype switchboard ---- - switchboard <- list( - 'REMIND_industry_trends' = function() { - . <- NULL - - match_subsector <- tribble( - ~name, ~subsector, - 'Production|cement|Primary|per capita', 'cement', - 'Production|iron and steel|Primary|per capita', 'steel_primary', - 'Production|iron and steel|Secondary|per capita', 'steel_secondary') - - x <- readSource(type = 'ODYM_RECC', subtype, convert = FALSE) %>% - madrat_mule() %>% - left_join(match_subsector, 'name') %>% - select(-'name') %>% - group_by(.data$scenario, .data$iso3c, .data$subsector) %>% - mutate(value = .data$value / first(.data$value, - order_by = .data$year)) %>% - ungroup() %>% - pivot_wider(names_from = 'subsector') %>% - group_by(.data$scenario, .data$iso3c, .data$year) %>% - mutate(chemicals = mean(c(!!!syms(match_subsector$subsector))), - otherInd = .data$chemicals) %>% - ungroup() %>% - pivot_longer(-c('scenario', 'iso3c', 'year'), - names_to = 'subsector') - - if (smooth) { - x <- x %>% - group_by(.data$scenario, .data$iso3c, .data$subsector) %>% - mutate(value = c(first(.data$value), - rollmean(.data$value, k = 3, - fill = c('extend', NA, 'extend'), - align = 'center')[-c(1, n())], - last(.data$value))) %>% - ungroup() - } - - return( - list(x = x %>% - as.magpie(spatial = 2, temporal = 3, data = ncol(.)), - weight = NULL, - unit = '', - description = '')) - }) - - # check if the subtype called is available ---- - if (!subtype %in% names(switchboard)) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } - - # load data and do whatever ---- - return(switchboard[[subtype]]()) -} diff --git a/R/UNIDO.R b/R/UNIDO.R deleted file mode 100644 index a7cd0caa..00000000 --- a/R/UNIDO.R +++ /dev/null @@ -1,336 +0,0 @@ -#' UNIDO data -#' -#' Read and convert data from United Nations Industrial Organisation. -#' -#' @md -#' @param subtype one of -#' - `INDSTAT2`: read INDSTAT2 data -#' @param x result from `readUNIDO()` as passed to `convertUNIDO()` -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' `readUNIDO` returns raw INDSTAT2 data. `convertUNIDO` converts to iso3c -#' country codes, selects industry subsectors value added data according to this -#' table -#' -#' | subsector | ISIC | ctable | utable | -#' |---------------|------|--------|--------| -#' | manufacturing | D | 20 | 17–20 | -#' | cement | 20 | 20 | 17–20 | -#' | chemicals | 24 | 20 | 17–20 | -#' | steel | 27 | 20 | 17–20 | -#' -#' and filters data that is either unreasonable or would unduly bias regional -#' regressions according to this table -#' -#' | subsector | iso3c | years | -#' |---------------|-------|------------------| -#' | manufacturing | BIH | 1990–91 | -#' | manufacturing | CHN | 1963–97 | -#' | manufacturing | HKG | 1963–2015 | -#' | manufacturing | IRQ | 1994–98 | -#' | manufacturing | MAC | 1963–2015 | -#' | manufacturing | MDV | 1963–2015 | -#' | cement | BDI | 1980–2010 | -#' | cement | CIV | 1990–93 | -#' | cement | HKG | 1973–79 | -#' | cement | IRQ | 1992–97 | -#' | cement | NAM | 2007–10 | -#' | cement | RUS | 1970–90 | -#' | chemicals | CIV | 1989 | -#' | chemicals | HKG | 1973–79, 2008–15 | -#' | chemicals | MAC | 1978–79 | -#' | chemicals | NER | 1999–2002 | -#' | steel | BGD | 2011 | -#' | steel | CHE | 1995–96 | -#' | steel | CHL | 2008 | -#' | steel | HKG | 1973–79 | -#' | steel | HRV | 2012 | -#' | steel | IRL | 1980 | -#' | steel | LKA | 2006 | -#' | steel | MAR | 1989–2004 | -#' | steel | MKD | 1996 | -#' | steel | PAK | 1981–82 | -#' | steel | TUN | 2003–06 | -#' -#' `calcUNIDO()` calculates `otherInd` subsector values as the difference -#' between `manufacturing` and `cement`, `chemicals`, and `steel` values and is -#' intended to be called through [`calcOutput()`], which will aggregate regions. -#' -#' @author Michaja Pehl -#' -#' @seealso [`readSource()`], [`calcOutput()`] -#' -#' @importFrom assertr assert not_na verify -#' @importFrom dplyr anti_join bind_rows filter group_by inner_join left_join -#' mutate select summarise -#' @importFrom GDPuc convertGDP -#' @importFrom magrittr %>% -#' @importFrom quitte list_to_data_frame madrat_mule -#' @importFrom readr read_csv -#' @importFrom rlang .data is_empty -#' @importFrom tibble tibble tribble -#' @importFrom tidyr unite - -#' @rdname UNIDO -#' @export -readUNIDO <- function(subtype = 'INDSTAT2') -{ - # define read functions for all subtypes ---- - switchboard <- list( - `INDSTAT2` = function() - { - path <- '~/PIK/swap/inputdata/sources/UNIDO/' # used for debugging - path <- './' - - read_csv(file = file.path(path, 'INDSTAT2', - 'INDSTAT2_2017_ISIC_Rev_3.csv'), - col_names = c('ctable', 'country', 'year', 'isic', - 'isiccomb', 'value', 'utable', 'source', - 'lastupdated', 'unit'), - col_types = 'iiiccdiddc', - na = '...') %>% - select('ctable', 'country', 'year', 'isic', 'isiccomb', - 'utable', 'source', 'lastupdated', 'unit', 'value') %>% - filter(!is.na(.data$value)) %>% - madrat_mule() %>% - return() - } - ) - - # check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) -} - -#' @rdname UNIDO -#' @export -convertUNIDO <- function(x, subtype = 'INDSTAT2') -{ - # define convert functions for all subtypes ---- - switchboard <- list( - `INDSTAT2` = function(x) - { - . <- NULL - # add iso3c codes ---- - - # FIXME We are substituting some historic country codes by 'default' - # codes of current countries. Generally, they are situated in the - # same aggregation region, so this has no impact on the derivation - # of regional statistics. This does not apply to former Yugoslavia - # however. Since the countries in question (currently Slovenia and - # Croatia, others might join the EU at a later time and then require - # reclassification) are small compared to the respective regions - # (Europe and Rest-of-World), the impact should be limited. - x <- x %>% - madrat_mule() %>% - ## fix countries up ---- - filter(810 != .data$country) %>% # SUN data synthetic anyhow - left_join( - tribble( - ~country, ~replacement, - 200, 203, # CSE for CSK - 530, 531, # CUW for ANT - 890, 688, # SRB for YUG - 891, 688 # SRB for SCG - ), - - 'country' - ) %>% - mutate(country = ifelse(!is.na(.data$replacement), - .data$replacement, - .data$country)) %>% - select(-'replacement') %>% - ## add country codes ---- - left_join( - bind_rows( - countrycode::codelist %>% - select('iso3c', 'un') %>% - filter(!is.na(.data$iso3c), !is.na(.data$un)), - - # country codes missing from package countrycode - tribble( - ~iso3c, ~un, - 'TWN', 158, # Republic of China - 'ETH', 230, # Ethiopia and Eritrea - 'DEU', 278, # East Germany - 'DEU', 280, # West Germany - 'PAN', 590, # Panama - 'SDN', 736 # Sudan - ) - ), - - c('country' = 'un') - ) %>% - assert(not_na, everything()) - - # aggregate subsectors ---- - ## subsector selection ---- - subsector_selection <- bind_rows( - tibble(subsector = 'manufacturing', - isic = 'D', - ctable = 20, - utable = 17:20), - - tibble(subsector = 'cement', - isic = '26', - ctable = 20, - utable = 17:20), - - tibble(subsector = 'chemicals', - isic = '24', - ctable = 20, - utable = 17:20), - - tibble(subsector = 'steel', - isic = '27', - ctable = 20, - utable = 17:20) - ) - - ## subsector exclusion ---- - subsector_exclusion <- bind_rows( - list_to_data_frame( - list( - # unreasonable data - IRQ = 1994:1998, - MDV = unique(x$year), - BIH = 1990:1991, - # unrepresentative data - HKG = unique(x$year), - MAC = unique(x$year), - CHN = min(x$year):1997), - 'iso3c', 'year') %>% - mutate(subsector = 'manufacturing'), - - # Data with an obvious mismatch between steel production and - # steel value added figures is excluded. - # Data for Hong Kong (1973-1979) is excluded since no data for - # PR China is available for this period and the data would bias - # any regression for the CHA region. - list_to_data_frame( - list(BGD = 2011, - CHE = 1995:1996, - CHL = 2008, - HKG = 1973:1979, - HRV = 2012, - IRL = 1980, - LKA = 2006, - MAR = 1989:2004, - MKD = 1996, - PAK = 1981:1982, - TUN = 2003:2006), - 'iso3c', 'year') %>% - mutate(subsector = 'steel'), - - list_to_data_frame( - list(BDI = 1980:2010, # zero cement production - CIV = 1990:1993, # cement VA 100 times higher than - # before and after - NAM = 2007:2010, # zero cement production - HKG = 1973:1979, # no data for CHN prior to 1980 - IRQ = 1992:1997, # cement VA 100 times higher than - # before and after - RUS = 1970:1990 # exclude data from Soviet period - # which biases projections up - ), - 'iso3c', 'year') %>% - mutate(subsector = 'cement'), - - list_to_data_frame( - list(CIV = 1989, - NER = 1999:2002, - HKG = c(1973:1979, 2008:2015), - MAC = c(1978:1979)), - 'iso3c', 'year') %>% - mutate(subsector = 'chemicals') - ) - - ### aggregation ---- - x <- x %>% - inner_join( - subsector_selection, - - c('isic', 'ctable', 'utable') - ) %>% - anti_join( - subsector_exclusion, - - c('iso3c', 'year', 'subsector') - ) %>% - # GDP conversion is only valid for monetary units - verify('$' == .data$unit) %>% - GDPuc::toolConvertGDP(unit_in = 'constant 2005 US$MER', - unit_out = mrdrivers::toolGetUnitDollar(), - replace_NAs = 'with_USA') %>% - group_by(.data$iso3c, .data$subsector, .data$year) %>% - filter(max(.data$lastupdated) == .data$lastupdated) %>% - # for split countries, which lead to duplicates (e.g. CUW), use - # the maximum - summarise(value = max(.data$value), .groups = 'drop') - - # return ---- - x %>% - as.magpie(spatial = 1, temporal = 3, data = ncol(.)) %>% - toolCountryFill(verbosity = 2) %>% - return() - } - ) - - # check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - - # ---- load data and do whatever ---- - return(switchboard[[subtype]](x)) -} - -#' @rdname UNIDO -#' @export -calcUNIDO <- function(subtype = 'INDSTAT2') -{ - # define calc functions for all subtypes ---- - switchboard <- list( - `INDSTAT2` = function(x) - { - x <- readSource(type = 'UNIDO', subtype = subtype, convert = TRUE) - - x_manufacturing <- dimSums(x[,,'manufacturing'], dim = 3) - x_no_manufacturing <- x[,,'manufacturing', invert = TRUE] - x_otherInd <- setNames( - ( x_manufacturing - - dimSums(x_no_manufacturing, dim = 3) - ), - 'otherInd') - - # $/year * 1e-9 $bn/$ = $bn/year - x <- mbind(x_no_manufacturing, x_otherInd) * 1e-9 - - # give proper variable names - subsector_names <- c('cement', 'chemicals', 'steel', 'otherInd') - variable_names <- paste0('Value Added|Industry|', - c('Cement', 'Chemicals', 'Steel', - 'Other Industry')) - - getNames(x) <- variable_names[match(getNames(x), subsector_names)] - - return(list(x = x, - weight = NULL, - unit = 'billion US$2017/yr', - description = 'industry subsector value added')) - } - ) - - # check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) -} diff --git a/R/calcCapacity.R b/R/calcCapacity.R index 1c990469..c12e05aa 100644 --- a/R/calcCapacity.R +++ b/R/calcCapacity.R @@ -3,6 +3,7 @@ #' #' @param subtype data subtype. Either "capacityByTech" or "capacityByPE" #' @return magpie object of capacity data +#' @importFrom dplyr tribble #' @author Renato Rodrigues, Stephen Bi #' @examples #' diff --git a/R/calcCement.R b/R/calcCement.R deleted file mode 100644 index 64ba1591..00000000 --- a/R/calcCement.R +++ /dev/null @@ -1,54 +0,0 @@ -#' Calculate Historic Cement Production -#' -#' Combines cement production data from [`readvanRuijven2016()`] and -#' [`readUSGS(cement)`][readUSGS] into a single data set, using USGS data from -#' 2005 on. -#' -#' @md -#' @return A list with a [`magpie`][magclass::magclass] object `x` with -#' country-level cement production in tonnes, `weight`, `unit`, `description`, -#' and `min` fields. -#' -#' @author Michaja Pehl -#' -#' @seealso [calcOutput] -#' -#' @importFrom dplyr anti_join arrange bind_rows filter group_by select ungroup -#' @importFrom rlang .data syms !!! - -#' @export -calcCement <- function() { - transition_year <- 2005 - . <- NULL - - d_vanRuijvan2016 <- readSource('vanRuijven2016', convert = FALSE) %>% quitte::madrat_mule() - - d_USGS_cement <- readSource('USGS', 'cement', convert = FALSE) %>% - quitte::madrat_mule() %>% - group_by(!!!syms(c('iso3c', 'year'))) %>% - filter(max(.data$reporting.year) == .data$reporting.year) %>% - ungroup() %>% - select(-'reporting.year') - - d <- d_USGS_cement %>% filter(transition_year <= .data$year) - - d <- bind_rows( - d, - - d_vanRuijvan2016 %>% - anti_join(d, c('iso3c', 'year')) - ) %>% - group_by(!!!syms(c('iso3c', 'year'))) %>% - mutate(count = n()) %>% - assertr::verify(1 == .data$count, description = 'only one data point per country/year') %>% - select(-'count') %>% - arrange(!!!syms(c('iso3c', 'year'))) - - return(list(x = d %>% - as.magpie(spatial = 1, temporal = 2, data = ncol(.)) %>% - toolCountryFill(verbosity = 2), - weight = NULL, - description = 'historical cement production', - unit = 'tonnes of cement', - min = 0)) -} diff --git a/R/calcCementShare.R b/R/calcCementShare.R deleted file mode 100644 index 59eeec00..00000000 --- a/R/calcCementShare.R +++ /dev/null @@ -1,69 +0,0 @@ -#' Calculate Cement Share in NONMET FE Use -#' -#' Estimated shares of cement in `NONMET` final energy use based on OECD and -#' Non-OECD figures from IEA 2017 [Energy Technology Perspectives]( -#' https://www.zotero.org/groups/52011/rd3/items/X8XCUJ5U). Shares are weighted -#' by GDP for aggregation and converge towards global values by 2100. -#' -#' @md -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, `description`, `min`, and `max`. -#' -#' @author Michaja Pehl -#' -#' @seealso [`calcOutput()`] -#' -#' @importFrom dplyr bind_rows left_join select filter mutate pull -#' @importFrom rlang sym -#' @importFrom tibble tribble as_tibble -#' @importFrom tidyr pivot_longer complete nesting - -#' @export -calcCementShare <- function() { - cement_NONMET_share <- tribble( - ~fety, ~OECD, ~`Non-OECD`, ~World, - 'feso_cement', 0.80, 0.68, 0.69, - 'feli_cement', 0.73, 0.62, 0.66, - 'fega_cement', 0.03, 0.75, 0.40, - 'feh2_cement', 0.03, 0.75, 0.40, - 'feel_cement', 0.34, 0.68, 0.59) %>% - pivot_longer(-'fety', names_to = 'region') - - list(x = bind_rows( - left_join( - toolGetMapping('regionmappingOECD.csv', 'regional', where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = 'CountryCode', region = 'RegionCode'), - - cement_NONMET_share %>% - filter('World' != !!sym('region')), - - 'region' - ) %>% - select(-'region') %>% - mutate(year = 2015), - - cement_NONMET_share %>% - filter('World' == !!sym('region')) %>% - select(-'region') %>% - mutate(iso3c = NA_character_) %>% - complete(nesting(!!sym('fety'), !!sym('value')), - iso3c = toolGetMapping('regionmappingOECD.csv', 'regional') %>% - pull('CountryCode')) %>% - filter(!is.na(!!sym('iso3c'))) %>% - mutate(year = 2100) - ) %>% - quitte::interpolate_missing_periods( - year = unique(quitte::remind_timesteps$period), - expand.values = TRUE) %>% - select('iso3c', 'year', 'fety', 'value') %>% - as.magpie(spatial = 1, temporal = 2, data = 4), - - weight = calcOutput('GDP', aggregate = FALSE) %>% - `[`(,unique(quitte::remind_timesteps$period),'gdp_SSP2') %>% - dimSums(dim = 3), - - unit = 'share', - description = 'Share of Cement in NONMET FE use', - min = 0, max = 1) -} diff --git a/R/calcChemicalFeedstocksShare.R b/R/calcChemicalFeedstocksShare.R deleted file mode 100644 index 40c97cd8..00000000 --- a/R/calcChemicalFeedstocksShare.R +++ /dev/null @@ -1,132 +0,0 @@ -#' Calculate Chemical Feedstock share projections -#' -#' Calculates the share of `CHEMICAL` in `CHEMICAL = NECHEM` and converges it -#' towards the maximum value of either OECD or non-OECD countries by 2050. -#' -#' @md -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, `description`, `min`, and `max`. -#' -#' @author Michaja Pehl -#' -#' @seealso [`calcOutput()`] -#' -#' @importFrom assertr assert not_na -#' @importFrom dplyr filter group_by inner_join mutate pull select summarise -#' @importFrom quitte character.data.frame interpolate_missing_periods_ -#' @importFrom rlang .data sym -#' @importFrom tibble as_tibble -#' @importFrom tidyr complete everything nesting - -#' @export -calcChemicalFeedstocksShare <- function() -{ - region_mapping <- toolGetMapping( - name = 'regionmapping_21_EU11.csv', - type = 'regional', where = "mappingfolder") %>% - as_tibble() %>% - select(region = 'RegionCode', iso3c = 'CountryCode') - - OECD_iso3c <- toolGetMapping(name = 'regionmappingOECD.csv', type = 'regional', where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = 'CountryCode', region = 'RegionCode') %>% - filter('OECD' == .data$region) %>% - pull('iso3c') - - # get CHEMICAL and NECHEM data ---- - CHEMICAL_NECHEM <- readSource(type = 'IEA', 'EnergyBalances') %>% - `[`(,,paste('TOTAL', c('CHEMICAL', 'NECHEM'), sep = '.')) %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = 'Region', year = 'Year', flow = 'Data2', value = 'Value') %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year)) %>% - filter(0 != .data$value) %>% - filter(max(.data$year) - 20 <= .data$year) %>% - inner_join(region_mapping, 'iso3c') - - convergence_share <- c( - # max share in OECD countries of last 20 year - CHEMICAL_NECHEM %>% - filter(.data$iso3c %in% OECD_iso3c) %>% - group_by(.data$year) %>% - summarise(share = sum(.data$value['NECHEM' == .data$flow]) - / sum(.data$value), - .groups = 'drop') %>% - pull('share') %>% - max(na.rm = TRUE), - - # max share in non-OECD countries of last 20 years - CHEMICAL_NECHEM %>% - filter(!.data$iso3c %in% OECD_iso3c) %>% - group_by(.data$year) %>% - summarise(share = sum(.data$value['NECHEM' == .data$flow]) - / sum(.data$value), - .groups = 'drop') %>% - pull('share') %>% - max(na.rm = TRUE) - ) %>% - max() %>% - round(2) - - x <- CHEMICAL_NECHEM %>% - group_by(.data$region, .data$year) %>% - summarise(share = sum(.data$value['NECHEM' == .data$flow]) - / sum(.data$value), - .groups = 'drop') %>% - complete(nesting(!!sym('region')), year = c(unique(.data$year), 2050), - fill = list(share = convergence_share)) %>% - interpolate_missing_periods_( - periods = list(year = min(CHEMICAL_NECHEM$year):2150), - value = 'share', expand.values = TRUE) %>% - full_join(region_mapping, 'region') %>% - select('iso3c', 'year', 'share') %>% - assert(not_na, everything()) - - weight <- calcOutput('GDP', aggregate = FALSE) %>% - `[`(,,'gdp_SSP2') %>% - dimSums(dim = 3) - - return( - list( - x = x %>% - filter(.data$year %in% unique(quitte::remind_timesteps$period)) %>% - as.magpie(spatial = 1, temporal = 2, tidy = TRUE) %>% - collapseDim(dim = 3), - - weight = weight[,unique(quitte::remind_timesteps$period),], - - unit = 'share', - description = 'Share of feedstocks in chemicals FE input', - min = 0, max = 1) - ) -} - -# product_mapping <- toolGetMapping( -# name = 'structuremappingIO_outputs_Industry_subsectors.csv', -# type = 'sectoral' -# where = 'mrremind') %>% -# as_tibble() %>% -# select(product = 'iea_product', flow = 'iea_flows', pf = 'REMINDitems_out', -# weight = 'Weight') %>% -# filter('CHEMICAL' == .data$flow) %>% -# right_join( -# tribble( -# ~pf, ~group, -# 'feso_chemicals', 'solids', -# 'feli_chemicals', 'liquids', -# 'fega_chemicals', 'gases'), -# -# 'pf' -# ) %>% -# select('product', 'group', 'weight') - -# # FIXME: emission factors from generisdata_emi.prn; replace with numbers -# # hard-coded into this (or some) package -# emission_factors <- tribble( -# ~group, ~emission.factor, -# 'solids', 26.1, -# 'liquids', 20, -# 'gases', 15.3) %>% -# # GtC/ZJ * 1000 ZJ/EJ = GtC/EJ -# mutate(emission.factor = .data$emission.factor * 1000) diff --git a/R/calcClinker_to_cement_ratio.R b/R/calcClinker_to_cement_ratio.R deleted file mode 100644 index 63ae5e81..00000000 --- a/R/calcClinker_to_cement_ratio.R +++ /dev/null @@ -1,20 +0,0 @@ -#' Calculate Clinker-to-Cement Ratio -#' -#' @md -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, and `description`. -#' -#' @author Michaja Pehl -#' -#' @seealso [`calcOutput()`], [`readADVANCE_WP2()`], [`convertADVANCE_WP2()`] -#' -#' -#' @export -calcClinker_to_cement_ratio <- function() { - list(x = `getYears<-`(readSource('ADVANCE_WP2', 'clinker-to-cement-ratio'), - 2005), - weight = calcOutput('GDPPast', aggregate = FALSE)[,2015,], - unit = 'ratio', - description = 'clinker-to-cement ratio') %>% - return() -} diff --git a/R/calcEmissionFactorsFeedstocks.R b/R/calcEmissionFactorsFeedstocks.R deleted file mode 100644 index 663076b8..00000000 --- a/R/calcEmissionFactorsFeedstocks.R +++ /dev/null @@ -1,206 +0,0 @@ -#' Calculate emission factors for feedstocks in the chemicals industry -#' using emissions from UNFCCC and energy demands from IEA Energy Balances -#' -#' -#' @md -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, `description`. -#' -#' @author Falk Benke, Renato Rodrigues, Simón Moreno Leiva -#' -#' @seealso [`calcOutput()`] -#' -#' @importFrom dplyr filter pull select -#' @importFrom rlang .data -#' @importFrom tibble as_tibble - -#' @export -#' -calcEmissionFactorsFeedstocks <- function() { - - # read source data ---- - - # read in FE data from IEA and convert from ktoe to ZJ - iea <- readSource("IEA", subtype = "EnergyBalances", convert = T)[, , "NECHEM"] %>% collapseDim() * 4.1868e-5 * 1e-3 - iea[iea == 0] <- NA - remove <- magpply(iea, function(y) all(is.na(y)), MARGIN = 1) - iea <- iea[!remove, , ] - - # read in emissions from UNFCCC and convert from kt CO2 to GtC - emi <- readSource("UNFCCC", convert = F)[, , "Table2(I)s1|Total industrial processes|Chemical industry|CO2.kt CO2"] * 1e-6 / 3.666666666667 - remove <- magpply(emi, function(y) all(is.na(y)), MARGIN = 1) - emi <- emi[!remove, , ] - - # filter by common regions and years - regions <- intersect(getItems(emi, dim = 1), getItems(iea, dim = 1)) - years <- intersect(getItems(emi, dim = 2), getItems(iea, dim = 2)) - iea <- iea[regions, years, ] - emi <- emi[regions, years, ] - - # calculate FE demands in chemicals grouped by solids, liquids, gases (ZJ) ---- - fe.demand <- new.magpie( - cells_and_regions = getItems(iea, dim = 1), years = getItems(iea, dim = 2), - names = c("solids", "liquids", "gases") - ) - - product_mapping <- toolGetMapping( - name = "structuremappingIO_outputs_NECHEM.csv", - type = "sectoral", - where = "mrremind") %>% - as_tibble() %>% - right_join( - tribble( - ~remind, ~group, - "pecoal", "solids", - "peoil", "liquids", - "pegas", "gases" - ), - "remind" - ) %>% - select("products", "group") %>% - filter(!!sym("products") %in% getItems(iea, dim = 3.1)) - - for (g in unique(product_mapping$group)) { - products <- product_mapping %>% - filter(g == .data$group) %>% - select("products") %>% - pull() - - fe.demand[, , g] <- dimSums(iea[, , products], dim = 3, na.rm = T) - } - - fe.demand[fe.demand == 0] <- NA - - # calculate carbon used in chemicals (GtC) ---- - - # carbon content, taken from REMIND (GtC/ZJ) - carbon.content <- tribble( - ~group, ~emission.factor, - "solids", 26.1, - "liquids", 20, - "gases", 15.3 - ) - - carbon.use <- fe.demand - - for (g in getNames(fe.demand)) { - carbon.use[, , g] <- carbon.content %>% - filter(g == .data$group) %>% - pull() * fe.demand[, , g] - } - - # total carbon used in chemicals in GtC (GtC/ZJ * ZJ = GtC) - total.carbon <- dimSums(carbon.use, dim = 3, na.rm = T) - - # calculate emission factors (GtC/ZJ) for chemical solids, liquids, gases ---- - # emission factors = carbon used * (chemicals process emissions / total carbon used in chemicals) / energy demand in chemicals - # GtC * (GtC / GtC ) / ZJ = GtC / ZJ - emission.factors <- fe.demand - for (g in getNames(emission.factors)) { - emission.factors[, , g] <- carbon.use[, , g] * (emi / total.carbon) / fe.demand[, , g] - } - - # fill missing countries and years from 2025 - 2150 ---- - - y <- c(seq(2005, 2060, 5), seq(2070, 2100, 10), seq(2110, 2150, 20)) - - # convergence targets assumed for 2050 and missing emission factors based on 2015 USA and Russia chemicals emission factors - # minimum emission factors assumed to be equal to JPN values to remove outliers from data - emifact.ranges <- tribble( - ~group, ~convergence, ~minimum, - "solids", 5.3, 1.14, - "liquids", 4, 0.87, - "gases", 3.2, 0.67 - ) - - # magclass with all countries that have at least some values for 2005 - 2020 - x.fill <- new.magpie( - cells_and_regions = getItems(emission.factors, dim = 1), - years = y, - names = c("solids", "liquids", "gases") - ) - - x.fill[, c(2005, 2010, 2015, 2020), ] <- emission.factors[, c(2005, 2010, 2015, 2020), ] - - # magclass with countries without emission factors - x.empty <- new.magpie( - cells_and_regions = setdiff(getISOlist(), getItems(emission.factors, dim = 1)), - years = y, - names = c("solids", "liquids", "gases") - ) - - for (g in emifact.ranges$group) { - conv <- emifact.ranges %>% - filter(g == .data$group) %>% - select("convergence") %>% - pull() - - min <- emifact.ranges %>% - filter(g == .data$group) %>% - select("minimum") %>% - pull() - - # maximum emission factors in chemicals assumed to be 2 times the convergence values to remove outliers from data - max <- conv * 2 - - # filter outliers - clean <- x.fill[, c(2005, 2010, 2015, 2020), g] - clean[clean < min] <- NA - clean[clean > max] <- NA - - # set convergence value for all-NAs - empty <- magpply(clean, function(y) all(is.na(y)), MARGIN = 1) - clean[empty, , ] <- conv - - # for missing 2005 - 2020 values repeat nearest data - # fill 2005 NAs, try 2010 first, then 2015, then 2020 - clean[, 2005, ][is.na(clean[, 2005, ])] <- clean[, 2010, ][is.na(clean[, 2005, ])] - clean[, 2005, ][is.na(clean[, 2005, ])] <- clean[, 2015, ][is.na(clean[, 2005, ])] - clean[, 2005, ][is.na(clean[, 2005, ])] <- clean[, 2020, ][is.na(clean[, 2005, ])] - - # fill 2010 NAs, try 2015 first, then 2020, then 2005 - clean[, 2010, ][is.na(clean[, 2010, ])] <- clean[, 2015, ][is.na(clean[, 2010, ])] - clean[, 2010, ][is.na(clean[, 2010, ])] <- clean[, 2020, ][is.na(clean[, 2010, ])] - clean[, 2010, ][is.na(clean[, 2010, ])] <- clean[, 2005, ][is.na(clean[, 2010, ])] - - # fill 2015 NAs, try 2020 first, then 2010 - clean[, 2015, ][is.na(clean[, 2015, ])] <- clean[, 2020, ][is.na(clean[, 2015, ])] - clean[, 2015, ][is.na(clean[, 2015, ])] <- clean[, 2010, ][is.na(clean[, 2015, ])] - - # fill 2020 NAs with 2015 - clean[, 2020, ][is.na(clean[, 2020, ])] <- clean[, 2015, ][is.na(clean[, 2020, ])] - - x.fill[, c(2005, 2010, 2015, 2020), g] <- clean - - # set values from 2050 onwards to convergence values: either the fixed value "conv", - # or the 2020 value if lower than "conv" - x.conv <- x.fill[, 2020, g] - x.conv[x.conv > conv] <- conv - x.fill[, c(2050, 2055, 2060, seq(2070, 2100, 10), seq(2110, 2150, 20)), g] <- x.conv - - # interpolate values between 2020 and 2050 - x.fill[, seq(2005, 2050, 5), g] <- toolFillYears(x.fill[, c(2005, 2010, 2015, 2020, 2050), g], seq(2005, 2050, 5)) - - # set emission factors for countries without values to convergence values - x.empty[, , g] <- conv - } - - x <- mbind(x.fill, x.empty) - - # create weights ---- - weights <- new.magpie(cells_and_regions = getItems(x, dim = 1), years = getItems(x, dim = 2)) - iea <- readSource("IEA", subtype = "EnergyBalances", convert = T)[, c(2005, 2010, 2015, 2020), "NECHEM"] %>% - collapseDim() %>% - dimSums(dim = 3, na.rm = T) * 4.1868e-5 * 1e-3 - weights[, c(2005, 2010, 2015, 2020), ] <- iea - weights[, c(seq(2025, 2060, 5), seq(2070, 2100, 10), seq(2110, 2150, 20)), ] <- weights[, 2020, ] - - return( - list( - x = x, - weight = weights, - unit = "GtC/ZJ", - description = "Emission factors for feedstocks in the chemicals industry" - ) - ) -} diff --git a/R/calcEmissionsTe.R b/R/calcEmissionsTe.R deleted file mode 100644 index 1252a347..00000000 --- a/R/calcEmissionsTe.R +++ /dev/null @@ -1,20 +0,0 @@ - -calcEmissionsTe <- function() { - - x <- calcOutput("Emissions", datasource = "CDIAC", aggregate = FALSE)[, 2010, ] - x <- x[, , "Emissions|CO2|Fossil Fuels and Industry (Mt/yr)"] - - x[, , "Emissions|CO2|Industrial Processes|Cement (Mt/yr)"] + - x[, , "Emissions|CO2|Energy|Bunkers (Mt/yr)"] - - # convert from Mt CO2 into GtC - x <- x/44*12/1000 - - # reduce dimension - x <- collapseNames(x) - - return(list(x = x, - weight = NULL, - unit = "GtC", - description = "CO2 emissions (Fossil Fuels and Industry) from CDIAC") - ) -} diff --git a/R/calcFeDemandIndustry.R b/R/calcFeDemandIndustry.R deleted file mode 100644 index cd50e1b1..00000000 --- a/R/calcFeDemandIndustry.R +++ /dev/null @@ -1,1589 +0,0 @@ -#' Calculates FE demand in industry as REMIND variables -#' -#' @param use_ODYM_RECC per-capita pathways for `SDP_xx` scenarios? (Defaults -#' to `FALSE`.) -#' @importFrom assertr assert not_na verify -#' @importFrom data.table := -#' @importFrom dplyr anti_join arrange as_tibble between bind_rows case_when -#' distinct filter first full_join group_by inner_join lag last left_join -#' matches mutate n rename right_join select semi_join summarise ungroup -#' @importFrom quitte as.quitte cartesian character.data.frame -#' interpolate_missing_periods interpolate_missing_periods_ madrat_mule -#' magclass_to_tibble overwrite seq_range -#' @importFrom readr read_delim -#' @importFrom rlang .data sym syms !!! !! -#' @importFrom tibble tribble -#' @importFrom tidyr complete crossing extract gather nesting pivot_longer -#' pivot_wider replace_na separate spread unite -#' @importFrom zoo na.fill -#' @author Michaja Pehl -#' -calcFeDemandIndustry <- function(use_ODYM_RECC = FALSE) { - - # Functions ------------------ - - addSDP_industry <- function(reminditems) { - # Modify industry FE trajectories of SSP1 (and SSP2) to generate SDP - # scenario trajectories - - # mask non-global variables so R doesn't get its panties twisted - year <- Year <- Data1 <- Data2 <- Region <- Value <- Data3 <- scenario <- - iso3c <- value <- variable <- pf <- FE <- VA <- GDP <- VApGDP <- FEpVA <- - gdp_SSP1 <- gdp_SSP2 <- f <- gdp_SDP <- .FE <- f.mod <- gdp <- pop <- - GDPpC <- item <- NULL - - # output years - years <- as.integer(sub("^y", "", getYears(reminditems))) - - tmp_GDPpC <- bind_rows( - # load GDP projections - tmp_GDP <- calcOutput("GDP", average2020 = FALSE, aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - character.data.frame() %>% - mutate(Year = as.integer(as.character(Year))) %>% - filter(grepl("^gdp_SSP[12]$", Data1), - Year %in% years) %>% - separate(col = "Data1", into = c("variable", "scenario"), sep = "_") %>% - select("scenario", iso3c = "Region", year = "Year", "variable", - value = "Value"), - - tmp_pop <- calcOutput("Population", aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - character.data.frame() %>% - mutate(Year = as.integer(as.character(Year))) %>% - filter(grepl("^pop_SSP[12]$", Data1), - Year %in% years) %>% - separate(col = "Data1", into = c("variable", "scenario"), sep = "_") %>% - select("scenario", iso3c = "Region", year = "Year", "variable", - value = "Value") - ) %>% - mutate(scenario = paste0("gdp_", scenario)) %>% - pivot_wider(names_from = "variable") %>% - group_by(.data$scenario, .data$iso3c, .data$year) %>% - summarise(GDPpC = .data$gdp / .data$pop, .groups = "drop") - - # - for each country and scenario, compute a GDPpC-dependent specific energy - # use reduction factor according to 3e-7 * GDPpC + 0.2 [%], which is - # capped at 0.7 % - # - the mean GDPpC of countries with GDPpC > 15000 (in 2015) is about 33k - # - so efficiency gains range from 0.4 % at zero GDPpC (more development - # leeway) to 1.4 % at 33k GDPpC (more stringent energy efficiency) - # - percentage numbers are halved and applied twice, to VA/GDP and FE/VA - # - linearly reduce this reduction factor from 1 to 0 over the 2020-2150 - # interval - # - cumulate the reduction factor over the time horizon - - SSA_countries <- read_delim( - file = toolGetMapping(type = "regional", name = "regionmappingH12.csv", - returnPathOnly = TRUE, where = "mappingfolder"), - delim = ";", - col_names = c("country", "iso3c", "region"), - col_types = "ccc", - skip = 1 - ) %>% - filter("SSA" == !!sym("region")) %>% - select(-"country", -"region") %>% - getElement("iso3c") - - sgma <- 8e3 - cutoff <- 1.018 - epsilon <- 0.018 - exp1 <- 3 - exp2 <- 1.5 - - reduction_factor <- tmp_GDPpC %>% - interpolate_missing_periods(year = seq_range(range(year)), value = "GDPpC") %>% - group_by(scenario, iso3c) %>% - mutate( - # no reduction for SSA countries before 2050, to allow for more - # equitable industry and infrastructure development - f = cumprod(ifelse(2020 > year, 1, pmin(cutoff, 1 + 4 * epsilon * ((sgma / GDPpC)^exp1 - (sgma / GDPpC)^exp2)) - ))) %>% - ungroup() %>% - select(-GDPpC) %>% - filter(year %in% years) - - bind_rows( - # select industry FE use - reminditems %>% - as.data.frame() %>% - as_tibble() %>% - mutate(Year = as.integer(as.character(Year))) %>% - filter(grepl("^gdp_SSP[12]$", Data1), - grepl("fe..i", Data2)) %>% - select(scenario = Data1, iso3c = Region, year = Year, variable = Data2, - value = Value) %>% - character.data.frame(), - - # reuse GDP projections - tmp_GDP %>% - mutate(variable = "GDP", - scenario = paste0("gdp_", scenario)), - - # load VA projections - calcOutput( - type = "Industry_Value_Added", - subtype = "economic", - match.steel.historic.values = TRUE, - match.steel.estimates = "IEA_ETP", - China_Production = readSource(type = "ExpertGuess", - subtype = "Chinese_Steel_Production", - convert = FALSE) %>% - madrat_mule(), - aggregate = FALSE, years = years, supplementary = FALSE) %>% - magclass_to_tibble() %>% - filter(grepl("^gdp_SSP(1|2EU)$", .data$scenario)) %>% - group_by(scenario, iso3c, year) %>% - summarise(value = sum(value), .groups = "drop") %>% - mutate(variable = "VA") %>% - interpolate_missing_periods(year = years, expand.values = TRUE) %>% - character.data.frame() - ) %>% - spread(variable, value) %>% - gather(pf, FE, matches("^fe..i$")) %>% - inner_join(reduction_factor, c("scenario", "iso3c", "year")) %>% - mutate(VApGDP = VA / GDP, - FEpVA = FE / VA) %>% - # Modify reduction factor f based on feeli share in pf - # f for feeli is sqrt of f; for for others choosen such that total - # reduction equals f - group_by(scenario, iso3c, year) %>% - mutate(f.mod = ifelse("feeli" == pf & f < 1, sqrt(f), f)) %>% - ungroup() %>% - select(-f, f = f.mod) %>% - # gather(variable, value, GDP, FE, VA, VApGDP, FEpVA) %>% - # SDP scenario is equal to SSP1 scenario, except for VA/GDP and FE/VA - # indicators, which are equal to the lower value of the SSP1 or SSP2 - # scenario times the reduction factor f(t) - group_by(iso3c, year, pf) %>% - mutate(VApGDP = min(VApGDP) * f, - FEpVA = min(FEpVA) * f) %>% - ungroup() %>% - select(-f) %>% - filter("gdp_SSP2" != scenario) %>% - mutate(scenario = "gdp_SDP") %>% - mutate(.FE = FEpVA * VApGDP * GDP, - value = ifelse(!is.na(.FE), .FE, FE)) %>% - select(scenario, iso3c, year, item = pf, value) %>% - # TODO: differentiate these scenarios if there is a applicable storyline - # for them - complete(nesting(iso3c, year, item, value), - scenario = paste0("gdp_SDP", c("", "_EI", "_MC", "_RC"))) %>% - select(scenario, iso3c, year, item, value) %>% - as.magpie() %>% - return() - } - - stationary <- readSource("Stationary") - - # aggregate to 5-year averages to suppress volatility - stationary <- toolAggregateTimeSteps(stationary) - - # ---- _ modify Industry FE data to carry on current trends ---- - v <- grep("\\.fe(..i$|ind)", getNames(stationary), value = TRUE) - - modified <- stationary[, , v] %>% - as.quitte() %>% - as_tibble() %>% - select("scenario", "iso3c" = "region", "pf" = "item", "year" = "period", - "value") %>% - character.data.frame() - - regionmapping <- toolGetMapping(type = "regional", - name = "regionmappingH12.csv", where = "mappingfolder") %>% - select(country = "X", iso3c = "CountryCode", region = "RegionCode") - - historic_trend <- c(2004, 2020) - phasein_period <- c(2020, 2050) # FIXME: extend to 2055 to keep 35 yrs? - phasein_time <- phasein_period[2] - phasein_period[1] - - modified <- bind_rows( - modified %>% - filter(phasein_period[1] > !!sym("year")), - inner_join( - # calculate regional trend - modified %>% - # get trend period - filter(between(!!sym("year"), historic_trend[1], historic_trend[2]), - 0 != !!sym("value")) %>% - # sum regional totals - full_join(regionmapping %>% select(-!!sym("country")), "iso3c") %>% - group_by(!!sym("scenario"), !!sym("region"), !!sym("pf"), - !!sym("year")) %>% - summarise(value = sum(!!sym("value")), .groups = "drop") %>% - # calculate average trend over trend period - interpolate_missing_periods(year = seq_range(historic_trend), expand.values = TRUE) %>% - group_by(!!sym("scenario"), !!sym("region"), !!sym("pf")) %>% - summarise(trend = mean(!!sym("value") / lag(!!sym("value")), - na.rm = TRUE), - .groups = "drop") %>% - # only use negative trends (decreasing energy use) - mutate(trend = ifelse(!!sym("trend") < 1, !!sym("trend"), NA)), - # modify data projection - modified %>% - filter(phasein_period[1] <= !!sym("year")) %>% - interpolate_missing_periods(year = phasein_period[1]:max(modified$year)) %>% - group_by(!!sym("scenario"), !!sym("iso3c"), !!sym("pf")) %>% - mutate(growth = replace_na(!!sym("value") / lag(!!sym("value")), 1)) %>% - full_join(regionmapping %>% select(-"country"), "iso3c"), - c("scenario", "region", "pf")) %>% - group_by(!!sym("scenario"), !!sym("iso3c"), !!sym("pf")) %>% - mutate( - # replace NA (positive) trends with end. growth rates -> no change - trend = ifelse(is.na(!!sym("trend")), !!sym("growth"), !!sym("trend")), - value_ = first(!!sym("value")) - * cumprod( - ifelse( - phasein_period[1] == !!sym("year"), 1, - (!!sym("trend") - * pmax(0, phasein_period[2] - !!sym("year") + 1) - + !!sym("growth") - * pmin(phasein_time, !!sym("year") - phasein_period[1] - 1) - ) / phasein_time)), - value = ifelse(is.na(!!sym("value_")) | 0 == !!sym("value_"), - !!sym("value"), !!sym("value_"))) %>% - ungroup() %>% - select(-"region", -"value_", -"trend", -"growth") %>% - filter(!!sym("year") %in% unique(modified$year)) - ) %>% - rename("region" = "iso3c", "item" = "pf") %>% - as.magpie() - - # ---- _ modify SSP1 Industry FE demand ---- - # compute a reduction factor of 1 before 2021, 0.84 in 2050, and increasing - # to 0.78 in 2150 - y <- getYears(stationary, as.integer = TRUE) - f <- y - 2020 - f[f < 0] <- 0 - f <- 0.95^pmax(0, log(f)) - - # get Industry FE items - v <- grep("^SSP1\\.fe(..i$|ind)", getNames(modified), value = TRUE) - - # apply changes - for (i in 1:length(y)) { - if (1 != f[i]) { - modified[, y[i], v] <- modified[, y[i], v] * f[i] - } - } - - # ---- _ map to REMIND variables ---- - - data <- mbind(stationary[, , getNames(modified), invert = TRUE], modified) - - mapping <- toolGetMapping(type = "sectoral", name = "structuremappingIO_outputs.csv", - where = "mrcommons") - - mapping <- mapping %>% - select("EDGEitems", "REMINDitems_out", "weight_Fedemand") %>% - na.omit() %>% - filter(.data$EDGEitems %in% getNames(data, dim = "item")) %>% - # REMIND variables in focus: those ending with i and stationary items with industry focus - filter(grepl("i$", .data$REMINDitems_out) | - (grepl("s$", .data$REMINDitems_out)) & grepl("fe(..i$|ind)", .data$EDGEitems)) %>% - distinct() - - remind <- new.magpie(cells_and_regions = getItems(data, dim = 1), - years = getYears(data), - names = cartesian(getNames(data, dim = "scenario"), unique(mapping$REMINDitems_out)), - sets = getSets(data)) - - for (v in unique(mapping$REMINDitems_out)) { - - w <- mapping %>% - filter(.data$REMINDitems_out == v) %>% - select(-"REMINDitems_out") %>% - as.magpie() - - tmp <- mselect(data, item = getNames(w)) * w - - tmp <- dimSums(tmp, dim = "item", na.rm = TRUE) %>% - add_dimension(dim = 3.3, add = "item", nm = v) - - remind[, , getNames(tmp)] <- tmp - } - - # 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)) - - # ---- _ modify SSP1/SSP2 data of CHN/IND further ---- - # To achieve projections more in line with local experts, apply tuning - # factor f to liquids and gas consumption in industry in CHN and IND. - # Apply additional energy intensity reductions 2015-30, that are phased - # out halfway until 2040 again. - # IEIR - initial energy intensity reduction [% p.a.] in 2016 - # FEIR - final energy intensity recovery [% p.a.] in 2040 - # The energy intensity reduction is cumulative over the 2016-40 interval - # and thereafter constant. - - mod_factors <- tribble( - # enter tuning factors for regions/energy carriers - ~region, ~pf, ~IEIR, ~FEIR, - "CHN", "fehoi", 2.5, -0.5, - "CHN", "fegai", -2.5, 3, - "CHN", "feeli", 0.5, 1.5, - "IND", "fehoi", 3, 0, - "IND", "fegai", 12, -5) %>% - # SSP1 factors are half those of SSP2 - gather("variable", "gdp_SSP2", !!sym("IEIR"), !!sym("FEIR"), - factor_key = TRUE) %>% - mutate(gdp_SSP1 = !!sym("gdp_SSP2") / 2) %>% - gather("scenario", "value", matches("^gdp_SSP")) %>% - spread("variable", "value") %>% - mutate(t = as.integer(2016)) %>% - # add missing combinations (neutral multiplication by 1) for easy - # joining - complete(crossing(!!sym("scenario"), !!sym("region"), !!sym("pf"), !!sym("t")), - fill = list(IEIR = 0, FEIR = 0)) %>% - # fill 2016-40 values - complete(nesting(!!sym("scenario"), !!sym("region"), !!sym("pf"), - !!sym("IEIR"), !!sym("FEIR")), - t = 2016:2040) %>% - group_by(!!sym("scenario"), !!sym("region"), !!sym("pf")) %>% - mutate( - f = seq(1 - unique(!!sym("IEIR")) / 100, - 1 - unique(!!sym("FEIR")) / 100, - along.with = !!sym("t"))) %>% - # extend beyond 2050 (neutral multiplication by 1) - complete(t = c(1993:2015, 2041:2150), fill = list(f = 1)) %>% - arrange(!!sym("t")) %>% - mutate(f = cumprod(!!sym("f"))) %>% - filter(t %in% getYears(remind, as.integer = TRUE)) %>% - ungroup() %>% - select(-"IEIR", -"FEIR") - - mod_factors <- bind_rows( - mod_factors, - - mod_factors %>% - filter("gdp_SSP2" == .data$scenario) %>% - mutate(scenario = "gdp_SSP2EU") - ) - - mod_r <- unique(mod_factors$region) - mod_sp <- cartesian(unique(mod_factors$scenario), unique(mod_factors$pf)) - - remind[mod_r, , mod_sp] <- remind[mod_r, , mod_sp] %>% - as.quitte() %>% - as_tibble() %>% - mutate(scenario = as.character(!!sym("scenario")), - region = as.character(!!sym("region")), - item = as.character(!!sym("item"))) %>% - full_join(mod_factors, c("scenario", "region", "period" = "t", - "item" = "pf")) %>% - mutate(value = !!sym("f") * !!sym("value")) %>% - select(-"f") %>% - as.quitte() %>% - as.magpie() - - - feIndustryModifications <- addSDP_industry(remind) - - # delete punk SDP data calculated illicitly in readEDGE('FE_stationary') - remind <- mbind( - remind[, , setdiff(getNames(remind), - getNames(feIndustryModifications))], - feIndustryModifications) - - ## calculate *real* useful (i.e., motive) energy instead of - ## fossil-fuel equivalents for light- and heavy-duty vehicles - ## sources for TtW efficiencies: - ## Cox, B., et al. (2020) Life cycle environmental and cost comparison of - ## current and future passenger cars under different energy scenarios. - ## Applied Energy2. - ## Sacchi, R., et al. (2020) carculator: an open-source tool for - ## prospective environmental and economic life cycle assessment of - ## vehicles. When, Where and How can battery-electric vehicles help reduce - ## greenhouse gas emissions? Renewable and Sustainable Energy Reviews, - ## submitted (in review). https://www.psi.ch/en/media/57994/download - - # remind[,, "ueLDVt"] <- remind[,, "ueLDVt"] * 0.22 - # remind[,, "ueHDVt"] <- remind[,, "ueHDVt"] * 0.24 - - - # ---- Industry subsectors data and FE stubs ---- - ## subsector activity projections ---- - industry_subsectors_ue <- mbind( - calcOutput( - type = "Industry_Value_Added", - match.steel.historic.values = TRUE, - match.steel.estimates = "IEA_ETP", - China_Production = readSource(type = "ExpertGuess", - subtype = "Chinese_Steel_Production", - convert = FALSE) %>% - madrat_mule(), - aggregate = FALSE, - years = getYears(remind), - supplementary = FALSE, - warnNA = FALSE - ), - - calcOutput( - type = "Steel_Projections", - subtype = "production", - match.steel.historic.values = TRUE, - match.steel.estimates = "IEA_ETP", - China_Production = readSource(type = "ExpertGuess", - subtype = "Chinese_Steel_Production", - convert = FALSE) %>% - madrat_mule(), - aggregate = FALSE, - years = getYears(remind), supplementary = FALSE) - ) - - ## re-curve specific industry activity per unit GDP ---- - GDP <- calcOutput(type = "GDP", average2020 = FALSE, - years = getYears(remind), aggregate = FALSE, - supplementary = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", year = "Year", scenario = "Data1", - GDP = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year)) - - ### fix missing GDP numbers ---- - # (see https://github.com/pik-piam/mrdrivers/issues/40) - if (any(0 == GDP$GDP)) { - GDP_fuckup_point <- GDP %>% - filter(0 == .data$GDP) %>% - group_by(!!!syms(c("iso3c", "scenario"))) %>% - filter(min(.data$year) == .data$year) %>% - ungroup() - - GDP_replacement_scenario <- setdiff( - unique(GDP$scenario), - - GDP_fuckup_point %>% - pull("scenario") %>% - unique() - ) %>% - first() - - GDP_fuckup_point <- GDP_fuckup_point %>% - group_by(!!!syms(c("iso3c", "scenario"))) %>% - mutate(base.year = getYears(remind, TRUE) %>% - `[`(which(getYears(remind, TRUE) == !!sym("year")) - 1), - base.scenario = GDP_replacement_scenario) %>% - ungroup() %>% - select(-"GDP") - - GDP_replacement <- full_join( - GDP %>% - semi_join( - GDP_fuckup_point, - c("iso3c", "scenario") - ) %>% - left_join( - GDP_fuckup_point %>% - select("iso3c", "scenario", "base.year"), - - c("iso3c", "scenario") - ) %>% - filter(.data$year >= .data$base.year) %>% - select("iso3c", "year", "scenario", "GDP"), - - GDP %>% - semi_join( - GDP_fuckup_point, - - c("iso3c", scenario = "base.scenario") - ) %>% - left_join( - GDP_fuckup_point %>% - select("iso3c", "base.year", scenario = "base.scenario") %>% - distinct(), - - c("iso3c", "scenario") - ) %>% - filter(.data$year >= .data$base.year) %>% - select(-"base.year") %>% - group_by(!!!syms(c("iso3c", "scenario"))) %>% - mutate(factor = .data$GDP / lag(.data$GDP, default = first(.data$GDP), - order_by = .data$year)) %>% - ungroup() %>% - select("iso3c", "year", "factor"), - - c("iso3c", "year") - ) %>% - group_by(!!!syms(c("iso3c", "scenario"))) %>% - mutate(GDP = first(.data$GDP) * .data$factor) %>% - ungroup() %>% - select(all_of(colnames(GDP))) - - GDP <- bind_rows( - anti_join(GDP, GDP_replacement, by = c("iso3c", "scenario", "year")), - GDP_replacement - ) %>% - verify(expr = 0 < .data$GDP, description = "All GDP numbers > 0") - } - - ### calculate specific material demand factors ---- - foo <- full_join( - industry_subsectors_ue %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", year = "Year", scenario = "Data1", subsector = "Data2", value = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(.data$year)) %>% - # remove zero activity from historic data, to be extended backwards by - # first projection below - filter(!(0 == .data$value & 2020 >= .data$year)) %>% - verify(expr = .data$value != 0, description = "No zero subsector activity after 2020"), - GDP, - by = c("iso3c", "year", "scenario") - ) %>% - # expand time series of per-GDP production back into time for - # non-existent production in historic periods (e.g. primary steel in - # NEN) - complete(nesting(!!!syms(c("iso3c", "year", "scenario", "GDP"))), - subsector = unique(.data$subsector), - fill = list(value = NA_real_)) %>% - group_by(.data$iso3c, .data$scenario, .data$subsector) %>% - mutate( - value = .data$GDP - * na.fill(object = .data$value / .data$GDP, - fill = first( - x = .data$value[!is.na(.data$value)] - / .data$GDP[!is.na(.data$value)], - order_by = .data$year[!is.na(.data$value)])) - ) %>% - ungroup() - - region_mapping_21 <- toolGetMapping("regionmapping_21_EU11.csv", "regional", where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = "CountryCode", region = "RegionCode") - - industry_subsectors_material_alpha <- calcOutput( - type = "industry_subsectors_specific", subtype = "material_alpha", - scenarios = c(getNames(x = industry_subsectors_ue, dim = 1), - "gdp_SSP2_lowEn"), - regions = unique(region_mapping_21$region), - aggregate = FALSE - ) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", region = "Data2", subsector = "Data3", - name = "Data4", value = "Value") %>% - character.data.frame() %>% - pivot_wider() %>% - mutate(subsector = paste0("ue_", .data$subsector)) - - industry_subsectors_material_relative <- calcOutput( - type = "industry_subsectors_specific", subtype = "material_relative", - scenarios = c(getNames(x = industry_subsectors_ue, dim = 1), - "gdp_SSP2_lowEn"), - regions = unique(region_mapping_21$region), - aggregate = FALSE - ) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", base.scenario = "Data2", region = "Data3", - subsector = "Data4", name = "Data5", value = "Value") %>% - character.data.frame() %>% - pivot_wider() %>% - mutate(subsector = paste0("ue_", .data$subsector)) - - if (use_ODYM_RECC) { - industry_subsectors_material_relative <- industry_subsectors_material_relative %>% - filter(!.data$scenario %in% c("gdp_SDP_EI", "gdp_SDP_MC", - "gdp_SDP_RC")) - } - - - industry_subsectors_material_relative_change <- calcOutput( - type = "industry_subsectors_specific", - subtype = "material_relative_change", - scenarios = c(getNames(x = industry_subsectors_ue, dim = 1), - "gdp_SSP2_lowEn"), - regions = unique(region_mapping_21$region), - aggregate = FALSE - ) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", base.scenario = "Data2", region = "Data3", - subsector = "Data4", name = "Data5", value = "Value") %>% - character.data.frame() %>% - pivot_wider() %>% - mutate(subsector = paste0("ue_", .data$subsector)) - - if (use_ODYM_RECC) { - industry_subsectors_material_percapita <- calcOutput( - type = "ODYM_RECC", - subtype = "REMIND_industry_trends", - aggregate = FALSE) %>% - magclass_to_tibble() %>% - mutate(scenario = sub("^(gdp_)?", "gdp_", .data$scenario)) %>% - filter(!.data$scenario %in% c( - unique(industry_subsectors_material_alpha$scenario), - unique(industry_subsectors_material_relative$scenario), - unique(industry_subsectors_material_relative_change$scenario))) %>% - interpolate_missing_periods_( - periods = list( - year = unique(pmax(getYears(remind, as.integer = TRUE), - min(.$year)))), - expand.values = TRUE) - } - - bind_rows( - industry_subsectors_material_alpha %>% select("scenario", "region", "subsector"), - industry_subsectors_material_relative %>% select("scenario", "region", "subsector"), - industry_subsectors_material_relative_change %>% select("scenario", "region", "subsector") - ) %>% - group_by(!!!syms(c("scenario", "region", "subsector"))) %>% - summarise(count = n(), .groups = "drop") %>% - verify(expr = 1 == .data$count, - error_fun = function(errors, data) { - stop("Industry specific material is over-specified for:\n", - paste( - format(data[errors[[1]]$error_df$index, ], - width = 80, - n = errors[[1]]$num.violations), - collapse = "\n")) - }) - - foo2 <- bind_rows( - ### SSP2EU default scenario ---- - foo %>% filter("gdp_SSP2EU" == .data$scenario), - - ### alpha relative to SSP2EU ---- - industry_subsectors_material_alpha %>% - full_join( - full_join( - foo %>% - filter("gdp_SSP2EU" == .data$scenario) %>% - # keep specific production constant for historic years without - # production - group_by(!!!syms(c("iso3c", "subsector"))) %>% - mutate(specific.production = .data$value / .data$GDP, - specific.production = ifelse( - 0 != .data$value, .data$specific.production, - first(.data$specific.production[0 != .data$value], - order_by = .data$year))) %>% - ungroup() %>% - select("iso3c", "year", "subsector", "specific.production") %>% - interpolate_missing_periods_( - periods = list(year = seq_range(range(.$year))), - value = "specific.production", - method = "linear"), - - # mark years which have no historic production so that we can set - # them to 0 once we calculated future production for the new - # scenarios - foo %>% - filter("gdp_SSP2EU" == .data$scenario) %>% - mutate(fake.value = 0 == .data$value) %>% - select("iso3c", "year", "subsector", "fake.value") %>% - interpolate_missing_periods_( - periods = list(year = seq_range(range(.$year))), - value = "fake.value", - method = "linear") %>% - mutate(fake.value = 0 != .data$fake.value), - - c("iso3c", "year", "subsector") - ) %>% - full_join(region_mapping_21, "iso3c"), - - by = c("region", "subsector"), - relationship = "many-to-many" - ) %>% - group_by(!!!syms(c("scenario", "subsector", "iso3c"))) %>% - mutate( - # alpha factors converge linearly towards zero over the convergence - # time - conv.fctr = 1 - pmin(1, ((.data$year - 2015) - / .data$convergence_time)), - alpha.conv = ifelse(2015 >= .data$year, 1, - 1 - .data$alpha * .data$conv.fctr), - # specific production is scaled with the cumulated converged alpha - # factors - cum.fctr = cumprod(.data$alpha.conv), - specific.production = ifelse( - 2020 >= .data$year, .data$specific.production, - (.data$specific.production[2020 == .data$year] - * .data$cum.fctr - )) - # ensure that years without historic production are 0 - * (1 - .data$fake.value)) %>% - ungroup() %>% - select("region", "iso3c", "year", "scenario", "subsector", - "specific.production") %>% - filter(.data$year %in% unique(foo$year)) %>% - left_join( - bind_rows( - foo, - - foo %>% - filter("gdp_SSP2EU" == .data$scenario) %>% - mutate(scenario = "gdp_SSP2_lowEn") - ), - - c("scenario", "subsector", "iso3c", "year") - ) %>% - mutate(value = .data$specific.production * .data$GDP) %>% - select(all_of(colnames(foo))) %>% - assert(not_na, everything()) - ) - - foo3 <- bind_rows( - foo2, - - ### specific production relative baseline ---- - industry_subsectors_material_relative %>% - left_join( - foo2 %>% - mutate(specific.production = .data$value / .data$GDP) %>% - select(base.scenario = "scenario", "subsector", "iso3c", "year", - "specific.production") %>% - full_join(region_mapping_21, "iso3c"), - - by = c("base.scenario", "region", "subsector"), - relationship = "many-to-many" - ) %>% - left_join( - foo %>% - select("scenario", "subsector", "iso3c", "year", "GDP", "value"), - - c("scenario", "subsector", "iso3c", "year") - ) %>% - assert(not_na, everything()) %>% - # scale factor in from 2020-35 - mutate(l = pmin(1, pmax(0, (.data$year - 2020) / (2035 - 2020))), - value = .data$specific.production - * .data$GDP - * (.data$factor * .data$l + 1 * (1 - .data$l))) %>% - select(all_of(colnames(foo))), - - ### specific production change relative baseline ---- - full_join( - # base scenario data - foo %>% - rename(base.scenario = "scenario") %>% - semi_join( - industry_subsectors_material_relative_change, - - c("base.scenario", "subsector") - ) %>% - full_join(region_mapping_21, "iso3c"), - - # change parameters - industry_subsectors_material_relative_change, - - c("base.scenario", "region", "subsector") - ) %>% - select("scenario", "iso3c", "region", "subsector", "year", - base.value = "value", base.GDP = "GDP", "factor") %>% - # GDP trajectories of target scenarios - left_join( - foo %>% - select("scenario", "iso3c", "subsector", "year", "GDP"), - - c("scenario", "iso3c", "subsector", "year") - ) %>% - group_by(!!!syms(c("scenario", "iso3c", "subsector"))) %>% - mutate( - # specific production of base scenarios - base.specific.production = .data$base.value / .data$base.GDP, - # change in specific production of base scenarios relative to 2020 - base.change = (.data$base.specific.production - / .data$base.specific.production[2020 == .data$year]), - # modified change of target scenarios - # If base change is below (above) 1, i.e. material efficiency is - # improving (deteriorating), efficiency gains (losses) are halved - # (doubled). Changes of historic values (i.e. before 2015) are - # identical to base scenario. Not finite changes (e.g. division by - # zero) lead to constant values. - change = case_when( - !is.finite(.data$base.change) ~ 1, - 2020 >= .data$year ~ .data$base.change, - TRUE ~ (((.data$base.change - 1) - * .data$factor^sign(1 - .data$base.change) - ) - + 1)), - specific.production = - (.data$base.specific.production[2020 == .data$year] - * .data$change - ), - value = ifelse(!is.finite(.data$base.change) | 2020 >= .data$year, - .data$base.value, - .data$specific.production * .data$GDP)) %>% - ungroup() %>% - select("scenario", "iso3c", "subsector", "year", "value", "GDP") - ) - - ### per-capita projections ---- - . <- NULL - - if (use_ODYM_RECC) { - foo4 <- bind_rows( - foo3 %>% select(-"GDP"), - - foo3 %>% - filter( - "gdp_SSP2EU" == .data$scenario, - min(industry_subsectors_material_percapita$year) > .data$year) %>% - select(-"GDP") %>% - complete( - nesting(!!!syms(c("iso3c", "year", "subsector", "value"))), - scenario = unique(industry_subsectors_material_percapita$scenario) - ) %>% - filter("gdp_SSP2EU" != .data$scenario), - - foo3 %>% - filter( - "gdp_SSP2EU" == .data$scenario, - min(industry_subsectors_material_percapita$year) == .data$year - ) %>% - select(-"scenario", -"GDP", -"year") %>% - inner_join( - calcOutput(type = "Population", aggregate = FALSE, - years = getYears(remind)) %>% - magclass_to_tibble() %>% - select("iso3c", "scenario" = "variable", "year", - "population" = "value") %>% - mutate(scenario = sub("^pop_", "gdp_", .data$scenario)) %>% - filter( - min(industry_subsectors_material_percapita$year) <= .data$year, - .data$scenario %in% - unique(industry_subsectors_material_percapita$scenario) - ) %>% - group_by(.data$iso3c, .data$scenario) %>% - mutate( - population = .data$population - / first(.data$population, order_by = .data$year)) %>% - ungroup(), - - "iso3c") %>% - inner_join( - industry_subsectors_material_percapita %>% - mutate(subsector = paste0("ue_", .data$subsector)) %>% - interpolate_missing_periods_( - periods = list(year = seq_range(range(.$year)))) %>% - rename(activity = "value"), - - c("iso3c", "subsector", "year", "scenario") - ) %>% - mutate(value = .data$value * .data$population * .data$activity) %>% - select("iso3c", "scenario", "subsector", "year", "value") - ) - - industry_subsectors_ue <- foo4 %>% - select("iso3c", "year", "scenario", pf = "subsector", "value") %>% - as.magpie(spatial = 1, temporal = 2, data = ncol(.)) - } else { - industry_subsectors_ue <- foo3 %>% - select("iso3c", "year", "scenario", pf = "subsector", "value") %>% - as.magpie(spatial = 1, temporal = 2, data = ncol(.)) - } - - ## subsector FE shares ---- - ### get 1993-2020 industry FE ---- - industry_subsectors_en <- calcOutput( - type = "IO", subtype = "output_Industry_subsectors", - aggregate = FALSE - ) %>% - # convert to data frame - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", year = "Year", pf = "Data2", - value = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - # get 1993-2020 industry FE data - filter(grepl("^fe.*_(cement|chemicals|steel|otherInd)", .data$pf), - between(.data$year, 1993, 2020)) %>% - # sum up fossil and bio SE (which produce the same FE), aggregate - # regions - full_join(region_mapping_21, "iso3c") %>% - group_by(!!!syms(c("year", "region", "pf"))) %>% - summarise(value = sum(.data$value), .groups = "drop") %>% - # split feel steel into primary and secondary production - left_join( - industry_subsectors_ue[, , "ue_steel", pmatch = TRUE] %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", scenario = "Data1", year = "Year", - subsector = "Data2", production = "Value") %>% - filter("gdp_SSP2" == .data$scenario) %>% - select(-"scenario") %>% - # aggregate regions - full_join(region_mapping_21, "iso3c") %>% - group_by(!!!syms(c("year", "region", "subsector"))) %>% - summarise(production = sum(.data$production), .groups = "drop") %>% - mutate(year = as.integer(as.character(.data$year)), - pf = "feel_steel", - subsector = sub("_production$", "", .data$subsector)) %>% - pivot_wider(names_from = "subsector", values_from = "production"), - - c("region", "year", "pf") - ) %>% - group_by(!!!syms(c("year", "region", "pf"))) %>% - # if there was no historic steel production, but feel_steel was - # allocated, assume primary and secondary production to be identical - mutate( - foo = .data$ue_steel_primary + .data$ue_steel_secondary, - ue_steel_primary = ifelse(0 == .data$foo, 1, - .data$ue_steel_primary), - ue_steel_secondary = ifelse(0 == .data$foo, 1, - .data$ue_steel_secondary) - ) %>% - select(-"foo") %>% - # assume that secondary steel production is nine times as electricity - # intensive (not energy intensive!) as primary production, since - # detailed data is missing so far - mutate(feel_steel_secondary = - (9 * .data$ue_steel_secondary * .data$value) - / (9 * .data$ue_steel_secondary + .data$ue_steel_primary), - feel_steel_primary = .data$value - .data$feel_steel_secondary - ) %>% - ungroup() %>% - select(-"ue_steel_primary", -"ue_steel_secondary") %>% - pivot_wider(names_from = "pf") %>% - select(-"feel_steel") %>% - pivot_longer(matches("^fe.*"), names_to = "pf", values_drop_na = TRUE) - - # can be removed once feelwlth is replaced by feel in all mappings for - # calcIO() - industry_subsectors_en <- industry_subsectors_en %>% - mutate(pf = sub("^feelwlth_", "feel_", .data$pf)) - - - FE_alpha_mod <- 1 - - ### calculate 1993-2020 industry subsector FE shares ---- - industry_subsectors_en_shares <- industry_subsectors_en %>% - mutate(subsector = sub("^[^_]+_", "", .data$pf), - subsector = ifelse("steel" == .data$subsector, "steel_primary", - .data$subsector)) %>% - group_by(!!!syms(c("year", "region", "subsector"))) %>% - mutate(share = .data$value / sum(.data$value), - share = ifelse(is.finite(.data$share), .data$share, 0)) %>% - ungroup() %>% - select(-"value") - - failed_share_sum <- industry_subsectors_en_shares %>% - group_by(!!!syms(c("year", "region", "subsector"))) %>% - summarise(failed = abs(sum(.data$share) - 1) > 1e-15, .groups = "drop") - - if (any(failed_share_sum$failed)) { - stop("industry_subsectors_en_shares don't add up to 1.") - } - - ### future subsector FE shares from IEA ETP 2017 ---- - IEA_ETP_Ind_FE_shares <- readSource("IEA_ETP", "industry", - convert = FALSE) %>% - # filter for OECD and Non-OECD regions and RTS scenario - `[`(c("OECD", "Non-OECD"), , "RTS", pmatch = "left") %>% - # convert to data frame - as.data.frame() %>% - as_tibble() %>% - select(region = "Region", year = "Year", variable = "Data2", - value = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - # filter for future data - filter(max(industry_subsectors_en$year) < .data$year) %>% - # rename variables - right_join( - tribble( - ~subsector, ~fety, ~variable, - "cement", "feso", "Industry|Cement - final energy consumption|Coal", - "cement", "feso", "Industry|Cement - final energy consumption|Biomass", - "cement", "feso", "Industry|Cement - final energy consumption|Waste", - "cement", "feso", "Industry|Cement - final energy consumption|Other renewables", - "cement", "feli", "Industry|Cement - final energy consumption|Oil", - "cement", "fega", "Industry|Cement - final energy consumption|Natural gas", - "cement", "feel", "Industry|Cement - final energy consumption|Electricity", - - "chemicals", "feso", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Coal", - "chemicals", "feso", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Biomass", - "chemicals", "feso", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Waste", - "chemicals", "feso", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Other renewables", - "chemicals", "feli", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Oil", - "chemicals", "fega", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Natural gas", - "chemicals", "feel", "Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|Electricity", - - "steel", "feso", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Coal", - "steel", "feso", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Biomass", - "steel", "feso", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Waste", - "steel", "feso", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Other renewables", - "steel", "feli", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Oil", - "steel", "fega", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Natural gas", - "steel", "feel", "Industry|Iron and steel - final energy consumption incl_ blast furnaces and coke ovens|Electricity", - - "total", "feso", "Industry|Total industry final energy consumption|Coal", - "total", "feso", "Industry|Total industry final energy consumption|Biomass", - "total", "feso", "Industry|Total industry final energy consumption|Waste", - "total", "feso", "Industry|Total industry final energy consumption|Other renewables", - "total", "feli", "Industry|Total industry final energy consumption|Oil", - "total", "fega", "Industry|Total industry final energy consumption|Natural gas", - "total", "fehe", "Industry|Total industry final energy consumption|Heat", - "total", "feel", "Industry|Total industry final energy consumption|Electricity"), - - "variable" - ) %>% - # drop faulty data - # - 2055/OECD/Chemicals (all zero) - anti_join( - tribble( - ~region, ~year, ~subsector, - 'OECD', 2055, 'chemicals'), - c('region', 'year', 'subsector')) %>% - # - 2055/Non-OECD/Chemicals/Oil (exceeds total Oil) - anti_join( - tribble( - ~region, ~year, ~subsector, ~fety, - 'Non-OECD', 2055, 'chemicals', 'feli'), - c('region', 'year', 'subsector', 'fety')) %>% - # aggregate by subsector and fety - group_by(!!!syms(c("region", "year", "subsector", "fety"))) %>% - summarise(value = sum(.data$value), .groups = "drop") %>% - # fill gaps (e.g. 2055/Chemicals) with interpolated data - complete(nesting(!!!syms(c("region", "subsector", "fety"))), - year = unique(.data$year)) %>% - interpolate_missing_periods_(periods = list(year = unique(.$year))) %>% - # calculate otherInd as total - cement - chemicals - steel - pivot_wider(names_from = "subsector", values_fill = 0) %>% - mutate(otherInd = .data$total - - (.data$cement + .data$chemicals + .data$steel)) %>% - select(-"total") %>% - pivot_longer(c("cement", "chemicals", "steel", "otherInd"), - names_to = "subsector") %>% - filter(0 != .data$value) %>% - # calculate share - group_by(!!!syms(c("region", "year", "subsector"))) %>% - mutate(share = .data$value / sum(.data$value)) %>% - ungroup() %>% - select(-"value") - - ### split feel shares and extend to SSP scenarios ---- - IEA_ETP_Ind_FE_shares <- bind_rows( - # all pf that don't need splitting - IEA_ETP_Ind_FE_shares %>% - semi_join( - industry_subsectors_en %>% - distinct(.data$pf) %>% - separate("pf", c("fety", "subsector"), sep = "_", - extra = "merge"), - - c("fety", "subsector") - ) %>% - unite("pf", c("fety", "subsector"), sep = "_") %>% - # extend to SSP scenarios - mutate(scenario = "gdp_SSP1") %>% - complete(nesting(!!sym("year"), !!sym("region"), !!sym("pf"), - !!sym("share")), - scenario = unique(sub("\\..*$", "", - getNames(industry_subsectors_ue)))), - - # split feel_steel into primary and secondary steel - IEA_ETP_Ind_FE_shares %>% - filter("feel" == .data$fety, "steel" == .data$subsector) %>% - select(-"fety", -"subsector") %>% - inner_join( - industry_subsectors_ue %>% - `[`(, , "ue_steel_", pmatch = TRUE) %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", year = "Year", scenario = "Data1", - pf = "Data2", value = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - inner_join( - toolGetMapping(name = "regionmappingOECD.csv", - type = "regional", where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = "CountryCode", region = "RegionCode"), - - "iso3c" - ) %>% - group_by(!!!syms(c("region", "year", "scenario", "pf"))) %>% - summarise(value = sum(.data$value), .groups = "drop"), - - c("region", "year") - ) %>% - pivot_wider(names_from = "pf") %>% - # as above, assume that secondary steel production is nine times as - # electricity intensive (not energy intensive!) as primary production, - # since detailed data is missing so far - # reduce primary steel electricity share accordingly; secondary steel - # electricity share is 1 since only electricity is assumed to be used - mutate( - feel_steel_primary = .data$share - - ((9 * .data$ue_steel_secondary * .data$share) - / (9 * .data$ue_steel_secondary + .data$ue_steel_primary) - ), - feel_steel_secondary = 1) %>% - select(-"share", -"ue_steel_primary", -"ue_steel_secondary") %>% - pivot_longer(c("feel_steel_primary", "feel_steel_secondary"), - names_to = "pf", values_to = "share") - ) - - ### extend time horizon and convert regions ---- - IEA_ETP_Ind_FE_shares <- IEA_ETP_Ind_FE_shares %>% - mutate(subsector = sub("^[^_]+_", "", .data$pf), - subsector = ifelse("steel" == .data$subsector, "steel_primary", - .data$subsector)) %>% - interpolate_missing_periods_( - periods = list(year = unique(pmax(min(IEA_ETP_Ind_FE_shares$year), - getYears(remind, - as.integer = TRUE)))), - value = "share", - expand.values = TRUE) %>% - inner_join( - toolGetMapping(name = "regionmappingOECD.csv", - type = "regional", where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = "CountryCode", region = "RegionCode"), - - "region" - ) %>% - select("scenario", "iso3c", period = "year", "pf", "subsector", - "share") %>% - # weight by subsector activity - left_join( - industry_subsectors_ue %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = "Region", period = "Year", scenario = "Data1", - subsector = "Data2", activity = "Value") %>% - character.data.frame() %>% - mutate(period = as.integer(as.character(.data$period)), - subsector = sub("^ue_", "", .data$subsector)), - - c("scenario", "iso3c", "period", "subsector") - ) %>% - full_join(region_mapping_21, "iso3c") %>% - group_by( - !!!syms(c("scenario", "region", "period", "subsector", "pf"))) %>% - summarise( - share = ifelse(0 == sum(.data$activity), 0, - sum(.data$share * .data$activity) - / sum(.data$activity)), - .groups = "drop_last") %>% - # re-normalise shares - mutate(share = case_when( - 0 == sum(.data$share) ~ 1 / n(), - TRUE ~ .data$share / sum(.data$share))) %>% - verify(expr = is.finite(.data$share), - description = paste("Finite IEA ETP industry FE shares after", - "time horizon extension.")) %>% - ungroup() - - ### combine historic and future industry FE shares ---- - industry_subsectors_en_shares <- inner_join( - industry_subsectors_en_shares %>% - mutate(scenario = first(IEA_ETP_Ind_FE_shares$scenario)) %>% - complete(nesting(!!!syms(setdiff(colnames(.), "scenario"))), - scenario = unique(IEA_ETP_Ind_FE_shares$scenario)) %>% - interpolate_missing_periods_( - periods = list(year = unique(c(industry_subsectors_en_shares$year, - getYears(remind, - as.integer = TRUE)))), - value = "share", - expand.values = TRUE) %>% - select("scenario", "year", "region", "pf", "subsector", - share.hist = "share") %>% - # re-normalise steel_primary to extended feel_steel_primary - group_by(!!!syms(c("scenario", "year", "region", "subsector"))) %>% - mutate(share.hist = .data$share.hist / sum(.data$share.hist)) %>% - ungroup(), - - IEA_ETP_Ind_FE_shares %>% - interpolate_missing_periods_( - periods = list(period = unique(c(industry_subsectors_en_shares$year, - getYears(remind, - as.integer = TRUE)))), - value = "share", - expand.values = TRUE) %>% - select("scenario", year = "period", "region", "pf", "subsector", - share.future = "share"), - - c("scenario", "year", "region", "pf", "subsector") - ) %>% - mutate(foo = pmin(1, pmax(0, (.data$year - 2015) / (2100 - 2015))), - share = .data$share.hist * (1 - .data$foo) - + .data$share.future * .data$foo, - subsector = ifelse("steel" == .data$subsector, - "steel_primary", .data$subsector)) %>% - verify(expr = is.finite(.data$share), - description = paste("Finite industry FE shares after combining", - "historic and future values")) %>% - select(-"foo", -"share.hist", -"share.future") - - failed_share_sum <- industry_subsectors_en_shares %>% - group_by(!!!syms(c("scenario", "year", "region", "subsector"))) %>% - summarise(failed = abs(sum(.data$share) - 1) > 1e-15, .groups = "drop") - - if (any(failed_share_sum$failed)) { - stop("industry_subsectors_en_shares don't add up to 1.") - } - - ### extend to H2 and HTH_el shares ---- - feh2_share_in_fega <- 0.3 - # H2 shares grow linearly from 0.1 % to feh2_share_in_fega of fega from - # 2020 to 2050 and are constant afterwards - industry_subsectors_en_shares <- bind_rows( - industry_subsectors_en_shares %>% - filter(!grepl("^fega", .data$pf)), - - industry_subsectors_en_shares %>% - filter(grepl("^fega", .data$pf)) %>% - extract("pf", c("pf.fety", "pf.subsector"), "^([^_]*)_(.*)") %>% - complete(nesting(!!!syms(setdiff(colnames(.), "pf.fety"))), - pf.fety = c("fega", "feh2")) %>% - pivot_wider(names_from = "pf.fety", values_from = "share") %>% - mutate(feh2 = pmin(feh2_share_in_fega, - pmax(0.01, - feh2_share_in_fega - * (.data$year - 2020) / (2050 - 2020))) - * .data$fega, - fega = .data$fega - .data$feh2) %>% - pivot_longer(c("fega", "feh2"), names_to = "pf.fety", - values_to = "share") %>% - unite("pf", c("pf.fety", "pf.subsector"), sep = "_") - ) - - feelhth_share_in_fuel <- 0.08 - # HTH_el shares grow linearly from 0.1 % to feelhth_share_in_fuel of all - # FE but feel from 2020 to 2050 and are constant afterwards - industry_subsectors_en_shares <- bind_rows( - # subsectors w/o HTH_el - industry_subsectors_en_shares %>% - filter(!.data$subsector %in% c("chemicals", "otherInd")), - - # WLTH_el - industry_subsectors_en_shares %>% - filter(.data$subsector %in% c("chemicals", "otherInd"), - grepl("^feel", .data$pf)) %>% - mutate(pf = sub("^feel_", "feelwlth_", .data$pf)), - - # HTH_el based on share of fuels - industry_subsectors_en_shares %>% - filter(.data$subsector %in% c("chemicals", "otherInd"), - !grepl("^feel", .data$pf)) %>% - mutate(pf = sub("(_[^_]+)$", "", .data$pf)) %>% - group_by(!!!syms(setdiff(colnames(.), c("pf", "share")))) %>% - mutate(feelhth = pmin(feelhth_share_in_fuel, - pmax(0.01, - feelhth_share_in_fuel - * (.data$year - 2020) / (2050 - 2020))) - * sum(.data$share), - share = .data$share - / sum(.data$share) - * (sum(.data$share) - .data$feelhth)) %>% - ungroup() %>% - pivot_wider(names_from = "pf", values_from = "share") %>% - pivot_longer(c(-"scenario", -"year", -"region", -"subsector"), - names_to = "pf", values_to = "share", - values_drop_na = TRUE) %>% - mutate(pf = paste(.data$pf, .data$subsector, sep = "_")) - ) - - failed_share_sum <- industry_subsectors_en_shares %>% - group_by(!!!syms(c("scenario", "year", "region", "subsector"))) %>% - summarise(failed = abs(sum(.data$share) - 1) > 1e-15, .groups = "drop") - - if (any(failed_share_sum$failed)) { - stop("industry_subsectors_en_shares don't add up to 1.") - } - - ### calculate industry total FE level ---- - # scale industry subsector total FE by subsector activity and exogenous - # energy efficiency gains - - specific_FE_limits <- readSource(type = "industry_subsectors_specific", - subtype = "industry_specific_FE_limits", - convert = FALSE) %>% - madrat_mule() - - industry_subsectors_specific_FE <- calcOutput( - type = "industry_subsectors_specific", subtype = "FE", - scenarios = c(getNames(x = industry_subsectors_ue, dim = 1), - "gdp_SSP2_lowEn"), - regions = unique(region_mapping_21$region), - aggregate = FALSE - ) %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", region = "Data2", subsector = "Data3", - name = "Data4", value = "Value") %>% - character.data.frame() %>% - pivot_wider() %>% - mutate(alpha = .data$alpha * FE_alpha_mod) - - industry_subsectors_specific_energy <- inner_join( - industry_subsectors_en %>% - mutate(subsector = sub("^[^_]+_", "", .data$pf), - subsector = ifelse("steel" == .data$subsector, "steel_primary", - .data$subsector), - scenario = first(IEA_ETP_Ind_FE_shares$scenario)) %>% - # extend to SSP scenarios - complete(nesting(!!!syms(setdiff(colnames(.), "scenario"))), - scenario = unique(industry_subsectors_en_shares$scenario) - ) %>% - group_by(!!!syms(c("scenario", "region", "year", "subsector"))) %>% - summarise(value = sum(.data$value), .groups = "drop"), - - industry_subsectors_ue %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", iso3c = "Region", year = "Year", - pf = "Data2", level = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - filter(.data$year %in% unique(industry_subsectors_en$year)) %>% - # aggregate regions - full_join(region_mapping_21, "iso3c") %>% - group_by(!!!syms(c("scenario", "region", "year", "pf"))) %>% - summarise(level = sum(.data$level), .groups = "drop") %>% - extract("pf", "subsector", "^ue_(.*)$"), - - c("scenario", "region", "year", "subsector") - ) %>% - mutate(specific.energy = .data$value / .data$level, - specific.energy = ifelse(is.finite(.data$specific.energy), - .data$specific.energy, 0)) %>% - select("scenario", "region", "year", "subsector", "specific.energy") - - # replace 0 specific energy (e.g. primary steel NEN) with global averages - industry_subsectors_specific_energy <- - industry_subsectors_specific_energy %>% - anti_join( - industry_subsectors_specific_energy %>% - filter(0 == .data$specific.energy), - - c("scenario", "region", "year", "subsector") - ) %>% - bind_rows( - left_join( - industry_subsectors_specific_energy %>% - filter(0 == .data$specific.energy) %>% - select(-"specific.energy"), - - industry_subsectors_specific_energy %>% - filter(0 != .data$specific.energy) %>% - group_by(!!!syms(c("scenario", "year", "subsector"))) %>% - summarise(specific.energy = mean(.data$specific.energy), - .groups = "drop"), - - c("scenario", "year", "subsector") - ) - ) %>% - verify(expr = 0 < .data$specific.energy, - description = "All specific energy factors above 0") - - # replace absurdly high specific energy (e.g. primary steel NEN after IEA - # 2021 data update) with EUR averages (considered peer-countries to NEN – - # CHE, GRL, ISL, LIE, NOR, SJM). - industry_subsectors_specific_energy <- industry_subsectors_specific_energy %>% - filter("steel_primary" == .data$subsector, 100 < .data$specific.energy) %>% - select(-"specific.energy") %>% - left_join( - industry_subsectors_specific_energy %>% - filter("steel_primary" == .data$subsector, - .data$region %in% c("DEU", "ECE", "ECS", "ENC", "ESC", "ESW", "EWN", "FRA", "UKI")) %>% - group_by(.data$scenario, .data$year, .data$subsector) %>% - summarise(specific.energy = mean(.data$specific.energy), .groups = "drop"), - - c("scenario", "year", "subsector") - ) %>% - overwrite(industry_subsectors_specific_energy, except = "specific.energy") - - # extend time horizon - industry_subsectors_specific_energy <- - industry_subsectors_specific_energy %>% - interpolate_missing_periods_( - periods = list(year = unique(industry_subsectors_en_shares$year)), - value = "specific.energy", expand.values = TRUE) - - # correct lower-then-thermodynamic limit projections - too_low_projections <- industry_subsectors_specific_energy %>% - left_join( - specific_FE_limits %>% - filter("absolute" == .data$type) %>% - select(-"type"), - - "subsector" - ) %>% - filter(2020 < .data$year, - !is.na(.data$limit), - .data$specific.energy < .data$limit) %>% - select("scenario", "region", "subsector", "year") - - if (0 != nrow(too_low_projections)) { - industry_subsectors_specific_energy <- bind_rows( - industry_subsectors_specific_energy %>% - anti_join( - too_low_projections, - - c("scenario", "region", "subsector", "year") - ), - - industry_subsectors_specific_energy %>% - semi_join( - too_low_projections %>% - select(-"region"), - - c("scenario", "subsector", "year") - ) %>% - anti_join( - too_low_projections, - - c("scenario", "region", "subsector", "year") - ) %>% - group_by(!!!syms(c("scenario", "subsector", "year"))) %>% - summarise(specific.energy = mean(.data$specific.energy), - .groups = "drop") %>% - full_join( - too_low_projections, - - c("scenario", "subsector", "year") - ) %>% - assert(not_na, everything()) - ) - } - - # decrease values by alpha p.a. - industry_subsectors_specific_energy <- - industry_subsectors_specific_energy %>% - # FIXME this factors should be derived from region data - # since the IEA data needs fixing first, they were derived manually for - # now - inner_join( - industry_subsectors_specific_FE, - - c("scenario", "region", "subsector") - ) %>% - inner_join(specific_FE_limits, "subsector") %>% - group_by(!!!syms(c("scenario", "region", "subsector"))) %>% - arrange(.data$year) %>% - mutate( - specific.energy = ifelse( - "absolute" == .data$type, - ((.data$specific.energy - .data$limit) - * pmin(1, (1 - .data$alpha)^(.data$year - 2020)) - ) - + .data$limit, - - (.data$specific.energy * (1 - .data$limit) - * pmin(1, (1 - .data$alpha)^(.data$year - 2020)) - ) - + (.data$specific.energy * .data$limit))) %>% - ungroup() %>% - select("scenario", "region", "year", "subsector", "specific.energy") - - ### converge subsector en shares to global value ---- - # calculate global shares, weighted by subsector activity - industry_subsectors_en_shares_global <- industry_subsectors_en_shares %>% - inner_join( - industry_subsectors_ue %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", iso3c = "Region", year = "Year", - pf = "Data2", level = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - extract("pf", "subsector", "^ue_(.*)$") %>% - inner_join(region_mapping_21, "iso3c") %>% - group_by(!!!syms(c("scenario", "region", "year", "subsector"))) %>% - summarise(level = sum(.data$level), .groups = "drop"), - - c("scenario", "region", "year", "subsector") - ) %>% - group_by(!!!syms(c("scenario", "year", "subsector", "pf"))) %>% - summarise( - share.global = sum(.data$share * .data$level) / sum(.data$level), - .groups = "drop_last") %>% - mutate(share.global = .data$share.global / sum(.data$share.global)) %>% - ungroup() - - # converge - industry_subsectors_en_shares <- inner_join( - industry_subsectors_en_shares, - industry_subsectors_en_shares_global, - - c("scenario", "year", "subsector", "pf") - ) %>% - mutate( - # converge from 2020 to 2100 - foo = pmin(1, pmax(0, (.data$year - 2020) / (2100 - 2020))), - share = (1 - .data$foo) * .data$share - # use minimum of regional and global share, so regions doing - # better than the average don't regress - + .data$foo * pmin(.data$share, .data$share.global)) %>% - select(-"foo", -"share.global") %>% - # fill possible gaps in the time steps - interpolate_missing_periods_( - periods = list(year = getYears(remind, TRUE)), - value = "share", - expand.values = TRUE) %>% - group_by(!!!syms(c("scenario", "year", "region", "subsector"))) %>% - mutate(share = .data$share / sum(.data$share)) %>% - ungroup() - - failed_share_sum <- industry_subsectors_en_shares %>% - group_by(!!!syms(c("scenario", "year", "region", "subsector"))) %>% - summarise(failed = abs(sum(.data$share) - 1) > 1e-15, .groups = "drop") - - if (any(failed_share_sum$failed)) { - stop("industry_subsectors_en_shares don't add up to 1.") - } - - industry_subsectors_en <- inner_join( - industry_subsectors_specific_energy %>% - # expand regions to iso3c - full_join(region_mapping_21, "region") %>% - select(-"region"), - - industry_subsectors_ue %>% - as.data.frame() %>% - as_tibble() %>% - select(scenario = "Data1", iso3c = "Region", year = "Year", pf = "Data2", level = "Value") %>% - character.data.frame() %>% - mutate(year = as.integer(as.character(.data$year))) %>% - extract("pf", "subsector", "^ue_(.*)$") %>% - group_by(!!!syms(c("scenario", "iso3c", "year", "subsector"))) %>% - summarise(level = sum(.data$level), .groups = "drop"), - - c("scenario", "iso3c", "year", "subsector") - ) %>% - mutate(value = .data$level * .data$specific.energy) %>% - select("scenario", "iso3c", "year", "subsector", "value") %>% - assert(is.finite, "value") %>% - inner_join( - industry_subsectors_en_shares %>% - full_join(region_mapping_21, "region") %>% - select(-"region"), - - c("scenario", "iso3c", "year", "subsector") - ) %>% - group_by(!!!syms(c("scenario", "iso3c", "year", "subsector"))) %>% - mutate(share = .data$share / sum(.data$share)) %>% - ungroup() %>% - mutate(value = .data$value * .data$share) %>% - select("scenario", region = "iso3c", "year", item = "pf", "value") %>% - verify(expr = is.finite(.data$value), description = "Finite industry_subsectors_en values") %>% - as.magpie(spatial = 2, temporal = 3, datacol = 5) - - - remind <- mbind(remind, industry_subsectors_en, industry_subsectors_ue) - - # ---- _ prepare output ---- - - return(list( - x = remind, - weight = NULL, - unit = paste0( - "EJ, except ue_cement (Gt), ue_primary_steel and ", - "ue_secondary_steel (Gt) and ue_chemicals and ", - "ue_otherInd ($tn)" - ), - description = "demand pathways for final energy demand in industry", - structure.data = "^gdp_(SSP[1-5].*|SDP.*)\\.(fe|ue)" - )) - -} diff --git a/R/calcHistorical.R b/R/calcHistorical.R index 9079409a..a144cadd 100644 --- a/R/calcHistorical.R +++ b/R/calcHistorical.R @@ -1,7 +1,7 @@ #' Gather reference data from various sources. #' @importFrom dplyr filter group_by mutate select ungroup #' @importFrom rlang syms -#' @importFrom tidyr complete nesting +#' @importFrom tidyr complete nesting pivot_wider calcHistorical <- function() { # Final Energy diff --git a/R/calcIO.R b/R/calcIO.R index a2295e89..59ff3840 100644 --- a/R/calcIO.R +++ b/R/calcIO.R @@ -21,7 +21,7 @@ #' a <- calcOutput("IO", subtype = "output") #' } #' -#' @importFrom rlang .data +#' @importFrom rlang .data is_empty #' @importFrom dplyr filter mutate #' @importFrom tidyr unite #' @importFrom tidyselect all_of diff --git a/R/calcIndustry_CCS_limits.R b/R/calcIndustry_CCS_limits.R deleted file mode 100644 index ad82961b..00000000 --- a/R/calcIndustry_CCS_limits.R +++ /dev/null @@ -1,283 +0,0 @@ -#' Calculate Limits on Industry CCS Capacities -#' -#' @md -#' @details -#' The limits on industry CCS capacities are calculated from data of the -#' [Global Status of CCS 2023](zotero://select/items/3_E5GNNPZ8) report (through -#' [`readGlobalCCSinstitute()`]. CCS projects are -#' - filtered for valid (i.e. not "Under Evaluation") data for "Operation date" -#' and "CO~2~ capture capacity" -#' - assigned to REMIND industry subsectors according to `facility_subsector`, -#' which defaults to -#' | Facility Industry | subsector | -#' |:--------------------------------|:----------| -#' | Cement | cement | -#' | Chemical | chemicals | -#' | Hydrogen / Ammonia / Fertiliser | chemicals | -#' | Ethan | chemicals | -#' | Iron and Steel Production | steel | -#' -#' - weighted by lifecycle stage according to `stage_weight`, which defaults to -#' | Lifecycle stage | weight | -#' |:---------------------|-------:| -#' | Operational | 100 % | -#' | In construction | 100 % | -#' | Advanced development | 50 % | -#' | Early development | 20 % | -#' -#' The resulting project capacities constitute the limits on industry subsector -#' CCS capacity for 2025. The limit on CCS capacities for regions (or countries -#' if `region_mapping` is `NULL`) is set to a value of total 2025 subsector CCS -#' capacity, times the regions share in subsector activity (e.g. cement -#' production) of the SSP2EU scenario -#' - in 2030 if the region as some CCS capacity in 2025 in a different industry -#' subsector, or -#' - in 2035 if the region has no industry CCS capacity in 2030 at all. -#' -#' CCS capacities are increased by the annual growth factor `a1` for the ten -#' first years, and by the annual growth factor `a2` afterwards (defaulting to -#' 70 % and 20 %, respectively). -#' -#' @param a1,a2 Annual growth factors of CCS capacity limits, for the first ten -#' years and thereafter, default to `0.7` and `0.2` (70 % and 20 %, -#' respectively). -#' @param installation_minimum Minimum emission capacity (in MtCO~2~/year) -#' capacities are rounded up to. Defaults to `0.5` (500 ktCO~2~/year). -#' @param stage_weight A named vector of weight factors for different lifecycle -#' stages. See Details. -#' @param facility_subsector A named vector mapping the "Facility Industry" of -#' CCS projects to REMIND industry subsectors. See Details. -#' @param region_mapping A data frame with columns `iso3c` and `region` detailing -#' the regional resolution on which data should be extrapolated. If `NULL` -#' (the default), extrapolation is done at the country level. -#' -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, `description`, and `min`. -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr arrange group_by full_join inner_join left_join mutate -#' pull rename select summarise tibble ungroup -#' @importFrom quitte add_countrycode_ madrat_mule magclass_to_tibble -#' @importFrom readr read_delim -#' @importFrom rlang .data -#' @importFrom tidyr complete fill pivot_longer replace_na -#' -#' @export -calcIndustry_CCS_limits <- function( - a1 = 0.3, a2 = 0.15, - installation_minimum = 1, - stage_weight = c('Operational' = 1, - 'In construction' = 1, - 'Advanced development' = 0.5, - 'Early development' = 0.2), - facility_subsector = c('Cement' = 'cement', - 'Chemical' = 'chemicals', - 'Hydrogen / Ammonia / Fertiliser' = 'chemicals', - 'Ethan' = 'chemicals', - 'Iron and Steel Production' = 'steel'), - region_mapping = NULL) { - - # configuration ---- - stage_weight <- tibble(stage = names(stage_weight), factor = stage_weight) - - facility_subsector <- tibble(`Facility Industry` = names(facility_subsector), - subsector = facility_subsector) - - remind_timesteps <- unique(quitte::remind_timesteps$period) - - ## read SSP2EU industry activity ---- - ind_activity <- calcOutput('FEdemand', aggregate = FALSE, - years = remind_timesteps) %>% - `[`(,,paste0('gdp_SSP2EU.', - c('ue_cement', 'ue_chemicals', 'ue_steel_primary'))) %>% - magclass_to_tibble() %>% - mutate(subsector = sub('ue_([^_]+).*', '\\1', .data$item), - .keep = 'unused') %>% - select(iso3c = 'region', 'subsector', period= 'year', activity = 'value') - - ## set/check region mapping ---- - iso3c_list <- read_delim( - file = system.file('extdata', 'iso_country.csv', package = 'madrat'), - delim = ';', - col_names = c('-', 'iso3c'), - col_types = '-c', - skip = 1) %>% - pull('iso3c') %>% - sort() - - if (is.null(region_mapping)) { - region_mapping <- tibble(iso3c = iso3c_list, region = iso3c_list) - } - else if (!setequal(region_mapping[['iso3c']], iso3c_list)) { - stop('region_mapping iso3c column does not match madrat prescribed iso3c list') - } - - # calculation ---- - x <- readSource('GlobalCCSinstitute', '2023-11', convert = FALSE) %>% - madrat_mule() %>% - rename(value = 'Capture, transport and/or storage capacity (Mtpa CO2)', - stage = 'Lifecycle stage') %>% - filter(!is.na(.data$value)) %>% - # filter for facility industry - inner_join(facility_subsector, 'Facility Industry') %>% - add_countrycode_(origin = c('Country' = 'country.name'), - destination = 'iso3c') %>% - left_join(region_mapping, 'iso3c' ) %>% - left_join(stage_weight, 'stage') %>% - # split facilities existing already in 2025 or only in 2030 - mutate(`2025` = .data$`Operational date` <= 2027, - `2030` = .data$`Operational date` <= 2032) %>% - pivot_longer(cols = c('2025', '2030'), names_to = 'period', - names_transform = as.integer, values_to = 'include') %>% - filter(.data$include) %>% - select(-'include') %>% - # regional aggregation and applying stage factors - group_by(.data$period, .data$region, .data$subsector) %>% - summarise(value = sum(.data$value * .data$factor), .groups = 'drop') %>% - complete(crossing(!!!syms(c('region', 'subsector', 'period'))), - fill = list(value = 0)) %>% - full_join( - ind_activity %>% - full_join(region_mapping, 'iso3c') %>% - group_by(.data$region, .data$subsector, .data$period) %>% - summarise(activity = sum(.data$activity), .groups = 'drop'), - - c('region', 'subsector', 'period') - ) %>% - replace_na(list(period = 2025L, value = 0)) %>% - # classes: - A: non-zero 2025 data (is continued) - # - B: zero 2025 data, but non-zero 2025 data in different - # subsector (gets initialised in 2030) - # - C: zero 2025 data in all subsectors (get initialised in 2035) - group_by(.data$region, .data$subsector) %>% - mutate( - class = ifelse(0 != .data$value[2025 == .data$period], 'A', NA)) %>% - group_by(.data$region) %>% - mutate( - class = case_when( - 'A' == .data$class ~ .data$class, - 0 != sum(.data$value[2025 == .data$period]) ~ 'B', - 0 == sum(.data$value[2025 == .data$period]) ~ 'C')) %>% - ungroup() %>% - complete(nesting(!!!syms(c('region', 'subsector', 'class'))), - period = remind_timesteps, - fill = list(value = NA)) %>% - group_by(.data$period, .data$subsector) %>% - mutate(activity.total = sum(.data$activity, na.rm = TRUE), - value.total = case_when( - .data$period <= 2030 ~ sum(.data$value, na.rm = TRUE))) %>% - group_by(.data$subsector) %>% - fill('value.total', .direction = 'down') %>% - arrange(.data$region, .data$subsector, .data$period) %>% - # conversion factors for activity to emissions for cement and steel - full_join( - tribble( - ~subsector, ~factor, # ad-hoc emission factors - 'cement', 0.78, # Gt cement/a -> GtCO2/a - 'chemicals', NA, - 'steel', 1.85), # Gt steel/a -> GtCO2/a - - 'subsector') %>% - group_by(.data$region, .data$subsector) %>% - mutate( - value = case_when( - 2045 < .data$period ~ NA, - TRUE ~ .data$value), - - value = case_when( - ## before 2025 ---- - .data$period < 2025 ~ 0, - - # 2025 just keeps data present - - ## 2030 ---- - - 'A' == .data$class & 2030 == .data$period ~ - # either the 2030 value, or the expanded 2025 value, whichever is - # higher - max(sum(.data$value, na.rm = TRUE), - .data$value[2025 == .data$period] * (1 + a1) ^ 5), - - 'B' == .data$class & 2030 == .data$period ~ - # global 2025 subsector CCS times regional share in global subsector - # activity, rounded up to MtCO2/a, but not - # exceeding regional subsector emissions - (.data$value.total * .data$activity / .data$activity.total) %>% - max(installation_minimum) %>% - min(.data$activity * 1e3 * .data$factor, na.rm = TRUE), - - 'C' == .data$class & 2030 == .data$period ~ 0, - - TRUE ~ .data$value), - - ## 2035 ---- - value = case_when( - .data$class %in% c('A', 'B') & 2035 == .data$period ~ - # expanded 2030 value - .data$value[2030 == .data$period] * (1 + a1) ^ 5, - - 'C' == .data$class & 2035 == .data$period ~ - # global 2025 subsector CCS times regional share in global subsector - # activity, rounded up to MtCO2/a, but not - # exceeding regional subsector emissions - (.data$value.total * .data$activity / .data$activity.total) %>% - max(installation_minimum) %>% - min(.data$activity * 1e3 * .data$factor, na.rm = TRUE), - - TRUE ~ .data$value), - - ## 2040 ---- - value = case_when( - 'A' == .data$class & 2040 == .data$period ~ - # expand 2035 value, using a2 - .data$value[2035 == .data$period] * (1 + a2) ^ 5, - - .data$class %in% c('B', 'C') & 2040 == .data$period ~ - # expand 2035 value, using a1 - .data$value[2035 == .data$period] * (1 + a1) ^ 5, - - TRUE ~ .data$value), - - ## 2045 ---- - value = case_when( - .data$class %in% c('A', 'B') & 2045 == .data$period ~ - # expand 2040 value, using a2 - .data$value[2040 == .data$period] * (1 + a2) ^ 5, - - 'C' == .data$class & 2045 == .data$period ~ - # expand 2040 value, using a1 - .data$value[2040 == .data$period] * (1 + a1) ^ 5, - - TRUE ~ .data$value), - - ## after 2045 ---- - value = ifelse(!is.na(.data$value), .data$value, - ( last(.data$value[!is.na(.data$value)]) - * (1 + a2) - ^ (.data$period - last(.data$period[!is.na(.data$value)])) - ))) %>% - ungroup() %>% - select('region', 'subsector', 'period', 'value') %>% - # expand from regions to iso3c (if different), convert unit - full_join( - full_join(ind_activity, region_mapping, 'iso3c'), - - by = c('region', 'subsector', 'period'), - relationship = 'many-to-many' - ) %>% - group_by(.data$region, .data$subsector, .data$period) %>% - ## convert units ---- - # Mt CO2/yr * 1e-3 Gt/Mt / (44/12 CO2/C) = GtC/yr - mutate(value = .data$value - * .data$activity / sum(.data$activity) - * 12/44 * 1e-3) %>% - ungroup() %>% - select('iso3c', 'period', 'subsector', 'value') - - return(list(x = as.magpie(x = x, spatial = 1, temporal = 2, datacol = 4), - weight = NULL, - unit = 'GtC/yr', - description = 'Limits on Industry CCS Capacities', - min = 0)) -} diff --git a/R/calcIndustry_EEK.R b/R/calcIndustry_EEK.R deleted file mode 100644 index 3a5f53a9..00000000 --- a/R/calcIndustry_EEK.R +++ /dev/null @@ -1,367 +0,0 @@ -#' Industry Energy Efficiency Capital -#' -#' @param kap General internal capital stock, as calculated internally by -#' `calcCapital()`. -#' -#' @return A list with a [`magpie`][magclass::magclass] object `x`, `weight`, -#' `unit`, and `description` fields. -#' -#' @importFrom assertr assert -#' @importFrom dplyr arrange bind_rows filter group_by lag lead mutate n -#' row_number select -#' @importFrom quitte madrat_mule -#' @importFrom rlang .data .env sym syms -#' @importFrom tidyr nest pivot_longer unnest - -#' @export -calcIndustry_EEK <- function(kap) { - # setup ---- - i <- log(4) / 50 # assuming 50 year lifetime of EEK - base_year <- 2015 - - . <- NULL - - # read data ---- - ## subsector activity projections ---- - industry_VA <- calcOutput( - type = 'Industry_Value_Added', - subtype = 'economic', - match.steel.historic.values = TRUE, - match.steel.estimates = 'IEA_ETP', - China_Production = readSource(type = 'ExpertGuess', - subtype = 'Chinese_Steel_Production', - convert = FALSE) %>% - madrat_mule(), - aggregate = FALSE, years = base_year, supplementary = FALSE, warnNA = FALSE) %>% - `[`(,,'gdp_SSP2EU') %>% - quitte::magclass_to_tibble() %>% - select('iso3c', subsector = 'name', VA = 'value') %>% - mutate(subsector = sub('_VA$', '', .data$subsector)) - - ## investment volumes into energy efficiency ---- - IEA_WEIO_2014 <- readSource('IEA_WEIO_2014', convert = FALSE) %>% - madrat_mule() - - ## industry subsector activity and FE projections ---- - FEdemand <- calcOutput(type = 'FEdemand', aggregate = FALSE, supplementary = FALSE) - - # calculate EEK ---- - ## split industry VA into IEA investment sectors ---- - # Cement, Chemicals, and Steel are 'energy intensive', Other Industry is - # 'non-energy intensive' - industry_VA <- industry_VA %>% - full_join( - IEA_WEIO_2014 %>% - getElement('country_groups'), - - 'iso3c' - ) %>% - assert(not_na, everything()) %>% - mutate(name = ifelse('otherInd' == .data$subsector, - 'Non-energy intensive', - 'Energy intensive')) %>% - # calculate country/sector share in IEA region VA - group_by(!!!syms(c('IEA region', 'name'))) %>% - mutate(VA.share = .data$VA / sum(.data$VA)) %>% - ungroup() - - ## calculate EEK from investments ---- - EEK <- IEA_WEIO_2014 %>% - getElement('data') %>% - # combine investment estimates with subsector VA figures - inner_join( - industry_VA, - - c('IEA region', 'name') - ) %>% - # assuming a "steady state", where investments only replace existing EEK - # stock - mutate(EEK = .data$VA.share * .data$value / .env$i) %>% - select('iso3c', 'subsector', 'EEK') - - ## split steel EEK ---- - EEK <- EEK %>% - # split steel EEK based on primary/secondary steel FE shares (higher FE - # shares should lead to higher EEK shares) - left_join( - FEdemand %>% - `[`(,base_year,'gdp_SSP2EU.fe', pmatch = 'left') %>% - `[`(,,'steel', pmatch = TRUE) %>% - quitte::magclass_to_tibble() %>% - select(iso3c = 'region', 'item', FE = 'value') %>% - # everything not 'steel_secondary' is 'steel_primary' - mutate(item = sub('steel$', 'steel_primary', .data$item)) %>% - extract('item', 'foo', '^fe.*_(steel_.*)$') %>% - # calculate primary/secondary steel FE shares - group_by(!!!syms(c('iso3c', 'foo'))) %>% - summarise(FE = sum(.data$FE), .groups = 'drop_last') %>% - mutate(FE.share = tidyr::replace_na(.data$FE / sum(.data$FE), 0), subsector = 'steel') %>% - ungroup() %>% - select(-'FE'), - - c('iso3c', 'subsector') - ) %>% - mutate(subsector = ifelse(is.na(.data$foo), .data$subsector, .data$foo), - EEK = ifelse(is.na(.data$foo), .data$EEK, - .data$EEK * .data$FE.share)) %>% - select('iso3c', 'subsector', 'EEK') - - ## Converting from billion 2012 to trillion 2017 dollars - EEK <- EEK %>% - dplyr::rename("value" = "EEK") %>% - GDPuc::toolConvertGDP(unit_in = "constant 2012 US$MER", - unit_out = mrdrivers::toolGetUnitDollar(), - replace_NAs = "with_USA") %>% - dplyr::mutate(value = .data$value * 1e-3) %>% - dplyr::rename("EEK" = "value") - - ## temper EEK share in total capital ---- - # Temper industry EEK share in total capital by applying a geometric average - # between the regional shares and the global share. - temper <- full_join( - EEK %>% - group_by(.data$iso3c) %>% - summarise(EEK = sum(.data$EEK), .groups = 'drop') %>% - sum_total_(group = 'iso3c', value = 'EEK', name = 'World'), - - kap %>% - sum_total_(group = 'iso3c', value = 'kap', name = 'World'), - - 'iso3c' - ) %>% - assert(not_na, everything()) %>% - mutate( - kap_ind_share = .data$EEK / .data$kap, - temper = .data$kap_ind_share['World' == .data$iso3c], - kap_ind_share_tempered = ifelse( - 'World' == .data$iso3c, FALSE, - (.data$kap_ind_share * .data$temper ^ 2) ^ (1 / 3)), - kap_ind_tempered = .data$kap_ind_share_tempered * .data$kap, - kap_ind_tempered = ifelse('World' == .data$iso3c, - sum(.data$kap_ind_tempered), - .data$kap_ind_tempered), - temper = .data$kap_ind_tempered / .data$EEK) %>% - filter('World' != .data$iso3c) - - EEK <- full_join( - EEK, - - temper %>% - select('iso3c', 'temper'), - - 'iso3c' - ) %>% - mutate(EEK = .data$EEK * .data$temper) %>% - select(-'temper') - - ## calculate EEK growth rates ---- - # EEK is assumed to stay constant in relation to subsector output, so it - # grows/shrinks as the output grows/shrinks. Shrinking of EEK is limited by - # the depreciation rate i. - EEK_change <- FEdemand %>% - # select relevant subsector outputs, transform into usable format - `[`(,,'ue_', pmatch = 'left') %>% - quitte::magclass_to_tibble(c('iso3c', 'year', 'scenario', 'subsector', 'value')) %>% - filter(.data$subsector %in% c('ue_cement', 'ue_chemicals', - 'ue_steel_primary', 'ue_steel_secondary', - 'ue_otherInd')) %>% - # calculate baseline change rates relative to base_year - group_by(!!!syms(c('iso3c', 'scenario', 'subsector'))) %>% - mutate( - # Replace zeros with the first non-zero data. This keeps EEK constant - # back in time for countries/subsectors that have no historic production. - value = ifelse(0 != .data$value, - .data$value, - first(.data$value[0 != .data$value], - order_by = .data$year)), - # change in production relative to base year - change = .data$value / .data$value[base_year == .data$year], - # temper change down to avoid unduly high EEK in developing regions, - # especially SSA - change = sqrt(.data$change) - ) - - # find all countries/scenarios/subsectors where capital depreciation rate is - # exceeded - EEK_change_invalid_forward <- EEK_change %>% - filter(base_year <= .data$year) %>% - summarise( - valid = all(.data$change >= ( lag(.data$change, order_by = .data$year) - * (1 - i) - ^ (.data$year - lag(.data$year, - order_by = .data$year)) - ), - na.rm = TRUE), - .groups = 'drop') %>% - filter(!.data$valid) %>% - select(-'valid') - - EEK_change_invalid_backward <- EEK_change %>% - filter(base_year >= .data$year) %>% - summarise( - valid = all(.data$change <= ( lag(.data$change, - order_by = desc(.data$year)) - * (1 - i) - ^ (.data$year - lag(.data$year, - order_by = desc(.data$year)) - ) - ), - na.rm = TRUE), - .groups = 'drop') %>% - filter(!.data$valid) %>% - select(-'valid') - - # recalculate capital change rates - EEK_change_valid_forward <- EEK_change %>% - ungroup() %>% - filter(base_year <= .data$year) %>% - semi_join( - EEK_change_invalid_forward, - - c('iso3c', 'scenario', 'subsector') - ) %>% - # duplicate year, as the variable gets lost during nesting - mutate(year2 = .data$year) %>% - group_by(.data$year2) %>% - nest() %>% - pull(.data$data) %>% - # Sequentially operate on two one-row data frames, x being the result of - # the previous operation, or starting at the base year row, y the - # 'current' row. So y holds the baseline change rate computed earlier - # (below), while x holds the change rate conforming to the capital - # depreciation limit, as it has already been processed. reduce() returns - # only the last row of the computation, so we get one output row for each - # of the input rows. - purrr::reduce( - .f = function(x, y) { - bind_rows(x, y) %>% - group_by(.data$iso3c, .data$scenario, .data$subsector) %>% - # re-calculate change rate - mutate( - change = ifelse( - # first row has already been processed - 1 == row_number(), - .data$change, - # capital after the base year can decrease by no more than the - # capital depreciation rate i - pmax(.data$change, - ( lag(.data$change, order_by = .data$year) - * (1 - i) - ^ (.data$year - lag(.data$year, order_by = .data$year)) - )))) %>% - ungroup() %>% - select(-'value') - }) - - # do the same for backwards data - EEK_change_valid_backward <- EEK_change %>% - ungroup() %>% - filter(base_year >= .data$year) %>% - semi_join( - EEK_change_invalid_backward, - - c('iso3c', 'scenario', 'subsector') - ) %>% - mutate(year2 = .data$year) %>% - group_by(.data$year2) %>% - nest() %>% - pull(.data$data) %>% - purrr::reduce( - .f = function(x, y) { - bind_rows(x, y) %>% - group_by(.data$iso3c, .data$scenario, .data$subsector) %>% - # re-calculate change rate - mutate( - change = ifelse( - # first row has already been processed (but is the last row as we - # work backwards) - n() == row_number(), - .data$change, - # capital before the base year can only have been higher by - # the inverse of the depreciation rate i - pmin(.data$change, - ( lead(.data$change, order_by = .data$year) - * (1 - i) - ^ (.data$year - lead(.data$year, order_by = .data$year)) - )))) %>% - ungroup() %>% - select(-'value') - }, - .dir = 'backward') - - # combine all valid change data - EEK_change <- bind_rows( - # valid data forward - EEK_change %>% - filter(base_year <= .data$year) %>% - anti_join( - EEK_change_invalid_forward, - - c('iso3c', 'scenario', 'subsector') - ), - - # valid data backward - EEK_change %>% - filter(base_year > .data$year) %>% - anti_join( - EEK_change_invalid_backward, - - c('iso3c', 'scenario', 'subsector') - ), - - # fixed rates - EEK_change_valid_forward, - - EEK_change_valid_backward - ) %>% - distinct(.data$iso3c, .data$scenario, .data$subsector, .data$year, - .data$change) - - EEK <- full_join( - EEK %>% - mutate(subsector = paste0('ue_', .data$subsector)), - - EEK_change, - - c('iso3c', 'subsector') - ) %>% - assert(not_na, everything()) %>% - mutate(value = .data$EEK * .data$change, - subsector = sub('^ue_', 'kap_', .data$subsector)) %>% - select('iso3c', 'year', 'scenario', 'subsector', 'value') - - # quick-fix to infeasible 2025 SSA kap_steel_primary ---- - SSA_iso3c <- toolGetMapping('regionmappingH12.csv', 'regional', where = "mappingfolder") %>% - as_tibble() %>% - filter('SSA' == .data$RegionCode) %>% - pull('CountryCode') - - EEK <- bind_rows( - EEK %>% - anti_join( - tibble(iso3c = SSA_iso3c, - year = 2025, - scenario = 'gdp_SSP5', - subsector = 'kap_steel_primary'), - - c('iso3c', 'year', 'scenario', 'subsector') - ), - - EEK %>% - semi_join( - tibble(tidyr::crossing(iso3c = SSA_iso3c, year = c(2020, 2025, 2030)), - scenario = 'gdp_SSP5', - subsector = 'kap_steel_primary'), - by = c('iso3c', 'year', 'scenario', 'subsector') - ) %>% - group_by(.data$iso3c, .data$scenario, .data$subsector) %>% - summarise(value = mean(.data$value), year = 2025L, .groups = 'drop') - ) - - # return ---- - return(list(x = EEK %>% - as.magpie(spatial = 1, temporal = 2, data = ncol(.)), - weight = NULL, - unit = 'trillion US$2017', - description = 'Industry energy efficiency capital stock')) -} diff --git a/R/calcShareIndFE.R b/R/calcShareIndFE.R deleted file mode 100644 index 0e07fc07..00000000 --- a/R/calcShareIndFE.R +++ /dev/null @@ -1,143 +0,0 @@ -#' Share of Industry Subsectors in FE Use -#' -#' Calculates industry subsector shares in final energy carrier use for the -#' `fixed_shares` realisation of the `industry` module. -#' -#' For the region mapping `regionmapping_21_EU11.csv`, these are based on IEA -#' data from `calcOutput(type = 'FEdemand')`, for all other -#' region mappings on vintage data which is ultimately based on Enerdata data. -#' -#' @note There is a discrepancy between the shares calculated from these two -#' sources, that will affect REMIND emission reporting. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Lavinia Baumstark -#' @author Michaja Pehl -#' -#' @seealso [`calcOutput()`][madrat::calcOutput]. -#' @md -#' -#' @importFrom dplyr as_tibble select filter inner_join tribble group_by -#' summarise mutate ungroup -#' @importFrom quitte character.data.frame -#' @importFrom tidyr pivot_wider pivot_longer complete -#' -#' @export -calcShareIndFE <- function() { - if ('regionmapping_21_EU11.csv' != getConfig('regionmapping')) { - x <- readSource("REMIND_11Regi",subtype="shareIndFE") - w <- calcOutput("IO",subtype="output",aggregate=FALSE)[,2010,] - } else { - x <- calcOutput(type = 'FEdemand', aggregate = FALSE) %>% - as.data.frame() %>% - as_tibble() %>% - select('iso3c' = 'Region', 'year' = 'Year', 'scenario' = 'Data1', - 'pf' = 'Data2', 'value' = 'Value') %>% - character.data.frame() %>% - filter('2005' == !!sym('year'), - 'gdp_SSP2' == !!sym('scenario')) %>% - select(-'year', -'scenario') %>% - inner_join( - tribble( - ~pf.fixed_shares, ~sector.fixed_shares, ~pf.subsectors, - 'fesoi', 'cement', 'feso_cement', - 'fesoi', 'chemicals', 'feso_chemicals', - 'fesoi', 'steel', 'feso_steel', - 'fesoi', 'otherInd', 'feso_otherInd', - 'fehoi', 'cement', 'feli_cement', - 'fehoi', 'chemicals', 'feli_chemicals', - 'fehoi', 'steel', 'feli_steel', - 'fehoi', 'otherInd', 'feli_otherInd', - 'fegai', 'cement', 'fega_cement', - 'fegai', 'chemicals', 'fega_chemicals', - 'fegai', 'steel', 'fega_steel', - 'fegai', 'otherInd', 'fega_otherInd', - 'fehei', 'cement', NA, - 'fehei', 'chemicals', NA, - 'fehei', 'steel', NA, - 'fehei', 'otherInd', 'fehe_otherInd', - 'feeli', 'cement', 'feel_cement', - 'feeli', 'chemicals', 'feelhth_chemicals', - 'feeli', 'chemicals', 'feelwlth_chemicals', - 'feeli', 'steel', 'feel_steel_primary', - 'feeli', 'steel', 'feel_steel_secondary', - 'feeli', 'otherInd', 'feelhth_otherInd', - 'feeli', 'otherInd', 'feelwlth_otherInd'), - c('pf' = 'pf.subsectors') - ) %>% - select(-'pf', !!sym('pf') := 'pf.fixed_shares') %>% - group_by(!!sym('iso3c'), !!sym('pf'), !!sym('sector.fixed_shares')) %>% - summarise(!!sym('value') := sum(!!sym('value'), na.rm = TRUE), - .groups = 'drop_last') %>% - mutate(!!sym('value') := !!sym('value') / sum(!!sym('value'), - na.rm = TRUE)) %>% - ungroup() %>% - filter('otherInd' != !!sym('sector.fixed_shares')) %>% - pivot_wider(names_from = 'pf', values_fill = 0) %>% - mutate(!!sym('feh2i') := !!sym('fegai')) %>% - pivot_longer(matches('^fe..i$'), names_to = 'pf', ) %>% - complete( - !!sym('iso3c'), - !!sym('sector.fixed_shares') := c('cement', 'chemicals', 'steel'), - !!sym('pf') := c('fesoi', 'fehoi', 'fegai', 'feh2i', 'fehei', 'feeli'), - fill = list(value = 0)) %>% - select('region' = 'iso3c', 'type' = 'pf', - 'variable' = 'sector.fixed_shares', 'value') %>% - as.magpie() - - w <- calcOutput('IO', subtype = 'output', aggregate = FALSE)[,2015,] - } - - w <- w[,,intersect(getNames(x, dim = 1),getNames(w, dim=2))] - w <- dimSums(w,dim=c(3.1,3.3)) - # duplicate fegai as a weight for feh2i - w <- mbind(w, setNames(w[,,'fegai'], 'feh2i')) - - return(list(x = x, - weight = w, - unit = "ratio", - description = "share of industry sub-sectors in FE use")) -} - -# Extension notes MJP 2019-07-25 -# # Add shares for H2, heat, and electricity, since they are needed for the new -# # emissions accounting by Renato. -# bind_rows( -# # Read electricity shares from 2010 IEA data -# calcOutput('IO', subtype = 'output_Industry_subsectors') %>% -# as.data.frame() %>% -# as_tibble() %>% -# filter(2010 == Year, -# grepl('^feel[^_]*_.*', Data2)) %>% -# select(region = Region, pf = Data2, value = Value) %>% -# character.data.frame() %>% -# separate(pf, c('type', 'sector'), sep = '_') %>% -# mutate(type = sub('feel.*$', 'feeli', type)) %>% -# group_by(region, sector, type) %>% -# summarise(value = sum(value)) %>% -# group_by(region, type) %>% -# mutate(share = value / sum(value)) %>% -# ungroup() %>% -# arrange(region, type) %>% -# select(-value) %>% -# filter('otherInd' != sector) %>% -# # put 100 % of heat into otherInd -# complete(nesting(region, sector), type = c('feeli', 'fehei')) %>% -# mutate(share = round(ifelse('fehei' == type, 0, share), 2)) %>% -# spread(sector, share), -# -# # use gas shares for H2 -# read_delim( -# file = '~/PIK/swap/inputdata/sources/REMIND_11Regi/shareIndustyFE.csv', -# delim = ';', -# col_types = 'ccddd', -# skip = 3) %>% -# filter('fegai' == type) %>% -# mutate(type = 'feh2i') -# ) %>% -# write_delim( -# path = '~/PIK/swap/inputdata/sources/REMIND_11Regi/shareIndustyFE.csv', -# delim = ';', -# append = TRUE, -# col_names = FALSE) diff --git a/R/calcSteelStock.R b/R/calcSteelStock.R deleted file mode 100644 index 5a8d4fa1..00000000 --- a/R/calcSteelStock.R +++ /dev/null @@ -1,26 +0,0 @@ -#' Calculate Steel Stock from Mueller steel stock per capita and WDI population -#' -#' @md -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Falk Benke -#' -#' @importFrom dplyr filter select mutate left_join -#' @export - -calcSteelStock <- function() { - steel_stock_per_capita <- readSource(type = "Mueller", subtype = "stocks", convert = TRUE)[, , "Steel stock per-capita|med (t)"] - population <- calcOutput("PopulationPast", PopulationPast = 'UN_PopDiv', aggregate = FALSE) - - y <- intersect(getItems(steel_stock_per_capita, 2), getItems(population, 2)) - - stock <- steel_stock_per_capita[, y, ] * population[, y, ] - getNames(stock) <- c("Steel stock (million t)") - - return(list( - x = stock, - weight = NULL, - unit = "million t", - description = "Historical steel stock based on Mueller's medium stock per capita numbers and WDI population" - )) -} diff --git a/R/calcindustry_max_secondary_steel_share.R b/R/calcindustry_max_secondary_steel_share.R deleted file mode 100644 index 15c17cd5..00000000 --- a/R/calcindustry_max_secondary_steel_share.R +++ /dev/null @@ -1,35 +0,0 @@ -#' Calculate Maximum Secondary Steel Production Share -#' -#' Reads ExpertGuess/industry_max_secondary_steel_share and expands to all -#' `scenarios`/`regions` using default data. See [`tool_expand_tibble()`] for -#' details. -#' -#' @param scenarios A character vector of scenarios to expand data to. -#' @param regions A character vector of regions to expand data to. -#' -#' @return A list with a [`magpie`][magclass::magclass] object `x`. - -#' @export -calcindustry_max_secondary_steel_share <- function(scenarios = NULL, - regions = NULL) { - if (is.null(scenarios)) { - stop('Scenario definitions missing.') - } - - if (is.null(regions)) { - stop('Region definitions missing.') - } - - . <- NULL - - return(list( - x = readSource(type = 'ExpertGuess', - subtype = 'industry_max_secondary_steel_share', - convert = FALSE) %>% - madrat_mule() %>% - tool_expand_tibble(scenarios, regions) %>% - pivot_longer( - !all_of(names(which('character' == unlist(lapply(., typeof)))))) %>% - as.magpie(spatial = 0, temporal = 0, data = ncol(.)), - weight = NULL, unit = '', description = '')) -} diff --git a/R/calcindustry_specific_FE_limits.R b/R/calcindustry_specific_FE_limits.R deleted file mode 100644 index 9227fa04..00000000 --- a/R/calcindustry_specific_FE_limits.R +++ /dev/null @@ -1,28 +0,0 @@ -#' Thermodynamic Limits for Industry Specific FE Demand -#' -#' Return `readindustry_subsectors_specific('industry_specific_FE_limits')` in a -#' format usable as a REMIND input. -#' -#' @md -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr filter mutate select -#' @importFrom quitte madrat_mule - -#' @export -calcindustry_specific_FE_limits <- function() { - return(list(x = readSource(type = 'industry_subsectors_specific', - subtype = 'industry_specific_FE_limits', - convert = FALSE) %>% - madrat_mule() %>% - filter('absolute' == .data$type) %>% - select(-'type') %>% - mutate(subsector = paste0('ue_', .data$subsector)) %>% - as.magpie(spatial = 0, temporal = 0, data = 2), - weight = NULL, - unit = 'GJ/t', - description = paste('Thermodynamic limits for industry subsector', - 'specific FE demand'))) -} diff --git a/R/calcnonEnergyIndFE.R b/R/calcnonEnergyIndFE.R deleted file mode 100644 index 4a4d2e9f..00000000 --- a/R/calcnonEnergyIndFE.R +++ /dev/null @@ -1,18 +0,0 @@ -#' Final energy demand for feedstocks (non-energy use) -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Renato Rodrigues -#' -#' @seealso [`calcOutput()`][madrat::calcOutput]. -#' @md -#' -calcnonEnergyIndFE <- function() { - - x <- readSource("nonEnergyDemand") - - return(list(x = x, - weight = NULL, - unit = "EJ", - description = "Final energy demand for feedstocks (non-energy use)")) -} diff --git a/R/convertADVANCE_WP2.R b/R/convertADVANCE_WP2.R deleted file mode 100644 index 92a96978..00000000 --- a/R/convertADVANCE_WP2.R +++ /dev/null @@ -1,55 +0,0 @@ -#' Convert ADVANCE WP2 Data -#' -#' @md -#' @param x A [`magpie`][magclass::magclass] object returned by -#' [`readADVANCE_WP2()`]. -#' @param subtype One of -#' - `clinker-to-cement-ratio` for the clinker-to-cement ratios from figure 21 -#' of Edelenbosch, O. _Enhancing the representation of energy demand -#' developments in IAM models - A Modeling Guide for the Cement Industry_ -#' (2015) [zotero://select/items/JP8X2QFK](zotero://select/items/JP8X2QFK), -#' which is extended from H12 regions to country level. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @seealso [`readSource()`], [`readADVANCE_WP2()`] -#' -#' @importFrom assertr assert -#' @importFrom dplyr full_join select -#' @importFrom rlang sym -#' @importFrom tibble as_tibble -#' @importFrom tidyselect everything -#' -#' @export -convertADVANCE_WP2 <- function(x, subtype) { - # ---- list all available subtypes with functions doing all the work ---- - switchboard <- list( - 'clinker-to-cement-ratio' = function(x) { - x %>% - as.data.frame() %>% - as_tibble() %>% - select(region = 'Region', ratio = 'Value') %>% - full_join( - toolGetMapping(name = 'regionmappingH12.csv', type = 'regional', where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = 'CountryCode', region= 'RegionCode'), - - 'region' - ) %>% - assert(not_na, everything()) %>% - select(-'region') %>% - as.magpie() - } - ) - - # ---- check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } else { - # ---- load data and do whatever ---- - return(switchboard[[subtype]](x)) - } -} diff --git a/R/convertMueller.R b/R/convertMueller.R deleted file mode 100644 index de02b905..00000000 --- a/R/convertMueller.R +++ /dev/null @@ -1,33 +0,0 @@ -#' Convert Mueller data -#' -#' @md -#' @param x A [`magpie`][magclass::magclass] object returned from -#' [`readMueller()`]. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' @param subtype One of: -#' - `countries`: read table mapping country names use by Müller et al. 2013 -#' to ISO 3166-1 alpha-3 codes. -#' - `stocks`: read low/medium/high estimates of per-capita steel stocks from -#' Müller et al. 2013 SI2 -#' -#' @author Falk Benke -#' -#' @importFrom dplyr mutate select -#' @importFrom quitte madrat_mule -#' -#' -#' @export -convertMueller <- function(x, subtype) { - if (subtype == "stocks") { - x %>% - madrat_mule() %>% - mutate("variable" = paste0("Steel stock per-capita|", !!sym("estimate"), " (t)")) %>% - select("region" = "iso3c", "period" = "year", "variable", "value" = "steel.stock.per.capita") %>% - as.magpie() %>% - toolCountryFill(fill = 0, verbosity = 2, no_remove_warning = c("ANT")) %>% - return() - } else { - return(x) - } -} diff --git a/R/convertStegmann2022.R b/R/convertStegmann2022.R deleted file mode 100644 index 99da3922..00000000 --- a/R/convertStegmann2022.R +++ /dev/null @@ -1,19 +0,0 @@ -#' @title convertStegmann2022 -#' @description Converts data from Stegmann2022 -#' @param x unconverted magpie object from read-script -#' -#' @return magpie object with a completed dataset. -#' - -convertStegmann2022 <- function(x) { - x <- x[c("World"), , , invert = TRUE] - - regmapping <- toolGetMapping("regionmapping_IMAGE_PBL_Stegmann2022.csv", where = "mrremind", type = "regional") - - fe <- calcOutput("FE", source = "IEA", aggregate = FALSE)[unique(regmapping$CountryCode), 2016, "FE (EJ/yr)"] - - out <- toolAggregate(x, regmapping, from = "RegionAbbreviation", to = "CountryCode", weight = fe) - out <- toolCountryFill(out, fill = 0, verbosity = 2) - - return(out) -} diff --git a/R/industry_subsectors_specific.R b/R/industry_subsectors_specific.R deleted file mode 100644 index f1e70156..00000000 --- a/R/industry_subsectors_specific.R +++ /dev/null @@ -1,156 +0,0 @@ -#' industry/subsector change factors -#' -#' Change factors of specific FE and material demand for the -#' `industry/subsector` realisation of REMIND. -#' -#' @md -#' @param subtype One of -#' - `FE` for specific final energy demand change factors -#' - `material_alpha` for alpha factors and convergence time of specific -#' material demand decreases relative to the `SSP2EU` scenario -#' - `material_relative` for scaling factors of specific material demand -#' relative to baseline scenarios -#' - `material_relative_change` for scaling factors of specific material -#' demand _change_ relative to baseline scenarios -#' -#' @param scenarios A vector of scenarios for which factors are to be returned. -#' @param regions A vector of regions for which factors are to be returned. -#' @param direct A data frame as returned by -#' `readindustry_subsectors_specific()` to load debugging/developing data -#' directly instead of from file. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @details -#' Factors are read from the files `specific_FE.csv`, -#' `specific_material_alpha.csv`, `specific_material_relative.csv`, and -#' `specific_material_relative_change.csv`, respectively. `NA` is used to mark -#' defaults for the `scenario` and `region` columns, and specified values will -#' overwrite these defaults. -#' -#' So -#' - `NA,NA,cement,1` will be extended to all `scenarios` and `regions` -#' - `scen1,NA,cement,2` will overwrite this default for all `regions` in -#' `scen1` -#' - `NA,regi1,cement,3` will overwrite this again for all `scenarios` -#' (including `scen1`) for `regi1` -#' - `scen1,regi1,cement,4` will lastly overwrite the value for the `scen1`, -#' `regi1` combination -#' -#' Replacements occure in this fixed order (`NA`/`NA`, `scenario`/`NA`, -#' `NA`/`region`, `scenario`/`region`). -#' -#' Lastly, output is filtered for `scenarios` and `regions`. -#' -#' For debugging and development, instead of modifying the .csv files in -#' `sources/industry_subsectors_specific/` and interfering with production runs, -#' modify the calling code (e.g. `calcFEdemand.R`) to use `direct` data (entered -#' verbatim or loaded from somewhere else.) -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr anti_join bind_rows filter select -#' @importFrom quitte madrat_mule -#' @importFrom readr read_csv -#' @importFrom tidyr complete nesting - -#' @export -#' @rdname industry_subsector_specific -readindustry_subsectors_specific <- function(subtype = NULL) { - # file path (for easier debugging) - path <- './' - # path <- '~/PIK/swap/inputdata/sources/industry_subsectors_specific/' - - # subtype switchboard ---- - switchboard <- list( - 'FE' = function() { - read_csv(file = file.path(path, 'specific_FE.SSP3.csv'), - col_types = 'cccd', - comment = '#', - progress = FALSE) %>% - madrat_mule() - }, - - 'material_alpha' = function() { - read_csv(file = file.path(path, 'specific_material_alpha.csv'), - col_types = 'cccdi', - comment = '#', - progress = FALSE) %>% - madrat_mule() - }, - - 'material_relative' = function() { - read_csv(file = file.path(path, 'specific_material_relative.csv'), - col_types = 'ccccd', - comment = '#', - progress = FALSE) %>% - madrat_mule() - }, - - 'material_relative_change' = function() { - read_csv(file = file.path(path, 'specific_material_relative_change.csv'), - col_types = 'ccccd', - comment = '#', - progress = FALSE) %>% - madrat_mule() - }, - - 'industry_specific_FE_limits' = function() { - read_csv(file = file.path(path, 'industry_specific_FE_limits.csv'), - comment = '#', - show_col_types = FALSE) %>% - madrat_mule() - } - ) - - # check if the subtype called is available ---- - if (!subtype %in% names(switchboard)) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } - - # load data and to whatever ---- - return(switchboard[[subtype]]()) -} - -#' @export -#' @rdname industry_subsector_specific -calcindustry_subsectors_specific <- function(subtype = NULL, scenarios = NULL, - regions = NULL, direct = NULL) { - if (is.null(scenarios)) { - stop('Scenario definitions missing.') - } - - if (is.null(regions)) { - stop('Region definitions missing.') - } - - . <- NULL - - if (is.null(direct)) { - x <- readSource(type = 'industry_subsectors_specific', subtype = subtype, - convert = FALSE) %>% - madrat_mule() - } else { - if (!is.data.frame(direct)) { - stop('`direct` is not a data frame') - } - if (!all(c('scenario', 'region', 'subsector') %in% colnames(direct))) { - stop('`direct` is missing columns: ', - paste(setdiff(c('scenario', 'region', 'subsector'), - colnames(direct)), - collapse = ', ')) - } - x <- direct - } - - return(list( - x = x %>% - tool_expand_tibble(scenarios, regions, - structure.columns = 'subsector') %>% - pivot_longer( - !all_of(names(which('character' == unlist(lapply(., typeof))))) - ) %>% - as.magpie(spatial = 0, temporal = 0, data = ncol(.)), - weight = NULL, unit = '', description = '')) -} diff --git a/R/madrat.R b/R/madrat.R index 0e95ef17..563486ef 100644 --- a/R/madrat.R +++ b/R/madrat.R @@ -1,5 +1,5 @@ .onLoad <- function(libname, pkgname) { - madrat::madratAttach(c(pkgname, "edgeTransport", "GDPuc", "mrcommons", "mrdrivers")) + madrat::madratAttach(c(pkgname, "edgeTransport", "GDPuc", "mrcommons", "mrdrivers", "mrindustry")) madrat::setConfig( nolabels = c("REMIND", "VALIDATIONREMIND"), .cfgchecks = FALSE, .verbose = FALSE @@ -8,7 +8,7 @@ .onUnload <- function(libpath) { - madrat::madratDetach(c(libpath, "edgeTransport", "GDPuc", "mrcommons", "mrdrivers")) + madrat::madratDetach(c(libpath, "edgeTransport", "GDPuc", "mrcommons", "mrdrivers", "mrindustry")) } # redirect standard messaging functions to vcat diff --git a/R/readADVANCE_WP2.R b/R/readADVANCE_WP2.R deleted file mode 100644 index 5f55aeb8..00000000 --- a/R/readADVANCE_WP2.R +++ /dev/null @@ -1,38 +0,0 @@ -#' Read ADVANCE WP2 Data -#' -#' @md -#' @param subtype One of -#' - `clinker-to-cement-ratio` for the clinker-to-cement ratios from figure 21 -#' of Edelenbosch, O. _Enhancing the representation of energy demand -#' developments in IAM models - A Modeling Guide for the Cement Industry_ -#' (2015) [zotero://select/items/JP8X2QFK](zotero://select/items/JP8X2QFK) -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @seealso [`readSource()`], [`convertADVANCE_WP2()`] -#' -#' @importFrom readr read_csv -#' -#' @export -readADVANCE_WP2 <- function(subtype) { - # ---- list all available subtypes with functions doing all the work ---- - switchboard <- list( - 'clinker-to-cement-ratio' = function() { - read_csv(file = './clinker-to-cement-ratio.csv', - col_types = 'cd', - comment = '#') %>% - as.magpie() - } - ) - - # ---- check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } else { - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) - } -} diff --git a/R/readECLIPSE.R b/R/readECLIPSE.R index d45a37cd..f6e0cdb0 100755 --- a/R/readECLIPSE.R +++ b/R/readECLIPSE.R @@ -1,4 +1,5 @@ # NH3 only has one scenario +#' @importFrom dplyr matches readECLIPSE <- function(subtype) { if (is.null(subtype) | ! subtype %in% c('activities.aggregated', 'activities.extended', 'emissions.aggregated', 'emissions.extended', 'shipping.emi', 'shipping.ef')) diff --git a/R/readIEA_WEIO_2014.R b/R/readIEA_WEIO_2014.R index 5570da82..25eaf316 100644 --- a/R/readIEA_WEIO_2014.R +++ b/R/readIEA_WEIO_2014.R @@ -9,11 +9,12 @@ #' `country_groups` with `IEA region`s and corresponding `iso3c` country #' codes. #' -#' @importFrom dplyr anti_join group_by left_join mutate pull select summarise +#' @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 ---- diff --git a/R/readMueller.R b/R/readMueller.R deleted file mode 100644 index 38142e00..00000000 --- a/R/readMueller.R +++ /dev/null @@ -1,83 +0,0 @@ -#' Read Müller et al. 2013 data. -#' -#' Read data from Müller et al. 2013 (http://dx.doi.org/10.1021/es402618m). -#' -#' @md -#' @param subtype One of: -#' - `countries`: read table mapping country names use by Müller et al. 2013 -#' to ISO 3166-1 alpha-3 codes. -#' - `stocks`: read low/medium/high estimates of per-capita steel stocks from -#' Müller et al. 2013 SI2 -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom assertr assert in_set -#' @importFrom dplyr distinct -#' -#' @seealso [`readSource()`] - - -#' @export -readMueller <- function(subtype) -{ - # ---- list all available subtypes with functions doing all the work ---- - switchboard <- list( - countries = function() - { - read_csv(file = './Mueller_countries.csv', - col_types = 'cc') %>% - # check for duplicated ISO codes - distinct(.data$country, .data$iso3c) %>% - group_by(.data$iso3c) %>% - mutate(iso3c.count = n()) %>% - ungroup() %>% - assert(in_set(1), .data$iso3c.count) %>% - select(-.data$iso3c.count) %>% - madrat_mule() - }, - - stocks = function() - { - # read low, medium, and high estimates ---- - lapply(c('low', 'med', 'high'), function(estimate) - { - read_excel(path = paste0('./Mueller_2013_CarbonEmissions_', - 'InfrastructureDevelopment_SI2.xlsx'), - sheet = paste('steel stock per cap', estimate), - range = 'A3:BH292') %>% - select(country = .data$Country, matches('^[0-9]{4}$')) %>% - pivot_longer(cols = matches('^[0-9]{4}$'), names_to = 'year', - names_transform = list(year = as.integer)) %>% - filter(.data$value != 0) %>% - mutate(estimate = estimate) - }) %>% - # combine all three sheets ---- - bind_rows() %>% - # combine USA and "USA (before 1981)" ---- - mutate(country = ifelse('USA (before 1981)' == .data$country, 'USA', - .data$country)) %>% - # add iso3c codes ---- - inner_join( - readSource(type = 'Mueller', subtype = 'countries', - convert = FALSE) %>% - madrat_mule(), - - 'country' - ) %>% - select(.data$estimate, .data$iso3c, .data$year, - steel.stock.per.capita = .data$value) %>% - madrat_mule() - } - ) - - # ---- check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) { - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - } else { - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) - } -} diff --git a/R/readPauliuk.R b/R/readPauliuk.R deleted file mode 100644 index 5e068b78..00000000 --- a/R/readPauliuk.R +++ /dev/null @@ -1,47 +0,0 @@ -#' Read Pauliuk et al. 2013 data -#' -#' Read data from Pauliuk et al. 2013 -#' (https://dx.doi.org/10.1016/j.resconrec.2012.11.008). -#' -#' @md -#' @param subtype One of: -#' - `lifetime`: Read estimated lifetime of overall steel stocks (approach b) -#' in years. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @seealso [`readSource()`] -#' -#' @importFrom dplyr mutate -#' @importFrom quitte madrat_mule -#' @importFrom readxl read_xlsx -#' @importFrom rlang .data - -#' @export -readPauliuk <- function(subtype = 'lifetime') -{ - # ---- list all available subtypes with functions doing all the work ---- - switchboard <- list( - lifetime = function() - { - read_xlsx(path = './Supplementary_Table_23.xlsx', - sheet = 'Supplementray_Table_23', - range = 'A5:J201', - col_names = c('country', 'lifetime'), - col_types = c('text', rep('skip', 8), 'numeric')) %>% - mutate(lifetime = as.integer(.data$lifetime)) %>% - madrat_mule() %>% - return() - }) - - # ---- check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) { - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - } else { - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) - } -} diff --git a/R/readStegmann2022.R b/R/readStegmann2022.R deleted file mode 100644 index 4185ccc6..00000000 --- a/R/readStegmann2022.R +++ /dev/null @@ -1,28 +0,0 @@ -#' Read PlasticsEoL -#' -#' Read-in data for the End-of-Life fate of plastics -#' from 1.Stegmann, P., Daioglou, V., Londo, M., van Vuuren, -#' D. P. & Junginger, M. Plastic futures and their CO2 emissions. -#' Nature 612, 272–276 (2022). -#' https://www.nature.com/articles/s41586-022-05422-5 -#' Link to SI: -#' https://static-content.springer.com/esm/art%3A10.1038%2Fs41586-022-05422-5/MediaObjects/41586_2022_5422_MOESM1_ESM.xlsx #nolint -#' -#' @md -#' @return magpie object of the data -#' @author Falk Benke, Simón Moreno -#' @seealso \code{\link{readSource}} -#' @examples -#' \dontrun{ -#' a <- readSource(type = "Stegmann2022") -#' } -#' -#' @importFrom readxl read_xlsx - -readStegmann2022 <- function() { - - data <- read_xlsx("41586_2022_5422_MOESM1_ESM.xlsx", sheet = "Data") - - reshape2::melt(data, id.vars = seq(1, 5), variable.name = "period") %>% - as.magpie(spatial = 3) -} diff --git a/R/readUSGS.R b/R/readUSGS.R deleted file mode 100644 index de3931c8..00000000 --- a/R/readUSGS.R +++ /dev/null @@ -1,282 +0,0 @@ -#' Read U.S. Geological Survey data -#' -#' @md -#' @param subtype One of -#' - 'cement': read cement production data from -#' [U.S. Geological Survey Minerals Yearbook](https://www.usgs.gov/centers/national-minerals-information-center/cement-statistics-and-information) -#' (unit: tonnes per year) -#' @param x Data returned by [readUSGS()]. -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom assertr assert -#' @importFrom dplyr bind_rows mutate pull rename select distinct -#' @importFrom readr read_csv -#' @importFrom readxl read_excel -#' @importFrom rlang is_empty -#' @importFrom tibble tibble -#' @importFrom tidyr pivot_longer - -#' @export -#' @rdname USGS -readUSGS <- function(subtype = 'cement') { - # subtype switchboard ---- - switchboard <- list( - 'cement' = function() { - # list of excel files and the sheet containing cement production ---- - files_sheets <- tribble( - ~file, ~sheet, - 'cememyb_2002.xls', 'Table23', - 'cemenmyb_2003.xls', 'T23', - 'cemenmyb_2004.xls', 'T22', - 'cemenmyb_2005.xls', 'T22', - 'myb1-2006-cemen.xls', 'T22', - 'myb1-2007-cemen.xls', 'T22', - 'myb1-2008-cemen.xls', 'T22', - 'myb1-2009-cemen.xls', 'T22', - 'myb1-2010-cemen.xls', 'T22', - 'myb1-2011-cemen.xls', 'T22', - 'myb1-2012-cemen.xls', 'T22', - 'myb1-2013-cemen.xlsx', 'T22', - 'myb1-2014-cemen.xlsx', 'T22', - 'myb1-2015-cemen.xlsx', 'T22', - 'myb1-2016-cement.xls', 'T22', - 'myb1-2017-cemen.xls', 'T22', - 'myb1-2018-cemen-adv-1.xlsx', 'T22', - 'Copy of myb1-2019-cemen-adv.xlsx', 'T22', - 'myb1-2020-cemen-ERT.xlsx', 'T22') - - # in case the cement files are moved to a subdirectory - base_path <- './' - # base_path <- '~/PIK/swap/inputdata/sources/USGS/' - - # country name to iso3c code mapping ---- - country.name_iso3c <- read_csv( - file = file.path(base_path, 'country.names.csv'), - comment = '#', - show_col_types = FALSE) - - # read all excel sheets ---- - data <- tibble() - for (i in 1:nrow(files_sheets)) { - f <- path.expand(file.path(base_path, files_sheets[[i, 'file']])) - ## read the entire sheet with global cement production data ---- - d <- suppressMessages( - read_excel(path = f, - sheet = files_sheets[[i, 'sheet']], - col_types = 'text', - trim_ws = TRUE) - ) - - ## find those portions that contain data ---- - foo <- pull(d, 1) - - # data sections start with either 'Country' or 'Country or locality' - # extend the regex to accomodate future changes - starts <- which(foo %in% c('Country', - 'Country or locality')) - - # data sections end with 'See footnotes at end of table.', - # 'Grand total', or 'Total', which in turn might have an estimator mark - # ('e') - # extend the regex to accomodate future changes - ends <- which(foo %in% c('See footnotes at end of table.', - 'Grand total', - 'Total', - 'Totale', - '\u2003Grand totale')) - - # subsections start with a heading ending with ':', - # e.g. 'Tunesia, portland:' - subsections <- which(grepl(':$', foo)) - - combination <- sort(c(subsections, ends)) - subsection_ends <- combination[which(combination %in% subsections) + 1] - ends <- setdiff(ends, subsection_ends) - - # filter cases in which 'Total' is directly follow by a 'See footnotes' - # note - ends <- setdiff(ends, ends[na.omit(match((ends + 1), ends))]) - - # there should be as many 'starts' as 'ends' - if (length(starts) != length(ends)) { - stop('Unmatched start or end of global cement data in file ', f, '\n', - 'Matched segments:\n', - paste(paste(sort(c(starts, ends)), foo[sort(c(starts, ends))]), - collapse = '\n')) - } - - # fix subsection names to country names and replace with total data - if (!is_empty(subsections)) { - d[subsections,1] <- sub('^(.*),.*', '\\1', d[[subsections,1]]) - d[subsections,-1] <- d[subsection_ends,-1] - } - - # valid data is everything between 'starts' and 'ends' (excluding), - # without subsections, but including the (fixed) first lines of - # subsections - sequences_by_index <- function(start, end) { - if (all(is_empty(start), is_empty(end))) { - return(NULL) - } - lapply(1:length(start), function(i) { start[i]:end[i] }) %>% - unlist() - } - - ## combine valid data with data from other excel files ---- - header <- as.character(d[starts[1],]) - - data <- bind_rows( - data, - - d[setdiff(sequences_by_index(starts+1, ends-1), - sequences_by_index(subsections + 1, subsection_ends)),] %>% - # rename columns - `colnames<-`(header) %>% - # remove comment columns - select(c(1, which(grepl('^[0-9]{4}$', header)))) %>% - # harmonise country column name - rename(country.name = 1) %>% - # convert to long format - pivot_longer(cols = -1, names_to = 'year') %>% - mutate( - year = as.integer(sub('^([0-9]*).*', '\\1', .data$year)), - # add reporting year; this supposes that the _last_ four-digit - # number in the file name is the reporting year - reporting.year = as.integer(sub('.*([0-9]{4}).*', '\\1', f)), - # remove notes from country names - country.name = sub('(, *)?[0-9]*$', '', .data$country.name), - # clear special value codes - value = case_when( - '--' == .data$value ~ '0', - '(5)' == .data$value ~ '0', - 'XX' == .data$value ~ '0', - TRUE ~ .data$value)) %>% - left_join(country.name_iso3c, 'country.name') %>% - assert(not_na, 'iso3c', - error_fun = function(errors, data, warn) { - error_df <- data[errors[[1]]$error_df$index,] - - stop('Unmachtched country names in ', f, '\n', - paste(format(error_df, )[-c(1, 3)], collapse = '\n'), - call. = FALSE) - }) %>% - select(-'country.name') - ) - } - - # estimate withheld information using GDP ---- - region_mapping <- toolGetMapping(name = 'regionmapping_21_EU11.csv', - type = 'regional', where = "mappingfolder") %>% - as_tibble() %>% - select(iso3c = 'CountryCode', region = 'RegionCode') - - to_estimate <- data %>% - # only estimate data when the last reporting year has no data - group_by(!!!syms(c('year', 'iso3c'))) %>% - filter(max(.data$reporting.year) == .data$reporting.year) %>% - ungroup() %>% - filter('W' == .data$value) %>% - distinct(.data$year, .data$iso3c) %>% - left_join(region_mapping, 'iso3c') - - estimation_base <- data %>% - inner_join(region_mapping,'iso3c') %>% - semi_join(to_estimate %>% select(-'iso3c'), c('year', 'region')) %>% - arrange(.data$reporting.year) %>% - group_by(!!!syms(c('year', 'iso3c', 'region'))) %>% - filter(max(.data$reporting.year) == .data$reporting.year) %>% - ungroup() %>% - select(-'reporting.year') %>% - left_join( - calcOutput('GDP', aggregate = FALSE, average2020 = FALSE, years = unique(to_estimate$year)) %>% - # historic data should be all identical, so just pick the 'default' - # scenario - `[`(,,'gdp_SSP2EU') %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = 'Region', year = 'Year', GDP = 'Value') %>% - mutate(year = as.integer(as.character(.data$year))), - - c('iso3c', 'year') - ) - - estimates <- full_join( - estimation_base %>% - anti_join(to_estimate %>% select(-'region'), c('year', 'iso3c')) %>% - mutate(value = as.numeric(.data$value)) %>% - group_by(!!!syms(c('year', 'region'))) %>% - summarise(production_per_GDP = sum(.data$value) / sum(.data$GDP), - .groups = 'drop'), - - estimation_base %>% - semi_join(to_estimate, c('region', 'iso3c', 'year')), - - c('year', 'region') - ) %>% - mutate(value = .data$GDP * .data$production_per_GDP) %>% - select('year', 'iso3c', 'value') - - data <- bind_rows( - data %>% - filter('W' != .data$value) %>% - mutate(value = as.numeric(.data$value)), - - data %>% - select(-'value') %>% - right_join(estimates, c('year', 'iso3c')) - ) %>% - # kt/year * 1e3 t/kt = t/year - mutate(value = .data$value * 1e3) %>% - # merge countries with several entries (e.g. Kosovo & Serbia -> SRB) - group_by(!!!syms(c('iso3c', 'reporting.year', 'year'))) %>% - summarise(value = sum(.data$value), .groups = 'drop') - - return(madrat_mule(data)) - } - ) - - # check if the subtype called is available ---- - if (!subtype %in% names(switchboard)) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } - - # load data and to whatever ---- - return(switchboard[[subtype]]()) -} - -#' @export -#' @rdname USGS -convertUSGS <- function(x, subtype = 'cement') { - . <- NULL - # subtype switchboard ---- - switchboard <- list( - 'cement' = function() { - x %>% - madrat_mule() %>% - group_by(!!!syms(c('iso3c', 'year'))) %>% - filter(max(.data$reporting.year) == .data$reporting.year) %>% - ungroup() %>% - select(-'reporting.year') %>% - complete(nesting(!!sym('year')), - iso3c = toolGetMapping(name = getConfig('regionmapping'), - type = 'regional') %>% - pull('CountryCode') %>% - unique(), - fill = list(value = 0)) %>% - as.magpie(spatial = 2, temporal = 1, datacol = ncol(.)) - } - ) - - # check if the subtype called is available ---- - if (!subtype %in% names(switchboard)) { - stop(paste('Invalid subtype -- supported subtypes are:', - paste(names(switchboard), collapse = ', '))) - } - - # load data and to whatever ---- - return(switchboard[[subtype]]()) -} diff --git a/R/readnonEnergyDemand.R b/R/readnonEnergyDemand.R deleted file mode 100644 index 90164fe8..00000000 --- a/R/readnonEnergyDemand.R +++ /dev/null @@ -1,18 +0,0 @@ -#' Read Final energy demand for feedstocks (non-energy use) -#' -#' @return magpie object of region dependent data -#' @author Renato Rodrigues -#' -#' @seealso \code{\link{readSource}} -#' -#' @examples -#' \dontrun{ -#' a <- readSource(type = "nonEnergyDemand") -#' } -#' -readnonEnergyDemand <- function() { - x <- read.csv("Final_Energy_demand_for_non_energy_use_in_industry.csv") - x <- as.magpie(x, spatial = 2, temporal = 1) - - return(x) -} diff --git a/R/readvanRuijven2016.R b/R/readvanRuijven2016.R deleted file mode 100644 index 65c20145..00000000 --- a/R/readvanRuijven2016.R +++ /dev/null @@ -1,57 +0,0 @@ -#' Read van Ruijven et al. (2016) data. -#' -#' Read data from van Ruijven et al. 2016, -#' (http://dx.doi.org/10.1016/j.resconrec.2016.04.016, -#' https://www.zotero.org/groups/52011/rd3/items/itemKey/6QMNBEHQ), obtained -#' through personal communication (e-mail to Michaja Pehl). Units are tonnes -#' per year. -#' -#' @md -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr filter mutate select -#' @importFrom quitte add_countrycode_ madrat_mule -#' @importFrom readxl read_excel -#' @importFrom tidyr expand_grid pivot_longer -#' -#' @seealso [`readSource()`] - -#' @export -readvanRuijven2016 <- function() { - USSR_iso3c <- c('ARM', 'AZE', 'BLR', 'EST', 'GEO', 'KAZ', 'KGZ', 'LTU', - 'LVA', 'MDA', 'RUS', 'TJK', 'TKM', 'UKR', 'UZB') - - x <- read_excel( - path = './Cement_data_from_Bas.xlsx', - # path = '~/PIK/swap/inputdata/sources/vanRuijven2016/Cement_data_from_Bas.xlsx', - sheet = 'Production', - range = 'A4:AS321') %>% - filter(999 != .data$`Region #`) %>% - pivot_longer(cols = matches('^[0-9]{4}$'), names_to = 'year', - names_transform = list(year = as.integer), - values_drop_na = TRUE) %>% - add_countrycode_(origin = c('FAO #' = 'fao'), destination = 'iso3c', - warn = FALSE) %>% - add_countrycode_(origin = c('FAO Name' = 'country.name'), - destination = c('iso3c.alt' = 'iso3c'), warn = FALSE) - - # remove Soviet Republics if USSR is available, to avoid double counting - x %>% - anti_join( - expand_grid(iso3c = USSR_iso3c, - year = x %>% - filter('USSR' == .data$`FAO Name`) %>% - pull('year') %>% - unique()), - - c('iso3c', 'year') - ) %>% - mutate(iso3c = ifelse(!is.na(.data$iso3c), .data$iso3c, .data$iso3c.alt), - # kg * 0.001 t/kg = t - value = .data$value * 1e-3) %>% - filter(!is.na(.data$iso3c)) %>% - select('iso3c', 'year', 'value') %>% - madrat_mule() -} diff --git a/R/readworldsteel.R b/R/readworldsteel.R deleted file mode 100644 index f6680134..00000000 --- a/R/readworldsteel.R +++ /dev/null @@ -1,183 +0,0 @@ -#' Read World Steel Statistical Yearbook Data -#' -#' Read combined data of World Steel Association statistical yearbooks -#' (https://www.worldsteel.org/steel-by-topic/statistics/steel-statistical-yearbook.html). -#' -#' @param subtype One of -#' - `detailed` returning data for the worksheets -#' - `Pig Iron Production` -#' - `DRI Production` -#' - `Total Production of Crude Steel` -#' - `Production in Oxygen-Blown Converters` -#' - `Production in Open Hearth Furnaces` -#' - `Production in Electric Arc Furnaces` -#' - `Apparent Steel Use (Crude Steel Equivalent)` -#' from 1991 on or -#' - `long` returning total production data from 1967 on -#' -#' @return A [`magpie`][magclass::magclass] object. -#' -#' @author Michaja Pehl -#' -#' @importFrom dplyr bind_rows filter group_by inner_join left_join mutate -#' select summarise -#' @importFrom quitte add_countrycode_ madrat_mule -#' @importFrom readr read_delim read_rds -#' @importFrom rlang is_empty -#' @importFrom tibble as_tibble tribble -#' @importFrom tidyr pivot_longer -#' -#' @seealso [`readSource()`] - -#' @export -readworldsteel <- function(subtype = 'detailed') { - # ---- list all available subtypes with functions doing all the work ---- - switchboard <- list( - 'detailed' = function() { - . <- NULL - - # to facilitate debugging - file_path <- './Steel_Statistical_Yearbook_combined.ods' - base_path <- file.path('./source/statistical_yearbook_2023_data/') - - .country_to_iso3c <- function(data) - { - data %>% - add_countrycode_(origin = c(country = 'country.name'), - destination = 'iso3c', warn = FALSE) %>% - # add fake iso3c codes for former countries and aggregates - left_join( - tribble( - ~country, ~iso3c.alt, - 'Belgium-Luxembourg', 'blx', - 'Belgium-Luxemburg', 'blx', - 'Czechoslovakia', 'CSK', - 'Serbia and Montenegro', 'SCG', - 'Serbia-Montenegro', 'SCG', - 'Yugoslavia', 'YUG', - 'F.R. Yugoslavia', 'YUG', - 'Former Yugoslavia', 'YUG'), - - 'country' - ) %>% - mutate(iso3c = ifelse(!is.na(.data$iso3c), .data$iso3c, - .data$iso3c.alt)) %>% - select('iso3c', 'year', 'name', 'value') %>% - assert(not_na, 'iso3c') %>% - # combine country aggregates - group_by(.data$iso3c, .data$year, .data$name) %>% - summarise(value = sum(.data$value, na.rm = TRUE), - .groups = 'drop') %>% - filter(0 != .data$value) - } - - d_old <- lapply( - # read these worksheets - c('Pig Iron Production', - 'DRI Production', - 'Total Production of Crude Steel', - 'Production in Oxygen-Blown Converters', - 'Production in Open Hearth Furnaces', - 'Production in Electric Arc Furnaces', - 'Apparent Steel Use (Crude Steel Equivalent)'), - function(sheet) { - # from this file - readODS::read_ods(path = file_path, sheet = sheet, na = '...') %>% - as_tibble() %>% - mutate(name = sheet) %>% - pivot_longer(c(-'country', -'name'), names_to = 'year', - names_transform = list(year = function(x) { - as.integer(sub('^X', '', x)) })) - }) %>% - bind_rows() %>% - .country_to_iso3c() - - layout <- tribble( - ~name, ~file, ~range, - 'Pig Iron Production', 'P26_Production_of_pig_iron.xlsx', 'A3:U54', - 'DRI Production', 'P27_Production_of_direct_reduced_iron.xlsx', 'A3:U35', - 'Total Production of Crude Steel', 'P01_Total_production_of_crude_steel.xlsx', 'A3:U101', - 'Production in Oxygen-Blown Converters', 'P05_Production_of_crude_steel_in_oxygen_blown_converters.xlsx', 'A3:U54', - 'Production in Open Hearth Furnaces', 'P07_Production_of_crude_steel_in_other_processes.xlsx', 'A3:U82', - 'Production in Electric Arc Furnaces', 'P06_Production_of_crude_steel_in_electric_furnaces.xlsx', 'A3:U100', - 'Apparent Steel Use (Crude Steel Equivalent)', 'U01_Apparent_steel_use_(crude_steel_equivalent).xlsx', 'A3:U125') - - d_new <- lapply(seq_len(nrow(layout)), - function(i) { - readxl::read_excel(path = file.path(base_path, layout[[i,'file']]), - range = layout[[i,'range']]) %>% - rename(country = 'Country') %>% - filter(!.data$country %in% c('World', 'Others')) %>% - pivot_longer(-'country', names_to = 'year', - names_transform = as.integer) %>% - mutate(name = layout[[i,'name']]) - }) %>% - bind_rows() %>% - .country_to_iso3c() - - d_new_incomplete_years <- d_new %>% - group_by(.data$year) %>% - distinct(.data$name) %>% - summarise(count = n()) %>% - filter(max(.data$count) != .data$count) %>% - pull('year') - - d_new <- d_new %>% - filter(!.data$year %in% d_new_incomplete_years) - - d <- overwrite(d_new, d_old) - - - # split historic aggregates into current countries - d %>% - complete(nesting(!!!syms(c('iso3c', 'year'))), - tidyr::crossing(!!sym('name')), - fill = list(value = 0)) %>% - as.magpie(spatial = 1, temporal = 2, tidy = TRUE) %>% - toolISOhistorical( - mapping = read_delim( - file = system.file('extdata', 'ISOhistorical.csv', - package = 'madrat'), - delim = ';', - col_types = 'ccc') %>% - inner_join( - d %>% - group_by(.data$iso3c) %>% - summarise(year = max(.data$year), .groups = 'drop'), - - c('fromISO' = 'iso3c') - ) %>% - mutate( - lastYear = ifelse( - as.integer(sub('^y', '', .data$lastYear)) > .data$year, - .data$lastYear, paste0('y', .data$year))) %>% - select(-'year') - ) %>% - as.data.frame() %>% - as_tibble() %>% - select(iso3c = 'Region', name = 'Data1', year = 'Year', - value = 'Value') %>% - # remove fake magpie values - filter(0 != .data$value, !is.na(.data$value)) %>% - mutate(year = as.integer(as.character(.data$year))) %>% - madrat_mule() - }, - - 'long' = function() { - read_rds('./data_steel_production.rds') %>% - # kt/year * 1000 t/kt = t/year - mutate(value = .data$value * 1000) %>% - madrat_mule() - }, - - NULL) - - # ---- check if the subtype called is available ---- - if (is_empty(intersect(subtype, names(switchboard)))) { - stop(paste('Invalid subtype -- supported subtypes are:', - names(switchboard))) - } else { - # ---- load data and do whatever ---- - return(switchboard[[subtype]]()) - } -} diff --git a/R/tool_expand_tibble.R b/R/tool_expand_tibble.R deleted file mode 100644 index 045948a1..00000000 --- a/R/tool_expand_tibble.R +++ /dev/null @@ -1,104 +0,0 @@ -#' Expand tibble across scenarios and regions with default values -#' -#' The data.frame `d` is expanded in such a manner that all rows with `NA` in -#' either the `scenario` or `region` columns are extended to repeat for all -#' scenarios and regions listed in `scenarios` and `regions`. Rows with -#' specified scenarios and/or regions will overwrite extended ones. Regions are -#' expanded before scenarios. -#' -#' @param d A data.frame with columns `scenario` and `region`. -#' @param scenarios A character vector of scenario names. -#' @param regions A character vector of region names. -#' @param structure.columns A character vector of column names to be carried -#' along. -#' -#' @return A `tibble`. -#' -#' @importFrom dplyr anti_join bind_rows filter select -#' @importFrom rlang syms -#' @importFrom tidyr complete nesting -#' @importFrom tidyselect all_of -#' -#' @examples -#' \dontrun{ -#' tribble( -#' ~scenario, ~region, ~value, -#' NA, NA, 0, -#' NA, 'CHA', 1, -#' 'SSP1', NA, 2, -#' 'SSP2EU', 'DEU', 3) %>% -#' tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'), -#' regions = c('CHA', 'DEU', 'USA')) %>% -#' pivot_wider(names_from = 'region') -#' -#' tribble( -#' ~scenario, ~region, ~name, ~value, -#' NA, NA, 'A', 0, -#' NA, 'CHA', 'B', 1, -#' 'SSP1', NA, 'A', 2, -#' 'SSP2EU', 'DEU', 'B', 3) %>% -#' tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'), -#' regions = c('CHA', 'DEU', 'USA'), -#' structure.columns = 'name') -#' } - -#' @export -tool_expand_tibble <- function(d, scenarios, regions, - structure.columns = NULL) { - . <- NULL - - # entries with both scenarios and regions defined - d.scenario.region <- d %>% - filter(!is.na(.data$scenario), .data$scenario %in% scenarios, - !is.na(.data$region), .data$region %in% regions) - - # entries with only scenarios defined - d.scenario <- d %>% - filter(!is.na(.data$scenario), .data$scenario %in% scenarios, - is.na(.data$region)) %>% - complete(nesting(!!!syms(setdiff(colnames(.), 'region'))), - region = regions) %>% - filter(!is.na(.data$region)) - - # entries with only regions defined - d.region <- d %>% - filter(is.na(.data$scenario), - !is.na(.data$region), .data$region %in% regions) %>% - complete(nesting(!!!syms(setdiff(colnames(.), 'scenario'))), - scenario = scenarios) %>% - filter(!is.na(.data$scenario)) - - # entries with neither scenario nor regions defined - d.global <- d %>% - filter(is.na(.data$scenario), is.na(.data$region)) %>% - complete(nesting(!!!syms(setdiff(colnames(.), c('scenario', 'region')))), - scenario = scenarios, - region = regions) %>% - filter(!is.na(.data$scenario), !is.na(.data$region)) - - # combine all entries - d.global %>% - # scenarios overwrite global data - anti_join( - d.scenario, - - c('scenario', 'region', structure.columns) - ) %>% - bind_rows(d.scenario) %>% - # regions overwrite global and scenario data - anti_join( - d.region, - - c('scenario', 'region', structure.columns) - ) %>% - bind_rows(d.region) %>% - # specific data overwrites everything - anti_join( - d.scenario.region, - - c('scenario', 'region', structure.columns) - ) %>% - bind_rows(d.scenario.region) %>% - select(all_of(colnames(d))) %>% - return() -} diff --git a/R/tool_fix_IEA_data_for_Industry_subsectors.R b/R/tool_fix_IEA_data_for_Industry_subsectors.R index bf7601b3..fc400b0b 100644 --- a/R/tool_fix_IEA_data_for_Industry_subsectors.R +++ b/R/tool_fix_IEA_data_for_Industry_subsectors.R @@ -28,10 +28,10 @@ #' #' @importFrom assertr not_na assert #' @importFrom dplyr anti_join group_by inner_join left_join mutate pull rename -#' select summarise +#' select summarise right_join semi_join #' @importFrom readr read_delim cols col_skip col_character #' @importFrom quitte cartesian interpolate_missing_periods overwrite -#' character.data.frame interpolate_missing_periods_ +#' character.data.frame interpolate_missing_periods_ sum_total_ #' @importFrom rlang .data #' @importFrom tibble as_tibble #' @importFrom tidyr complete gather nesting spread diff --git a/README.md b/README.md index 47bf87d7..39228d01 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MadRat REMIND Input Data Package -R package **mrremind**, version **0.194.1** +R package **mrremind**, version **0.196.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrremind)](https://cran.r-project.org/package=mrremind) [![R build status](https://github.com/pik-piam/mrremind/workflows/check/badge.svg)](https://github.com/pik-piam/mrremind/actions) [![codecov](https://codecov.io/gh/pik-piam/mrremind/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrremind) [![r-universe](https://pik-piam.r-universe.dev/badges/mrremind)](https://pik-piam.r-universe.dev/builds) @@ -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 (2024). _mrremind: MadRat REMIND Input Data Package_. R package version 0.196.0, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrremind: MadRat REMIND Input Data Package}, author = {Lavinia Baumstark and Renato Rodrigues and Antoine Levesque and Julian Oeser and Christoph Bertram and Ioanna Mouratiadou and Aman Malik and Felix Schreyer and Bjoern Soergel and Marianna Rottoli and Abhijeet Mishra and Alois Dirnaichner and Michaja Pehl and Anastasis Giannousakis and David Klein and Jessica Strefler and Lukas Feldhaus and Regina Brecha and Sebastian Rauner and Jan Philipp Dietrich and Stephen Bi and Falk Benke and Pascal Weigmann and Oliver Richters and Robin Hasse and Sophie Fuchs and Rahel Mandaroux and Johannes Koch}, year = {2024}, - note = {R package version 0.194.1}, + note = {R package version 0.196.0}, url = {https://github.com/pik-piam/mrremind}, } ``` diff --git a/man/EDGE-Industry.Rd b/man/EDGE-Industry.Rd deleted file mode 100644 index a58d4445..00000000 --- a/man/EDGE-Industry.Rd +++ /dev/null @@ -1,67 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/EDGE-Industry.R -\name{calcSteel_Projections} -\alias{calcSteel_Projections} -\alias{calcIndustry_Value_Added} -\title{EDGE-Industry} -\usage{ -calcSteel_Projections( - subtype = "production", - match.steel.historic.values = TRUE, - match.steel.estimates = "none", - save.plots = NULL, - China_Production = NULL -) - -calcIndustry_Value_Added( - subtype = "physical", - match.steel.historic.values = TRUE, - match.steel.estimates = "none", - save.plots = NULL, - China_Production = NULL -) -} -\arguments{ -\item{subtype}{One of -\itemize{ -\item \code{production} Returns trajectories of primary and secondary steel -production (\code{calcSteel_Projections()}). -\item \code{secondary.steel.max.share} Returns the maximum share of secondary steel -in total steel production (\code{calcSteel_Projections()}). -\item \code{physical} Returns physical production trajectories for cement -(\code{calcIndustry_Value_Added()}). -\item \code{economic} Returns value added trajectories for all subsectors -(\code{calcIndustry_Value_Added()}). -}} - -\item{match.steel.historic.values}{Should steel production trajectories match -historic values?} - -\item{match.steel.estimates}{Should steel production trajectories match -exogenous estimates? \code{NULL} or one of -\itemize{ -\item \code{IEA_ETP} IEA 2017 Energy Transition Pathways steel production totals for -OECD and Non-OECD countries from the \emph{Reference Technologies Scenario} -until 2060, and original growth rates after that. -}} - -\item{save.plots}{\code{NULL} (default) if no plots are saved, or the path to save -directories to.} - -\item{China_Production}{A data frame with columns \code{period} and -\code{total.production} prescribing total production for China to have, -disregarding results from the stock saturation model.} -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, \code{description}, \code{min}, and \code{max}. -} -\description{ -Functions for calculating industry activity trajectories. -} -\seealso{ -\code{\link[=calcOutput]{calcOutput()}} -} -\author{ -Michaja Pehl -} diff --git a/man/ODYM_RECC.Rd b/man/ODYM_RECC.Rd deleted file mode 100644 index 3f6cdf54..00000000 --- a/man/ODYM_RECC.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/ODYM_RECC.R -\name{readODYM_RECC} -\alias{readODYM_RECC} -\alias{calcODYM_RECC} -\title{Read ODYM_RECC data from the SHAPE Project} -\usage{ -readODYM_RECC(subtype, smooth = TRUE) - -calcODYM_RECC(subtype, smooth = TRUE) -} -\arguments{ -\item{subtype}{One of -\itemize{ -\item \code{'REMIND_industry_trends'}: Trends in per-capita production of industry -subsectors \code{cement}, \code{chemicals}, \code{steel_primary}, \code{steel_secondary}, -and \code{otherInd}. Trends for \code{chemicals} and \code{otherInd} are averages of -the other three trends, which are provided by NTNU. -}} - -\item{smooth}{Smooth REMIND_industry_trends (default) or not.} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read ODYM_RECC data from the SHAPE Project -} -\author{ -Michaja Pehl -} diff --git a/man/UNIDO.Rd b/man/UNIDO.Rd deleted file mode 100644 index 11a979ed..00000000 --- a/man/UNIDO.Rd +++ /dev/null @@ -1,80 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/UNIDO.R -\name{readUNIDO} -\alias{readUNIDO} -\alias{convertUNIDO} -\alias{calcUNIDO} -\title{UNIDO data} -\usage{ -readUNIDO(subtype = "INDSTAT2") - -convertUNIDO(x, subtype = "INDSTAT2") - -calcUNIDO(subtype = "INDSTAT2") -} -\arguments{ -\item{subtype}{one of -- \code{INDSTAT2}: read INDSTAT2 data} - -\item{x}{result from \code{readUNIDO()} as passed to \code{convertUNIDO()}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. - -\code{readUNIDO} returns raw INDSTAT2 data. \code{convertUNIDO} converts to iso3c -country codes, selects industry subsectors value added data according to this -table\tabular{llll}{ - subsector \tab ISIC \tab ctable \tab utable \cr - manufacturing \tab D \tab 20 \tab 17–20 \cr - cement \tab 20 \tab 20 \tab 17–20 \cr - chemicals \tab 24 \tab 20 \tab 17–20 \cr - steel \tab 27 \tab 20 \tab 17–20 \cr -} - - -and filters data that is either unreasonable or would unduly bias regional -regressions according to this table\tabular{lll}{ - subsector \tab iso3c \tab years \cr - manufacturing \tab BIH \tab 1990–91 \cr - manufacturing \tab CHN \tab 1963–97 \cr - manufacturing \tab HKG \tab 1963–2015 \cr - manufacturing \tab IRQ \tab 1994–98 \cr - manufacturing \tab MAC \tab 1963–2015 \cr - manufacturing \tab MDV \tab 1963–2015 \cr - cement \tab BDI \tab 1980–2010 \cr - cement \tab CIV \tab 1990–93 \cr - cement \tab HKG \tab 1973–79 \cr - cement \tab IRQ \tab 1992–97 \cr - cement \tab NAM \tab 2007–10 \cr - cement \tab RUS \tab 1970–90 \cr - chemicals \tab CIV \tab 1989 \cr - chemicals \tab HKG \tab 1973–79, 2008–15 \cr - chemicals \tab MAC \tab 1978–79 \cr - chemicals \tab NER \tab 1999–2002 \cr - steel \tab BGD \tab 2011 \cr - steel \tab CHE \tab 1995–96 \cr - steel \tab CHL \tab 2008 \cr - steel \tab HKG \tab 1973–79 \cr - steel \tab HRV \tab 2012 \cr - steel \tab IRL \tab 1980 \cr - steel \tab LKA \tab 2006 \cr - steel \tab MAR \tab 1989–2004 \cr - steel \tab MKD \tab 1996 \cr - steel \tab PAK \tab 1981–82 \cr - steel \tab TUN \tab 2003–06 \cr -} - - -\code{calcUNIDO()} calculates \code{otherInd} subsector values as the difference -between \code{manufacturing} and \code{cement}, \code{chemicals}, and \code{steel} values and is -intended to be called through \code{\link[=calcOutput]{calcOutput()}}, which will aggregate regions. -} -\description{ -Read and convert data from United Nations Industrial Organisation. -} -\seealso{ -\code{\link[=readSource]{readSource()}}, \code{\link[=calcOutput]{calcOutput()}} -} -\author{ -Michaja Pehl -} diff --git a/man/USGS.Rd b/man/USGS.Rd deleted file mode 100644 index d963e913..00000000 --- a/man/USGS.Rd +++ /dev/null @@ -1,30 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readUSGS.R -\name{readUSGS} -\alias{readUSGS} -\alias{convertUSGS} -\title{Read U.S. Geological Survey data} -\usage{ -readUSGS(subtype = "cement") - -convertUSGS(x, subtype = "cement") -} -\arguments{ -\item{subtype}{One of -\itemize{ -\item 'cement': read cement production data from -\href{https://www.usgs.gov/centers/national-minerals-information-center/cement-statistics-and-information}{U.S. Geological Survey Minerals Yearbook} -(unit: tonnes per year) -}} - -\item{x}{Data returned by \code{\link[=readUSGS]{readUSGS()}}.} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read U.S. Geological Survey data -} -\author{ -Michaja Pehl -} diff --git a/man/calcCement.Rd b/man/calcCement.Rd deleted file mode 100644 index c108854e..00000000 --- a/man/calcCement.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcCement.R -\name{calcCement} -\alias{calcCement} -\title{Calculate Historic Cement Production} -\usage{ -calcCement() -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x} with -country-level cement production in tonnes, \code{weight}, \code{unit}, \code{description}, -and \code{min} fields. -} -\description{ -Combines cement production data from \code{\link[=readvanRuijven2016]{readvanRuijven2016()}} and -\code{\link[=readUSGS]{readUSGS(cement)}} into a single data set, using USGS data from -2005 on. -} -\seealso{ -\link{calcOutput} -} -\author{ -Michaja Pehl -} diff --git a/man/calcCementShare.Rd b/man/calcCementShare.Rd deleted file mode 100644 index 40acb702..00000000 --- a/man/calcCementShare.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcCementShare.R -\name{calcCementShare} -\alias{calcCementShare} -\title{Calculate Cement Share in NONMET FE Use} -\usage{ -calcCementShare() -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, \code{description}, \code{min}, and \code{max}. -} -\description{ -Estimated shares of cement in \code{NONMET} final energy use based on OECD and -Non-OECD figures from IEA 2017 \href{https://www.zotero.org/groups/52011/rd3/items/X8XCUJ5U}{Energy Technology Perspectives}. Shares are weighted -by GDP for aggregation and converge towards global values by 2100. -} -\seealso{ -\code{\link[=calcOutput]{calcOutput()}} -} -\author{ -Michaja Pehl -} diff --git a/man/calcChemicalFeedstocksShare.Rd b/man/calcChemicalFeedstocksShare.Rd deleted file mode 100644 index 65b45094..00000000 --- a/man/calcChemicalFeedstocksShare.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcChemicalFeedstocksShare.R -\name{calcChemicalFeedstocksShare} -\alias{calcChemicalFeedstocksShare} -\title{Calculate Chemical Feedstock share projections} -\usage{ -calcChemicalFeedstocksShare() -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, \code{description}, \code{min}, and \code{max}. -} -\description{ -Calculates the share of \code{CHEMICAL} in \code{CHEMICAL = NECHEM} and converges it -towards the maximum value of either OECD or non-OECD countries by 2050. -} -\seealso{ -\code{\link[=calcOutput]{calcOutput()}} -} -\author{ -Michaja Pehl -} diff --git a/man/calcClinker_to_cement_ratio.Rd b/man/calcClinker_to_cement_ratio.Rd deleted file mode 100644 index 47b568b5..00000000 --- a/man/calcClinker_to_cement_ratio.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcClinker_to_cement_ratio.R -\name{calcClinker_to_cement_ratio} -\alias{calcClinker_to_cement_ratio} -\title{Calculate Clinker-to-Cement Ratio} -\usage{ -calcClinker_to_cement_ratio() -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, and \code{description}. -} -\description{ -Calculate Clinker-to-Cement Ratio -} -\seealso{ -\code{\link[=calcOutput]{calcOutput()}}, \code{\link[=readADVANCE_WP2]{readADVANCE_WP2()}}, \code{\link[=convertADVANCE_WP2]{convertADVANCE_WP2()}} -} -\author{ -Michaja Pehl -} diff --git a/man/calcEmissionFactorsFeedstocks.Rd b/man/calcEmissionFactorsFeedstocks.Rd deleted file mode 100644 index 42b3c091..00000000 --- a/man/calcEmissionFactorsFeedstocks.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcEmissionFactorsFeedstocks.R -\name{calcEmissionFactorsFeedstocks} -\alias{calcEmissionFactorsFeedstocks} -\title{Calculate emission factors for feedstocks in the chemicals industry -using emissions from UNFCCC and energy demands from IEA Energy Balances} -\usage{ -calcEmissionFactorsFeedstocks() -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, \code{description}. -} -\description{ -Calculate emission factors for feedstocks in the chemicals industry -using emissions from UNFCCC and energy demands from IEA Energy Balances -} -\seealso{ -\code{\link[=calcOutput]{calcOutput()}} -} -\author{ -Falk Benke, Renato Rodrigues, Simón Moreno Leiva -} diff --git a/man/calcFeDemandIndustry.Rd b/man/calcFeDemandIndustry.Rd deleted file mode 100644 index 67829ac0..00000000 --- a/man/calcFeDemandIndustry.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcFeDemandIndustry.R -\name{calcFeDemandIndustry} -\alias{calcFeDemandIndustry} -\title{Calculates FE demand in industry as REMIND variables} -\usage{ -calcFeDemandIndustry(use_ODYM_RECC = FALSE) -} -\arguments{ -\item{use_ODYM_RECC}{per-capita pathways for `SDP_xx` scenarios? (Defaults -to `FALSE`.)} -} -\description{ -Calculates FE demand in industry as REMIND variables -} -\author{ -Michaja Pehl -} diff --git a/man/calcIndustry_CCS_limits.Rd b/man/calcIndustry_CCS_limits.Rd deleted file mode 100644 index 09837819..00000000 --- a/man/calcIndustry_CCS_limits.Rd +++ /dev/null @@ -1,88 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIndustry_CCS_limits.R -\name{calcIndustry_CCS_limits} -\alias{calcIndustry_CCS_limits} -\title{Calculate Limits on Industry CCS Capacities} -\usage{ -calcIndustry_CCS_limits( - a1 = 0.3, - a2 = 0.15, - installation_minimum = 1, - stage_weight = c(Operational = 1, `In construction` = 1, `Advanced development` = 0.5, - `Early development` = 0.2), - facility_subsector = c(Cement = "cement", Chemical = "chemicals", - `Hydrogen / Ammonia / Fertiliser` = "chemicals", Ethan = "chemicals", - `Iron and Steel Production` = "steel"), - region_mapping = NULL -) -} -\arguments{ -\item{a1, a2}{Annual growth factors of CCS capacity limits, for the first ten -years and thereafter, default to \code{0.7} and \code{0.2} (70 \% and 20 \%, -respectively).} - -\item{installation_minimum}{Minimum emission capacity (in MtCO~2~/year) -capacities are rounded up to. Defaults to \code{0.5} (500 ktCO~2~/year).} - -\item{stage_weight}{A named vector of weight factors for different lifecycle -stages. See Details.} - -\item{facility_subsector}{A named vector mapping the "Facility Industry" of -CCS projects to REMIND industry subsectors. See Details.} - -\item{region_mapping}{A data frame with columns \code{iso3c} and \code{region} detailing -the regional resolution on which data should be extrapolated. If \code{NULL} -(the default), extrapolation is done at the country level.} -} -\value{ -A list with a \code{\link[magclass:magclass-package]{magpie}} object \code{x}, \code{weight}, -\code{unit}, \code{description}, and \code{min}. -} -\description{ -Calculate Limits on Industry CCS Capacities -} -\details{ -The limits on industry CCS capacities are calculated from data of the -\href{zotero://select/items/3_E5GNNPZ8}{Global Status of CCS 2023} report (through -\code{\link[=readGlobalCCSinstitute]{readGlobalCCSinstitute()}}. CCS projects are -\itemize{ -\item filtered for valid (i.e. not "Under Evaluation") data for "Operation date" -and "CO~2~ capture capacity" -\item assigned to REMIND industry subsectors according to \code{facility_subsector}, -which defaults to\tabular{ll}{ - Facility Industry \tab subsector \cr - Cement \tab cement \cr - Chemical \tab chemicals \cr - Hydrogen / Ammonia / Fertiliser \tab chemicals \cr - Ethan \tab chemicals \cr - Iron and Steel Production \tab steel \cr -} - -\item weighted by lifecycle stage according to \code{stage_weight}, which defaults to\tabular{lr}{ - Lifecycle stage \tab weight \cr - Operational \tab 100 \% \cr - In construction \tab 100 \% \cr - Advanced development \tab 50 \% \cr - Early development \tab 20 \% \cr -} - -} - -The resulting project capacities constitute the limits on industry subsector -CCS capacity for 2025. The limit on CCS capacities for regions (or countries -if \code{region_mapping} is \code{NULL}) is set to a value of total 2025 subsector CCS -capacity, times the regions share in subsector activity (e.g. cement -production) of the SSP2EU scenario -\itemize{ -\item in 2030 if the region as some CCS capacity in 2025 in a different industry -subsector, or -\item in 2035 if the region has no industry CCS capacity in 2030 at all. -} - -CCS capacities are increased by the annual growth factor \code{a1} for the ten -first years, and by the annual growth factor \code{a2} afterwards (defaulting to -70 \% and 20 \%, respectively). -} -\author{ -Michaja Pehl -} diff --git a/man/calcIndustry_EEK.Rd b/man/calcIndustry_EEK.Rd deleted file mode 100644 index 91839d6a..00000000 --- a/man/calcIndustry_EEK.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcIndustry_EEK.R -\name{calcIndustry_EEK} -\alias{calcIndustry_EEK} -\title{Industry Energy Efficiency Capital} -\usage{ -calcIndustry_EEK(kap) -} -\arguments{ -\item{kap}{General internal capital stock, as calculated internally by -`calcCapital()`.} -} -\value{ -A list with a [`magpie`][magclass::magclass] object `x`, `weight`, - `unit`, and `description` fields. -} -\description{ -Industry Energy Efficiency Capital -} diff --git a/man/calcShareIndFE.Rd b/man/calcShareIndFE.Rd deleted file mode 100644 index 9d797fae..00000000 --- a/man/calcShareIndFE.Rd +++ /dev/null @@ -1,32 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcShareIndFE.R -\name{calcShareIndFE} -\alias{calcShareIndFE} -\title{Share of Industry Subsectors in FE Use} -\usage{ -calcShareIndFE() -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Calculates industry subsector shares in final energy carrier use for the -\code{fixed_shares} realisation of the \code{industry} module. -} -\details{ -For the region mapping \code{regionmapping_21_EU11.csv}, these are based on IEA -data from \code{calcOutput(type = 'FEdemand')}, for all other -region mappings on vintage data which is ultimately based on Enerdata data. -} -\note{ -There is a discrepancy between the shares calculated from these two -sources, that will affect REMIND emission reporting. -} -\seealso{ -\code{\link[madrat:calcOutput]{calcOutput()}}. -} -\author{ -Lavinia Baumstark - -Michaja Pehl -} diff --git a/man/calcSteelStock.Rd b/man/calcSteelStock.Rd deleted file mode 100644 index 48b31df1..00000000 --- a/man/calcSteelStock.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcSteelStock.R -\name{calcSteelStock} -\alias{calcSteelStock} -\title{Calculate Steel Stock from Mueller steel stock per capita and WDI population} -\usage{ -calcSteelStock() -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Calculate Steel Stock from Mueller steel stock per capita and WDI population -} -\author{ -Falk Benke -} diff --git a/man/calcindustry_max_secondary_steel_share.Rd b/man/calcindustry_max_secondary_steel_share.Rd deleted file mode 100644 index 8e66463e..00000000 --- a/man/calcindustry_max_secondary_steel_share.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcindustry_max_secondary_steel_share.R -\name{calcindustry_max_secondary_steel_share} -\alias{calcindustry_max_secondary_steel_share} -\title{Calculate Maximum Secondary Steel Production Share} -\usage{ -calcindustry_max_secondary_steel_share(scenarios = NULL, regions = NULL) -} -\arguments{ -\item{scenarios}{A character vector of scenarios to expand data to.} - -\item{regions}{A character vector of regions to expand data to.} -} -\value{ -A list with a [`magpie`][magclass::magclass] object `x`. -} -\description{ -Reads ExpertGuess/industry_max_secondary_steel_share and expands to all -`scenarios`/`regions` using default data. See [`tool_expand_tibble()`] for -details. -} diff --git a/man/calcindustry_specific_FE_limits.Rd b/man/calcindustry_specific_FE_limits.Rd deleted file mode 100644 index cbad7060..00000000 --- a/man/calcindustry_specific_FE_limits.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcindustry_specific_FE_limits.R -\name{calcindustry_specific_FE_limits} -\alias{calcindustry_specific_FE_limits} -\title{Thermodynamic Limits for Industry Specific FE Demand} -\usage{ -calcindustry_specific_FE_limits() -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Return \code{readindustry_subsectors_specific('industry_specific_FE_limits')} in a -format usable as a REMIND input. -} -\author{ -Michaja Pehl -} diff --git a/man/calcnonEnergyIndFE.Rd b/man/calcnonEnergyIndFE.Rd deleted file mode 100644 index f8b614dd..00000000 --- a/man/calcnonEnergyIndFE.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcnonEnergyIndFE.R -\name{calcnonEnergyIndFE} -\alias{calcnonEnergyIndFE} -\title{Final energy demand for feedstocks (non-energy use)} -\usage{ -calcnonEnergyIndFE() -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Final energy demand for feedstocks (non-energy use) -} -\seealso{ -\code{\link[madrat:calcOutput]{calcOutput()}}. -} -\author{ -Renato Rodrigues -} diff --git a/man/convertADVANCE_WP2.Rd b/man/convertADVANCE_WP2.Rd deleted file mode 100644 index 6ce6966c..00000000 --- a/man/convertADVANCE_WP2.Rd +++ /dev/null @@ -1,33 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/convertADVANCE_WP2.R -\name{convertADVANCE_WP2} -\alias{convertADVANCE_WP2} -\title{Convert ADVANCE WP2 Data} -\usage{ -convertADVANCE_WP2(x, subtype) -} -\arguments{ -\item{x}{A \code{\link[magclass:magclass-package]{magpie}} object returned by -\code{\link[=readADVANCE_WP2]{readADVANCE_WP2()}}.} - -\item{subtype}{One of -\itemize{ -\item \code{clinker-to-cement-ratio} for the clinker-to-cement ratios from figure 21 -of Edelenbosch, O. \emph{Enhancing the representation of energy demand -developments in IAM models - A Modeling Guide for the Cement Industry} -(2015) \url{zotero://select/items/JP8X2QFK}, -which is extended from H12 regions to country level. -}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Convert ADVANCE WP2 Data -} -\seealso{ -\code{\link[=readSource]{readSource()}}, \code{\link[=readADVANCE_WP2]{readADVANCE_WP2()}} -} -\author{ -Michaja Pehl -} diff --git a/man/convertMueller.Rd b/man/convertMueller.Rd deleted file mode 100644 index ca3cfd52..00000000 --- a/man/convertMueller.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/convertMueller.R -\name{convertMueller} -\alias{convertMueller} -\title{Convert Mueller data} -\usage{ -convertMueller(x, subtype) -} -\arguments{ -\item{x}{A \code{\link[magclass:magclass-package]{magpie}} object returned from -\code{\link[=readMueller]{readMueller()}}.} - -\item{subtype}{One of: -\itemize{ -\item \code{countries}: read table mapping country names use by Müller et al. 2013 -to ISO 3166-1 alpha-3 codes. -\item \code{stocks}: read low/medium/high estimates of per-capita steel stocks from -Müller et al. 2013 SI2 -}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Convert Mueller data -} -\author{ -Falk Benke -} diff --git a/man/convertStegmann2022.Rd b/man/convertStegmann2022.Rd deleted file mode 100644 index df5cf82b..00000000 --- a/man/convertStegmann2022.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/convertStegmann2022.R -\name{convertStegmann2022} -\alias{convertStegmann2022} -\title{convertStegmann2022} -\usage{ -convertStegmann2022(x) -} -\arguments{ -\item{x}{unconverted magpie object from read-script} -} -\value{ -magpie object with a completed dataset. -} -\description{ -Converts data from Stegmann2022 -} diff --git a/man/industry_subsector_specific.Rd b/man/industry_subsector_specific.Rd deleted file mode 100644 index d9b499d9..00000000 --- a/man/industry_subsector_specific.Rd +++ /dev/null @@ -1,74 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/industry_subsectors_specific.R -\name{readindustry_subsectors_specific} -\alias{readindustry_subsectors_specific} -\alias{calcindustry_subsectors_specific} -\title{industry/subsector change factors} -\usage{ -readindustry_subsectors_specific(subtype = NULL) - -calcindustry_subsectors_specific( - subtype = NULL, - scenarios = NULL, - regions = NULL, - direct = NULL -) -} -\arguments{ -\item{subtype}{One of -\itemize{ -\item \code{FE} for specific final energy demand change factors -\item \code{material_alpha} for alpha factors and convergence time of specific -material demand decreases relative to the \code{SSP2EU} scenario -\item \code{material_relative} for scaling factors of specific material demand -relative to baseline scenarios -\item \code{material_relative_change} for scaling factors of specific material -demand \emph{change} relative to baseline scenarios -}} - -\item{scenarios}{A vector of scenarios for which factors are to be returned.} - -\item{regions}{A vector of regions for which factors are to be returned.} - -\item{direct}{A data frame as returned by -\code{readindustry_subsectors_specific()} to load debugging/developing data -directly instead of from file.} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Change factors of specific FE and material demand for the -\code{industry/subsector} realisation of REMIND. -} -\details{ -Factors are read from the files \code{specific_FE.csv}, -\code{specific_material_alpha.csv}, \code{specific_material_relative.csv}, and -\code{specific_material_relative_change.csv}, respectively. \code{NA} is used to mark -defaults for the \code{scenario} and \code{region} columns, and specified values will -overwrite these defaults. - -So -\itemize{ -\item \verb{NA,NA,cement,1} will be extended to all \code{scenarios} and \code{regions} -\item \verb{scen1,NA,cement,2} will overwrite this default for all \code{regions} in -\code{scen1} -\item \verb{NA,regi1,cement,3} will overwrite this again for all \code{scenarios} -(including \code{scen1}) for \code{regi1} -\item \verb{scen1,regi1,cement,4} will lastly overwrite the value for the \code{scen1}, -\code{regi1} combination -} - -Replacements occure in this fixed order (\code{NA}/\code{NA}, \code{scenario}/\code{NA}, -\code{NA}/\code{region}, \code{scenario}/\code{region}). - -Lastly, output is filtered for \code{scenarios} and \code{regions}. - -For debugging and development, instead of modifying the .csv files in -\verb{sources/industry_subsectors_specific/} and interfering with production runs, -modify the calling code (e.g. \code{calcFEdemand.R}) to use \code{direct} data (entered -verbatim or loaded from somewhere else.) -} -\author{ -Michaja Pehl -} diff --git a/man/readADVANCE_WP2.Rd b/man/readADVANCE_WP2.Rd deleted file mode 100644 index d691e47f..00000000 --- a/man/readADVANCE_WP2.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readADVANCE_WP2.R -\name{readADVANCE_WP2} -\alias{readADVANCE_WP2} -\title{Read ADVANCE WP2 Data} -\usage{ -readADVANCE_WP2(subtype) -} -\arguments{ -\item{subtype}{One of -\itemize{ -\item \code{clinker-to-cement-ratio} for the clinker-to-cement ratios from figure 21 -of Edelenbosch, O. \emph{Enhancing the representation of energy demand -developments in IAM models - A Modeling Guide for the Cement Industry} -(2015) \url{zotero://select/items/JP8X2QFK} -}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read ADVANCE WP2 Data -} -\seealso{ -\code{\link[=readSource]{readSource()}}, \code{\link[=convertADVANCE_WP2]{convertADVANCE_WP2()}} -} -\author{ -Michaja Pehl -} diff --git a/man/readMueller.Rd b/man/readMueller.Rd deleted file mode 100644 index fca03d91..00000000 --- a/man/readMueller.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readMueller.R -\name{readMueller} -\alias{readMueller} -\title{Read Müller et al. 2013 data.} -\usage{ -readMueller(subtype) -} -\arguments{ -\item{subtype}{One of: -\itemize{ -\item \code{countries}: read table mapping country names use by Müller et al. 2013 -to ISO 3166-1 alpha-3 codes. -\item \code{stocks}: read low/medium/high estimates of per-capita steel stocks from -Müller et al. 2013 SI2 -}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read data from Müller et al. 2013 (http://dx.doi.org/10.1021/es402618m). -} -\seealso{ -\code{\link[=readSource]{readSource()}} -} -\author{ -Michaja Pehl -} diff --git a/man/readPauliuk.Rd b/man/readPauliuk.Rd deleted file mode 100644 index 73da1480..00000000 --- a/man/readPauliuk.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readPauliuk.R -\name{readPauliuk} -\alias{readPauliuk} -\title{Read Pauliuk et al. 2013 data} -\usage{ -readPauliuk(subtype = "lifetime") -} -\arguments{ -\item{subtype}{One of: -\itemize{ -\item \code{lifetime}: Read estimated lifetime of overall steel stocks (approach b) -in years. -}} -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read data from Pauliuk et al. 2013 -(https://dx.doi.org/10.1016/j.resconrec.2012.11.008). -} -\seealso{ -\code{\link[=readSource]{readSource()}} -} -\author{ -Michaja Pehl -} diff --git a/man/readStegmann2022.Rd b/man/readStegmann2022.Rd deleted file mode 100644 index 7618e6a8..00000000 --- a/man/readStegmann2022.Rd +++ /dev/null @@ -1,32 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readStegmann2022.R -\name{readStegmann2022} -\alias{readStegmann2022} -\title{Read PlasticsEoL} -\usage{ -readStegmann2022() -} -\value{ -magpie object of the data -} -\description{ -Read-in data for the End-of-Life fate of plastics -from 1.Stegmann, P., Daioglou, V., Londo, M., van Vuuren, -D. P. & Junginger, M. Plastic futures and their CO2 emissions. -Nature 612, 272–276 (2022). -https://www.nature.com/articles/s41586-022-05422-5 -Link to SI: -https://static-content.springer.com/esm/art\%3A10.1038\%2Fs41586-022-05422-5/MediaObjects/41586_2022_5422_MOESM1_ESM.xlsx #nolint -} -\examples{ -\dontrun{ -a <- readSource(type = "Stegmann2022") -} - -} -\seealso{ -\code{\link{readSource}} -} -\author{ -Falk Benke, Simón Moreno -} diff --git a/man/readnonEnergyDemand.Rd b/man/readnonEnergyDemand.Rd deleted file mode 100644 index feac7581..00000000 --- a/man/readnonEnergyDemand.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readnonEnergyDemand.R -\name{readnonEnergyDemand} -\alias{readnonEnergyDemand} -\title{Read Final energy demand for feedstocks (non-energy use)} -\usage{ -readnonEnergyDemand() -} -\value{ -magpie object of region dependent data -} -\description{ -Read Final energy demand for feedstocks (non-energy use) -} -\examples{ -\dontrun{ -a <- readSource(type = "nonEnergyDemand") -} - -} -\seealso{ -\code{\link{readSource}} -} -\author{ -Renato Rodrigues -} diff --git a/man/readvanRuijven2016.Rd b/man/readvanRuijven2016.Rd deleted file mode 100644 index e14fa960..00000000 --- a/man/readvanRuijven2016.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readvanRuijven2016.R -\name{readvanRuijven2016} -\alias{readvanRuijven2016} -\title{Read van Ruijven et al. (2016) data.} -\usage{ -readvanRuijven2016() -} -\value{ -A \code{\link[magclass:magclass-package]{magpie}} object. -} -\description{ -Read data from van Ruijven et al. 2016, -(http://dx.doi.org/10.1016/j.resconrec.2016.04.016, -https://www.zotero.org/groups/52011/rd3/items/itemKey/6QMNBEHQ), obtained -through personal communication (e-mail to Michaja Pehl). Units are tonnes -per year. -} -\seealso{ -\code{\link[=readSource]{readSource()}} -} -\author{ -Michaja Pehl -} diff --git a/man/readworldsteel.Rd b/man/readworldsteel.Rd deleted file mode 100644 index 6a294a16..00000000 --- a/man/readworldsteel.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readworldsteel.R -\name{readworldsteel} -\alias{readworldsteel} -\title{Read World Steel Statistical Yearbook Data} -\usage{ -readworldsteel(subtype = "detailed") -} -\arguments{ -\item{subtype}{One of -- `detailed` returning data for the worksheets - - `Pig Iron Production` - - `DRI Production` - - `Total Production of Crude Steel` - - `Production in Oxygen-Blown Converters` - - `Production in Open Hearth Furnaces` - - `Production in Electric Arc Furnaces` - - `Apparent Steel Use (Crude Steel Equivalent)` - from 1991 on or -- `long` returning total production data from 1967 on} -} -\value{ -A [`magpie`][magclass::magclass] object. -} -\description{ -Read combined data of World Steel Association statistical yearbooks -(https://www.worldsteel.org/steel-by-topic/statistics/steel-statistical-yearbook.html). -} -\seealso{ -[`readSource()`] -} -\author{ -Michaja Pehl -} diff --git a/man/tool_expand_tibble.Rd b/man/tool_expand_tibble.Rd deleted file mode 100644 index 2a2c3a7e..00000000 --- a/man/tool_expand_tibble.Rd +++ /dev/null @@ -1,51 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tool_expand_tibble.R -\name{tool_expand_tibble} -\alias{tool_expand_tibble} -\title{Expand tibble across scenarios and regions with default values} -\usage{ -tool_expand_tibble(d, scenarios, regions, structure.columns = NULL) -} -\arguments{ -\item{d}{A data.frame with columns `scenario` and `region`.} - -\item{scenarios}{A character vector of scenario names.} - -\item{regions}{A character vector of region names.} - -\item{structure.columns}{A character vector of column names to be carried -along.} -} -\value{ -A `tibble`. -} -\description{ -The data.frame `d` is expanded in such a manner that all rows with `NA` in -either the `scenario` or `region` columns are extended to repeat for all -scenarios and regions listed in `scenarios` and `regions`. Rows with -specified scenarios and/or regions will overwrite extended ones. Regions are -expanded before scenarios. -} -\examples{ -\dontrun{ -tribble( - ~scenario, ~region, ~value, - NA, NA, 0, - NA, 'CHA', 1, - 'SSP1', NA, 2, - 'SSP2EU', 'DEU', 3) \%>\% - tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'), - regions = c('CHA', 'DEU', 'USA')) \%>\% - pivot_wider(names_from = 'region') - -tribble( - ~scenario, ~region, ~name, ~value, - NA, NA, 'A', 0, - NA, 'CHA', 'B', 1, - 'SSP1', NA, 'A', 2, - 'SSP2EU', 'DEU', 'B', 3) \%>\% - tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'), - regions = c('CHA', 'DEU', 'USA'), - structure.columns = 'name') -} -}