Skip to content

Commit

Permalink
Merge pull request #3 from JGCRI/dev
Browse files Browse the repository at this point in the history
Update Diagnostic
  • Loading branch information
zarrarkhan authored Aug 4, 2022
2 parents e6ded97 + edd4ba6 commit a7e8ed3
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 46 deletions.
167 changes: 124 additions & 43 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,69 +89,79 @@ 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))) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
ggplot2::geom_line() +
# combined years with color gradients with free scale
if(T){
data_plot <- hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))

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

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

ggplot2::ggplot(data = data_plot) +
ggplot2::geom_line(ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(year, heatcool))) +
ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") +
ggplot2::ggtitle(paste0("HDCD WRF to GCAM ")) +
ggplot2::scale_color_manual(values = c("heat" = "firebrick",
"cool" = "dodgerblue")) +
ggplot2::ylab('Degree-Hours') +
ggplot2::scale_color_manual(values = pal,
guide = ggplot2::guide_legend(title = 'HDCD (All Years)')) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::guides(color = ggplot2::guide_legend(title = 'heatcool')) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
vjust = 0.5),
legend.position = 'right')

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

ggplot2::ggsave(filename = filename_diagnostics_i,
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))) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
ggplot2::geom_line() +
# combined years with color gradients with fixed scale
if(T){
data_plot <- hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))

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

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

ggplot2::ggplot(data = data_plot) +
ggplot2::geom_line(ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(year, heatcool))) +
ggplot2::facet_wrap(subRegion ~ ., scales = "fixed") +
ggplot2::ggtitle(paste0("HDCD WRF to GCAM ")) +
ggplot2::scale_color_manual(values = c("heat" = "firebrick", "cool" =
"dodgerblue")) +
ggplot2::ylab('Degree-Hours') +
ggplot2::scale_color_manual(values = pal,
guide = ggplot2::guide_legend(title = 'HDCD (All Years)')) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::guides(color = ggplot2::guide_legend(title = 'heatcool')) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
vjust = 0.5))

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

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

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

Expand Down Expand Up @@ -182,8 +208,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 +267,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
Binary file modified data/noaa_hddcdd.rda
Binary file not shown.
Binary file modified inst/extras/noaa_hddcdd.RDS
Binary file not shown.
6 changes: 3 additions & 3 deletions inst/extras/saveDataFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ for (year_i in years){
}
}
}
tmp<-read.fwf(file = url_i, skip=15,nrows=48,widths = c(17, 6, 1000)) %>%
tmp<-read.fwf(file = url_i, skip=15,nrows=51,widths = c(17, 6, 1000)) %>%
dplyr::mutate(stateName=gsub(" ","", V1, perl=T),
value=V2)%>%
left_join(stateCodes, by="stateName")%>%
Expand Down Expand Up @@ -136,7 +136,7 @@ for (year_i in years){
}
}
}
tmp<-read.fwf(file = url_i, skip=15,nrows=48,widths = c(17, 6, 1000)) %>%
tmp<-read.fwf(file = url_i, skip=15,nrows=51,widths = c(17, 6, 1000)) %>%
dplyr::mutate(stateName=gsub(" ","", V1, perl=T),
value=V2)%>%
left_join(stateCodes, by="stateName")%>%
Expand All @@ -148,7 +148,7 @@ for (year_i in years){
}
}

noaa_hddcdd <- tbl%>%
noaa_hddcdd <- tbl%>%
left_join(monthCodes, by="month")%>%
dplyr::mutate(monthCode=monthsShort)%>%dplyr::select(-monthsShort)%>%
dplyr::mutate(monthCode=factor(monthCode,levels=monthsShort)) %>%
Expand Down

0 comments on commit a7e8ed3

Please sign in to comment.