-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
345 lines (315 loc) Β· 10.7 KB
/
Copy pathapp.R
File metadata and controls
345 lines (315 loc) Β· 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
library(shiny)
library(leaflet)
library(bslib)
library(tidyverse)
library(glue)
library(httr)
library(jsonlite)
library(akima)
library(bsicons)
library(memoise)
library(leaflet)
library(plotly)
#TODO make text bigger on polar graph
#TODO add maestro (package) script that automatically loads data during early hours
#TODO Memoise package to make polar plot caching plots much faster
# Load in Data
# Reading Erddap from direct link
current_time <- format(Sys.time(), "T%H:%M:%SZ", tz = "UTC") %>%
str_split_1(pattern = ":")
current_date <- Sys.Date()
erddap_url <- glue("https://erddap.sensors.axds.co/erddap/tabledap/edu_ucsd_cdip_142.json?time%2Clatitude%2Clongitude%2Cz%2Csea_surface_wave_mean_period%2Csea_surface_wave_period_at_variance_spectral_density_maximum%2Csea_surface_wave_significant_height%2Csea_surface_wave_from_direction%2Cstation&time%3E=2000-01-01T04%3A45%3A00Z&time%3C={current_date}{current_time[1]}%3A{current_time[2]}%3A{current_time[3]}")
res = GET(erddap_url)
full_erddap_list <- fromJSON(rawToChar(res$content))
full_erddap_data <- as.data.frame(full_erddap_list$table$rows)
colnames(full_erddap_data) <- full_erddap_list$table$columnNames
####Filter and mutate to get non-blank wave periods----
filtered_erddap_data <- full_erddap_data %>%
filter(!is.na(sea_surface_wave_mean_period)) %>%
mutate(
time = as.Date(time),
year = format(time, "%Y"),
month = format(time, "%m"),
day = format(time, "%d"),
month_day = format(time, "%m-%d"),
across(starts_with("sea"), as.numeric),
sea_surface_wave_significant_height_ft = sea_surface_wave_significant_height * 3.28084,
month = lubridate::month(time, label = TRUE)
) %>%
relocate(c("year", "month", "day", "month_day"), .after = time) %>%
mutate(
season = case_when(
month %in% c("Dec", "Jan", "Feb") ~ "Winter",
month %in% c("Mar", "Apr", "May") ~ "Spring",
month %in% c("Jun", "Jul", "Aug") ~ "Summer",
TRUE ~ "Autumn"
)
)
####Summarize by month----
summarized_erddap_data <- filtered_erddap_data %>%
group_by(month) %>% # group
group_split() %>% # split
lapply(., function(x) {
grouped_frame <- with(
x,
interp(
x = sea_surface_wave_from_direction,
y = sea_surface_wave_significant_height_ft,
z = sea_surface_wave_mean_period,
#extrap = TRUE,
nx = 100,
ny = 100,
duplicate = "mean"
)
) %>%
interp2xyz() %>%
as.data.frame()
group_month <- x %>%
pull(month) %>%
unique()
final_frame <- grouped_frame %>%
mutate(month = group_month) %>%
filter(!is.na(z))
}) %>%
bind_rows() %>%
rename(
sea_surface_wave_from_direction = x,
sea_surface_wave_significant_height_ft = y,
sea_surface_wave_mean_period = z
)
unique_months <- unique(summarized_erddap_data$month)
first_date_of_data_collection <- min(filtered_erddap_data$time)
last_date_of_data_collection <- max(filtered_erddap_data$time)
buoy_position <- as.numeric(c(unique(filtered_erddap_data$longitude), unique(filtered_erddap_data$latitude)))
####Median Wave Stats processing----
median_stats_height_per_month_day <- filtered_erddap_data %>%
group_by(month_day) %>%
summarize(
median_wave_height = median(sea_surface_wave_significant_height_ft),
median_period = median(sea_surface_wave_mean_period)
) %>%
mutate(
month_day = paste("2024", month_day, sep = "-"),
month_day = as.Date(month_day, format = "%Y-%m-%d")
)
####Current Wave Stats processing----
current_wave_stats <- filtered_erddap_data %>%
arrange(desc(time)) %>%
slice(1)
####Define Wave Time Series Cards----
wave_series_cards <- list(
card(
card_header(glue("Median Wave Height (Ft) by Date ({first_date_of_data_collection}-{last_date_of_data_collection})")),
plotlyOutput("interactive_wave_height_plot"),
full_screen = TRUE
),
card(
card_header(glue("Median Wave Period (Seconds) by Date ({first_date_of_data_collection}-{last_date_of_data_collection})")),
plotlyOutput("interactive_wave_period_plot"),
full_screen = TRUE
)
)
####Define Current Wave Stat Value Boxes----
current_wave_stat_value_boxes <- list(
value_box(
title = "Current Wave Height",
value = paste0(round(current_wave_stats$sea_surface_wave_significant_height_ft, 2), "ft"),
showcase = bs_icon("tsunami")
),
value_box(
title = "Current Wave Period",
value = paste0(round(current_wave_stats$sea_surface_wave_mean_period, 2), "s"),
showcase = bs_icon("water")
),
value_box(
title = "Current Wave Direction",
value = paste0(round(current_wave_stats$sea_surface_wave_from_direction, 2), "Β°"),
showcase = bs_icon("compass")
)
)
####Wave Period Plotly----
wave_period_plotly <- plot_ly(data = median_stats_height_per_month_day,
x = ~month_day,
y = ~median_period,
type = 'scatter',
mode = 'markers',
#marker = list(color = '00b4d8', size = 5),
color = I('#00b4d8')
) %>%
layout(
xaxis = list(
title = 'Date',
tickformat = '%b<br>%d', # Format as abbreviated month
dtick = 'M1' # 1-month interval for x-axis
),
yaxis = list(
title = ''
),
showlegend = FALSE,
font = list(family = 'Times New Roman', size = 14, color = '#000000')
) %>%
add_lines(
x = ~month_day,
y = ~fitted(loess(median_period ~ as.numeric(month_day))),
line = list(color = '#0077b6', width = 2),
name = 'Smoothed Line'
)
####Wave height Plotly----
wave_height_plotly <- plot_ly(data = median_stats_height_per_month_day,
x = ~month_day,
y = ~median_wave_height,
type = 'scatter',
mode = 'markers',
#marker = list(color = '00b4d8', size = 5),
color = I('#00b4d8')
) %>%
layout(
xaxis = list(
title = 'Date',
tickformat = '%b<br>%d', # Format as abbreviated month
dtick = 'M1' # 1-month interval for x-axis
),
yaxis = list(
title = ''
),
showlegend = FALSE,
font = list(family = 'Times New Roman', size = 14, color = '#000000')
) %>%
add_lines(
x = ~month_day,
y = ~fitted(loess(median_wave_height ~ as.numeric(month_day))),
line = list(color = '#0077b6', width = 2),
name = 'Smoothed Line'
)
####Buoy leaflet icon----
icons_list <- icons(
iconUrl = 'https://cdn1.iconfinder.com/data/icons/unigrid-bluetone-maps-travel-vol-1/60/007_041_buoy_marine_nautical-1024.png',
iconWidth = c(25, 45, 20), iconHeight = c(25, 45, 20)
)
####Define UI----
ui <- page_fillable(
navset_card_tab(
#Nav Panel Wave Time Series
nav_panel(
"Current/Historical Swell Stats",
style = "background-color: #ade8f4;",
layout_columns(
fill = FALSE,
!!!current_wave_stat_value_boxes
),
!!!wave_series_cards
),
#Wave Rose nav panel
nav_panel(
"Wave Rose",
id = "wave_rose_tab",
#Column layouts
layout_columns(
col_widths = c(2, 10),
#Page Sidebar
page_sidebar(
title = "Filters",
open = "desktop",
fill = TRUE,
#Sidebar
sidebar = sidebar(
title = "Controls",
#Select Input
selectInput(
'month_selecter',
'Months Filter',
selected = unique_months,
choices = unique_months,
multiple=TRUE,
selectize=TRUE
),
#Action Button
actionButton("update_months", "Update Months")
),
),
#Plot Output in first Nav Panel
plotOutput("wave_polar_plot", width = "100%", height = "100%", fill = TRUE)
),
),
nav_panel(
"Buoy Map",
leafletOutput("buoy_leaflet_map", width = "100%", height = "100%")
),
nav_spacer(),
nav_menu(
"Link to Data Source",
nav_item(
a("CDIP Data", href = "https://sensors.ioos.us/#metadata/103447/station/data", target = "_blank")
),
),
),
id = "tab"
)
####Define Server----
server <- function(input, output) {
# Store the selected months, initially set to default values
selected_months <- reactiveVal(unique_months)
# Update the selected months only when the action button is clicked
observeEvent(input$update_months, {
selected_months(input$month_selecter)
})
# Reactive data based on selected months
data <- reactive({
summarized_erddap_data %>%
filter(month %in% selected_months()) # Use the reactive value for selected months
})
output$wave_polar_plot <- renderPlot({
ggplot(data(),
aes(x = sea_surface_wave_from_direction, y = sea_surface_wave_significant_height_ft, z = sea_surface_wave_mean_period)) +
geom_contour_filled(alpha = 0.75, breaks = c(2, 4, 6, 8, 10, 12, 14, 16)) +
scale_x_continuous(limits = c(180, 360),
breaks = seq(0, 360, by = 45),
minor_breaks = seq(0, 360, by = 15),
labels = function(x) paste0(x, " Β°")) +
coord_radial(r.axis.inside = TRUE, expand = FALSE, start = pi, end = 2 * pi) +
scale_y_continuous(labels = function(x) paste0(x, " ft")) +
labs(
fill = "Period (Sec)",
#x = "Direction (Degrees)",
#y = "Mean Significant Wave Height (Ft)",
#title = "CDIP San Francisco Bar, CA Buoy"
) +
facet_wrap(~ month, nrow = 2) +
theme_minimal() +
theme(legend.position="bottom",
legend.title.position = "bottom",
legend.title = element_text(hjust = 0.5),
legend.key.height= unit(1, 'cm'),
legend.key.width= unit(2, 'cm'),
axis.title.x=element_blank(),
axis.title.y=element_blank()
) +
guides(fill = guide_colorsteps(
ticks = TRUE,
show.limits = TRUE,
even.steps = FALSE,
frame.linewidth = 0.55,
frame.colour = "black",
ticks.colour = "black",
ticks.linewidth = 0.3)
)
}) %>% bindCache(data())
output$interactive_wave_height_plot <- renderPlotly({
wave_height_plotly
})
output$interactive_wave_period_plot <- renderPlotly({
wave_period_plotly
})
output$buoy_leaflet_map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -122.55, lat = 37.75, zoom = 10) %>%
addMarkers(
lng = buoy_position[1],
lat = buoy_position[2],
icon = icons_list,
label = "142 - San Francisco Bar, CA (46237)")
})
}
# Run the application
shinyApp(ui = ui, server = server)