diff --git a/R/diagnostics.R b/R/diagnostics.R index 20a7714..d6171b7 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -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, @@ -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, @@ -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')){ @@ -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() + @@ -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( diff --git a/R/hdcd.R b/R/hdcd.R index 794cdde..eb4a4a3 100644 --- a/R/hdcd.R +++ b/R/hdcd.R @@ -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() %>% @@ -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() %>% @@ -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