|
| 1 | + |
| 2 | +## Produce FTE histograms for downscaled GEFS data |
| 3 | + |
| 4 | +library(lubridate) |
| 5 | +library(dplyr) |
| 6 | +library(tidyr) |
| 7 | +library(reshape2) |
| 8 | +library(ggplot2) |
| 9 | +library(fitdistrplus) |
| 10 | +library(latex2exp) |
| 11 | + |
| 12 | + |
| 13 | +# Threshold exceedence ranking -------------------------------------------- |
| 14 | + |
| 15 | +## import fields_dat dim(lat x lon x time x mem) |
| 16 | +load("./data/gefs_downscaled_fields.RData") |
| 17 | + |
| 18 | +## compute the mean threshold exceedence of fields_df at array of thresholds |
| 19 | +## and return analysis ranks |
| 20 | +exceed_ranks <- function(dat_arr, tau){ |
| 21 | + # dat_arr: 3d array (lon x lat x member) |
| 22 | + # tau: vector of thresholds |
| 23 | + ranks <- array(dim = length(tau)) |
| 24 | + for (i in 1:length(tau)) { |
| 25 | + m <- apply(dat_arr, 3, function(field) mean(as.vector(field) > tau[i])) |
| 26 | + if(length(unique(m)) != 1) { |
| 27 | + ranks[i] <- rank(m, ties.method = "random")[1] |
| 28 | + } else { |
| 29 | + # exclude exact ties |
| 30 | + ranks[i] <- NA |
| 31 | + } |
| 32 | + } |
| 33 | + return(ranks) |
| 34 | +} |
| 35 | + |
| 36 | +disagg_rank <- function(r) { |
| 37 | + return(runif(1, r-1/24, r+1/24)) |
| 38 | +} |
| 39 | + |
| 40 | +## iterate over time, compute ranks at different thresholds |
| 41 | +set.seed(10) |
| 42 | +tau <- c(5,10,20) |
| 43 | +ranks_df <- data.frame(t(apply(field_dat, 3, exceed_ranks, tau=tau))) # (day x tau) |
| 44 | + |
| 45 | + |
| 46 | +# FTE Histograms ---------------------------------------------------------- |
| 47 | + |
| 48 | +## stratify days by month and build histograms for each month-threshold pair |
| 49 | +names(ranks_df) <- paste('tau', tau, sep='') |
| 50 | + |
| 51 | +m <- c(1, 4, 7, 10) # Jan, Apr, Jul, Oct |
| 52 | +dates <- seq.Date(as.Date('2002-01-02'), as.Date('2015-12-30'), by='day') |
| 53 | +date_idx <- (month(dates) %in% m) |
| 54 | + |
| 55 | +## fit beta parameters to density fte hists |
| 56 | +set.seed(20) |
| 57 | +down_fit_tab <- ranks_df %>% |
| 58 | + mutate(month = month(dates[date_idx])) %>% |
| 59 | + melt(id.vars='month', variable.name='tau', value.name='rank') %>% |
| 60 | + mutate(rank = sapply((rank-0.5)/12, disagg_rank)) %>% |
| 61 | + group_by(tau, month) %>% |
| 62 | + drop_na() %>% |
| 63 | + summarise(params=paste(fitdist(rank,'beta')$estimate, collapse=" ")) %>% |
| 64 | + separate(params, c('a', 'b'), sep=" ") %>% |
| 65 | + mutate(a=round(as.numeric(a), 3), b=round(as.numeric(b),3)) %>% |
| 66 | + unite(params, a:b, sep = ", ") |
| 67 | + |
| 68 | +## build dataframe with months and tau as factors to facet over |
| 69 | +month_labs <- rep("", 12) |
| 70 | +month_labs[c(1,4,7,10)] <- c("January", "April", "July", "October") |
| 71 | +tau_labs <- c(tau5=TeX(paste("$\\tau = $", "5 mm")), |
| 72 | + tau10=TeX(paste("$\\tau = $", "10 mm")), |
| 73 | + tau20=TeX(paste("$\\tau = $", "20 mm"))) |
| 74 | + |
| 75 | +ranks_df <- ranks_df %>% |
| 76 | + mutate(month = month(dates[date_idx])) %>% |
| 77 | + melt(id.vars='month', variable.name='tau', value.name='rank') %>% |
| 78 | + mutate(month = as.factor(month_labs[month])) %>% |
| 79 | + mutate(rank = (rank-0.5)/12) |
| 80 | + |
| 81 | +ranks_df$month <- factor(ranks_df$month, |
| 82 | + levels = c("January", "April", "July", "October"), |
| 83 | + labels = c("bold(January)", "bold(April)", "bold(July)", "bold(October)")) |
| 84 | +levels(ranks_df$tau) <- tau_labs |
| 85 | + |
| 86 | + |
| 87 | +png("fte_downscaled.png", units="in", height=6.2, width=8, res=200, pointsize=10) |
| 88 | + |
| 89 | +ranks_df %>% |
| 90 | + ggplot(aes(x=rank)) + |
| 91 | + geom_hline(yintercept=1, linetype=3, size=0.3, color="grey") + |
| 92 | + geom_histogram(aes(y=..density..), bins=12, fill="black", color="white") + |
| 93 | + scale_y_continuous(breaks=seq(0,2)) + |
| 94 | + facet_grid(rows=vars(tau), cols=vars(month), labeller=label_parsed) + |
| 95 | + annotate("text", x=0.48, y=2.5, size=4, label=down_fit_tab$params) + |
| 96 | + labs(y="", x="") + |
| 97 | + theme_bw() + |
| 98 | + theme(legend.title = element_blank(), |
| 99 | + strip.background = element_blank(), |
| 100 | + text = element_text(color="black"), |
| 101 | + strip.text = element_text(size=12), |
| 102 | + axis.text = element_text(size=9, color="black"), |
| 103 | + axis.text.x = element_blank(), |
| 104 | + axis.ticks.x = element_blank(), |
| 105 | + panel.grid.minor = element_blank(), |
| 106 | + panel.grid.major.x = element_blank(), |
| 107 | + panel.grid.major = element_blank(), |
| 108 | + aspect.ratio = 1/1, |
| 109 | + plot.margin = unit(c(0,0,0,0), "cm")) |
| 110 | + |
| 111 | +dev.off() |
| 112 | + |
| 113 | + |
0 commit comments