Skip to contents

Preprocessing function that standardizes raster files using precomputed mean and standard deviation values from species occurrence data. Supports both temporally static and dynamic rasters.

Usage

scale_rasters(input_dir, output_dir, scaling_params_file,
              variable_patterns, time_cols = NULL, output_suffix = "_Scaled",
              overwrite = FALSE, verbose = TRUE)

Arguments

input_dir

Character. Directory containing aligned input .tif raster files, typically the output of raster_align.

output_dir

Character. Directory where scaled rasters will be saved.

scaling_params_file

Character. Path to CSV file with columns: variable, mean, sd. Typically generated by temporally_explicit_extraction.

variable_patterns

Named character vector mapping clean variable names to raster filename patterns. For time-varying variables include the time placeholder in the pattern (e.g. "forest_cover" = "forest_cover_YEAR"); for static variables omit it (e.g. "elevation" = "elevation"). Time placeholders must match entries in time_cols.

time_cols

Character vector of time placeholders for dynamic variables. Default is NULL.

output_suffix

Character. Suffix to append to output raster filenames. Default is "_Scaled".

overwrite

Logical. If TRUE, overwrites existing output files. If FALSE (default), existing files are skipped.

verbose

Logical. If TRUE (default), prints progress messages during processing. Includes per-variable scaling progress.

Value

Invisibly returns a list containing:

  • n_scaled: Integer. Total number of variables successfully scaled (static plus dynamic).

  • output_dir: Character. Path to the directory containing scaled rasters.

Details

Applies scaling to rasters via z-score transformation: (value - mean) / sd as is calculated during temporally_explicit_extraction. Scaled rasters are written to output_dir.

Scaled rasters should use the same scaling parameters derived from species occurrence data to ensure consistent standardization between training data and prediction layers.

See also

Examples

aln_dir     <- system.file("extdata/rasters_aligned",
                           package = "TemporalModelR")

params_file <- system.file(
  "extdata/points/extracted_seasonal_Scaling_Parameters.csv",
  package = "TemporalModelR"
)

out_dir <- file.path(tempdir(), "scaled")

scale_rasters(
  input_dir           = aln_dir,
  output_dir          = out_dir,
  scaling_params_file = params_file,
  variable_patterns   = c(
    "elevation"    = "elevation",
    "forest_cover" = "forest_cover_YEAR",
    "prseas"       = "prseas_YEAR_SEASON"
  ),
  time_cols           = c("year", "season"),
  overwrite           = TRUE,
  verbose             = FALSE
)