-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_CouplingAER2FDIEffortLandingsRect.r
2149 lines (1654 loc) · 123 KB
/
03_CouplingAER2FDIEffortLandingsRect.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# MERGE STECF AER WITH STECF FDI Csquare (i.e. ICES Rectangle)
# AND DISPACH AER LANDINGS, EFFORT AND COSTS
# ON C-SQUARE (ICES RECTANGLE) CELLS PER YEAR, COUNTRY AND METIER LEVEL6
# Author: Francois Bastardie (May 2023)
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
setwd(file.path("D:","FBA", "FishSpatOverlayTool"))
RinputPath <- file.path(getwd(), "INPUT_DATASETS")
ROutputPathToDatasets <- file.path(getwd(), "OUTCOME_DATASETS")
RoutputPath4LargeFiles <- file.path("E:", "SeaWise")
library(data.table)
library(plyr)
library(dplyr)
library(vmstools)
#-----------------------------------
#-----------------------------------
#-----------------------------------
# READ STECF AER DATA
#filename <- file.path(RinputPath, "STECF_DATA", "STECF 22-06 AER 2022 - data",
# "2022_STECF_22-06_EU_Fleet_Economic_and_Transversal data_fleet segment_landings_2017_2021.csv")
filename <- file.path(RinputPath, "STECF_DATA", "2023_AER_data",
"2018_2021_landings_per_fs.csv")
stecf_fleetdata <- read.csv(file=filename, sep = ";", dec = ",")
stecf_fleetdata$gear_type <- NA
stecf_fleetdata <- stecf_fleetdata[,c("country_code","year","supra_reg","fishing_tech","vessel_length","cluster_name","fs_name","variable_group","variable_name","variable_code",
"value","species_name","species_code",
"sub_reg","gear_type")]
str(stecf_fleetdata)
stecf_fleetdata$year <- as.character(stecf_fleetdata$year)
stecf_fleetdata$year <- as.factor(stecf_fleetdata$year)
# setting -1 values to 0
stecf_fleetdata$value[stecf_fleetdata$value < 0] <- 0
# filtering for NAO, calculating value of landings ####
stecf_fleetdata_value <- filter(stecf_fleetdata, variable_name=="Value of landings")
stecf_fleetdata_value <- stecf_fleetdata_value[, c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name","value","species_name","species_code","sub_reg","gear_type")]
# filtering for NAO, calculating kilo of landings ####
stecf_fleetdata_kilo <- filter(stecf_fleetdata, variable_name=="Live weight of landings")
colnames(stecf_fleetdata_kilo)[names(stecf_fleetdata_kilo)=="value"] <-"weight"
stecf_fleetdata_kilo <- stecf_fleetdata_kilo[,c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name","weight","species_name","species_code","sub_reg","gear_type")]
# joining AER value and weight data
stecf_fleetdata_with_kilo <- left_join(stecf_fleetdata_value, stecf_fleetdata_kilo, by=c("year","supra_reg","fishing_tech","vessel_length","fs_name","sub_reg","gear_type","country_code","species_name","species_code"))
# removing rows containing weight NAs
stecf_fleetdata_with_kilo <- stecf_fleetdata_with_kilo[!is.na(stecf_fleetdata_with_kilo$weight),]
# Refine dataframe to all variables necessary
stecf_fleetdata_with_kilo <- stecf_fleetdata_with_kilo[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","sub_reg","gear_type","species_code","value","weight")]
#-----------------------------------
#-----------------------------------
#-----------------------------------
# AGGREGATE STECF AER to REMOVE THE AER SPECIES INFO
stecf_fleetdata_with_kilo <- data.table(stecf_fleetdata_with_kilo)
# filter out non-deep sea species for adding an extra effort column
deepseaspp <- c("CFB","CYO","CYP","SCK","ETR","APQ","HXC","DCA","SHO","GAM","SBL","ETX","OXN","SYR","GSK",
"PZC","ALC","PHO","BSF","ARU","ALF","KEF","CMO","CYH","RCT","RNG","EPI","BRF","ORY","RHG",
"BLI","RIB","ANT","SBR","WRF","GHL","TVY","HPR","RTX","NEN","NNN","RIW","RJG","JAD","TJX","SFS","LXK","SFV") # with Annex I of Reg https://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:32016R2336?
stecf_fleetdata_with_kilo$value_deepsea <- stecf_fleetdata_with_kilo$value # init
stecf_fleetdata_with_kilo$weight_deepsea <- stecf_fleetdata_with_kilo$weight # init
stecf_fleetdata_with_kilo[!stecf_fleetdata_with_kilo$species_code %in% deepseaspp, "weight_deepsea"] <- 0
stecf_fleetdata_with_kilo[!stecf_fleetdata_with_kilo$species_code %in% deepseaspp, "value_deepsea"] <- 0
stecf_fleetdata_with_kilo <- stecf_fleetdata_with_kilo[, .(
value = sum(an(value), na.rm=T),
weight = sum(an(weight), na.rm=T),
value_deepsea = sum(an(value_deepsea), na.rm=T),
weight_deepsea = sum(an(weight_deepsea), na.rm=T)),
by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name","sub_reg","gear_type")]
##-------
##-------
#### ### Reading and exploring STECF effort Data ####
#filename <- file.path(RinputPath, "STECF_DATA", "STECF 22-06 AER 2022 - data",
# "2022_STECF_22-06_EU_Fleet_Economic_and_Transversal data_fleet segment_effort.csv")
filename <- file.path(RinputPath, "STECF_DATA", "2023_AER_data",
"2018_2021_effort_per_fs.csv")
stecf_fleetdata_effort <- read.csv(file=file.path(filename), sep = ";", dec = ",")
stecf_fleetdata_effort_aerdaysatsea <- filter(stecf_fleetdata_effort, variable_name=="Days at sea")
stecf_fleetdata_effort_aerfishingdays <- filter(stecf_fleetdata_effort, variable_name=="Fishing days")
stecf_fleetdata_effort_aerkwFishingdays <- filter(stecf_fleetdata_effort, variable_name=="kW fishing days")
stecf_fleetdata_effort_daysatsea <- stecf_fleetdata_effort_aerdaysatsea[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value","sub_reg")]
stecf_fleetdata_effort_fishingdays <- stecf_fleetdata_effort_aerfishingdays[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value","sub_reg")]
stecf_fleetdata_effort_kwFishingdays <- stecf_fleetdata_effort_aerkwFishingdays[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value","sub_reg")]
names(stecf_fleetdata_effort_daysatsea)[7] <- "aerdaysatsea"
names(stecf_fleetdata_effort_fishingdays)[7] <- "aerfishingdays"
names(stecf_fleetdata_effort_kwFishingdays)[7] <- "aerkwFishingdays"
# Joining...
stecf_fleetdata_effort_kwFishingdays <- left_join(stecf_fleetdata_effort_kwFishingdays, stecf_fleetdata_effort_fishingdays,
by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name","sub_reg"))
stecf_fleetdata_effort_kwFishingdays <- left_join(stecf_fleetdata_effort_kwFishingdays, stecf_fleetdata_effort_daysatsea,
by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name","sub_reg"))
table(is.na(stecf_fleetdata_effort_kwFishingdays$kwFishingdays))
stecf_fleetdata_effort_kwFishingdays$year <- as.factor(stecf_fleetdata_effort_kwFishingdays$year)
# joining dataframe with value and weight with the effort-dataframe
stecf_fleetdata_with_kiloandeffort <- left_join(stecf_fleetdata_with_kilo, stecf_fleetdata_effort_kwFishingdays,
by=c("country_code","year","fishing_tech","vessel_length","sub_reg", "fs_name", "supra_reg"))
stecf_fleetdata_with_kiloandeffort$aerkwFishingdays <- an(stecf_fleetdata_with_kiloandeffort$aerkwFishingdays)
stecf_fleetdata_with_kiloandeffort$aerfishingdays <- an(stecf_fleetdata_with_kiloandeffort$aerfishingdays)
stecf_fleetdata_with_kiloandeffort$aerdaysatsea <- an(stecf_fleetdata_with_kiloandeffort$aerdaysatsea)
# check
dd <- stecf_fleetdata_with_kiloandeffort
ddd <- dd[dd$country_code=="BEL" & dd$fishing_tech=="TBB" & dd$vessel_length=="VL2440" & dd$year=="2019",]
sum(ddd$aerkwFishingdays)
#-----------------------------------
#-----------------------------------
# AGGREGATE STECF AER to REMOVE THE AER FS_NAME DIMENSION
#caution: some combination of coutry-fishtech-vesselsize has several fs_name....
# input
dat <- stecf_fleetdata_with_kiloandeffort
# check
dat[dat$year=="2020" & dat$country_code=="ESP" & dat$fishing_tech=="HOK" & dat$vessel_length=="VL2440" ,]
# so aggregate
dat <- as.data.frame(dat)
dat <- dat[!duplicated(dat [,c("country_code", "year", "fishing_tech", "vessel_length", "fs_name", "sub_reg")]),]
dat <- data.table(dat)
dat <- dat[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("value", "weight", "aerkwFishingdays", "aerkwFishingdaysDeepSea", "aerfishingdays", "aerdaysatsea"),
by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length", "sub_reg")
]
#caution: some combination of coutry-fishtech-vesselsize has several fs_name....
dat[dat$year=="2020" & dat$country_code=="ESP" & dat$fishing_tech=="HOK" & dat$vessel_length=="VL2440" ,]
# output
agg_stecf_fleetdata_with_kiloeffort_nofs <- dat
#-----------------------------------
#-----------------------------------
#-----------------------------------
##-------
####### formatting a AER costratios ####
filename <- file.path(RinputPath, "STECF_DATA", "STECF 22-06 AER 2022 - data",
"2022_STECF_22-06_EU_Fleet_Economic_and_Transversal data_fleet segment_economic_variables.csv")
AER2022_eco <- read.csv(file=file.path(filename),sep = ";",dec = ",")
filename <- file.path(RinputPath, "STECF_DATA", "2023_AER_data",
"2018_2021_economic_variables_per_fs.csv")
AER_eco <- read.csv(file=file.path(filename),sep = ";",dec = ",") ##MISSING 2018!!
cols <- colnames(AER_eco)[colnames(AER_eco) %in% colnames(AER2022_eco)]
AER_eco <- rbind.data.frame(AER2022_eco[AER2022_eco$year==2018,cols], AER_eco[cols])
stecf_fleetdata_energy_costs <- filter(AER_eco, variable_name=="Energy costs") # EUR
stecf_fleetdata_personnel_costs <- filter(AER_eco, variable_name=="Personnel costs") # EUR
stecf_fleetdata_repair_costs <- filter(AER_eco, variable_name=="Repair & maintenance costs") # EUR
stecf_fleetdata_oth_variable_costs <- filter(AER_eco, variable_name=="Other variable costs") # EUR
stecf_fleetdata_other_income <- filter(AER_eco, variable_name=="Other income") # EUR
stecf_fleetdata_unpaid_labour <- filter(AER_eco, variable_name=="Unpaid labour") # EUR
stecf_fleetdata_oth_non_var_costs <- filter(AER_eco, variable_name=="Other non-variable costs") # EUR
stecf_fleetdata_kwFishingdays <- filter(AER_eco, variable_name=="kW fishing days") # kWday
stecf_fleetdata_fishingdays <- filter(AER_eco, variable_name=="Fishing days") # day
stecf_fleetdata_kwDaysAtSea <- filter(AER_eco, variable_name=="kW days at sea") # kWday
stecf_fleetdata_engagedCrew <- filter(AER_eco, variable_name=="Engaged crew") # number
stecf_fleetdata_cons_of_fixed_capital <- filter(AER_eco, variable_name=="Consumption of fixed capital") # EUR
stecf_fleetdata_value_of_physical_capital <- filter(AER_eco, variable_name=="Value of physical capital") # EUR
stecf_fleetdata_energy_costs <- stecf_fleetdata_energy_costs[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_personnel_costs <- stecf_fleetdata_personnel_costs[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_repair_costs <- stecf_fleetdata_repair_costs[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_oth_variable_costs <- stecf_fleetdata_oth_variable_costs[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_other_income <- stecf_fleetdata_other_income[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_unpaid_labour <- stecf_fleetdata_unpaid_labour[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_oth_non_var_costs <- stecf_fleetdata_oth_non_var_costs[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_aerECOkwFishingdays <- stecf_fleetdata_kwFishingdays[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_fishingdays <- stecf_fleetdata_fishingdays[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_kwDaysAtSea <- stecf_fleetdata_kwDaysAtSea [,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_engagedCrew <- stecf_fleetdata_engagedCrew [,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_cons_of_fixed_capital <- stecf_fleetdata_cons_of_fixed_capital[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
stecf_fleetdata_value_of_physical_capital <- stecf_fleetdata_value_of_physical_capital[,c("country_code", "year","supra_reg","fishing_tech","vessel_length","fs_name","value")]
names(stecf_fleetdata_energy_costs)[7] <- "energycosts"
names(stecf_fleetdata_personnel_costs)[7] <- "personnelcosts"
names(stecf_fleetdata_repair_costs)[7] <- "repaircosts"
names(stecf_fleetdata_oth_variable_costs)[7] <- "othvarcosts"
names(stecf_fleetdata_aerECOkwFishingdays)[7] <- "aerECOkwFishingdays"
names(stecf_fleetdata_fishingdays)[7] <- "fishingdays"
names(stecf_fleetdata_kwDaysAtSea)[7] <- "kwDaysAtSea"
names(stecf_fleetdata_other_income)[7] <- "other_income"
names(stecf_fleetdata_unpaid_labour)[7] <- "unpaid_labour"
names(stecf_fleetdata_oth_non_var_costs)[7] <- "oth_non_var_costs"
names(stecf_fleetdata_engagedCrew)[7] <- "engagedCrew"
names(stecf_fleetdata_cons_of_fixed_capital)[7] <- "cons_of_fixed_capital"
names(stecf_fleetdata_value_of_physical_capital)[7] <- "value_of_physical_capital"
# the equations for the Economic Evaluation we target are:
# GVA <- (landings_kg * average_price_EUR_per_kg) + other_income - unpaid_labour - energycosts - othvarcosts - oth_non_var_costs - repaircosts
# GrossProfit <- GVA - personnelcosts
# OperatingProfit <- GrossProfit - cons_of_fixed_capital
# CapitalOpportunityCosts <- value_of_physical_capital * opportunity_interest_rate/100.0
# NetProfit <- OperatingProfit - CapitalOpportunityCosts - value_of_physical_capital * ((100.0-annual_depreciation_rate)/100.0)
#=> to be computed on the final merged dataset, then to be recomputed after applying displacement scenarios (e.g. based on LPUEs-costs...) changing the income from landings and the costs
#=> would need to compute a price from landing_eur/landing_kg...
# Joining...
AERcosts <- left_join(stecf_fleetdata_energy_costs, stecf_fleetdata_personnel_costs, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERcosts <- left_join(AERcosts, stecf_fleetdata_repair_costs, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERcosts <- left_join(AERcosts, stecf_fleetdata_oth_variable_costs, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERcosts <- left_join(AERcosts, stecf_fleetdata_aerECOkwFishingdays, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERcosts <- left_join(AERcosts, stecf_fleetdata_fishingdays, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERcosts <- left_join(AERcosts, stecf_fleetdata_kwDaysAtSea, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
dd <- AERcosts[AERcosts$country_code=="BEL" & AERcosts$fishing_tech=="TBB" & AERcosts$vessel_length=="VL2440" & AERcosts$year=="2019",]
(dd$aerECOkwFishingdays) #=> 6864051 # really different than the AER fs effort one...
# aggregate to remove the fs_name dimension
AERcosts <- as.data.frame(AERcosts)
AERcosts <- AERcosts[!duplicated(AERcosts [,c("country_code", "year", "fishing_tech", "vessel_length", "fs_name", "supra_reg")]),]
AERcosts <- data.table(AERcosts)
AERcosts$energycosts <- as.numeric(AERcosts$energycosts)
AERcosts$personnelcosts <- as.numeric(AERcosts$personnelcosts)
AERcosts$repaircosts <- as.numeric(AERcosts$repaircosts)
AERcosts$othvarcosts <- as.numeric(AERcosts$othvarcosts)
AERcosts$aerECOkwFishingdays <- as.numeric(AERcosts$aerECOkwFishingdays)
AERcosts$fishingdays <- as.numeric(AERcosts$fishingdays)
AERcosts$kwDaysAtSea <- as.numeric(AERcosts$kwDaysAtSea)
AERcosts <- AERcosts[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("energycosts", "personnelcosts", "repaircosts", "othvarcosts", "aerECOkwFishingdays", "fishingdays", "kwDaysAtSea"),
by=c("year", "country_code", "fishing_tech", "vessel_length", "supra_reg")
]
AERcosts[AERcosts$year=="2020" & AERcosts$country_code=="ESP" & AERcosts$fishing_tech=="HOK" & AERcosts$vessel_length=="VL2440" ,]
AERothvars <- left_join(stecf_fleetdata_engagedCrew, stecf_fleetdata_cons_of_fixed_capital, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERothvars <- left_join(AERothvars, stecf_fleetdata_value_of_physical_capital, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERothvars <- left_join(AERothvars, stecf_fleetdata_other_income, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERothvars <- left_join(AERothvars, stecf_fleetdata_unpaid_labour, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
AERothvars <- left_join(AERothvars, stecf_fleetdata_oth_non_var_costs, by=c("country_code","year","supra_reg","fishing_tech","vessel_length","fs_name"))
# aggregate to remove the fs_name dimension
AERothvars <- as.data.frame(AERothvars)
AERothvars <- AERothvars[!duplicated(AERothvars [,c("country_code", "supra_reg", "year", "fishing_tech", "vessel_length", "fs_name")]),]
AERothvars <- data.table(AERothvars)
AERothvars$engagedCrew <- as.numeric(AERothvars$engagedCrew)
AERothvars$other_income <- as.numeric(AERothvars$other_income)
AERothvars$unpaid_labour <- as.numeric(AERothvars$unpaid_labour)
AERothvars$oth_non_var_costs <- as.numeric(AERothvars$oth_non_var_costs)
AERothvars$cons_of_fixed_capital <- as.numeric(AERothvars$cons_of_fixed_capital)
AERothvars$value_of_physical_capital <- as.numeric(AERothvars$value_of_physical_capital)
AERothvars <- AERothvars[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("engagedCrew", "other_income", "unpaid_labour", "oth_non_var_costs", "cons_of_fixed_capital", "value_of_physical_capital"),
by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length")
]
AERothvars[AERothvars$year=="2020" & AERothvars$country_code=="ESP" & AERothvars$fishing_tech=="HOK" & AERothvars$vessel_length=="VL2440" ,]
# save for later use
#filename <- file.path(RinputPath, "STECF_DATA", "STECF 22-06 AER 2022 - data",
# "AERothvars_fromAER2022.RData")
filename <- file.path(RinputPath, "STECF_DATA", "2023_AER_Data",
"AERothvars_fromAER2023.RData")
save(AERothvars, file=file.path(filename))
# side note: a change in nb of jobs could be expressed as: nb job change = (variation in revenue before-after) / (revenue/crew)
# or: In terms of the short term and long-term effect my suggestion, and given that there is not a dynamic model,
# would be to calculate gross profits as short term indicator and net profit as a long term indicator.
# When assessing the likely risk on unemployment you can refer to these two indicators, by saying that the quantity x of employees are at a risk
# in the short and/or long term if any of these indicators shifts from positive to negative.
uan <- function(x) unlist(c(x)) # caution with data.table
AER_costratios <- cbind.data.frame(
AERcosts[, c("country_code","year","supra_reg","fishing_tech","vessel_length")],
aerECOkwFishingdays=uan(AERcosts[, "aerECOkwFishingdays"]),
enerbykwfishdy= uan(AERcosts[, "energycosts"]) / uan(AERcosts[, "aerECOkwFishingdays"]), # because variable
wagebyinc= uan(AERcosts[, "personnelcosts"]) / uan(AERcosts[, "aerECOkwFishingdays"]), # because variable
repbykwfishday= uan(AERcosts[, "repaircosts"]) / uan(AERcosts[, "aerECOkwFishingdays"]), # because variable
varbykwfishday= uan(AERcosts[, "othvarcosts"]) / uan(AERcosts[, "aerECOkwFishingdays"]) # because variable
)
AER_costratios$year <- as.character(AER_costratios$year)
AER_costratios <- AER_costratios[,c("country_code", "year", "supra_reg", "fishing_tech", "vessel_length", "aerECOkwFishingdays", "enerbykwfishdy", "wagebyinc", "repbykwfishday", "varbykwfishday")]
AER_nonvariablevars <- cbind.data.frame(
AERothvars[, c("country_code","year","supra_reg","fishing_tech","vessel_length")],
engagedCrew= AERothvars[, "engagedCrew"], # not variable
other_income= AERothvars[, "other_income"], # not variable
unpaid_labour= AERothvars[, "unpaid_labour"], # not variable
oth_non_var_costs= AERothvars[, "oth_non_var_costs"], # not variable
cons_of_fixed_capital= AERothvars[, "cons_of_fixed_capital"], # not variable
value_of_physical_capital= AERothvars[, "value_of_physical_capital"] # not variable
)
AER_nonvariablevars$year <- as.character(AER_nonvariablevars$year)
AER_nonvariablevars <- AER_nonvariablevars[,c("country_code", "year", "supra_reg", "fishing_tech", "vessel_length", "engagedCrew",
"other_income", "unpaid_labour", "oth_non_var_costs", "cons_of_fixed_capital", "value_of_physical_capital")]
AER_nonvariablevars[AER_nonvariablevars$year=="2020" & AER_nonvariablevars$country_code=="ESP" & AER_nonvariablevars$fishing_tech=="HOK" & AER_nonvariablevars$vessel_length=="VL2440" ,]
### CAUTION: NO COST INFORMED IN 2019 FROM DATABASE "STECF 20-06 - AER 2020 - data"
### CAUTION: LIKELY NO COST INFORMED IN 2021 FROM DATABASE "STECF xx-xx - AER 2022 - data"
aa<- AER_costratios[AER_costratios$year=="2019", "fs_name"]
bb<- AER_costratios[AER_costratios$year=="2020", "fs_name"]
unique(bb[!bb %in% aa])
##-------
# formatting a AER costratios
#joining AER dataframe with value, weight and effort with costratio-dataframe
stecf_fleetdata_with_kiloandeffort_andcostratios <- left_join(agg_stecf_fleetdata_with_kiloeffort_nofs, AER_costratios,
by= c("country_code", "year","supra_reg", "fishing_tech","vessel_length"))
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
# input
dat <- stecf_fleetdata_with_kiloandeffort_andcostratios
# dispatch the aerECOkwFishingdays variable over region. This var (as all AER_eco variables) is not regionalised
tot <- dat[,.(tot_aerECOkwFishingdays=sum(an(aerECOkwFishingdays)/1e9)), by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length")]
dd <- left_join(dat, tot)
dd$share_aerECOkwFishingdays <- (an(dd$aerECOkwFishingdays)/1e9) / an(dd$tot_aerECOkwFishingdays) # caution: a 1e9 rescaling used to avoid the too large numbers overflow
dd$aerECOkwFishingdays_perregion <- an(dd$share_aerECOkwFishingdays) * an(dd$aerECOkwFishingdays)
AER_nonvariablevars[AER_nonvariablevars$year=="2020" & AER_nonvariablevars$country_code=="ESP" & AER_nonvariablevars$fishing_tech=="HOK" & AER_nonvariablevars$vessel_length=="VL2440" ,]
dd <- left_join (dd, AER_nonvariablevars, by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length"))
dd$varcosts <- dd$share_aerECOkwFishingdays * dd$aerECOkwFishingdays * (dd$enerbykwfishdy + dd$repbykwfishday + dd$varbykwfishday)
#dd$fs_name <- dd$fs_name.x # a fix
# check numbers...
ddd <- dd[dd$year=="2019" & dd$country_code=="BEL" & dd$fishing_tech=="TBB" & dd$vessel_length=="VL2440",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018",]
ddd$GVA <- (an(ddd$weight) * # landing kg * price
(an(ddd$value)/an(ddd$weight))) +
an(ddd$other_income) - # plus other income
an(ddd$unpaid_labour) - an(ddd$varcosts) # minus var costs
dd[dd$year=="2019" & dd$country_code=="ESP" & dd$fishing_tech=="HOK" & dd$vessel_length=="VL2440" ,]
# output
stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched <- dd [, .SD, .SDcols=c(colnames(stecf_fleetdata_with_kiloandeffort_andcostratios), "aerECOkwFishingdays_perregion")]
# check
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched
head(dd[year=="2019" & vessel_length=="VL0010" & fishing_tech=="DFN" & sub_reg=="27.10.a",])
# check numbers
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched[stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched$supra_reg=="NAO",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018",]
sum(ddd$weight)
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
#-----------------------------------
#-----------------------------------
#-----------------------------------
# READ STECF FDI DATA
# Disclaimer STECF FDI "Fisheries landings & effort: data by c-square" (https://data.jrc.ec.europa.eu/dataset/00ae6659-ddde-4314-a9da-717bb2e82582):
#The spatial dataset (years 2013-2021) published on this page is a subset of the data provided by EU Member States in the context of the DCF (Data Collection Framework) data call
#collecting Fisheries Dependent Information (FDI). The data provided by Member States during the 2022 FDI data call were analyzed by the Scientific, Technical and Economic Committee for Fisheries (STECF) Expert Working Group 22-10. Before accessing the data we strongly encourage to read the STECF report 22-10 (https://stecf.jrc.ec.europa.eu/reports). Disclaimer: Although the data published here have been assessed by STECF, the quality and completeness of the data are under the responsibility of the EU Member States.
# Note: no country info....(hence, the previous historical dataset set up during the STECF fishing effort regime working groups (not anymore updated since 2017) is still downloaded because contains the country information.
# Note: some record kept confidential in FDI, therefore there are some datagaps.....
#------------
# Read the FDI effort per rectangle
filename <- file.path(RinputPath, "STECF_DATA","FDI_2022_data","spatial_effort_EU28.csv")
library(data.table)
fdi_effort_rect22 <- fread(filename, quote="\"")
# 2023 FDI data with confidential data and also adding the country info
filename <- file.path(RinputPath, "STECF_DATA","FDI_2023_data_adhoc","table_i.csv")
library(data.table)
fdi_effort_rect23 <- fread(filename, quote="\"")
# fdi_effort_rect23[ ,sum(totfishdays), by=c( "vessel_length", "supra_region", "confidential")]
# check
#fdi_effort_rect23[ year=="2019" & supra_region=="NAO" & fishing_tech =="DTS" & vessel_length=="VL1824" & sub_region =="27.4.A" & metier== "OTB_DEF_100-119_0_0" & icesname=="44E7" ,]
# Caution: the Table I 2023 confidential does not come up as the the pulbic 2022 spatial_effort_EU28.csv which is already split per csquare...
# Therfore, assign a cscode but caution to overwrite for ICES rectangle by splitting them into two c-squares
dd <- as.data.frame(fdi_effort_rect23)
dd$cscode <- vmstools::CSquare(an(dd[,"rectangle_lon"]), an(dd[,"rectangle_lat"]), degrees=0.5)
idx <- dd$c_square!="NA" & dd$rectangle_type=="NA" # if c_square already informed then use it!
dd[idx, "cscode"] <- dd[idx, "c_square"] # if c_square already informed then use it!
dd[idx, "rectangle_type"] <- "05*05" # if c_square already informed then use it!
dd$icesname <- vmstools::ICESrectangle(data.frame(SI_LONG=an(dd$rectangle_lon), SI_LATI=an(dd$rectangle_lat)))
ddd1 <- dd[dd$rectangle_type=="05*1",]
ddd2 <- dd[dd$rectangle_type=="05*1",]
ddd1$rectangle_lon2 <- an(ddd1$rectangle_lon) -0.25
ddd2$rectangle_lon2 <- an(ddd2$rectangle_lon) +0.25
ddd1$totfishdays <- ddd1$totfishdays/2
ddd2$totfishdays <- ddd2$totfishdays/2
ddd1$cscode <- vmstools::CSquare(an(ddd1[,"rectangle_lon2"]), an(ddd1[,"rectangle_lat"]), degrees=0.5)
ddd2$cscode <- vmstools::CSquare(an(ddd2[,"rectangle_lon2"]), an(ddd2[,"rectangle_lat"]), degrees=0.5)
ddd1 <- ddd1[,!colnames(ddd1) %in% "rectangle_lon2"]
ddd2 <- ddd2[,!colnames(ddd2) %in% "rectangle_lon2"]
res <- rbind.data.frame(ddd1, ddd2)
fdi_effort_rect <- rbind.data.frame(dd[dd$rectangle_type!="05*1",], res)
fdi_effort_rect <- data.table (fdi_effort_rect)
# check
#fdi_effort_rect[ year=="2019" & supra_region=="NAO" & fishing_tech =="DTS" & vessel_length=="VL1824" & sub_region =="27.4.A" & metier== "OTB_DEF_100-119_0_0" & icesname=="44E7" ,]
# make the code conistent in case the FDI data are not having the country dimension informed
if(!"country_code" %in% colnames(fdi_effort_rect)) fdi_effort_rect$country_code <- "ALL"
# a small fix and renaming
fdi_effort_rect$fditotfishdays <- as.numeric(gsub(",", ".", fdi_effort_rect$totfishdays))
# AER and FDI are compatible given fleet seg names
# Retrieve the sub_reg?
#library(vmstools)
#data(ICESareas) # in vmstools
#sq <- cbind.data.frame(unique(fdi_effort_rect$cscode), vmstools::CSquare2LonLat(unique(fdi_effort_rect$cscode), 0.5))
#sq$idx <- an(ICESarea(sq, ICESareas, fast=TRUE))
#sq <- sq[!is.na(sq$idx),]
#dd <- as.data.frame(ICESareas)
#sq$sub_reg <- dd[match(sq$idx+1,dd$OBJECTID),"Area_Full"] # caution: +1
#colnames(sq) <- c("cscode", "lat_csq", "long_csq", "idx", "sub_reg")
#sq <- sq[,c("cscode", "sub_reg")]
## joining geo information to fdi data
#fdi_effort_rect <- dplyr::left_join(fdi_effort_rect, sq, by=c("cscode"))
# replaced by:
fdi_effort_rect$sub_reg <- tolower(fdi_effort_rect$sub_region)
# renaming
colnames(fdi_effort_rect)[colnames(fdi_effort_rect)=="fshng_t"] <- "fishing_tech"
colnames(fdi_effort_rect)[colnames(fdi_effort_rect)=="vssl_ln"] <- "vessel_length"
colnames(fdi_effort_rect)[colnames(fdi_effort_rect)=="ger_typ"] <- "gear_type"
# a fix in naming countries
dd <- as.data.frame(fdi_effort_rect)
dd$country_code <- factor(dd$country_code)
levels(dd$country_code)[levels(dd$country_code) %in% "ENG"] <- "GBR"
levels(dd$country_code)[levels(dd$country_code) %in% "SCO"] <- "GBR"
fdi_effort_rect <- data.table(fdi_effort_rect)
#------------
# Read the FDI landings per rectangle per year (tonnes, and euros)
#(identical values for a given key is likely the outcome of an even dispatching over c-square done by EU MS before submitting data to the JRC...)
#(...here we don´t care as we aggregate back to the ICES rectangle level)
#filename <- file.path(getwd(), "ADVICES","STECF","newFDI","2021_FDI_data","spatial_effort_attribute_table.csv")
filename <- file.path(RinputPath, "STECF_DATA","FDI_2023_data_adhoc","table_h2018.csv") # 2023 FDI Confidential years: 2013-2021
library(data.table)
fdi_land_rect_2018 <- fread(filename, quote="\"")
#filename <- file.path(RinputPath, "STECF_DATA","FDI_2022_data","spatial_landings_2019_EU28.csv") # years: 2013-2021
filename <- file.path(RinputPath, "STECF_DATA","FDI_2023_data_adhoc","table_h2019.csv") # 2023 FDI Confidential years: 2013-2021
library(data.table)
fdi_land_rect_2019 <- fread(filename, quote="\"")
#filename <- file.path(RinputPath, "STECF_DATA","FDI_2022_data","spatial_landings_2020_EU28.csv")
filename <- file.path(RinputPath, "STECF_DATA","FDI_2023_data_adhoc","table_h2020.csv") # 2023 FDI Confidential years: 2013-2021
library(data.table)
fdi_land_rect_2020 <- fread(filename, quote="\"")
#filename <- file.path(RinputPath, "STECF_DATA","FDI_2022_data","spatial_landings_2021_EU28.csv")
filename <- file.path(RinputPath, "STECF_DATA","FDI_2023_data_adhoc","table_h2021.csv") # 2023 FDI Confidential years: 2013-2021
library(data.table)
fdi_land_rect_2021 <- fread(filename, quote="\"")
fdi_land_rect <- rbind(fdi_land_rect_2018,
fdi_land_rect_2019,
fdi_land_rect_2020,
fdi_land_rect_2021)
# a fix in naming countries
dd <- as.data.frame(fdi_land_rect)
dd$country_code <- factor(dd$country_code)
levels(dd$country_code)[levels(dd$country_code) %in% "ENG"] <- "GBR"
levels(dd$country_code)[levels(dd$country_code) %in% "SCO"] <- "GBR"
fdi_land_rect <- data.table(fdi_land_rect)
# Caution: the Table H 2023 confidential does not come up as the public 2022 which is already split per csquare...
# Therfore, assign a cscode but caution to overwrite for ICES rectangle by splitting them into two c-squares
dd <- as.data.frame(fdi_land_rect)
dd$cscode <- vmstools::CSquare(an(dd[,"rectangle_lon"]), an(dd[,"rectangle_lat"]), degrees=0.5)
idx <- dd$c_square!="NA" & dd$rectangle_type=="NA" # if c_square already informed then use it!
dd[idx, "cscode"] <- dd[idx, "c_square"] # if c_square already informed then use it!
dd[idx, "rectangle_type"] <- "05*05" # if c_square already informed then use it!
dd$icesname <- vmstools::ICESrectangle(data.frame(SI_LONG=an(dd$rectangle_lon), SI_LATI=an(dd$rectangle_lat)))
ddd1 <- dd[dd$rectangle_type=="05*1",]
ddd2 <- dd[dd$rectangle_type=="05*1",]
ddd1$rectangle_lon2 <- an(ddd1$rectangle_lon) -0.25
ddd2$rectangle_lon2 <- an(ddd2$rectangle_lon) +0.25
ddd1$totwghtlandg <- ddd1$totwghtlandg/2
ddd2$totwghtlandg <- ddd2$totwghtlandg/2
ddd1$totvallandg <- ddd1$totvallandg/2
ddd2$totvallandg <- ddd2$totvallandg/2
ddd1$cscode <- vmstools::CSquare(an(ddd1[,"rectangle_lon2"]), an(ddd1[,"rectangle_lat"]), degrees=0.5)
ddd2$cscode <- vmstools::CSquare(an(ddd2[,"rectangle_lon2"]), an(ddd2[,"rectangle_lat"]), degrees=0.5)
ddd1 <- ddd1[,!colnames(ddd1) %in% "rectangle_lon2"]
ddd2 <- ddd2[,!colnames(ddd2) %in% "rectangle_lon2"]
res <- rbind.data.frame(ddd1, ddd2)
fdi_land_rect <- rbind.data.frame(dd[dd$rectangle_type!="05*1",], res)
fdi_land_rect <- data.table (fdi_land_rect)
dim(fdi_land_rect[deep=="DEEP",])
##-------
##-------
# aggregate FDI for keeping main species only (with a percent threshold)
# AND get rid of the "year-quarter" dimension
an <- function (x) as.numeric(as.character(x))
main_species <- fdi_land_rect[, .(fditotwghtlandg = sum(an(totwghtlandg), na.rm=T),
fditotvallandg = sum(an(totvallandg), na.rm=T)),
by=c("species")] [order(-fditotvallandg)]
main_species$percent <- cumsum(an(main_species$fditotwghtlandg)/sum(an(main_species$fditotwghtlandg)))*100
#oth_species <- unique(unlist(c(main_species[main_species$percent>98, "species"])))
oth_species <- unique(unlist(c(main_species[main_species$percent>50, "species"])))
fdi_land_rect[fdi_land_rect$species %in% oth_species, "species"] <- "OTH"
fdi_land_rect_mainsp <- fdi_land_rect[, .(fditotvallandg = sum(an(totvallandg), na.rm=T),
fditotwghtlandg = sum(an(totwghtlandg), na.rm=T)),
by=c("year", "country_code", "supra_region","fishing_tech","vessel_length","sub_region", "metier","species", "rectangle_type", "icesname","cscode", "rectangle_lon", "rectangle_lat")]
fdi_land_rect_mainsp$sub_reg <- tolower(fdi_land_rect_mainsp$sub_reg)
# interlude to show that STECF FDI landings Rect is at the ICES rectangle resolution
if(FALSE){
# retrieve the coord
library(vmstools)
a_var <- fdi_land_rect_mainsp[, .(landings_in_cell=sum(fditotwghtlandg)), by=c("cscode", "rectangle_lat", "rectangle_lon", "species")]
a_var <- as.data.frame(a_var)
resy <- 0.5
resx <- 1
cutbreakval <- c(-1,0,20,40,80,160,320,3000000) # kilos
colyellowred <- terrain.colors(length(cutbreakval))
a_species <- "HER"
cols <- c("white", colyellowred)[cut(unlist(an(a_var[a_var$species==a_species, "landings_in_cell"])), breaks=cutbreakval)]
coord <- a_var[a_var$species==a_species, c("rectangle_lon", "rectangle_lat")]
plot(coord, pch="")
an <- function(x) as.numeric(as.character(x))
for (i in 1: nrow(coord)) rect(an(coord[i, "rectangle_lon"])-resx/2, an(coord[i,"rectangle_lat"])-resy/2, an(coord[i,"rectangle_lon"])+resx/2, an(coord[i,"rectangle_lat"])+resy/2, col=cols[i], border=FALSE)
#=> looks quite a coarse resolution (i.e. ICES rect)
}
#-----------------------------------
#-----------------------------------
#-----------------------------------
#------------
# merge FDI effort rect with FDI Landings rect
# but first remove the "quarter" dimension:
fdi_effort_rect_annual <- fdi_effort_rect[, .(fditotfishdays = sum(an(fditotfishdays), na.rm=T)),
by=c("year","country_code","supra_region","fishing_tech","vessel_length","sub_region", "metier","rectangle_type", "icesname", "cscode", "rectangle_lon", "rectangle_lat")]
fdi_land_effort_rect_mainsp <- left_join(fdi_land_rect_mainsp, fdi_effort_rect_annual,
by=c("year", "country_code","supra_region", "fishing_tech", "vessel_length", "metier", "icesname", "cscode", "rectangle_type", "rectangle_lon", "rectangle_lat"))
# caution however: the effort is repeated alongside the species dimension....so the sum of effort from now onwards are imflated because of this....
dd <- fdi_effort_rect_annual[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_region=="27.8.B",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
ddd <- fdi_land_effort_rect_mainsp[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_region.x=="27.8.B",]
ddd[,sum(fditotfishdays, na.rm=TRUE),]
#=> Effort, Landings per SPecies in C-square....with such a dataset it is possible to compute percentage of landing value & volume in any kind of polygons and LPUEs for reallocation....
# However we need to couple with AER economic info:
#-----------------------------------
#-----------------------------------
#-----------------------------------
# forsee the merging consistency
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched # AER 2017-2021
dd2 <- fdi_land_effort_rect_mainsp[fdi_land_effort_rect_mainsp$year %in% c("2018", "2019", "2020", "2021"),]
# inconsistent sub_reg definition found with
unique(dd$sub_reg[!dd$sub_reg %in% dd2$sub_reg]) # aer reg not in fdi reg
unique(dd2$sub_reg[!dd2$sub_reg %in% dd$sub_reg])
# and a close match searched by hand with e.g.:
# unique(dd$sub_reg)[grep("27.3", unique(dd$sub_reg))]
# unique(dd2$sub_reg)[grep("34.1", unique(dd2$sub_reg))]
# fix on the AER side
# 1. fix for GSA coding in AER not compatible with FDI
idx <- grepl("sa ", dd$sub_reg)
dd[idx,"sub_reg"] <- paste0("g", as.character(unlist(dd[idx,"sub_reg"])))
idx <- grepl("gsa ", dd$sub_reg)
library(stringr)
temp <- as.data.frame(stringr::str_split_fixed(as.character(unlist(dd[idx,"sub_reg"]))," ",2))
dd[idx,"sub_reg"] <-paste0(temp[,1], temp[,2])
# 2. fix for too refined coding:
dd[dd$sub_reg %in% c("34.1.1.1"), "sub_reg"] <- "34.1.1"
dd[dd$sub_reg %in% c("34.1.3.1"), "sub_reg"] <- "34.1.3"
dd[dd$sub_reg %in% c("41.1.1"), "sub_reg"] <- "41.1"
dd[dd$sub_reg %in% c("41.2.4"), "sub_reg"] <- "41.2"
dd[dd$sub_reg %in% c("47.1.3"), "sub_reg"] <- "47.1.1"
dd[dd$sub_reg %in% c("34.3.1.2"), "sub_reg"] <- "34.3.1"
dd[dd$sub_reg %in% c("27.7.j.2"), "sub_reg"] <- "27.7.j"
dd[dd$sub_reg %in% c("34.3.1.3"), "sub_reg"] <- "34.3.1"
dd[dd$sub_reg %in% c("41.2.2"), "sub_reg"] <- "41.2.1"
dd[dd$sub_reg %in% c("27.12.c"), "sub_reg"] <- "27.12"
dd[dd$sub_reg %in% c("27.7.c.2"), "sub_reg"] <- "27.7.c"
dd[dd$sub_reg %in% c("34.1.1.2"), "sub_reg"] <- "34.1.1"
dd[dd$sub_reg %in% c("27.10.a.2"), "sub_reg"] <- "27.10.a"
dd[dd$sub_reg %in% c("34.2"), "sub_reg"] <- "34.2.0"
dd[dd$sub_reg %in% c("27.6.b.2"), "sub_reg"] <- "27.6.b"
dd[dd$sub_reg %in% c("41.1.4"), "sub_reg"] <- "41.1"
dd[dd$sub_reg %in% c("27.9.b.1"), "sub_reg"] <- "27.9.b"
dd[dd$sub_reg %in% c("34.1.3.2"), "sub_reg"] <- "34.1.3"
dd[dd$sub_reg %in% c("34.3.1.1"), "sub_reg"] <- "34.3.1"
dd[dd$sub_reg %in% c("27.2.b.2"), "sub_reg"] <- "27.2.b"
dd[dd$sub_reg %in% c("41.3.2"), "sub_reg"] <- "41.3.1"
dd[dd$sub_reg %in% c("47.1.2"), "sub_reg"] <- "47.1.1"
dd[dd$sub_reg %in% c("47.1.5"), "sub_reg"] <- "47.1.1"
dd[dd$sub_reg %in% c("21.3.m"), "sub_reg"] <- "41.3.n"
dd[dd$sub_reg %in% c("27.8.e.1"), "sub_reg"] <- "27.8.e"
dd[dd$sub_reg %in% c("27.7.k.2"), "sub_reg"] <- "27.7.k"
dd[dd$sub_reg %in% c("27.8"), "sub_reg"] <- "27.8.a"
dd[dd$sub_reg %in% c("27.10.a.1"), "sub_reg"] <- "27.10.a"
dd[dd$sub_reg %in% c("27.6.b.1"), "sub_reg"] <- "27.6.b"
dd[dd$sub_reg %in% c("47.b.1"), "sub_reg"] <- "47.b.0"
dd[dd$sub_reg %in% c("27.3.d.28"), "sub_reg"] <- "27.3.d.28.1"
dd[dd$sub_reg %in% c("27.12.b"), "sub_reg"] <- "27.12"
dd[dd$sub_reg %in% c("47.1.4"), "sub_reg"] <- "47.1.1"
dd[dd$sub_reg %in% c("47.c.1"), "sub_reg"] <- "47.c.0"
dd[dd$sub_reg %in% c("27.8.e.2"), "sub_reg"] <- "27.8.e"
dd[dd$sub_reg %in% c("27.2.a.2"), "sub_reg"] <- "27.2.a"
dd[dd$sub_reg %in% c("27.12.a.1"), "sub_reg"] <- "27.12.a"
dd[dd$sub_reg %in% c("27.14.b.1"), "sub_reg"] <- "27.14.b"
dd[dd$sub_reg %in% c("27.1"), "sub_reg"] <- "27.1.a"
dd[dd$sub_reg %in% c("21.4.v.s"), "sub_reg"] <- "21.4.v"
dd[dd$sub_reg %in% c( "27.9.b.2"), "sub_reg"] <- "27.9.b"
dd[dd$sub_reg %in% c("27.12.a"), "sub_reg"] <- "27.12"
dd[dd$sub_reg %in% c("34.1.1.3"), "sub_reg"] <- "34.1.1"
dd[dd$sub_reg %in% c("41.2.3"), "sub_reg"] <- "41.2.1"
dd[dd$sub_reg %in% c("27.7.j.1"), "sub_reg"] <- "27.7.j"
dd[dd$sub_reg %in% c("21.1.f"), "sub_reg"] <- "21.1.d"
dd[dd$sub_reg %in% c("41.3.3"), "sub_reg"] <- "41.3.1"
dd[dd$sub_reg %in% c("27.7.k.1"), "sub_reg"] <- "27.7.k"
dd[dd$sub_reg %in% c("27.3.b"), "sub_reg"] <- "27.3.b.23"
dd[dd$sub_reg %in% c("27.5.b.2"), "sub_reg"] <- "27.5.b"
dd[dd$sub_reg %in% c("27.8.d.1"), "sub_reg"] <- "27.8.d"
dd[dd$sub_reg %in% c( "27.7.c.1"), "sub_reg"] <- "27.7.c"
dd[dd$sub_reg %in% c("58"), "sub_reg"] <- "58.4.1"
dd[dd$sub_reg %in% c("87.3.3"), "sub_reg"] <- "87"
dd[dd$sub_reg %in% c("37.1.3"), "sub_reg"] <- "gsa6"
dd[dd$sub_reg %in% c("27.9"), "sub_reg"] <- "27.9.a"
dd[dd$sub_reg %in% c("27.12.a.4"), "sub_reg"] <- "27.12"
dd[dd$sub_reg %in% c("27.5.b.1.b"), "sub_reg"] <- "27.5.b"
dd[dd$sub_reg %in% c("37.2.2"), "sub_reg"] <- "gsa19"
dd[dd$sub_reg %in% c("27.2.a.1"), "sub_reg"] <- "27.2.a"
dd[dd$sub_reg %in% c("37.3.1"), "sub_reg"] <- "gsa24"
dd[dd$sub_reg %in% c("37.1.1"), "sub_reg"] <- "gsa1"
dd[dd$sub_reg %in% c("27.5.b.1"), "sub_reg"] <- "27.5.b"
dd[dd$sub_reg %in% c("41.3.n"), "sub_reg"] <- "41.3"
dd[dd$sub_reg %in% c("41.2"), "sub_reg"] <- "41.2.1"
dd[dd$sub_reg %in% c("47.d.1"), "sub_reg"] <- "47.d"
dd[dd$sub_reg %in% c("27.4"), "sub_reg"] <- "27.4.a"
dd[dd$sub_reg %in% c("21"), "sub_reg"] <- "21.1.b"
dd[dd$sub_reg %in% c("18"), "sub_reg"] <- "gsa18"
dd[dd$sub_reg %in% c("27.8.d.2"), "sub_reg"] <- "27.8.d"
dd[dd$sub_reg %in% c("41.3"), "sub_reg"] <- "41.3.1"
dd[dd$sub_reg %in% c("34"), "sub_reg"] <- "34.1.3"
dd[dd$sub_reg %in% c("47"), "sub_reg"] <- "47.a.0"
dd[dd$sub_reg %in% c("27.7"), "sub_reg"] <- "27.7.e"
dd[dd$sub_reg %in% c("27.7"), "sub_reg"] <- "27.7.e"
dd[dd$sub_reg %in% c("57.3"), "sub_reg"] <- "57"
dd[dd$sub_reg %in% c("27.14.b.2"), "sub_reg"] <- "27.14.b"
dd[dd$sub_reg %in% c("21.4"), "sub_reg"] <- "21.4.v"
dd[dd$sub_reg %in% c("21.4.x"), "sub_reg"] <- "21.4.v"
dd[dd$sub_reg %in% c("58.4.1"), "sub_reg"] <- "21.4.v"
# fix on the FDI side
dd2[dd2$sub_reg %in% c("27.3.a.20", "27.3.a.21"), "sub_reg"] <- "27.3.a"
dd2[dd2$sub_reg %in% c("21.3m"),"sub_reg"] <- "27.3.m"
dd2[dd2$sub_reg %in% c("21.6h"),"sub_reg"] <- "21.6.h"
dd2[dd2$sub_reg %in% c("21.1c"),"sub_reg"] <- "21.1.c"
dd2[dd2$sub_reg %in% c("21.3k"),"sub_reg"] <- "21.3.k"
dd2[dd2$sub_reg %in% c("21.1d"),"sub_reg"] <- "21.1.d"
dd2[dd2$sub_reg %in% c("57.1"),"sub_reg"] <- "57"
dd2[dd2$sub_reg %in% c("47.a"),"sub_reg"] <- "47.a.1"
dd2[dd2$sub_reg %in% c("47.1"),"sub_reg"] <- "47.1.1"
dd2[dd2$sub_reg %in% c("47"),"sub_reg"] <- "47.a.0"
dd2[dd2$sub_reg %in% c("47.b"),"sub_reg"] <- "47.b.0"
dd2[dd2$sub_reg %in% c("21.3n"),"sub_reg"] <- "21.3.n"
dd2[dd2$sub_reg %in% c("21.6g"),"sub_reg"] <- "21.6.g"
dd2[dd2$sub_reg %in% c("21.4v"),"sub_reg"] <- "21.4.v"
dd2[dd2$sub_reg %in% c("21.3l"),"sub_reg"] <- "21.3.l"
dd2[dd2$sub_reg %in% c("21.3o"),"sub_reg"] <- "21.3.o"
dd2[dd2$sub_reg %in% c("21.1b"),"sub_reg"] <- "21.1.b"
dd2[dd2$sub_reg %in% c("41.2"),"sub_reg"] <- "41.2.1"
dd2[dd2$sub_reg %in% c("47.c"),"sub_reg"] <- "47.c.0"
dd2[dd2$sub_reg %in% c("21.6f"),"sub_reg"] <- "21.6.f"
dd2[dd2$sub_reg %in% c("87.1"),"sub_reg"] <- "87.1.4"
dd2[dd2$sub_reg %in% c("87.2"),"sub_reg"] <- "87.2.6"
dd2[dd2$sub_reg %in% c("41.3"),"sub_reg"] <- "41.3.1"
dd2[dd2$sub_reg %in% c("58.4"),"sub_reg"] <- "58.4.1"
dd2[dd2$sub_reg %in% c("21.1a"),"sub_reg"] <- "21.1.a"
dd2[dd2$sub_reg %in% c("gsa28"),"sub_reg"] <- "gsa29"
dd2[dd2$sub_reg %in% c("21.4w"),"sub_reg"] <- "21.4.w"
dd2[dd2$sub_reg %in% c("21.4x"),"sub_reg"] <- "21.4.x"
dd2[dd2$sub_reg %in% c("61"),"sub_reg"] <- "27.8.d.2"
dd2[dd2$sub_reg %in% c("47.d"),"sub_reg"] <- "47.d.1"
dd2[dd2$sub_reg %in% c("87.3"),"sub_reg"] <- "87"
aer_key_met <- paste(dd$year, dd$fishing_tech, dd$vessel_length, dd$sub_reg, sep="_") # aer
fdi_key_met <- paste(dd2$year, dd2$fishing_tech, dd2$vessel_length, dd2$sub_reg, sep="_") # fdi
not_in_aer_keys <- fdi_key_met[!fdi_key_met %in% aer_key_met]
not_in_aer <- unique(not_in_aer_keys)
not_in_fdi_keys <- aer_key_met[!aer_key_met %in% fdi_key_met]
not_in_fdi <- unique(not_in_fdi_keys)
# one example of unmatched seg:
dd[dd$year=="2018" & dd$fishing_tech=="DFN" & dd$vessel_length=="VL0612" & dd$sub_reg=="gsa5",] # aer
dd2[dd2$year=="2018" & dd2$fishing_tech=="DFN" & dd2$vessel_length=="VL0612" & dd2$sub_reg=="gsa5",] # fdi
dd2[dd2$year=="2018" & dd2$fishing_tech=="DFN" & dd2$vessel_length=="VL0612",]
# output
stecf_fleetdata_with_kiloandeffort_andcostratios <- data.table(dd) # 2017-2021
fdi_land_effort_rect_mainsp <- data.table(dd2) # 2019-2021
# check
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios
#"2019_VL0010_DFN_27.10.a"
head(dd[year=="2019" & vessel_length=="VL0010" & fishing_tech=="DFN" & sub_reg=="27.10.a",])
# check
dd <- fdi_land_effort_rect_mainsp[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_reg=="27.8.b",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# subset for the region of interest....
# HERE THE NAO:
stecf_fleetdata_with_kiloandeffort_andcostratios_nao <-
stecf_fleetdata_with_kiloandeffort_andcostratios[stecf_fleetdata_with_kiloandeffort_andcostratios$supra_reg=="NAO",]
# check numbers
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_nao[stecf_fleetdata_with_kiloandeffort_andcostratios_nao$supra_reg=="NAO",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018",]
sum(ddd$weight)
fdi_land_effort_rect_mainsp_nao <-
fdi_land_effort_rect_mainsp[supra_region=="NAO",]
# check
dd <- fdi_land_effort_rect_mainsp_nao[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_reg=="27.8.b",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
dd <- fdi_land_effort_rect_mainsp_nao[country_code=="PRT" & fishing_tech=="HOK" & vessel_length=="VL2440" & year=="2018" & sub_reg=="27.9.a",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# aggregate a bit to remove the quarter dim and to keep the essential columns only...
fdi_land_effort_rect_mainsp_nao_annual <- fdi_land_effort_rect_mainsp_nao[, .(
fditotwghtlandg = sum(an(fditotwghtlandg), na.rm=T),
fditotvallandg = sum(an(fditotvallandg), na.rm=T),
fditotfishdays = sum(an(fditotfishdays), na.rm=T)),
by=c("year", "country_code", "vessel_length", "fishing_tech", "sub_reg", "metier", "species", "rectangle_type", "icesname", "cscode")]
# check
dd <- fdi_land_effort_rect_mainsp_nao_annual[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_reg=="27.8.b",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
head(fdi_land_effort_rect_mainsp_nao_annual)
head(fdi_effort_rect[fdi_effort_rect$year=="2019" & fdi_effort_rect$vessel_length=="VL0010" & fdi_effort_rect$fishing_tech=="MGO" & fdi_effort_rect$metier=="GND_DEF_80-99_0_0",])
head(fdi_land_rect[fdi_land_rect$year=="2019" & fdi_land_rect$vessel_length=="VL0010" & fdi_land_rect$fishing_tech=="MGO" & fdi_land_rect$metier=="GND_DEF_80-99_0_0",])
head(fdi_effort_rect[fdi_effort_rect$year=="2019" & fdi_effort_rect$vessel_length=="VL1824" & fdi_effort_rect$fishing_tech=="DTS" & fdi_effort_rect$metier=="OTT_CRU_100-119_0_0",])
head(fdi_land_rect[fdi_land_rect$year=="2019" & fdi_land_rect$vessel_length=="VL1824" & fdi_land_rect$fishing_tech=="DTS" & fdi_land_rect$metier=="OTT_CRU_100-119_0_0",])
#=> we can already see that there was an even split over c-square within ices rectangle that has been done to produce these data.....
# so, in the NAO, those data are really resolved at the ices rectangle data.
dd <- fdi_land_effort_rect_mainsp_nao_annual
"2019_VL0010_DFN_27.10.a"
head(dd[year=="2019" & vessel_length=="VL0010" & fishing_tech=="DFN" & sub_reg=="27.10.a",])
head(dd[year=="2019" & vessel_length=="VL2440" & fishing_tech=="HOK" & sub_reg=="27.4.b",])
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# adding a coding to FDI csquare for a depth zone
# According to the FDI data call specification, spatial data on landings and effort (Tables H and I) must be submitted using one of the following notations:
# C-square code at 0.5x0.5 degree resolution, or:
# Latitude and longitude of the center of the rectangle together and its dimensions in decimal degrees:
# 0.5*0.5, corresponding to a c-square,
# 0.5*1, corresponding to an ICES rectangle,
# 1*1 for ICCAT squares,
# 5*5 for IOTC squares.
# retrieve the coord
library(vmstools)
library(dplyr)
dd <- fdi_land_effort_rect_mainsp_nao_annual[fdi_land_effort_rect_mainsp_nao_annual$rectangle_type=="05*1",] # ices rectangle
sq <- cbind.data.frame(cscode=unique(dd$cscode), vmstools::CSquare2LonLat(unique(dd$cscode), 0.5))
dd <- dplyr::left_join(dd, sq, by="cscode")
dd1 <- fdi_land_effort_rect_mainsp_nao_annual[fdi_land_effort_rect_mainsp_nao_annual$rectangle_type=="05*05",]
sq <- cbind.data.frame(cscode=unique(dd1$cscode), vmstools::CSquare2LonLat(unique(dd1$cscode), 0.5))
dd1 <- dplyr::left_join(dd1, sq, by="cscode")
dd2 <- fdi_land_effort_rect_mainsp_nao_annual[fdi_land_effort_rect_mainsp_nao_annual$rectangle_type=="1*1",]
if(nrow(dd2)!=0){
sq <- cbind.data.frame(cscode=unique(dd2$cscode), vmstools::CSquare2LonLat(unique(dd2$cscode), 1))
dd2 <- dplyr::left_join(dd2, sq, by="cscode")
}
dd3 <- fdi_land_effort_rect_mainsp_nao_annual[fdi_land_effort_rect_mainsp_nao_annual$rectangle_type=="5*5",]
if(nrow(dd3)!=0){
sq <- cbind.data.frame(cscode=unique(dd3$cscode), vmstools::CSquare2LonLat(unique(dd3$cscode), 5))
dd3 <- dplyr::left_join(dd3, sq, by="cscode")
}
fdi_land_effort_rect_mainsp_nao_annual_and_depths <- rbind(dd, dd1)
# do an extract
library(raster)
coords <- fdi_land_effort_rect_mainsp_nao_annual_and_depths[, c("SI_LONG","SI_LATI")]
bathy <- raster(file.path(getwd(), "INPUT_SPATIAL_LAYERS", "GEBCO_May_2023","gebco.tif"))
id.cells <- extract(bathy, SpatialPoints(coords), cellnumbers=TRUE)
fdi_land_effort_rect_mainsp_nao_annual_and_depths$in_400_800_zone <- id.cells[,2] # NA if not in the 400-800m
# CAUTION: OVERWRITE FOR NOW: ASSUMING NO EFFECT OF BATHYMETRY:
#fdi_land_effort_rect_mainsp_nao_annual_and_depths$in_400_800_zone <- 1
# check
dd <- fdi_land_effort_rect_mainsp_nao_annual_and_depths[country_code=="BEL" & fishing_tech=="TBB" & vessel_length=="VL2440" & year=="2018" & sub_reg=="27.8.b",]
dd[,sum(fditotfishdays, na.rm=TRUE),]
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
years <- as.character(2018:2021)
# caution: merging for 2021 is leaking, likely because incomplete AER data (if AER 2022 is used)
# add primary keys before merging - adapt the key on the fly to avoid loosing effort or landings....
# ROBUST MERGING: catch for leaks and adapt the key on the fly
aer_all_y <- NULL
fdi_all_y <- NULL
aer_y_leftover <- NULL
aer_y_leftover2 <- NULL
aer_y_leftover3 <- NULL
fdi_y_leftover <- NULL
fdi_y_leftover2 <- NULL
fdi_y_leftover3 <- NULL
tracked_leaks <- NULL
for(y in years)
{
# Add a default key (i.e. year-quarter-Level5-icesrect)
fdi_y <- fdi_land_effort_rect_mainsp_nao_annual_and_depths[year==y,]
fdi_y$key <- paste(fdi_y$year, fdi_y$country_code, fdi_y$vessel_length, fdi_y$fishing_tech, fdi_y$sub_reg, sep="_") # full key on fdi
aer_y <- stecf_fleetdata_with_kiloandeffort_andcostratios_nao[year==y,]
aer_y$key <- paste(aer_y$year, aer_y$country_code, aer_y$vessel_length, aer_y$fishing_tech, aer_y$sub_reg, sep="_") # full key on aer
aer_y$chunk <- 0
fdi_y$chunk <- 0
m1 <- unique(fdi_y$key)
m2 <- unique(aer_y$key)
fdi_y_met_not_in_aer <- m1[!m1 %in% m2]
fdi_y_met_in_aer <- m1[m1 %in% m2]
aer_y_leftover <- aer_y[!aer_y$key %in% fdi_y_met_in_aer,]
fdi_y_leftover <- fdi_y[!fdi_y$key %in% fdi_y_met_in_aer,]
aer_y_main <- aer_y[aer_y$key %in% fdi_y_met_in_aer,]
fdi_y_main <- fdi_y[fdi_y$key %in% fdi_y_met_in_aer,]
aer_y_main$chunk <- 1 # coding
fdi_y_main$chunk <- 1 # coding