-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_resuspension.Rmd
More file actions
146 lines (120 loc) · 5.25 KB
/
5_resuspension.Rmd
File metadata and controls
146 lines (120 loc) · 5.25 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
---
title: "5_resuspension"
output: html_document
date: "2023-04-24"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#libraries
```{r}
library(tidyverse)
library(sf)
library(lubridate)
library(grDevices)
library(mapview)
library(extrafont)
library(ggpubr)
library(ggmap)
library(RgoogleMaps)
library(broom)
library(feather)
library(tidyhydat)
library(sp)
library(data.table)
library(ggalluvial)
library(patchwork)
library(magick)
library(units)
library(Kendall)
library(ggspatial)
library(dtplyr)
```
# Import files / set constants
```{r}
# dates for version control
todayDate = "20230324" # the first data join phase
# Names of files and folders for reflectance data
import.filePath = "C:/Users/whyana/OneDrive/DocumentsLaptop/001_GraduateSchool/Research/Connectivity/Mackenzie/Data/GEE Downloads"
# intermediate working directory
int.wd="C:/Users/whyana/OneDrive/DocumentsLaptop/001_GraduateSchool/Research/Connectivity/Mackenzie/Data/intermediaryDownloads"
refl.import = read_feather('srCorrected_mackLakes_202303138.feather')
refl.import.chan = read_feather('srCorrected_mackChans_20230424.feather')
#Name of file and folder for lake shapefiles & island polygon shapefiles
shapeFiles.filePath = "C:/Users/whyana/OneDrive/DocumentsLaptop/001_GraduateSchool/Research/Connectivity/Mackenzie/Data/shapeFiles"
lakes.shapeFile = "mackenzieGoodLakes.shp"
islands.shapeFile = "vectorIslandArea2.shp"
import.sword = "na_sword_reaches_hb82_v14.shp"
setwd(shapeFiles.filePath)
lakes.sf = st_read(lakes.shapeFile)
islands.sf=st_read(islands.shapeFile)
images.wd = "C:/Users/whyana/OneDrive/DocumentsLaptop/001_GraduateSchool/Research/Connectivity/Mackenzie/images"
# import results
setwd(int.wd)
all.classified.filter = read_feather(paste0("final.class_", todayDate, ".feather"))
# Set crs for plots
crs.plot = "+proj=tcea +lon_0=-134.3847656 +datum=WGS84 +units=m +no_defs"
```
# Analyze for resuspension!
```{r}
# Figure out which island each lake is within and join accordingly
island.lakes = lakes.sf %>% st_transform(crs.plot) %>%
select(OBJECTID, geometry) %>%
st_join(islands.sf %>% st_transform(crs.plot) %>%
select(fid, geometry)) %>%
filter(!is.na(fid)) %>%
as_tibble() %>% select(-geometry)
# If there are more than one observation of each lake in a day, keep only the one with the most pixels.
lake.prep = refl.import %>% select(OBJECTID, Nir_mean, Landsat, dateTime, date, WRS_PATH, WRS_ROW, Red_count) %>%
rename(Nir_lake = Nir_mean) %>%
group_by(OBJECTID, date) %>%
mutate(my_ranks = order(Red_count, decreasing=TRUE)) %>%
filter(my_ranks==1) %>% ungroup() %>% select(-my_ranks) %>%
left_join(island.lakes, by="OBJECTID", multiple="all") %>% filter(!is.na(fid))
chan.prep=refl.import.chan %>%
select(OBJECTID, Nir_mean, Landsat,dateTime, date, WRS_PATH, WRS_ROW) %>%
as_tibble() %>%
rename(Nir_chan = Nir_mean, fid=OBJECTID) %>% mutate(fid=fid*-1)
# Join lake and channel info
lake.chan.combo = lake.prep %>%
left_join(chan.prep, by=c("fid", "dateTime", "date", "Landsat", "WRS_PATH", "WRS_ROW")) %>%
filter(!is.na(Nir_chan)) %>%
mutate(Nir_ratio = Nir_lake/Nir_chan)
# Join lake and channel info with the connectivity classification associated with the observation
lake.chan.class = lake.chan.combo %>%
left_join(all.classified.filter %>%
select(.pred_class, OBJECTID, date), by=c("OBJECTID", "date")) %>%
mutate(.pred_class = as.numeric(as.character(.pred_class)))
length(unique(lake.chan.class$OBJECTID))
# Get list of observations with high NIR ratios where the lakes were classified as high connectivity (class 2). Keep only one observation for each lake. Export these observations for import into a GEE script for visual inspection
lake.chan.export = lake.chan.class %>% filter(Nir_ratio>1.3 & .pred_class==2) %>%
group_by(OBJECTID) %>% mutate(group_id=row_number()) %>% filter(group_id==1) %>% ungroup() %>%
select(OBJECTID, date, dateTime, .pred_class, Nir_ratio, fid) %>%
mutate(fxd_ndx=row_number()) %>%
left_join(lakes.sf, by="OBJECTID") %>%
st_as_sf()
setwd(int.wd)
st_write(lake.chan.export, "Nir1_3_class2.shp")
# Import the data into GEE and complete the analysis. Link to GEE script here: https://code.earthengine.google.com/b2a63a5cd472a802a08c1a5d794fbf85
# Import the data that you just inspected in GEE!
setwd(import.filePath)
resus.class = st_read("resuspension_20230425.shp") %>%
mutate(dateTime = as_datetime(date*0.001),
date = as_date(dateTime))
# Print results for use in the manuscript
## Number of observations classified as cloud, ice, not resuspension, or possible resuspension
resus.class %>%
group_by(type) %>%
count()
# Total number of observations that were flagged as needing to be looked at for resuspension
all.lake.summary = lake.chan.class %>% filter(Nir_ratio>1.3 & .pred_class==2) %>% group_by(OBJECTID) %>% count() %>% ungroup()
sum(all.lake.summary$n)
# Assuming that every flagged observation of a lake should be classified the same way with regaurds to resuspension, calculate the number of total resuspension observations you'd expect
resus = resus.class %>%
left_join(all.lake.summary, by="OBJECTID") %>% as_tibble() %>%
select(-geometry) %>%
filter(type=="possibleResuspension")
sum(resus$n)
```