Skip to contents

Computes the 19 standard bioclimatic variables (BIO01-BIO19) from monthly climate raster data (SpatRaster objects with 12 layers, one per month) using terra's block-loop architecture for memory-efficient processing.

Usage

bioclim_raster(
  tas,
  tasmax,
  tasmin,
  pr,
  filename = "",
  n_blocks = NULL,
  ncores = 1L,
  overwrite = FALSE,
  ...
)

Arguments

tas

SpatRaster with 12 layers: monthly mean temperature.

tasmax

SpatRaster with 12 layers: monthly maximum temperature.

tasmin

SpatRaster with 12 layers: monthly minimum temperature.

pr

SpatRaster with 12 layers: monthly precipitation.

filename

Character string: output file path. Pass "" (default) to keep the result in memory.

n_blocks

Integer: target number of row blocks. If NULL (default), terra selects an appropriate number based on available memory.

ncores

Integer: number of CPU cores for within-block parallel processing via OpenMP. Default is 1 (sequential).

overwrite

Logical: whether to overwrite an existing output file. Default is FALSE.

...

Additional arguments passed to terra::writeStart().

Value

A SpatRaster with 19 layers named bio01 through bio19, sharing the spatial extent, resolution and CRS of tas.

Details

This function follows terra's block-loop pattern: the input rasters are read one horizontal block at a time (using terra::readStart(), terra::readValues(), and terra::readStop()), keeping peak memory use proportional to the block size rather than the full raster extent. Results are written to the output raster block by block (using terra::writeStart(), terra::writeValues(), and terra::writeStop()).

When ncores > 1, pixels within each block are processed in parallel using OpenMP threads via bioclim_cpp(). Only one block is held in memory at a time regardless of the number of threads.

See also

bioclim() for single-pixel (vector) computation.

Examples

library(terra)

# Create small test rasters: 4 rows x 3 cols, 12 monthly layers
make_rast <- function(vals) {
  r <- rast(nrows = 4, ncols = 3, nlyr = 12)
  values(r) <- vals
  r
}

n <- 4 * 3  # number of cells
tas    <- make_rast(matrix(rep(1:12, each = n), nrow = n, ncol = 12))
tasmax <- make_rast(matrix(rep(2:13, each = n), nrow = n, ncol = 12))
tasmin <- make_rast(matrix(rep(0:11, each = n), nrow = n, ncol = 12))
pr     <- make_rast(matrix(rep(1:12, each = n), nrow = n, ncol = 12))

# Compute all 19 bioclimatic variables in a single pass
result <- bioclim_raster(tas, tasmax, tasmin, pr)
nlyr(result)  # 19
#> [1] 19