-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate-survey-data.R
461 lines (377 loc) · 14.4 KB
/
evaluate-survey-data.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
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
##################################################################################
#
# WGBEAM Tor a, b (1): Evaluate combined offshore and inshore data by region,
# and cross-regionally where relevant, for species used in fish stock assessment
# incl. elasmobranchs and brown shrimp
#
#
# Created by Loes, Gudjon, Giuseppe & Vaishav
# 25/03/2020
#
# Updated on 26/03/2021 by Loes
#################################################################################
# clear environment
rm(list=ls())
# load libraries
library(icesDatras)
library(tidyverse)
library(dplyr)
library(tidyr)
isurvey <-"DYFS"
##########################################
##########################################
## Get data from DATRAS
##########################################
##########################################
## Get station (HH) data from Datras
getSurveyList() # gives overview of all survey acronyms
tmp<-getDATRAS(record = "HH",
survey = "DYFS", #only 1 survey at a time
years= 2004:2020,
quarters = 1:4)
# look what's there
head(tmp)
table(tmp$Year)
table(tmp$Quarter)
table(tmp$Country)
table(tmp$Country, tmp$Year)
# select useful colums
tmp1<-tmp %>% select(Year, Quarter, Country, Ship, Survey, Gear, StNo, HaulNo, Month, Day,
Stratum, HaulDur,DayNight,ShootLat, ShootLong, HaulLat, HaulLong, StatRec,
Depth, HaulVal, DataType, Rigging, Tickler, Distance, GroundSpeed,
SurTemp, BotTemp, SurSal, BotSal)
# look for NAs
for (Var in names(tmp1)) {
missing <- sum(is.na(tmp1[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
## Get length (HL) data from Datras
tmpf<-getDATRAS(record = "HL",
survey = "DYFS", #only 1 survey at a time
years= 2004:2020,
quarters = 1:4)
# look what's there
head(tmpf)
table(tmpf$Year)
table(tmpf$Quarter)
table(tmpf$Country)
table(tmpf$Country, tmpf$Year)
# select useful colums
tmpf1<-tmpf %>% select(Year, Quarter, Country, Ship, Survey, Gear, StNo, HaulNo,
SpecCode, SpecVal, Sex, TotalNo, CatIdentifier, NoMeas,
SubFactor, SubWgt, CatCatchWgt, LngtCode, LngtClass, HLNoAtLngt, Valid_Aphia)
# look for NAs
for (Var in names(tmpf1)) {
missing <- sum(is.na(tmpf1[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
## Get age (CA) data from Datras
tmpa<-getDATRAS(record = "CA",
survey = "DYFS", #only 1 survey at a time
years= 2004:2020,
quarters = 1:4)
# look whats there
table(tmpa$Country, tmpa$Year)
table(tmpa$SpecCode, tmpa$Country)
# select useful colums
tmpa1 <- tmpa %>% select(Year, Quarter, Country, Ship, Survey, Gear, StNo, HaulNo,
SpecCode, AreaCode, LngtCode, LngtClass, Sex, Maturity,
Age, NoAtALK, IndWgt, Valid_Aphia)
##########################################
##########################################
## Create one dataset
##########################################
##########################################
## Joining HH en HL datasets
dat <-tmpf1 %>%
left_join(tmp1)
# look for NAs
for (Var in names(dat)) {
missing <- sum(is.na(dat[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
## Create a species list from the aphia codes
sp <-worms::wormsbyid(unique(dat$Valid_Aphia))
## Tidy up the species list
sp1<-sp %>% select(Valid_Aphia=AphiaID,scientificname)
## Get species names from worms
dat1 <- dat %>%
left_join(sp1,by="Valid_Aphia")
# look for NAs
for (Var in names(dat1)) {
missing <- sum(is.na(dat1[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
# only take data from valid hauls
dat2 <- dat1 %>%
filter(HaulVal == "V")
## Get width of gear from gear codes
# look which gearcodes are in the data
table(dat2$Gear)
# crate new column beamwidth
dat2 <- dat2 %>%
mutate(beamwidth = case_when(Gear == "BT8" ~ 8,
Gear == "BT7" ~ 7,
Gear == "BT6" ~ 6,
Gear == "BT4A" ~ 4,
Gear == "BT4AI" ~ 4,
Gear == "BT4S" ~ 4,
Gear == "BT3" ~ 3,
TRUE ~ NA_real_))
# check for NAs
table(dat2$beamwidth, useNA = "always")
# set all lenght measurements in mm
table(dat2$LngtCode)
dat2 <- dat2 %>%
mutate(Length = ifelse (LngtCode %in% c("1", "5"), LngtClass*10, LngtClass))
# rename dataset to dat
dat <- dat2
#save dat
#save(dat, file = "dat.rda")
load("dat.rda")
##########################################
##########################################
## Calculate swept area
##########################################
##########################################
# Option 1: based on beam width and distance
# check if there are missing records for Distance
test <- dat %>% filter(is.na(Distance))
table(test$Country, test$Year)
# look at the relationship HaulDur vs. Distance
ggplot(dat)+
geom_point(aes(x=HaulDur, y=Distance, color=Country))
# save plot
# setwd(outputpath)
# ggsave(filename = paste(isurvey, "hauldur_distance.png", sep="_"), plot = last_plot(), width=10,height=8, dpi=600)
# based on the plot above chose if you want to remove the outliers
dat <- dat %>%
filter(HaulDur < 40) %>%
filter(Distance < 4000)
# create column swept area
dat<-dat %>%
mutate(sweptarea1 = beamwidth*Distance/1000000) ## Calculate per swept area in km2
# When there is no distance recorded:
# Option 2: calculate swept area based on beam width, ground speed and haulduration
table(dat$GroundSpeed)
dat$GroundSpeed[dat$GroundSpeed == -9]<- 3.5 ## Set speed to average 3.5 knots when not recorded
# create column swept area
dat<-dat %>%
mutate(sweptarea2 = ((beamwidth/1000)*(1.852*GroundSpeed*HaulDur/60))) ## Calculate swept area in km2
# look for NAs
for (Var in names(dat)) {
missing <- sum(is.na(dat[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
look at difference between methodologies for swept area calculation??
ggplot(dat)+
geom_point(aes(x=sweptarea1, y=sweptarea2, color=Country))
ggplot(dat)+
geom_point(aes(x=sweptarea2, y=sweptarea1, color=Country))
# save plot
# setwd(outputpath)
# ggsave(filename = paste(isurvey, "swept_area_comparison.png", sep="_"), plot = last_plot(), width=10,height=8, dpi=600)
# make a list of the fished stations in the survey
stations <- dat %>%
select(Country, Ship, Survey, Gear, Year, StNo, HaulNo, ShootLat, ShootLong, StatRec, sweptarea2) %>%
distinct()
##########################################
##########################################
## Select species ##
##########################################
##########################################
ispec <- "Crangon crangon"
datspec<-dat %>%
filter(scientificname == ispec)
# look for NAs
for (Var in names(datspec)) {
missing <- sum(is.na(datspec[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
str(dat)
# calculate number of shrimp that were measured
number_measured <- datspec %>%
select(Country, Ship, Survey, Gear, Year, StNo, HaulNo, ShootLat, ShootLong, NoMeas) %>%
distinct()
number_measured <- number_measured %>%
group_by(Country, Ship, Survey, Gear, Year) %>%
summarize(NumMeas = sum(NoMeas))
ggplot(number_measured %>% filter(Year %in% c(2017:2020)))+
geom_bar(aes(x=Year, y=NumMeas, fill=Country), stat = "identity", position="dodge") +
facet_wrap(Country~Ship)
number_measured_last3years <- number_measured %>%
filter(Year %in% c(2017:2020))
number_measured_last3years[is.na(number_measured_last3years)] <- 0
# write table
# setwd(outputpath)
# write.csv(number_measured_last3years, file=paste(ispec, "number_measured.csv"))
# save plot
# setwd(outputpath)
# ggsave(filename = paste(ispec, "number_measured.png", sep="_"), plot = last_plot(), width=10,height=8, dpi=600)
# Calculate numbers per station
SpecNo <- datspec %>%
group_by(Country, Ship, Survey, Gear, Year, StNo, HaulNo, ShootLat, ShootLong, SubFactor, sweptarea2, TotalNo) %>%
#filter(HLNoAtLngt>0 & SubFactor>0) %>%
summarize(No = sum(HLNoAtLngt)) %>%
mutate(TotalNo1 = ifelse(TotalNo>0, TotalNo, #if TotalNo is filled in than take this value
ifelse(No>0 & SubFactor>0, No*SubFactor, NA))) # when TotalNo not filled in, calculate TotalNo based on HLNoAtLngt and SubFactor
# look for NAs
for (Var in names(SpecNo)) {
missing <- sum(is.na(SpecNo[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
ggplot(SpecNo)+
geom_bar(aes(x=Year, y=TotalNo1, fill=Country),stat = "identity")+
facet_wrap(~Ship)
# setwd(outputpath)
# ggsave(filename = paste(ispec, "total_numbers.png"), plot = last_plot(), width=10,height=8, dpi=600)
##########################################
##########################################
## CPUE
##########################################
##########################################
# Calculate CPUE (numbers/km2) by haul
byhaul <- SpecNo %>%
mutate(fishkm2 = TotalNo1/sweptarea2) %>%
mutate(sqrt_fishkm2 = sqrt(fishkm2))
### Plot catches
## Map by Haul ##
#first, make map of the north sea #
library(mapdata)
m <-map_data("worldHires")
## set map boundaries
xmin <- min(stations$ShootLong)
xmax <- max(stations$ShootLong)
ymin <- min(stations$ShootLat)
ymax <- max(stations$ShootLat)
### plot the map with the data ##
## note to change years to years of interest
mapbyhaul <-
ggplot()+
theme_light()+
geom_polygon(data=m,aes(long,lat,group=group),fill=NA,color="grey") + #plot the land
geom_point(data=byhaul %>% filter(Year %in% c(2015:2020)),
(aes(ShootLong,ShootLat, colour=Country, size=sqrt_fishkm2)), alpha=0.4) + #plot catches by haul
geom_point(data=stations %>% filter(Year %in% c(2015:2020)), aes(ShootLong,ShootLat), shape='.', size=0.05) + #plot the fished stations
facet_wrap(~Year,dir='v')+
labs(x = "Longitude (Degrees)", y = "Latitude (Degrees)", title = paste(ispec, "CPUE by haul", sep=" "), fill="CPUE (sqrt n/km^2)") +
coord_quickmap(xlim = c(xmin, xmax), ylim = c(ymin, ymax)) +
theme(legend.title = element_text(face = "italic", size = 8))
# mapbyhaul
# setwd(outputpath)
# ggsave(filename = paste(ispec, "CPUE_by_haul.png"), plot = last_plot(), width=8,height=10, dpi=600)
## Map by stat rectangle ##
# install geo package
# devtools::install_github("hafro/geo")
## ADD STATIONS WITH ZERO OBSERVATIONS!
byhaul_stat <- stations %>%
left_join(byhaul)
# look for NAs
for (Var in names(byhaul_stat)) {
missing <- sum(is.na(byhaul_stat[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
# Replace NAs with zero observations
byhaul_stat$fishkm2[is.na(byhaul_stat$fishkm2)]<- 0
byhaul_stat$sqrt_fishkm2[is.na(byhaul_stat$sqrt_fishkm2)]<- 0
# add StatRec where missing
byhaul_stat <- byhaul_stat %>%
mutate(StatRec = ifelse(!is.na(StatRec),
StatRec,geo::d2ir(ShootLat, ShootLong)))
## Calculate mean catch per rectangle
bystatrec<-byhaul_stat %>%
group_by(Country,Year,Ship, Survey, StatRec) %>%
summarize(mean_fishkm2=mean(fishkm2),
mean_sqrt_fishkm2=mean(sqrt_fishkm2)) %>%
mutate(lon = geo::ir2d(StatRec)$lon,
lat = geo::ir2d(StatRec)$lat)
## Breaks in data for scale in plot
my_breaks<-c(0,100, 200, 300, 400, 500,1000,1500,2000,2500,3000)
#Plot the map with the data ##
mapbystatrec <-
ggplot()+
theme_light()+
geom_tile(data=bystatrec %>% filter(Year %in% c(2016:2019)),aes(lon,lat,fill=mean_sqrt_fishkm2)) + #color statrec tiles based on mean cpue values
scale_fill_continuous(low="yellow",high="red",trans="log")+
geom_polygon(data=m,aes(long,lat,group=group), fill="grey") + #plot the land
geom_point(data=stations %>% filter(Year %in% c(2016:2019)),aes(ShootLong,ShootLat),shape=".")+ #plot fished stations
facet_wrap(~Year,dir='v')+
labs(x = "Longitude (Degrees)", y = "Latitude (Degrees)", title = paste(ispec, "mean CPUE by StatRec", sep=" "), fill="Mean CPUE (sqrt n/km^2)") +
coord_quickmap(xlim = c(xmin, xmax), ylim = c(ymin, ymax)) +
theme(legend.title = element_text(face = "italic", size = 8)) +
labs()
mapbystatrec
# setwd(outputpath)
# ggsave(filename = paste(ispec, "CPUE_by_statrec.png"), plot = last_plot(), width=8,height=10, dpi=600)
##########################################
##########################################
## Length distributions
##########################################
##########################################
LFD <- datspec %>%
filter(HLNoAtLngt >0 ) %>%
filter(HaulVal=="V") %>% # take only valid hauls into account
group_by(Year, Survey, Country, Length) %>%
summarize(lenf=sum(HLNoAtLngt),
raised_lenf=sum(HLNoAtLngt*SubFactor))
#look for outliers
ggplot(LFD)+
geom_point(aes(x=Length, y=lenf, color=Country))
# setwd(outputpath)
# ggsave(filename = paste(ispec, "lengths.png"), plot = last_plot(), width=10,height=8, dpi=600)
# delete outliers
LFD <- LFD %>%
filter(Length<= 150 & Length >=10) # chose where to cut off lengts
# plot LFD
plotLFD <- ggplot(data=LFD, aes(x=Length, y=lenf)) + geom_line(size=1)
plotLFD <- plotLFD + facet_grid(Year~Country) + labs(title = "Raised number shrimp DYFS")
plotLFD
# setwd(outputpath)
# ggsave(filename = paste(ispec, "LFD_by_year.png"), plot = last_plot(), width=10,height=8, dpi=600)
#plot raised LFD
plotLFD <- ggplot(data=LFD, aes(x=Length, y=raised_lenf)) + geom_line(size=1)
plotLFD <- plotLFD + facet_grid(Year~Country) + labs(title = "Raised number shrimp DYFS")
plotLFD
# setwd(outputpath)
# ggsave(filename = paste(ispec, "raised_LFD_by_year.png"), plot = last_plot(), width=10,height=8, dpi=600)
# standardise to swept area
sweptarea <- datspec %>%
filter(HLNoAtLngt >0 ) %>%
filter(HaulVal=="V") %>% # take only valid hauls into account
select(Year, Survey, Country, StNo, HaulNo, sweptarea2) %>%
distinct() %>%
group_by(Year, Survey, Country) %>%
summarize(SA=sum(sweptarea2))
LFD.std <- LFD %>%
left_join(sweptarea) %>%
mutate(lenf.std=raised_lenf/SA)
plotLFD2 <- ggplot(data=LFD.std, aes(x=Length, y=lenf.std)) + geom_line(size=1)
plotLFD2 <- plotLFD2 + facet_grid(Year~Country)
plotLFD2
plotLFD3 <- ggplot(data=LFD.std %>% filter(Year %in% c(2012:2020)), aes(x=Length, y=lenf.std)) + geom_line(size=1, aes(color=Country))
plotLFD3 <- plotLFD3 + facet_grid(Survey~Year)
plotLFD3
# setwd(outputpath)
# ggsave(filename = paste(ispec, "std_raised_LFD_by_year.png"), plot = last_plot(), width=10,height=8, dpi=600)
##########################################
##########################################
## LWK & ALK
##########################################
##########################################
# to be continued