Skip to content

Commit

Permalink
Update diagnostics plot and add output of segment map with auto-selec…
Browse files Browse the repository at this point in the history
…ted super peaks
  • Loading branch information
mengqi-z committed Mar 14, 2024
1 parent a33dd70 commit 9de96a4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
16 changes: 8 additions & 8 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
ggplot2::facet_wrap(subRegion ~ ., scales = 'free_y') +
ggplot2::ggtitle(paste0('HDCD at GCAM-USA Dispatch Segment in ', year_i)) +
ggplot2::ylab('Degree-Hours') +
ggplot2::scale_color_manual(values = c('heat' = '#1AB2FF',
'cool' = '#E61A33')) +
ggplot2::scale_color_manual(values = c('heat' = '#E61A33',
'cool' = '#1AB2FF')) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
Expand Down Expand Up @@ -156,8 +156,8 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
ggplot2::facet_wrap(subRegion ~ ., scales = 'free_y') +
ggplot2::ggtitle(paste0('HDCD Load at GCAM-USA Dispatch Segment in ', year_i)) +
ggplot2::ylab('Degree Load') +
ggplot2::scale_color_manual(values = c('heat' = '#1AB2FF',
'cool' = '#E61A33')) +
ggplot2::scale_color_manual(values = c('heat' = '#E61A33',
'cool' = '#1AB2FF')) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
Expand Down Expand Up @@ -187,7 +187,7 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
n_color <- length(unique(hdcd_comb_diagnostics$year))
pal_hd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlOrRd'))
pal_cd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlGnBu'))
pal <- c(rev(pal_hd(n_color)), rev(pal_cd(n_color)))
pal <- c(rev(pal_cd(n_color)), rev(pal_hd(n_color)))

for (scale_i in c('free_y', 'fixed')){

Expand Down Expand Up @@ -427,8 +427,8 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
ggplot2::facet_wrap(subRegion ~ ., scales = 'free_y') +
ggplot2::ggtitle(paste0('NCDF-', year_i, ' VS ', noaa_name)) +
ggplot2::ylab(paste0('Monthly ', unit)) +
ggplot2::scale_color_manual(values = c('HD' = '#1AB2FF',
'CD' = '#E61A33')) +
ggplot2::scale_color_manual(values = c('HD' = '#E61A33',
'CD' = '#1AB2FF')) +
ggplot2::scale_linetype_manual(values = c(1, 2)) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
Expand Down Expand Up @@ -456,7 +456,7 @@ diagnostics <- function(hdcd_segment = tibble::tibble(),
n_color <- length(unique(hdcd_comb_monthly_diagnostics_all$year))
pal_hd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlOrRd'))
pal_cd <- colorRampPalette(RColorBrewer::brewer.pal(9, 'YlGnBu'))
pal <- c(rev(pal_hd(n_color)), rev(pal_cd(n_color)))
pal <- c(rev(pal_cd(n_color)), rev(pal_hd(n_color)))

# create file name to save
filename_monthly_diagnostics_all <- file.path(
Expand Down
50 changes: 45 additions & 5 deletions R/hdcd.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,22 @@ hdcd <- function(ncdf = NULL,
# calculate the HDCD
hdcd_region_segments <- hdcd_region %>%
dplyr::left_join(temporal_subset, by = c('datetime', 'year')) %>%
dplyr::left_join(helios::segment_map_utc_no_superpeak ,
dplyr::left_join(helios::segment_map_utc_no_superpeak,
by = c('subRegion', 'month', 'day', 'hour')) %>%
dplyr::left_join(elec_share_region, by = c('subRegion', 'year', 'HDCD')) %>%
dplyr::mutate(value_elec = abs(value * elec_frac)) %>%
dplyr::group_by(region, subRegion, year) %>%
dplyr::mutate(value_rank = order(order(value_elec, decreasing = TRUE)),
segment = ifelse(value_rank <= 10, 'superpeak', segment)) %>%
dplyr::ungroup() %>%
dplyr::ungroup()

# create new segment map
segment_map_utc_region <- hdcd_region_segments %>%
dplyr::select(subRegion, year, segment, month, day, hour) %>%
dplyr::distinct()

# aggregate by segment and HDCD
hdcd_region_segments <- hdcd_region_segments %>%
dplyr::group_by(region, subRegion, year, segment, HDCD) %>%
dplyr::summarise(value = sum(value, na.rm = T)) %>%
dplyr::ungroup() %>%
Expand Down Expand Up @@ -524,7 +532,15 @@ hdcd <- function(ncdf = NULL,
dplyr::group_by(region, subRegion, year) %>%
dplyr::mutate(value_rank = order(order(value_elec, decreasing = TRUE)),
segment = ifelse(value_rank <= 10, 'superpeak', segment)) %>%
dplyr::ungroup() %>%
dplyr::ungroup()

# create new segment map
segment_map_utc_gridregion <- hdcd_gridregion_segments %>%
dplyr::select(subRegion, year, segment, month, day, hour) %>%
dplyr::distinct()

# aggregate by segment and HDCD
hdcd_gridregion_segments <- hdcd_gridregion_segments %>%
dplyr::group_by(region, subRegion, year, segment, HDCD) %>%
dplyr::summarise(value = sum(value, na.rm = T)) %>%
dplyr::ungroup() %>%
Expand Down Expand Up @@ -1015,12 +1031,36 @@ hdcd <- function(ncdf = NULL,
}
}



}

}

#......................
# Step 9e: Save segment mapping with auto-pick super peak
#......................

if(im3_analysis){

# save us 49 segment mapping
filename_segment_map <- file.path(
folder,
helios::create_name(c('segment_map', year_min_i, year_max_i,
gsub('_', '-', gsub('gcam_', '', spatial)), name_append), 'csv'))

data.table::fwrite(segment_map_utc_region,
file = filename_segment_map)
print(paste0('File saved as : ', filename_segment_map))


# save grid region segement mapping
filename_segment_map <- file.path(
folder,
helios::create_name(c('segment_map', year_min_i, year_max_i, 'gridregion', name_append), 'csv'))

data.table::fwrite(segment_map_utc_gridregion,
file = filename_segment_map)
print(paste0('File saved as : ', filename_segment_map))
}

#......................
# Step 10: Save XML
Expand Down

0 comments on commit 9de96a4

Please sign in to comment.