R/zchunk_L210.water_demand_primary.R

Defines functions module_water_L210.water_demand_primary

Documented in module_water_L210.water_demand_primary

# Copyright 2019 Battelle Memorial Institute; see the LICENSE file.

#' 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 bind_rows filter left_join mutate select
#' @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/water_td_sectors",
             FILE = "energy/A21.globaltech_coef",
             FILE = "energy/A22.globaltech_coef",
             FILE = "energy/A_ff_RegionalTechnology",
             FILE = "energy/A_ff_TradedTechnology",
             "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")
    water_td_sectors <- get_data(all_data, "water/water_td_sectors")
    A21.globaltech_coef <- get_data(all_data, "energy/A21.globaltech_coef")
    A22.globaltech_coef <- get_data(all_data, "energy/A22.globaltech_coef")
    A_ff_RegionalTechnology <- get_data(all_data, "energy/A_ff_RegionalTechnology")
    A_ff_TradedTechnology <- get_data(all_data, "energy/A_ff_TradedTechnology")
    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

    A_ff_RegionalTechnology %>%
      select(supplysector,subsector,technology) ->
      A_ff_RegionalTechnology.tmp

    A_ff_TradedTechnology %>%
      select(supplysector,subsector,technology) ->
      A_ff_TradedTechnology.tmp

    bind_rows(A21.tmp1, A22.tmp,A_ff_RegionalTechnology.tmp,A_ff_TradedTechnology.tmp) %>%
      unique() %>%
      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.
      # Avoid double counting imported fuels as they are accounted for at the point of production
      filter(!grepl("traded", subsector),
             !(supplysector %in% "regional oil" & subsector %in% "unconventional oil")) %>%
      mutate(water_sector = "Mining",
             minicam.energy.input = set_water_input_name(water_sector, water_type, water_td_sectors)) %>%
      # Add in GCAM region names
      left_join_error_no_match(GCAM_region_names, by = "GCAM_region_ID") %>%
      mutate(market.name = region,
             subsector = if_else(grepl("traded", subsector), paste(region, subsector), subsector),
             technology = if_else(grepl("traded", technology), paste(region, technology), technology),
             region = if_else(grepl("traded", subsector), "USA", 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/water_td_sectors", "energy/A21.globaltech_coef",
                     "energy/A22.globaltech_coef", "energy/A_ff_RegionalTechnology","energy/A_ff_TradedTechnology",
                     "L110.water_demand_primary_R_S_W_m3_GJ") ->
      L210.TechCoef

    return_data(L210.TechCoef)
  } else {
    stop("Unknown command")
  }
}
JGCRI/gcamdata documentation built on March 21, 2023, 2:19 a.m.