-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemo_biomass_viz_new.R
167 lines (121 loc) · 5.06 KB
/
Demo_biomass_viz_new.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
#### 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 <- 100
# 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)
Sub_L_M_patch_v2_df$temperature <- predict(gam_depth_sim, newdata = Sub_L_M_patch_v2_df, se.fit = T)
Sub_L_M_patch_v3_df$temperature <- predict(gam_depth_sim, newdata = Sub_L_M_patch_v3_df, se.fit = T)
#### 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[["fit"]] - 2.916), 0, 2)/dnorm(0,0,2)
Sub_L_M_patch_v2_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[["fit"]] - 2.916), 0, 2)/dnorm(0,0,2)
Sub_L_M_patch_v3_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[["fit"]] - 2.916), 0, 2)/dnorm(0,0,2)