R/zchunk_L210.water.demand.primary.R

Defines functions module_water_L210.water.demand.primary

Documented in module_water_L210.water.demand.primary

#' module_water_L210.water.demand.primary
#'
#' Generate (water demand) technology coefficients for each GCAM region for base years and future years.
#'
#' @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{L210.TechCoef}. The corresponding file in the
#' original data system was \code{L210.water.demand.primary.R} (water level2).
#' @details This chunk generate (water demand) technology coefficients for each GCAM region for base years and future years,
#' with information of supplysector, subsector, technology, minicam.energy.input and market.name.
#' @importFrom assertthat assert_that
#' @importFrom dplyr filter mutate select
#' @importFrom tidyr gather spread
#' @author YL May 2017
module_water_L210.water.demand.primary <- function(command, ...) {
  if(command == driver.DECLARE_INPUTS) {
    return(c(FILE = "common/GCAM_region_names",
             FILE = "water/A03.sector",
             FILE = "energy/A21.globaltech_coef",
             FILE = "energy/A22.globaltech_coef",
             "L110.water_demand_primary_R_S_W_m3_GJ"))
  } else if(command == driver.DECLARE_OUTPUTS) {
    return(c("L210.TechCoef"))
  } else if(command == driver.MAKE) {

    supplysector <- subsector <- technology <- . <- water_sector <- water_type <- region <-
      year <- minicam.energy.input <- coefficient <- market.name <- NULL  # silence notes on package check

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

    # Load required inputs
    GCAM_region_names <- get_data(all_data, "common/GCAM_region_names")
    A03.sector <- get_data(all_data, "water/A03.sector")
    A21.globaltech_coef <- get_data(all_data, "energy/A21.globaltech_coef")
    A22.globaltech_coef <- get_data(all_data, "energy/A22.globaltech_coef")
    L110.water_demand_primary_R_S_W_m3_GJ <- get_data(all_data, "L110.water_demand_primary_R_S_W_m3_GJ")


    A21.globaltech_coef %>%
      select(supplysector,subsector,technology) ->
      A21.tmp1

    A22.globaltech_coef %>%
      filter(grepl("nuclear", supplysector)) %>%
      select(supplysector, subsector, technology) ->
      A22.tmp

    bind_rows(A21.tmp1, A22.tmp) %>%
      left_join(L110.water_demand_primary_R_S_W_m3_GJ, ., by = "supplysector") %>%

      # Avoid double acounting unconventional oil in the regional oil sector as it is already
      # accounted for in unconventional oil production.
      filter(!(supplysector %in% "regional oil" & subsector %in% "unconventional oil")) %>%
      mutate(water_sector = "Mining",
             minicam.energy.input = set_water_input_name(water_sector, water_type, A03.sector)) %>%
      # Add in GCAM region names
      left_join_error_no_match(GCAM_region_names, by = "GCAM_region_ID") %>%
      mutate(market.name = region) ->
      L210.TechCoef

    # Repeat coefficients across all model years
    tibble(year = MODEL_YEARS, MERGEFIELD = 1) %>%
      left_join(mutate(L210.TechCoef, MERGEFIELD = 1), by = "MERGEFIELD") %>%
      select(region, supplysector, subsector, technology, year, minicam.energy.input, coefficient, market.name) %>%

      # Produce outputs
      add_title("Global water demand technology coefficients") %>%
      add_units("m3/GJ") %>%
      add_comments("Data is generated by first combining A21.globaltech_coef and rows with 'nuclear' in supplysector from A22.globaltech_coef") %>%
      add_comments("Avoid double acounting unconventional oil in the regional oil sector, and then set the (water demand) coefficient") %>%
      add_comments("for each year, region, supplysector, subsector, technology, minicam.energy.input and market") %>%
      add_legacy_name("L210.TechCoef") %>%
      add_precursors("common/GCAM_region_names", "water/A03.sector", "energy/A21.globaltech_coef",
                     "energy/A22.globaltech_coef", "L110.water_demand_primary_R_S_W_m3_GJ") ->
      L210.TechCoef

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