Skip to content

Commit

Permalink
Add capability to calculate pop-weighted hdcd at electricity intercon…
Browse files Browse the repository at this point in the history
…nection level (WECC, ERCOT, EI)
  • Loading branch information
mengqi-z committed Jul 17, 2024
1 parent 4390c89 commit 151ca4b
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Remotes:
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
14 changes: 14 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@
#' }
"mapping_states_gridregion"

#--------------------------------
# State to Electricity Interconnection Mapping
#--------------------------------
#' mapping_states_interconnect
#'
#' @source electricity trade update from Matthew Binsted
#' @format tibble::tribble
#' @examples
#' \dontrun{
#' library(helios);
#' mapping_states_interconnect <- helios::mapping_states_interconnect
#' }
"mapping_states_interconnect"


#.................................
# Pre-processed Population Files
Expand Down
24 changes: 22 additions & 2 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
# calculate the number of hours in each segment by state
segment_hours <- helios::segment_map_utc %>%
dplyr::group_by(subRegion, segment) %>%
dplyr::summarise(segment_hours = n()) %>%
dplyr::summarise(segment_hours = dplyr::n()) %>%
dplyr::ungroup()

if(any(grepl('grid', unique(hdcd_segment$subRegion)))) {
Expand All @@ -77,11 +77,31 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
dplyr::rename(subRegion = grid_region) %>%
dplyr::distinct()
}

if(any(grepl('WECC', unique(hdcd_segment$subRegion)))) {
# calculate the number of hours in each segment by electricity interconnection region
# if at electricity interconnection level, select Central Northeast grid region (e.g., Montana) as the
# Eastern interconnection's segment time
# Note: The eastern interconnection is not suppose to be used in GO-CERF-GO (WECC is the one to be used)
segment_hours_temp <- segment_hours %>%
dplyr::filter(subRegion == 'MO') %>%
dplyr::mutate(subRegion = 'EI')

segment_hours <- segment_hours %>%
dplyr::left_join(helios::mapping_states_interconnect, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::distinct() %>%
dplyr::rename(subRegion = grid_region) %>%
dplyr::filter(!subRegion == 'EI') %>%
dplyr::bind_rows(segment_hours_temp)

}

} else {

segment_hours <- segment_map %>%
dplyr::group_by(subRegion, year, segment) %>%
dplyr::summarise(segment_hours = n()) %>%
dplyr::summarise(segment_hours = dplyr::n()) %>%
dplyr::ungroup()

}
Expand Down
4 changes: 2 additions & 2 deletions R/format_temperature.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param ncdf_grid Default = NULL. output from process_temperature.
#' @param model Default = NULL. String for climate model that generates the ncdf file. Options: 'wrf' or 'cmip'.
#' @param time_periods Default = NULL. Integer vector for selected time periods to process. If not specified, set to GCAM periods seq(2020, 2100, 5).
#' @param to_year Default = NULL. Integer for the time step the design year/representative year is for.
#' @param to_year Default = NULL. Integer for renaming the time step the design year/representative year is for.
#' @importFrom magrittr %>%
#' @importFrom data.table :=
#' @export
Expand All @@ -32,7 +32,7 @@ format_temperature <- function(ncdf_grid = NULL,
ncdf_times_update <- gsub(years, to_year, ncdf_times)

ncdf_grid <- ncdf_grid %>%
dplyr::rename_at(vars(ncdf_times), ~ ncdf_times_update)
dplyr::rename_at(dplyr::vars(ncdf_times), ~ ncdf_times_update)

ncdf_times <- ncdf_times_update

Expand Down
53 changes: 42 additions & 11 deletions R/hdcd.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#' @param im3_analysis Default = T. Output annual HDCD at grid region scale for trend-representative year analysis
#' @param elec_share Default = NULL. data frame for the fraction of building heating and cooling energy consumption met by electricity at grid region scale. Column [subRegion, year, HDCD, elec_frac]. If elec_share is provided, helios will recalculate the super peak hours; Otherwise, helios will use the default super peak hours. Note, to get the correct super peak hours, the ncdf argument should include all files that cover full years.
#' @param to_year Default = NULL. Integer for the time step the design year/representative year is for.
#' @param use_elec_interconnect Default = F. Set to TRUE to output degree-days and degree-hours at electricity interconnection level (instead of grid region level). IM3 analysis enabled only.
#' @importFrom magrittr %>%
#' @importFrom data.table :=
#' @export
Expand All @@ -39,7 +40,8 @@ hdcd <- function(ncdf = NULL,
save = T,
im3_analysis = T,
elec_share = NULL,
to_year = NULL) {
to_year = NULL,
use_elec_interconnect = F) {

print('Starting function process_hdcd...')

Expand Down Expand Up @@ -177,7 +179,8 @@ hdcd <- function(ncdf = NULL,
time_periods = time_periods,
climate_years = years,
spatial = spatial,
im3_analysis = im3_analysis)
im3_analysis = im3_analysis,
use_elec_interconnect = use_elec_interconnect)

population_j_weighted <- pop_list$population_weighted
population_j_weighted_gridregion <- pop_list$population_weighted_gridregion
Expand Down Expand Up @@ -207,8 +210,15 @@ hdcd <- function(ncdf = NULL,
# for IM3 grid region analysis
if(im3_analysis){

if(use_elec_interconnect){
mapping_elec_subregion <- helios::mapping_states_interconnect
} else {
mapping_elec_subregion <- helios::mapping_states_gridregion
}


ncdf_pivot_gridregion <- ncdf_pivot %>%
dplyr::left_join(helios::mapping_states_gridregion, by = 'subRegion') %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::mutate(subRegion = grid_region,
ID = grid_region) %>%
dplyr::select(-grid_region)
Expand Down Expand Up @@ -311,7 +321,7 @@ hdcd <- function(ncdf = NULL,
dplyr::select(grid_region = subRegion, year, HDCD, elec_frac)

# for state level calculation
elec_share_region <- helios::mapping_states_gridregion %>%
elec_share_region <- mapping_elec_subregion %>%
dplyr::left_join(elec_share_region, by = c('grid_region')) %>%
dplyr::select(subRegion, year, HDCD, elec_frac)

Expand Down Expand Up @@ -430,11 +440,32 @@ hdcd <- function(ncdf = NULL,
dplyr::select(subRegion, year, HDCD, elec_frac)

# segment map for grid regions
segment_map_utc_no_superpeak_gridregion <- helios::segment_map_utc_no_superpeak %>%
dplyr::left_join(helios::mapping_states_gridregion, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::distinct() %>%
dplyr::rename(subRegion = grid_region)
if(use_elec_interconnect){
# if at electricity interconnection level, select Central Northeast grid region (e.g., Montana) as the
# Eastern interconnection's segment time
# Note: The eastern interconnection is not suppose to be used in GO-CERF-GO (WECC is the one to be used)
segment_map_utc_no_superpeak_gridregion_temp <- helios::segment_map_utc_no_superpeak %>%
dplyr::filter(subRegion == 'MO') %>%
dplyr::mutate(subRegion = 'EI')

segment_map_utc_no_superpeak_gridregion <- helios::segment_map_utc_no_superpeak %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::distinct() %>%
dplyr::rename(subRegion = grid_region) %>%
dplyr::filter(!subRegion == 'EI') %>%
dplyr::bind_rows(segment_map_utc_no_superpeak_gridregion_temp)

} else {

segment_map_utc_no_superpeak_gridregion <- helios::segment_map_utc_no_superpeak %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::distinct() %>%
dplyr::rename(subRegion = grid_region)

}


# calculate the HDCD
hdcd_gridregion_segments <- hdcd_gridregion %>%
Expand Down Expand Up @@ -465,7 +496,7 @@ hdcd <- function(ncdf = NULL,

# segment map for grid regions
segment_map_utc_gridregion <- helios::segment_map_utc %>%
dplyr::left_join(helios::mapping_states_gridregion, by = 'subRegion') %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::distinct() %>%
dplyr::rename(subRegion = grid_region)
Expand Down Expand Up @@ -532,7 +563,7 @@ hdcd <- function(ncdf = NULL,

# create building service structure for grid region
L2441.HDDCDD_Fixed_gcamusa_seg_gridregion <- helios::L2441.HDDCDD_Fixed_gcamusa_seg %>%
dplyr::left_join(helios::mapping_states_gridregion, by = 'subRegion') %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::select(-subRegion) %>%
dplyr::rename(subRegion = grid_region) %>%
dplyr::distinct()
Expand Down
13 changes: 11 additions & 2 deletions R/process_population.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param climate_years Default = NULL. Integer vector of years that is covered by the climate data to check if the population years overlap with the climate years. If Null, skip the check.
#' @param spatial Default = NULL. String for spatial aggregation boundaries. Options: check helios::spatial_options. 'gcam_us49', 'gcam_regions32', 'gcam_regions31_us52', 'gcam_countries', 'gcam_basins'.
#' @param im3_analysis Default = TRUE. Output annual HDCD at grid region scale for trend-representative year analysis
#' @param use_elec_interconnect Default = F. Set to TRUE to output degree-days and degree-hours at electricity interconnection level (instead of grid region level). IM3 analysis enabled only.
#' @importFrom magrittr %>%
#' @importFrom data.table :=
#' @export
Expand All @@ -17,7 +18,8 @@ process_population <- function(population = NULL,
time_periods = NULL,
climate_years = NULL,
spatial = NULL,
im3_analysis = TRUE){
im3_analysis = TRUE,
use_elec_interconnect = FALSE){

if (im3_analysis) {

Expand Down Expand Up @@ -96,8 +98,15 @@ process_population <- function(population = NULL,

# for IM3 grid region analysis
if(im3_analysis){

if(use_elec_interconnect){
mapping_elec_subregion <- helios::mapping_states_interconnect
} else {
mapping_elec_subregion <- helios::mapping_states_gridregion
}

population_weighted_gridregion <- population_grid %>%
dplyr::left_join(helios::mapping_states_gridregion, by = 'subRegion') %>%
dplyr::left_join(mapping_elec_subregion, by = 'subRegion') %>%
dplyr::mutate(subRegion = grid_region,
ID = grid_region) %>%
dplyr::select(-grid_region) %>%
Expand Down
Binary file added data/mapping_states_interconnect.rda
Binary file not shown.
22 changes: 17 additions & 5 deletions inst/extras/dev_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ ncdf_format <- helios::format_temperature(ncdf_grid = ncdf_grid,
to_year = 2045)$ncdf_pivot

pop_grid <- helios::process_population(population = path_to_population,
coordinates = ncdf_pivot,
coordinates = ncdf_format,
time_periods = 2020,
climate_years = NULL,
spatial = 'gcam_us49',
im3_analysis = TRUE)
im3_analysis = TRUE,
use_elec_interconnect = TRUE)

# check mapping grid
mapping <- helios::find_mapping_grid(data = pop,
Expand Down Expand Up @@ -150,10 +151,16 @@ path_to_climate_ncdf <- file.path(data_dir, 'climate', 'wrfout_d01_2020-01-01_01
path_to_population <- file.path(
data_dir, 'population', 'population_conus_total_ssp2_2020-2100_wrf_wgs84.csv')

# for grid region
elec_share <- data.table::fread(
'C:/WorkSpace/IM3/helios/hddcdd/constance/output/im3_bldg_hdcd_electricity_fraction.csv') %>%
dplyr::filter(scenario == 'rcp85hotter_ssp5')

# for electricity interconnection
elec_share <- data.table::fread(
'C:/WorkSpace/IM3/helios/hddcdd/constance/output/im3_bldg_hdcd_electricity_intensity_interconnection.csv') %>%
dplyr::filter(scenario == 'rcp85hotter_ssp5')

# test file list
# path_to_climate_ncdf <- c(
# helios::pkg_example('wrfout_d01_2020-01-01_01%3A00%3A00_sub.nc'),
Expand All @@ -175,19 +182,20 @@ hdcd_wrf_all <- helios::hdcd(
folder = file.path(getwd(), 'output_im3_test'),
diagnostics = F,
xml = F,
name_append = '',
name_append = 'interconnection',
save = T,
im3_analysis = T,
elec_share = elec_share,
to_year = NULL
to_year = NULL,
use_elec_interconnect = T
)

helios::diagnostics(
hdcd_segment = hdcd_wrf_all$hdcd_comb_gcam,
hdcd_monthly = hdcd_wrf_all$hdcd_comb_monthly,
min_diagnostic_months = 1,
folder = file.path(getwd(), 'output_im3_test'),
name_append = 'wrf_all'
name_append = 'interconnection'
)


Expand All @@ -208,6 +216,10 @@ ncdf_grid <- helios::read_ncdf(ncdf = path_to_climate_ncdf,
time_periods = 2020)
population_j_grid <- helios::read_population(path_to_population, time_periods = 2020)

ncdf_format <- helios::format_temperature(ncdf_grid = ncdf_grid,
model = 'cmip',
to_year = NULL)$ncdf_pivot

hdcd_cmip_all <- helios::hdcd(
ncdf = path_to_climate_ncdf,
ncdf_var = 'tas',
Expand Down
16 changes: 16 additions & 0 deletions inst/extras/saveDataFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,19 @@ mapping_states_gridregion <- data.table::fread(
dplyr::select(subRegion = state, grid_region)

usethis::use_data(mapping_states_gridregion, version = 3, overwrite = T)


#--------------------------------
# State to Grid Region Mapping
#--------------------------------
# read state to grid region mapping from gcamdata
mapping_states_interconnect <- data.table::fread(
file.path('C:/WorkSpace/github/helios/inst/extras/GCAM-USA_interconnections.csv')) %>%
dplyr::left_join(rmap::mapUS52 %>% tibble::as_tibble() %>% dplyr::select(state_name = subRegionAlt, state = subRegion),
by = 'state_name') %>%
dplyr::mutate(interconnect = ifelse(interconnect == 'n/a', NA, interconnect),
interconnect = ifelse(interconnect == 'Eastern Interconnection', 'EI', interconnect),
interconnect = ifelse(is.na(interconnect), state, interconnect)) %>%
dplyr::select(subRegion = state, grid_region = interconnect)

usethis::use_data(mapping_states_interconnect, version = 3, overwrite = T)
2 changes: 1 addition & 1 deletion man/format_temperature.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/hdcd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/helios.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 151ca4b

Please sign in to comment.