Skip to content

Commit

Permalink
Merge pull request #2 from JGCRI/dev
Browse files Browse the repository at this point in the history
Update hdcd and diagnostic
  • Loading branch information
zarrarkhan authored Jun 24, 2022
2 parents 8cb44ae + f3c6224 commit e6ded97
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 81 deletions.
95 changes: 53 additions & 42 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ diagnostics <- function(hdcd = tibble::tibble(),

if(T){

NULL -> subRegion -> year -> segment -> value ->heatcool ->
month -> stateCode -> HDDCDD -> scenario -> scenario_hddcdd
NULL -> subRegion -> year -> segment -> value -> heatcool ->
month -> stateCode -> HDDCDD -> scenario -> scenario_hddcdd

}

Expand All @@ -37,16 +37,17 @@ diagnostics <- function(hdcd = tibble::tibble(),
folder_diagnostics <- paste0(folder,"/diagnostics",name_append)
if(!dir.exists(folder_diagnostics)){dir.create(folder_diagnostics)}

if(nrow(hdcd)>0){
if(nrow(hdcd) > 0){

#..............
# By Segment
#.............

hdcd_comb_diagnostics <- hdcd %>%
dplyr::select(subRegion,year,segment,value) %>%
dplyr::select(subRegion, year, segment, value) %>%
unique() %>%
dplyr::mutate(heatcool = dplyr::if_else(value < 0, "heat","cool"))
dplyr::mutate(heatcool = dplyr::if_else(value < 0, "heat","cool"),
value = abs(value))

segment_levels = c("Jan_day","Jan_night","Feb_day","Feb_night",
"Mar_day","Mar_night","Apr_day","Apr_night",
Expand All @@ -55,7 +56,7 @@ diagnostics <- function(hdcd = tibble::tibble(),
"Sep_day","Sep_night","Oct_day","Oct_night",
"Nov_day","Nov_night","Dec_day","Dec_night","superpeak")

# Individul Years
# Individual Years
for(year_i in (hdcd_comb_diagnostics$year) %>% unique()) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::filter(year == year_i) %>%
Expand All @@ -64,12 +65,12 @@ diagnostics <- function(hdcd = tibble::tibble(),
ggplot2::geom_line(ggplot2::aes(color = heatcool)) +
ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") +
ggplot2::ggtitle(paste0("HDCD WRF to GCAM ", year_i , " ")) +
ggplot2::scale_color_manual(values = c("heat" = "firebrick",
"cool" = "dodgerblue")) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust =
0.5))+
ggplot2::scale_color_manual(values = c("heat" = "firebrick", "cool" =
"dodgerblue")) +
ggplot2::scale_x_discrete(drop=FALSE)
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
vjust = 0.5))

filename_diagnostics_i <-
paste0(folder_diagnostics, "/", basename(gsub(".csv", "", filename)), "_", year_i,name_append,".png")
Expand All @@ -85,16 +86,20 @@ diagnostics <- function(hdcd = tibble::tibble(),
if(T) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))) +
ggplot2::aes(x = segment, y = value, group = year) +
ggplot2::geom_line(ggplot2::aes(color = heatcool)) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
ggplot2::geom_line() +
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::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))+
ggplot2::scale_color_manual(values = c("heat" = "firebrick", "cool" =
"dodgerblue"))+
ggplot2::scale_x_discrete(drop=FALSE)
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")
Expand All @@ -110,16 +115,19 @@ diagnostics <- function(hdcd = tibble::tibble(),
if(T) {
ggplot2::ggplot(data = hdcd_comb_diagnostics %>%
dplyr::mutate(segment = factor(segment, levels = segment_levels))) +
ggplot2::aes(x = segment, y = value, group = year) +
ggplot2::geom_line(ggplot2::aes(color = heatcool)) +
ggplot2::aes(x = segment, y = value,
group = interaction(year, heatcool),
color = interaction(heatcool)) +
ggplot2::geom_line() +
ggplot2::facet_wrap(subRegion ~ ., scales = "fixed") +
ggplot2::ggtitle(paste0("HDCD WRF to GCAM ")) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust =
0.5))+
ggplot2::scale_color_manual(values = c("heat" = "firebrick", "cool" =
"dodgerblue"))+
ggplot2::scale_x_discrete(drop=FALSE)
"dodgerblue")) +
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")
Expand All @@ -137,7 +145,7 @@ diagnostics <- function(hdcd = tibble::tibble(),
# By Month compare against NOAA
#.............

if(nrow(hdcd_monthly)>0){
if(nrow(hdcd_monthly) > 0){
months = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
monthNums = c("01","02","03","04","05","06","07","08","09","10","11","12")
monthsShort<- c("JA","FB","MR","AP","MY","JN","JL","AG","SP","OC","NV","DC")
Expand Down Expand Up @@ -174,41 +182,44 @@ diagnostics <- function(hdcd = tibble::tibble(),
current_years <- as.integer(unique(hdcd_monthly$year))
noaa_years <- as.integer(unique(helios::noaa_hddcdd$year))

# Individul Years
# Individual Years
for(year_i in current_years) {

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

hdcd_comb_monthly_diagnostics %>%
dplyr::filter((year == year_i & scenario == "ncdf") |
(year == noaa_year_i & scenario == "noaa")) %>%
dplyr::mutate(scenario = paste0(scenario,"_",year)) %>%
dplyr::select(subRegion, scenario, year, month,HDDCDD, value)->
(year == noaa_year_i &
scenario == "noaa")) %>%
dplyr::mutate(scenario = paste0(scenario, "_", year)) %>%
dplyr::select(subRegion, scenario, year, month, HDDCDD, value) ->
hdcd_comb_monthly_diagnostics_i

# Expand to include all year months
all <- hdcd_comb_monthly_diagnostics_i %>%
tidyr::expand(subRegion,scenario,year,month,HDDCDD)
tidyr::expand(subRegion, scenario, year, month, HDDCDD)
hdcd_comb_monthly_diagnostics_i %>%
dplyr::right_join(all) %>%
dplyr::filter((year == year_i & scenario == paste0("ncdf_",year_i)) |
(year == noaa_year_i & scenario == paste0("noaa_",noaa_year_i))) %>%
dplyr::mutate(scenario_hddcdd = paste0(scenario,HDDCDD)) %>%
tidyr::replace_na(list(value=0))->
dplyr::filter((year == year_i &
scenario == paste0("ncdf_", year_i)) |
(year == noaa_year_i &
scenario == paste0("noaa_", noaa_year_i))) %>%
dplyr::mutate(scenario_hddcdd = paste0(scenario, HDDCDD)) %>%
tidyr::replace_na(list(value = 0)) ->
hdcd_comb_monthly_diagnostics_i

ggplot2::ggplot(data = hdcd_comb_monthly_diagnostics_i,
ggplot2::aes(x = month, y = value, group=scenario_hddcdd)) +
ggplot2::aes(x = month, y = value, group = scenario_hddcdd)) +
ggplot2::geom_line(ggplot2::aes(color = HDDCDD, linetype = scenario)) +
ggplot2::facet_wrap(subRegion ~ ., scales = "free_y") +
ggplot2::ggtitle(paste0("NCDF_", year_i," NOAA_",noaa_year_i)) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust =
0.5))+
ggplot2::ggtitle(paste0("NCDF_", year_i, " NOAA_", noaa_year_i)) +
ggplot2::scale_color_manual(values = c("HDD" = "firebrick",
"CDD" = "dodgerblue")) +
ggplot2::scale_linetype_manual(values = c(1,2)) +
ggplot2::scale_x_discrete(drop=FALSE)
ggplot2::scale_linetype_manual(values = c(1, 2)) +
ggplot2::scale_x_discrete(drop = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
vjust = 0.5))

filename_monthly_diagnostics_i <-
paste0(folder_diagnostics, "/monthly_ncdf_", year_i,"_noaa_",noaa_year_i,name_append,".png")
Expand Down
63 changes: 24 additions & 39 deletions R/hdcd.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,38 +238,23 @@ hdcd <- function(ncdf = NULL,

# hdcd segments
hdcd_region_segments <- hdcd_region %>%
# dplyr::left_join(temporal_subset, by="x") %>%
dplyr::left_join(temporal_subset, by=c("datetime", "year")) %>%
dplyr::left_join(helios::segment_map_utc, by=c("subRegion", "month", "day", "hour")) %>%
dplyr::group_by(year,month,segment) %>%
dplyr::mutate(num_days = length(unique(day))) %>%
dplyr::ungroup() %>%
dplyr::select(subRegion,year,segment,day,HDDCDD,num_days,value) %>%
dplyr::group_by(subRegion,year,segment,day,HDDCDD, num_days) %>%
dplyr::summarize(value=mean(value,na.rm=T)) %>%
dplyr::ungroup() %>%
dplyr::select(subRegion,segment,HDDCDD,year,num_days,value) %>%
dplyr::group_by(subRegion,year,HDDCDD,segment, num_days) %>%
dplyr::summarize(value=sum(value,na.rm=T)) %>%
dplyr::ungroup() %>%
dplyr::filter(!is.na(subRegion))
dplyr::group_by(subRegion, year, segment, HDDCDD) %>%
dplyr::summarise(value = sum(value, na.rm=T))

# hdcd monthly
hdcd_region_monthly <- hdcd_region %>%
# dplyr::left_join(temporal_subset, by="x") %>%
dplyr::left_join(temporal_subset, by=c("datetime", "year")) %>%
dplyr::group_by(year,month) %>%
dplyr::mutate(num_days = length(unique(day))) %>%
dplyr::ungroup()%>%
dplyr::select(subRegion,year,month,day,HDDCDD,num_days,value) %>%
dplyr::group_by(subRegion,year,day,HDDCDD,month, num_days) %>%
dplyr::summarize(value=mean(value,na.rm=T)) %>%
dplyr::group_by(subRegion, year, month, day) %>%
dplyr::summarise(value = (max(value) + min(value))/2) %>%
dplyr::ungroup() %>%
dplyr::select(subRegion,year,month,HDDCDD,num_days,value) %>%
dplyr::group_by(subRegion,year,HDDCDD,month,num_days) %>%
dplyr::summarize(value=sum(value,na.rm=T)) %>%
dplyr::group_by(subRegion, year, month) %>%
dplyr::summarise(HDD = sum(value[value < 0]),
CDD = sum(value[value > 0])) %>%
dplyr::ungroup() %>%
dplyr::filter(!is.na(subRegion))
tidyr::gather(key = 'HDDCDD', value = 'value', HDD, CDD)


#......................
# Step 8: Add in building components for GCAMUSA
Expand Down Expand Up @@ -304,26 +289,26 @@ hdcd <- function(ncdf = NULL,
hdcd_comb <- hdcd_comb %>%
dplyr::bind_rows(hdcd_region_segments_bld) %>%
dplyr::ungroup() %>%
dplyr::group_by(subRegion,year,segment,gcam.consumer,
dplyr::group_by(subRegion, year, segment, gcam.consumer,
nodeInput, building.node.input,
thermal.building.service.input, num_days) %>%
dplyr::summarize(value=sum(value,na.rm=T)) %>%
thermal.building.service.input) %>%
dplyr::summarize(value = sum(value, na.rm = T)) %>%
dplyr::ungroup() %>%
dplyr::filter(!is.na(subRegion),
!is.na(year),
!is.na(segment))

year_min_i <- min(hdcd_comb$year,na.rm=T)
year_max_i <- max(hdcd_comb$year,na.rm=T)
year_min_i <- min(hdcd_comb$year, na.rm = T)
year_max_i <- max(hdcd_comb$year, na.rm = T)

if(i < length(ncdf)){
filename_i <- paste0(folder,"/hdcd_wrf_to_gcam_intermediate",name_append,".csv")
filename_i <- paste0(folder, "/hdcd_wrf_to_gcam_intermediate", name_append, ".csv")
} else {
filename_i <- paste0(folder,"/hdcd_wrf_to_gcam_",year_min_i,"_",year_max_i,name_append,".csv")
filename_i <- paste0(folder, "/hdcd_wrf_to_gcam_", year_min_i, "_", year_max_i, name_append, ".csv")
}

if(save){
data.table::fwrite(hdcd_comb, file=filename_i)
data.table::fwrite(hdcd_comb, file = filename_i)
print(paste0("File saved as : ", filename_i))
}
}
Expand All @@ -337,15 +322,15 @@ hdcd <- function(ncdf = NULL,
hdcd_comb_monthly <- hdcd_comb_monthly %>%
dplyr::bind_rows(hdcd_region_monthly) %>%
dplyr::ungroup() %>%
dplyr::group_by(subRegion,year,month, num_days) %>%
dplyr::summarize(value=sum(value,na.rm=T)) %>%
dplyr::group_by(subRegion, year, month, HDDCDD) %>%
dplyr::summarize(value = sum(value, na.rm = T)) %>%
dplyr::ungroup() %>%
dplyr::filter(!is.na(subRegion),
!is.na(year),
!is.na(month))

year_min_i <- min(hdcd_comb_monthly$year,na.rm=T)
year_max_i <- max(hdcd_comb_monthly$year,na.rm=T)
year_min_i <- min(hdcd_comb_monthly$year, na.rm = T)
year_max_i <- max(hdcd_comb_monthly$year, na.rm = T)

if(i < length(ncdf)){
filename_i_monthly <- paste0(folder,"/hdcd_wrf_to_gcam_intermediate_monthly",name_append,".csv")
Expand All @@ -368,9 +353,9 @@ hdcd <- function(ncdf = NULL,

if(xml){
helios::save_xml(hdcd = hdcd_comb,
folder = folder,
filename = filename_i,
name_append = name_append)
folder = folder,
filename = filename_i,
name_append = name_append)
}


Expand Down

0 comments on commit e6ded97

Please sign in to comment.