assemble_phi_u_tables: Assemble completed phi (exergy-to-energy ratio) tables

View source: R/table_functions.R

assemble_phi_u_tablesR Documentation

Assemble completed phi (exergy-to-energy ratio) tables

Description

This function is used in the drake workflow to assemble completed phi (exergy-to-energy ratio) tables given a set of phi tables read from machine data files and a phi constants table. The algorithm gives priority in this order:

  1. phi values from the incomplete_phi_u_table argument

  2. phi values from climatic temperatures

  3. phi values from the phi_constants_table argument

Usage

assemble_phi_u_tables(
  incomplete_phi_u_table,
  phi_constants_table,
  completed_efficiency_table = NULL,
  countries,
  max_year = NULL,
  country = IEATools::iea_cols$country,
  year = IEATools::iea_cols$year,
  product = IEATools::iea_cols$product,
  machine = IEATools::template_cols$machine,
  quantity = IEATools::template_cols$quantity,
  phi_u = IEATools::template_cols$phi_u,
  .values = IEATools::template_cols$.values,
  eu_product = IEATools::template_cols$eu_product,
  eta_fu_source = IEATools::template_cols$eta_fu_source,
  phi_colname = IEATools::phi_constants_names$phi_colname,
  phi_source_colname = IEATools::phi_constants_names$phi_source_colname,
  is_useful = IEATools::phi_constants_names$is_useful_colname,
  eta_fu_tables = PFUWorkflow::phi_sources$eta_fu_tables,
  phi_constants = PFUWorkflow::phi_sources$phi_constants
)

Arguments

incomplete_phi_u_table

A data frame of phi values read from machine efficiency and phi data files. This data frame can be "incomplete," i.e., it can be missing phi values. The phi values from phi_constants_table will be used instead.

phi_constants_table

A data frame of constant phi values with reasonable default values for all energy products.

completed_efficiency_table

A data frame containing completed efficiency tables. This data frame identifies all useful products for which we need phi values. Default is NULL, meaning that missing (NA) values in incomplete_phi_u_table should be completed.

countries

A vector of countries for which completed phi tables are to be assembled.

max_year

The latest year for which analysis is desired. Default is NULL, meaning analyze all years.

country, year, product

See IEATools::iea_cols.

machine, quantity, phi_u, .values, eu_product, eta_fu_source

See IEATools::template_cols.

phi_colname, phi_source_colname, is_useful

See IEATools::phi_constants_names.

eta_fu_tables, phi_constants

See PFUWorkflow::phi_sources.

Details

Note that the needed phi values are taken from completed_efficiency_table (when not NULL). If completed_efficiency_table is NULL, the needed phi values are taken from incomplete_phi_u_table, meaning that any empty (NA) phi values are obtained from climatic temperatures or phi_constants_table.

Value

A data frame of phi values for every combination of country, year, machine, destination, etc.

Examples

library(dplyr)
library(IEATools)
library(magrittr)
phi_constants_table <- IEATools::load_phi_constants_table()
# Load a phi_u_table.
phi_table <- IEATools::load_eta_fu_data() %>%
  # Convert to tidy format.
  dplyr::mutate(
    "{IEATools::template_cols$maximum_values}" := NULL,
    "{IEATools::iea_cols$unit}" := NULL
  ) %>%
  tidyr::pivot_longer(cols = IEATools::year_cols(.),
                      names_to = IEATools::iea_cols$year,
                      values_to = IEATools::template_cols$.values) %>%
  # Convert to a table of phi values only
  dplyr::filter(.data[[IEATools::template_cols$quantity]] == IEATools::template_cols$phi_u)
# Set a value to NA (Charcoal stoves, MTH.100.C, GHA, 1971) in the phi table.
  incomplete_phi_table <- phi_table %>%
    dplyr::mutate(
      "{IEATools::template_cols$.values}" := dplyr::case_when(
        .data[[IEATools::iea_cols$country]] == "GHA" &
        .data[[IEATools::iea_cols$year]] == 1971 &
        .data[[IEATools::template_cols$machine]] == "Charcoal stoves" ~ NA_real_,
        TRUE ~ .data[[IEATools::template_cols$.values]]
      )
    )
# Run through the assemble_phi_u_tables function
completed_phi_u_table <- assemble_phi_u_tables(incomplete_phi_table,
                                               phi_constants_table,
                                               countries = "GHA")
# Show that Charcoal stoves was filled
completed_phi_u_table %>%
  dplyr::filter(.data[[IEATools::template_cols$machine]] == "Charcoal stoves")

MatthewHeun/SEAPSUTWorkflow documentation built on April 22, 2022, 2:21 p.m.