data-raw/import_pub_results.R

# Read in raw Non-CO2 MAC curve data

# Global Non-CO2 Greenhouse Gas Emission Projections & Mitgation Potential: 2015-2050 (EPA-430-R-19-010, September 2019)
# Downloaded from: https://www.epa.gov/global-mitigation-non-co2-greenhouse-gases/global-non-co2-greenhouse-gas-emission-projections

library(drake)
library(tidyverse)
library(countrycode)


plan <- drake_plan(
  
  ### Read in data ----------------
  proj_raw = readxl::read_xlsx(
    file_in("data-raw/proj-data-annex_Sept2019.xlsx"),
    sheet = "rawdata"),
  
  mac_raw = readxl::read_xlsx(
    "data-raw/NonCO2 MACs-Raw Results.xlsx",
    sheet = "Raw Results"),
  
  ### lists & xwalks
  sector_xwalk = tribble(
    ~sector.proj, ~sector.mac,
    "Energy", "ENERGY",
    "Industrial Processes", "INDUSTRIAL",
    "Agriculture", "AGRICULTURE",
    "Waste", "WASTE"),
  
  source_xwalk = readr::read_csv(
    "data-raw/source_designation.csv") %>%
    select(proj_source, proj_subsource, mac_source, source_common),
  
  ### standardize country names, codes, sectors, and sources
  proj = proj_raw %>%
    mutate(
      genc3c = countrycode(country, 
                           origin = "country.name", 
                           destination = "genc3c"),
      country = countrycode(country, 
                            origin = "country.name", 
                            destination = "country.name")) %>%
    left_join(source_xwalk, by = c("source" = "proj_source", "subsource" = "proj_subsource")) %>%
    select(-source, -subsource, -mac_source) %>%
    select(sector, source = source_common, gas, year, country, genc3c, unit, value) %>%
    arrange(sector, source, year, country, gas)
  ,
  
  mac = mac_raw %>%
    mutate(
      genc3c = countrycode(country, 
                           origin = "country.name", 
                           destination = "genc3c"),
      country = countrycode(country, 
                            origin = "country.name", 
                            destination = "country.name")) %>%
    select(-country_code) %>%
    left_join(sector_xwalk, by = c("sector" = "sector.mac")) %>%
    select(-sector) %>% rename(sector = sector.proj) %>%
    
    left_join(
      # version of the xwalk with unique mac_source
      distinct(source_xwalk, mac_source, source_common), 
      by = c("source" = "mac_source")) %>%
    select(-source) %>%
    select(sector, source = source_common, year, country, genc3c, tech, p, q) %>%
    arrange(sector, source, year, country)
)

make(plan)

loadd(proj, mac)


glimpse(proj)
glimpse(mac)

#### Bring data into common form -------------
# Country names & codes
# Sectors/Sources

## code to verify the full alignment now...
MollieCarroll/NonCO2-Figs documentation built on April 19, 2020, 6:05 p.m.