-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_cleanup.R
392 lines (290 loc) · 10.6 KB
/
02_cleanup.R
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
library(dplyr)
library(readr)
library(tidyr)
library(sp)
full_data <- read_csv("./full_data.csv")
#General code, but not relevant here due to multiple utm zones
#lonlatdf <- data.frame(full_data$longitude,full_data$latitude)
#lonlatsp <- SpatialPoints(lonlatdf, proj4string=CRS("+proj=longlat +datum=WGS84"))
#scenesp_utm <- spTransform(scenesp, ("+proj=utm +zone=11 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
#sceneUTM <- as.data.frame(scenesp_utm)
#San Francisco is in UTM zone 10S
#pull out everything north of santa maria and call it zone 10 for reprojection
utm_zone_10S <- filter(full_data, latitude > 34.963623)
lonlatdf_zone10 <- data.frame(utm_zone_10S$longitude,utm_zone_10S$latitude)
lonlat_10_sp <- SpatialPoints(lonlatdf_zone10, proj4string=CRS("+proj=longlat +datum=WGS84"))
utm_zone_10S_utm <- spTransform(lonlat_10_sp, ("+proj=utm +zone=10 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
utm_zone_10S_utm <- as.data.frame(utm_zone_10S_utm)
zone_10 <- cbind(utm_zone_10S, utm_zone_10S_utm)
#cleanup
rm(lonlat_10_sp)
rm(lonlatdf_zone10)
rm(utm_zone_10S_utm)
write_csv(zone_10, "zone_10.csv")
rm(utm_zone_10S)
#everything else is zone 11
utm_zone_11S <- filter(full_data, latitude < 34.963623)
lonlatdf_zone11 <- data.frame(utm_zone_11S$longitude,utm_zone_11S$latitude)
lonlat_11_sp <- SpatialPoints(lonlatdf_zone11, proj4string=CRS("+proj=longlat +datum=WGS84"))
utm_zone_11S_utm <- spTransform(lonlat_11_sp, ("+proj=utm +zone=11 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
utm_zone_11S_utm <- as.data.frame(utm_zone_11S_utm)
zone_11 <- cbind(utm_zone_11S, utm_zone_11S_utm)
#cleanup
rm(lonlat_11_sp)
rm(lonlatdf_zone11)
rm(utm_zone_11S_utm)
write_csv(zone_11, "zone_11.csv")
write_csv(utm_zone_11S, "zone_11_small.csv")
rm(utm_zone_11s)
#now filter out cities and their bounding boxes. pixels are 30m, need 1667 to get ~25 km.
#take each city's coordinates
zone_11 <- read_csv("./zone_11.csv")
SB_x <- 252030
SB_y <- 3812120
box_max_x <- SB_x + 1667
box_min_x <- SB_x - 1667
box_max_y <- SB_y
box_min_y <- SB_y
#find them in the table and filter out all rows where x and y = the city +/- 1667 pixels
SB <- zone_11 %>%
filter(utm_zone_11S.longitude > box_min_x) %>%
filter(utm_zone_11S.longitude < box_max_x) %>%
filter(biomass > -1) #remove clouds
write_csv(SB, "SB.csv")
#tangent for ggmap to see what this looks like becfause the histogram isn't promising
#ggmap makes it look like everything is a-ok - prefiltered!
library(ggmap)
base_map <- get_map("santa barbara", zoom =10, source = "stamen")
ggmap(base_map) +
geom_point(data = SB, aes(x = longitude, y = latitude, color = biomass), alpha = 0.1) +
geom_point(data = SB2, aes(x = longitude, y = latitude, color = biomass), alpha = 0.1)+
facet_wrap(~date_utc)
#now make boxes for all cities
#LA
#convert
lall <- data.frame(longitude = -118.253734,latitude = 33.717393)
lallsp <- SpatialPoints(lall, proj4string=CRS("+proj=longlat +datum=WGS84"))
la_utm <- spTransform(lallsp, ("+proj=utm +zone=11 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
la_utm <- as.data.frame(la_utm)
LA_x <- 383834
LA_y <- 3731527
box_max_x <- LA_x
box_min_x <- LA_x
box_max_y <- LA_y + 1667
box_min_y <- LA_y - 1667
#find them in the table and filter out all rows where x and y = the city +/- 1667 pixels
LA <- zone_11 %>%
filter(utm_zone_11S.latitude > box_min_y) %>%
filter(utm_zone_11S.latitude < box_max_y) %>%
filter(biomass > -1) #remove clouds
write_csv(LA, "LA.csv")
#San Diego
SD_x <- 475877
SD_y <- 3633875
box_max_x <- SD_x
box_min_x <- SD_x
box_max_y <- SD_y + 1667
box_min_y <- SD_y - 1667
#find them in the table and filter out all rows where x and y = the city +/- 1667 pixels
SD <- zone_11 %>%
filter(utm_zone_11S.latitude > box_min_y) %>%
filter(utm_zone_11S.latitude < box_max_y) %>%
filter(biomass >-1) #remove clouds
write_csv(SD, "SD.csv")
#San Francisco
zone_10 <- read_csv("./zone_10.csv")
SF_x <- 550083
SF_y <- 4180889
box_max_x <- SF_x + 1667
box_min_x <- SF_x - 1667
box_max_y <- SF_y + 1667
box_min_y <- SF_y - 1667
#find them in the table and filter out all rows where x and y = the city +/- 1667 pixels
SF <- zone_10 %>%
filter(utm_zone_10S.latitude > box_min_y) %>%
filter(biomass >-1) #remove clouds
write_csv(SF, "SF.csv")
#then replace biomass with presence/absence
SB <- SB %>%
mutate(kelp = ifelse(SB$biomass >0, 1, 0))
write_csv(SB, "SB.csv")
SD <- SD %>%
mutate(kelp = ifelse(SD$biomass >0, 1, 0))
write_csv(SD, "SD.csv")
LA <- LA %>%
mutate(kelp = ifelse(LA$biomass >0, 1, 0))
write_csv(LA, "LA.csv")
SF <- SF %>%
mutate(kelp = ifelse(SF$biomass >0, 1, 0))
write_csv(SF, "SF.csv")
#add site codes
SB <- SB %>%
mutate(site = rep("SB", times = nrow(SB)))
write_csv(SB, "SB.csv")
SD <- SD %>%
mutate(site = rep("SD", times = nrow(SD)))
write_csv(SD, "SD.csv")
LA <- LA %>%
mutate(site = rep("LA", times = nrow(LA)))
write_csv(LA, "LA.csv")
SF <- SF %>%
mutate(site = rep("SF", times = nrow(SF)))
write_csv(SF, "SF.csv")
#and create one full dataframe
#first rename columns
names(SB)[5:6] =
c("UTM_x", "UTM_y")
names(SD)[5:6] =
c("UTM_x", "UTM_y")
names(LA)[5:6] =
c("UTM_x", "UTM_y")
names(SF)[5:6] =
c("UTM_x", "UTM_y")
kelp <- rbind(SB, SD, LA)
write_csv(kelp, "kelp.csv")
#now expand dates to make quarterly averages more straightforward
library(lubridate)
kelp <- kelp %>%
mutate(date = ymd(kelp$date_utc))
kelp <- kelp %>%
separate(date, c("year", "month", "day"), sep = "-")
kelp$year <- as.numeric(kelp$year)
kelp$month <- as.numeric(kelp$month)
kelp$day <- as.numeric(kelp$day)
kelp <- kelp %>%
mutate(qtr = ifelse(kelp$month == 01| kelp$month == 02| kelp$month == 3, 1,
ifelse(kelp$month == 04| kelp$month == 05| kelp$month == 6, 2,
ifelse(kelp$month == 07| kelp$month == 08| kelp$month == 9, 3,
4))))
kelp <- kelp %>%
select(-qtr)
#great, this looks good. now do the same for control sites.
#make control site origins as OG sites - 50 km
#convert
control_latitude <- c(34.047543, 32.240051, 33.307471)
control_longitude <- c(-118.950585,-116.926515, -117.499485)
controls <- data.frame(longitude = control_longitude ,latitude =control_latitude )
controls <- SpatialPoints(controls, proj4string=CRS("+proj=longlat +datum=WGS84"))
controls_utm <- spTransform(controls, ("+proj=utm +zone=11 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
controls_utm <- as.data.frame(controls_utm)
SB_control_x <- 319955
SB_control_y <- 3769144
LA_control_x <- 453502
LA_control_y <- 3685485
SD_control_x <- 506922
SD_control_y <- 3567046
SF_control_x <- 550083 - 3334
SF_control_y <- 4180889 - 3334
#now do the filters
#SB_control
box_max_x <- SB_control_x + 1667
box_min_x <- SB_control_x - 1667
box_max_y <- SB_control_y
box_min_y <- SB_control_y
SB_control <- zone_11 %>%
filter(utm_zone_11S.longitude > box_min_x) %>%
filter(utm_zone_11S.longitude < box_max_x) %>%
filter(biomass > -1) #remove clouds
write_csv(SB_control, "SB_control.csv")
#LA_control
box_max_x <- LA_control_x
box_min_x <- LA_control_x
box_max_y <- LA_control_y + 1667
box_min_y <- LA_control_y - 1667
LA_control <- zone_11 %>%
filter(utm_zone_11S.latitude > box_min_y) %>%
filter(utm_zone_11S.latitude < box_max_y) %>%
filter(biomass > -1) #remove clouds
write_csv(LA_control, "LA_control.csv")
#SD_control
box_max_x <- SD_control_x
box_min_x <- SD_control_x
box_max_y <- SD_control_y + 1667
box_min_y <- SD_control_y - 1667
SD_control <- zone_11 %>%
filter(utm_zone_11S.latitude > box_min_y) %>%
filter(utm_zone_11S.latitude < box_max_y) %>%
filter(biomass > -1) #remove clouds
write_csv(SD_control, "SD_control.csv")
#SF_control
box_max_x <- SF_control_x + 1667
box_min_x <- SF_control_x - 1667
box_max_y <- SF_control_y + 1667
box_min_y <- SF_control_y - 1667
SF_control <- zone_10 %>%
filter(utm_zone_10S.latitude > box_min_y) %>%
filter(biomass > -1) #remove clouds
write_csv(SF_control, "SF_control.csv")
#now the post processing
SB_control <- read_csv("./SB_control.csv")
LA_control <- read_csv("./LA_control.csv")
SD_control <- read_csv("./SD_control.csv")
SF_control <- read_csv("./SF_control.csv")
SB_control <- SB_control %>%
mutate(kelp = ifelse(SB_control$biomass >0, 1, 0))
SD_control <- SD_control %>%
mutate(kelp = ifelse(SD_control$biomass >0, 1, 0))
LA_control <- LA_control %>%
mutate(kelp = ifelse(LA_control$biomass >0, 1, 0))
SF_control <- SF_control %>%
mutate(kelp = ifelse(SF_control$biomass >0, 1, 0))
#add site codes
SB_control <- SB_control %>%
mutate(site = rep("SB_control", times = nrow(SB_control)))
SD_control <- SD_control %>%
mutate(site = rep("SD_control", times = nrow(SD_control)))
LA_control <- LA_control %>%
mutate(site = rep("LA_control", times = nrow(LA_control)))
SF_control <- SF_control %>%
mutate(site = rep("SF_control", times = nrow(SF_control)))
#and create one full dataframe
#first rename columns
names(SB_control)[5:6] =
c("UTM_x", "UTM_y")
names(SD_control)[5:6] =
c("UTM_x", "UTM_y")
names(LA_control)[5:6] =
c("UTM_x", "UTM_y")
names(SF_control)[5:6] =
c("UTM_x", "UTM_y")
controls <- rbind(SB_control, SD_control, LA_control)
write_csv(controls, "controls.csv")
#dates
controls <- controls %>%
mutate(date = ymd(controls$date_utc))
controls <- controls %>%
separate(date, c("year", "month", "day"), sep = "-")
controls$year <- as.numeric(controls$year)
controls$month <- as.numeric(controls$month)
controls$day <- as.numeric(controls$day)
controls <- controls %>%
mutate(qtr = ifelse(controls$month == 01| controls$month == 02| controls$month == 3, 1,
ifelse(controls$month == 04| controls$month == 05| controls$month == 6, 2,
ifelse(controls$month == 07| controls$month == 08| controls$month == 9, 3,
4))))
#attach the cities to their controls
kelp_cities <- read_csv("./kelp.csv")
kelp_all_sites <- rbind(kelp, controls)
write_csv(kelp_all_sites, "kelp_all_sites.csv")
kelp_all_sites <- read_csv("./kelp_all_sites.csv")
kelp_all_sites <- kelp_all_sites %>%
group_by(site,year, qtr) %>%
mutate(max_extent = sum(kelp)) %>%
ungroup() %>%
group_by(site) %>%
mutate(max_extent = max(max_extent))
kelp_all_sites_summary <- kelp_all_sites %>%
group_by(site, year, qtr) %>%
summarise(quarterly_extent = sum(kelp), max_extent = mean(max_extent))%>%
mutate(percent_coverage = (quarterly_extent/max_extent)*100 ) %>%
ungroup() %>%
group_by(site) %>%
mutate(time_point = row_number()) %>%
ungroup()
write_csv(kelp_all_sites_summary, "kelp_all_sites_summary.csv")
kelp_all_sites <- kelp_all_sites %>%
group_by(site) %>%
mutate(time_point = row_number())
ggplot(data = kelp_all_sites_summary, aes(x = time_point, y = percent_coverage, color = site)) +
geom_line() +
facet_wrap(~site)