Skip to contents

This function facilitates the import of rasters that have been generated and written to disk by the project_selected(), projection_changes(), variability_projections(), and projection_mop() functions. Users can select specific periods (past/future), emission scenarios, General Circulation Models (GCMs), and result types for import.

Usage

import_projections(
  projection,
  consensus = c("median", "range", "mean", "stdev"),
  present = TRUE,
  past_period = NULL,
  past_gcm = NULL,
  future_period = NULL,
  future_pscen = NULL,
  future_gcm = NULL,
  change_types = c("summary", "by_gcm", "by_change"),
  mop_types = c("simple", "basic", "towards_high_combined", "towards_low_combined",
    "towards_high_end", "towards_low_end")
)

Arguments

projection

an object of class model_projections, changes_projections, variability_projections, or mop_projections. This object is the direct output from one of the projection functions listed in the description.

consensus

(character) consensus measures to import. Available options are: 'median', 'range', 'mean' and 'stdev' (standard deviation). Default is c("median", "range", "mean", "stdev"), which imports all options. Only applicable if projection is a model_projections object.

present

(logical) wheter to import present-day projections. Default is TRUE. Not applicable if projection is a changes_projections object.

past_period

(character) names of specific past periods (e.g., 'LGM' or 'MID') to import. Default is NULL, meaning all available past periods will be imported.

past_gcm

(character) names of specific General Circulation Models (GCMs) from the past to import. Default is NULL, meaning all available past GCMs will be imported.

future_period

(character) names of specific future periods (e.g., '2041-2060' or '2081-2100') to import. Default is NULL, meaning all available future periods will be imported.

future_pscen

(character) names of specific future emission scenarios (e.g., 'ssp126' or 'ssp585') to import. Default is NULL, meaning all available future scenarios will be imported.

future_gcm

(character) names of specific General Circulation Models (GCMs) from the future to import. Default is NULL, meaning all available future GCMs will be imported.

change_types

(character) names of the type of computed changes to import. Available options are: 'summary', 'by_gcm', and 'by_change'. Default is c("summary", "by_gcm", "by_change"), importing all types. Only applicable if projection is a changes_projections object.

mop_types

(character) type(s) of MOP to import. Available options are: basic', 'simple', 'towards_high_combined', 'towards_low_combined', towards_high_end', and 'towards_low_end'. Default is NULL, meaning all available MOPs will be imported. Only applicable if projection is a mop_projections object.

Value

A SpatRaster or a list of SpatRasters, structured according to the input projection class:

  • If projection is model_projections: A stacked SpatRaster containing all selected projections.

  • If projection is changes_projections: A list of SpatRasters, organized by the selected change_types (e.g., 'summary', 'by_gcm', and/or 'by_change').

  • If projection is mop_projections: A list of SpatRasters, organized by the selected mop_types (e.g., 'simple' and 'basic').

  • If projection is variability_projections: A list of SpatRasters, containing the computed variability.

Examples

# Load packages
library(terra)
#> terra 1.8.54
# Step 1: Organize variables for current projection
## Import current variables (used to fit models)
var <- terra::rast(system.file("extdata", "Current_variables.tif",
                               package = "kuenm2"))

## Create a folder in a temporary directory to copy the variables
out_dir_current <- file.path(tempdir(), "Current_raw2")
dir.create(out_dir_current, recursive = TRUE)

## Save current variables in temporary directory
terra::writeRaster(var, file.path(out_dir_current, "Variables.tif"))


# Step 2: Organize future climate variables (example with WorldClim)
## Directory containing the downloaded future climate variables (example)
in_dir <- system.file("extdata", package = "kuenm2")

## Create a folder in a temporary directory to copy the future variables
out_dir_future <- file.path(tempdir(), "Future_raw2")

## Organize and rename the future climate data (structured by year and GCM)
### 'SoilType' will be appended as a static variable in each scenario
organize_future_worldclim(input_dir = in_dir, output_dir = out_dir_future,
                          name_format = "bio_", fixed_variables = var$SoilType)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |======================================================================| 100%
#> 
#> Variables successfully organized in directory:
#> /tmp/RtmppZ1hZC/Future_raw2

# Step 3: Prepare data to run multiple projections
## An example with maxnet models
## Import example of fitted_models (output of fit_selected())
data(fitted_model_maxnet, package = "kuenm2")

## Prepare projection data using fitted models to check variables
pr <- prepare_projection(models = fitted_model_maxnet,
                         present_dir = out_dir_current,
                         future_dir = out_dir_future,
                         future_period = c("2041-2060", "2081-2100"),
                         future_pscen = c("ssp126", "ssp585"),
                         future_gcm = c("ACCESS-CM2", "MIROC6"),
                         raster_pattern = ".tif*")

# Step 4: Run multiple model projections
## A folder to save projection results
out_dir <- file.path(tempdir(), "Projection_results/maxnet")
dir.create(out_dir, recursive = TRUE)

## Project selected models to multiple scenarios
p <- project_selected(models = fitted_model_maxnet, projection_data = pr,
                      out_dir = out_dir)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |======================================================================| 100%

# Use import_projections to import results:
raster_p <- import_projections(projection = p, consensus = "mean")
#> Error in import_projections(projection = p, consensus = "mean"): object 'p' not found
plot(raster_p)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'raster_p' not found

# Step 5: Identify areas of change in projections
## Contraction, expansion and stability
changes <- projection_changes(model_projections = p, output_dir = out_dir,
                              overwrite = TRUE)
# Use import_projections to import results:
raster_changes <- import_projections(projection = changes,
                                     change_type = c("summary", "by_gcm"))

plot(raster_changes$by_gcm)

plot(raster_changes$Summary)


# Step 6: Perform MOP for all projection scenarios
## Create a folder to save MOP results
out_dir <- file.path(tempdir(), "MOP_results")
dir.create(out_dir, recursive = TRUE)

#Import prepared data to serve as a base for MOP comparisons
data(sp_swd_cat, package = "kuenm2")

## Run MOP
kmop <- projection_mop(data = sp_swd_cat, projection_data = pr,
                       out_dir = out_dir, fitted_models = fitted_model_maxnet,
                       type = "detailed")
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |======================================================================| 100%
# Use import_projections to import results:
raster_mop <- import_projections(projection = kmop,
                                 mop_types = c("simple", "basic",
                                               "towards_high_combined",
                                               "towards_low_combined"))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'unique': object 'p' not found
plot(raster_mop$simple)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'raster_mop' not found
plot(raster_mop$basic)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'raster_mop' not found
plot(raster_mop$towards_high_combined)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'raster_mop' not found
plot(raster_mop$towards_low_combined)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'raster_mop' not found

# Step 7: Compute variance from distinct sources
## Set folder to save results
out_dir <- file.path(tempdir())
v <- projection_variability(model_projections = p, by_replicate = FALSE,
                            write_files = TRUE, output_dir = out_dir,
                            overwrite=TRUE)
#> Calculating variability from distinct models: scenario 1 of 5
#> Calculating variability from distinct models: scenario 2 of 5
#> Calculating variability from distinct GCMs: scenario 2 of 5
#> Calculating variability from distinct models: scenario 3 of 5
#> Calculating variability from distinct GCMs: scenario 3 of 5
#> Calculating variability from distinct models: scenario 4 of 5
#> Calculating variability from distinct GCMs: scenario 4 of 5
#> Calculating variability from distinct models: scenario 5 of 5
#> Calculating variability from distinct GCMs: scenario 5 of 5
v
#> $Present
#> class       : SpatRaster 
#> size        : 52, 40, 1  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -53.5, -46.83333, -30.83333, -22.16667  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> name        :    by_model 
#> min value   : 8.74016e-18 
#> max value   : 1.78190e-01 
#> 
#> $`Future_2041-2060_ssp126`
#> class       : SpatRaster 
#> size        : 52, 40, 2  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -53.5, -46.83333, -30.83333, -22.16667  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :     by_model,       by_gcm 
#> min values  : 3.919573e-24, 2.292895e-20 
#> max values  : 3.273341e-01, 1.272195e-01 
#> 
#> $`Future_2041-2060_ssp585`
#> class       : SpatRaster 
#> size        : 52, 40, 2  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -53.5, -46.83333, -30.83333, -22.16667  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :     by_model,       by_gcm 
#> min values  : 4.933464e-24, 5.044065e-24 
#> max values  : 3.362731e-01, 1.425250e-01 
#> 
#> $`Future_2081-2100_ssp126`
#> class       : SpatRaster 
#> size        : 52, 40, 2  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -53.5, -46.83333, -30.83333, -22.16667  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :     by_model,       by_gcm 
#> min values  : 3.264598e-23, 2.467159e-20 
#> max values  : 2.897881e-01, 1.651514e-01 
#> 
#> $`Future_2081-2100_ssp585`
#> class       : SpatRaster 
#> size        : 52, 40, 2  (nrow, ncol, nlyr)
#> resolution  : 0.1666667, 0.1666667  (x, y)
#> extent      : -53.5, -46.83333, -30.83333, -22.16667  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :     by_model,       by_gcm 
#> min values  : 6.933348e-33, 1.386670e-32 
#> max values  : 2.583937e-01, 2.152776e-01 
#> 
#> $root_directory
#> [1] "/tmp/RtmppZ1hZC/variance"
#> 
#> attr(,"class")
#> [1] "variability_projections"
raster_variability <- import_projections(projection = v,
                                         future_period = "2041-2060")
plot(raster_variability)