-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
213 lines (181 loc) · 6.73 KB
/
app.R
File metadata and controls
213 lines (181 loc) · 6.73 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
library(shiny)
library(leaflet)
library(rerddap)
library(surveyjoin)
library(viridis)
library(dplyr)
# ── Static data loaded once at startup ──────────────────────────────────────
# Load NWFSC.Combo grid points
grid_pts <- surveyjoin::nwfsc_grid |>
dplyr::filter(survey == "NWFSC.Combo") |>
dplyr::mutate(cell_id = dplyr::row_number()) |>
dplyr::select(cell_id, lon, lat)
# Fetch available date range from ERDDAP metadata
erddap_url <- "https://coastwatch.pfeg.noaa.gov/erddap/"
dataset_id <- "ncdcOisst21Agg_LonPM180"
get_date_range <- function() {
tryCatch({
meta <- rerddap::info(dataset_id, url = erddap_url)
time_row <- meta$alldata$time[
meta$alldata$time$attribute_name == "actual_range", ]
if (nrow(time_row) == 0) stop("No time range found")
# actual_range is two doubles (seconds since 1970-01-01)
vals <- as.numeric(strsplit(time_row$value, ",\\s*")[[1]])
list(
min = as.Date(as.POSIXct(vals[1], origin = "1970-01-01", tz = "UTC")),
max = as.Date(as.POSIXct(vals[2], origin = "1970-01-01", tz = "UTC"))
)
}, error = function(e) {
list(min = as.Date("1981-09-01"), max = Sys.Date() - 2)
})
}
date_range <- get_date_range()
# ── UI ───────────────────────────────────────────────────────────────────────
ui <- fluidPage(
titlePanel("NWFSC Survey Grid - Sea Surface Temperature"),
sidebarLayout(
sidebarPanel(
width = 3,
dateInput(
inputId = "selected_date",
label = "Select Date",
value = date_range$max - 1,
min = date_range$min,
max = date_range$max,
format = "yyyy-mm-dd"
),
hr(),
helpText("Click a grid point on the map to see its SST details."),
hr(),
verbatimTextOutput("click_info")
),
mainPanel(
width = 9,
leafletOutput("map", height = "600px")
)
)
)
# ── Server ───────────────────────────────────────────────────────────────────
server <- function(input, output, session) {
# Fetch SST from ERDDAP – only re-runs when the date changes
sst_data <- eventReactive(input$selected_date, {
date_str <- format(input$selected_date, "%Y-%m-%d")
# Bounding box around the nwfsc_grid extent
lon_min <- floor(min(grid_pts$lon)) - 0.5
lon_max <- ceiling(max(grid_pts$lon)) + 0.5
lat_min <- floor(min(grid_pts$lat)) - 0.5
lat_max <- ceiling(max(grid_pts$lat)) + 0.5
withProgress(message = "Fetching SST data…", value = 0.5, {
raw <- tryCatch(
rerddap::griddap(
datasetx = dataset_id,
url = erddap_url,
time = c(date_str, date_str),
longitude = c(lon_min, lon_max),
latitude = c(lat_min, lat_max),
fields = "sst",
fmt = "csv"
),
error = function(e) NULL
)
})
if (is.null(raw)) return(NULL)
sst_df <- raw$data |>
dplyr::rename(lon = longitude, lat = latitude) |>
dplyr::filter(!is.na(sst)) |>
dplyr::select(lon, lat, sst)
sst_df
})
# Spatially match each grid point to the nearest SST observation
joined_data <- reactive({
sst_df <- sst_data()
if (is.null(sst_df) || nrow(sst_df) == 0) return(NULL)
# Round to OISST grid resolution (0.25°) for fast join
res <- 0.25
sst_df <- sst_df |>
dplyr::mutate(
lon_r = round(lon / res) * res,
lat_r = round(lat / res) * res
)
grid_pts |>
dplyr::mutate(
lon_r = round(lon / res) * res,
lat_r = round(lat / res) * res
) |>
dplyr::left_join(
sst_df |> dplyr::select(lon_r, lat_r, sst),
by = c("lon_r", "lat_r")
) |>
dplyr::select(cell_id, lon, lat, sst) |>
dplyr::filter(!is.na(sst))
})
# ── Base map (rendered once) ──────────────────────────────────────────────
output$map <- renderLeaflet({
leaflet() |>
addProviderTiles(providers$CartoDB.Positron) |>
setView(lng = -124.5, lat = 46.0, zoom = 5)
})
# ── Update coloured points whenever joined_data changes ──────────────────
observe({
df <- joined_data()
proxy <- leafletProxy("map")
proxy |> clearGroup("sst_points") |> removeControl("legend")
if (is.null(df) || nrow(df) == 0) return()
pal <- colorNumeric(
palette = viridis::viridis(256),
domain = df$sst,
na.color = "transparent"
)
proxy |>
addCircleMarkers(
data = df,
lng = ~lon,
lat = ~lat,
radius = 4,
color = ~pal(sst),
fillColor = ~pal(sst),
fillOpacity = 0.85,
stroke = FALSE,
group = "sst_points",
layerId = ~cell_id,
label = ~paste0("Cell ", cell_id, " | SST: ", round(sst, 2), " °C")
) |>
addLegend(
position = "bottomright",
pal = pal,
values = df$sst,
title = "SST (°C)",
layerId = "legend"
)
})
# ── Click popup ──────────────────────────────────────────────────────────
observeEvent(input$map_marker_click, {
click <- input$map_marker_click
df <- joined_data()
if (is.null(df) || is.null(click$id)) return()
row <- df[df$cell_id == as.integer(click$id), ]
if (nrow(row) == 0) return()
mean_sst <- round(row$sst, 2)
output$click_info <- renderText({
paste0(
"Grid Cell ID : ", row$cell_id, "\n",
"Longitude : ", round(row$lon, 4), "\n",
"Latitude : ", round(row$lat, 4), "\n",
"SST : ", mean_sst, " °C\n",
"Date : ", format(input$selected_date, "%Y-%m-%d")
)
})
leafletProxy("map") |>
addPopups(
lng = click$lng,
lat = click$lat,
popup = paste0(
"<b>Cell ID:</b> ", row$cell_id, "<br>",
"<b>SST:</b> ", mean_sst, " °C<br>",
"<b>Date:</b> ", format(input$selected_date, "%Y-%m-%d")
)
)
})
}
# ── Launch ───────────────────────────────────────────────────────────────────
shinyApp(ui = ui, server = server)