-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickmap2020.r
1301 lines (1123 loc) · 64.9 KB
/
quickmap2020.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
setwd (file.path("D:","FBA","BENTHIS_2020")) # adapt to your need
##!!!!!!!!!!!##
##!!!UTILS!!!##
##!!!!!!!!!!!##
legend.gradient2 <-
function (pnts, cols = heat.colors(100), limits = c(0, 1), title = "Legend", legend="",
...)
{
pnts = try(as.matrix(pnts), silent = T)
if (!is.matrix(pnts))
stop("you must have a 4x2 matrix")
if (dim(pnts)[1] != 4 || dim(pnts)[2] != 2)
stop("Matrix must have dimensions of 4 rows and 2 columms")
if (length(cols) < 2)
stop("You must have 2 or more colors")
rect(min(pnts[, 1])-0.5, min(pnts[, 2])-0.5, max(pnts[, 1])+2, max(pnts[, 2])+0.5, col="white", border=NA)
yvals = seq(min(pnts[, 2]), max(pnts[, 2]), length = length(cols) +
1)
for (i in 1:length(cols)) {
polygon(x = pnts[, 1], y = c(yvals[i], yvals[i], yvals[i +
1], yvals[i + 1]), col = cols[i], border = F)
}
text(max(pnts[, 1]), min(pnts[, 2]), labels = limits[1],
pos = 4, ...)
text(max(pnts[, 1]), max(pnts[, 2]), labels = limits[2],
pos = 4, ...)
start_pos <- (min(pnts[, 2])+((max(pnts[, 2])-min(pnts[, 2]))/length(legend))/2)
for (i in 1: length(legend)){
text(max(pnts[, 1])-0, start_pos + ((i-1) * ((max(pnts[, 2])-min(pnts[, 2]))/length(legend)) ), labels = legend[i],
pos = 4, ...)
#browser()
}
rect(min(pnts[, 1]), max(pnts[, 2])+0.1, min(pnts[, 1])+8, max(pnts[, 2])+1, col="white", border=NA)
text(min(pnts[, 1])-0.1, max(pnts[, 2])-0.1, labels = title, adj = c(0,
-1), ...)
}
##!!!!!!!!!!!!!!!!!!!##
##!!!CORE FUNCTION!!!##
##!!!!!!!!!!!!!!!!!!!##
quickmap <- function(namefile = paste0("LE_KG_COD_2019", ".tif"),
aggResult = aggResult,
# file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
#nameobj = "aggResult",
a_met = NULL,
nametype = "LE_KG_COD",
a_unit = 1,
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= readOGR(file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp")),
spatial_polys=NULL,
a_width=2000,
a_height=2000,
output_dir=file.path(getwd(), "outputs2020", "output_plots" )
){
dir.create(output_dir, recursive=TRUE)
an <- function(x) as.numeric(as.character(x))
#load(a_file)
#my_data <- get(nameobj)
if(!is.null(a_met)) aggResult <- aggResult[aggResult$LE_MET==a_met,]
# caution: test if early stop.
do_it <- FALSE
cat("look into ", namefile, " and ", nametype[1],"\n")
if( grepl("sp_with_max_vpuf",nametype[1]) || grepl("sp_with_max_cpue",nametype[1]) || grepl("sp_with_max_cpuf",nametype[1]) || grepl("sp_with_max_vpufswa",nametype[1])
|| any(aggResult[!is.na(aggResult[,nametype[1]]),nametype[1]]>0.5))
{
do_it <- TRUE
}
if(!do_it){
cat("irrelevant combination of met-sp \n")
return()
} # early stop to avoid plotting for a irrelevant combination of met-sp
# viable namefile
namefile <- gsub("<","u", namefile)
namefile <- gsub(">","o", namefile)
namefile <- gsub("\\[","", namefile)
namefile <- gsub("\\)","", namefile)
namefile <- gsub("\\,","-", namefile)
multi1 <- 1 ; multi2 <- 1
if(length(nametype)==4) {multi1 <- 2 ; multi2 <- 2}
if(length(nametype)==3) {multi1 <- 3 ; multi2 <- 1}
if(length(nametype)==1) {multi1 <- 1 ; multi2 <- 1}
tiff(filename=file.path(output_dir, namefile), width = a_width*multi1, height = a_height*multi2,
units = "px", pointsize = 12, res=600, compression = c("lzw"))
par(mfrow=c(1,length(nametype)))
if(length(nametype)==4) par(mfrow=c(2,2))
for(itype in 1: length(nametype))
{
a_nametype <- nametype[itype]
my_data <- aggResult
if(is.numeric(my_data[1, a_nametype]))
{
my_data[,nametype] <- an(my_data[, a_nametype])/a_unit
}
my_data <- my_data[!is.na(my_data[,long]),]
my_data <- my_data[!is.na(my_data[,lat]),]
if(use_fao_areas){
library(rgdal)
an_area <- unlist(lapply(strsplit(a_met, split="_"), function(x) x[2]))
if(an_area=="OTHER" || is.na(an_area)) an_area <- c("27.3","27.4")
fao_areas <- fao_areas[ fao_areas$F_SUBAREA %in% c(an_area),]
xlims <- c(extent(fao_areas)@xmin,extent(fao_areas)@xmax) # override
ylims <- c(extent(fao_areas)@ymin,extent(fao_areas)@ymax) # override
}
my_data <- my_data[my_data[,long]>=xlims[1] & my_data[,long]<=xlims[2],]
my_data <- my_data[my_data[,lat]>=ylims[1] & my_data[,lat]<=ylims[2],]
print(head(my_data,2))
if(nrow(my_data)==0) return(0)
#-------------------------
# possible post treatment on data (TODO: REVISE IF REQUIRED)
if(FALSE){
# compute the subsurface swept area
matprop <- read.table(file=file.path("C:", "BENTHIS", "data_gear_spec_questionnaire",
"Subsurface_proportion_by_metier_Figure10_Eigaard_et_al.csv"),
header=TRUE, sep=";")
#=> small discrepencies in naming the metier between the paper and the BENTHIS workflow corrected by hand. (e.g. OT_MIX_NEP=>OT_MIX_CRU, etc.)
rownames(matprop) <- matprop$LE_MET
my_data$multiplier <- factor(my_data$LE_MET)
levels(my_data$multiplier) <- matprop[levels(my_data$multiplier), "Subsurface"]
}
#-------------------------
#-------------------------
# export to ArcGIS, if it comes it is more convenient for the final rendering
if(FALSE){
library(raster)
xrange <- range(aggResult$CELL_LONG, na.rm=TRUE) # ALL
yrange <- range(aggResult$CELL_LATI, na.rm=TRUE) # ALL
r <- raster(xmn=xrange[1], xmx=xrange[2], ymn=yrange[1], ymx=yrange[2], res=c(0.0167,0.0167), crs=CRS("+proj=longlat +datum=WGS84"))
some_coords <- SpatialPoints(cbind(SI_LONG=aggResult$CELL_LONG, SI_LATI=aggResult$CELL_LATI))
# caution: because three years then we need to divide estimates by 3
rstr <- rasterize(x=some_coords, y=r, field=aggResult$SWEPT_AREA_KM2/3, fun="sum")
plot(rstr, xlim=c(-5,13), ylim=c(53,60))
a <- area(rstr)
rstr_std <- rstr/a # transform to SAR
# equivalent to:
# tacsatSweptArea$cell_area <- (cos(tacsatSweptArea$CELL_LATI *pi/180) * 111.325 )/60 * (111/60)
# tacsatSweptArea[,c("SWEPT_AREA_KM2","SWEPT_AREA_KM2_LOWER", "SWEPT_AREA_KM2_UPPER")] <-
# tacsatSweptArea[,c("SWEPT_AREA_KM2","SWEPT_AREA_KM2_LOWER", "SWEPT_AREA_KM2_UPPER")] / tacsatSweptArea$cell_area # standardize
rstr_eea <- projectRaster(rstr, crs="+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs")
rstr_eea[is.na(rstr_eea)] <- -999
rstr_eea[rstr_eea<0.001] <- -999
writeRaster(rstr_eea, file.path(getwd(), "outputs2020", "output_plots","AnnualAgg2RasterEEA"), format = "GTiff")
##
#ArcGIS 10.1 Workflow for producing the BENTHIS WP2 map final polishing:
#1- Import XY AggResult.txt into ArcGIS in Layers>Add Data, click on the imported data and define X, Y and Z field, and define basic World projection WGS84 (put this system in Favorite). [bug: You might encounter some trouble if not integer values in the Z column i.e. <Null> fields...then round the values before importing]
#2- Import ne_50m_ocean.shp – [optional: draw a rectangle to clip in Custumize>toolbars>draw and save the rectangle as .shp by clicking right on Layers in the Table of Contents and then >Convert Graphics to Features. Then in ArcToolbox>Clip...clip the ocean with the rectangle shape]
#3- Geoprocessing> ArcToolBox >Conversion Tools>to Raster> Point to Raster for rasterizing the XY layer e.g. with 0.0167 degree i.e. 1 minute
#4- In Data Management Tools, reprojection of the raster layer with projection>Raster>project and use the EEA proj definition (Lambert Azimuthal Equal Area, - put this system in Favorite)
#5- In Data Management Tools, reprojection of the feature shape layer with projection>Feature> project and use the EEA proj definition (Lambert Azimuthal Equal Area)
#6- Define the coordinate system of the Layers to be the EEA projection by clicking right on Layers.
#7- For a given layer>properties, define classification for the symbolisation e.g. streched and manual breaks...
#8- Add a graticule (will be only visible in layout mode) in Layers>properties>grid – close and reedit the graticule to adapt.
#9- Add a legend (in layout mode) with menu>Insert>legend – dont forget to look into “style”
#10- T copy/paste the symbology from one layer to the next use the ArcTools>data management tools>Layers and table views>Apply symbology from layers
#11- For layers with labels, click right on the layer and click on Label Features. To edit the lable use menu>Custumize>Toolbars>Label>Managing labels
#12- Export a tif with menu>File>export map in lwz compression and in 500 dpi
#13- To re-apply symbology of a raster from an external project, first save the raster layer as a layer file and then import the symbology from the layer i.e. to save as a layer file right click on the layer in the table of contents and select 'save as layer file'.To import the symbology click on the open folder in the raster properties dialog.
#Layer query is:
#"Countrynam" = 'Belgium' OR "Countrynam" = 'DE' OR "Countrynam" = 'Denmark' OR "Countrynam" = 'Faroe Islands' OR "Countrynam" = 'Greece' OR "Countrynam" = 'Ireland' OR "Countrynam" = 'Isle of Man' OR "Countrynam" = 'Italy' OR "Countrynam" = 'Netherlands' OR "Countrynam" = 'Norway' OR "Countrynam" = 'Portugal' OR "Countrynam" = 'Sweden' OR "Countrynam" = 'United Kingdom'
#Color scale
# R0 G132 B168 – R142 G189 B181 – R255 G255 B191 – R 222 G130 B80 – R168 G0 B0
#To do some operations from raster layerss then we need the Spatial Analyst Tools extension:
#1- Enable it by ticking the box in Custumize>Extensions (!)
#2- Use it in Geoprocessing>ArcToolBox>Spatial AnalystTools>Map Algebra> Raster calculator e.g. (100*(sce1/baseline) )-100
}
# get the spatial extent of the variable distribution!!
#where_there_is_effort <- my_data[!is.na(my_data[, nametype]),]
#where_there_is_effort <- where_there_is_effort [as.numeric(as.character(where_there_is_effort[, nametype]))>10,] # cuation: a threshold in hour here. And also scale dependent (here grid 0.05 degress)....
#where_there_is_effort <- where_there_is_effort[!duplicated(where_there_is_effort$c_square),]
#sum(where_there_is_effort[, "cell_area"])
# ...and map
Satellite.Palette.baseline <-colorRampPalette(c("cyan","aquamarine","orange","red"))
if( grepl("FPUCallsp",a_nametype) || grepl("FPUVallsp",a_nametype)) Satellite.Palette.baseline <- colorRampPalette(c("red","orange","aquamarine","cyan")) # reverse palette
if(a_nametype %in% c('SWEPT_AREA_KM2', 'SWEPT_AREA_KM2_LOWER', 'SWEPT_AREA_KM2_UPPER')){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(-1.5, 2.5, by=0.3)),1), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, 0.1, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(-1.5, 2.5, by=0.3)),1), 10000) # less than 1 minute i.e. 0.01 degree
}
if(length(grep("LE_KG", a_nametype)>0)){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(3, 8, by=0.6)),1), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, round(exp(seq(3, 8, by=0.6)),1), 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(3, 8, by=0.6)),1), 10000) # less than 1 minute i.e. 0.01 degree
}
if(grepl("CPUF",a_nametype) || grepl("VPUE",a_nametype) || grepl("VPUF",a_nametype) ||
grepl("CPUEallsp",a_nametype) || grepl("CPUFallsp",a_nametype) || grepl("VPUEallsp",a_nametype) || grepl("VPUFallsp",a_nametype)){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 1.7, by=0.25)),2), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 1.7, by=0.25)),2), 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(-1, 1.7, by=0.25)),2), 10000) # less than 1 minute i.e. 0.01 degree
# c(0, round(exp(seq(-0, 4, by=0.5)),0), 10000)
}
if(grepl("CPUE",a_nametype) ){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(-0, 4, by=0.5)),0), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, round(exp(seq(-0, 4, by=0.5)),0), 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(-0, 4, by=0.5)),0), 10000) # less than 1 minute i.e. 0.01 degree
# c(0, round(exp(seq(-0, 4, by=0.5)),0), 10000)
}
if(a_met=="PelagicGears" && (grepl("FPUCallsp",a_nametype) || grepl("FPUVallsp",a_nametype))){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # less than 1 minute i.e. 0.01 degree
# c(0, round(exp(seq(-1, 1.7, by=0.3)),1), 10000)
}
if(a_met=="BottomContactingGears" && (grepl("FPUCallsp",a_nametype) || grepl("FPUVallsp",a_nametype))){
if(grid_agg_res== (1/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # if 1 minute i.e. 0.16 degree
if(grid_agg_res== (3/60)) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # if 3 minutes i.e. 0.05 degrees
if(grid_agg_res== 0.01) the_breaks_baseline<- c(0, round(exp(seq(-1, 0.8, by=0.15)),2), 10000) # less than 1 minute i.e. 0.01 degree
# c(0, round(exp(seq(-0, 1.6, by=0.2)),1), 10000)
}
#-------------------------
a_func <- "sum"
if( grepl("FPUCallsp",a_nametype) || grepl("FPUVallsp",a_nametype) || grepl("CPUE",a_nametype) || grepl("CPUF",a_nametype) || grepl("VPUE",a_nametype) || grepl("VPUF",a_nametype) ||
grepl("CPUEallsp",a_nametype) || grepl("CPUFallsp",a_nametype) || grepl("VPUEallsp",a_nametype) || grepl("VPUFallsp",a_nametype)) a_func <- "mean" # do an average in cells when ratios provided in input
if( grepl("sp_with_max_vpuf",a_nametype) || grepl("sp_with_max_cpue",a_nametype) || grepl("sp_with_max_cpuf",a_nametype) || grepl("sp_with_max_vpufswa",a_nametype)) {
a_func <- function(x) {names(table(x))[which.max(table(x))]} # find the most frequent sp
}
if(plot_per_c_square){
#aggregate per c_square
library(vmstools) # for c_square
if(!any(colnames(my_data) %in% "c_square") ||
grid_agg_res==0.05) my_data$c_square <- vmstools::CSquare (lon=my_data[,long], lat=my_data[,lat], degrees=grid_agg_res)
# then, aggregate the data per c_square...
my_data <- aggregate(an(my_data[,a_nametype]), list(my_data$c_square), a_func, na.rm=TRUE)
colnames(my_data) <- c("c_square", a_nametype)
my_data <- cbind.data.frame(my_data, CSquare2LonLat(my_data$c_square, grid_agg_res)) # get the mid point coordinates
colnames(my_data) <- c("gridCellID", a_nametype, "mid_lat", "mid_lon")
} else{
#-------------------------
#aggregate on a grid
# Set grid
library(vmstools)
library(RColorBrewer)
colintens <- brewer.pal(9,"YlOrRd")
resx <- grid_agg_res
resy <- grid_agg_res
grd <- createGrid(xlims,ylims,resx=resx, resy=resy, type="SpatialGrid", exactBorder=TRUE)
# Grid all tacsatSweptArea data
# Convert all tacsat points first to SpatialPoints
coords <- SpatialPoints(cbind(SI_LONG=an(my_data[, long]), SI_LATI=an(my_data[, lat])))
idx <- over(coords,grd)
my_data$gridCellID <- idx
# Remove records that are not in the study area
my_data <- subset(my_data,is.na(gridCellID)==FALSE)
# then, do an agg
if(grepl("sp_with_max_vpuf",a_nametype) || grepl("sp_with_max_cpue",a_nametype) || grepl("sp_with_max_cpuf",a_nametype) || grepl("sp_with_max_vpufswa",a_nametype)){
my_data[my_data[,"CPUEallsp"]==0,a_nametype] <- as.character(0) # debug misclassified (false positive in pelagic)
my_data <- aggregate(as.character(my_data[,a_nametype]), list(my_data$gridCellID), a_func)
} else{
my_data <- aggregate(an(my_data[,a_nametype]), list(my_data$gridCellID), a_func, na.rm=TRUE) # as.numeric
}
colnames(my_data)[1:2] <- c("gridCellID", a_nametype)
my_data <- cbind(my_data, mid_lat=an(coordinates(grd)[my_data$gridCellID,2]), mid_lon=an(coordinates(grd)[my_data$gridCellID,1]))
#plot(my_data$mid_lon, my_data$mid_lat, col=Satellite.Palette.baseline(length(the_breaks_baseline[-1]))[as.numeric(cut(as.numeric(my_data[,a_nametype]),breaks=the_breaks_baseline, right=FALSE))], pch="+")
}
#-------------------------
# transform to SAR
if(a_nametype %in% c('SWEPT_AREA_KM2', 'SWEPT_AREA_KM2_LOWER', 'SWEPT_AREA_KM2_UPPER')){
my_data[,a_nametype] <- replace (my_data[,a_nametype],
my_data[,a_nametype]>the_breaks_baseline[length(the_breaks_baseline)],
the_breaks_baseline[length(the_breaks_baseline)])
# caution, transform to SAR for the plot grid resolution
my_data$cell_area <- (cos(my_data$mid_lat *pi/180) * 111.325 )*grid_agg_res * (111*grid_agg_res) # 0.05 degree is 3 minutes
my_data[,a_nametype] <- round(my_data[,a_nametype]) / an(my_data$cell_area) # transform to SAR
}
# plot(my_data$mid_lon, my_data$mid_lat, col=Satellite.Palette.baseline(length(the_breaks_baseline[-1]))[cut(as.numeric(my_data[,a_nametype]),breaks=the_breaks_baseline, right=FALSE)], pch="+")
spdata <- unique(my_data[,c("gridCellID", "mid_lat", "mid_lon")]) # make sure to remove duplicates...(but should already have vanished from the prior aggregation)
rownames(spdata) <- spdata$gridCellID
#-------------------------
r1 <- rbind(x = c(-1, -1, 1, 1, -1) * grid_agg_res /2, # 1 by 1 minute or 3 by 3
y = c(-1, 1, 1, -1, -1) * grid_agg_res /2)
library(sp)
spatialLookup <-
SpatialPolygons(
lapply(1:nrow(spdata),
function(i) {
Polygons(
list(
Polygon(
t(r1 + c(spdata$mid_lon[i], spdata$mid_lat[i]))
)
), ID = as.character(spdata$gridCellID[i]))
}
)
)
# now assign data to the columns
out <- spatialLookup[as.character(my_data$gridCellID),]
out$data <- my_data[,a_nametype]
# save output
#rgdal::writeOGR(out, paste0("a_shp", year),
# driver = "ESRI Shapefile", overwrite_layer = TRUE)
# or:
# r <- raster(xmn=xrange[1], xmx=xrange[2], ymn=yrange[1], ymx=yrange[2], res=raster_res, crs=CRS("+proj=longlat +datum=WGS84"))
# some_coords <- SpatialPoints(cbind(SI_LONG=vmspp_this$mid_lon, SI_LATI=vmspp_this$mid_lat))
## rstr <- rasterize(x=some_coords, y=r, field=vmspp_this$landings_from_cell*1000, fun="sum") # converted in kilo
# rstr_eea <- projectRaster(rstr, crs="+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs") # European EEA projection
# rstr_eea[is.na(rstr_eea)] <- -999 # arbitrary code, to get rid of true 0s in GIS
# rstr_eea[rstr_eea<0.001] <- -999
# writeRaster(rstr_eea, file.path(outPath, paste("DispatchedStecfLandingsOnVMS4", a_species, sep='')), format = "GTiff", overwrite=TRUE)
if(grepl("sp_with_max_vpuf",a_nametype) || grepl("sp_with_max_cpue",a_nametype) || grepl("sp_with_max_cpuf",a_nametype) || grepl("sp_with_max_vpufswa",a_nametype)){
some_color_species<- c("COD"="#E69F00", "CSH"="hotpink", "DAB"="#56B4E9", "ELE"="#F0E442", "FLE"="green",
"HAD"="#0072B2", "HER"="mediumorchid4", "HKE"="#CC79A7","HOM"="indianred2", "LEM"="#EEC591",
"MAC"="#458B00", "MON"="#F0F8FF", "MUS"="black", "NEP"="#e3dcbf", "NOP"="#CD5B45", "PLE"="lightseagreen",
"POK"="#6495ED", "PRA"="#CDC8B1", "SAN"="#00FFFF", "SOL"="#8B0000", "SPR"="#008B8B", "TUR"="#A9A9A9", "WHB"="#76a5c4",
"WIT"="red", "WHG"="yellow", "OTH"="blue")
Stocknames <- c("COD"="cod", "COD.nsea"="North Sea cod", "COD.2224"="west Baltic cod", "CSH"="brown shrimp", "DAB"="dab", "ELE"="eel", "FLE"="flounder",
"HAD"="haddock", "HAD.nsea"="North Sea haddock", "HER"="herring", "HER.nsea"="North Sea herring", "HKE"="hake","HKE.nsea"="North Sea hake",
"HOM"="horse mackerel", "LEM"="lemon sole",
"MAC"="mackerel", "MAC.nsea"="North Sea mackerel", "MON"="monkfish", "MUS"="mussel", "NEP"="Nephrops", "NEP.kask"="Kattegat Nephrops",
"NOP"="Norway pout", "PLE"="plaice", "PLE.nsea"="North Sea plaice", "PLE.2123"="Kattegat plaice",
"POK"="saithe", "POK.nsea"="North Sea saithe", "PRA"="boreal shrimp", "PRA.nsea"="North Sea boreal shrimp", "SAN"="sandeel", "SOL"="sole","SOL.nsea"="North Sea sole",
"SOL.2024"="Baltic sole", "SPR"="sprat", "SPR.2232"="Baltic sprat", "TUR"="turbot", "WHB"="blue whiting",
"WIT"="witch flounder", "WHG"="whiting", "OTH"="other",
"COC"="cokle", "OYF"="oyster", "LUM"="lumpfish", "SAL"="salmon", "BLL"="brill", "GAR"="garfish")
some_color_speciesnames <- c("cod"="#E69F00", "brown shrimp"="hotpink", "dab"="#56B4E9", "eel"="#F0E442", "flounder"="green",
"haddock"="#0072B2", "herring"="mediumorchid4", "hake"="#CC79A7","horse mackerel"="indianred2", "lemon sole"="#EEC591",
"mackerel"="#458B00", "monkfish"="#F0F8FF", "mussel"="black", "Nephrops"="#e3dcbf", "Norway pout"="#CD5B45", "plaice"="lightseagreen",
"saithe"="#6495ED", "boreal shrimp"="#CDC8B1", "sandeel"="#00FFFF", "sole"="#8B0000", "sprat"="#008B8B", "turbot"="#A9A9A9", "blue whiting"="#76a5c4",
"witch flounder"="red", "whiting"="yellow", "other"="blue",
"cockle"="#108291", "oyster"="#6a9110", "lumpfish"="red", "salmon"="#c2a515", "brill"="cyan", "garfish"="grey")
out$color <- some_color_species[as.character(out$data)]
} else{
out$color <- Satellite.Palette.baseline(length(the_breaks_baseline[-1])) [cut(out$data, the_breaks_baseline)]
}
# create a rectangle from xlim and ylim to fix bad limits
library(raster)
bb <- as(extent(c(xlims,ylims)), "SpatialPolygons")
proj4string(bb) <- proj4string(out)
#plot(bb, add=FALSE, col=NA)
# coastline cropped
sh_coastlines <- readShapePoly(file.path(getwd(), "ne_50m_ocean","ne_50m_ocean.shp"))
proj4string(sh_coastlines) <- CRS("+proj=longlat +datum=WGS84")
CP <- as(extent(xlims[1], xlims[2], ylims[1], ylims[2]), "SpatialPolygons")
proj4string(CP) <- CRS("+proj=longlat +datum=WGS84")
library(rgeos)
sh_coastlines_clipped <- gIntersection(sh_coastlines, CP, byid=TRUE) # clip
# plot
fao_areas_lambert <- spTransform(fao_areas, CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"))
if(use_fao_areas)
{
#plot (fao_areas_lambert, add=FALSE)
## plot + graticules to the plot
library(sf)
mrc_proj <- st_as_sf(fao_areas_lambert)
mrc <- st_as_sf(fao_areas)
a_cex_axis <- 2.0
if(nametype == "sp_with_max_vpuf") a_cex_axis<-1.3
plot(mrc_proj["geometry"], axes = TRUE, cex.axis=a_cex_axis, graticule = st_crs(mrc), key.pos = NULL, reset = FALSE) # key.pos = NULL, reset = FALSE to avoid pbl with par(mfrow)
#g=st_graticule(mrc_proj["geometry"])
#plot(g[1], col="grey", add=TRUE)
} else{
plot (sh_coastlines_clipped, add=FALSE, axes=FALSE)
}
# invisible(lapply(seq_len(nrow(g)), function(i) {
#if (g$type[i] == "N" && g$x_start[i] - min(g$x_start) < 1000)
# text(g[i,"x_start"], g[i,"y_start"], labels = parse(text = g[i,"degree_label"]),
# srt = g$angle_start[i], pos = 2, cex = 1.7)
#if (g$type[i] == "E" && g$y_start[i] - min(g$y_start) < 1000)
# text(g[i,"x_start"], g[i,"y_start"], labels = parse(text = g[i,"degree_label"]),
# srt = g$angle_start[i] - 90, pos = 1, cex = 1.7)
#if (g$type[i] == "N" && g$x_end[i] - max(g$x_end) > -1000)
# text(g[i,"x_end"], g[i,"y_end"], labels = parse(text = g[i,"degree_label"]),
# srt = g$angle_end[i], pos = 4, cex = 1.7)
#if (g$type[i] == "E" && g$y_end[i] - max(g$y_end) > -1000)
# text(g[i,"x_end"], g[i,"y_end"], labels = parse(text = g[i,"degree_label"]),
# srt = g$angle_end[i] - 90, pos = 3, cex = 1.7)
#}))
library(rgeos)
#out2 <- gIntersection(out, bb, byid=TRUE) not returning the data in it(!)...so substitute with:
out2 <- raster::intersect(out, bb)
crs(out2) <- "+proj=longlat +datum=WGS84"
out2 <- spTransform(out2, CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"))
plot(out2, col = out2$color, border=NA, xlim = xlims, ylim=ylims, add=TRUE)
plot (fao_areas_lambert, add=TRUE)
#plot (sh_coastlines_clipped, add=TRUE)
sh_coastlines <- spTransform(sh_coastlines, CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"))
plot (sh_coastlines, add=TRUE)
if(!is.null(spatial_polys)) plot(spatial_polys, add=TRUE, density=10)
#mtext(side=3, namesce[count], cex=1.5, line=0.5)
#mtext(side=3, someletters[count], cex=2, line=1, adj=0)
a_cex <- 1.3
if(a_nametype == "sp_with_max_vpuf" ){ a_cex <- 0.8}
mtext("Latitude", 1, line=2, cex=a_cex, outer=TRUE)
mtext(side=2,"Longitude",line=2, cex=a_cex, outer=TRUE)
#axis(1, cex.axis=1.0)
#axis(2, las=2, cex.axis=1.0)
box()
a_title <- gsub(".tif", "", namefile)
#if(length(nametype)==1) title(a_title)
x = c(xlims[1]-5+0.1*(xlims[2]-xlims[1]), xlims[1]-5+0.11*(xlims[2]-xlims[1]), xlims[1]-5+0.11*(xlims[2]-xlims[1]), xlims[1]-5+0.1*(xlims[2]-xlims[1]))
y = c(ylims[1]-0.5+0.1*(ylims[2]-ylims[1]), ylims[1]-0.5+0.7*(ylims[2]-ylims[1]), ylims[1]-0.5+0.5*(ylims[2]-ylims[1]), ylims[1]-0.7+0.1*(ylims[2]-ylims[1]))
the_breaks_leg <-NULL
if(grepl("VPUFSWAallsp", a_nametype)) a_title_leg <- c("euro per area swept")
if(grepl("LE_VPUF", a_nametype)) a_title_leg <- c("Catch value (euro) per litre of fuel")
if(grepl("VPUFallsp", a_nametype)) a_title_leg <- c("Catch value (euro) per litre of fuel")
if(grepl("LE_CPUF", a_nametype)) a_title_leg <- c("kg per litre")
if(grepl("CPUFallsp", a_nametype)) a_title_leg <- c("kg per litre")
if(grepl("FPUCallsp", a_nametype)) a_title_leg <- c("Fuel use (litre) per kg catch")
if(grepl("FPUVallsp", a_nametype)) a_title_leg <- c("Fuel use (litre) per euro catch")
if(grepl("LE_CPUE", a_nametype)) a_title_leg <- c("Catch (kg) per effective effort (h)")
if(grepl("CPUEallsp", a_nametype)) a_title_leg <- c("Catch (kg) per effective effort (h)")
if(grepl("LE_VPUE", a_nametype)) a_title_leg <- c("Catch value (euro) per effective effort(h)")
if(grepl("VPUEallsp", a_nametype)) a_title_leg <- c("Catch value (euro) per effective effort(h)")
if(grepl("LE_KG", a_nametype)) a_title_leg <- c("kg")
if(grepl("sp_with_max_vpuf", a_nametype)) a_title_leg <- c("Species caught at highest VPUF")
if(grepl("sp_with_max_cpue", a_nametype)) a_title_leg <- c("Species with max. CPUE")
if(grepl("sp_with_max_cpuf", a_nametype)) a_title_leg <- c("Species with max. CPUF")
if(grepl("sp_with_max_vpufswa", a_nametype)) a_title_leg <- c("Species with max. VPUFSWA")
#legend.gradient2 (cbind(x = x , y = y ), cols=Satellite.Palette.baseline(length(the_breaks_baseline[-1])),
# limits="", title=a_title_leg,
# legend= the_breaks_leg,
# cex=1.3, col="black")
if(grepl("sp_with_max_vpuf",a_nametype) || grepl("sp_with_max_cpue",a_nametype) || grepl("sp_with_max_cpuf",a_nametype) || grepl("sp_with_max_vpufswa",a_nametype)){
name_leg <- names(some_color_speciesnames[Stocknames[unique(out2$data)]]) ; name_leg <- name_leg[!is.na(name_leg)]
fill_leg <- some_color_speciesnames[Stocknames[unique(out2$data)]] ; fill_leg <- fill_leg[!is.na(fill_leg)]
legend("topright", legend=name_leg, fill=fill_leg, bty="o", border=NA, ncol=4, box.col="white", title=a_title_leg)
} else{
for(i in 1: length(the_breaks_baseline[-1])){ if(the_breaks_baseline[i]>1) {the_breaks_leg[i] <- round(the_breaks_baseline[i],1)} else{the_breaks_leg[i]<- the_breaks_baseline[i]}}
legend("topright", legend=the_breaks_leg, fill=Satellite.Palette.baseline(length(the_breaks_baseline[-1])), bty="o", border=NA, ncol=4, box.col="white", title=a_title_leg, cex=2.2)
}
box()
} # end a_nametype
dev.off()
return()
}
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
## CALLS ##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
plot_per_c_square <- FALSE
xrange <- c(-30,50) # ALL
yrange <- c(30,81) # ALL
########
########
########
########
########
########
# AVERAGE OVER THE ENTIRE PERIOD 2005-2019 - BOTTOM CONTACTING GEARS
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndRatiosForBottContact_",2012,".RData") )) # aggResult
# load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",2012,".RData") )) # aggResult
metiers <- as.character(unique(aggResult$LE_MET))
#load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",2019,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndRatiosForBottContact_",2012,".RData") )) # aggResult
metiers <- unique(c(metiers, as.character(unique(aggResult$LE_MET))))
metiers <- metiers[!grepl("NA", metiers)]
plot_per_c_square <- FALSE
library(rgdal); library(raster); fao_areas <- readOGR(file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"))
agg_dem <- NULL
an <- function(x) as.numeric(as.character(x))
for (y in years){
#load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",y,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndRatiosForBottContact_",y,".RData") )) # aggResult
# litre per kilo catch
aggResult$FPUCallsp <- an(aggResult$LE_KG_LITRE_FUEL)/(an(aggResult$KKGallsp)*1000)
aggResult <- aggResult [!is.infinite(aggResult$FPUCallsp),]
# litre per euro catch
aggResult$FPUVallsp <- an(aggResult$LE_KG_LITRE_FUEL)/(an(aggResult$KEUROallsp)*1000)
aggResult <-aggResult [!is.infinite(aggResult$FPUVallsp),]
aggResult <- aggResult[aggResult$FPUCallsp<10000,] # clean up a bit for outliers coming from merged VMS pts with no logbk catch
agg_dem <- rbind.data.frame(agg_dem,
cbind.data.frame(year=y,aggResult[,c("LE_MET","CELL_LONG", "CELL_LATI",
"CPUEallsp","CPUFallsp","VPUFallsp", "VPUFSWAallsp", "FPUCallsp", "FPUVallsp",
"sp_with_max_vpuf", "sp_with_max_cpue", "sp_with_max_cpuf", "sp_with_max_vpufswa")])
)
}
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#ALL METIERS POOLED
library(maptools)
years <- 2005:2019
aggall_dem <- agg_dem
aggall_dem$LE_MET <- "BottomContactingGears"
quickmap (namefile = paste0("CPUEallspAndCoFor_","BottomContactingGears","_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall_dem, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("CPUEallsp","FPUCallsp","VPUFallsp"), ## 3 plots
a_met = "BottomContactingGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020", "output_plots_maps_met")
)
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!END FOR PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#PER METIER
if(FALSE) for (a_met in metiers){
quickmap (namefile = paste0("CPUEallspAndCoFor_",a_met,"_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = agg, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("CPUEallsp","FPUCallsp","VPUFallsp"), ## 3 plots
a_met = a_met,
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=3000,
a_height=3000,
output_dir=file.path(getwd(), "outputs2020", "output_plots_maps_met")
)
}
########
########
########
########
########
########
# AVERAGE OVER THE ENTIRE PERIOD 2005-2019 -PELAGIC GEARS
#load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",2012,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",2012,".RData") )) # aggResult
metiers <- as.character(unique(aggResult$LE_MET))
#load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",2019,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",2019,".RData") )) # aggResult
metiers <- unique(c(metiers, as.character(unique(aggResult$LE_MET))))
metiers <- metiers[!grepl("NA", metiers)]
plot_per_c_square <- FALSE
library(rgdal); library(raster); fao_areas <- readOGR(file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"))
years <- 2005:2019
an <- function(x) as.numeric(as.character(x))
agg_pel <- NULL
for (y in years){
#load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",y,".RData") )) # aggResult
# litre per kilo catch
aggResult$FPUCallsp <- an(aggResult$LE_KG_LITRE_FUEL)/(an(aggResult$KKGallsp)*1000)
aggResult <- aggResult [!is.infinite(aggResult$FPUCallsp),]
# litre per euro catch
aggResult$FPUVallsp <- an(aggResult$LE_KG_LITRE_FUEL)/(an(aggResult$KEUROallsp)*1000)
aggResult <-aggResult [!is.infinite(aggResult$FPUVallsp),]
aggResult <- aggResult[aggResult$FPUCallsp<10000,] # clean up a bit for outliers coming from merged VMS pts with no logbk catch
agg_pel <- rbind.data.frame(agg_pel,
cbind.data.frame(year=y,aggResult[,c("LE_MET","CELL_LONG", "CELL_LATI",
"CPUEallsp","CPUFallsp","VPUFallsp", "VPUFSWAallsp", "FPUCallsp", "FPUVallsp",
"sp_with_max_vpuf", "sp_with_max_cpue", "sp_with_max_cpuf", "sp_with_max_vpufswa")])
)
}
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#ALL METIERS POOLED
aggall_pel <- agg_pel
aggall_pel$LE_MET <- "PelagicGears"
quickmap (namefile = paste0("CPUEallspAndCoFor_","PelagicGears","_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall_pel, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("CPUEallsp","FPUCallsp","VPUFallsp"), ## 4 plots
a_met = "PelagicGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020_pel", "output_plots_maps_met")
)
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!END PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#PER METIER
if(FALSE) for (a_met in metiers){
quickmap (namefile = paste0("CPUEallspAndCoFor_",a_met,"_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = agg_pel, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("CPUEallsp","FPUCallsp","VPUFallsp"), ## 3 plots
a_met = a_met,
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020_pel", "output_plots_maps_met")
)
}
########
########
########
########
########
########
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
## PLOT PER CELL THE SEPCIES COLOR CODE WITH THE LOCALLY HIGHEST VPUF etc.
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
#AVERAGE OVER THE ENTIRE PERIOD 2005-2019 - BOTTOM CONTACTING GEARS
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",2012,".RData") )) # aggResult
metiers <- as.character(unique(aggResult$LE_MET))
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",2019,".RData") )) # aggResult
metiers <- unique(c(metiers, as.character(unique(aggResult$LE_MET))))
metiers <- metiers[!grepl("NA", metiers)]
plot_per_c_square <- FALSE
library(rgdal); library(raster); fao_areas <- readOGR(file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"))
agg <- NULL
for (y in years){
load(file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForBottContact_",y,".RData") )) # aggResult
agg <- rbind.data.frame(agg,
cbind.data.frame(year=y,aggResult[,c("LE_MET","CELL_LONG", "CELL_LATI",
"CPUEallsp","CPUFallsp","VPUFallsp", "VPUFSWAallsp",
"sp_with_max_vpuf", "sp_with_max_cpue", "sp_with_max_cpuf", "sp_with_max_vpufswa")])
)
}
aggall <- agg
aggall$LE_MET <- "BottomContactingGears"
library(maptools)
library(sf)
quickmap (namefile = paste0("ColorSpCodeMapFor_","BottomContactingGears","_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("sp_with_max_cpue","sp_with_max_cpuf","sp_with_max_vpuf"), ## 3 plots
a_met = "BottomContactingGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020", "output_plots_maps_met")
)
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
library(maptools)
library(sf)
aggall <- agg
aggall$LE_MET <- "BottomContactingGears"
quickmap (namefile = paste0("ColorSpCodeMapFor_","BottomContactingGears","_",years[1],"-",years[length(years)],"_maxvpuf.tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("sp_with_max_vpuf"), ## 1 plots
a_met = "BottomContactingGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020", "output_plots_maps_met")
)
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!END PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
########
########
########
########
########
########
# AVERAGE OVER THE ENTIRE PERIOD 2005-2019 -PELAGIC GEARS
#load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",2012,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",2012,".RData") )) # aggResult
metiers <- as.character(unique(aggResult$LE_MET))
#load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",2019,".RData") )) # aggResult
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",2012,".RData") )) # aggResult
metiers <- unique(c(metiers, as.character(unique(aggResult$LE_MET))))
metiers <- metiers[!grepl("NA", metiers)]
plot_per_c_square <- FALSE
library(rgdal); library(raster); fao_areas <- readOGR(file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"))
years <- 2005:2019
agg <- NULL
for (y in years){
load(file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndRatiosForPel_",y,".RData") )) # aggResult
agg <- rbind.data.frame(agg,
cbind.data.frame(year=y,aggResult[,c("LE_MET","CELL_LONG", "CELL_LATI",
"CPUEallsp","CPUFallsp","VPUFallsp", "VPUFSWAallsp",
"sp_with_max_vpuf", "sp_with_max_cpue", "sp_with_max_cpuf", "sp_with_max_vpufswa")])
)
}
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
aggall <- agg
aggall$LE_MET <- "PelagicGears"
quickmap (namefile = paste0("ColorSpCodeMapFor_","PelagicGears","_",years[1],"-",years[length(years)],"_maxvpuf.tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("sp_with_max_vpuf"), ## 1 plot
a_met = "PelagicGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020_pel", "output_plots_maps_met")
)
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
#!END PAPER
#!#!#!#!#!#!#!#!
#!#!#!#!#!#!#!#!
aggall <- agg
aggall$LE_MET <- "PelagicGears"
quickmap (namefile = paste0("ColorSpCodeMapFor_","PelagicGears","_",years[1],"-",years[length(years)],".tif"),
#a_file = file.path(getwd(), "outputs2020_pel", paste0("AggregatedSweptAreaPlusMet6AndVsizeAndRatiosForPel_",y,".RData") ),
#nameobj = "aggResult",
aggResult = aggall, # file.path(getwd(),"outputs2020", paste("AggregatedSweptAreaPlus_2019.RData") ),
a_unit = 1, # 1 because 1 year agg
nametype =c("sp_with_max_cpue","sp_with_max_cpuf","sp_with_max_vpuf"), ## 3 plots
a_met = "PelagicGears",
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=TRUE,
fao_areas= fao_areas,
spatial_polys=NULL,
a_width=5000,
a_height=5000,
output_dir=file.path(getwd(), "outputs2020_pel", "output_plots_maps_met")
)
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!DIVERSE HELFUL PLOTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
# plot average SAR
quickmap (namefile = paste0("SAR_Average_2017-2019", ".tif") ,
a_file = file.path(getwd(), "outputs2020", "AggregatedSweptArea_2017-2019.RData"),
nameobj = "aggResult",
a_unit = 3, # divide by 3 because 3y
nametype = "SWEPT_AREA_KM2",
a_met = NULL,
long = "SI_LONG",
lat = "SI_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=FALSE,
fao_namefile= file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"),
spatial_polys=NULL,
a_width=6000,
a_height=6000,
output_dir=file.path(getwd(), "outputs2020", "output_plots")
)
# spatial SAR
years <- 2012:2019
for (y in years){
#load(file.path(getwd(), y, "tacsatSweptArea.RData")) # aggResult
quickmap (namefile = paste0("SAR_", y, ".tif"),
a_file = file.path(getwd(),"outputs2020", y, "tacsatSweptArea.RData"),
nameobj = "tacsatSweptArea",
a_unit = 1, # divide by 1 because 1y
nametype = "SWEPT_AREA_KM2",
a_met = NULL,
long = "SI_LONG",
lat = "SI_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=FALSE,
fao_namefile= file.path(getwd(), "FAO_AREAS", "FAO_AREAS.shp"),
spatial_polys=NULL,
a_width=6000,
a_height=6000,
output_dir=file.path(getwd(), "outputs2020", "output_plots")
)
}
# spatial landings
spp <- c("PLE", "COD", "NEP", "LITRE_FUEL")
years <- 2017:2019
for (sp in spp){
for (y in years){
quickmap (namefile = paste0("LE_KG_",sp,"_",y, ".tif"),
a_file = file.path(getwd(), "outputs2020", paste0("AggregatedSweptAreaPlus_",y,".RData") ),
nameobj = "aggResult",
a_unit = 1, # divide by 1 because only one y
nametype = paste0("LE_KG_",sp),
a_met = NULL,
long = "CELL_LONG",
lat = "CELL_LATI",
plot_per_c_square =FALSE,
grid_agg_res =if(plot_per_c_square){0.05} else {3/60},
xlims = c(-7,25),
ylims = c(50,65),
use_fao_areas=FALSE,