auk_set_awk_path("C:/Program Files\\Git\\usr\\bin\\gawk.exe", overwrite=TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite=TRUE)
?auk_set_awk_path
auk_set_awk_path("/usr/bin/awk", overwrite=TRUE)
auk_set_awk_path("C:/cygwin64/bin/gawk.exe", overwrite=TRUE)
auk_get_awk_path()
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite=TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/", overwrite=TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/awk.exe", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite = TRUE)
system('"C:/Program Files/Git/usr/bin/awk.exe" --version')
Sys.setenv(AWK_PATH = "C:/Program Files/Git/usr/bin")
Sys.getenv("AWK_PATH")
auk_filter(ebd_filters,
file = ebd_filtered,
file_sampling = sampling_filtered,
overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/awk", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk", overwrite = TRUE)
library(auk)
library(dplyr)
# Set file paths
ebd_file <- "G:/Biodiversity/eBird/ebd_PT_202101_202512_smp_relNov-2025/ebd_PT_202101_202512_smp_relNov-2025.txt"
sampling_file <- "G:/Biodiversity/eBird/ebd_PT_202101_202512_smp_relNov-2025/ebd_PT_202101_202512_smp_relNov-2025_sampling.txt"
# Define output files (will be created in your working directory)
ebd_filtered <- "ebd_PT_stationary_complete.txt"
sampling_filtered <- "sampling_PT_stationary_complete.txt"
# Create filter object
ebd_filters <- auk_ebd(ebd_file, file_sampling = sampling_file) %>%
auk_complete() %>%  # Only complete checklists
auk_protocol(protocol = "Stationary")  # Only stationary surveys
# Apply filters
auk_filter(ebd_filters,
file = ebd_filtered,
file_sampling = sampling_filtered,
overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/awk.exe", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/awk", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/", overwrite = TRUE)
auk_set_awk_path("C:/cygwin64/bin/gawk.exe", overwrite=TRUE)
auk_set_awk_path("C:/cygwin64/bin/awk.exe", overwrite=TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/awk.exe", overwrite = TRUE)
auk_set_awk_path("C:/Program Files/Git/usr/bin/gawk.exe", overwrite = TRUE)
auk_set_awk_path("C:\cygwin64\bin\gawk.exe\bin\gawk.exe", overwrite = TRUE)
auk_set_awk_path("C:\\cygwin64\\bin\\gawk.exe\\bin\\gawk.exe", overwrite = TRUE)
library(rgbif)
library(sf)
library(dplyr)
library(leaflet)
library(ggplot2)
library(tidyterra)
library(terra)
library(gridExtra)
library(eks) # kernel smoothing functions
library(gstat)
rm(list=ls())
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1"  # <- change to your root!
setwd(mywd)
setwd(paste0(mywd,"/GIS/Administrative"))
?dir.create
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1"  # <- change to your root!
setwd(mywd)
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1_20260302"  # <- change to your root!
setwd(mywd)
dir.create(paste0(mywd,"/GIS"), recursive = T)
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1_20260302"  # <- change to your root!
setwd(mywd)
setwd(mywd)
# dir.create(paste0(mywd,"/GIS"), recursive = T)
setwd(paste0(mywd,"/GIS"))
dir.create(paste0(mywd,"/GIS"), recursive = T)
setwd(mywd)
# study area
Iberia <- st_read(paste0(mywd, "/Administrative/Iberia.shp"))
Iberia
Iberia4326 <- st_transform(Iberia, 4326)
plot(st_geometry(Iberia4326))
# select species name
taxon_name_query <- "Pelodytes ibericus"
species_key_ <- rgbif::name_backbone(taxon_name_query)
species_key_
species_key <- species_key_[[1]]
occ = rgbif::occ_data(taxonKey=species_key,
country = c("PT","ES"),
hasCoordinate = TRUE,
limit = 10000)
str(occ)
nrow(occ$PT$data)
nrow(occ$ES$data)
occ_data1 = occ$PT$data
occ_data2 = occ$ES$data
occ_data <- bind_rows(occ_data1, occ_data2)
names(occ_data)
nrow(occ_data)
table(occ_data$basisOfRecord)
table(occ_data$country)
# select columns
(occ_myd <-  occ_data %>%
select(order, familyKey, scientificName, decimalLongitude, decimalLatitude,
year, month, day, eventDate))
# plot by year
#
ggplot(occ_myd, aes(year)) +
geom_bar() +
theme_minimal()
# convert to spatial
occ_sf <- st_as_sf(occ_myd,
coords = c("decimalLongitude","decimalLatitude"),
crs=4326)
names(occ_sf)
leaflet() %>%
setView(lng = -4, lat = 39.5, zoom = 6) %>%
addTiles() %>%
addCircles(lng = ~decimalLongitude, lat = ~decimalLatitude,
data = occ_myd)
occ_sf # <-- WGS84
mystudyarea3763 <- myNUTS %>%
filter(CNTR_CODE  == "PT") %>%
st_union() %>%
st_transform(., 3763)
mystudyarea3763 <- Iberia %>%
filter(CNTR_CODE  == "PT") %>%
st_union() %>%
st_transform(., 3763)
Iberia
mystudyarea3763 <- Iberia %>%
filter(shapeISO  == "PT") %>%
st_union() %>%
st_transform(., 3763)
plot(st_geometry(mystudyarea3763))
mystudyarea3763 <- Iberia %>%
filter(shapeISO  == "PT")
mystudyarea3763
Iberia
mystudyarea3763 <- Iberia %>%
filter(shapeISO  == "PRT") %>%
st_union() %>%
st_transform(., 3763)
plot(st_geometry(mystudyarea3763))
index <- occ_sf %>%
st_transform(., 3763) %>%
st_intersects(mystudyarea3763, sparse = F)
occ_sf3763 <- occ_sf %>%
st_transform(., 3763)
occ_sf3763 <- occ_sf3763[index,]
(g1 <- ggplot(mystudyarea3763) +
geom_sf(fill="grey99", col="grey") +
geom_sf(data=occ_sf3763, col="blue") +
theme(legend.position = "none"))
my_template1k <- rast(ext(occ_sf3763), res=1000)
my_template5k <- rast(ext(occ_sf3763), res=5000)
my_template10k <- rast(ext(occ_sf3763), res=10000)
myr1k <- rasterize(occ_sf3763, my_template1k)
myr5k <- rasterize(occ_sf3763, my_template5k)
myr10k <- rasterize(occ_sf3763, my_template10k)
myr10k_ <- rasterize(occ_sf3763, my_template10k, fun=sum)
(r1 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = myr1k) +
scale_fill_gradient(na.value = NA) +
theme(legend.position = "none"))
(r2 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = myr5k) +
scale_fill_gradient(na.value = NA) +
theme(legend.position = "none"))
(r3 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = myr10k) +
scale_fill_gradient(na.value = NA) +
theme(legend.position = "none"))
(r4 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = myr10k_) +  # log?
scale_fill_viridis_c(na.value = NA) )
grid.arrange(r1, r2,
r3, r4)
names(myr10k_) <- "density10k"
myr10k_
names(myr10k_) <- "density10k"
myr10k_df <- as.data.frame(myr10k_, xy = TRUE)
head(myr10k_df)
names(myr10k_df) <- c("x", "y", "z")
idw_r <- interpIDW(myr10k_, as.matrix(myr10k_df), radius=50000, power=1, smooth=1, maxPoints=5)
crs(idw_r) <- crs(mystudyarea3763)
idw_r <- interpIDW(myr10k_, as.matrix(myr10k_df), radius=50000, power=1, smooth=1, maxPoints=5)
crs(idw_r) <- crs(mystudyarea3763)
crs(idw_r)
st_crs(mystudyarea3763)
crs(idw_r) <- st_crs(mystudyarea3763)
crs(idw_r) <- 3763
mystudyarea3763
crs(idw_r) <- crs(vect(mystudyarea3763))
crs(idw_r)
# Create a template raster covering the full study area extent
mystudyarea3763_r <- rast(vect(mystudyarea3763), resolution = 10000)
# Rasterize the study area polygon (1 inside, NA outside)
mystudyarea3763_rast <- rasterize(vect(mystudyarea3763), mystudyarea3763_r, touches=TRUE, field = 1)
# Resample IDW to match the template
idw_r <- resample(idw_r, mystudyarea3763_rast)
# Mask: keeps values only inside the study area,
# but ensures the full extent is covered
idw_r <- mask(idw_r, mystudyarea3763_rast)
plot(log(idw_r+1))
(r5 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = log(idw_r+1)) +
geom_sf(fill=NA, col="black", lwd=2) +
geom_sf(fill=NA, col="white") +
scale_fill_viridis_c(na.value = NA))
(r5b <- r5 +
geom_sf(data = occ_sf3763, colour = "white", fill = NA))
idw_r <- interpIDW(myr10k_, as.matrix(myr10k_df), radius=10000, power=1, smooth=1, maxPoints=5)
crs(idw_r) <- crs(vect(mystudyarea3763))
# Create a template raster covering the full study area extent
mystudyarea3763_r <- rast(vect(mystudyarea3763), resolution = 10000)
# Rasterize the study area polygon (1 inside, NA outside)
mystudyarea3763_rast <- rasterize(vect(mystudyarea3763), mystudyarea3763_r, touches=TRUE, field = 1)
# Resample IDW to match the template
idw_r <- resample(idw_r, mystudyarea3763_rast)
# Mask: keeps values only inside the study area,
# but ensures the full extent is covered
idw_r <- mask(idw_r, mystudyarea3763_rast)
plot(log(idw_r+1))
(r5 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = log(idw_r+1)) +
geom_sf(fill=NA, col="black", lwd=2) +
geom_sf(fill=NA, col="white") +
scale_fill_viridis_c(na.value = NA))
(r5b <- r5 +
geom_sf(data = occ_sf3763, colour = "white", fill = NA))
idw_r <- interpIDW(myr10k_, as.matrix(myr10k_df), radius=50000, power=1, smooth=1, maxPoints=5)
crs(idw_r) <- crs(vect(mystudyarea3763))
# Create a template raster covering the full study area extent
mystudyarea3763_r <- rast(vect(mystudyarea3763), resolution = 10000)
# Rasterize the study area polygon (1 inside, NA outside)
mystudyarea3763_rast <- rasterize(vect(mystudyarea3763), mystudyarea3763_r, touches=TRUE, field = 1)
# Resample IDW to match the template
idw_r <- resample(idw_r, mystudyarea3763_rast)
# Mask: keeps values only inside the study area,
# but ensures the full extent is covered
idw_r <- mask(idw_r, mystudyarea3763_rast)
plot(log(idw_r+1))
(r5 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = log(idw_r+1)) +
geom_sf(fill=NA, col="black", lwd=2) +
geom_sf(fill=NA, col="white") +
scale_fill_viridis_c(na.value = NA))
# Create a template raster covering the full study area extent
mystudyarea3763_r <- rast(vect(mystudyarea3763), resolution = 1000)
# Rasterize the study area polygon (1 inside, NA outside)
mystudyarea3763_rast <- rasterize(vect(mystudyarea3763), mystudyarea3763_r, touches=TRUE, field = 1)
# Resample IDW to match the template
idw_r <- resample(idw_r, mystudyarea3763_rast)
# Mask: keeps values only inside the study area,
# but ensures the full extent is covered
idw_r <- mask(idw_r, mystudyarea3763_rast)
plot(log(idw_r+1))
(r5 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = log(idw_r+1)) +
geom_sf(fill=NA, col="black", lwd=2) +
geom_sf(fill=NA, col="white") +
scale_fill_viridis_c(na.value = NA))
(r5b <- r5 +
geom_sf(data = occ_sf3763, colour = "white", fill = NA))
# Kernel density
library(spatialEco)
crs(myr10k_) <- crs(occ_sf3763)
pt.kde <- sf.kde(x = occ_sf3763,
bw = 20000,
standardize = TRUE,
res=1000)
pt.kde <- mask(pt.kde, mystudyarea3763)
plot(pt.kde)
(r6 <- ggplot(mystudyarea3763) +
geom_sf(fill=NA, col="grey") +
theme_void() +
geom_spatraster(data = log(pt.kde+1)) +
geom_sf(fill=NA, col="black", lwd=2) +
geom_sf(fill=NA, col="white") +
scale_fill_viridis_c(na.value = NA))
(rbb <- r6 +
geom_sf(data = occ_sf3763, colour = "white", fill = NA))
library(geodata)
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1"  # <- change to your root!
setwd(mywd)
dir(pattern=".shp")
mywd <- "C:/Users/fjascensao/OneDrive - Universidade de Lisboa/Aulas/MIBC/Classes/Class_1_20260302"  # <- change to your root!
dir()
setwd(mywd)
dir()
mystudyarea <- st_read("/Administrative/Iberia.shp")
mystudyarea <- st_read("Administrative/Iberia.shp")
# Worldclim
?worldclim_global
mypath = file.path(mywd, "GIS/Env_var")
mypath = file.path(mywd, "Env_var")
mypath
file.path(mywd, "Env_var")
mypath = setwd(file.path(mywd, "Env_var"))
dir()
mypath = setwd(file.path(mywd, "Env_var", "climate"))
dir()
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
worldclim_country
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=NULL, version="2.1")
mypath = setwd(file.path(mywd, "Env_var"))
dir()
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
mypath = setwd(file.path(mywd, "Env_var", "climate"))
dir()
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
setwd(mypath)
mypath
dir()
mypath = setwd(file.path(mywd, "Env_var", "climate"))
dir()
dir()
mypath = setwd(file.path(mywd, "Env_var", "climate"))
dir()
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
mypath
mypath = (file.path(mywd, "Env_var", "climate"))
dir()
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
setwd(mywd)
dir()
mypath = (file.path(mywd, "Env_var", "climate"))
dir()
mypath = file.path(mywd, "Env_var", "climate")
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
mypath = file.path(mywd, "Env_var", "climate")
mypath
mypath = file.path(mywd, "Env_var", "climate","wc2.1_country")
mypath
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
mypath = file.path(mywd, "Env_var")
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
mypath = file.path(mywd, "Env_var")
# includes Portugal and Spain / will create a new 'climate' folder
worldclim_country(country="ES", var="bio", path=mypath, version="2.1")
setwd(file.path(mypath, "climate", "wc2.1_country"))
dir()
bioclim_es <- rast("ESP_wc2.1_30s_bio.tif")
plot(bioclim_es)
mystudyarea4326 <- st_transform(mystudyarea, 4326)
bioclim_iberia <- terra::crop(bioclim_es, mystudyarea4326, mask=TRUE)
plot(bioclim_iberia) # remove 8 and 9
plot(bioclim_iberia[[-c(8,9)]])
# change resolution and project to 3035
my_bioclim <- project(bioclim_iberia[[-c(8,9)]], crs("EPSG:3035"), res=1000)
names(my_bioclim) <- gsub("wc2.1_30s_", "", names(my_bioclim))
ifelse(!dir.exists(file.path(mywd, "GIS", "Iberia")),
dir.create(file.path(mywd, "GIS", "Iberia")), FALSE)
setwd(file.path(mywd, "GIS", "Iberia"))  #  <--- attention where you store this!!!!!!
setwd(file.path(mywd, "Env_var","Bioclim"))  #  <--- attention where you store this!!!!!!
dir.create(file.path(mywd, "Env_var","Bioclim"))
setwd(file.path(mywd, "Env_var","Bioclim"))  #  <--- attention where you store this!!!!!!
writeRaster(my_bioclim, "my_bioclim.tif", overwrite=T)
writeRaster(my_bioclim, filename=paste0(names(my_bioclim), ".asc"), overwrite=T, NAflag=-9999)
library(pastclim)
list_available_datasets()[grepl("WorldClim_2.1",list_available_datasets())]
get_vars_for_dataset(dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_10m")
set_data_path(path_to_nc =file.path(mywd, "GIS", "climate future"))
set_data_path(path_to_nc =file.path(mywd, "Env_var", "climate future"))
# # A complete list of available combinations can be obtained with:
#
# # list_available_datasets()[grepl("WorldClim_2.1",list_available_datasets())]
# # So, if we are interested in the the HadGEM3-GC31-LL model, with ssp set to 245 and at 10 arc-minutes, we can get the available variables:
#
# get_vars_for_dataset(dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_10m")
#
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
future_slice <- region_slice(
time_ce = 2030,
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
future_slice <- region_slice(
time_ce = 2030,
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
set_data_path(path_to_nc =file.path(mywd, "Env_var", "climate future"))
file.path(mywd, "Env_var", "climate future")
future_slice <- region_slice(
time_ce = 2030,
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
file.path(mywd, "Env_var", "climate future")
set_data_path(path_to_nc =file.path(mywd, "Env_var"))
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m",
bio_variables = c("bio01", "bio02")
)
set_data_path(path_to_nc =file.path(mywd, "Env_var", "Future_climate"))
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m"
)
unlink(file.path(mywd, "Env_var", "Future_climate"), recursive = TRUE)
unlink(file.path(mywd, "Env_var", "Future_climate"), recursive = TRUE)
disk.size()
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m"
)
set_data_path(path_to_nc =file.path(mywd, "Env_var", "Future_climate"))
set_data_path(path_to_nc =file.path(mywd, "Env_var", "Future_climate"))
# # We can now download "bio01" and "bio02" for that dataset with:
#
download_dataset(
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m"
)
# read rasters ------------------------------------------------------------
library(terra)
setwd(file.path(mywd, "Env_var", "Future_climate", "worldclim_2.1"))
dir()
bio_ssp245_2030 <- rast("wc2.1_2.5m_bioc_HadGEM3-GC31-LL_ssp245_2021-2040.tif")
bio_ssp245_2030 <- terra::crop(bio_ssp245_2030, mystudyarea4326, mask=TRUE)
bio_ssp245_2030 <- project(bio_ssp245_2030[[-c(8,9)]], crs("EPSG:3035"), res=1000)
names(bio_ssp245_2030) <- gsub("wc2.1_30s_", "", names(my_bioclim))
bio_ssp245_2030 <- resample(bio_ssp245_2030, my_bioclim[[1]])
plot(bio_ssp245_2030[[1]])
plot(bio_ssp245_2030)
plot(bio_ssp245_2030[[1]]-my_bioclim[[1]])
future_slice <- region_slice(
time_ce = 2030,
dataset = "WorldClim_2.1_HadGEM3-GC31-LL_ssp245_2.5m"
)
dir.create(file.path(mywd, "Env_var", "Future_bioclim"))
setwd(file.path(mywd, "Env_var", "Future_bioclim"))
bio_ssp245_2030
nlyr(bio_ssp245_2030)
bio_ssp245_2030 <- rast("wc2.1_2.5m_bioc_HadGEM3-GC31-LL_ssp245_2021-2040.tif")
setwd(file.path(mywd, "Env_var", "Future_climate", "worldclim_2.1"))
dir()
bio_ssp245_2030 <- rast("wc2.1_2.5m_bioc_HadGEM3-GC31-LL_ssp245_2021-2040.tif")
bio_ssp245_2030 <- terra::crop(bio_ssp245_2030, mystudyarea4326, mask=TRUE)
bio_ssp245_2030 <- project(bio_ssp245_2030, crs("EPSG:3035"), res=1000)
names(bio_ssp245_2030) <- gsub("wc2.1_30s_", "", names(my_bioclim))
names(bio_ssp245_2030)
names(my_bioclim)
bio_ssp245_2030 <- rast("wc2.1_2.5m_bioc_HadGEM3-GC31-LL_ssp245_2021-2040.tif")
bio_ssp245_2030 <- terra::crop(bio_ssp245_2030, mystudyarea4326, mask=TRUE)
bio_ssp245_2030 <- project(bio_ssp245_2030[[-c(8,9)]], crs("EPSG:3035"), res=1000)
names(bio_ssp245_2030) <- gsub("wc2.1_30s_", "", names(my_bioclim))
bio_ssp245_2030 <- resample(bio_ssp245_2030, my_bioclim[[1]])
plot(bio_ssp245_2030[[1]])
plot(bio_ssp245_2030[[1]]-my_bioclim[[1]])
# save
dir.create(file.path(mywd, "Env_var", "Future_bioclim"))
setwd(file.path(mywd, "Env_var", "Future_bioclim"))
names(bio_ssp245_2030)
setwd(file.path(mywd, "Env_var", "Future_bioclim"))
for (i in 1:nlyr(bio_ssp245_2030)) {
layer_name <- names(bio_ssp245_2030)[i]
writeRaster(
bio_ssp245_2030[[i]],
filename = paste0("asc_layers/", layer_name, ".asc"),
filetype = "AAIGrid",
overwrite = TRUE
)
}
bio_ssp245_2030[[i]]
for (i in 1:nlyr(bio_ssp245_2030)) {
layer_name <- names(bio_ssp245_2030)[i]
writeRaster(
bio_ssp245_2030[[i]],
filename = paste0(layer_name, ".asc"),
filetype = "AAIGrid",
overwrite = TRUE
)
}
