-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathget_dfo-sog.R
More file actions
463 lines (393 loc) · 17.5 KB
/
get_dfo-sog.R
File metadata and controls
463 lines (393 loc) · 17.5 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
################################################################################
#### R code to clean trawl survey for the DFO Strait of Georgia Survey
#### NB: this is only 2 years of data
#### Public data Ocean Adapt
#### Contacts: Shelee Hamilton Shelee.Hamilton@dfo-mpo.gc.ca Head,
#### Fishery & Assessment Data Section, Science Branch, DFO Canada
#### Maria Cornthwaite Maria.Cornthwaite@dfo-mpo.gc.ca Program Head,
#### Groundfish Data Unit, Science Branch, DFO Canada
#### Coding: Dan Forrest, Zoë Kitchel November 2021
################################################################################
####Update
####Zoe Kitchel
#### May 4, 2024
####Following issue 47, need to update sum technique to remove duplicates
################################################################################
#--------------------------------------------------------------------------------------#
#### LOAD LIBRARIES AND FUNCTIONS ####
#--------------------------------------------------------------------------------------#
library(tidyverse)
library(lubridate)
library(googledrive)
library(taxize) # for getting correct species names
library(magrittr) # for names wrangling
library(readr)
library(dplyr)
library(PBSmapping)
library(readxl)
library(here)
source("functions/clean_taxa.R")
source("functions/write_clean_data.R")
source("functions/apply_trimming_method1.R")
source("functions/apply_trimming_method2.R")
source("functions/flag_spp.R")
fishglob_data_columns <- read_excel("standard_formats/fishglob_data_columns.xlsx")
#Data for the Strait of Georgia Survey can be best accessed using the Pinsky
#Lab Ocean Adapt Git Hub Repository.
#Contact malin.pinsky@rutgers.edu for questions or help accessing
#--------------------------------------------------------------------------------------#
#### PULL IN AND EDIT RAW DATA FILES ####
#--------------------------------------------------------------------------------------#
SOG_catch <- read_csv(
"https://github.com/pinskylab/OceanAdapt/raw/master/data_raw/SOG_catch.csv",
col_types = cols(
Survey.Year = col_integer(),
Trip.identifier = col_integer(),
Set.number = col_integer(),
ITIS.TSN = col_integer(),
Species.code = col_character(),
Scientific.name = col_character(),
English.common.name = col_character(),
French.common.name = col_character(),
LSID = col_character(),
Catch.weight..kg. = col_double(),
Catch.count..pieces. = col_integer()
))
SOG_effort <- read_csv(
"https://github.com/pinskylab/OceanAdapt/raw/master/data_raw/SOG_effort.csv",
col_types =
cols(
Survey.Year = col_integer(),
Trip.identifier = col_integer(),
Vessel.name = col_character(),
Trip.start.date = col_character(),
Trip.end.date = col_character(),
GMA = col_character(),
PFMA = col_character(),
Set.number = col_integer(),
Set.date = col_character(),
Start.latitude = col_double(),
Start.longitude = col_double(),
End.latitude = col_double(),
End.longitude = col_double(),
Bottom.depth..m. = col_double(),
Tow.duration..min. = col_integer(),
Distance.towed..m. = col_double(),
Vessel.speed..m.min. = col_double(),
Trawl.door.spread..m. = col_double(),
Trawl.mouth.opening.height..m. = col_double()
)) %>%
dplyr::select(Trip.identifier, Set.number,Survey.Year,Set.date, Trip.start.date,Trip.end.date,
GMA, PFMA,Set.date, Start.latitude,Start.longitude, End.latitude, End.longitude,
Bottom.depth..m., Tow.duration..min.,Distance.towed..m., Trawl.door.spread..m.,
Trawl.mouth.opening.height..m. )
#--------------------------------------------------------------------------------------#
#### REFORMAT AND MERGE DATA FILES ####
#--------------------------------------------------------------------------------------#
SOG <- left_join(SOG_catch, SOG_effort, by = c("Trip.identifier", "Set.number",
"Survey.Year"))
SOG <- SOG %>%
# Create a unique haul_id
mutate(
haul_id = paste(
formatC(
Trip.identifier, width=3, flag=0), formatC(Set.number, width=3, flag=0),
sep= "-"),
# Add "strata" (define by lat, lon and depth bands) where needed # degree bins
# # 100 m bins
# no need to use lon grids on west coast (so narrow)
stratum = paste(
floor(Start.latitude), floor(Start.longitude),floor(Bottom.depth..m./100)*100,
sep= "-"),
# catch weight (kg.) per tow/
# (distance towed in m * trawl door spread m) * (1000000m^2/1km^2)
wgt_cpue = Catch.weight..kg./(Distance.towed..m.*Trawl.door.spread..m.) *1000000,
# catch weight (kg.) per tow/
# time of tow in minutes*60 minutes/hour
wgt_h = Catch.weight..kg./Tow.duration..min.*60,
# catch abundance per tow/
# (distance towed in m * trawl door spread m) * (1000000m^2/1km^2)
num_cpue = Catch.count..pieces./(Distance.towed..m.*Trawl.door.spread..m.) *1000000,
# catch weight (kg.) per tow/
# time of tow in minutes*60 minutes/hour
num_h = Catch.count..pieces./Tow.duration..min.*60,
area_swept = (Distance.towed..m.*Trawl.door.spread..m.)/1000000
)
SOG <- SOG %>%
rename(
latitude = Start.latitude,
longitude = Start.longitude,
depth = Bottom.depth..m.,
verbatim_name = Scientific.name,
num = Catch.count..pieces.,
wgt = Catch.weight..kg.
) %>%
mutate(
date = as.Date(Set.date),
year = Survey.Year,
haul_dur = Tow.duration..min./60
) %>%
filter(
verbatim_name != "" &
!grepl("egg", verbatim_name)
) %>%
# adjust verbatim_name names
mutate(verbatim_name = ifelse(grepl("Lepidopsetta", verbatim_name),
"Lepidopsetta sp.", verbatim_name),
verbatim_name = ifelse(grepl("Bathyraja", verbatim_name),
'Bathyraja sp.', verbatim_name),
verbatim_name = ifelse(grepl("Squalus", verbatim_name),
'Squalus suckleyi', verbatim_name))
# Does the spp column contain any eggs or non-organism notes?
#As of fall 2021, nothing stuck out as needing to be removed
test <- SOG %>%
dplyr::select(verbatim_name) %>%
filter(!is.na(verbatim_name)) %>%
distinct() %>%
mutate(verbatim_name = as.factor(verbatim_name)) %>%
filter(grepl("egg", verbatim_name) & grepl("", verbatim_name))
stopifnot(nrow(test)==0)
# combine the wtcpue for each species by haul which is necessary because
#sometimes there are multiple observations for a single genus or family
#i.e.
#HEXACTINELLIDA, GLASS SPONGES; WILLEMOES'S WHITE SEA PEN; CRANGONS
#function to make sum of NAs a NA, but some of NA, 1, 1
my_sum <- function(x){
if(all(is.na(x))){
return(NA)
}
else{
return(sum(x, na.rm = TRUE))
}
}
SOG <- SOG %>%
group_by(haul_id,year, latitude, longitude, depth, verbatim_name, area_swept,
date, haul_dur) %>%
summarise(wgt = my_sum(wgt), num = my_sum(num),
wgt_cpue = my_sum(wgt_cpue), wgt_h = my_sum(wgt_h),
num_h = my_sum(num_h), num_cpue = my_sum(num_cpue)) %>%
ungroup()
SOG <- SOG %>%
# add survey column
mutate(survey = "DFO-SOG",
source = "DFO",
timestamp = mdy("08/21/2020"),
country = "Canada",
continent = "n_america",
stat_rec = NA,
verbatim_aphia_id = NA,
aphia_id = NA,
sub_area = NA,
station = NA,
stratum = NA,
month = lubridate::month(date),
day = lubridate::day(date),
season = NA,
quarter = NA,
gear = NA,
sbt = NA,
sst = NA
) %>%
dplyr::select(survey, haul_id, source, timestamp, country, sub_area, continent, stat_rec, station, stratum,
year, month, day, quarter, season, latitude, longitude, haul_dur, area_swept,
gear, depth, sbt, sst, verbatim_name, num, num_h, num_cpue,
wgt, wgt_h, wgt_cpue, verbatim_name, verbatim_aphia_id)
#check for duplicates, should not be any with more than 1 obs
#check for duplicates
count_SOG <- SOG %>%
group_by(haul_id, verbatim_name) %>%
mutate(count = n())
#none!
#which ones are duplicated?
unique_name_match <- count_SOG %>%
group_by(verbatim_name) %>%
filter(count>1) %>%
distinct(verbatim_name)
unique_name_match
#check if empty
#duplicated before fix above
#verbatim_name
#MERLUCCIUS PRODUCTUS
#RAJA RHINA
#PLATICHTHYS STELLATUS
#PORICHTHYS NOTATUS
#GLYPTOCEPHALUS ZACHIRUS
#EUALUS
#APHRODITA
#BRISASTER LATIFRONS
#ECHIURA
#--------------------------------------------------------------------------------------#
#### INTEGRATE CLEAN TAXA FROM TAXA ANALYSIS ####
#--------------------------------------------------------------------------------------#
# Get WoRM's id for sourcing
wrm <- gna_data_sources() %>%
filter(title == "World Register of Marine Species") %>%
pull(id)
### Automatic cleaning
# Set Survey code
sog_survey_code <- "DFO-SOG"
SOG <- SOG %>%
mutate(
taxa2 = str_squish(verbatim_name),
taxa2 = str_remove_all(taxa2," spp.| sp.| spp| sp|NO "),
taxa2 = str_to_sentence(str_to_lower(taxa2))
)
# Get clean taxa
clean_auto <- clean_taxa(unique(SOG$taxa2), input_survey = sog_survey_code,
save = F, output=NA, fishbase=T)
#This fails to ID 0 species
#--------------------------------------------------------------------------------------#
#### INTEGRATE CLEAN TAXA in DFO-SOG survey data ####
#--------------------------------------------------------------------------------------#
correct_taxa <- clean_auto %>%
dplyr::select(-survey)
clean_sog <- left_join(SOG, correct_taxa, by=c("taxa2"="query")) %>%
filter(!is.na(taxa)) %>% # query does not indicate taxa entry
#that were removed in the cleaning procedure
# so all NA taxa have to be removed from the surveys because: non-existing,
#non marine or non fish
rename(accepted_name = taxa,
aphia_id = worms_id) %>%
mutate(verbatim_aphia_id = NA,
num_cpua = num_cpue,
num_cpue = num_h,
wgt_cpua = wgt_cpue,
wgt_cpue = wgt_h,
survey_unit = ifelse(survey %in% c("BITS","NS-IBTS","SWC-IBTS"),
paste0(survey,"-",quarter),survey),
survey_unit = ifelse(survey %in% c("NEUS","SEUS","SCS","GMEX"),
paste0(survey,"-",season),survey_unit)) %>%
dplyr::select(fishglob_data_columns$`Column name fishglob`)
#check for duplicates
count_clean_sog <- clean_sog %>%
group_by(haul_id, accepted_name) %>%
mutate(count = n())
#none!
#which ones are duplicated?
unique_name_match <- count_clean_sog %>%
group_by(verbatim_name, accepted_name) %>%
filter(count>1) %>%
distinct(verbatim_name, accepted_name)
unique_name_match
#check if empty
# -------------------------------------------------------------------------------------#
#### SAVE DATABASE IN GOOGLE DRIVE ####
# -------------------------------------------------------------------------------------#
# Just run this routine should be good for all
write_clean_data(data = clean_sog, survey = "SOG", overwrite = T, csv = T)
# -------------------------------------------------------------------------------------#
#### FLAGS ####
# -------------------------------------------------------------------------------------#
#install required packages that are not already installed
required_packages <- c("data.table",
"devtools",
"dggridR",
"dplyr",
"fields",
"forcats",
"ggplot2",
"here",
"magrittr",
"maps",
"maptools",
"raster",
"rcompendium",
"readr",
"remotes",
"rrtools",
"sf",
"sp",
"tidyr",
"usethis")
not_installed <- required_packages[!(required_packages %in% installed.packages()[ , "Package"])]
if(length(not_installed)) install.packages(not_installed)
#load pipe operator
library(magrittr)
######### Apply taxonomic flagging per region
#get vector of regions (here the survey column)
regions <- levels(as.factor(clean_sog$survey))
#run flag_spp function in a loop
for (r in regions) {
flag_spp(clean_sog, r)
}
######### Apply trimming per survey_unit method 1
#apply trimming for hex size 7
dat_new_method1_hex7 <- apply_trimming_per_survey_unit_method1(clean_sog, 7)
#apply trimming for hex size 8
dat_new_method1_hex8 <- apply_trimming_per_survey_unit_method1(clean_sog, 8)
######### Apply trimming per survey_unit method 2
dat_new_method2 <- apply_trimming_per_survey_unit_method2(clean_sog)
#-------------------------------------------------------------------------------------------#
#### ADD STRANDARDIZATION FLAGS ####
#-------------------------------------------------------------------------------------------#
surveys <- sort(unique(clean_sog$survey))
survey_units <- sort(unique(clean_sog$survey_unit))
survey_std <- clean_sog %>%
mutate(flag_taxa = NA_character_,
flag_trimming_hex7_0 = NA_character_,
flag_trimming_hex7_2 = NA_character_,
flag_trimming_hex8_0 = NA_character_,
flag_trimming_hex8_2 = NA_character_,
flag_trimming_2 = NA_character_)
# integrate taxonomic flags
for(i in 1:length(surveys)){
if(!surveys[i] %in% c("FALK","GSL-N","MRT","NZ-CHAT","SCS", "SWC-IBTS")){
xx <- data.frame(read_delim(paste0("outputs/Flags/taxonomic_flagging/",
surveys[i],"_flagspp.txt"),
delim=";", escape_double = FALSE, col_names = FALSE,
trim_ws = TRUE))
xx <- as.vector(unlist(xx[1,]))
survey_std <- survey_std %>%
mutate(flag_taxa = ifelse(survey == surveys[i] & accepted_name %in% xx,
"TRUE",flag_taxa))
rm(xx)
}
}
# integrate spatio-temporal flags
for(i in 1:length(survey_units)){
hex_res7_0 <- read.csv(paste0("outputs/Flags/trimming_method1/hex_res7/",
survey_units[i], "_hex_res_7_trimming_0_hauls_removed.csv"),
sep = ";", colClasses=c(haul_id = "character"))
hex_res7_0 <- as.vector(hex_res7_0[,1])
hex_res7_2 <- read.csv(paste0("outputs/Flags/trimming_method1/hex_res7/",
survey_units[i], "_hex_res_7_trimming_02_hauls_removed.csv"),
sep = ";", colClasses=c(haul_id = "character"))
hex_res7_2 <- as.vector(hex_res7_2[,1])
hex_res8_0 <- read.csv(paste0("outputs/Flags/trimming_method1/hex_res8/",
survey_units[i], "_hex_res_8_trimming_0_hauls_removed.csv"),
sep= ";", colClasses=c(haul_id = "character"))
hex_res8_0 <- as.vector(hex_res8_0[,1])
hex_res8_2 <- read.csv(paste0("outputs/Flags/trimming_method1/hex_res8/",
survey_units[i], "_hex_res_8_trimming_02_hauls_removed.csv"),
sep = ";", colClasses=c(haul_id = "character"))
hex_res8_2 <- as.vector(hex_res8_2[,1])
# trim_2 <- read.csv(paste0("outputs/Flags/trimming_method2/",
# survey_units[i],"_hauls_removed.csv"))
# trim_2 <- as.vector(trim_2[,1])
survey_std <- survey_std %>%
mutate(flag_trimming_hex7_0 = ifelse(survey_unit == survey_units[i] & haul_id %in% hex_res7_0,
"TRUE",flag_trimming_hex7_0),
flag_trimming_hex7_2 = ifelse(survey_unit == survey_units[i] & haul_id %in% hex_res7_2,
"TRUE",flag_trimming_hex7_2),
flag_trimming_hex8_0 = ifelse(survey_unit == survey_units[i] & haul_id %in% hex_res8_0,
"TRUE",flag_trimming_hex8_0),
flag_trimming_hex8_2 = ifelse(survey_unit == survey_units[i] & haul_id %in% hex_res8_2,
"TRUE",flag_trimming_hex8_2),
# flag_trimming_2 = ifelse(survey_unit == survey_units[i] & haul_id %in% trim_2,
# "TRUE", flag_trimming_2)
)
rm(hex_res7_0, hex_res7_2, hex_res8_0)
# }
}
# verify that the flagging worked. these values should match the respective _stats_hauls.csv files in outputs/Flags/trimming_methods1 and 2
survey_std |>
group_by(survey_unit) |>
distinct(haul_id, flag_trimming_hex7_0, flag_trimming_hex7_2, flag_trimming_hex8_0, flag_trimming_hex8_2, flag_trimming_2) |>
summarize(hex7_0 = sum(!is.na(flag_trimming_hex7_0)),
hex7_2 = sum(!is.na(flag_trimming_hex7_2)),
hex8_0 = sum(!is.na(flag_trimming_hex8_0)),
hex8_2 = sum(!is.na(flag_trimming_hex8_2)),
trim_2 = sum(!is.na(flag_trimming_2))) # number of hauls doesn't match AI_stats_hauls.csv but does match AI_hauls_removed.csv. Odd.
# Just run this routine should be good for all
write_clean_data(data = survey_std, survey = "SOG_std",
overwrite = T, rdata=TRUE)