-
Notifications
You must be signed in to change notification settings - Fork 0
/
waterTempThresholds.Rmd
444 lines (347 loc) · 13.2 KB
/
waterTempThresholds.Rmd
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
---
title: "Data sketches of water temperatures and thresholds"
knit: (function(input_file, encoding) {
out_dir <- 'docs';
rmarkdown::render(input_file,
encoding=encoding,
output_file=file.path(dirname(input_file), out_dir, 'index.html'))})
output: html_document
---
```{r setup, include=FALSE}
here::i_am("waterTempThresholds.Rmd")
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
library(plotly)
library(tidyverse)
library(dplyr)
library(magrittr)
library(chron)
library(stringr)
library(htmltools)
library(lubridate)
library(dataRetrieval)
# plotting tools
library(ggplot2)
library(ggthemes)
library(ggExtra)
library(ggtext)
library(gghighlight)
library(scales)
library(hrbrthemes)
# Spatial packages
library(sf)
library(maptools)
library(RColorBrewer)
library(mapview)
library(knitr)
library(rmarkdown)
# library(weatherData)
library(lunar)
# Tidal data
library(VulnToolkit)
library(Tides)
# timeseries
library(tsibble)
library(fable)
library(feasts)
library(xts)
library(here)
library(units)
source(here::here("../LI_SaltMarshMonitoring/R/truBlue.R")) # lots of code went into trublue utilities from another project. consider making a package.
library(lunar)
library(suncalc)
library(extrafont)
```
### Water temperature stations
Exploring temperature data in the Peconics.
2 USGS guage stations exist with continuous data collection.
```{r sites}
h5("Data presented within this document have been made available through the",
a(href = 'http://waterdata.usgs.gov/nwis', "USGS NWIS Web Interface:")
)
# estuarySites <- whatNWISsites(stateCd = "NY",
# # water temp C
# parameterCd = "00010") %>%
# filter(site_tp_cd == "ES") %>%
# pull(site_no)
peconicSiteIDs <- c("01304200", "01304562")
# pulling continuous data down from USGS takes some time so this will run
# only when passing update as TRUE
update = FALSE
if (update) {
# get unit values for sites to explore
continuousTemps <- readNWISuv(
siteNumbers = peconicSiteIDs,
startDate = "2013-01-01",
#first full year of data collection 2013.
endDate = Sys.Date(),
# statCd = c("00001", "00002", "00003"),
parameterCd = c("00010", "62620")
) %>%
dataRetrieval::renameNWISColumns()
# filter out coded -9999s and 9999s
continuousTemps <-
continuousTemps %>% filter(Wtemp_Inst < 100 &
Wtemp_Inst > -10)
# save data to save time and processing each time.
write_rds(continuousTemps, here("data/continuousTemps.rds"))
} else{
continuousTemps <- read_rds(here("data/continuousTemps.rds"))
}
estuarySiteLocations <- readNWISsite(peconicSiteIDs) %>%
st_as_sf(coords = c("dec_long_va", "dec_lat_va"), crs = 4269) %>%
mutate(stationName = case_when(
station_nm == "ORIENT HARBOR AT ORIENT NY" ~ "Orient Harbor",
station_nm == "PECONIC RIVER AT COUNTY HWY 105 AT RIVERHEAD NY" ~ "Peconic River Rte 105"
))
# popup
popupLink <- function(siteName, siteNo){
url <- paste('<a href="https://waterdata.usgs.gov/nwis/inventory?agency_code=USGS&site_no=',
siteNo,'"','> Access data and site information from NWIS </a>', sep = "")
popupFinal <- paste(sep = "<br/>",
siteName,
url)
popupFinal
}
stationMap <- mapview(estuarySiteLocations, popup = popupLink(siteName = estuarySiteLocations$stationName, siteNo = estuarySiteLocations$site_no))
cntrcords <- c(mean(st_coordinates(estuarySiteLocations)[,1]),
mean(st_coordinates(estuarySiteLocations)[,2]))
stationMap@map %>% leaflet::setView(cntrcords[1], cntrcords[2], zoom = 10)
```
## Plots
Data has been collected since 2012, with 2013 being the first 'full' year of data collection.
```{r tempdata, echo=FALSE}
# go tsibble.
theme_set(theme_ipsum())
temps <- continuousTemps %>%
# join to get station names
left_join(estuarySiteLocations %>%
st_drop_geometry() %>%
select(site_no, stationName), by = "site_no") %>%
as_tsibble(key = stationName, index = dateTime) %>%
rename(tidalHt = X_NAVD88_62620_Inst) %>%
# fill_gaps() %>%
mutate(month = month(dateTime),
year = year(dateTime),
day_of_year = yday(dateTime),
commonDate = as.Date(paste0("2000-",format(dateTime, "%j")), "%Y-%j"),
year2019 = ifelse(year == 2019, 1, 0)) %>%
# Calc ecdf first -
group_by(stationName, year) %>%
mutate(calc_ecdf = ecdf(Wtemp_Inst)(Wtemp_Inst),
inv_calc_ecdf = 1- calc_ecdf,
totTime = length(Wtemp_Inst) * 6,
yearmins = 60*24*365.25, # minutes in a year
mins_yr = units::set_units(yearmins, minutes),
hours_yr = units::set_units(mins_yr, hours),
hours_above = hours_yr * inv_calc_ecdf,
hours_num = drop_units(hours_above),
days_num = drop_units(set_units(hours_above, days)))
```
```{r tempplot, echo=FALSE}
temps %>%
ggplot(aes(x = commonDate, y = Wtemp_Inst, group = year)) +
geom_line() +
gghighlight(year == 2022) +
scale_color_gradient(low = "grey", high = "blue", aesthetics = "color") +
# scale_color_viridis_c(option = "inferno") +
# facet_grid(stationName ~ year) +
facet_grid(stationName ~ .) +
scale_x_date(labels = function(x) format(x, "%d-%b")) +
theme(panel.spacing = unit(.5, "lines")) +
# scale_x_continuous(labels = function(x) format(as.Date(as.character(x), "%j"), "%d-%b")) +
theme(legend.position = "none", axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(y = "Temp (c)", x = NULL, title = "Water temperature through the year", subtitle = "2019 shown in blue")
```
```{r}
temps %>%
ggplot(aes(x = commonDate, y = Wtemp_Inst, group = year)) +
geom_line() +
gghighlight(year == 2022) +
# scale_color_gradient(low = "grey", high = "blue", aesthetics = "color") +
# scale_color_viridis_d(option = "inferno") +
# facet_grid(stationName ~ year) +
facet_grid(stationName ~ .) +
scale_x_date(labels = function(x) format(x, "%d-%b")) +
theme(panel.spacing = unit(.5, "lines")) +
scale_x_continuous(labels = function(x) format(as.Date(as.character(x), "%j"), "%d-%b")) +
theme(legend.position = "none", axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(y = "Temp (c)", x = NULL, title = "Water temperature through the year", subtitle = "2019 shown in blue")
```
### Cumulative Distribution Approach -
More to do... And think about...
Focus on the temps above threshold temps?
```{r message=FALSE, warning=FALSE, paged.print=FALSE}
plot_ecdf_temps <- temps %>% filter(year != 2022) %>%
ggplot(aes(x = Wtemp_Inst, color = as.character(year))) +
stat_ecdf(pad = F, geom = "line") +
scale_y_reverse() +
facet_wrap(~ stationName, nrow = 2) +
labs(color = NULL) +
# scale_y_continuous(trans = "time")
# scale_x_continuous(limits = c(min = 25, max = 30)) +
NULL
plot_ecdf_temps
ggplotly(plot_ecdf_temps)
plot_time_adjustedcdf <- temps %>% group_by(stationName, year) %>% arrange(hours_num) %>%
filter(Wtemp_Inst > 20) %>%
ggplot(aes(x = Wtemp_Inst, y = days_num, group = as.character(year))) +
geom_line() +
geom_ribbon(data ={ . %>% filter(Wtemp_Inst > 25)}, aes(ymin = 0, ymax = days_num), fill = 'red') +
ylim(c(0, 100)) +
geom_hline(aes(yintercept = 50)) +
facet_grid(stationName ~ year) +
labs(color = NULL, fill = NULL) +
scale_x_continuous(breaks = c(20, 22, 24, 26, 28)) +
theme(legend.position = "none") +
labs(y = "Number of days at or below temperature X",
x = "Temperature")
plot_time_adjustedcdf
# ggplotly(plot_time_adjustedcdf)
```
```{r temp_raster}
# Not enough daily variation to make this effective.
#
#
water_temp_raster <- temps %>%
filter(month %in% c(4:10)) %>%
filter(year != 2022) %>%
as_tibble() %>%
filter(stationName == "Peconic River Rte 105") %>%
separate(dateTime, into = c("Date", "measTime"), sep = " ") %>%
mutate(measTime = as.POSIXct(.$measTime, format = "%H:%M:%S", tz = 'GMT'),
time = as.character(.$measTime, format = "%H:%M:%S", tz = 'GMT'),
date = as.Date(Date)) %>% # timezone of raw data is GMT
append_truBlu(date_col = "date") %>%
ggplot(aes(y = commonDate,
x = measTime,
fill = Wtemp_Inst,
interpolate = T)) + #,
# alpha = 1 - (max(Wtemp_Inst) - Wtemp_Inst)/max(Wtemp_Inst))) +
geom_raster() +
scale_fill_viridis_c(option = "inferno") +
# scale_fill_viridis_c(option = "viridis") +
# scale_fill_viridis_c(option = "plasma") +
scale_x_datetime(breaks = NULL) +
scale_y_date(date_labels = "%B", date_breaks = "1 month") +
theme_few() +
theme(legend.position = "left",
panel.spacing.y = unit(5, "points"),
panel.spacing.x = unit(15, "points")) +
guides(alpha = "none") +
labs(y = NULL, x = NULL, fill = "Water temperature C")
# perhaps more intuitive longitudinal look with date across horizontal.
water_temp_raster +
facet_grid(year ~ stationName, switch = "y") +
coord_flip()
water_temp_raster +
facet_grid(year ~ stationName, switch = "y") +
coord_flip()
# # vertical days - time across horizontal
# water_temp_raster +
# facet_grid(. ~ year)
## Tidal height ----
tidal_ht_raster <- temps %>% filter(month %in% c(5:10)) %>%
as_tibble() %>% filter(stationName != "Peconic River Rte 105") %>%
separate(dateTime, into = c("Date", "Time"), sep = " ") %>%
mutate(Time = as.POSIXct(.$Time, format = "%H:%M:%S", tz = 'GMT')) %>% # timezone of raw data is GMT
# ggplot(aes(y = commonDate, x = Time, fill = Wtemp_Inst)) +
ggplot(aes(y = commonDate, x = Time, fill = tidalHt)) +
theme_bw() +
geom_raster(interpolate = T) +
scale_fill_viridis_c(option = "cividis", aesthetics = c("color", "fill")) +
# scale_fill_gradient(low = "blue", high = "red") +
# scale_x_datetime(breaks = date_breaks('1 hour'), labels = date_format("%H")) +
scale_x_datetime(labels = NULL) +
scale_y_date(breaks = date_breaks('1 month'), labels = date_format("%b")) +
facet_grid(. ~ year) +
theme(legend.position = "bottom") +
labs(y = NULL, x = NULL)
tidal_ht_raster
temps %>% filter(month %in% c(5:10)) %>%
as_tibble() %>% filter(stationName != "Peconic River Rte 105") %>%
separate(dateTime, into = c("Date", "Time"), sep = " ") %>%
mutate(Time = as.POSIXct(.$Time, format = "%H:%M:%S", tz = 'GMT')) %>% # timezone of raw data is GMT
HL(level = .$tidalHt, time = .$dateTime) %>% HL.plot()
library(patchwork)
water_temp_raster + tidal_ht_raster
## Moon phasses----
temps %>%
filter(month %in% c(4:10)) %>%
filter(year != 2022) %>%
as_tibble() %>%
filter(stationName == "Peconic River Rte 105") %>%
separate(dateTime, into = c("Date", "measTime"), sep = " ") %>%
mutate(measTime = as.POSIXct(.$measTime, format = "%H:%M:%S", tz = 'GMT'),
time = as.character(.$measTime, format = "%H:%M:%S", tz = 'GMT'),
date = as.Date(Date)) %>% # timezone of raw data is GMT
filter(moon.font == "O") %>%
append_truBlu(date_col = "date") %>%
ggplot(aes(x = commonDate, y = time)) +
geom_text(aes(label = moon.font)) + #TODO: Moon font not showing..
# library(rayshader)
#
# a <- water_temp_raster +
# facet_grid(year ~ stationName, switch = "y") +
# coord_flip()
#
# plot_gg(ggobj = a, width = 5, height = 5, multicore = TRUE, scale = 200 )
```
## Differencing
```{r}
temps %>% filter(month %in% c(5:10)) %>%
as_tibble() %>%
filter(stationName != "Peconic River Rte 105") %>%
separate(dateTime, into = c("Date", "Time"), sep = " ") %>%
mutate(Time = as.POSIXct(.$Time, format = "%H:%M:%S", tz = 'GMT'),
diffed = difference(Wtemp_Inst, lag = 1)) %>%
ggplot(aes(y = commonDate, x = Time, fill = diffed)) +
theme_bw() +
geom_raster(interpolate = T) +
scale_fill_viridis_c(option = "cividis", aesthetics = c("color", "fill")) +
# scale_fill_gradient(low = "blue", high = "red") +
# scale_x_datetime(breaks = date_breaks('1 hour'), labels = date_format("%H")) +
scale_x_datetime(labels = NULL) +
scale_y_date(breaks = date_breaks('1 month'), labels = date_format("%b")) +
facet_grid(. ~ year) +
theme(legend.position = "bottom") +
labs(y = NULL, x = NULL)
```
## Tidal characteristics
```{r}
library(Tides)
temps %>% select(dateTime, tidalHt) %>%
filter_index("2020-05-01" ~ "2020-06-30") %>%
ggplot(aes(x = dateTime, y = tidalHt)) +
geom_path()
temps %>% select(dateTime, tidalHt, Wtemp_Inst) %>%
filter_index("2020-05-01" ~ "2020-06-30") %>% autoplot(vars(tidalHt, difference(tidalHt)))
temps %>%
filter(stationName != "Peconic River Rte 105") %>%
filter_index("2020-05-01" ~ "2020-06-30") %>%
as_tibble() %>%
ungroup() %>%
select(time = dateTime, h = tidalHt) %>% # must rename the columns to time and h.
tidyr::drop_na() %>%
extrema(h0 = 0)
tideOrient <- temps %>%
filter(stationName != "Peconic River Rte 105") %>%
filter_index("2020-06-01" ~ "2020-06-15") %>%
as_tibble() %>%
ungroup() %>%
select(time = dateTime, h = tidalHt) %>% # must rename the columns to time and h.
tidyr::drop_na() %>% extrema(h0 = 0)
View(tideOrient$h)
View(tideOrient$HL)
tidal_c_orient <- temps %>%
filter(stationName != "Peconic River Rte 105") %>%
# filter_index("2020-06-01" ~ "2020-06-15") %>%
as_tibble() %>%
ungroup() %>%
select(time = dateTime, h = tidalHt) %>% # must rename the columns to time and h.
tidyr::drop_na() %>%
TidalCharacteristics(h0 = 0, hoffset = .5)
```