forked from JW-spec-tech/S_land_bio_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo_biomass_viz_new_git.R
225 lines (158 loc) · 7.16 KB
/
Demo_biomass_viz_new_git.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
#### Load packages ####
library(arrow)
library(NLMR)
library(sf)
library(raster)
library(fasterize)
library(sspm)
library(rgeos)
library(tidyr)
library(readr)
library(dplyr)
library(mgcv)
library(readr)
library(foreach)
library(doParallel)
library(parallelly)
library(ranger)
library(tidyverse)
library(kableExtra)
library(ggplot2)
library(patchwork)
library(viridis)
#### Load the Functions ####
source(file = "function_patches.R")
source(file = "Make_local.domain_local.arena.R")
source(file = "Strap_calculations.R")
source(file = "Make_PB_fall.dat.R")
#### Load model - TODO ####
# have the depth model here
#### Set Simulation Parameters ####
# Landscape dimensions
x=y=size <- 500
# Number of years/simulations
n <- 1
# Vairation
v1 <- 1
v2 <- v1*2
v3 <- v1*3
#### 1. Generate base Landscape ####
message("1. Generate base Landscape")
Main_L <- nlm_mpd(ncol = size, nrow = size, roughness = roughness)*nlm_mpd(ncol = size, nrow = size, roughness = roughness)*nlm_mpd(ncol = size, nrow = size, roughness = roughness)
plot(Main_L)
#### 1.B Generate secondary landscapes which will be used to vary temperature.####
message("1.B Generate secondary landscapes which will be used to vary temperature.")
list_x <- rep(x-1, n)
list_y <- rep(y-1, n)
Sub_L <- mapply(FUN = nlm_gaussianfield,
ncol = list_x, nrow = list_y,
resolution = 1,
autocorr_range = 1,
mag_var = 5,
nug = 0.2,
mean = 0.5)
plot(Sub_L[[1]])
#### 1.C Generate secondary depths which will be used to generate alternate temperatures for the different years ####
Sub_L_M <- list()
for(i in 1:n){
Sub_L_M[[i]] <- (Main_L + (Sub_L[[i]]/10))/2
}
plot(Sub_L_M[[1]])
#### 1.D Vizualizing before and after Variation
df1 <- as.data.frame(Main_L, xy = TRUE)
df1$layer <- df1$layer/max(df1$layer)
df2 <- as.data.frame(Sub_L_M[[1]], xy = TRUE)
df2$layer <- df2$layer/max(df2$layer)
plot1 <- ggplot(df1, aes(x = x, y = y, fill = layer)) +
geom_raster() +
theme_minimal() +
labs(title = "Before yearly variation") +
viridis::scale_fill_viridis()
plot2 <- ggplot(df2, aes(x = x, y = y, fill = layer)) +
geom_raster() +
theme_minimal() +
labs(title = "After yearly variation") +
viridis::scale_fill_viridis()
combined_plot <- plot1 + plot2
combined_plot
#### 2. Create Depth patches ####
message("2. Create Depth patches")
# Add patches of depth variation
depth_patch_variation <- nlm_randomcluster(ncol = size-1, nrow = size-1,
p = 0.54,
ai = c(0.7, 0.10, 0.10, 0.10))
plot(depth_patch_variation)
# Multiply the landscape depth at every location
Sub_L_M_patch_v1 <- exp(depth_patch_variation*v1)*Sub_L_M[[1]]@data@values
Sub_L_M_patch_v1@data@values <- Sub_L_M_patch_v1@data@values/max(Sub_L_M_patch_v1@data@values)
Sub_L_M_patch_v2 <- exp(depth_patch_variation*v2)*Sub_L_M[[1]]@data@values
Sub_L_M_patch_v2@data@values <- Sub_L_M_patch_v2@data@values/max(Sub_L_M_patch_v2@data@values)
Sub_L_M_patch_v3 <- exp(depth_patch_variation*v3)*Sub_L_M[[1]]@data@values
Sub_L_M_patch_v3@data@values <- Sub_L_M_patch_v3@data@values/max(Sub_L_M_patch_v3@data@values)
#### 3. Generate depth ####
Sub_L_M_patch_v1@data@values <- (Sub_L_M_patch_v1@data@values*1126)+58
Sub_L_M_patch_v2@data@values <- (Sub_L_M_patch_v2@data@values*1126)+58
Sub_L_M_patch_v3@data@values <- (Sub_L_M_patch_v3@data@values*1126)+58
Sub_L_M_patch_v1_df <- as.data.frame(Sub_L_M_patch_v1, xy = TRUE)
Sub_L_M_patch_v2_df <- as.data.frame(Sub_L_M_patch_v2, xy = TRUE)
Sub_L_M_patch_v3_df <- as.data.frame(Sub_L_M_patch_v3, xy = TRUE)
# Rename the 3rd column
names(Sub_L_M_patch_v1_df)[3] <- "depth"
names(Sub_L_M_patch_v2_df)[3] <- "depth"
names(Sub_L_M_patch_v3_df)[3] <- "depth"
#### 4. Generate Temperature ####
message("4. Generate Temperature")
real <- read.csv("/trawl_nl.csv")
# Generate gam based on real depth and temp
gam_depth_sim <- gam(temp_bottom ~ s(sqrt(depth), bs="ad"), data = real)
# Predict the temperature from depth
Sub_L_M_patch_v1_df$temperature <- predict(gam_depth_sim, newdata = Sub_L_M_patch_v1_df, se.fit = T)$fit
Sub_L_M_patch_v2_df$temperature <- predict(gam_depth_sim, newdata = Sub_L_M_patch_v2_df, se.fit = T)$fit
Sub_L_M_patch_v3_df$temperature <- predict(gam_depth_sim, newdata = Sub_L_M_patch_v3_df, se.fit = T)$fit
#### 5. Generate Biomass ####
message("5. Generate Biomass")
# Biomass parameters #
depth_sd = 200
temp_sd = 2
scale_depth <- dnorm(0,0,depth_sd)
scale_temp <- dnorm(0,0,2)
Sub_L_M_patch_v1_df$biomass <- dnorm((Sub_L_M_patch_v1_df$depth - 312.5), 0, 100)/dnorm(0,0,100)*dnorm((Sub_L_M_patch_v1_df$temperature - 2.916), 0, 2)/dnorm(0,0,2)
Sub_L_M_patch_v2_df$biomass <- dnorm((Sub_L_M_patch_v2_df$depth - 312.5), 0, 100)/dnorm(0,0,100)*dnorm((Sub_L_M_patch_v2_df$temperature - 2.916), 0, 2)/dnorm(0,0,2)
Sub_L_M_patch_v3_df$biomass <- dnorm((Sub_L_M_patch_v3_df$depth - 312.5), 0, 100)/dnorm(0,0,100)*dnorm((Sub_L_M_patch_v3_df$temperature - 2.916), 0, 2)/dnorm(0,0,2)
#### 7. Plotting ####
library(ggplot2)
library(viridis)
library(cowplot)
# Function to create stacked plot for each data frame and variable
create_stacked_plot <- function(df, variable, title) {
mean_value <- mean(df[[variable]]) # Calculate the mean value
sum_value <- sum(df[["biomass"]]) # Calculate the sum of biomass
plot <- ggplot(df, aes(x = x, y = y, fill = .data[[variable]])) +
geom_tile() +
scale_fill_viridis() +
labs(title = title) +
annotate("text", x = Inf, y = Inf, label = paste("Mean:", round(mean_value, 2)),
hjust = 1, vjust = 1, size = 4, color = "black") + # Add mean value as annotation
annotate("text", x = Inf, y = -Inf, label = paste("Sum:", round(sum_value, 2)),
hjust = 1, vjust = 0, size = 4, color = "black") # Add sum of biomass as annotation
return(plot)
}
# Create the stacked plots for each data frame and variable
plot_v1_depth <- create_stacked_plot(Sub_L_M_patch_v1_df, "depth", "Patch v1 - Depth")
plot_v1_temperature <- create_stacked_plot(Sub_L_M_patch_v1_df, "temperature", "Patch v1 - Temperature")
plot_v1_biomass <- create_stacked_plot(Sub_L_M_patch_v1_df, "biomass", "Patch v1 - Biomass")
plot_v2_depth <- create_stacked_plot(Sub_L_M_patch_v2_df, "depth", "Patch v2 - Depth")
plot_v2_temperature <- create_stacked_plot(Sub_L_M_patch_v2_df, "temperature", "Patch v2 - Temperature")
plot_v2_biomass <- create_stacked_plot(Sub_L_M_patch_v2_df, "biomass", "Patch v2 - Biomass")
plot_v3_depth <- create_stacked_plot(Sub_L_M_patch_v3_df, "depth", "Patch v3 - Depth")
plot_v3_temperature <- create_stacked_plot(Sub_L_M_patch_v3_df, "temperature", "Patch v3 - Temperature")
plot_v3_biomass <- create_stacked_plot(Sub_L_M_patch_v3_df, "biomass", "Patch v3 - Biomass")
# Arrange the plots in a 3x3 grid
grid_plot <- plot_grid(
plot_v1_depth, plot_v2_depth, plot_v3_depth,
plot_v1_temperature, plot_v2_temperature, plot_v3_temperature,
plot_v1_biomass, plot_v2_biomass, plot_v3_biomass,
ncol = 3
)
# Display the grid plot
print(grid_plot)