-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_getinputs_ISIMIP3a.R
454 lines (380 loc) · 18.2 KB
/
01_getinputs_ISIMIP3a.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
# STEP 1: GET GCM INPUTS FOR DYNAMIC BENTHIC-PELAGIC SIZE SPECTRUM MODEL
# Loading libraries -------------------------------------------------------
library(tidyverse)
library(data.table)
library(parallel)
library(dtplyr)
source("supporting_functions.R")
source("dbpm_model_functions.R")
# FAO sectors -------------------------------------------------------------
## Apply calc_inputs_all() function to each FAO region (0.25 degree res) ----
file_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/fao_inputs",
"obsclim/025deg")
file_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/fao_inputs",
"ctrlclim/025deg")
region_choice <- c(21, 27, 31, 34, 41, 47, 48, 51, 57, 58, 61, 67, 71, 77,
81, 87, 88)
#Applying function to all chosen regions (gridded outputs)
#Define paths for gridded outputs
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs_gridcell/obsclim",
"025deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs_gridcell/ctrlclim",
"025deg")
region_choice |>
map(~calc_inputs_gridded(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
#Applying weighting function to all chosen regions
#Defining paths for weighted outputs
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs/obsclim/025deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs/ctrlclim/025deg")
region_choice |>
map(~calc_inputs_all(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
## Merging processed inputs into a single file ----------------------------
combined_FAO_inputs <- list.files(out_path_obs, full.names = TRUE) |>
#Ignoring files containing "DBPM" in their file name
str_subset("DBPM", negate = T) |>
#Note that the amount of cores available will depend on compute size chosen
mclapply(FUN = fread, mc.cores = 28) |>
rbindlist() |>
#Original comment: all depths are almost definitely > 200m in FAO regions
mutate(deptho_m = 200,
#Keep only the ID identifying the FAO region
region = as.integer(str_remove(region, "FAO-LME-"))) |>
as_tibble()
#Get a list of FAO regions and their total area in km2 to calculate effort/m2
FAO_area <- combined_FAO_inputs |>
distinct(region, total_area_km2, deptho_m) |>
as_tibble()
#From original code: Variable names were standardised to use common terminology
# sphy = phypico-vint_mol_m-2
# lphy = phyc-vint_mol_m-2 - phypico-vint_mol_m-2
combined_FAO_inputs <- combined_FAO_inputs |>
mutate(sphy = phypico_vint, lphy = phyc_vint - phypico_vint) |>
#Removing columns not needed
select(-c(phyc_vint, phypico_vint))
## Loading effort and catches data ----------------------------------------
effort_file_path <- "/g/data/vf71/fishmip_inputs/ISIMIP3a/DKRZ_EffortFiles"
#Effort data
effort_FAO <- file.path(effort_file_path,
"effort_isimip3a_histsoc_1841_2010.csv") |>
fread() |>
filter(LME == 0) |>
as_tibble() |>
# calculate sum of effort by FAO region/by total area of FAO region
group_by(Year, fao_area) |>
summarize(total_nom_active = sum(NomActive, na.rm = T),
.groups = "drop") |>
ungroup() |>
rename(year = Year, region = fao_area) |>
full_join(FAO_area, by = "region") |>
mutate(total_nom_active_area_m2 = total_nom_active/(total_area_km2*1e6))
#Catches data
FAO_catch_input <- list.files(effort_file_path,
"catch-validation_isimip3a_histsoc",
full.names = T) |>
read_csv() |>
filter(LME == 0) |>
# catch is in tonnes, checked in FishingEffort Rproject
mutate(catch_tonnes = Reported+IUU) |>
group_by(Year, fao_area) |>
summarize(catch_tonnes = sum(catch_tonnes), .groups = "drop") |>
# also Reg advise to exclude discards
ungroup() |>
rename(year = Year, region = fao_area) |>
full_join(FAO_area, by = "region") |>
mutate(catch_tonnes_area_m2 = catch_tonnes/(total_area_km2*1e6))
#Merging catches and effort data
DBPM_FAO_effort_catch_input <- effort_FAO |>
full_join(FAO_catch_input)
#Removing individual data frames
rm(effort_FAO, FAO_catch_input)
## Plotting fish and catch data -------------------------------------------
# Creating plots to ensure data makes sense - The original code was changed
# slightly to match the original saved image
#Split dataset as items in a list based on FAO area
plot_df <- DBPM_FAO_effort_catch_input |>
group_by(region) |>
group_split() |>
#Select first FAO region
first()
#Plotting data
plot_df |>
ggplot(aes(Year, total_nom_active))+
ggtitle(paste("FAO region #", unique(plot_df$region), sep = " "))+
# spin-up edf8fb
annotate("rect", xmin = 1841, xmax = 1960, ymin = 0, ymax = Inf,
fill = "#b2e2e2", alpha = 0.4)+
# projection 66c2a4
annotate("rect", xmin = 1961, xmax = 2010, ymin = 0, ymax = Inf,
fill = "#238b45", alpha = 0.4)+
geom_point(size = 1)+
geom_line()+
theme_bw()+
theme(labs(y = "Total nom active"),
text = element_text(size = 11),
axis.title.x = element_blank(),
plot.title = element_text(size = 11),
axis.title.y = element_text(size = 10),
axis.text = element_text(size = 9),
legend.title = element_text(size = 10),
legend.text = element_text(size = 9),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(0.1, "cm"))
#Saving result that matches previous work
ggsave("Output/Effort_FAO1_check_DFA.pdf", device = "pdf")
#Removing variables not in use
rm(plot_df)
## Calculating intercept and slope ----------------------------------------
DBPM_FAO_climate_inputs_slope <- combined_FAO_inputs |>
mutate(area_m2 = total_area_km2*1e6) |>
#Remove unused columns and reorder them
select(region, date, tos, tob, sphy, lphy, deptho_m, area_m2,
expc_bot) |>
# name columns as in "dbpm_model_functions.R" script
rename(t = date, depth = deptho_m, sbt = tob, sst = tos,
expcbot = expc_bot) |>
#Calculate slope and intercept
mutate(er = getExportRatio(sphy, lphy, sst, depth),
er = ifelse(er < 0, 0, ifelse(er > 1, 1, er)),
intercept = GetPPIntSlope(sphy, lphy, mmin = 10^-14.25,
mmid = 10^-10.184,
mmax = 10^-5.25, depth,
output = "intercept"),
slope = GetPPIntSlope(sphy, lphy, mmin = 10^-14.25,
mmid = 10^-10.184, mmax = 10^-5.25, depth,
output = "slope")) |>
relocate(all_of(c("er", "intercept", "slope")), .before = sphy)
## Saving catch and effort, and inputs data -------------------------------
#Folder where outputs will be stored
folder_out <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs/obsclim/025deg")
#Saving DBPM inputs
DBPM_FAO_climate_inputs_slope |>
fwrite(file.path(folder_out, "DBPM_FAO_climate_inputs_slope.csv"))
#Saving catch and effort data
DBPM_FAO_effort_catch_input |>
fwrite(file.path(folder_out, "DBPM_FAO_effort_catch_input.csv"))
## Apply calc_inputs_all() function to each FAO region (1 degree res) -------
file_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/fao_inputs",
"obsclim/1deg")
file_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/fao_inputs",
"ctrlclim/1deg")
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs_gridcell/obsclim/1deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/fao_inputs_gridcell/ctrlclim",
"1deg")
#Applying function to all chosen regions
region_choice |>
map(~calc_inputs_gridded(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
## ISIMIP3a scale 3 -------------------------------------------------------
#Applying getGCM() function to all experiments and resolutions
base_path <- "/g/data/vf71/fishmip_inputs/ISIMIP3a"
#0.25 degree datasets
getGCM(folder_path = file.path(base_path, "global_inputs/ctrlclim/025deg"),
save_path = file.path(base_path, "processed_forcings/ctrlclim/025deg"))
getGCM(folder_path = file.path(base_path, "global_inputs/obsclim/025deg"),
save_path = file.path(base_path, "processed_forcings/obsclim/025deg"))
#1 degree datasets
getGCM(folder_path = file.path(base_path, "global_inputs/ctrlclim/1deg"),
save_path = file.path(base_path, "processed_forcings/ctrlclim/1deg"))
getGCM(folder_path = file.path(base_path, "global_inputs/obsclim/1deg"),
save_path = file.path(base_path, "processed_forcings/obsclim/1deg"))
## Calculate spinup from gridded ctrlclim data ----------------------------
base_folder <- "/g/data/vf71/fishmip_inputs/ISIMIP3a/processed_forcings"
calc_input_spinup_gridcell(base_path = file.path(base_folder, "ctrlclim/1deg"),
save_path = file.path(base_folder, "spinup/1deg"))
calc_input_spinup_gridcell(base_path = file.path(base_folder, "ctrlclim/025deg"),
save_path = file.path(base_folder, "spinup/025deg"))
# LMEs --------------------------------------------------------------------
## Apply calc_inputs_all() function to each LME (0.25 degree res) ----
file_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/lme_inputs",
"obsclim/025deg")
file_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/lme_inputs",
"ctrlclim/025deg")
region_choice <- 1:66
#Applying function to all chosen regions (gridded outputs)
#Define paths for gridded outputs
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs_gridcell/obsclim",
"025deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs_gridcell/ctrlclim",
"025deg")
region_choice |>
map(~calc_inputs_gridded(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
#Applying weighting function to all chosen regions
#Defining paths for weighted outputs
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs/obsclim/025deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs/ctrlclim/025deg")
region_choice |>
map(~calc_inputs_all(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
## Merging processed inputs into a single file ----------------------------
combined_LME_inputs <- list.files(out_path_obs, full.names = TRUE) |>
#Ignoring files containing "DBPM" in their file name
str_subset("DBPM", negate = T) |>
#Note that the amount of cores available will depend on compute size chosen
mclapply(FUN = fread, mc.cores = 28) |>
rbindlist() |>
as_tibble() |>
#Keep only the ID identifying the LME region
mutate(region = as.integer(str_remove(region, "LME_")))
#Getting depth data
depth <- combined_LME_inputs |>
drop_na(deptho) |>
select(deptho, region)
#Tidying up depth inputs
combined_LME_inputs <- combined_LME_inputs |>
#Removing depth from combined inputs
filter(is.na(deptho)) |>
select(!deptho) |>
left_join(depth, by = join_by("region"))
#Removing variable not needed
rm(depth)
#Get a list of LME regions and their total area in km2 to calculate effort/m2
LME_area <- combined_LME_inputs |>
distinct(region, total_area_km2, deptho) |>
as_tibble()
#From original code: Variable names were standardised to use common terminology
# sphy = phypico-vint_mol_m-2
# lphy = phyc-vint_mol_m-2 - phypico-vint_mol_m-2
combined_LME_inputs <- combined_LME_inputs |>
mutate(sphy = phypico_vint, lphy = phyc_vint - phypico_vint) |>
#Removing columns not needed
select(-c(phyc_vint, phypico_vint))
## Loading effort and catches data ----------------------------------------
effort_file_path <- "/g/data/vf71/fishmip_inputs/ISIMIP3a/DKRZ_EffortFiles"
#Effort data
effort_LME <- file.path(effort_file_path,
"effort_isimip3a_histsoc_1841_2010.csv") |>
fread() |>
#Removing LME = 0 because there is no information about area
filter(LME > 0) |>
# calculate sum of effort by LME/by total area of LME
group_by(Year, LME) |>
summarize(total_nom_active = sum(NomActive, na.rm = T),
.groups = "drop") |>
ungroup() |>
rename(year = Year, region = LME) |>
full_join(LME_area, by = "region") |>
mutate(total_nom_active_area_m2 = total_nom_active/(total_area_km2*1e6))
#Catches data
LME_catch_input <- list.files(effort_file_path,
"catch-validation_isimip3a_histsoc",
full.names = T) |>
read_csv() |>
#Removing LME = 0 because there is no information about area
filter(LME > 0) |>
# catch is in tonnes, checked in FishingEffort Rproject
mutate(catch_tonnes = Reported+IUU) |>
group_by(Year, LME) |>
summarize(catch_tonnes = sum(catch_tonnes), .groups = "drop") |>
# also Reg advise to exclude discards
ungroup() |>
rename(year = Year, region = LME) |>
full_join(LME_area, by = "region") |>
mutate(catch_tonnes_area_m2 = catch_tonnes/(total_area_km2*1e6))
#Merging catches and effort data
DBPM_LME_effort_catch_input <- effort_LME |>
full_join(LME_catch_input)
#Removing individual data frames
rm(effort_LME, LME_catch_input)
## Plotting fish and catch data -------------------------------------------
# Creating plots to ensure data makes sense - The original code was changed
# slightly to match the original saved image
#Split dataset as items in a list based on LME area
plot_df <- DBPM_LME_effort_catch_input |>
group_by(region) |>
group_split() |>
#Select first LME region
first()
#Plotting data
plot_df |>
ggplot(aes(Year, total_nom_active))+
ggtitle(paste("LME #", unique(plot_df$region), sep = " "))+
annotate("rect", xmin = 1841, xmax = 1960, ymin = 0, ymax = Inf,
fill = "#b2e2e2", alpha = 0.4)+
# projection 66c2a4
annotate("rect", xmin = 1961, xmax = 2010, ymin = 0, ymax = Inf,
fill = "#238b45", alpha = 0.4)+
geom_point(size = 1)+
geom_line()+
theme_bw()+
theme(labs(y = "Total nom active"),
text = element_text(size = 11),
axis.title.x = element_blank(),
plot.title = element_text(size = 11),
axis.title.y = element_text(size = 10),
axis.text = element_text(size = 9),
legend.title = element_text(size = 10),
legend.text = element_text(size = 9),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(0.1, "cm"))
#Saving result that matches previous work
ggsave("Output/Effort_LME1_check_DFA.pdf", device = "pdf")
#Removing variables not in use
rm(plot_df)
## Calculating intercept and slope ----------------------------------------
DBPM_LME_climate_inputs_slope <- combined_LME_inputs |>
mutate(area_m2 = total_area_km2*1e6) |>
#Remove unused columns and reorder them
select(region, date, tos, tob, sphy, lphy, deptho, area_m2,
expc_bot) |>
# name columns as in "dbpm_model_functions.R" script
rename(t = date, depth = deptho, sbt = tob, sst = tos, expcbot = expc_bot) |>
#Calculate slope and intercept
mutate(er = getExportRatio(sphy, lphy, sst, depth),
er = ifelse(er < 0, 0, ifelse(er > 1, 1, er)),
intercept = GetPPIntSlope(sphy, lphy, mmin = 10^-14.25,
mmid = 10^-10.184,
mmax = 10^-5.25, depth,
output = "intercept"),
slope = GetPPIntSlope(sphy, lphy, mmin = 10^-14.25,
mmid = 10^-10.184, mmax = 10^-5.25, depth,
output = "slope")) |>
relocate(all_of(c("er", "intercept", "slope")), .before = sphy)
## Saving catch and effort, and inputs data -------------------------------
#Folder where outputs will be stored
folder_out <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs/obsclim/025deg")
#Saving DBPM inputs
DBPM_LME_climate_inputs_slope |>
fwrite(file.path(folder_out, "DBPM_LME_climate_inputs_slope.csv"))
#Saving catch and effort data
DBPM_LME_effort_catch_input |>
fwrite(file.path(folder_out, "DBPM_LME_effort_catch_input.csv"))
## Apply calc_inputs_all() function to each LME region (1 degree res) -------
file_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/lme_inputs",
"obsclim/1deg")
file_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a/lme_inputs",
"ctrlclim/1deg")
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs_gridcell/obsclim/1deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs_gridcell/ctrlclim",
"1deg")
#Applying function to all chosen regions
region_choice |>
map(~calc_inputs_gridded(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))
#Applying weighting function to all chosen regions
#Defining paths for weighted outputs
out_path_obs <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs/obsclim/1deg")
out_path_ctrl <- file.path("/g/data/vf71/fishmip_inputs/ISIMIP3a",
"processed_forcings/lme_inputs/ctrlclim/1deg")
region_choice |>
map(~calc_inputs_all(file_path_ctrl, file_path_obs, ., out_path_ctrl,
out_path_obs))