-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_volume_calcs.Rmd
More file actions
220 lines (169 loc) · 6.76 KB
/
03_volume_calcs.Rmd
File metadata and controls
220 lines (169 loc) · 6.76 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
---
title: "03_volume_calcs"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE)
```
# Pull in the munged lake height and lake area data
```{r}
areas <- read_csv('data/out/areas_munged_CC30.csv') %>%
select(area = Reconstructed, sat, name, date = timestamp) %>%
mutate(date = as_date(date),
areaID = row_number()) %>%
group_by(name, date) %>%
summarise(area = mean(area)) %>%
ungroup()
heights <- read_csv('data/out/heights_munged.csv') %>%
select(height, name = name.std, date)
lake.summaries <- read_csv('data/in/lake_properties.csv') %>%
distinct(name,.keep_all = T)
```
## Find matchups (+/- 1 day pairs)
```{r}
sameday <- areas %>% inner_join(heights) %>%
group_by(name, date) %>%
summarise(area = mean(area),
height = mean(height)) %>%
mutate(obID = paste0(date,"_",name)) %>%
ungroup()
plusone <- areas %>%
mutate(area_date = date,
date = date + 1) %>%
inner_join(heights) %>%
group_by(name, date) %>%
summarise(area = mean(area),
height = mean(height)) %>%
mutate(obID = paste0(date,"_",name)) %>%
ungroup()
minusone <- areas %>%
mutate(area_date = date,
date = date - 1) %>%
inner_join(heights) %>%
group_by(name, date) %>%
summarise(area = mean(area),
height = mean(height)) %>%
mutate(obID = paste0(date,"_",name)) %>%
ungroup()
## We prefer same day,
plusone <- plusone %>% filter(!obID %in% sameday$obID)
minusone <- minusone %>% filter(!obID %in% sameday$obID)
## Check for obs both in both plus and minus and average them
overlap <- plusone %>% filter(obID %in% minusone$obID) %>%
group_by(name, date, obID) %>%
summarise(height = mean(height),
area = mean(area))
matchups <- sameday %>% mutate(matchup = 'same') %>%
bind_rows(plusone %>% filter(!obID %in% overlap$obID) %>% mutate(matchup = 'plusone')) %>%
bind_rows(minusone %>% filter(!obID %in% overlap$obID) %>% mutate(matchup = 'minusone')) %>%
bind_rows(overlap %>% mutate(matchup = 'average')) %>%
mutate(area_m = area*1000000)
## This leaves us with only 73 lakes where there is at least 1 matchup
dplyr::count(matchups,name) %>% filter(n>2)
## and 68 lakes with at least 3
```
## Calculate volume changes
```{r}
## Simple volumetric change based on trapezoidal volume
volChange <- function(lake){
date.first <- lake[lake$date == min(lake$date),]
lake %>% arrange(date) %>%
mutate(area_diff = area_m + date.first$area_m[1],
height_diff = height - date.first$height[1],
vol_change = height_diff*(area_diff/2))
}
volumes <- matchups %>% group_by(name) %>%
nest() %>%
mutate(volCalcs = map(data, volChange)) %>%
select(-data) %>%
unnest(cols = c(volCalcs))
## Rating curve calculations to expand volume change to all the height measurements
filter <- dplyr::count(volumes, name) %>% filter(n<2)
fits <- volumes %>%
filter(!name %in% filter$name) %>%
group_by(name) %>%
nest() %>%
mutate(fit = map(data, function(df) lm(vol_change ~ height, data = df))) %>%
select(-data)
volumes_fitted <- heights %>%
nest(-name) %>%
inner_join(fits) %>%
mutate(vol_fit = map2(fit, data, ~predict(.x, newdata = .y) %>% unname())) %>%
select(-fit) %>%
unnest(cols = c(data, vol_fit))
write_csv(volumes_fitted, 'data/out/volumes_fitted_CC30.csv')
```
## Join the the areas and look at some timeseries
```{r}
areas <- read_csv('data/out/areas_munged_CC30.csv') %>%
select(area = Reconstructed, sat, name, date = timestamp) %>%
mutate(date = as_date(date))
volsLong <- volumes_fitted %>%
full_join(areas) %>%
#filter(area > 5) %>%
select(name, area, date, height, vol_fit) %>%
pivot_longer(c(area, height, vol_fit), names_to = 'Metric') %>%
mutate(Metric = factor(Metric, levels = c('height', 'area', 'vol_fit')))
lake <- 'Salters Lake'
volsLong %>% filter(name == lake, date > '2017-04-01') %>%
ggplot(aes(x = date, y = value)) + geom_path() + geom_point() +
facet_wrap(~Metric, scales = 'free_y', ncol = 1) +
ggtitle(lake)
## Make them individually for correct lines
p1 <- ggplot(heights %>% filter(name == lake), aes(x = date, y = height)) + geom_line(color = 'blue') +
labs(y = 'Level (m)', subtitle = 'a) Lake Level') +
theme_classic() +
theme(axis.title.x = element_blank(),
axis.text.y = element_text(angle = 45, hjust = .5),
plot.subtitle = element_text(hjust = 0.5))
p2 <- ggplot(areas %>% filter(name == lake, date > '2017-04-18'), aes(x = date, y = area)) + geom_line(color = 'red') +
labs(y = expression(Surface~Area~(km^2)), subtitle = 'b) Lake Surface Area') +
theme_classic() +
theme(axis.title.x = element_blank(),
axis.text.y = element_text(angle = 45, hjust = .5),
plot.subtitle = element_text(hjust = 0.5))
p3 <- ggplot(volumes_fitted %>% filter(name == lake), aes(x = date, y = vol_fit)) + geom_line(color = 'black') +
labs(y = expression(Delta~Volume~(m^2)), subtitle = 'c) Change in Lake Volume') +
theme_classic() +
theme(axis.title.x = element_blank(),
axis.text.y = element_text(angle = 45, hjust = .5),
plot.subtitle = element_text(hjust = 0.5))
g <- gridExtra::grid.arrange(p1, p2,p3, ncol = 1)
ggsave('figures/Salters3panel.png', plot = g, width = 5, height = 4, units = 'in', dpi = 600)
```
## Rating curve
```{r}
ggplot(volumes %>% filter(name == "Bay Tree Lake"), aes(x = height_diff, y = vol_change)) +
geom_point() +
geom_smooth(method = 'lm') +
ggpmisc::stat_poly_eq(formula = y~x, rr.digits = 3,coef.digits = 5, label.x = 'right', label.y = 'bottom',
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
parse = TRUE, color = 'red') +
labs(y = expression(Delta~Volume~(m^2)), x = expression(Delta~Height~(m))) +
theme_bw() +
theme(axis.text.y = element_text(angle = 45, hjust = .5))
ggsave('figures/RatingCurve.png', width = 3.5, height = 3, units = 'in', dpi = 600)
```
## Make the summary table
```{r}
satCounts <- areas %>%
filter(timestamp > '2015-01-01',timestamp < '2020-01-01') %>%
group_by(name) %>%
summarise(countAreas = n(),
mean = mean(Reconstructed))
heightsCounts <- heights %>% group_by(name) %>%
summarise(countLevels = n(),
dateFirst = min(date),
dateLast = max(date))
tabOut <- lake.summaries %>% select(name, region) %>%
left_join(heightsCounts) %>%
left_join(satCounts)
## West Loon and East Loon are the same
tabOut$countLevels[tabOut$name == 'West Loon Lake'] <- tabOut$countLevels[tabOut$name == 'East Loon Lake']
tabOut$dateFirst[tabOut$name == 'West Loon Lake'] <- tabOut$dateFirst[tabOut$name == 'East Loon Lake']
tabOut$dateLast[tabOut$name == 'West Loon Lake'] <- tabOut$dateLast[tabOut$name == 'East Loon Lake']
write_csv(tabOut, 'data/out/SummaryTable.csv')
```