R/zchunk_LA154.transportation_UCD.R

Defines functions module_energy_LA154.transportation_UCD

Documented in module_energy_LA154.transportation_UCD

#' module_energy_LA154.transportation_UCD
#'
#' Generates transportation energy and other data using UCD transportation database and IEA data.
#'
#' @param command API command to execute
#' @param ... other optional parameters, depending on command
#' @return Depends on \code{command}: either a vector of required inputs,
#' a vector of output names, or (if \code{command} is "MAKE") all
#' the generated outputs: \code{L154.in_EJ_R_trn_m_sz_tech_F_Yh}, \code{L154.in_EJ_ctry_trn_m_sz_tech_F}, \code{L154.intensity_MJvkm_R_trn_m_sz_tech_F_Y}, \code{L154.loadfactor_R_trn_m_sz_tech_F_Y}, \code{L154.cost_usdvkm_R_trn_m_sz_tech_F_Y}, \code{L154.speed_kmhr_R_trn_m_sz_tech_F_Y}, \code{L154.out_mpkm_R_trn_nonmotor_Yh}. The corresponding file in the
#' original data system was \code{LA154.transportation_UCD.R} (energy level1).
#' @details L154.in_EJ_R_trn_m_sz_tech_F_Yh and L154.in_EJ_ctry_trn_m_sz_tech_F generated by aggregating IEA data UCD
#' transportation technologies, then scaling to transportation enduse data. Other outputs generated by aggregating UCD data
#' to GCAM regions.
#' @importFrom assertthat assert_that
#' @importFrom dplyr filter mutate select
#' @importFrom tidyr gather spread
#' @author RH May 2017
module_energy_LA154.transportation_UCD <- function(command, ...) {

  UCD_trn_data_name <- paste0("UCD_trn_data_", energy.TRN_SSP)

  if(command == driver.DECLARE_INPUTS) {
    return(c(FILE = "common/iso_GCAM_regID",
             FILE = "energy/mappings/calibrated_techs_trn_agg",
             FILE = "energy/enduse_fuel_aggregation",
             FILE = "energy/mappings/UCD_ctry",
             FILE = "energy/mappings/UCD_techs",
             # This file is currently using a constant to select the correct SSP database
             # All SSP databases will be included in the input files
             UCD_trn_data_name,
             "L101.in_EJ_ctry_trn_Fi_Yh",
             "L1011.in_EJ_ctry_intlship_TOT_Yh",
             "L131.in_EJ_R_Senduse_F_Yh",
             "L100.Pop_thous_ctry_Yh"))
  } else if(command == driver.DECLARE_OUTPUTS) {
    return(c("L154.in_EJ_R_trn_m_sz_tech_F_Yh",
             "L154.in_EJ_ctry_trn_m_sz_tech_F",
             "L154.intensity_MJvkm_R_trn_m_sz_tech_F_Y",
             "L154.loadfactor_R_trn_m_sz_tech_F_Y",
             "L154.cost_usdvkm_R_trn_m_sz_tech_F_Y",
             "L154.speed_kmhr_R_trn_m_sz_tech_F_Y",
             "L154.out_mpkm_R_trn_nonmotor_Yh"))
  } else if(command == driver.MAKE) {

    ## silence package check.
    year <- value <- sector <- fuel <- EIA_value <- iso <- UCD_category <- variable <-
      UCD_region <- agg <- UCD_region.x <- UCD_region.y <- UCD_sector <- size.class <-
      UCD_technology <- UCD_fuel <- UCD_share <- GCAM_region_ID <- trn <- unscaled_value <-
      scaled_value <- unit <- vkt_veh_yr <- speed <- speed.x <- speed.y <- weight_EJ <-
      intensity <- Tvkm <- `load factor` <- `non-fuel costs` <- size.class.x <- Tpkm <-
      Tusd <- Thr <- intensity_MJvkm <- loadfactor <- cost_usdvkm <- speed_kmhr <- variable  <-
      population <- pkm_percap <- country_name <- year.x <- NULL

    all_data <- list(...)[[1]]

    # Load required inputs
    iso_GCAM_regID <- get_data(all_data, "common/iso_GCAM_regID")
    calibrated_techs_trn_agg <- get_data(all_data, "energy/mappings/calibrated_techs_trn_agg")
    enduse_fuel_aggregation <- get_data(all_data, "energy/enduse_fuel_aggregation")
    UCD_ctry <- get_data(all_data, "energy/mappings/UCD_ctry")
    UCD_techs <- get_data(all_data, "energy/mappings/UCD_techs")
    UCD_trn_data <- get_data(all_data, UCD_trn_data_name) %>%
      gather_years
    L101.in_EJ_ctry_trn_Fi_Yh <- get_data(all_data, "L101.in_EJ_ctry_trn_Fi_Yh")
    L1011.in_EJ_ctry_intlship_TOT_Yh <- get_data(all_data, "L1011.in_EJ_ctry_intlship_TOT_Yh")
    L131.in_EJ_R_Senduse_F_Yh <- get_data(all_data, "L131.in_EJ_R_Senduse_F_Yh")
    L100.Pop_thous_ctry_Yh <- get_data(all_data, "L100.Pop_thous_ctry_Yh")
    # ===================================================
    # Part 1: downscaling country-level transportation energy data to UCD transportation technologies, then scaling to transportation
    # enduse data.

    # NOTE: We are currently aggregating IEA's data on rail and road due to inconsistencies (e.g. no rail in the Middle East)
    # First, replace the international shipping data (swapping in EIA for IEA)
    # Only perform this swap for international shipping / refined liquids, and in countries in the EIA database
    IEA_data_EIA_intlship <- L101.in_EJ_ctry_trn_Fi_Yh %>%
      # expecting NAs here because we only want to replace certain values
      left_join_keep_first_only(L1011.in_EJ_ctry_intlship_TOT_Yh %>% rename(EIA_value = value), by = c("iso", "year")) %>%
      mutate(value = if_else(sector == "in_trn_international ship" &
                               fuel == "refined liquids" &
                               !is.na(EIA_value), EIA_value, value),
             sector = sub("in_", "", sector)) %>%
      select(iso, sector, fuel, year, value)

    # Need to map IEA sector to UCD_category, calibrated_techs_trn_agg data is too busy
    UCD_category_mapping <- calibrated_techs_trn_agg %>% select(sector, UCD_category) %>% distinct

    # Aggregate IEA data to UCD_category in each country/year instead of sector
    IEA_data_aggregated_by_UCD_cat <- IEA_data_EIA_intlship %>%
      left_join_error_no_match(UCD_category_mapping, by = "sector") %>%
      group_by(iso, UCD_category, fuel, year) %>%
      summarise(value = sum(value)) %>%
      ungroup()

    # Aggregating UCD transportation database by the general categories used for the IEA transportation data
    # These will be used to compute shares for allocation of energy to mode/technology/fuel within category/fuel
    UCD_trn_data_UCD_techs <- UCD_trn_data %>%
      filter(variable == "energy") %>%
      left_join_error_no_match(UCD_techs, by = c("UCD_sector", "mode", "size.class", "UCD_technology", "UCD_fuel"))

    UCD_trn_data_UCD_cat <- UCD_trn_data_UCD_techs %>%
      # Filtering only to base year for computing shares
      filter(year == energy.UCD_EN_YEAR) %>%
      group_by(UCD_region, UCD_category, fuel) %>%
      summarise(agg = sum(value)) %>%
      ungroup()

    # Match these energy quantities back into the complete table for computation of shares of fuel in category
    UCD_trn_data_UCD_techs <- UCD_trn_data_UCD_techs %>%
      filter(year == energy.UCD_EN_YEAR) %>%
      left_join_error_no_match(UCD_trn_data_UCD_cat, by = c("UCD_region", "UCD_category", "fuel")) %>%
      # If the aggregate is 0 or value is NA, set share to 0, rather than NA
      mutate(UCD_share =  value / agg) %>%
      replace_na(list(UCD_share = 0))

    # Writing out the UC Davis mode/technology/fuel shares within category/fuel at the country level
    # First, creating a table of desired countries with their UCD regions
    ctry_iso_region <- tibble(iso = unique(L101.in_EJ_ctry_trn_Fi_Yh$iso)) %>%
      left_join_error_no_match(UCD_ctry, by = "iso")

    UCD_fuel_share_in_cat <- UCD_trn_data_UCD_techs %>%
      # Adds country name and region for all observations, filtering out by matching region in next step
      repeat_add_columns(ctry_iso_region) %>%
      filter(UCD_region.x == UCD_region.y) %>%
      select(iso, UCD_sector, mode, size.class, UCD_technology,
             UCD_fuel, UCD_category, fuel, UCD_share)

    # Multiplying historical energy by country/category/fuel times the shares of country/mode/tech/fuel within country/category/fuel
    # Need a value for each iso, year, UCD category, and fuel combo, even if not currently in L154.in_EJ_ctry_trn_Fi_Yh
    UCD_cat_fuel <- UCD_fuel_share_in_cat %>%
      select(UCD_category, fuel) %>%
      distinct
    iso_year <- IEA_data_aggregated_by_UCD_cat %>%
      ungroup %>%
      select(iso, year) %>%
      distinct

    IEA_hist_data_times_UCD_shares <- UCD_cat_fuel %>%
      repeat_add_columns(iso_year) %>%
      left_join(UCD_fuel_share_in_cat, by = c("UCD_category", "fuel", "iso")) %>%
      fast_left_join(IEA_data_aggregated_by_UCD_cat, by = c("UCD_category", "fuel", "iso", "year")) %>%
      fast_left_join(iso_GCAM_regID %>% select(iso, GCAM_region_ID), by = "iso") %>%
      # Multiply value by share. Set missing values to 0. These are combinations not available in the data from IEA.
      replace_na(list(value = 0)) %>%
      mutate(value = value * UCD_share) %>%
      select(iso, UCD_sector, mode, size.class, UCD_technology,
             UCD_fuel, UCD_category, fuel, GCAM_region_ID, year, value)

    # Aggregating by GCAM region
    IEA_hist_data_times_UCD_shares_region <- IEA_hist_data_times_UCD_shares %>%
      group_by(GCAM_region_ID, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, fuel, year) %>%
      summarise(value = sum(value)) %>%
      ungroup()

    # Aggregating by fuel to calculate scalers
    IEA_UCD_shared_region_fuel_sum <- IEA_hist_data_times_UCD_shares_region %>%
      group_by(GCAM_region_ID, fuel, year) %>%
      summarise(unscaled_value = sum(value)) %>%
      ungroup()

    trn_enduse_data <- L131.in_EJ_R_Senduse_F_Yh %>%
      # Filtering out transportation sectors only
      filter(grepl("trn", sector)) %>%
      # Need to match "aggregate" fuels from IEA
      left_join_error_no_match(enduse_fuel_aggregation %>% select(fuel, trn), by = c("fuel")) %>%
      select(-fuel, fuel = trn)

    trn_enduse_data_fuel_aggregated <- trn_enduse_data %>%
      group_by(GCAM_region_ID, fuel, year) %>%
      summarise(value = sum(value)) %>%
      ungroup()

    IEA_UCD_shared_scaled_to_enduse_data <- IEA_UCD_shared_region_fuel_sum %>%
      # Keep NAs and then set to zero
      left_join(trn_enduse_data_fuel_aggregated, by = c("GCAM_region_ID", "fuel", "year")) %>%
      mutate(scaled_value = value / unscaled_value) %>%
      replace_na(list(scaled_value = 0)) %>%
      select(-unscaled_value, -value)

    # Multiplying scalers by original estimates
    IEA_hist_data_times_UCD_shares_region_scaled_to_enduse_data <- IEA_hist_data_times_UCD_shares_region %>%
      left_join(IEA_UCD_shared_scaled_to_enduse_data, by = c("GCAM_region_ID", "fuel", "year")) %>%
      mutate(value = value * scaled_value) %>%
      # Energy is being dropped due to zeroes in the UCD database. Might want to add new techs to the UC Davis database
      replace_na(list(value = 0)) %>%
      select(-scaled_value)

    # Part 2: Downscaling of parameters in the UCD database to the country level
    # 2a: Merging of non-fuel costs to assign each technology with a single cost per vkm
    # Exogenous fixed charge rate to convert $/veh to $/veh/yr
    fcr_veh <- energy.DISCOUNT_RATE_VEH +
      energy.DISCOUNT_RATE_VEH / (((1 + energy.DISCOUNT_RATE_VEH) ^ energy.NPER_AMORT_VEH) - 1)

    UCD_trn_data_vkm_veh <- UCD_trn_data %>%
      filter(variable == "annual travel per vehicle") %>%
      # Dropping UCD technology and UCD fuel because they are "All"
      select(UCD_region, UCD_sector, mode, size.class, year, vkt_veh_yr = value)

    UCD_trn_cost_data <- UCD_trn_data %>%
      filter(grepl("\\$", unit)) %>%
      # Use the fixed charge rate to convert to $/veh/yr
      mutate(value = if_else(unit == "2005$/veh", value * fcr_veh, value),
             unit = if_else(unit == "2005$/veh", "2005$/veh/yr", unit)) %>%
      # Match in the number of km per vehicle per year in order to calculate a levelized cost (per vkm)
      left_join(UCD_trn_data_vkm_veh, by = c("UCD_region", "UCD_sector", "mode", "size.class", "year")) %>%
      mutate(value = if_else(unit == "2005$/veh/yr", value / vkt_veh_yr, value),
             unit = if_else(unit == "2005$/veh/yr", "2005$/vkt", unit)) %>%
      group_by(UCD_region, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, unit, year) %>%
      summarise(value = sum(value)) %>%
      ungroup() %>%
      mutate(variable = "non-fuel costs")

    # Creating tibble with all GCAM years to join with. The values will be filled out using the first available year.
    # Remove years in all GCAM years that are already in UCD database
    all_years <- tibble( year = c(HISTORICAL_YEARS, FUTURE_YEARS)) %>%
      filter(!(year %in% unique(UCD_trn_data$year)))

    UCD_trn_data_allyears <- bind_rows(
      filter( UCD_trn_data, variable %in% c("intensity", "load factor", "speed")),
      UCD_trn_cost_data) %>%
      select(-year, -value) %>%
      distinct() %>%
      repeat_add_columns(all_years) %>%
      mutate(value = NA)

    UCD_trn_data_fillout <- bind_rows(
      filter( UCD_trn_data, variable %in% c("intensity", "load factor", "speed")),
      UCD_trn_cost_data,
      UCD_trn_data_allyears) %>%
      # Fill out all missing values with the nearest available year that is not missing
      group_by(UCD_region, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, variable, unit) %>%
      arrange(UCD_region, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, variable, unit, year) %>%
      mutate(value = if_else(is.na(value), approx_fun(year, value, rule = 2), value)) %>%
      ungroup()

    # Aggregate the country-level energy consumption by sector and mode. First need to add in the future years for matching purposes
    IEA_fut_data_times_UCD_shares <- IEA_hist_data_times_UCD_shares %>%
      select(-year, -value) %>%
      distinct() %>%
      repeat_add_columns(tibble(year = FUTURE_YEARS, value = NA))

    IEA_histfut_data_times_UCD_shares <- IEA_hist_data_times_UCD_shares %>%
      bind_rows(IEA_fut_data_times_UCD_shares) %>%
      group_by(iso, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, UCD_category, fuel) %>%
      mutate(value = if_else(is.na(value), approx_fun(year, value, rule = 2), value)) %>%
      ungroup()

    IEA_data_times_UCD_shares_UCD_sector_agg <- IEA_histfut_data_times_UCD_shares %>%
      group_by(iso, UCD_sector, mode, year) %>%
      summarise(value = sum(value)) %>%
      ungroup()

    # Spreading by variable to join all at once
    UCD_trn_data_variable_spread <- UCD_trn_data_fillout %>%
      ungroup %>%
      distinct() %>%
      select(-unit) %>%
      spread(variable, value)

    ALL_ctry_var <- IEA_histfut_data_times_UCD_shares %>%
      left_join_error_no_match(UCD_ctry, by = "iso") %>%
      # The energy weights will be replaced by the energy weights of each mode, as many techs have 0 consumption in the base year
      select(-value) %>%
      fast_left_join(IEA_data_times_UCD_shares_UCD_sector_agg,
                     by = c("iso", "UCD_sector", "mode", "year")) %>%
      # Using a floor on the weighting factor to avoid having zero weights for any countries
      mutate(weight_EJ = pmax(value, energy.MIN_WEIGHT_EJ)) %>%
      # Next, match in the derived variables, specific to each individual country/sector/mode/size.class/tech/fuel, except speed
      # There will be NA non-fuel costs which can be set to zero
      fast_left_join(UCD_trn_data_variable_spread %>% select(-speed),
                     by = c("UCD_sector", "mode", "size.class", "UCD_technology", "UCD_fuel", "year", "UCD_region")) %>%
      replace_na(list(`non-fuel costs` = 0))

    # Adding in speed - this is matched by the mode and (for some) size class. Match size class first
    speed_data <- UCD_trn_data_variable_spread %>%
      select(UCD_sector, mode, size.class, year, UCD_region, speed) %>%
      filter(!is.na(speed))

    ALL_ctry_var <- ALL_ctry_var %>%
      fast_left_join(speed_data,
                     by = c("UCD_sector", "mode", "size.class", "year", "UCD_region")) %>%
      # For the missing values, join using the mode ID
      left_join_keep_first_only(speed_data,
                                by = c("UCD_sector", "mode", "year", "UCD_region")) %>%
      mutate(speed.x = if_else(is.na(speed.x), speed.y, speed.x)) %>%
      replace_na(list(speed.x = 1))

    # Calculate the weighted volumes. No need to convert units because these will be aggregated and divided by weights
    ALL_region_var <- ALL_ctry_var %>%
      mutate(Tvkm = weight_EJ / intensity,
             Tpkm = Tvkm * `load factor`,
             Tusd = Tvkm * `non-fuel costs`,
             Thr = Tvkm / speed.x) %>%
      group_by(UCD_technology,UCD_fuel, UCD_sector, mode, size.class.x, year, GCAM_region_ID) %>%
      summarise(weight_EJ = sum(weight_EJ), Tvkm = sum(Tvkm), Tpkm = sum(Tpkm),Tusd = sum(Tusd), Thr = sum(Thr)) %>%
      ungroup()

    # Reverse the calculations to calculate the weighted average of each derived variable
    ALL_region_var <- ALL_region_var %>%
      mutate(intensity_MJvkm = weight_EJ / Tvkm,
             loadfactor = Tpkm / Tvkm,
             cost_usdvkm = Tusd / Tvkm,
             speed_kmhr = Tvkm / Thr) %>%
      # Dropping unnecessary columns
      select(-Tvkm, -Tpkm, -Tusd, -Thr, -weight_EJ) %>%
      gather(variable, value, intensity_MJvkm, loadfactor, cost_usdvkm, speed_kmhr) %>%
      # Reordering columns
      select(GCAM_region_ID, UCD_sector, mode, size.class = size.class.x, UCD_technology, UCD_fuel, variable, year, value)

    # Build the final data frames by variable
    out_var_df <- split(ALL_region_var, ALL_region_var$variable) %>%
      lapply(function(df) {select(df, -variable)})

    # Part 3: Downscaling of non-motorized transport to the country level, using population
    # Filtering population to UCD energy year and then aggregating by UCD region
    Pop_thous_UCD_R <- L100.Pop_thous_ctry_Yh %>%
      filter(year == energy.UCD_EN_YEAR) %>%
      left_join_error_no_match(UCD_ctry, by = "iso") %>%
      group_by(UCD_region) %>%
      summarise(population = sum(value)) %>%
      ungroup()

    # Calculating non-motorized passenger-km per person
    PKM_percap_nonmotor_UCD_R <- UCD_trn_data %>%
      filter(mode %in% c("Walk", "Cycle"), variable == "service output") %>%
      left_join_error_no_match(Pop_thous_UCD_R, by = "UCD_region") %>%
      mutate(pkm_percap = value * CONV_BIL_MIL * CONV_MIL_THOUS / population) %>%
      select(UCD_region, mode, year, pkm_percap)

    # Compute the nonmotorized service output at the country level, using the historical population
    PKM_nonmotor_ctry <- L100.Pop_thous_ctry_Yh %>%
      repeat_add_columns(tibble(mode = c("Walk", "Cycle"))) %>%
      left_join_error_no_match(UCD_ctry %>% select(-country_name), by = "iso") %>%
      left_join_error_no_match(PKM_percap_nonmotor_UCD_R %>% filter(year == energy.UCD_EN_YEAR),
                               by = c("UCD_region", "mode")) %>%
      mutate(value = value * 1 / CONV_MIL_THOUS * pkm_percap)

    # Aggregate by GCAM region and write it out
    PKM_nonmotor_GCAM_R <- PKM_nonmotor_ctry %>%
      left_join_error_no_match(iso_GCAM_regID, by = "iso") %>%
      group_by(GCAM_region_ID, mode, year = year.x) %>%
      summarise(value = sum(value)) %>%
      ungroup() %>%
      filter(year %in% HISTORICAL_YEARS)

    # ===================================================

    # Produce outputs
    IEA_hist_data_times_UCD_shares_region_scaled_to_enduse_data %>%
      add_title("Regional transportation energy data at UCD transportation technology level") %>%
      add_units("EJ") %>%
      add_comments("Aggregated country-level transportation energy data to UCD transportation technologies") %>%
      add_comments("Scaled to transport end-use data") %>%
      add_legacy_name("L154.in_EJ_R_trn_m_sz_tech_F_Yh") %>%
      add_precursors("common/iso_GCAM_regID", "L101.in_EJ_ctry_trn_Fi_Yh",
                     "L1011.in_EJ_ctry_intlship_TOT_Yh", "L131.in_EJ_R_Senduse_F_Yh",
                     "energy/mappings/calibrated_techs_trn_agg", "energy/enduse_fuel_aggregation",
                     "energy/mappings/UCD_ctry", "energy/mappings/UCD_techs",
                     UCD_trn_data_name) ->
      L154.in_EJ_R_trn_m_sz_tech_F_Yh

    IEA_hist_data_times_UCD_shares %>%
      add_title("Country-level transportation energy data at UCD transportation technology level") %>%
      add_units("EJ") %>%
      add_comments("Aggregated country-level transportation energy data to UCD transportation technologies") %>%
      add_legacy_name("L154.in_EJ_ctry_trn_m_sz_tech_F") %>%
      add_precursors("common/iso_GCAM_regID", "L101.in_EJ_ctry_trn_Fi_Yh",
                     UCD_trn_data_name, "energy/mappings/calibrated_techs_trn_agg",
                     "energy/mappings/UCD_ctry", "energy/mappings/UCD_techs",
                     "L1011.in_EJ_ctry_intlship_TOT_Yh") ->
      L154.in_EJ_ctry_trn_m_sz_tech_F

    out_var_df[["intensity_MJvkm"]] %>%
      add_title("Transportation energy intensity") %>%
      add_units("MJ/vkm") %>%
      add_comments("UCD transportation database data aggregated to GCAM region") %>%
      add_legacy_name("L154.intensity_MJvkm_R_trn_m_sz_tech_F_Y") %>%
      add_precursors(UCD_trn_data_name, "energy/mappings/UCD_ctry",
                     "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg", "energy/enduse_fuel_aggregation",
                     "energy/mappings/UCD_techs",
                     "L131.in_EJ_R_Senduse_F_Yh", "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg",
                     "energy/enduse_fuel_aggregation", "energy/mappings/UCD_techs",
                     "L101.in_EJ_ctry_trn_Fi_Yh", "L1011.in_EJ_ctry_intlship_TOT_Yh",
                     "L131.in_EJ_R_Senduse_F_Yh") ->
      L154.intensity_MJvkm_R_trn_m_sz_tech_F_Y

    out_var_df[["loadfactor"]] %>%
      add_title("Transortation load factors") %>%
      add_units("pers/veh or tonnes/veh") %>%
      add_comments("UCD transportation database data aggregated to GCAM region") %>%
      add_legacy_name("L154.loadfactor_R_trn_m_sz_tech_F_Y") %>%
      add_precursors(UCD_trn_data_name, "energy/mappings/UCD_ctry",
                     "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg", "energy/enduse_fuel_aggregation",
                     "energy/mappings/UCD_techs",
                     "L131.in_EJ_R_Senduse_F_Yh", "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg",
                     "energy/enduse_fuel_aggregation", "energy/mappings/UCD_techs",
                     "L101.in_EJ_ctry_trn_Fi_Yh", "L1011.in_EJ_ctry_intlship_TOT_Yh",
                     "L131.in_EJ_R_Senduse_F_Yh") ->
      L154.loadfactor_R_trn_m_sz_tech_F_Y

    out_var_df[["cost_usdvkm"]] %>%
      add_title("Transportation non-fuel costs") %>%
      add_units("2005USD/vkm") %>%
      add_comments("UCD transportation database data aggregated to GCAM region") %>%
      add_legacy_name("L154.cost_usdvkm_R_trn_m_sz_tech_F_Y") %>%
      add_precursors(UCD_trn_data_name, "energy/mappings/UCD_ctry",
                     "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg", "energy/enduse_fuel_aggregation",
                     "energy/mappings/UCD_techs",
                     "L131.in_EJ_R_Senduse_F_Yh", "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg",
                     "energy/enduse_fuel_aggregation", "energy/mappings/UCD_techs",
                     "L101.in_EJ_ctry_trn_Fi_Yh", "L1011.in_EJ_ctry_intlship_TOT_Yh",
                     "L131.in_EJ_R_Senduse_F_Yh") ->
      L154.cost_usdvkm_R_trn_m_sz_tech_F_Y

    out_var_df[["speed_kmhr"]] %>%
      add_title("Transportation vehicle speeds") %>%
      add_units("km/hr") %>%
      add_comments("UCD transportation database data aggregated to GCAM region") %>%
      add_legacy_name("L154.speed_kmhr_R_trn_m_sz_tech_F_Y") %>%
      add_precursors(UCD_trn_data_name, "energy/mappings/UCD_ctry",
                     "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg", "energy/enduse_fuel_aggregation",
                     "energy/mappings/UCD_techs",
                     "L131.in_EJ_R_Senduse_F_Yh", "common/iso_GCAM_regID", "energy/mappings/calibrated_techs_trn_agg",
                     "energy/enduse_fuel_aggregation", "energy/mappings/UCD_techs",
                     "L101.in_EJ_ctry_trn_Fi_Yh", "L1011.in_EJ_ctry_intlship_TOT_Yh",
                     "L131.in_EJ_R_Senduse_F_Yh") ->
      L154.speed_kmhr_R_trn_m_sz_tech_F_Y

    PKM_nonmotor_GCAM_R %>%
      add_title("Non-motor transportation service output") %>%
      add_units("Million passenger kilometers") %>%
      add_comments("UCD transportation data combined with population data") %>%
      add_legacy_name("L154.out_mpkm_R_trn_nonmotor_Yh") %>%
      add_precursors("common/iso_GCAM_regID", "energy/mappings/UCD_ctry",
                     UCD_trn_data_name,
                     "L100.Pop_thous_ctry_Yh") ->
      L154.out_mpkm_R_trn_nonmotor_Yh

    return_data(L154.in_EJ_R_trn_m_sz_tech_F_Yh, L154.in_EJ_ctry_trn_m_sz_tech_F,
                L154.intensity_MJvkm_R_trn_m_sz_tech_F_Y, L154.loadfactor_R_trn_m_sz_tech_F_Y,
                L154.cost_usdvkm_R_trn_m_sz_tech_F_Y, L154.speed_kmhr_R_trn_m_sz_tech_F_Y,
                L154.out_mpkm_R_trn_nonmotor_Yh)
  } else {
    stop("Unknown command")
  }
}
rohmin9122/gcam-korea-release documentation built on Nov. 26, 2020, 8:11 a.m.