-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVSC_LPS2025.R
More file actions
161 lines (138 loc) · 5.83 KB
/
VSC_LPS2025.R
File metadata and controls
161 lines (138 loc) · 5.83 KB
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
# Virtual Species Suitability Data Cube Tutorial
# Description: Simulate habitat suitability for three virtual species in Madagascar
# using bioclimatic variables under current (2020) and future (2070) climate scenarios.
# =========================================================
# 1. Load Required Libraries
# =========================================================
library(terra) # for raster data handling
library(virtualspecies) # for generating virtual species with response curves
library(geodata) # for downloading climate and administrative data
library(sf) # for vector data and spatial features
library(stars) # for creating multidimensional spatio-temporal cubes
library(ggplot2) # for visualization
library(tidyverse) # for general data manipulation
# =========================================================
# 2. Download and Crop Bioclimatic Data for Madagascar
# =========================================================
# Bioclimatic variables represent biologically meaningful climate summaries
# bio1 = annual mean temperature
# bio12 = annual precipitation
# bio15 = precipitation seasonality
# Download present and future climate data
BIO_current <- worldclim_country("MG", var = "bio", res = 5, path = tempdir())
BIO_future_global <- cmip6_world(
model = "CNRM-CM6-1", ssp = "585", time = "2061-2080",
var = "bio", res = 5, version = "2.1", path = tempdir()
)
BIO_future <- crop(BIO_future_global, BIO_current)
# Select 3 relevant variables
BIO_current_sub <- BIO_current[[c("wc2.1_30s_bio_1", "wc2.1_30s_bio_12", "wc2.1_30s_bio_15")]]
BIO_future_sub <- BIO_future[[c("bio01", "bio12", "bio15")]]
# Rename for simplicity
names(BIO_current_sub) <- names(BIO_future_sub) <- c("bio1", "bio12", "bio15")
# =========================================================
# 3. Define Virtual Species via Ecological Preferences
# =========================================================
# Each species has a Gaussian response curve to each variable.
# These represent:
# - Species 1: Temperate generalist
# - Species 2: Warm-adapted species
# - Species 3: Cool-adapted, dry-specialist
curve_list <- list(
formatFunctions(
# species 1
bio1 = c(fun = 'dnorm', mean = 15, sd = 2),
bio12 = c(fun = 'dnorm', mean = 800, sd = 200),
bio15 = c(fun = 'dnorm', mean = 60, sd = 10)
),
formatFunctions(
# species 2
bio1 = c(fun = 'dnorm', mean = 22, sd = 3),
bio12 = c(fun = 'dnorm', mean = 1200, sd = 300),
bio15 = c(fun = 'dnorm', mean = 80, sd = 10)
),
formatFunctions(
# species 3
bio1 = c(fun = 'dnorm', mean = 10, sd = 2),
bio12 = c(fun = 'dnorm', mean = 400, sd = 100),
bio15 = c(fun = 'dnorm', mean = 40, sd = 8)
)
)
# =========================================================
# 4. Generate Virtual Species Suitability (Current & Future)
# =========================================================
species_current <- list()
species_future <- list()
for (i in 1:3) {
species_current[[paste0("species_", i)]] <- generateSpFromFun(
raster.stack = BIO_current_sub,
parameters = curve_list[[i]],
rescale = FALSE
)
species_future[[paste0("species_", i)]] <- generateSpFromFun(
raster.stack = BIO_future_sub,
parameters = curve_list[[i]],
rescale = FALSE
)
}
# =========================================================
# 5. Create Regular Grid Over Madagascar
# =========================================================
madagascar_shape <- gadm("Madagascar", level = 0, path = tempdir())
# Create a hexagonal grid with 0.9° spacing
mad_grid <- st_make_grid(
madagascar_shape,
cellsize = 0.7,
what = "polygons",
square = FALSE
) %>%
st_as_sf() %>%
mutate(cell_id = 1:n())
# =========================================================
# 6. Aggregate Suitability Over Spatial Grid
# =========================================================
suitability_aggregated <- list()
for (i in 1:3) {
sp <- paste0("species_", i)
# Convert terra raster to stars object
r_2020 <- st_as_stars(species_current[[sp]]$suitab.raster)
r_2070 <- st_as_stars(species_future[[sp]]$suitab.raster)
# Aggregate mean suitability over each cell of the grid
suitability_aggregated[[paste0(sp, "_2020")]] <- aggregate(r_2020, mad_grid, FUN = mean, na.rm = TRUE)
suitability_aggregated[[paste0(sp, "_2070")]] <- aggregate(r_2070, mad_grid, FUN = mean, na.rm = TRUE)
}
# =========================================================
# 7. Build Suitability Data Cube (stars)
# =========================================================
# Combine species into stars objects for each time period
current_cube <- c(
suitability_aggregated$species_1_2020,
suitability_aggregated$species_2_2020,
suitability_aggregated$species_3_2020
)
future_cube <- c(
suitability_aggregated$species_1_2070,
suitability_aggregated$species_2_2070,
suitability_aggregated$species_3_2070
)
# Add "species" dimension
current_cube <- st_redimension(current_cube) %>%
st_set_dimensions(2, values = paste0("species_", 1:3), names = "species")
future_cube <- st_redimension(future_cube) %>%
st_set_dimensions(2, values = paste0("species_", 1:3), names = "species")
# Combine along the time dimension
suitability_cube <- c(current_cube, future_cube, along = list(time = c(2020, 2070)))
# Set final dimension names
suitability_cube <- st_set_dimensions(suitability_cube, 1, names = "cell")
names(suitability_cube) <- "suitability"
# =========================================================
# 8. Explore and Visualize the Data Cube
# =========================================================
print(suitability_cube)
ggplot() +
geom_stars(data = suitability_cube) +
facet_grid(time ~ species) +
scale_fill_viridis_c(name = "Suitability", na.value = "white") +
theme_minimal() +
labs(title = "Suitability of Virtual Species in Madagascar",
subtitle = "Under Present (2020) and Future (2070) Climate Conditions")