-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_CouplingAER2VMS.r
1743 lines (1337 loc) · 96.7 KB
/
02_CouplingAER2VMS.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 VMS
# AND DISPACH AER LANDINGS, EFFORT AND COSTS
# ON VMS C-SQUARE CELLS PER YEAR, COUNTRY AND METIER LEVEL6
# Author: Francois Bastardie (May 2023)
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
setwd(file.path("C:","Users","fbas","Documents", "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")]
# check numbers
dd <- stecf_fleetdata[stecf_fleetdata$supra_reg=="NAO",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018" & dd$variable_code=="totwghtlandg",]
sum(an(ddd$value)) # weight kg
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")]
# check numbers
dd <- stecf_fleetdata_with_kilo[stecf_fleetdata_with_kilo$supra_reg=="NAO",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018",]
sum(ddd$weight)
##-------
##-------
#### ### 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)
dd <- stecf_fleetdata_with_kiloandeffort
ddd <- dd[dd$country_code=="ESP" & dd$fishing_tech=="DTS" & dd$vessel_length=="VL2440" & dd$year=="2021",]
sum(ddd$aerkwFishingdays, na.rm=TRUE)
#-----------------------------------
#-----------------------------------
# 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", "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
# check numbers
dd <- agg_stecf_fleetdata_with_kiloeffort_nofs[agg_stecf_fleetdata_with_kiloeffort_nofs$supra_reg=="NAO",]
ddd <- dd[dd$country_code=="ESP" & dd$vessel_length=="VL2440" & dd$fishing_tech=="DTS" & dd$year=="2018",]
sum(ddd$weight)
#-----------------------------------
#-----------------------------------
#-----------------------------------
##-------
####### 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")
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
dd[dd$country_code=="ESP" & dd$year=="2021" & dd$vessel_length=="VL2440" & dd$supra_reg=="NAO" & dd$fishing_tech=="DTS",]
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=="2021" & AERothvars$country_code=="ESP" & AERothvars$fishing_tech=="DTS" & 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=="2021" & AER_nonvariablevars$country_code=="ESP" & AER_nonvariablevars$fishing_tech=="DTS" & 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"))
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios
dd[dd$year=="2021" & dd$country_code=="ESP" & dd$fishing_tech=="DTS" & dd$vessel_length=="VL2440" ,]
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
# 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)/1e6)), by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length")]
dd <- left_join(dat, tot)
dd$share_aerECOkwFishingdays <- (an(dd$aerECOkwFishingdays)/1e6) / an(dd$tot_aerECOkwFishingdays) # caution: a 1e6 rescaling used to avoid the too large numbers overflow
dd$aerECOkwFishingdays_perregion <- dd$share_aerECOkwFishingdays * 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$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 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 SOME VMS DATA
load(file=file.path(RinputPath, "VMS_DATA", "prodT53all_agg_FB.Rdata")) # SeaWise
head(prodT53all_agg)
#unique(prodT53all_agg$Year)
#[1] 2011 2010 2009 2013 2015 2012 2016 2018 2014 2019 2017 2020 2021
library(vmstools)
prodT53all_agg$icesname <- vmstools::ICESrectangle(prodT53all_agg)
prodT53all_agg$quarter <- factor(prodT53all_agg$Month) # init
levels(prodT53all_agg$quarter) <- c(1,1,1,2,2,2,3,3,3,4,4,4)
prodT53all_agg$quarter <- as.numeric(prodT53all_agg$quarter)
#-----------------------------------
#-----------------------------------
#-----------------------------------
# make a compatible fleet seg names with STECF AER coding
# splitting the metier data using a temp dataframe
library(stringr)
temp <- as.data.frame(str_split_fixed(prodT53all_agg$MetierL6,"_",3))
prodT53all_agg$gear <- temp[,1]
prodT53all_agg$assemblage <- temp[,2]
prodT53all_agg$meshsize <- temp[,3]
# checking gear table (VMS Data - DCF Data gears) to retrieve the AER compatible fishing_tech coding
dcf_gears <- read.csv(file=file.path(getwd(),"INPUT_DATASETS", "DCF_DATA", "codetransl_fish_tech08xx.csv"), sep = ";")
names(dcf_gears)[1]<- "gear"
names(dcf_gears)[2]<- "fishing_tech"
# joining gear table with VMS rawdata
prodT53all_agg <- left_join(prodT53all_agg, dcf_gears, by="gear")
# Retrieve the sub_reg?
data(ICESareas) # in vmstools
sq <- cbind.data.frame(unique(prodT53all_agg$Csquare), vmstools::CSquare2LonLat(unique(prodT53all_agg$Csquare), 0.05))
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("Csquare", "lat_csq", "long_csq", "idx", "sub_reg")
sq <- sq[,c("Csquare", "sub_reg")]
# Faster (but only for NS and BS): retrieve the sub_reg corresponding to the c-squares
#csquare_region_data <- read.csv(file=file.path(getwd(),"SEAWise","WP5","T 5.5 Fishable areas", "Software", "C_SQS.csv"))
#csquare_region_data <- csquare_region_data[!is.na(csquare_region_data$EcoRegion),]
#names(csquare_region_data)[1] <- "Csquare"
#names(csquare_region_data)[2] <- "ecoregion"
#names(csquare_region_data)[4] <- "sub_reg"
# joining geo information to vms data
#prodT53all_agg <- left_join(prodT53all_agg, csquare_region_data, by=c("Csquare"))
prodT53all_agg <- left_join(prodT53all_agg, sq, by=c("Csquare"))
colnames(prodT53all_agg)[colnames(prodT53all_agg)=="VesselLengthRange"] <- "vessel_length"
prodT53all_agg <- data.table(prodT53all_agg)
# aggregate VMS at Level5 (caution: all y here)
prodT53all_agg$fs_name <- paste(prodT53all_agg$fishing_tech, prodT53all_agg$vessel_length, prodT53all_agg$sub_reg, sep="_") # vms
vms <- prodT53all_agg[, .(FishingHour = sum(FishingHour, na.rm=T),
kWFishingHour = sum(kWFishingHour, na.rm=T)),
by=c("Year", "Csquare", "icesname", "SI_LATI", "SI_LONG", "MetierL6", "vessel_length", "fishing_tech", "sub_reg")] # here the month dim is lost
vms$year <- vms$Year
vms <- vms[, colnames(vms)[!colnames(vms) %in% "Year"], with=FALSE]
rm(prodT53all_agg)
gc(full=TRUE)
#-----------------------------------
#-----------------------------------
#-----------------------------------
# forsee the merging consistency and fix
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_kwdispatched # 2017-2021
dd2 <- vms[vms$year %in% c("2018","2019", "2020", "2021"),]
# inconsistent sub_reg definition found with
unique(dd$sub_reg[!dd$sub_reg %in% dd2$sub_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 vms
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"
# fix on the VMS side
dd2[dd2$sub_reg %in% c("27.3.a.20", "27.3.a.21"), "sub_reg"] <- "27.3.a"
aer_key_met <- paste(dd$year, dd$fishing_tech, dd$vessel_length, dd$sub_reg, sep="_") # aer
vms_key_met <- paste(dd2$year, dd2$fishing_tech, dd2$vessel_length, dd2$sub_reg, sep="_") # vms
not_in_aer_keys <- vms_key_met[!vms_key_met %in% aer_key_met]
not_in_aer <- unique(not_in_aer_keys)
not_in_vms_keys <- aer_key_met[!aer_key_met %in% vms_key_met]
not_in_vms <- unique(not_in_vms_keys)
# one example of unmatched seg?
dd[dd$year=="2019" & dd$fishing_tech=="DFN" & dd$vessel_length=="VL0612" & dd$sub_reg=="gsa5",] # aer
dd2[dd2$year=="2019" & dd2$fishing_tech=="DFN" & dd2$vessel_length=="VL0612" & dd2$sub_reg=="gsa5",] # vms
dd2[dd2$year=="2019" & dd2$fishing_tech=="DFN" & dd2$vessel_length=="VL0612",]
# output
stecf_fleetdata_with_kiloandeffort_andcostratios <- data.table(dd) # 2017-2021
vms_effort <- data.table(dd2) # 2018-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",])
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# 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",]
vms_effort_nao <- vms_effort#[supra_region=="NAO",]
rm(vms_effort)
gc()
# an overall check
dd <- stecf_fleetdata_with_kiloandeffort_andcostratios_nao[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("value","weight","aerkwFishingdays", "aerfishingdays", "aerdaysatsea"),
keyby=c("year")]
dd <- knitr::kable(as.data.frame(dd), format = "html")
library(readr)
readr::write_file(dd, file.path(ROutputPathToDatasets, "an_overall_check0.html"))
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
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
vms_all_y <- NULL
aer_y_leftover <- NULL
aer_y_leftover2 <- NULL
aer_y_leftover3 <- NULL
vms_y_leftover <- NULL
vms_y_leftover2 <- NULL
vms_y_leftover3 <- NULL
tracked_leaks <- NULL
for(y in years)
{
# Add a default key (i.e. year-quarter-Level5-icesrect)
vms_y <- vms_effort_nao[year==y,]
vms_y$key <- paste(vms_y$year, vms_y$vessel_length, vms_y$fishing_tech, vms_y$sub_reg, sep="_") # full key on vms
aer_y <- stecf_fleetdata_with_kiloandeffort_andcostratios_nao[year==y,]
aer_y$key <- paste(aer_y$year, aer_y$vessel_length, aer_y$fishing_tech, aer_y$sub_reg, sep="_") # full key on aer
aer_y$chunk <- 0
vms_y$chunk <- 0
m1 <- unique(vms_y$key)
m2 <- unique(aer_y$key)
vms_y_met_not_in_aer <- m1[!m1 %in% m2]
vms_y_met_in_aer <- m1[m1 %in% m2]
aer_y_leftover <- aer_y[!aer_y$key %in% vms_y_met_in_aer,]
vms_y_leftover <- vms_y[!vms_y$key %in% vms_y_met_in_aer,]
aer_y_main <- aer_y[aer_y$key %in% vms_y_met_in_aer,]
vms_y_main <- vms_y[vms_y$key %in% vms_y_met_in_aer,]
aer_y_main$chunk <- 1 # coding
vms_y_main$chunk <- 1 # coding
# repeat with a less constraining key to catch the left-over records:
if(nrow(aer_y_leftover)!=0 && nrow(vms_y_leftover)!=0)
{
aer_y_leftover$key <- paste(aer_y_leftover$year, aer_y_leftover$vessel_length, aer_y_leftover$fishing_tech, sep="_") # aer
vms_y_leftover$key <- paste(vms_y_leftover$year, vms_y_leftover$vessel_length, vms_y_leftover$fishing_tech, sep="_") # vms
m1 <- unique(vms_y_leftover$key)
m2 <- unique(aer_y_leftover$key)
vms_y_met_not_in_aer <- m1[!m1 %in% m2]
vms_y_met_in_aer <- m1[m1 %in% m2]
aer_y_leftover2 <- aer_y_leftover[!aer_y_leftover$key %in% vms_y_met_in_aer,]
vms_y_leftover2 <- vms_y_leftover[!vms_y_leftover$key %in% vms_y_met_in_aer,]
aer_y_leftover <- aer_y_leftover[aer_y_leftover$key %in% vms_y_met_in_aer,]
vms_y_leftover <- vms_y_leftover[vms_y_leftover$key %in% vms_y_met_in_aer,]
#destroy the sub_reg dim in aer
aer_y_leftover_agg1 <- aer_y_leftover[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("value", "weight", "aerkwFishingdays", "aerfishingdays", "aerdaysatsea", "aerECOkwFishingdays", "aerECOkwFishingdays_perregion"),
by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length", "key")]
aer_y_leftover_agg2 <- aer_y_leftover[,lapply(.SD, mean, na.rm=TRUE),
.SDcols=c("enerbykwfishdy","wagebyinc","repbykwfishday","varbykwfishday"),
by=c("year", "supra_reg", "country_code", "fishing_tech", "vessel_length", "key")]
aer_y_leftover <- cbind(aer_y_leftover_agg1, aer_y_leftover_agg2[,-c(1:6)])
aer_y_leftover$chunk <- 2 # coding
#destroy the sub_reg dim in vms
vms_y_leftover <- vms_y_leftover[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("FishingHour", "kWFishingHour"),
by=c("Csquare", "icesname", "SI_LATI", "SI_LONG", "MetierL6", "vessel_length", "fishing_tech", "year", "key")]
vms_y_leftover$chunk <- 2 # coding
if(nrow(aer_y_leftover2)) warning("First attempt: There aer records left here...")
if(nrow(vms_y_leftover2)) warning("First attempt: There vms records left here...")
# repeat with a less constraining key to catch the left-over records:
if(nrow(aer_y_leftover2)!=0 && nrow(vms_y_leftover2)!=0){
aer_y_leftover2$key <- paste(aer_y_leftover2$year, aer_y_leftover2$vessel_length, sep="_") # aer
vms_y_leftover2$key <- paste(vms_y_leftover2$year, vms_y_leftover2$vessel_length, sep="_") # vms
m1 <- unique(vms_y_leftover2$key)
m2 <- unique(aer_y_leftover2$key)
vms_y_met_not_in_aer <- m1[!m1 %in% m2]
vms_y_met_in_aer <- m1[m1 %in% m2]
aer_y_leftover3 <- aer_y_leftover2[!aer_y_leftover2$key %in% vms_y_met_in_aer,]
vms_y_leftover3 <- vms_y_leftover2[!vms_y_leftover2$key %in% vms_y_met_in_aer,]
aer_y_leftover2 <- aer_y_leftover2[aer_y_leftover2$key %in% vms_y_met_in_aer,]
vms_y_leftover2 <- vms_y_leftover2[vms_y_leftover2$key %in% vms_y_met_in_aer,]
#destroy the sub_reg and fishing_tech dims in aer
aer_y_leftover2_agg1 <- aer_y_leftover2[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("value", "weight", "aerkwFishingdays", "aerfishingdays", "aerdaysatsea", "aerECOkwFishingdays", "aerECOkwFishingdays_perregion"),
by=c("year", "supra_reg", "country_code", "vessel_length", "key")]
aer_y_leftover2_agg2 <- aer_y_leftover2[,lapply(.SD, mean, na.rm=TRUE),
.SDcols=c("enerbykwfishdy","wagebyinc","repbykwfishday","varbykwfishday"),
by=c("year", "supra_reg", "country_code", "vessel_length", "key")]
aer_y_leftover2 <- cbind(aer_y_leftover2_agg1, aer_y_leftover2_agg2[,-c(1:6)])
aer_y_leftover2$chunk <- 3 # coding
#destroy the sub_reg and fishing_tech dims in vms
vms_y_leftover2 <- vms_y_leftover2[,lapply(.SD, sum, na.rm=TRUE),
.SDcols=c("FishingHour", "kWFishingHour"),
by=c("Csquare", "icesname", "SI_LATI", "SI_LONG", "MetierL6", "vessel_length", "year", "key")]
vms_y_leftover2$chunk <- 3 # coding
if(nrow(aer_y_leftover3)) warning("Second attempt: There aer records left here...") # lost
if(nrow(vms_y_leftover3)) warning("Second attempt: There vms records left here...") # lost
}
}
# bind # fill=TRUE to add missing columns and fill out with NAs
aer_left <- rbind(aer_y_main, aer_y_leftover, aer_y_leftover2, fill=TRUE)
vms_left <- rbind(vms_y_main, vms_y_leftover, vms_y_leftover2, fill=TRUE)
# document effort leak this y
d <- function(x) as.data.frame(x)
dd1 <-rbind.data.frame(
"init"=d(vms_y[ ,.(vmstotfishhours=sum(FishingHour )),]), # initial tot eff vms met in vms
"year, vessel_length, fishing_tech, sub_reg"=d(vms_y_main[ ,.(vmstotfishhours =sum(FishingHour )),]), # year, vessel_length, fishing_tech, sub_reg # full key
"year, vessel_length, fishing_tech"=d(vms_y_leftover[ ,.(vmstotfishhours =sum(FishingHour )),]), # year, vessel_length, fishing_tech
"year, vessel_length"=d(vms_y_leftover2[ ,.(vmstotfishhours =sum(FishingHour )),]), # year, vessel_length
"unfortunate lost"=d(vms_y_leftover3[ ,.(vmstotfishhours =sum(FishingHour )),]), # the remaining: not matched...
"finally left"=d(vms_left[ ,.(vmstotfishhours =sum(FishingHour )),]) # tot eff vms left
)
# document landings leak this y
d <- function(x) as.data.frame(x)
dd2 <-rbind.data.frame(
"init"=d(vms_y[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]), # initial tot in vms
"year, vessel_length, fishing_tech, sub_reg"=d(vms_y_main[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]), # year, vessel_length, fishing_tech, sub_reg # full key
"year, vessel_length, fishing_tech"=d(vms_y_leftover[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]), # year, vessel_length, fishing_tech
"year, vessel_length"=d(vms_y_leftover2[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]), # year, vessel_length
"unfortunate lost"=d(vms_y_leftover3[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]), # the remaining: not matched...
"finally left"=d(vms_left[ ,.(vmstotKwfishhours=sum(an(kWFishingHour))),]) # tot in vms left
)
tracked_leaks <- rbind.data.frame (tracked_leaks, cbind.data.frame(y, dd1, dd2))
aer_all_y <- rbind(aer_all_y, aer_left)
vms_all_y <- rbind(vms_all_y, vms_left)
} # end y
# check what is left, what is lost...
print(tracked_leaks)
dd <- knitr::kable(as.data.frame(tracked_leaks), format = "html")
library(readr)
readr::write_file(dd, file.path(ROutputPathToDatasets, "tracked_leaks_AER_to_VMS.html"))
# check
vms_all_y[, .(vmstotKwfishhours = sum(an(kWFishingHour), na.rm=T),
vmstotfishhours = sum(an(FishingHour), na.rm=T)),
by=c("year", "chunk")]
aer_all_y[, .(weight = sum(an(weight), na.rm=T),
value = sum(an(value), na.rm=T)),
by=c("year", "chunk")]
#-----------------------------------
#-----------------------------------
#-----------------------------------
# calculate share of countries on effort
aa <- data.table(aer_all_y)
agg_kwfdays <- aa[,.(tot_kwFishingdays=sum(aerECOkwFishingdays_perregion)),by=c("year", "key")]
aer_all_y <- merge(aer_all_y, agg_kwfdays,
by= c("year", "key"))
aer_all_y$country_share_effort <-
aer_all_y$aerECOkwFishingdays_perregion/aer_all_y$tot_kwFishingdays
# caution with Inf on aerECOkwFishingdays_perregion....REDO AFTER SCALING DOWN LARGE NUMBERS IF IT IS THE CASE.
aer_all_y[country_code=="FRA" & vessel_length=="VL1218" & year=="2020",]
#-----------------------------------
#-----------------------------------
#-----------------------------------
# MERGE vms WITH AER, AND DISPACH vms EFFORT AND AER kwfishingdays and COSTS ON vms C-SQUARE CELLS PER YEAR, COUNTRY AND METIER LEVEL6
save(vms_effort_nao, file=file.path(ROutputPathToDatasets, "vms_2018_2021_in_NAO_before_merging.RData"))
head(vms_effort_nao)
dispatched_aer <- list()
for (y in as.character(years))
{
cat(paste("y", y, "\n"))
# landings
aer_y <- aer_all_y[aer_all_y$year==y,]
aer_y[,.(tot_kg=sum(an(weight))),] # check
# effort
vms_y <- vms_all_y[vms_all_y$year==y,]
vms_y[,.(tot_totfishhours=sum(FishingHour)),] # check
# get a share_effort for dispatching data (caution, vms data are species explicit data)
sum_effort_y_per_key <- vms_y[, .(tot_totfishhours = sum(FishingHour, na.rm=T)), by=c("key")] # removing the c-square dimension here...
sum_effort_y_per_key_level6 <- vms_y[, .(tot_totfishhours_met = sum(FishingHour, na.rm=T)), by=c("MetierL6", "key")] # removing the c-square dimension here...
#sum_effort_y_per_key_level6_inzone[,.(tot_effort=sum(tot_totfishhours_met_csquare)),] # check
vms_y_e <- merge(vms_y, sum_effort_y_per_key, by=c("key"))
vms_y_e <- merge(vms_y_e, sum_effort_y_per_key_level6, by=c("MetierL6", "key"))
vms_y_e$share_effort_level6 <- vms_y_e$tot_totfishhours_met / vms_y_e$tot_totfishhours # for dispatching depending on the contribution of that metier to the total effort in this key
vms_y_e$share_effort_level6_csquare <- vms_y_e$FishingHour / vms_y_e$tot_totfishhours_met # for dispatching depending on the contribution of that cell to the total effort in that key-metier-zone
vms_y_e$year <- as.character(vms_y_e$year)
# a check column: "md5" should be equal to the sum at the key level i.e. "vmstotfishhours"
vms_y_e$md5 <- vms_y_e$tot_totfishhours * vms_y_e$share_effort_level6 * vms_y_e$share_effort_level6_csquare
vms_y_e[,.(tot_totfishhours=sum(FishingHour)),] # check
# clean a bit the data.table to save memory!
rm_col <- c("md5", "tot_totfishhours", "tot_totfishhours_met", "SI_LATI", "SI_LONG")
vms_y_e <- vms_y_e[, (rm_col):=NULL]
rm_col <- c("fishing_tech", "supra_reg", "vessel_length")
aer_y <- aer_y[, (rm_col):=NULL]
# check for unexpected duplicates?
#a_df <- as.data.frame(vms_y_e)
#a_tab <- table(paste0(a_df$key, a_df$Csquare, a_df$MetierL6))
#a_tab[a_tab>1]
# a check before the merging as such
if(FALSE){
nonvariablevars <- AER_nonvariablevars[AER_nonvariablevars$supra_reg=="NAO",]
temp <- as.data.frame(stringr::str_split_fixed(as.character(unlist(aer_y [,"key"])),"_",4))
aer_y[, "vessel_length"] <- temp[,2]
aer_y[, "fishing_tech"] <- temp[,3]
dd <- left_join (aer_y, nonvariablevars, by=c("year", "country_code", "fishing_tech", "vessel_length"))
dd$varcosts <- dd$aerECOkwFishingdays_perregion * (dd$enerbykwfishdy + dd$repbykwfishday + dd$varbykwfishday)
# check numbers...
ddd <- dd[dd$year=="2019" & dd$country_code=="BEL" & dd$fishing_tech=="TBB" & dd$vessel_length=="VL2440",]
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
ddd[ddd$sub_reg=="27.4.b","weight"] #=> 4204869
sum(ddd[,"weight"]) #=> 13386476
} # end FALSE
# merge (by chunk, to avoid memory issue)
gc(full=TRUE); rm(merged)
keys <- unlist(c(unique(aer_y[aer_y$chunk==1, "key"]))) # needed for the trick to chunk...
idx <- c(floor(seq(1,length(keys),by=length(keys)/10)),length(keys))
chunk1_1 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[1:idx[2]] ,], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_2 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[2]+1):idx[3]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_3 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[3]+1):idx[4]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_4 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[4]+1):idx[5]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_5 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[5]+1):idx[6]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_6 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[6]+1):idx[7]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_7 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[7]+1):idx[8]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_8 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[8]+1):idx[9]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_9 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[9]+1):idx[10]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk1_10 <- merge(aer_y[aer_y$chunk==1 & aer_y$key %in% keys[(idx[10]+1):idx[11]], ], vms_y_e[vms_y_e$chunk==1,], by.x="key", by.y="key", allow.cartesian=TRUE)
keys <- unlist(c(unique(aer_y[aer_y$chunk==2, "key"]))) # needed for the trick to chunk...
if(length(keys)>0) idx <- c(floor(seq(1,length(keys),by=length(keys)/5)),length(keys))
chunk2_1 <- merge(aer_y[aer_y$chunk==2 & aer_y$key %in% keys[1:idx[2]],], vms_y_e[vms_y_e$chunk==2,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk2_2 <- merge(aer_y[aer_y$chunk==2 & aer_y$key %in% keys[(idx[2]+1):idx[3]],], vms_y_e[vms_y_e$chunk==2,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk2_3 <- merge(aer_y[aer_y$chunk==2 & aer_y$key %in% keys[(idx[3]+1):idx[4]],], vms_y_e[vms_y_e$chunk==2,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk2_4 <- merge(aer_y[aer_y$chunk==2 & aer_y$key %in% keys[(idx[4]+1):idx[5]],], vms_y_e[vms_y_e$chunk==2,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk2_5 <- merge(aer_y[aer_y$chunk==2 & aer_y$key %in% keys[(idx[5]+1):idx[6]],], vms_y_e[vms_y_e$chunk==2,], by.x="key", by.y="key", allow.cartesian=TRUE)
keys <- unlist(c(unique(aer_y[aer_y$chunk==3, "key"]))) # needed for the trick to chunk...
if(length(keys)>0) idx <- c(floor(seq(1,length(keys),by=length(keys)/2)),length(keys))
chunk3_1 <- merge(aer_y[aer_y$chunk==3 & aer_y$key %in% keys[1:idx[2]],], vms_y_e[vms_y_e$chunk==3,], by.x="key", by.y="key", allow.cartesian=TRUE)
chunk3_2 <- merge(aer_y[aer_y$chunk==3 & aer_y$key %in% keys[(idx[2]+1):idx[3]],], vms_y_e[vms_y_e$chunk==3,], by.x="key", by.y="key", allow.cartesian=TRUE)
gc(full=TRUE); rm(merged)
cols <- c("key", "country_code","year.x","value", "weight","aerkwFishingdays", "aerdaysatsea", "aerECOkwFishingdays_perregion",
"FishingHour", "kWFishingHour","enerbykwfishdy","wagebyinc","repbykwfishday","varbykwfishday","country_share_effort",
"MetierL6","Csquare","icesname","share_effort_level6", "share_effort_level6_csquare")
merged <- rbind.data.frame(chunk1_1[,..cols], chunk1_2[,..cols], chunk1_3[,..cols], chunk1_4[,..cols], chunk1_5[,..cols], chunk1_6[,..cols], chunk1_7[,..cols], chunk1_8[,..cols], chunk1_9[,..cols], chunk1_10[,..cols],
chunk2_1[,..cols], chunk2_2[,..cols], chunk2_3[,..cols], chunk2_4[,..cols], chunk2_5[,..cols],
chunk3_1[,..cols], chunk3_2[,..cols])
gc(full=TRUE)
# dispatch AER vars over ctry-level6-csquare combinaisons depending on share alloc keys. Caution: no species info in AER
#(caution: merged$country_share_effort should only be used if dispatching the vms data....)
merged$FishingHour <- merged$FishingHour * merged$country_share_effort
a_dispatcher_key <- merged$country_share_effort * merged$share_effort_level6 * merged$share_effort_level6_csquare # because VMS are metier-based data on Csquare
merged$landings_aer_in_ctry_level6_csquare <- merged$weight * a_dispatcher_key # dispatch on cell
merged$value_aer_in_ctry_level6_csquare <- merged$value * a_dispatcher_key # dispatch on cell
merged$KwFishingdays_aer_in_ctry_level6_csquare <- an(merged$aerECOkwFishingdays_perregion) * a_dispatcher_key # dispatch on cell
merged$daysatsea_aer_in_ctry_level6_csquare <- an(merged$aerdaysatsea) * a_dispatcher_key # dispatch on cell
# check the overall conservation of FishingHour
merged[,.(tot_totfishhours=sum(FishingHour, na.rm=TRUE)),] # check
vms_y[,.(tot_totfishhours=sum(FishingHour, na.rm=TRUE)),] # check
# check for conservation of effort per fs
if(FALSE){
dd <- vms_y_e
for(k in unique(dd$key)){
aa <- dd[dd$year=="2020" & dd$key==k,]
ddd <- aer_y
temp <- as.data.frame(str_split_fixed(ddd$key,"_",4))
ddd$vessel_length <- temp[,2]
ddd$fishing_tech <- temp[,3]
bb <- ddd[ddd$year=="2020" & ddd$key==k,]
cc <- merge(bb, aa, by.x="key", by.y="key", allow.cartesian=TRUE)
if (round(sum(aa$FishingHour)) != round(sum(cc$FishingHour*cc$country_share_effort))) {
cat(paste("no conservation for", k, "...break\n"))
break
}
}
} # end FALSE
# check
library(stringr)
temp <- as.data.frame(stringr::str_split_fixed(as.character(unlist(merged[,"key"])),"_",4))
dd <- merged[merged$country_code=="BEL" & temp[,3]=="TBB" & temp[,2]=="VL2440" & merged$y=="2019",]
ddd <- unique(dd, by = c("key", "country_code", "Csquare"))# remove spp dim
ddd[,sum( KwFishingdays_aer_in_ctry_level6_csquare, na.rm=TRUE),] #=> 14230352
temp <- as.data.frame(stringr::str_split_fixed(as.character(unlist(aer_y[,"key"])),"_",4))
dd <- aer_y[aer_y$country_code=="BEL" & temp[,3]=="TBB" & temp[,2]=="VL2440" & aer_y$y=="2019",]
dd[,sum(aerECOkwFishingdays, na.rm=TRUE),]
dd[,sum(aerECOkwFishingdays, na.rm=TRUE),]*1.727336 #=> 130421746
dd <- AERcosts[AERcosts$country_code=="BEL" & AERcosts$fishing_tech=="TBB" & AERcosts$vessel_length=="VL2440" & AERcosts$y=="2019",]