sim_env <- terra::sds(terra::rast(vals = 1, nrow = 2, ncol = 2))
names(sim_env) <- "env_01"
test_sim <- metaRangeSimulation$new(source_environment = sim_env)
test_sim$add_species("species_01")
test_sim$add_traits(
"species_01",
trait_01 = matrix(1, nrow = 2, ncol = 2),
trait_02 = matrix(2, nrow = 2, ncol = 2)
)
file_prefix <- "This_could_be_a_time_step"
directory_name <- tempdir()
res_path <- save_species(
test_sim$species_01,
traits = "trait_01",
prefix = file_prefix,
path = directory_name
)
# the following should be TRUE
# but might fail due to floating point errors (that's why we round the values)
identical(
round(terra::as.matrix(terra::rast(res_path), wide = TRUE)),
round(test_sim$species_01$traits[["trait_01"]])
)
# test overwrite
res_path2 <- save_species(
test_sim$species_01,
traits = "trait_01",
prefix = file_prefix,
path = directory_name,
overwrite = TRUE
)
stopifnot(identical(res_path, res_path2))
# Saving all traits
res_path3 <- save_species(
test_sim$species_01,
prefix = basename(tempfile()),
path = directory_name
)
res_path3
# cleanup
unlink(c(res_path, res_path3))
stopifnot(all(!file.exists(res_path, res_path3)))
Run the code above in your browser using DataLab