-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcod-code.R
More file actions
401 lines (312 loc) · 15.2 KB
/
cod-code.R
File metadata and controls
401 lines (312 loc) · 15.2 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
## Clean REM Data Script##
# {
rm(list = (ls()))
setwd("Z:\\C8505_Future_catch_policy\\Working_Area\\C8505C_Catch Data Monitoring (REM)\\Data Analysis\\Secondary\\COD Report Code")
install.packages("RPostgreSQL")
install.packages("RPostgres")
install.packages("tidyverse")
install.packages("mappplots")
install.packages("odbc")
require(RPostgreSQL)
library(DBI)
library(RODBC)
library(RPostgreSQL)
library(DBI)
library(dplyr)
require(RPostgres)
library(dplyr)
library(ggplot2)
library(data.table)
library(tidyverse)
library(odbc)
library(mapplots)
## DB Call##
con <- dbConnect(
Postgres(),
host = "citpgsqlbx011.postgres.database.azure.com",
dbname = "bx011",
port = 5432,
user = "ana_rs@citpgsqlbx011",
password = "FQGg_v++5=T9"
)
dbListTables(con)
###
## get the length weight conversion factors
WL.params2 <- read.table("Ref_tables\\WL.params.2014.txt", sep = "\t", header = T)
#
# change the ices areas names
WL.params2$ices_area <- as.character(WL.params2$Region)
WL.params2$ices_area[WL.params2$ices_area == "101A"] <- "27.1.a"
WL.params2$ices_area[WL.params2$ices_area == "101D"] <- "27.1.d"
WL.params2$ices_area[WL.params2$ices_area == "101G"] <- "27.1.g"
WL.params2$ices_area[WL.params2$ices_area == "102A"] <- "27.2.a"
WL.params2$ices_area[WL.params2$ices_area == "102B"] <- "27.2.b"
WL.params2$ices_area[WL.params2$ices_area == "102C"] <- "27.2.c"
WL.params2$ices_area[WL.params2$ices_area == "102D"] <- "27.2.d"
WL.params2$ices_area[WL.params2$ices_area == "102E"] <- "27.2.e"
WL.params2$ices_area[WL.params2$ices_area == "102F"] <- "27.2.f"
WL.params2$ices_area[WL.params2$ices_area == "102G"] <- "27.2.g"
WL.params2$ices_area[WL.params2$ices_area == "103A"] <- "27.3.a"
WL.params2$ices_area[WL.params2$ices_area == "104A"] <- "27.4.a"
WL.params2$ices_area[WL.params2$ices_area == "104B"] <- "27.4.b"
WL.params2$ices_area[WL.params2$ices_area == "104C"] <- "27.4.c"
WL.params2$ices_area[WL.params2$ices_area == "107A"] <- "27.7.a"
WL.params2$ices_area[WL.params2$ices_area == "107B"] <- "27.7.b"
WL.params2$ices_area[WL.params2$ices_area == "107C"] <- "27.7.c"
WL.params2$ices_area[WL.params2$ices_area == "107D"] <- "27.7.d"
WL.params2$ices_area[WL.params2$ices_area == "107E"] <- "27.7.e"
WL.params2$ices_area[WL.params2$ices_area == "107F"] <- "27.7.f"
WL.params2$ices_area[WL.params2$ices_area == "107G"] <- "27.7.g"
WL.params2$ices_area[WL.params2$ices_area == "107H"] <- "27.7.h"
WL.params2$ices_area[WL.params2$ices_area == "107i"] <- "27.7.i"
WL.params2$ices_area[WL.params2$ices_area == "107J"] <- "27.7.j"
WL.params2$ices_area[WL.params2$ices_area == "107K"] <- "27.7.k"
WL.params2$ices_area[WL.params2$ices_area == "106A"] <- "27.6.a"
WL.params2$ices_area[WL.params2$ices_area == "106B"] <- "27.6.b"
WL.params2$ices_area[WL.params2$ices_area == "106C"] <- "27.6.c"
WL.params2$ices_area[WL.params2$ices_area == "108A"] <- "27.8.a"
WL.params2$ices_area[WL.params2$ices_area == "108B"] <- "27.8.b"
WL.params2$ices_area[WL.params2$ices_area == "108C"] <- "27.8.c"
WL.params2$ices_area[WL.params2$ices_area == "109A"] <- "27.9.a"
WL.params2$ices_area[WL.params2$ices_area == "109B"] <- "27.9.b"
#### explore DB
expl_catch <- dbGetQuery(con, "SELECT * FROM bx011.catch") # raw unsorted catch data
haul <- dbGetQuery(con, "SELECT * FROM bx011.haul") # haul information
expl_trip <- dbGetQuery(con, "SELECT * FROM bx011.trip") # trip information
expl_vessel <- dbGetQuery(con, "SELECT * FROM bx011.vessel") # list of all vessels around the UK
species <- dbGetQuery(con, "SELECT * FROM bx011.species") # list of cefas vessel species codes
### building DF###
DATE1 <- "2022-01-01" ## start date of data range
DATE2 <- "2022-06-30" ## end date of data range
# DATE1<-"2021-01-01"##start date of data range
# DATE2<-"2021-12-31"##end date of data range
# start extraction: added haul_number, month, state on March 2022
catch <- dbGetQuery(con, "SELECT bx011.catch.catch_id, bx011.catch.haul_id, bx011.haul.haul_number, date_part('quarter', bx011.haul.end_date) as QUARTER, date_part('month', bx011.haul.end_date) as MONTH,
bx011.haul.end_year, bx011.haul.start_date, bx011.haul.start_time,bx011.haul.end_date, bx011.haul.end_time, bx011.catch.species as Species, bx011.catch.species_comment, bx011.catch.tags,bx011.catch.state, bx011.catch.fate, bx011.catch.numberfish,
bx011.catch.catchlength, bx011.catch.weight, bx011.catch.volume, bx011.catch.volume_units,
bx011.haul.tripid, bx011.haul.vessel_id, bx011.haul.geartype, bx011.haul.fishingactivity_id, bx011.haul.end_ices_rectangle, bx011.haul.end_ices_area, bx011.haul.endlocation_latitude,bx011.haul.endlocation_longitude
FROM bx011.catch,
bx011.haul
WHERE bx011.catch.haul_id = bx011.haul.haul_id")
catch <- catch %>% filter(start_date >= DATE1, end_date <= DATE2) ## df containing full catch info for dates
haul.check.2022 <-haul%>%filter(start_date>=DATE1, end_date<=DATE2)
Vessel <- dbGetQuery(con, "SELECT bx011.vessel.vessel_name, bx011.vessel.vessel_rss, bx011.trip.tripid,bx011.trip.start_date, bx011.trip.end_date, bx011.trip.vessel_id
FROM bx011.vessel LEFT JOIN bx011.trip ON bx011.vessel.vessel_id = bx011.trip.vessel_id")
Vessel <- Vessel %>% filter(start_date >= DATE1, end_date <= DATE2) # extract right timeframe
names(Vessel)[names(Vessel) == "end_date"] <- "end_date_trip"
names(Vessel)[names(Vessel) == "start_date"] <- "start_date_trip"
#Check Vessel ID
unique(Vessel$vessel_id)
## identifying hauls with catch data
haul <- haul %>% filter(start_date >= DATE1, end_date <= DATE2)
hauls_identified <- length(unique(haul$haul_id))
hauls_identified
hauls_identified_with_catch_data <- length(unique(catch$haul_id))
hauls_identified_with_catch_data
# change the ices areas to match with WL.params2
catch$end_ices_area <- as.character(catch$end_ices_area)
catch$end_ices_area[catch$end_ices_area == "IVa"] <- "27.4.a"
catch$end_ices_area[catch$end_ices_area == "IVb"] <- "27.4.b"
catch$end_ices_area[catch$end_ices_area == "IVc"] <- "27.4.c"
catch$end_ices_area[catch$end_ices_area == "VIIa"] <- "27.7.a"
catch$end_ices_area[catch$end_ices_area == "VIIb"] <- "27.7.b"
catch$end_ices_area[catch$end_ices_area == "VIIc"] <- "27.7.c"
catch$end_ices_area[catch$end_ices_area == "VIId"] <- "27.7.d"
catch$end_ices_area[catch$end_ices_area == "VIIe"] <- "27.7.e"
catch$end_ices_area[catch$end_ices_area == "VIIf"] <- "27.7.f"
catch$end_ices_area[catch$end_ices_area == "VIIg"] <- "27.7.g"
catch$end_ices_area[catch$end_ices_area == "VIIh"] <- "27.7.h"
catch$end_ices_area[catch$end_ices_area == "VIIj"] <- "27.7.j"
catch$end_ices_area[catch$end_ices_area == "VIIk"] <- "27.7.k"
catch$end_ices_area[catch$end_ices_area == "VIa"] <- "27.6.a"
catch$end_ices_area[catch$end_ices_area == "VIb"] <- "27.6.b"
catch$end_ices_area[catch$end_ices_area == "VIc"] <- "27.6.c"
catch$end_ices_area[catch$end_ices_area == "VIIIA"] <- "27.8.a"
catch$end_ices_area[catch$end_ices_area == "VIIIB"] <- "27.8.b"
catch$end_ices_area[catch$end_ices_area == "VIIIC"] <- "27.8.c"
# Add Sex
catch$Sex <- "U" # could be updated with the tags column
# change the names of the variable to match with WL.params2
names(catch)[names(catch) == "species"] <- "Species"
names(catch)[names(catch) == "quarter"] <- "QUARTER"
names(catch)[names(catch) == "catchlength"] <- "Length"
names(catch)[names(catch) == "end_ices_area"] <- "ices_area"
## change the fate descriptions so we have consistent name between systems
unique(catch$fate)
catch$fate[catch$fate == "RET Gen"] <- "retainedGeneral"
catch$fate[catch$fate == "DIS Samp"] <- "discardedSample"
catch$fate[catch$fate == "RET Samp"] <- "retainedSample"
catch$fate[catch$fate == "DIS Gen"] <- "discardedGeneral"
catch <- catch[!(catch$fate == "NULL"), ] # delete lines with fate =NULL = 2 seabass that are in the skipper files but not seen on camera
catch <- catch[!(catch$fate == "Catch"), ] # delete fate=catch
unique(catch$fate)
## SubsampleCOD
catch_cod <- catch %>%
filter(Species == "COD")
# export csv to make changes (see emails)
#write.csv(catch_cod, file = "catch_cod.csv")
catch_cod <- read_csv("Z:\\C8505_Future_catch_policy\\Working_Area\\C8505C_Catch Data Monitoring (REM)\\Data Analysis\\Secondary\\COD Report Code\\catch_cod.csv")
## Add length weight conversions
d <- merge(catch_cod, WL.params2, all.x = T)
# head(d)
# d<-d %>% filter(year==2020 & QUARTER==4)#filter recent data
# convert length into 1cm interval
d$Length <- round(d$Length, 0)
# convert length to weight
d$Wt.At.Len_kg <- with(d, ((Length)^FACTOR.B) * FACTOR.A / 1000)
fish_dis_rf <- d %>%
filter(fate %in% c("discardedGeneral")) %>%
filter(is.na(weight)) %>%
select(haul_id, numberfish) %>%
rename(fish_rf = numberfish)
fish_ret_rf <- d %>%
filter(fate %in% c("retainedGeneral")) %>%
filter(is.na(weight)) %>%
select(haul_id, numberfish) %>%
rename(fish_rf = numberfish)
cod_general <- d %>%
filter(fate %in% c("discardedGeneral", "retainedGeneral"))
cod_general <- d %>%
filter(is.na(Wt.At.Len_kg)) %>%
filter(weight > 0) # weights for hauls where fish were not measured
## haul weights
cod_sample <- d %>%
filter(fate %in% c("retainedSample", "discardedSample"))
cod_sample_retained <- cod_sample %>%
filter(fate == "retainedSample")
cod_sample_retained <- inner_join(cod_sample_retained, fish_ret_rf, by = "haul_id")
cod_sample_discarded <- cod_sample %>%
filter(fate == "discardedSample")
cod_sample_discarded <- inner_join(cod_sample_discarded, fish_dis_rf, by = "haul_id")
cod_sample_ret_combined <- cod_sample_retained %>%
group_by(haul_id) %>%
mutate(total_weight = sum(Wt.At.Len_kg), no.fish = sum(numberfish)) %>%
distinct(haul_id, .keep_all = TRUE) %>%
mutate(sample_weight = total_weight * (fish_rf / no.fish))
cod_sample_dis_combined <- cod_sample_discarded %>%
group_by(haul_id) %>%
mutate(total_weight = sum(Wt.At.Len_kg), no.fish = sum(numberfish)) %>%
distinct(haul_id, .keep_all = TRUE) %>%
mutate(sample_weight = total_weight * (fish_rf / no.fish))
rec_dis <- sum(sum(cod_sample_dis_combined$sample_weight), sum(cod_gen_dis$general_weight))
rec_ret <- sum(sum(cod_sample_ret_combined$sample_weight), sum(cod_gen_ret$general_weight))
# Combining sample and gen weights.
cod_sample_ret_combined <- cod_sample_ret_combined %>%
subset(select = -c(weight)) %>%
add_column(general_weight = NA) %>%
add_column(dis.ret = "retained")
cod_sample_dis_combined <- cod_sample_dis_combined %>%
subset(select = -c(weight)) %>%
add_column(general_weight = NA) %>%
add_column(dis.ret = "discarded")
cod_gen_dis <- cod_general %>%
filter(fate == "discardedGeneral") %>%
rename(general_weight = weight) %>%
add_column(sample_weight = NA) %>%
add_column(dis.ret = "discarded")
cod_gen_ret <- cod_general %>%
filter(fate == "retainedGeneral") %>%
rename(general_weight = weight) %>%
add_column(sample_weight = NA) %>%
add_column(dis.ret = "retained")
# create rf to trip level
no.hauls <- haul %>%
group_by(tripid) %>%
mutate(total_hauls = length(unique(haul_id))) %>%
select(tripid, total_hauls) %>%
distinct(tripid, .keep_all = TRUE)
sampled_hauls <- catch %>%
group_by(tripid) %>%
mutate(sampled_hauls = length(unique(haul_id))) %>%
select(tripid, sampled_hauls) %>%
distinct(tripid, .keep_all = TRUE)
haul_rf <- inner_join(sampled_hauls, no.hauls, by = "tripid") %>%
mutate(trip_rf = total_hauls / sampled_hauls)
# combine gen+sampled
ret_cod <- bind_rows(cod_gen_ret, cod_sample_ret_combined)
dis_cod <- bind_rows(cod_gen_dis, cod_sample_dis_combined)
ret_cod <- inner_join(haul_rf, ret_cod, by = "tripid") %>%
mutate(recorded_weight = coalesce(general_weight, sample_weight)) %>%
mutate(fate_weight = trip_rf * recorded_weight) %>%
group_by(tripid) %>%
mutate(trip_weight = sum(fate_weight)) %>%
distinct(tripid, .keep_all = TRUE)
dis_cod <- inner_join(haul_rf, dis_cod, by = "tripid") %>%
mutate(recorded_weight = coalesce(general_weight, sample_weight)) %>%
mutate(fate_weight = trip_rf * recorded_weight) %>%
group_by(tripid) %>%
mutate(trip_weight = sum(fate_weight)) %>%
distinct(tripid, .keep_all = TRUE)
## values used in cod 2 pager
cod_retained_weight <- sum(ret_cod$trip_weight)
cod_discarded_weight <- sum(dis_cod$trip_weight)
#
analysed_trips <- n_distinct(catch$tripid)
analysed_hauls <- n_distinct(catch$haul_id)
# recorded weight is the sample weight and the general weights added together
# fate weight is recorded weight for each haul, raised to trip level
# trip weight is the raised haul weights added together
# raise to vessel level
no.trips <- haul %>%
group_by(vessel_id) %>%
mutate(total_trips = length(unique(tripid))) %>%
select(vessel_id, total_trips) %>%
distinct(vessel_id, .keep_all = TRUE)
sampled_trips <- catch %>%
group_by(vessel_id) %>%
mutate(sampled_trips = length(unique(tripid))) %>%
select(vessel_id, sampled_trips) %>%
distinct(vessel_id, .keep_all = TRUE)
vessel_rf <- inner_join(no.trips, sampled_trips, by = "vessel_id") %>%
mutate(vessel_rf = total_trips / sampled_trips)
ret_cod_vessel <- inner_join(vessel_rf, ret_cod, by = "vessel_id") %>%
mutate(vessel_weight = trip_weight * vessel_rf) %>%
group_by(vessel_id) %>%
mutate(vessel_weight_raised = sum(vessel_weight)) %>%
distinct(vessel_id, .keep_all = TRUE)
#
dis_cod_vessel <- inner_join(vessel_rf, dis_cod, by = "vessel_id") %>%
mutate(vessel_weight = trip_weight * vessel_rf) %>%
group_by(vessel_id) %>%
mutate(vessel_weight_raised = sum(vessel_weight)) %>%
distinct(vessel_id, .keep_all = TRUE)
# }
# cod ret vs dis count
cod_ret_count <- catch_cod %>%
filter(fate %in% c("retainedGeneral"))
cod_ret_count <- sum(cod_ret_count$numberfish)
cod_dis_count <- catch_cod %>%
filter(fate %in% c("discardedGeneral"))
cod_dis_count <- sum(cod_dis_count$numberfish)
## 2021 checks
ret_cod <- inner_join(haul_rf, ret_cod, by = "tripid") %>%
mutate(recorded_weight = coalesce(general_weight, sample_weight)) %>%
group_by(vessel_id) %>%
mutate(recorded_vessel_weight = sum(recorded_weight)) %>%
distinct(vessel_id, .keep_all = TRUE)
dis_cod <- inner_join(haul_rf, dis_cod, by = "tripid") %>%
mutate(recorded_weight = coalesce(general_weight, sample_weight)) %>%
group_by(vessel_id) %>%
mutate(recorded_vessel_weight = sum(recorded_weight)) %>%
distinct(vessel_id, .keep_all = TRUE)
cod_counts <- cod_general %>%
group_by(vessel_id) %>%
mutate(number_counted = sum(numberfish)) %>%
distinct(vessel_id, .keep_all = TRUE)
## Plotting
cod_sample <- cod_sample %>%
add_column(dis.ret = NA)
cod_sample$dis.ret[cod_sample$fate == "retainedSample"] <- "Retained "
cod_sample$dis.ret[cod_sample$fate == "discardedSample"] <- "Discarded"
cod_sample <- data.frame(cod_sample[rep(seq_len(dim(cod_sample)[1]), cod_sample$numberfish), , drop = FALSE], row.names = NULL)
library(ggplot2)
ggplot(data = cod_sample, aes(x = Length, fill = dis.ret)) +
geom_histogram(boundary = 0, binwidth = 5, colour = "black") +
scale_x_continuous(breaks = seq(0, 100, by = 5), name = "Length (cm)") +
scale_y_continuous(breaks = seq(0, 90, by = 5), name = "Number at Length") +
theme(panel.grid.minor = element_blank()) +
scale_fill_manual(name = "Fate", values = c("#006E8C", "#00AAB4")) +
geom_vline(
xintercept = 35, linetype = "dashed",
color = "red", size = 1
)