-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_manuscript_analysis_porewaterDOC.Rmd
282 lines (230 loc) · 14.1 KB
/
1_manuscript_analysis_porewaterDOC.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
---
title: "Porewater DOC Manuscript Analysis"
author: "AMP"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
library(lubridate)
library(tidyverse)
#double check your wd
getwd()
#clean out your environment
rm(list = ls())
```
### Set the study dates
```{r event and study dates}
endstudydate = lubridate::as_date("2024-01-31")
startstudydate = lubridate::as_date("2022-05-01")
endstudydate_yr1 = lubridate::as_date("2023-05-31")
startstudydate_yr1 = lubridate::as_date("2022-05-01")
year1_start = lubridate::as_date("2022-01-01")
year1_stop = lubridate::as_date("2022-12-31")
year2_start = lubridate::as_date("2023-01-01")
year2_stop = lubridate::as_date("2023-12-31")
year3_start = lubridate::as_date("2024-01-01")
year3_stop = lubridate::as_date("2024-12-31")
WaterDeliveryStart2022 = as.POSIXct("2022-06-22 05:30:00", tz = "EST")
WaterDeliveryStop2022 = as.POSIXct("2022-06-22 14:30:00", tz = "EST")
WaterDeliveryStart1 = as.POSIXct("2023-06-06 05:30:00", tz = "EST")
WaterDeliveryStop1 = as.POSIXct("2023-06-06 14:30:00", tz = "EST")
WaterDeliveryStart2 = as.POSIXct("2023-06-07 05:30:00", tz = "EST")
WaterDeliveryStop2 = as.POSIXct("2023-06-07 14:30:00", tz = "EST")
```
## DOC
1) Import DOC grid data for porewater from 2022 and 2023 events. Then, bind them together and take the average and standard deviation of the values for each event. Do this by the evacuation date, as this will capture multiple tries within the same dateframe. For this particular analysis, it's ok to take the average of the date time of sampling, because we're just going to be using this data to plot through time and do some simple summary statistics.
```{r read in doc}
#Porewater DOC data, from L1 google drive:
doc_l1_2022 <- read_csv("~/GitHub/TEMPEST-1-porewater/processed data/for_google_drive/TMP_PW_GRIDSONLY_NPOC_TDN_L1_2022.csv")
doc_l1_2023 <- read_csv("~/GitHub/TEMPEST-1-porewater/processed data/for_google_drive/TMP_PW_GRIDSONLY_NPOC_TDN_L1_2023.csv")
```
```{r doc avg stds}
doc_l1_pw_grids <- doc_l1_2022 %>%
full_join(doc_l1_2023) %>%
select(plot, evacuation_date, collection_datetime, doc_mg_l, tdn_mg_l) %>%
drop_na() #dont need to specify column here because the only ones with NAs are DOC or TDN and they are both NA at the same time.
#can't get both mean and sd to work across everything for some reason, so calculate separate then recombine.
# Some dates not enough water was collected to have multiple samples to get averages and sds for that evacuation period. Most of the time, those can be combined with sampling dates just before or just after as we go out over several multi-day periods. This, however isn't consistent across the FW, EW, or C plots, so it will be a bit tricky to do the gap filling. To make the gap filling more accurate, we'll take the average of those dates, so for example if the dates we're merging is 2022-11-25 and 2022-11-28 we'll have the date be the 26th (but we'll let R do that math).
#First, isolate the trouble dates/times:
#Control:
mean_doc_l1_c_to_gapfill <- doc_l1_pw_grids %>%
filter(plot == "Control") %>%
filter(evacuation_date %in% c("2022-09-09", "2022-09-12", "2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-12-05", "2023-12-08")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "A",
str_detect(evacuation_date,"2022-11-28") | str_detect(evacuation_date,"2022-11-25") ~ "B",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "C",
str_detect(evacuation_date,"2023-07-05") | str_detect(evacuation_date,"2023-07-07") ~ "D",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "E",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
dplyr::summarise(across(everything(), mean), .groups = "keep") %>%
mutate(collection_datetime = as.POSIXct(collection_datetime, origin = "1970-01-01"),
evacuation_date = as_date(evacuation_date)) %>%
ungroup()
sd_doc_l1_c_to_gapfill <- doc_l1_pw_grids %>%
select(-collection_datetime) %>%
filter(plot == "Control") %>%
filter(evacuation_date %in% c("2022-09-09", "2022-09-12", "2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-12-05", "2023-12-08")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "A",
str_detect(evacuation_date,"2022-11-28") | str_detect(evacuation_date,"2022-11-25") ~ "B",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "C",
str_detect(evacuation_date,"2023-07-05") | str_detect(evacuation_date,"2023-07-07") ~ "D",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "E",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
select(-evacuation_date) %>%
dplyr::summarise(across(everything(), sd), .groups = "keep") %>%
rename(doc_mg_l_sd = doc_mg_l,
tdn_mg_l_sd = tdn_mg_l) %>%
ungroup()
c_gapfill <- mean_doc_l1_c_to_gapfill %>%
full_join(sd_doc_l1_c_to_gapfill, by = c("plot", "gapfill_group")) %>%
select(-gapfill_group)
#Estuarine Water
mean_doc_l1_sw_to_gapfill <- doc_l1_pw_grids %>%
filter(plot == "Estuarine-water") %>%
filter(evacuation_date %in% c("2022-07-18", "2022-07-14", "2022-08-08", "2022-08-05","2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-12-05", "2023-12-08", "2023-01-30", "2023-02-02")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-07-18") | str_detect(evacuation_date,"2022-07-14") ~ "A",
str_detect(evacuation_date,"2022-08-08") | str_detect(evacuation_date,"2022-08-05") ~ "B",
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "C",
str_detect(evacuation_date,"2022-11-25") | str_detect(evacuation_date,"2022-11-28") ~ "D",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "E",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "F",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
dplyr::summarise(across(everything(), mean), .groups = "keep") %>%
ungroup()
sd_doc_l1_sw_to_gapfill <- doc_l1_pw_grids %>%
select(-collection_datetime) %>%
filter(plot == "Estuarine-water") %>%
filter(evacuation_date %in% c("2022-07-18", "2022-07-14", "2022-08-08", "2022-08-05","2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-12-05", "2023-12-08", "2023-01-30", "2023-02-02")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-07-18") | str_detect(evacuation_date,"2022-07-14") ~ "A",
str_detect(evacuation_date,"2022-08-08") | str_detect(evacuation_date,"2022-08-05") ~ "B",
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "C",
str_detect(evacuation_date,"2022-11-25") | str_detect(evacuation_date,"2022-11-28") ~ "D",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "E",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "F",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
select(-evacuation_date) %>%
dplyr::summarise(across(everything(), sd), .groups = "keep") %>%
rename(doc_mg_l_sd = doc_mg_l,
tdn_mg_l_sd = tdn_mg_l) %>%
ungroup()
sw_gapfill <- mean_doc_l1_sw_to_gapfill %>%
full_join(sd_doc_l1_sw_to_gapfill, by = c("plot", "gapfill_group")) %>%
select(-gapfill_group)
#Freshwater:
mean_doc_l1_fw_to_gapfill <- doc_l1_pw_grids %>%
filter(plot == "Freshwater") %>%
filter(evacuation_date %in% c("2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-09-29", "2023-10-02","2023-12-05", "2023-12-08")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "A",
str_detect(evacuation_date,"2022-11-28") | str_detect(evacuation_date,"2022-11-25") ~ "B",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "C",
str_detect(evacuation_date,"2023-07-05") | str_detect(evacuation_date,"2023-07-07") ~ "D",
str_detect(evacuation_date,"2023-09-29") | str_detect(evacuation_date,"2023-10-02") ~ "E",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "F",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
dplyr::summarise(across(everything(), mean), .groups = "keep") %>%
ungroup()
sd_doc_l1_fw_to_gapfill <- doc_l1_pw_grids %>%
select(-collection_datetime) %>%
filter(plot == "Freshwater") %>%
filter(evacuation_date %in% c("2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-09-29", "2023-10-02","2023-12-05", "2023-12-08")) %>%
mutate(gapfill_group = case_when(
str_detect(evacuation_date,"2022-09-09") | str_detect(evacuation_date,"2022-09-12") ~ "A",
str_detect(evacuation_date,"2022-11-28") | str_detect(evacuation_date,"2022-11-25") ~ "B",
str_detect(evacuation_date,"2023-01-30") | str_detect(evacuation_date,"2023-02-02") ~ "C",
str_detect(evacuation_date,"2023-07-05") | str_detect(evacuation_date,"2023-07-07") ~ "D",
str_detect(evacuation_date,"2023-09-29") | str_detect(evacuation_date,"2023-10-02") ~ "E",
str_detect(evacuation_date,"2023-12-05") | str_detect(evacuation_date,"2023-12-08") ~ "F",
TRUE ~ "no")) %>%
group_by(plot, gapfill_group) %>%
select(-evacuation_date) %>%
dplyr::summarise(across(everything(), sd), .groups = "keep") %>%
# mutate(evacuation_date = as_date(evacuation_date))%>%
rename(doc_mg_l_sd = doc_mg_l,
tdn_mg_l_sd = tdn_mg_l) %>%
ungroup()
fw_gapfill <- mean_doc_l1_fw_to_gapfill %>%
full_join(sd_doc_l1_fw_to_gapfill, by = c("plot", "gapfill_group")) %>%
select(-gapfill_group)
#All the rest:
means_doc_l1_pw_grids <- doc_l1_pw_grids %>%
filter(plot != "Control" |
(plot == "Control" & !evacuation_date %in% as.Date(c("2022-09-09", "2022-09-12", "2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-12-05", "2023-12-08")))) %>%
filter(plot != "Estuarine-water" |
(plot == "Estuarine-water" & !evacuation_date %in% as.Date(c("2022-07-18", "2022-07-14", "2022-08-08", "2022-08-05","2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-12-05", "2023-12-08", "2023-01-30", "2023-02-02")))) %>%
filter(plot != "Freshwater" |
(plot == "Freshwater" & !evacuation_date %in% as.Date(c("2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-09-29", "2023-10-02","2023-12-05", "2023-12-08")))) %>%
mutate(evacuation_date = as.numeric(evacuation_date),
collection_datetime= as.numeric(collection_datetime)) %>%
group_by(plot, evacuation_date) %>%
dplyr::summarise(across(everything(), mean), .groups = "keep") %>%
mutate(collection_datetime = as.POSIXct(collection_datetime, origin = "1970-01-01"),
evacuation_date = as_date(evacuation_date))
sds_doc_l1_pw_grids <- doc_l1_pw_grids %>%
select(-collection_datetime) %>%
filter(plot != "Control" |
(plot == "Control" & !evacuation_date %in% as.Date(c("2022-09-09", "2022-09-12", "2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-12-05", "2023-12-08")))) %>%
filter(plot != "Estuarine-water" |
(plot == "Estuarine-water" & !evacuation_date %in% as.Date(c("2022-07-18", "2022-07-14", "2022-08-08", "2022-08-05","2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-12-05", "2023-12-08", "2023-01-30", "2023-02-02")))) %>%
filter(plot != "Freshwater" |
(plot == "Freshwater" & !evacuation_date %in% as.Date(c("2022-09-09", "2022-09-12","2022-11-28", "2022-11-25", "2023-01-30", "2023-02-02", "2023-07-05", "2023-07-07", "2023-09-29", "2023-10-02","2023-12-05", "2023-12-08")))) %>%
mutate(evacuation_date = as.numeric(evacuation_date)) %>%
group_by(plot, evacuation_date) %>%
dplyr::summarise(across(everything(), sd), .groups = "keep") %>%
mutate(evacuation_date = as_date(evacuation_date)) %>%
rename(doc_mg_l_sd = doc_mg_l,
tdn_mg_l_sd = tdn_mg_l)
merging_dfs <- list(means_doc_l1_pw_grids, sds_doc_l1_pw_grids, c_gapfill, sw_gapfill, fw_gapfill)
pw_doc <- purrr::reduce(merging_dfs, full_join)
# write out for plotting:
saveRDS(pw_doc, "~/GitHub/TEMPEST-1-porewater/data/averaged_doc_porewater.rds")
```
### run stats for DOC:
[insert some version of BACI stats here]
## OM Chemistry
First source the functions from TSLA:
```{r tsla functions}
# set event window to use for all data frame function calls
flood_event_datetime <- c(as_datetime("2022-06-22 5:30:00", tz = "EST"), as_datetime("2022-06-22 14:30:00", tz = "EST"))
EVENT_START <- as_datetime("2022-06-22 05:30:00", tz = "EST")
EVENT_STOP <- as_datetime("2022-06-22 14:30:00", tz = "EST")
STUDY_START <- as_datetime("2022-05-01 00:00:00", tz = "EST")
STUDY_END <- as_datetime("2023-05-31 00:00:00", tz = "EST")
source("~/GitHub/tempest-system-level-analysis/scripts/tmp_test_functions.R")
# check them out:
calc_dist_metrics
```
```{r load fticrms}
# load npoc/tdn data
pw_ft <- read_rds("~/GitHub/TEMPEST-1-porewater/processed data/TMP_PW_FTICRMS_L1_May2022-May2023.rds") %>%
mutate(datetime_est = with_tz(collection_datetime, tzone = "EST")) %>%
group_by(Event, plot, grid, datetime_est, sample_name, Class_detailed) %>%
dplyr::summarise(abund = sum(presence)) %>%
ungroup() %>%
# create a new column for total counts per core assignment
# and then calculate relative abundance
group_by(Event, plot, grid, datetime_est, sample_name) %>%
dplyr::mutate(total = sum(abund),
relabund = round((abund/total)*100,2)) %>%
ungroup() %>%
dplyr::select(datetime_est, plot, Class_detailed, relabund) %>%
group_by(datetime_est, plot, Class_detailed) %>%
zscore_standard(vars = "relabund") %>% ungroup()
pw_ft %>% summary()
```
```{r tsla metrics}
sum_fticrms <- calc_dist_metrics(pw_ft, flood_event_datetime,
c("relabund"))
```