alexdum commited on
Commit
99d9397
·
1 Parent(s): ea4c284

feat: add interactive WMO station climatographs and update parameter binning logic for annual data

Browse files
Files changed (4) hide show
  1. funs/render_station_plots.R +170 -0
  2. global.R +2 -0
  3. server.R +49 -3
  4. ui.R +10 -1
funs/render_station_plots.R ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #' Generate Interactive Climatographs for a WMO Station
2
+ #' Each parameter gets its own subplot with its own legend.
3
+ #' All three temperatures share one plot using a red color ramp.
4
+ #' Arranged in a 2-column grid on desktop.
5
+ #' @param df The wide 12-month station matrix (Cols: Parameter, Jan-Dec, Annual)
6
+ #' @param station_name String name of the station for the chart title
7
+ render_station_plots <- function(df, station_name) {
8
+
9
+ months <- month.abb
10
+
11
+ # Melt into long format for easy slicing
12
+ long_df <- df %>%
13
+ select(Parameter, all_of(months)) %>%
14
+ tidyr::pivot_longer(cols = all_of(months), names_to = "Month", values_to = "Value") %>%
15
+ mutate(Month_F = factor(Month, levels = months))
16
+
17
+ get_param <- function(p_name) {
18
+ long_df %>% filter(Parameter == p_name) %>% arrange(Month_F)
19
+ }
20
+
21
+ # Common x-axis config to force chronological month ordering
22
+ xax <- list(categoryorder = "array", categoryarray = months, tickangle = -45)
23
+
24
+ plots <- list()
25
+ # Track legend group indices so each subplot gets its own legend entries
26
+ idx <- 0
27
+
28
+ # ---------------------------------------------------------------------------
29
+ # 1. Temperature Envelope — red ramp: light (Min) → medium (Mean) → dark (Max)
30
+ # ---------------------------------------------------------------------------
31
+ tmax <- get_param("Max Temperature (\u00b0C)")
32
+ tavg <- get_param("Mean Temperature (\u00b0C)")
33
+ tmin <- get_param("Min Temperature (\u00b0C)")
34
+ has_temp <- (nrow(tmax) > 0 || nrow(tavg) > 0 || nrow(tmin) > 0)
35
+
36
+ if (has_temp) {
37
+ idx <- idx + 1
38
+ p <- plot_ly()
39
+ if (nrow(tmax) > 0) {
40
+ p <- p %>% add_trace(x = tmax$Month_F, y = tmax$Value, type = "scatter",
41
+ mode = "lines+markers", name = "Max Temp",
42
+ legendgroup = paste0("g", idx),
43
+ line = list(color = "#8b0000", width = 2.5),
44
+ marker = list(color = "#8b0000", size = 6))
45
+ }
46
+ if (nrow(tavg) > 0) {
47
+ p <- p %>% add_trace(x = tavg$Month_F, y = tavg$Value, type = "scatter",
48
+ mode = "lines+markers", name = "Mean Temp",
49
+ legendgroup = paste0("g", idx),
50
+ line = list(color = "#e63946", width = 2.5),
51
+ marker = list(color = "#e63946", size = 6))
52
+ }
53
+ if (nrow(tmin) > 0) {
54
+ p <- p %>% add_trace(x = tmin$Month_F, y = tmin$Value, type = "scatter",
55
+ mode = "lines+markers", name = "Min Temp",
56
+ legendgroup = paste0("g", idx),
57
+ line = list(color = "#f4a0a0", width = 2.5),
58
+ marker = list(color = "#f4a0a0", size = 6))
59
+ }
60
+ if (nrow(tmax) > 0 && nrow(tmin) > 0) {
61
+ p <- p %>%
62
+ add_trace(x = tmax$Month_F, y = tmax$Value, type = "scatter", mode = "lines",
63
+ showlegend = FALSE, line = list(color = "transparent"),
64
+ legendgroup = paste0("g", idx)) %>%
65
+ add_trace(x = tmin$Month_F, y = tmin$Value, type = "scatter", mode = "lines",
66
+ showlegend = FALSE, line = list(color = "transparent"),
67
+ fill = "tonexty", fillcolor = "rgba(230,57,70,0.10)",
68
+ legendgroup = paste0("g", idx))
69
+ }
70
+ p <- p %>% layout(xaxis = xax,
71
+ yaxis = list(title = "Temperature (\u00b0C)", zeroline = TRUE, zerolinecolor = "#ccc"))
72
+ plots[[length(plots) + 1]] <- p
73
+ }
74
+
75
+ # ---------------------------------------------------------------------------
76
+ # 2. Precipitation Amount (mm)
77
+ # ---------------------------------------------------------------------------
78
+ prcp <- get_param("Precipitation (mm)")
79
+ if (nrow(prcp) > 0) {
80
+ idx <- idx + 1
81
+ p <- plot_ly(x = prcp$Month_F, y = prcp$Value, type = "bar", name = "Precipitation (mm)",
82
+ legendgroup = paste0("g", idx),
83
+ marker = list(color = "rgba(40,140,200,0.75)",
84
+ line = list(color = "rgba(20,80,140,1)", width = 1))) %>%
85
+ layout(xaxis = xax, yaxis = list(title = "Precipitation (mm)"))
86
+ plots[[length(plots) + 1]] <- p
87
+ }
88
+
89
+ # ---------------------------------------------------------------------------
90
+ # 3. Precipitation Days (>= 1 mm)
91
+ # ---------------------------------------------------------------------------
92
+ dp01 <- get_param("Precipitation Days (>=1mm)")
93
+ if (nrow(dp01) > 0) {
94
+ idx <- idx + 1
95
+ p <- plot_ly(x = dp01$Month_F, y = dp01$Value, type = "bar", name = "Rain Days",
96
+ legendgroup = paste0("g", idx),
97
+ marker = list(color = "rgba(100,100,200,0.7)",
98
+ line = list(color = "rgba(50,50,120,1)", width = 1))) %>%
99
+ layout(xaxis = xax, yaxis = list(title = "Days (\u2265 1 mm)"))
100
+ plots[[length(plots) + 1]] <- p
101
+ }
102
+
103
+ # ---------------------------------------------------------------------------
104
+ # 4. Sea Level Pressure (hPa)
105
+ # ---------------------------------------------------------------------------
106
+ mslp <- get_param("Mean Sea Level Pressure (hPa)")
107
+ if (nrow(mslp) > 0) {
108
+ idx <- idx + 1
109
+ y_lo <- min(mslp$Value, na.rm = TRUE) - 3
110
+ y_hi <- max(mslp$Value, na.rm = TRUE) + 3
111
+ p <- plot_ly(x = mslp$Month_F, y = mslp$Value, type = "scatter", mode = "lines+markers",
112
+ name = "MSLP (hPa)", legendgroup = paste0("g", idx),
113
+ line = list(color = "steelblue", width = 2),
114
+ marker = list(color = "steelblue", size = 5),
115
+ fill = "tozeroy", fillcolor = "rgba(200,220,240,0.35)") %>%
116
+ layout(xaxis = xax, yaxis = list(title = "Pressure (hPa)", range = c(y_lo, y_hi)))
117
+ plots[[length(plots) + 1]] <- p
118
+ }
119
+
120
+ # ---------------------------------------------------------------------------
121
+ # 5. Mean Vapor Pressure (hPa)
122
+ # ---------------------------------------------------------------------------
123
+ mnvp <- get_param("Mean Vapor Pressure (hPa)")
124
+ if (nrow(mnvp) > 0) {
125
+ idx <- idx + 1
126
+ p <- plot_ly(x = mnvp$Month_F, y = mnvp$Value, type = "scatter", mode = "lines+markers",
127
+ name = "Vapor Press. (hPa)", legendgroup = paste0("g", idx),
128
+ line = list(color = "#238b45", width = 2),
129
+ marker = list(color = "#238b45", size = 5),
130
+ fill = "tozeroy", fillcolor = "rgba(153,216,201,0.3)") %>%
131
+ layout(xaxis = xax, yaxis = list(title = "Vapor Pressure (hPa)"))
132
+ plots[[length(plots) + 1]] <- p
133
+ }
134
+
135
+ # ---------------------------------------------------------------------------
136
+ # 6. Sunshine Duration (hours)
137
+ # ---------------------------------------------------------------------------
138
+ tsun <- get_param("Sunshine Duration (hours)")
139
+ if (nrow(tsun) > 0) {
140
+ idx <- idx + 1
141
+ p <- plot_ly(x = tsun$Month_F, y = tsun$Value, type = "bar", name = "Sunshine (h)",
142
+ legendgroup = paste0("g", idx),
143
+ marker = list(color = "rgba(255,200,0,0.75)",
144
+ line = list(color = "rgba(200,140,0,1)", width = 1))) %>%
145
+ layout(xaxis = xax, yaxis = list(title = "Sunshine (hours)"))
146
+ plots[[length(plots) + 1]] <- p
147
+ }
148
+
149
+ # ---------------------------------------------------------------------------
150
+ # Assemble as 2-column grid (3 rows x 2 cols)
151
+ # ---------------------------------------------------------------------------
152
+ n <- length(plots)
153
+ if (n == 0) return(plotly_empty())
154
+
155
+ n_rows <- ceiling(n / 2)
156
+
157
+ final <- subplot(plots, nrows = n_rows, titleY = TRUE,
158
+ margin = c(0.06, 0.06, 0.08, 0.08)) %>%
159
+ layout(
160
+ title = list(text = paste("WMO 1991\u20132020 Climatological Normals:", station_name), x = 0.05),
161
+ hovermode = "x unified",
162
+ showlegend = TRUE,
163
+ margin = list(t = 60, b = 40, l = 65, r = 40)
164
+ ) %>%
165
+ config(displayModeBar = TRUE, displaylogo = FALSE,
166
+ toImageButtonOptions = list(format = "png",
167
+ filename = paste0("WMO_Normals_", station_name)))
168
+
169
+ return(final)
170
+ }
global.R CHANGED
@@ -5,6 +5,7 @@ library(sf)
5
  library(dplyr)
6
  library(leaflet)
7
  library(DT)
 
8
 
9
  # Map Configurations matching GHCNM
10
  ofm_positron_style <- "https://tiles.openfreemap.org/styles/positron"
@@ -18,6 +19,7 @@ cat("Loading multi-annual parameter composites...\n")
18
 
19
  # Source custom functions
20
  source("funs/load_wmo_data.R", local = TRUE)
 
21
 
22
  tavg_sf <- load_wmo_data("wmo_normals_9120_TAVG_min24.csv")
23
  tmax_sf <- load_wmo_data("wmo_normals_9120_TMAX_min24.csv")
 
5
  library(dplyr)
6
  library(leaflet)
7
  library(DT)
8
+ library(plotly)
9
 
10
  # Map Configurations matching GHCNM
11
  ofm_positron_style <- "https://tiles.openfreemap.org/styles/positron"
 
19
 
20
  # Source custom functions
21
  source("funs/load_wmo_data.R", local = TRUE)
22
+ source("funs/render_station_plots.R", local = TRUE)
23
 
24
  tavg_sf <- load_wmo_data("wmo_normals_9120_TAVG_min24.csv")
25
  tmax_sf <- load_wmo_data("wmo_normals_9120_TMAX_min24.csv")
server.R CHANGED
@@ -230,7 +230,11 @@ server <- function(input, output, session) {
230
  label_map <- c(TAVG="Mean Temp", TMAX="Max Temp", TMIN="Min Temp")
231
  prefix <- paste(time, label_map[param], sep=" ")
232
  } else if (param == "PRCP") {
233
- bins <- c(0, 10, 25, 50, 75, 100, 150, 200, 300, 500, 1000, 2000, Inf)
 
 
 
 
234
  # Exact 12-stage ECMWF precipitation scale (pale green -> magenta -> white)
235
  palette_colors <- c("#e4facc", "#aadc8c", "#68b444", "#28841c", "#00734c",
236
  "#0096c8", "#0046b4", "#500096", "#b40096", "#ff00b4",
@@ -238,13 +242,21 @@ server <- function(input, output, session) {
238
  units <- "mm"
239
  prefix <- paste(time, "Precip")
240
  } else if (param == "DP01") {
241
- bins <- c(0, 10, 20, 40, 60, 80, 100, 125, 150, 200, 250, 300, 365)
 
 
 
 
242
  palette_colors <- colorRampPalette(c("#ffffff", "#d0d1e6", "#a6bddb", "#74a9cf",
243
  "#3690c0", "#0570b0", "#045a8d", "#023858"))(length(bins) - 1)
244
  units <- "days"
245
  prefix <- paste(time, "Precip Days")
246
  } else if (param == "TSUN") {
247
- bins <- c(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, Inf)
 
 
 
 
248
  palette_colors <- colorRampPalette(c("#4d4d4d", "#b3b3b3", "#e0e0e0", "#ffffb2",
249
  "#fed976", "#feb24c", "#fd8d3c", "#f03b20", "#bd0026"))(length(bins) - 1)
250
  units <- "hours"
@@ -440,4 +452,38 @@ server <- function(input, output, session) {
440
  borderLeft = '2px solid #ddd'
441
  )
442
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
  }
 
230
  label_map <- c(TAVG="Mean Temp", TMAX="Max Temp", TMIN="Min Temp")
231
  prefix <- paste(time, label_map[param], sep=" ")
232
  } else if (param == "PRCP") {
233
+ if (time == "Annual") {
234
+ bins <- c(0, 50, 100, 250, 500, 750, 1000, 1500, 2000, 3000, 5000, 10000, Inf)
235
+ } else {
236
+ bins <- c(0, 10, 25, 50, 75, 100, 150, 200, 300, 500, 1000, 2000, Inf)
237
+ }
238
  # Exact 12-stage ECMWF precipitation scale (pale green -> magenta -> white)
239
  palette_colors <- c("#e4facc", "#aadc8c", "#68b444", "#28841c", "#00734c",
240
  "#0096c8", "#0046b4", "#500096", "#b40096", "#ff00b4",
 
242
  units <- "mm"
243
  prefix <- paste(time, "Precip")
244
  } else if (param == "DP01") {
245
+ if (time == "Annual") {
246
+ bins <- c(0, 10, 25, 50, 75, 100, 125, 150, 200, 250, 300, 365)
247
+ } else {
248
+ bins <- c(0, 1, 3, 5, 7, 10, 12, 15, 20, 25, 28, 31)
249
+ }
250
  palette_colors <- colorRampPalette(c("#ffffff", "#d0d1e6", "#a6bddb", "#74a9cf",
251
  "#3690c0", "#0570b0", "#045a8d", "#023858"))(length(bins) - 1)
252
  units <- "days"
253
  prefix <- paste(time, "Precip Days")
254
  } else if (param == "TSUN") {
255
+ if (time == "Annual") {
256
+ bins <- c(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, Inf)
257
+ } else {
258
+ bins <- c(0, 25, 50, 100, 150, 200, 250, 300, 350, Inf)
259
+ }
260
  palette_colors <- colorRampPalette(c("#4d4d4d", "#b3b3b3", "#e0e0e0", "#ffffb2",
261
  "#fed976", "#feb24c", "#fd8d3c", "#f03b20", "#bd0026"))(length(bins) - 1)
262
  units <- "hours"
 
452
  borderLeft = '2px solid #ddd'
453
  )
454
  })
455
+
456
+ # -----------------------------------------------------------------------------------
457
+ # Data Export & Plotly Graphing
458
+ # -----------------------------------------------------------------------------------
459
+
460
+ # Download Handler
461
+ output$download_normals <- downloadHandler(
462
+ filename = function() {
463
+ sid <- clicked_wmo_id()
464
+ paste0("WMO_Normals_1991-2020_", sid, ".csv")
465
+ },
466
+ content = function(file) {
467
+ df <- station_dashboard_data()
468
+ write.csv(df, file, row.names = FALSE)
469
+ }
470
+ )
471
+
472
+ # Plotly Subplot Canvas
473
+ output$climatograph_plot <- renderPlotly({
474
+ df <- station_dashboard_data()
475
+ req(df)
476
+
477
+ wmo <- clicked_wmo_id()
478
+
479
+ # Fetch top-level metadata safely for chart title
480
+ meta_df <- if(inherits(tavg_sf, "sf") && nrow(tavg_sf) > 0) st_drop_geometry(tavg_sf) %>% filter(WMO_ID == wmo) else data.frame()
481
+ if (nrow(meta_df) == 0 && inherits(prcp_sf, "sf") && nrow(prcp_sf) > 0) {
482
+ meta_df <- st_drop_geometry(prcp_sf) %>% filter(WMO_ID == wmo)
483
+ }
484
+ s_name <- if(nrow(meta_df) > 0) meta_df$Station_Name[1] else as.character(wmo)
485
+
486
+ # Delegate to the standalone plotting function
487
+ render_station_plots(df, s_name)
488
+ })
489
  }
ui.R CHANGED
@@ -120,7 +120,16 @@ ui <- page_navbar(
120
  style = "padding: 20px;",
121
  uiOutput("dashboard_header"),
122
  hr(),
123
- DT::DTOutput("dashboard_data")
 
 
 
 
 
 
 
 
 
124
  )
125
  ),
126
  nav_spacer(),
 
120
  style = "padding: 20px;",
121
  uiOutput("dashboard_header"),
122
  hr(),
123
+ div(
124
+ style = "text-align: right; margin-bottom: 10px;",
125
+ downloadButton("download_normals", "Export CSV", class = "btn-success", icon = icon("file-excel"))
126
+ ),
127
+ DT::DTOutput("dashboard_data"),
128
+ hr(),
129
+ div(
130
+ style = "margin-top: 30px; margin-bottom: 30px;",
131
+ plotlyOutput("climatograph_plot", height = "900px")
132
+ )
133
  )
134
  ),
135
  nav_spacer(),