Skip to content

Commit

Permalink
Update Diagnostic to include monthly hdcd plot for all years compare …
Browse files Browse the repository at this point in the history
…to NOAA latest year
  • Loading branch information
mengqi-z committed Jul 26, 2022
1 parent f3c6224 commit 52c9715
Showing 1 changed file with 91 additions and 14 deletions.
105 changes: 91 additions & 14 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ diagnostics <- function(hdcd = tibble::tibble(),
dplyr::mutate(heatcool = dplyr::if_else(value < 0, "heat","cool"),
value = abs(value))

hdcd_comb_year_range <- paste(min(unique(hdcd_comb_diagnostics$year)),
max(unique(hdcd_comb_diagnostics$year)),
sep = '-')

filename_diagnostics <-
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_",
hdcd_comb_year_range, name_append, '.csv')

# save plot data for segment diagnostic
data.table::fwrite(x = hdcd_comb_diagnostics,
file = filename_diagnostics)

print(paste0("Diagnostic table saved as ", filename_diagnostics))

segment_levels = c("Jan_day","Jan_night","Feb_day","Feb_night",
"Mar_day","Mar_night","Apr_day","Apr_night",
"May_day","May_night","Jun_day","Jun_night",
Expand All @@ -58,9 +72,11 @@ diagnostics <- function(hdcd = tibble::tibble(),

# Individual Years
for(year_i in (hdcd_comb_diagnostics$year) %>% unique()) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::filter(year == year_i) %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))) +
data_plot <- hdcd_comb_diagnostics %>%
dplyr::filter(year == year_i) %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))

ggplot2::ggplot(data = data_plot) +
ggplot2::aes(x = segment, y = value, group = heatcool) +
ggplot2::geom_line(ggplot2::aes(color = heatcool)) +
ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") +
Expand All @@ -73,19 +89,22 @@ diagnostics <- function(hdcd = tibble::tibble(),
vjust = 0.5))

filename_diagnostics_i <-
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_", year_i,name_append,".png")
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_", year_i,name_append, '.png')

ggplot2::ggsave(filename = filename_diagnostics_i,
# save plot
ggplot2::ggsave(filename = filename_diagnostics_i,
width = 25,
height = 15) # save plot
height = 15)

print(paste0("Diagnostic figure saved as ", filename_diagnostics_i))
}

# Combined years free scale
if(T) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))) +
data_plot <- hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))

ggplot2::ggplot(data = data_plot) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
Expand All @@ -102,19 +121,22 @@ diagnostics <- function(hdcd = tibble::tibble(),
legend.position = 'right')

filename_diagnostics_i <-
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_freeScale",name_append,".png")
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_allYears_freeScale", name_append, '.png')

ggplot2::ggsave(filename = filename_diagnostics_i,
# save plot
ggplot2::ggsave(filename = filename_diagnostics_i,
width = 25,
height = 15) # save plot
height = 15)

print(paste0("Diagnostic figure saved as ", filename_diagnostics_i))
}

# Combined years fixed scale
if(T) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))) +
data_plot <- hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))

ggplot2::ggplot(data = data_plot) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
Expand Down Expand Up @@ -182,8 +204,18 @@ diagnostics <- function(hdcd = tibble::tibble(),
current_years <- as.integer(unique(hdcd_monthly$year))
noaa_years <- as.integer(unique(helios::noaa_hddcdd$year))

filename_monthly_diagnostics <-
paste0(folder_diagnostics,
"/monthly_ncdf_", min(current_years), '-', max(current_years),
"_noaa_", min(noaa_years), '-', max(noaa_years), name_append,".csv")

# save plot data for monthly diagnostic
data.table::fwrite(x = hdcd_comb_monthly_diagnostics,
file = filename_monthly_diagnostics)
print(paste0("Diagnostic table saved as ", filename_monthly_diagnostics))

# Individual Years
for(year_i in current_years) {
for(year_i in intersect(current_years, noaa_years)) {

noaa_year_i <- noaa_years[which(abs(noaa_years - year_i) == min(abs(noaa_years - year_i)))]

Expand Down Expand Up @@ -231,6 +263,51 @@ diagnostics <- function(hdcd = tibble::tibble(),
print(paste0("Diagnostic figure saved as ", filename_monthly_diagnostics_i))
}

# All Years Together
noaa_year_latest <- noaa_years[length(noaa_years)]

noaa_latest <- hdcd_comb_monthly_diagnostics %>%
dplyr::filter((year == noaa_year_latest & scenario == "noaa") &
(!subRegion %in% c('AK', 'HI'))) %>%
dplyr::rename(noaa = value) %>%
dplyr::select(subRegion, month, HDDCDD, noaa)

hdcd_comb_monthly_diagnostics_all <- hdcd_comb_monthly_diagnostics %>%
dplyr::filter(scenario == "ncdf" &
(!subRegion %in% c('AK', 'HI'))) %>%
dplyr::left_join(noaa_latest, by = c('subRegion', 'month', 'HDDCDD')) %>%
dplyr::mutate(noaa = dplyr::if_else(is.na(noaa), 0, noaa)) %>%
dplyr::select(subRegion, scenario, year, month, HDDCDD, value, noaa)

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)))

filename_monthly_diagnostics_all <-
paste0(folder_diagnostics, "/monthly_ncdf_AllYears", "_noaa_",noaa_year_latest, name_append,".png")

ggplot2::ggplot(data = hdcd_comb_monthly_diagnostics_all) +
ggplot2::geom_line(ggplot2::aes(x = month, y = value,
group = interaction(scenario, year, HDDCDD),
color = interaction(year, HDDCDD))) +
ggplot2::geom_line(ggplot2::aes(x = month, y = noaa, group = HDDCDD),
color = 'black', size = 1.2, linetype = 'dashed') +
ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") +
ggplot2::ggtitle(paste0("NCDF (all years) VS", " NOAA (", noaa_year_latest, ')')) +
ggplot2::ylab('Monthly Degree-Hours') +
ggplot2::scale_color_manual(values = pal,
guide = ggplot2::guide_legend(title = 'NCDF (All Years)')) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
vjust = 0.5))

ggplot2::ggsave(filename = filename_monthly_diagnostics_all,
width = 25,
height = 15)
print(paste0("Diagnostic figure saved as ", filename_monthly_diagnostics_all))

}

#...............
Expand Down

0 comments on commit 52c9715

Please sign in to comment.