R/zchunk_LA154.Transport-korea.R

Defines functions module_gcam.korea_LA154.Transport

#' module_gcam.korea_LA154.Transport
#'
#' Downscale transportation energy consumption and nonmotor data to the state level, generating three ouput tables.
#'
#' @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_korea_trn_m_sz_tech_F}, \code{L154.out_mpkm_korea_trn_nonmotor_Yh}, \code{L154.in_EJ_korea_trn_F}. The corresponding file in the
#' original data system was \code{LA154.Transport.R} (gcam-korea level1).
#' @details Transportation energy data was downscaled in proportion to KEEI state-level transportation energy data
#' @details Transportation nonmotor data was downscaled in proportion to state population
#' @importFrom assertthat assert_that
#' @importFrom dplyr filter mutate select
#' @importFrom tidyr gather spread
#' @author S. Jeon
module_gcam.korea_LA154.Transport <- function(command, ...) {
  if(command == driver.DECLARE_INPUTS) {
    return(c(FILE = "gcam-korea/trnUCD_KEEI_mapping",
             FILE = "gcam-korea/regional_trn_road_mode_share",
             "L154.in_EJ_R_trn_m_sz_tech_F_Yh",
             "L154.out_mpkm_R_trn_nonmotor_Yh",
             "L100.Pop_thous_korea",
             "L101.KEEI_use_all_KTOE"))
  } else if(command == driver.DECLARE_OUTPUTS) {
    return(c("L154.in_EJ_korea_trn_m_sz_tech_F",
             "L154.out_mpkm_korea_trn_nonmotor_Yh",
             "L154.in_EJ_korea_trn_F"))
  } else if(command == driver.MAKE) {

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

    # Load required inputs
    trnUCD_KEEI_mapping <- get_data(all_data, "gcam-korea/trnUCD_KEEI_mapping")
    regional_trn_road_mode_share <- get_data(all_data, "gcam-korea/regional_trn_road_mode_share")

    L154.in_EJ_R_trn_m_sz_tech_F_Yh <- get_data(all_data, "L154.in_EJ_R_trn_m_sz_tech_F_Yh")
    L154.out_mpkm_R_trn_nonmotor_Yh <- get_data(all_data, "L154.out_mpkm_R_trn_nonmotor_Yh")
    L100.Pop_thous_korea <- get_data(all_data, "L100.Pop_thous_korea")
    L101.KEEI_use_all_KTOE <- get_data(all_data, "L101.KEEI_use_all_KTOE")

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

      # Silence package notes
      GCAM_region_ID <- UCD_sector <- mode <- size.class <- UCD_technology <- UCD_fuel <- fuel <- KEEI_fuel <-
        year <- value <- KEEI_sector <- . <- fuel_sector <- state <- sector <- value_state <- value_national <-
        value_share <- value_mode <- NULL

      # Calculate the state-wise percentages for each of KEEI's sector/fuel combinations that is relevant for disaggregating
      # nation-level transportation energy to the states

      # This starting table is transportation energy consumption by GCAM region (and other variables)
      # We will first subset this data for only the USA and values that are > 0 in the historical periods
        # Nonzero data are filtered out because it adds no useful information. By doing so, the number of rows of the tibble
        # will be reduced from 1840 to 800, and this is before expanding the table to apportion to states.
      L154.in_EJ_R_trn_m_sz_tech_F_Yh %>%
        filter(year %in% HISTORICAL_YEARS, GCAM_region_ID == gcamkorea.RegionNum) %>% # Filter for the USA and for historical years only
        filter(value != 0) %>% # Here any rows with value of 0 will be lost, even if other years of the same group are nonzero
        # We will next reintroduce those rows using "complete" and assign those values to be 0
        complete(nesting(GCAM_region_ID, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, fuel), year = HISTORICAL_YEARS, fill = list(value = 0)) %>%
        # Fuel and mode will be mapped to KEEI fuel and sector
        left_join_error_no_match(trnUCD_KEEI_mapping, by = c("fuel", "mode")) ->
        Transportation_energy_consumption

      # From the full state database, state shares will be calculated based on relevant KEEI sector and fuel combinations
      # These shares will later be multipled by the transportation energy consumption data above
      # We will create a list first, concatenating KEEI-fuel and -sector, so as to selectively remove those pairs from the dataset
      list_fuel_sector <- unique(paste(trnUCD_KEEI_mapping$KEEI_fuel, trnUCD_KEEI_mapping$KEEI_sector))

      # Here is the state-level data for which to calculate state shares
      # We will first filter for only relevant KEEI-fuel and -sector pairs
      L101.KEEI_use_all_KTOE %>%
        filter(year %in% HISTORICAL_YEARS) %>% # Ensure within historical period
        mutate(fuel_sector = paste(KEEI_fuel, KEEI_sector)) %>% # Create concatenated list in base dataframe to match the syntax of our list above
        filter(fuel_sector %in% list_fuel_sector) %>% # Filtering for just KEEI-fuel/sector pairs
        select(state, KEEI_fuel, KEEI_sector, sector, fuel, year, value_state = value) ->
        KEEI_transportation_state

      # To calculate the state share, we need to calculate the national amount
      KEEI_transportation_state %>%
        group_by(KEEI_fuel, KEEI_sector, sector, fuel, year) %>% # Dropping state
        summarise(value_national = sum(value_state)) %>%
        ungroup() ->
        KEEI_transportation_national

      # Now the state shares can be calculated by dividing the state data by the national
      KEEI_transportation_state %>%
        left_join_error_no_match(KEEI_transportation_national, by = c("KEEI_fuel", "KEEI_sector", "sector", "fuel", "year")) %>%
        mutate(value_share = value_state / value_national) %>% # Calculating state's share
        # NAs were introduced where national values were 0. Replace NAs with zeros.
        replace_na(list(value_share = 0)) %>%
        select(state, KEEI_fuel, KEEI_sector, year, value_share) ->
        KEEI_transportation_state_share

      # The full USA tran UCD database can now be apportioned to the states
      # A list of states is created for when expanding the transportation table to include states
      list_states <- unique(L100.Pop_thous_korea$state)

      ################################################################################################
      ## Written by Jeon ## Start
      ## Korea EB includes only 2010 data, which makes error in operating left_join_error_no_match.
      ## So select just 2010.
      Transportation_energy_consumption %>%
        filter(year == 2010) -> Transportation_energy_consumption

      ## Written by Jeon ## End
      ################################################################################################


      # Creating the first of the three output tables
      Transportation_energy_consumption %>%
        repeat_add_columns(tibble::tibble(state = list_states)) %>%
        left_join_error_no_match(KEEI_transportation_state_share, by = c("state", "KEEI_fuel", "KEEI_sector", "year")) %>%
        mutate(value = value * value_share) %>% # Allocating across the states
        select(state, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, fuel, year, value) ->
        L154.in_EJ_korea_trn_m_sz_tech_F

      ################################################################################################
      ## Written by Jeon ## Start

      target_mode <- c("Truck", "Bus", "LDV_4W")

      L154.in_EJ_korea_trn_m_sz_tech_F %>% filter(c(mode %in% target_mode) & c(fuel != "gas")) -> target_data
      target_data %>% group_by(state) %>% summarize(value = sum(value)) -> target_data_agg
      # setwd("C:/Users/e/Desktop/Regionl Modeling paper/trn_modeling/identity data control")
      # source("C:/Users/e/Desktop/Regionl Modeling paper/trn_modeling/identity data control/trn_identity.R")
      regional_trn_road_mode_share %>% rename(state = region) -> trn_road_mode_share

      #trn_road_mode_share %>% left_join(target_data_agg) %>% mutate(new_value = share * value) ->
      #trn_road_mode_share %>% left_join(target_data_agg) %>% mutate(new_value = share * value) %>% mutate(UCD_sector = if_else(GCAM_mode=="Truck", "Freight", "Passenger"))

      trn_road_mode_share %>% left_join(target_data_agg) %>% mutate(new_value = share * value) %>% mutate(size.class = if_else(GCAM_mode=="Bus", "All", GCAM_mode)) -> new_data
      #target_data %>% left_join(new_data, by = c("state", "size.class"))

      target_data %>% left_join(new_data, by = c("state", "size.class")) %>% select( state, UCD_sector, mode, size.class, UCD_technology, UCD_fuel, fuel, year, new_value) %>% rename(value = new_value) -> L154.in_EJ_korea_trn_m_sz_tech_F_only_road_adj


      L154.in_EJ_korea_trn_m_sz_tech_F %>% filter(!c(c(mode %in% target_mode) & c(fuel != "gas"))) -> L154.in_EJ_korea_trn_m_sz_tech_F_without_target_data


      L154.in_EJ_korea_trn_m_sz_tech_F_without_target_data %>% bind_rows(L154.in_EJ_korea_trn_m_sz_tech_F_only_road_adj) -> L154.in_EJ_korea_trn_m_sz_tech_F

      ## Written by Jeon ## End
      ################################################################################################








      # As a final step, aggregate by fuel and name the sector
      # This creates the second of three output tables
      L154.in_EJ_korea_trn_m_sz_tech_F %>%
        group_by(state, fuel, year) %>%
        summarise(value = sum(value)) %>%
        ungroup() %>%
        mutate(sector = "transportation") %>% # Adding a column named "sector" with "transportation" as the entries
        select(state, sector, fuel, year, value) ->
        L154.in_EJ_korea_trn_F

      # Apportion non-motorized energy consumption to states on the basis of population
      # First we will create the state shares based on population
      L100.Pop_thous_korea %>%
        group_by(year) %>%
        summarise(value_national = sum(value)) ->
        Pop_national

      L100.Pop_thous_korea %>%
        left_join_error_no_match(Pop_national, by = "year") %>%
        mutate(value_share = value / value_national) %>% # Creating state share based on population
        select(state, year, value_share) ->
        Pop_state_share

      # Now we can use these shares to allocate the national data across the states
      L154.out_mpkm_R_trn_nonmotor_Yh %>%
        rename(value_mode = value) %>%
        filter(GCAM_region_ID == gcamkorea.RegionNum) %>%
        # Number of rows will change by adding states, so left_join_error_no_match cannot be used
        left_join(Pop_state_share, by = "year") %>%
        mutate(value = value_mode * value_share) %>% # Apportioning across the modes using the share data
        filter(year %in% HISTORICAL_YEARS) %>% # Ensuring within historical period
        select(state, mode, year, value) %>%
        mutate(year = as.integer(year)) ->
        L154.out_mpkm_korea_trn_nonmotor_Yh

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

    L154.in_EJ_korea_trn_m_sz_tech_F %>%
      add_title("Transportation energy consumption by state") %>%
      add_units("EJ") %>%
      add_comments("Transportation energy consumption data was downscaled to the state level using KEEI state energy data") %>%
      add_legacy_name("L154.in_EJ_korea_trn_m_sz_tech_F") %>%
      add_precursors("L154.in_EJ_R_trn_m_sz_tech_F_Yh", "gcam-korea/trnUCD_KEEI_mapping", "gcam-korea/regional_trn_road_mode_share", "L101.KEEI_use_all_KTOE") ->
      L154.in_EJ_korea_trn_m_sz_tech_F

    L154.out_mpkm_korea_trn_nonmotor_Yh %>%
      add_title("Transportation non-motorized travel by mode and state") %>%
      add_units("million person-km") %>%
      add_comments("National data was allocated across the states in proportion to population") %>%
      add_legacy_name("L154.out_mpkm_korea_trn_nonmotor_Yh") %>%
      add_precursors("L154.out_mpkm_R_trn_nonmotor_Yh", "L100.Pop_thous_korea")  ->
      L154.out_mpkm_korea_trn_nonmotor_Yh

    L154.in_EJ_korea_trn_F %>%
      add_title("Transportation energy consumption by state and fuel") %>%
      add_units("EJ") %>%
      add_comments("Transportation energy consumption was aggregated by fuel, and the sector was named transportation") %>%
      add_legacy_name("L154.in_EJ_korea_trn_F") %>%
      add_precursors("L154.in_EJ_R_trn_m_sz_tech_F_Yh", "gcam-korea/trnUCD_KEEI_mapping", "L101.KEEI_use_all_KTOE") ->
      L154.in_EJ_korea_trn_F

    return_data(L154.in_EJ_korea_trn_m_sz_tech_F, L154.out_mpkm_korea_trn_nonmotor_Yh, L154.in_EJ_korea_trn_F)
  } else {
    stop("Unknown command")
  }
}
rohmin9122/gcam-korea-release documentation built on Nov. 26, 2020, 8:11 a.m.