-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratePopulationsStockAvaiFromTrawlSurveys.R
796 lines (676 loc) · 41.1 KB
/
GeneratePopulationsStockAvaiFromTrawlSurveys.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# GENERAL SETTINGS
args <- commandArgs(trailingOnly = TRUE)
general <- list()
if (length(args) < 2) {
if(.Platform$OS.type == "windows") {
general$application <- "testexample" # ...or myfish
general$main_path_gis <- file.path("C:","Users","fbas","Documents","GitHub","DISPLACE_input_gis", general$application)
general$main.path.ibm <- file.path("C:","Users","fbas","Documents","GitHub",paste("DISPLACE_input_", general$application, sep=''))
general$igraph <- 56 # caution: should be consistent with existing objects already built upon a given graph
general$main_path_R_inputs <- file.path("C:", "Users", "fbas", "Documents", "GitHub", "DISPLACE_R_inputs")
do_plot <- TRUE
}
} else {
general$application <- args[1]
general$main_path_gis <- args[2]
general$main.path.ibm <- args[3]
general$igraph <- args[4] # caution: should be consistent with existing vessels already built upon a given graph
general$main_path_R_inputs <- args[5]
general$main_path_R_inputs <- file.path("C:", "Users", "fbas", "Documents", "GitHub", "DISPLACE_R_inputs")
do_plot <- FALSE
}
cat(paste("START \n"))
if(general$application=="testexample"){
general$casestudy <- "testexample"
species.to.keep <- c("COD", "PLE", "WHG", "TUR", "FLE", "DAB", "SOL", "SPR", "HER")
general$method <- "maximum" # for the canadian paper
general$threshold <- 50
general$method <- "inverse" # for the baltic only CS. because the grid mesh size if much finer
general$threshold <- 25
general$p <- 0.1
years <- c(2013:2015)
} else{
stop("to be defined for this app - please look at the R script and adapt for this app - anyway this is an optional step - see the doc for an explanation")
}
#------------------------
#------------------------
#------------------------
c.listquote <- function( ... ) {
args <- as.list( match.call()[ -1 ] )
lstquote <- list( as.symbol( "list" ) );
for ( i in args ) {
# Evaluate expression in parent eviron to see what it refers to
if ( class( i ) == "name" || ( class( i ) == "call" && i[[1]] != "list" ) ) {
i <- eval( substitute( i ), sys.frame( sys.parent() ) )
}
if ( class( i ) == "call" && i[[1]] == "list" ) {
lstquote <- c( lstquote, as.list( i )[ -1 ] )
}
else if ( class( i ) == "character" )
{
for ( chr in i ) {
lstquote <- c( lstquote, list( parse( text=chr )[[1]] ) )
}
}
else
stop( paste( "[", deparse( substitute( i ) ), "] Unknown class [", class( i ), "] or is not a list()", sep="" ) )
}
return( as.call( lstquote ) )
}
#------------------------
#------------------------
#------------------------
# euclidean distance (from crossdist in spatstat)
if(FALSE){
x1 <- coord[,1] # long graph
y1 <- coord[,2] # lat graph
x2 <- an(ibts_and_bits_cpue$ShootLon) # long bits
y2 <- an(ibts_and_bits_cpue$ShootLat) # lat bits
n1 <- length(x1)
n2 <- length(x2)
X1 <- matrix(rep(x1, n2), ncol = n2)
Y1 <- matrix(rep(y1, n2), ncol = n2)
X2 <- matrix(rep(x2, n1), ncol = n1)
Y2 <- matrix(rep(y2, n1), ncol = n1)
mat <- sqrt((X1 - t(X2))^2 + (Y1 - t(Y2))^2)
mat[1,] <- sqrt((X1[1,]-t(X2)[1,])^2 + (Y1[1,]-t(Y2)[1,])^2)
lst.graph.pts.with.idx.in.bits <-
lapply(1:nrow(mat), function(i,mat) {which(mat[i,]<0.3)}, mat) # return the index in X less than < 0.3 for each Y
}
#------------------------
#------------------------
#------------------------
# great circle distance
`distance` <-
function(lon,lat,lonRef,latRef){
x1 <- lon
y1 <- lat
x2 <- lonRef
y2 <- latRef
pd <- pi/180
a1<- sin(((y2-y1)*pd)/2)
a2<- cos(y1*pd)
a3<- cos(y2*pd)
a4<- sin(((x2-x1)*pd)/2)
a <- a1*a1+a2*a3*a4*a4
c <- 2*atan2(sqrt(a),sqrt(1-a));
R <- 6371;
dx1 <- R*c
return(dx1)}
#------------------------
#------------------------
#------------------------
ibts_and_bits_cpue <- read.table(file.path(general$main_path_gis, "POPULATIONS", paste("Stock_spatial_research_survey_vessel_data.csv", sep='')), header=TRUE, sep=";")
cat(paste("Loading Stock_spatial_research_survey_vessel_data.csv....done \n"))
# subset for the chosen period
ibts_and_bits_cpue <- ibts_and_bits_cpue[ibts_and_bits_cpue$Year %in% years,]
ibts_and_bits_cpue$Year <- factor(ibts_and_bits_cpue$Year)
# get species fao code + add missing or mispelling species e.g. PRA and SAN
load(file.path(general$main_path_gis, "POPULATIONS", "Stock_latin_names.RData"))
speciesLatinNames <- rbind.data.frame(speciesLatinNames,
data.frame(species_eng="Pandalus", ff_species_latin="Pandalus", fao_code="PRA"))
speciesLatinNames <- rbind.data.frame(speciesLatinNames,
data.frame(species_eng="Ammodytes marinus", ff_species_latin="Ammodytes marinus", fao_code="SAN"))
speciesLatinNames <- rbind.data.frame(speciesLatinNames,
data.frame(species_eng="TUR", ff_species_latin="Scophthalmus maximus", fao_code="TUR"))
# get species fao code + add missing or mispelling species e.g. PRA and SAN
load(file.path(general$main_path_gis, "POPULATIONS", "Stock_latin_names.RData"))
speciesLatinNames <- rbind.data.frame(speciesLatinNames,
data.frame(species_eng="Pandalus", ff_species_latin="Pandalus", fao_code="PRA"))
speciesLatinNames <- rbind.data.frame(speciesLatinNames,
data.frame(species_eng="Ammodytes marinus", ff_species_latin="Ammodytes marinus", fao_code="SAN"))
## CAUTION: CSH and PRA are all at 0s in the surveys!!
## so use the commercial cpue instead.
## caution: tricky there because replace the records of the ibts survey with the commercial (Danish) geolocalized cpue...
# load the data from VMS/logbook coupling
load(file.path(general$main_path_gis, "FISHERIES", "coupled_VMS_logbooks_DNK_2015.RData"))
spp <- c("LE_KG_PRA", "LE_KG_CSH", "LE_KG_OYF", "LE_KG_MUS", "LE_KG_NEP")
ibts_and_bits_cpue$Species <- as.character(ibts_and_bits_cpue$Species)
ibts_and_bits_cpue$Species <- as.character(ibts_and_bits_cpue$Species)
for(sp in spp){
if(sp=="LE_KG_PRA") latin <- "Pandalus"
if(sp=="LE_KG_CSH") latin <- "Crangon crangon"
if(sp=="LE_KG_MUS") latin <- "Mytilus edulis"
if(sp=="LE_KG_OYF") latin <- "Ostrea edulis"
if(sp=="LE_KG_NEP") latin <- "Nephrops norvegicus"
this.sp <- coupled_VMS_logbooks[!is.na(coupled_VMS_logbooks[,sp]) & coupled_VMS_logbooks[,sp] !=0 &
as.numeric(as.character(coupled_VMS_logbooks$SI_LONG))>-10,
c("SI_LONG","SI_LATI","SI_DATE",sp,"LE_EFF_VMS")]
this.sp$cpue <- as.numeric(as.character(this.sp[,sp]))/(as.numeric(as.character(this.sp$LE_EFF_VMS))/60)
this.sp$quarter <- quarters(as.POSIXct(as.character(this.sp$SI_DATE)))
this.sp <- this.sp[this.sp$quarter!="QNA",]
this.sp$cpue <- replace(this.sp$cpue, is.na(this.sp$cpue), 0)
# create ibts_and_bits_cpue like data format and fill in from commercial cpue
cn <- colnames(ibts_and_bits_cpue[ibts_and_bits_cpue$Species==latin,])
zz <- data.frame(matrix(0,ncol=length(cn), nrow=nrow(this.sp)))
colnames(zz) <- cn
zz$ShootLat <- this.sp$SI_LATI
zz$ShootLon <- this.sp$SI_LONG
zz$Year <- "2015"
zz$LngtClass <- 5 # assume all indiv in the fist szgroup bin
zz$CPUE_number_per_hour <- this.sp$cpue
zz$Quarter <- gsub("Q","", this.sp$quarter)
zz$Survey <- "commercial"
zz$Species <- latin
zz$Ship <- ""
zz$Gear <- ""
rand_sampling_idx <- sample(size=min(nrow(zz), 5000), x=1:nrow(zz), replace=FALSE) # bc too time demanding to keep all the points!
zz <- zz[rand_sampling_idx,]
# replace the data
if(sp=="LE_KG_PRA" || sp=="LE_KG_CSH") {
ibts_and_bits_cpue <- ibts_and_bits_cpue[!ibts_and_bits_cpue$Species==latin,]
ibts_and_bits_cpue <- rbind.data.frame(ibts_and_bits_cpue, zz[,colnames(ibts_and_bits_cpue)])
}
if(sp=="LE_KG_MUS" || sp=="LE_KG_OYF") {
ibts_and_bits_cpue <- ibts_and_bits_cpue[!ibts_and_bits_cpue$Species==latin,]
ibts_and_bits_cpue <- rbind.data.frame(ibts_and_bits_cpue, zz[,colnames(ibts_and_bits_cpue)])
}
if(sp=="LE_KG_NEP") {
# do not remove the NEP in the (north sea) scientific survey but also add the info from commercial Danish activity
ibts_and_bits_cpue <- rbind.data.frame(ibts_and_bits_cpue, zz[,colnames(ibts_and_bits_cpue)])
}
} #=> do not care about warnings here...
rm(coupled_VMS_logbooks); gc(reset=TRUE)
ibts_and_bits_cpue$Species_latin <- ibts_and_bits_cpue$Species
ibts_and_bits_cpue$Species <- speciesLatinNames$fao_code[match(ibts_and_bits_cpue$Species, speciesLatinNames$ff_species_latin)]
ibts_and_bits_cpue <- ibts_and_bits_cpue[!is.na(ibts_and_bits_cpue$Species),]
## FILES FOR BUILDING A IGRAPH
coord <- read.table(file=file.path(general$main_path_gis, "GRAPH", paste("coord", general$igraph, ".dat", sep=""))) # build from the c++ gui
cat(paste("Loading the graph....done \n"))
coord <- as.matrix(as.vector(coord))
coord <- matrix(coord, ncol=3)
colnames(coord) <- c('x', 'y', 'dist')
#plot(coord[,1], coord[,2])
coord <- cbind(coord, 1:nrow(coord)) # keep track of the idx_node
# add a semester code
ibts_and_bits_cpue$Semester <- factor(ibts_and_bits_cpue$Quarter) # init
levels(ibts_and_bits_cpue$Semester) <- c(1,1,2,2) # BTS in Q1 and Q4
#------------------------
#------------------------
#------------------------
# keep only relevant species
ibts_and_bits_cpue <- ibts_and_bits_cpue [ibts_and_bits_cpue$Species %in% species.to.keep,]
ibts_and_bits_cpue$Species <- factor(ibts_and_bits_cpue$Species)
#------------------------
#------------------------
#------------------------
# design size group (every 5 cm)
ibts_and_bits_cpue$size_group <- floor(as.numeric(as.character(ibts_and_bits_cpue$LngtClas)) / 50 )
ibts_and_bits_cpue$size_group[ibts_and_bits_cpue$size_group>13] <-13 # a plusgroup when > 70 cm if every 5cm
cat(paste("14 size groups, 5 cm bins....done \n"))
#------------------------
#------------------------
#------------------------
# fast aggregation using data.table
# to sum the nb of individuals over the new defined size_group
library(data.table)
ibts_and_bits_cpue$ShootLat <- factor(ibts_and_bits_cpue$ShootLat)
ibts_and_bits_cpue$ShootLon <- factor(ibts_and_bits_cpue$ShootLon)
ibts_and_bits_cpue$size_group <- factor(ibts_and_bits_cpue$size_group)
ibts_and_bits_cpue$id <- factor(paste(ibts_and_bits_cpue$Year,".",ibts_and_bits_cpue$Quarter,".",
ibts_and_bits_cpue$HaulNo,".",ibts_and_bits_cpue$Species,".",ibts_and_bits_cpue$ShootLon,".",ibts_and_bits_cpue$ShootLat, sep=''))
DT <- data.table(ibts_and_bits_cpue) # library data.table for fast grouping replacing aggregate()
eq1 <- c.listquote(paste("sum(","CPUE_number_per_hour",",na.rm=TRUE)",sep=""))
agg.ibts_and_bits_cpue <- DT[,eval(eq1),by=list(Survey,Year,Semester,Quarter,id, ShootLon,ShootLat, Species,size_group)]
agg.ibts_and_bits_cpue <- data.frame(agg.ibts_and_bits_cpue)
colnames(agg.ibts_and_bits_cpue) <- c("Survey", "Year", "Semester", "Quarter", "id", "ShootLon", "ShootLat", "Species", "size_group", "nb_indiv")
#------------------------
#------------------------
#------------------------
#reshape to the wide format
# tricky because reshape() is painful for large dataset
# so do it chunk by chunk according to species.
library(doBy)
agg.ibts_and_bits_cpue <- orderBy(~size_group, data=agg.ibts_and_bits_cpue)
ibts_and_bits_cpue.lst <- NULL # chunk by chunk
levels(agg.ibts_and_bits_cpue$id) <- 1:length(levels(agg.ibts_and_bits_cpue$id)) # make it easier...
for(sp in unique(ibts_and_bits_cpue$Species)){
cat(paste(sp, "\n"))
ibts_and_bits_cpue.lst[[sp]] <- reshape(agg.ibts_and_bits_cpue[agg.ibts_and_bits_cpue$Species==sp,], timevar="size_group",
idvar="id", direction="wide", v.names="nb_indiv")
# make dim compatible for do.call("rbind")
nbcol.to.add <- 14 - length(grep("nb_", colnames(ibts_and_bits_cpue.lst[[sp]]))) # knowing that we need 14 szgroup bins
if(nbcol.to.add>0){
tmp <- matrix(0,ncol=nbcol.to.add, nrow=nrow(ibts_and_bits_cpue.lst[[sp]]))
ibts_and_bits_cpue.lst[[sp]] <- cbind(ibts_and_bits_cpue.lst[[sp]], tmp)
colnames(ibts_and_bits_cpue.lst[[sp]]) <-
c('Survey', 'Year', 'Semester', 'Quarter', 'id', 'ShootLon', 'ShootLat', 'Species',
'nb_indiv.0', 'nb_indiv.1', 'nb_indiv.2', 'nb_indiv.3',
'nb_indiv.4', 'nb_indiv.5', 'nb_indiv.6', 'nb_indiv.7',
'nb_indiv.8', 'nb_indiv.9', 'nb_indiv.10', 'nb_indiv.11','nb_indiv.12', 'nb_indiv.13' )
}
}
ibts_and_bits_cpue <- do.call("rbind", ibts_and_bits_cpue.lst)
ibts_and_bits_cpue <- replace(ibts_and_bits_cpue, is.na(ibts_and_bits_cpue) | ibts_and_bits_cpue=="Inf", 0)
# check
head(ibts_and_bits_cpue[ibts_and_bits_cpue$Species==sp,])
# save
dir.create(path=file.path(general$main_path_gis, "POPULATIONS", "avai"))
save(ibts_and_bits_cpue,
file=file.path(general$main_path_gis, "POPULATIONS", "avai", paste("cpue_graph", general$igraph,".RData",sep='')))
cat(paste("Save survey files in /POPULATIONS/avai folder....done \n"))
#------------------------
#------------------------
#------------------------
if(FALSE){
# obtain format for the displace merger:
#dd <- reshape(bits.cpue, varying = list(9:22), v.names="nb_indiv",
# direction="long") # we actually don´t need to reshape because displace require a wide format
#(with headers: "Year" "Semester" "ShootLat" "ShootLon" "Stock" and pattern "nb_indiv."
spp_table <- read.table(file=file.path(general$main_path_gis, "POPULATIONS",
paste("pop_names_", general$application,".txt",sep='')), header=TRUE)
spp <- as.character(spp_table$spp)
#=> required species for this app
load("C:\\Users\\fbas\\Documents\\GitHub\\DISPLACE_input_gis_myfish\\POPULATIONS\\avai\\cpue_graph56.RData") # for example...
obj <- ibts_and_bits_cpue # e.g. after load C:\Users\fbas\Documents\GitHub\DISPLACE_input_gis_DanishFleet\POPULATIONS\avai\cpue_graph40.RData
obj$Stock <- as.character(obj$Species) # init
obj$x <- as.numeric(as.character(obj$ShootLon)) # init
obj$y <- as.numeric(as.character(obj$ShootLat)) # init
source(file=file.path(general$main_path_R_inputs, "old", "vmstools_longlat_to_ICESareas.r"))
obj$ICESareas <- longlat_to_ICESareas(obj)
# convert Species in Stock name
idx <- obj$Species %in% c("SPR", "PLE", "FLE", "TUR", "DAB") & obj$ICESareas %in% c("IVa", "IVb", "IVc")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'nsea', sep=".")
idx <- obj$Species %in% c("SPR", "PLE", "FLE", "TUR", "DAB") & obj$ICESareas %in% c("IIIan","IIIas")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'kask', sep=".")
idx <- obj$Species %in% c("SPR", "PLE", "FLE", "TUR", "DAB") & obj$ICESareas %in% c("IIIan","IIIas")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'kask', sep=".")
idx <- obj$Species %in% c("SPR", "PLE", "FLE", "TUR", "DAB") & obj$ICESareas %in% c("22", "23", "24", "25", "26", "27","28-1","28-2","29","30","31","32")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2232', sep=".")
idx <- obj$Species %in% c("COD", "HAD") & obj$ICESareas %in% c("IVa", "IVb", "IVc","IIIan")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'nsea', sep=".")
idx <- obj$Species %in% c("COD", "HAD") & obj$ICESareas %in% c("IIIas")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'kat', sep=".")
idx <- obj$Species %in% c("COD", "HAD") & obj$ICESareas %in% c("22", "23", "24")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2224', sep=".")
idx <- obj$Species %in% c("COD", "HAD") & obj$ICESareas %in% c("25", "26", "27","28-1","28-2","29","30","31","32")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2532', sep=".")
idx <- obj$Species %in% c("HER") & obj$ICESareas %in% c("IVa", "IVb", "IVc")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'nsea', sep=".")
idx <- obj$Species %in% c("HER") & obj$ICESareas %in% c("IIIan")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '3a22', sep=".")
idx <- obj$Species %in% c("HER") & obj$ICESareas %in% c("22", "23", "24")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '3a22', sep=".")
idx <- obj$Species %in% c("HER") & obj$ICESareas %in% c("25", "26", "27","28-1","28-2","29","30","31","32")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2532', sep=".")
all_other_species <- !obj$Species %in% c("SPR", "PLE", "FLE", "TUR", "DAB", "COD", "HAD", "HER")
idx <- all_other_species & obj$ICESareas %in% c("IVa", "IVb", "IVc")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'nsea', sep=".")
idx <- all_other_species & obj$ICESareas %in% c("IIIan","IIIas")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], 'kask', sep=".")
idx <- all_other_species & obj$ICESareas %in% c("22", "23", "24")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2224', sep=".")
idx <- all_other_species & obj$ICESareas %in% c("25", "26", "27","28-1","28-2","29","30","31","32")
obj[idx, "Stock" ] <- paste(obj[idx,"Species"], '2532', sep=".")
obj$StockId <- factor(obj$Stock) # init
levels(obj$StockId) <- spp_table[match(levels(obj$StockId), as.character(spp_table[,2])), 1]
obj <- obj[!is.na(obj$StockId),] # get rid of stocks not in this DISPLACE app
obj$StockId <- factor(obj$StockId)
write.table(obj, file= file.path(general$main_path_gis, "POPULATIONS",
"avai", "input_file_for_displace_merger.csv"), sep=";", row.names=FALSE, quote=FALSE) # for DISPLACE popdistributionmerger
write.table(obj, file= file.path(general$main.path.ibm, paste("popsspe_", general$casestudy, sep=''),
"static_avai", "input_file_for_displace_merger.csv"), sep=";", row.names=FALSE, quote=FALSE) # for input2AvaiUpdater.R
}
#------------------------
#------------------------
#------------------------
get_cpue_on_graph_nodes <- function (obj=ibts_and_bits_cpue, coord=coord, sp=sp, S=S, survey="bits", general=general){
nb_size_group <- 13 # caution: magic number
# subset species and semester
obj <- obj[obj$Species==sp,]
obj <- obj[obj$Semester==S,]
cat(paste("sp ", sp, "\n"))
cat(paste("S ", S, "\n"))
if(nrow(obj)!=0){
# remove NA
obj <- replace(obj, is.na(obj), 0)
#
an <- function(x) as.numeric(as.character(x))
obj$ShootLon <- an(obj$ShootLon)
obj$ShootLat <- an(obj$ShootLat)
# a comment
a.comment <- paste("graph", general$igraph, "_", sp,"_","S",S,"_",general$method,"_", survey, sep='')
# TAKES HUGE TIME: compute the distance from each node of the graph to the nearest survey points
lst.graph.pts.with.idx.in.obj <- vector("list", length=nrow(coord))
lst.graph.pts.with.idx.in.obj <-
lapply(1:length(lst.graph.pts.with.idx.in.obj), function(i, obj, coord){
dd <- distance(an(obj$ShootLon), an(obj$ShootLat), coord[i,1],coord[i,2])
names(dd) <- 1:length(obj$ShootLon)
dd[dd < general$threshold] ##!!!! THRESHOLD IN km !!!!##
}, obj, coord)
# check the links between one randomly chosen node and its neighbours by plotting
if(FALSE){
an <- function(x) as.numeric(as.character(x))
plot(x=an(obj$ShootLon), y=an(obj$ShootLat), col=grey(0.7), pch=16)
segments(an(coord[,1])[760], an(coord[,2])[760],
an(obj$ShootLon)[ an(names(lst.graph.pts.with.idx.in.obj[[760]])) ],
an(obj$ShootLat)[ an(names(lst.graph.pts.with.idx.in.obj[[760]])) ], col=3)
points(x=coord[,"x"], y=coord[,"y"], col="black") # graph g
points(x=coord[760,"x"], y=coord[760,"y"], col="red") # a pt of the graph g
segments(an(coord[,1])[746], an(coord[,2])[746],
an(obj$ShootLon)[ an(names(lst.graph.pts.with.idx.in.obj[[746]])) ],
an(obj$ShootLat)[ an(names(lst.graph.pts.with.idx.in.obj[[746]])) ], col=3)
points(x=coord[746,"x"], y=coord[746,"y"], col="red") # a pt of the graph g
}
# inverse distance weighted average
nm <- paste(rep(sp, each=nb_size_group),".nb_indiv.", 0:nb_size_group,sep='')
tmp <- matrix(0, ncol=length(nm), nrow=nrow(coord))
colnames(tmp) <- nm
coord <- cbind(coord, tmp)
for(szgroup in paste("nb_indiv.",0:nb_size_group,sep='')){
if(do_plot) plot(x=an(obj$ShootLon), y=an(obj$ShootLat), col=grey(0.7), pch=16)
cat(paste("szgroup", szgroup, "\n"))
for(node in 1:nrow(coord)){
#cat(paste("node", node, "\n"))
idx.pts.obj <- an(names(lst.graph.pts.with.idx.in.obj[[node]]))
idx.pts.obj <- idx.pts.obj [ !is.na( an(obj[obj$Species==sp, szgroup][ idx.pts.obj ]) ) ] # remove distance with NA for catch (check why NA here)
dist.pts.obj <- lst.graph.pts.with.idx.in.obj[[node]] [as.character(idx.pts.obj)]
if(length(dist.pts.obj)>0){
coord[node, paste(sp,".",szgroup,sep='')] <-
switch(general$method,
inverse= sum(an(obj[obj$Species==sp, szgroup][ idx.pts.obj ]) * ( (1/(dist.pts.obj^general$p)) / sum(1/(dist.pts.obj^general$p)) )), #weighted mean
maximum= max(an(obj[obj$Species==sp, szgroup][ idx.pts.obj ])) # keep only the neighbour with max
)
} else{coord[node, paste(sp,".",szgroup,sep='')] <-0}
# check by plotting
if(FALSE && szgroup=="nb_indiv.0")
if(length(lst.graph.pts.with.idx.in.obj[[node]])!=0 && do_plot){
segments(an(coord[,1])[node], an(coord[,2])[node],
an(obj$ShootLon)[ an(names(lst.graph.pts.with.idx.in.obj[[node]])) ],
an(obj$ShootLat)[ an(names(lst.graph.pts.with.idx.in.obj[[node]])) ], col=3)
points(x=coord[node,"x"], y=coord[node,"y"], col="red") # a pt of the graph g
savePlot(filename = file.path(general$main_path_gis, "POPULATIONS", "avai",
paste(a.comment,"link_nodes_with_surveys_",general$threshold,".jpeg",sep="")),type ="jpeg")
}
} # end node
# check
if(do_plot){
plot(x= obj[obj$Species==sp, "ShootLon"], y=obj[obj$Species==sp, "ShootLat"], col=2, pch=1,
cex=obj[obj$Species==sp,szgroup] /max(coord[,paste(sp,".",szgroup,sep='')], obj[obj$Species==sp,szgroup])*4)
#points(x= coord[,1], y=coord[,2], col=1, pch=16,
# cex=coord[,paste(sp,".",szgroup,sep='')] /max(coord[,paste(sp,".",szgroup,sep='')], obj[obj$Species==sp,szgroup])*4)
points(x= coord[,1], y=coord[,2], col=1, pch=16,
cex=coord[,paste(sp,".",szgroup,sep='')]/ max(coord[,paste(sp,".",szgroup,sep='')])*4)
points(x= obj[obj$Species==sp, "ShootLon"], y=obj[obj$Species==sp, "ShootLat"], col=2, pch=1, # overlay...
cex=obj[obj$Species==sp,szgroup] /max(coord[,paste(sp,".",szgroup,sep='')], obj[obj$Species==sp,szgroup])*4)
map(add=TRUE, xlim=c(-10,25),ylim=c(50,65))
title(szgroup)
savePlot(filename = file.path(general$main_path_gis, "POPULATIONS", "avai",
paste(a.comment,"_",szgroup,".jpeg",sep="")),type ="jpeg")
}
} # end szgroup
coord <- replace(coord, is.infinite(coord), 0)
} else{
print("no observation there....fill in with 0s")
nm <- paste(rep(sp, each=nb_size_group),".nb_indiv.", 0:nb_size_group,sep='')
tmp <- matrix(0, ncol=length(nm), nrow=nrow(coord))
colnames(tmp) <- nm
coord <- cbind(coord, tmp)
for(szgroup in paste("nb_indiv.",0:nb_size_group,sep='')){
for(node in 1:nrow(coord)){
coord[node, paste(sp,".",szgroup,sep='')] <- 0
}
}
}
return(coord)
}
#--------------------
#--------------------
#--------------------
#--------------------
set.avai <- function(lst.avai, sp, S, areas){
obj <- get(paste("coord.",sp,".",S, sep=''), env=.GlobalEnv) # get in .GlobalEnv
source(file=file.path(general$main_path_R_inputs, "old", "vmstools_longlat_to_ICESareas.r"))
ICESareas <- longlat_to_ICESareas(obj)
idx.areas <- which(ICESareas %in% areas)
obj.in.areas <- obj[idx.areas,]
obj.in.areas <- replace(obj.in.areas, is.infinite(obj.in.areas), NA)
if(all(apply(obj.in.areas[,c(5:18)], 2, sum, na.rm=TRUE)==0))
{
cat(paste("CAUTION: no avai for this stock ",sp," in this area!!\n"))
obj.in.areas[,c(5:18)] <- 0.0000001 # assume even distribution!
}
obj.in.areas[,c(5:18)] <-
sweep(obj.in.areas[,c(5:18)], 2,
apply(obj.in.areas[,c(5:18)], 2, sum, na.rm=TRUE), FUN="/") # CAUTION: Assuming equal grid resolution
# Note that if the grid is made out of different grid resolution then this ratio will need to be adjusted e.g.
# by considering the Gauss circle problem. Because the nodes of the grid with lower resolution
# should represent a higher proportion of the stock then a node of the grid with higher resolution
# we need to compute how many nodes of the finest grid equals 1 node of the lower grid resolution.
# the Gauss circle approximation say 317 nodes for a circle of radius 10 time greater.
# i.e. if res1 spaced by 0.6km and res2 spaced by 6km then the circle centered on a node of the res2 (radius 3km)
# will include 317 nodes of res1 i.e. ca. pi*r^2.
#If the answer for a given r is denoted by N(r) then the following list shows the first few values of N(r)
#for r an integer between 0 and 12 followed by the list of values pi r^{2} rounded to the nearest integer:
#1, 5, 13, 29, 49, 81, 113, 149, 197, 253, 317, 377, 441
name.areas <- 'undefined'
if(sp %in% c("WHG", "SPR", "TUR", "DAB")){ # special case: west and east baltic merged
if(all(c("IVa", "IVb", "IVc") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIan","IIIas") %in% areas)) name.areas <- 'kask'
if(all(c("22", "23", "24") %in% areas)) name.areas <- '2232'
if(all(c("25", "26", "27","28-1","28-2","29","30","31","32") %in% areas)) name.areas <- '2232'
}else{
if(sp %in% c("FLE")){
if(all(c("IVa", "IVb", "IVc") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIan","IIIas") %in% areas)) name.areas <- 'kask'
if(all(c("22", "23") %in% areas)) name.areas <- '2223'
if(all(c("24","25") %in% areas)) name.areas <- '2425'
if(all(c("26", "28-1", "28-2") %in% areas)) name.areas <- '2628'
if(all(c("27", "29","30","31","32") %in% areas)) name.areas <- '2732'
} else{
if(sp %in% c("PLE")){
if(all(c("IVa", "IVb", "IVc", "IIIan") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIas", "22", "23") %in% areas)) name.areas <- '2123'
if(all(c("24","25","26", "28-1", "28-2", "29","30","31","32") %in% areas)) name.areas <- '2432'
} else{
if(sp %in% c("SOL")){
if(all(c("IVa", "IVb", "IVc") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIas", "22", "23") %in% areas)) name.areas <- '3a2223'
} else{
if(sp %in% c("COD", "HAD")){ # special case: skagerat with nsea, kat alone
if(all(c("IVa", "IVb", "IVc","IIIan") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIas") %in% areas)) name.areas <- 'kat'
if(all(c("22", "23", "24") %in% areas)) name.areas <- '2224'
if(all(c("25", "26", "27","28-1","28-2","29","30","31","32") %in% areas)) name.areas <- '2532'
}else{
if(sp %in% c("HER")){ # special case: 3a with western baltic
if(all(c("IVa", "IVb", "IVc") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIan") %in% areas)) name.areas <- '3a22'
if(all(c("22", "23", "24") %in% areas)) name.areas <- '3a22'
if(all(c("25", "26", "27","28-1","28-2","29","30","31","32") %in% areas)) name.areas <- '2532'
} else{ # default
if(all(c("IVa", "IVb", "IVc") %in% areas)) name.areas <- 'nsea'
if(all(c("IIIan","IIIas") %in% areas)) name.areas <- 'kask'
if(all(c("22", "23", "24") %in% areas)) name.areas <- '2224'
if(all(c("25", "26", "27","28-1","28-2","29","30","31","32") %in% areas)) name.areas <- '2532'
}
}
}
}
}
}
nm <- paste(sp,paste(name.areas ,collapse='.'),sep='.')
lst.avai[[ nm ]][[S]] <- obj.in.areas
# availability key: each col should sum to 1 i.e. for each age
return(lst.avai)
}
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!MAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
if(do_plot){
library(maps)
library(maptools)
library(mapdata)
map("worldHires", add=FALSE, col="green", fill=TRUE, bg="white", xlim=c(-10,20) , ylim=c(49,63) ,
regions=c('uk','ireland','france','germany','netherlands', 'norway','belgium',
'spain','luxembourg','denmark', 'sweden','iceland', 'portugal','italy','sicily','ussr','sardinia','albania',
'monaco','turkey','austria',
'switzerland','czechoslovakia','finland','libya', 'hungary','yugoslavia',
'poland','greece','romania','bulgaria', 'slovakia','morocco',
'tunisia','algeria','egypt' ))
library(maptools)
ices_areas <- readShapeSpatial(file.path(general$inPathManagement, "ices_areas", "ices_areas"))
plot(ices_areas, col="grey", add=TRUE)
}
##-----------------##
load(file.path(general$main_path_gis, "POPULATIONS", "avai", paste("cpue_graph", general$igraph, ".RData",sep='')))
cat(paste("Load survey files in /POPULATIONS/avai folder....done \n"))
# load a graph
coord <- read.table(file=file.path(general$main_path_gis, "GRAPH", paste("coord", general$igraph, ".dat", sep=""))) # build from the c++ gui
coord <- as.matrix(as.vector(coord))
coord <- matrix(coord, ncol=3)
colnames(coord) <- c('x', 'y', 'dist')
#plot(coord[,1], coord[,2])
coord <- cbind(coord, 1:nrow(coord)) # keep track of the idx_node
an <<- function(x) as.numeric(as.character(x))
# calls
# => assign the returned objects in .GlobalEnv
# testexample
spp <- unique(ibts_and_bits_cpue$Species)
for (sp in as.character(spp)){ # per species
for (S in 1:2){ # per semester
if(sp %in% unique(ibts_and_bits_cpue$Species)){
cat(paste("This will take some time to look at sp ", sp, "...\n"))
assign(paste("bits.coord.",sp,".",S,sep=''),
get_cpue_on_graph_nodes (obj=ibts_and_bits_cpue, coord=coord, sp=sp, S=S, survey="ibts_and_bits", general=general), envir =.GlobalEnv )}
assign(paste("coord.",sp,".",S,sep=''), get(paste("bits.coord.",sp,".",S,sep='')), envir =.GlobalEnv )
} # end S
} # end sp
# check with plot
if(do_plot){
obj <- coord.SOL.1
szgroup <-"nb_indiv.2"
plot(x= obj[,1], y=obj[,2], col=1, pch=16,
cex=obj[,paste("SOL",".",szgroup,sep='')] /max(obj[,paste("SOL",".",szgroup,sep='')],na.rm=TRUE)*4)
library(maps)
map(add=TRUE)
}
# => then, a spatial allocation key (i.e. the availability coeff) can be easily obtained from there...
# CAUTION: per stock i.e. per region i.e. nscod, wcod, ecod...
## potential pitfall: the spatial coverage of the surveys is not perfect (even if 5 years is taken)
## and then potential nodes with 0 avai in some place (blank areas) while not relevant...
# calls
lst.avai <- list() # init
for (sp in as.character(spp)){ # per species
if(sp %in% c("WHG", "SPR", "TUR", "DAB")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIan","IIIas") ) # IIIa
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIan","IIIas") ) # IIIa)
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("22", "23", "24","25", "26", "27","28-1","28-2","29","30","31","32") ) # 2232
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("22", "23", "24","25", "26", "27","28-1","28-2","29","30","31","32") ) # 2232
}else{
if(sp %in% c("PLE")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc", "IIIan") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc", "IIIan") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIas", "22", "23")) # 2123
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIas", "22", "23")) # 2123
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("24","25","26", "28-1", "28-2", "29","30","31","32") ) # 2432
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("24","25","26", "28-1", "28-2", "29","30","31","32") ) # 2432
} else{
if(sp %in% c("FLE")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIan","IIIas")) # kask
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIan","IIIas")) # kask
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("22", "23") ) # 2223
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("22", "23") ) # 2223
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("24","25") ) # 2425
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("24","25") ) # 2425
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("26", "28-1", "28-2") ) # 2628
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("26", "28-1", "28-2") ) # 2628
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("27", "29","30","31","32") ) # 2732
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("27", "29","30","31","32") ) # 2732
} else{
if(sp %in% c("SOL")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIan","IIIas", "22", "23")) # IIIa2223
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIan","IIIas", "22", "23")) # IIIa2223
} else{
if(sp %in% c("COD", "HAD")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc", "IIIan") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc", "IIIan") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIas") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIas") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
} else{
if(sp %in% c("HER")){
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIan","IIIas","22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIan","IIIas","22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
} else{
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IVa", "IVb", "IVc") ) # NS
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("IIIan","IIIas") ) # IIIa
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("IIIan","IIIas") ) # IIIa)
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("22", "23", "24") ) # BW
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="1", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
lst.avai <- set.avai(lst.avai=lst.avai, sp=sp, S="2", areas= c("25", "26", "27","28-1","28-2","29","30","31","32") ) # BE
}
}
}
}
}
}
} #=> NOTE THAT AVAI KEY IS RESTRICTED TO THE PROVIDED AREAS GIVEN THE SURVEY COVERAGE
# EVEN IF VESSELS USE TO GO FARTHER THAN THAT...
# SO VESSELS GOING FAR WILL LIKELY FISH ON AREAS WITHOUT ANY FISH BECAUSE AVAI WILL BE AT 0...
# THIS IS WHY EXPLICIT POP BECOME IMPLICIT POP IF OUT OF RANGE (SEE THE C++ IBM CODE)
#=> NOTE THAT POSSIBLE BIAS FROM VARIOUS NODE GRID RESOLUTION
# WHILE SOME STOCKS OVERALPPING SEVERAL REGIONS e.g. COD.NSEA including skagerrat
# SO THE RULE OF THUMB IS TO DEFINE THE SAME GRID RESOLUTION FOR THE AREA OF A GIVEN STUDIED STOCK....
#=> check: lapply(lst.avai,function(x) lapply(x, head)) and apply(lst.avai[[1]][[1]],2,sum,na.rm=TRUE) =>1,1,1,etc.
# check with plot
if(do_plot){
obj <- lst.avai$COD.2224$"1"
szgroup <-"nb_indiv.4"
plot(x= obj[,1], y=obj[,2], col=1, pch=16,
cex=obj[,paste("COD",".",szgroup,sep='')] /max(obj[,paste("COD",".",szgroup,sep='')],na.rm=TRUE)*4)
}
# check with plot
if(do_plot){
obj <- lst.avai$SOL.3a2223$"1"
szgroup <-"nb_indiv.5"
plot(x= obj[,1], y=obj[,2], col=1, pch=16,
cex=obj[,paste("SOL",".",szgroup,sep='')] /max(obj[,paste("SOL",".",szgroup,sep='')],na.rm=TRUE)*4)
}
# check with plot
if(do_plot){
obj <- lst.avai$PLE.nsea$"1"
szgroup <-"nb_indiv.5"
plot(x= obj[,1], y=obj[,2], col=1, pch=16,
cex=obj[,paste("PLE",".",szgroup,sep='')] /max(obj[,paste("PLE",".",szgroup,sep='')],na.rm=TRUE)*4)
}
# check with plot
if(do_plot){
obj <- lst.avai$TUR.2232$"1"
szgroup <-"nb_indiv.5"
plot(x= obj[,1], y=obj[,2], col=1, pch=16,
cex=obj[,paste("TUR",".",szgroup,sep='')] /max(obj[,paste("TUR",".",szgroup,sep='')],na.rm=TRUE)*4)
}
# save to R
save("lst.avai", file = file.path(general$main_path_gis, "POPULATIONS", "avai",
paste("lst_avai_igraph", general$igraph,"_", general$method, "_", general$threshold, ".RData",sep="")) )
cat(paste("Save lst.avai file in /POPULATIONS/avai folder....done \n"))
## CAUTION the ibts and bits surveys are combinations of hauls from different scientifc vessels
## with potentially gear trawl with different selectivity ogives....
### TO DO: NEED PELAGIC SURVEYS (ACOUSTIC?) TO COVER THE SPRAT AND HERRING
### THEN JUST INFORM THIS PIECE OF CODE WITH DATA HAVING THE SAME FORMAT LIKE THE IBTS ONE...
cat(paste("....done \n"))