Appendix A — Appendix 1 - WordClim 2.1 data transformation for NetLogo
A.1 Preset
Code
# dir <- rstudioapi::selectDirectory()
dir <- here::here("data-raw", "worldclim")
if (!checkmate::test_directory_exists(dir)) dir.create(dir)
A.2 Loading file
Code
file <- here::here("data-raw", "worldclim", "wc2.1_10m_tmin_03.tif")
Code
ras <- raster::raster(file)
A.3 Crop raster file
Code
br_shape <- geobr::read_country()
Code
raster::plot(croped_ras)
A.4 Transform raster in .asc
Code
files <-
here::here("data-raw", "worldclim") |>
list.files(full.names = TRUE, pattern = "tif$", recursive = TRUE)
Code
convert_to_asc <- function(file) {
checkmate::assert_character(file)
years <- c(
1872, 1900, 1911, 1920, 1933, 1940, 1950, 1960, 1970, 1980, 1991, 2000,
2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
)
for (i in file) {
checkmate::assert_file(i, access = "r")
year <-
i |>
stringr::str_extract("19[0-9]{2}|20[0-9]{2}") |>
as.numeric()
if (is.na(year)){
cli::cli_abort("Year not found in the file name.")
}
br_shape <- geobr::read_country(
year = years[which.min(abs(years - year))],
showProgress = FALSE
)
asc_file <- stringr::str_replace(i, "tif$", "asc")
i |>
raster::raster() |>
raster::crop(raster::extent(br_shape)) |>
raster::mask(br_shape) |>
raster::aggregate(fact = 2) |>
raster::writeRaster(
filename = asc_file,
format = "ascii",
overwrite = TRUE
)
}
invisible(NULL)
}
Code
convert_to_asc(files)
beepr::beep(1)
A.5 Extracting data
Code
files <-
"C:/Users/Daniel/Documents/GitHub/gs-abm/data-raw/worldclim/" |>
list.files(full.names = TRUE, pattern = "asc$", recursive = TRUE)
A.5.1 Months
Code
stringr::str_extract(files, "(?<=-)[0-9]{2}(?=.asc)") |>
rutils:::drop_na() |>
unique()
A.5.2 Historical years
Code
stringr::str_extract(files, "[0-9]{4}(?=-[0-9]{2}.asc)") |>
rutils:::drop_na() |>
unique()
A.5.3 Historical year-month
Code
stringr::str_extract(files, "[0-9]{4}-[0-9]{2}(?=.asc)") |>
rutils:::drop_na() |>
unique() |>
lubridate::ym()
A.5.4 Future years
Code
stringr::str_extract(files, "[0-9]{4}-[0-9]{4}(?=.asc)") |>
rutils:::drop_na() |>
unique()
A.6 GCM
Code
stringr::str_extract(files, "(?<=_)[A-Z].+(?=_ssp[0-9]{3}_[0-9]{4}-[0-9]{4}.asc)") |>
rutils:::drop_na() |>
unique()
A.6.1 SSP
Code
stringr::str_extract(files, "ssp[0-9]{3}(?=_[0-9]{4}-[0-9]{4}.asc)") |>
rutils:::drop_na() |>
unique()