Skip to content

Commit

Permalink
Pull request #290: Yo/bugfix/gcamusa industrial ghg
Browse files Browse the repository at this point in the history
Merge in JGCRI/gcam-core from yo/bugfix/gcamusa-industrial-ghg to master

Squashed commit of the following:

commit 48bfffc03981be5f3e0664686315503e48a21af1
Merge: 5e4e275b3 9333d55
Author: Pralit Patel <[email protected]>
Date:   Tue May 2 15:07:10 2023 -0400

    Merge remote-tracking branch 'origin/master' into yo/bugfix/gcamusa-industrial-ghg

commit 5e4e275b363cf9f56fe39f284e711d5c1ba5b4dd
Author: Yang Ou <[email protected]>
Date:   Thu Apr 20 23:43:46 2023 -0400

    Update generate_package_data.R

commit 2e5675e716449ec71965edd448f002cc53e3ea39
Author: Yang Ou <[email protected]>
Date:   Thu Apr 20 23:42:04 2023 -0400

    Revert "Update ModelInterface_headers.txt"

    This reverts commit a5729d927aad8df827c5fdf591789fc5fc9bf3c9.

commit 747dc43e6ccb71b8a754c656c30927bd138daf16
Author: Yang Ou <[email protected]>
Date:   Tue Apr 18 17:00:29 2023 -0400

    address PR comments

commit 6a1364ae75c3bb4a112a7203035324a82fe0ea2a
Author: Yang Ou <[email protected]>
Date:   Sat Apr 8 21:05:29 2023 -0400

    Revert "update data map"

    This reverts commit 1b32eb9b41ffe0efb36de9b4705d137c7d2487fb.

commit a5729d927aad8df827c5fdf591789fc5fc9bf3c9
Author: Yang Ou <[email protected]>
Date:   Fri Mar 17 12:02:33 2023 -0400

    Update ModelInterface_headers.txt

commit 10690dca4e71eb1bdb2263ece3339627cab2621b
Author: Yang Ou <[email protected]>
Date:   Sun Mar 12 11:32:56 2023 -0400

    Update configuration_usa.xml

    minor naming change

commit 65ff44a0b62bbc7d9b7fff4ea6188d3aee4ffbf8
Author: Yang Ou <[email protected]>
Date:   Sun Mar 12 11:32:10 2023 -0400

    Update configuration_usa.xml

    also move the GHG part after the nonGHG part, because the ind_urb_processing_sectors_USA.xml needs to be read prior to ind_urb_proc_ghg_emissions_USA.xml

commit 77871b9bcc69d6210ce00f02471fc3496dba3bdb
Author: Yang Ou <[email protected]>
Date:   Sat Mar 11 22:11:26 2023 -0500

    Update zchunk_batch_ind_urb_proc_emissions_USA_xml.R

    minor header fix

commit ef3b7ca6cfdad2b74f9ff39735e71fc11c8e0105
Author: Yang Ou <[email protected]>
Date:   Sat Feb 25 09:58:57 2023 -0500

    remove NA rows

commit cdc416d9fb8f20e23276aecbcbf88a979adc0082
Author: Yang Ou <[email protected]>
Date:   Fri Feb 24 21:34:26 2023 -0500

    Update zchunk_L277.ghg_prc_USA.R

commit 1b32eb9b41ffe0efb36de9b4705d137c7d2487fb
Author: Yang Ou <[email protected]>
Date:   Sun Feb 5 10:23:04 2023 -0500

    update data map

commit 70c0b75859ae79a352013625ae356bda3a6cad72
Author: Yang Ou <[email protected]>
Date:   Sat Feb 4 22:36:46 2023 -0500

    Update zchunk_batch_ind_urb_proc_emissions_USA_xml.R

commit 7d198171d3c860d21a26e01fd691a3f28abd08ee
Merge: 53c3af8b5 bf0e650
Author: Yang Ou <[email protected]>
Date:   Sat Feb 4 22:36:40 2023 -0500

    Merge branch 'master' into yo/bugfix/gcamusa-industrial-ghg

commit 53c3af8b58f367000ee049eb122fcfd45f497bd9
Author: Yang Ou <[email protected]>
Date:   Fri Feb 3 12:47:05 2023 -0500

    state-level ghg emissions in processes sectors
  • Loading branch information
ouyangyang363 authored and pralitp committed May 3, 2023
1 parent 9333d55 commit 3fd6edd
Show file tree
Hide file tree
Showing 7 changed files with 5,277 additions and 10 deletions.
11 changes: 6 additions & 5 deletions exe/configuration_usa.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@
<Value name = "municipal_water_usa">../input/gcamdata/xml/water_demand_municipal_USA.xml</Value>
<Value name = "ind_water_usa">../input/gcamdata/xml/water_demand_industry_USA.xml</Value>

<!--GHG EMISSIONS USA-->
<Value name = "ghg_usa">../input/gcamdata/xml/ghg_emissions_USA.xml</Value>
<Value name = "ghg_usa">../input/gcamdata/xml/elecS_ghg_emissions_water_USA.xml</Value>
<Value name = "ghg_usa">../input/gcamdata/xml/trn_ghg_emissions_USA.xml</Value>

<!--nonGHG EMISSIONS USA-->
<Value name = "bld_emiss_USA">../input/gcamdata/xml/bld_emissions_USA.xml</Value>
<Value name = "othertrn_emiss_USA">../input/gcamdata/xml/othertrn_emissions_USA.xml</Value>
Expand All @@ -152,6 +147,12 @@
<Value name = "prc_usa">../input/gcamdata/xml/ind_urb_processing_sectors_USA.xml</Value>
<Value name = "process_emiss_USA">../input/gcamdata/xml/ind_urb_proc_emissions_USA.xml</Value>
<Value name = "refining_emiss_USA">../input/gcamdata/xml/refinery_emissions_USA.xml</Value>

<!--GHG EMISSIONS USA-->
<Value name = "ghg_usa">../input/gcamdata/xml/ghg_emissions_USA.xml</Value>
<Value name = "ghg_usa">../input/gcamdata/xml/elecS_ghg_emissions_water_USA.xml</Value>
<Value name = "ghg_usa">../input/gcamdata/xml/trn_ghg_emissions_USA.xml</Value>
<Value name = "ghg_usa">../input/gcamdata/xml/ind_urb_proc_ghg_emissions_USA.xml</Value>

<Value name = "solver">../input/solution/cal_broyden_config.xml</Value>

Expand Down
174 changes: 174 additions & 0 deletions input/gcamdata/R/zchunk_L277.ghg_prc_USA.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Copyright 2019 Battelle Memorial Institute; see the LICENSE file.

#' module_gcamusa_L277.ghg_prc_USA
#'
#' Generates input ghg emissions and marginal abatement curves for industrial processes and urban processes by
#' energy technology for U.S. states
#' Writes out input emissions and MAC components to all process-related energy technologies and states
#'
#' @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{L277.ghg_prc_USA}, \code{L277.MAC_prc_USA}, \code{L277.MAC_prc_tc_average_USA}, \code{L277.MAC_prc_phaseInTime_USA}.
#' @details Generates input ghg emissions and marginal abatement curves for industrial processes and urban processes by
#' energy technology for U.S. states and writes out input emissions and MAC components to all process-related energy technologies and states
#' @importFrom dplyr filter mutate select
#' @author Yang Ou Feb 2023
#'
module_gcamusa_L277.ghg_prc_USA <- function(command, ...) {
if(command == driver.DECLARE_INPUTS) {
return(c(FILE="gcam-usa/emissions/EPA_state_ghg_emission_2022",
FILE="gcam-usa/emissions/EPA_state_ghg_emission_2022_mapping",
"L131.nonco2_tg_R_prc_S_S_Yh",
"L252.MAC_prc",
"L252.MAC_prc_tc_average",
"L252.MAC_prc_phaseInTime"))
} else if(command == driver.DECLARE_OUTPUTS) {
return(c("L277.ghg_prc_USA",
"L277.MAC_prc_USA",
"L277.MAC_prc_tc_average_USA",
"L277.MAC_prc_phaseInTime_USA"))
} else if(command == driver.MAKE) {

# Silence package checks
category <- GCAM_sector <- region <- Non.CO2 <- year <- value <- share <- national.total <- supplysector <-
subsector <- stub.technology <- NULL

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

# Load required inputs
EPA_GHG_states <- get_data(all_data, "gcam-usa/emissions/EPA_state_ghg_emission_2022", strip_attributes = TRUE)
EPA_GHG_states_mapping <- get_data(all_data, "gcam-usa/emissions/EPA_state_ghg_emission_2022_mapping", strip_attributes = TRUE)
L131.nonco2_tg_R_prc_S_S_Yh <- get_data(all_data, "L131.nonco2_tg_R_prc_S_S_Yh", strip_attributes = TRUE)
L252.MAC_prc <- get_data(all_data, "L252.MAC_prc", strip_attributes = TRUE)
L252.MAC_prc_tc_average <- get_data(all_data, "L252.MAC_prc_tc_average", strip_attributes = TRUE)
L252.MAC_prc_phaseInTime <- get_data(all_data, "L252.MAC_prc_phaseInTime", strip_attributes = TRUE)

# ===================================================
# Perform computations

# isolate industrial process sectors from US state-level GHG inventory
# EPA data contains data from 1990 to 2020
EPA_GHG_states_ind_proc_EPA_years <- EPA_GHG_states %>%
gather_years() %>%
# keep CH4 and N2O
filter(grepl("CH4|N2O", category)) %>%
# remove sum rows or LULUCF rows
filter(!grepl("Sum|LULUCF", category)) %>%
separate(category, into = c("Non.CO2", "source"), sep = "/") %>%
left_join_error_no_match(EPA_GHG_states_mapping, by = "source") %>%
filter(GCAM_sector %in% unique(L131.nonco2_tg_R_prc_S_S_Yh$stub.technology)) %>%
group_by(region, Non.CO2, GCAM_sector, year) %>%
summarise(value = sum(value)) %>%
ungroup()

# Temporarily fill in historical years before 1990 using the earliest available year, i.e. 1990
# then scale to CEDS emissions starting from 1975

EPA_GHG_states_ind_proc_previous_years <- EPA_GHG_states_ind_proc_EPA_years %>%
complete(nesting(region, Non.CO2, GCAM_sector), year = HISTORICAL_YEARS) %>%
group_by(region, Non.CO2, GCAM_sector) %>%
mutate(value = approx_fun(year, value, rule = 2)) %>%
ungroup() %>%
filter(!year %in% EPA_GHG_states_ind_proc_EPA_years$year)

# combine EPA data to have all historical years
EPA_GHG_states_ind_proc_Yh <- bind_rows(EPA_GHG_states_ind_proc_EPA_years,
EPA_GHG_states_ind_proc_previous_years) %>%
filter(year %in% MODEL_BASE_YEARS)

# develop state_shares
EPA_GHG_states_ind_proc_Yh_shares <- EPA_GHG_states_ind_proc_Yh %>%
group_by(Non.CO2, GCAM_sector, year) %>%
mutate(share = value / sum(value)) %>%
ungroup() %>%
select(region, Non.CO2, GCAM_sector, year, share)

# apply state shares by Non.CO2, GCAM_sector, year to the national total emissions from CEDS
L131.nonco2_tg_R_prc_S_S_Yh %>%
filter(GCAM_region_ID == gcam.USA_CODE & Non.CO2 %in% c("CH4", "N2O")) %>%
filter(year %in% MODEL_BASE_YEARS) %>%
rename(national.total = value) %>%
# use left_join because here we copy national total to all states so the number of rows are changed
left_join(EPA_GHG_states_ind_proc_Yh_shares, by = c("subsector" = "GCAM_sector", "Non.CO2", "year")) %>%
# drop CH4 of other industrial process and solvent, these sectors have nonGHG emissions but no CH4 emissions
na.omit() %>%
mutate(value = national.total * share) %>%
select(region, supplysector, subsector, stub.technology, year, Non.CO2, input.emissions = value) -> L277.ghg_prc_USA

# copy marginal abatement curves to states that with the corresponding sector/emissions
L277.ghg_prc_USA %>%
select(region, supplysector, subsector, stub.technology, Non.CO2) %>%
distinct() %>%
inner_join(L252.MAC_prc %>%
filter(region == gcam.USA_REGION) %>%
select(-region),
by = c("supplysector", "subsector", "stub.technology", "Non.CO2")) %>%
select(LEVEL2_DATA_NAMES[["MAC"]])->
L277.MAC_prc_USA

L277.ghg_prc_USA %>%
select(region, supplysector, subsector, stub.technology, Non.CO2) %>%
distinct() %>%
inner_join(L252.MAC_prc_tc_average %>%
filter(region == gcam.USA_REGION) %>%
select(-region),
by = c("supplysector", "subsector", "stub.technology", "Non.CO2")) %>%
select(LEVEL2_DATA_NAMES[["MACTC"]]) ->
L277.MAC_prc_tc_average_USA

L277.ghg_prc_USA %>%
select(region, supplysector, subsector, stub.technology, Non.CO2) %>%
distinct() %>%
inner_join(L252.MAC_prc_phaseInTime %>%
filter(region == gcam.USA_REGION) %>%
select(-region),
by = c("supplysector", "subsector", "stub.technology", "Non.CO2")) %>%
select(LEVEL2_DATA_NAMES[["MACPhaseIn"]]) ->
L277.MAC_prc_phaseInTime_USA

# ===================================================
# Produce outputs

L277.ghg_prc_USA %>%
add_title("Processes GHG emissions by technology for all U.S. states") %>%
add_units("Tg") %>%
add_comments("Apply state shares by Non.CO2 and GCAM_sector and year to the national total emissions from CEDS") %>%
add_precursors("gcam-usa/emissions/EPA_state_ghg_emission_2022",
"gcam-usa/emissions/EPA_state_ghg_emission_2022_mapping",
"L131.nonco2_tg_R_prc_S_S_Yh") ->
L277.ghg_prc_USA

L277.MAC_prc_USA %>%
add_title("copy USA proc MAC curves to states") %>%
add_units("%") %>%
add_comments("copy USA proc MAC curves to states") %>%
same_precursors_as("L277.ghg_prc_USA") %>%
add_precursors("L252.MAC_prc") ->
L277.MAC_prc_USA

L277.MAC_prc_tc_average_USA %>%
add_title("copy USA proc MAC curves tech change to states") %>%
add_units("% increase per year") %>%
add_comments("copy USA proc MAC curves tech change to states") %>%
same_precursors_as("L277.ghg_prc_USA") %>%
add_precursors("L252.MAC_prc_tc_average") ->
L277.MAC_prc_tc_average_USA

L277.MAC_prc_phaseInTime_USA %>%
add_title("copy USA proc MAC curves phase in to states") %>%
add_units("years") %>%
add_comments("copy USA proc MAC curves phase in to states") %>%
same_precursors_as("L277.ghg_prc_USA") %>%
add_precursors("L252.MAC_prc_phaseInTime") ->
L277.MAC_prc_phaseInTime_USA

return_data(L277.ghg_prc_USA,
L277.MAC_prc_USA,
L277.MAC_prc_tc_average_USA,
L277.MAC_prc_phaseInTime_USA)
} else {
stop("Unknown command")
}
}
29 changes: 25 additions & 4 deletions input/gcamdata/R/zchunk_batch_ind_urb_proc_emissions_USA_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
#' @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{ind_urb_proc_emissions_USA.xml}.
#' the generated outputs: \code{ind_urb_proc_emissions_USA.xml}, \code{ind_urb_proc_ghg_emissions_USA.xml}.
module_gcamusa_batch_ind_urb_proc_emissions_USA_xml <- function(command, ...) {
if(command == driver.DECLARE_INPUTS) {
return(c("L277.nonghg_prc_USA",
"L277.nonghg_max_reduction_USA",
"L277.nonghg_steepness_USA"))
"L277.nonghg_steepness_USA",
"L277.ghg_prc_USA",
"L277.MAC_prc_USA",
"L277.MAC_prc_tc_average_USA",
"L277.MAC_prc_phaseInTime_USA"))
} else if(command == driver.DECLARE_OUTPUTS) {
return(c(XML = "ind_urb_proc_emissions_USA.xml"))
return(c(XML = "ind_urb_proc_emissions_USA.xml",
XML = "ind_urb_proc_ghg_emissions_USA.xml"))
} else if(command == driver.MAKE) {

all_data <- list(...)[[1]]
Expand All @@ -24,6 +29,10 @@ module_gcamusa_batch_ind_urb_proc_emissions_USA_xml <- function(command, ...) {
L277.nonghg_prc_USA <- get_data(all_data, "L277.nonghg_prc_USA")
L277.nonghg_max_reduction_USA <- get_data(all_data, "L277.nonghg_max_reduction_USA")
L277.nonghg_steepness_USA <- get_data(all_data, "L277.nonghg_steepness_USA")
L277.ghg_prc_USA <- get_data(all_data, "L277.ghg_prc_USA")
L277.MAC_prc_USA <- get_data(all_data, "L277.MAC_prc_USA")
L277.MAC_prc_tc_average_USA <- get_data(all_data, "L277.MAC_prc_tc_average_USA")
L277.MAC_prc_phaseInTime_USA <- get_data(all_data, "L277.MAC_prc_phaseInTime_USA")

# ===================================================
# Produce outputs
Expand All @@ -36,7 +45,19 @@ module_gcamusa_batch_ind_urb_proc_emissions_USA_xml <- function(command, ...) {
"L277.nonghg_steepness_USA") ->
ind_urb_proc_emissions_USA.xml

return_data(ind_urb_proc_emissions_USA.xml)
create_xml("ind_urb_proc_ghg_emissions_USA.xml") %>%
add_xml_data(L277.ghg_prc_USA, "StbTechOutputEmissions") %>%
add_xml_data(L277.MAC_prc_USA, "MAC") %>%
add_xml_data(L277.MAC_prc_tc_average_USA, "MACTC") %>%
add_xml_data(L277.MAC_prc_phaseInTime_USA, "MACPhaseIn") %>%
add_precursors("L277.ghg_prc_USA",
"L277.MAC_prc_USA",
"L277.MAC_prc_tc_average_USA",
"L277.MAC_prc_phaseInTime_USA") ->
ind_urb_proc_ghg_emissions_USA.xml

return_data(ind_urb_proc_emissions_USA.xml,
ind_urb_proc_ghg_emissions_USA.xml)
} else {
stop("Unknown command")
}
Expand Down
Loading

0 comments on commit 3fd6edd

Please sign in to comment.