| Title: | A suite of convenience functions created by IIASA BNR Researchers |
|---|---|
| Description: | What the package does (one paragraph). |
| Authors: | Martin Jung [aut, cre] (ORCID: <https://orcid.org/0000-0002-7569-1390>), Maximilian H.K. Hesselbarth [aut] (ORCID: <https://orcid.org/0000-0003-1125-9918>) |
| Maintainer: | Martin Jung <jung@iiasa> |
| License: | MIT + file LICENSE |
| Version: | 0.1 |
| Built: | 2026-06-12 07:55:25 UTC |
| Source: | https://github.com/iiasa/BNRTools |
Calculates the set of entries not present in the second vector. Added for convenience since this is not supported by default in R.
a %notin% ba %notin% b
a |
First |
b |
Second |
A vector of logical entries of 'a' not present in 'b'.
Martin Jung
# example code# example code
ibis.iSDM
This function expects a downscaled GLOBIOM output as created in
the BIOCLIMA project. It converts the input to a stars object to be fed to
the ibis.iSDM R-package.
conv_downscalr2ibis( fname, ignore = NULL, period = "all", template = NULL, shares_to_area = FALSE, use_gdalutils = FALSE, verbose = TRUE )conv_downscalr2ibis( fname, ignore = NULL, period = "all", template = NULL, shares_to_area = FALSE, use_gdalutils = FALSE, verbose = TRUE )
fname |
A filename in |
ignore |
A |
period |
A |
template |
An optional |
shares_to_area |
A |
use_gdalutils |
(Deprecated) |
verbose |
|
A SpatRaster stack with the formatted GLOBIOM predictors.
Martin Jung
## Not run: ## Does not work unless downscalr file is provided. # Expects a filename pointing to a netCDF file. covariates <- conv_downscalr2ibis(fname) ## End(Not run)## Not run: ## Does not work unless downscalr file is provided. # Expects a filename pointing to a netCDF file. covariates <- conv_downscalr2ibis(fname) ## End(Not run)
Shows the size of the objects currently in the R environment. Helps to locate large objects cluttering the R environment and/or causing memory problems during the execution of large workflows.
misc_objectSize(n = 10)misc_objectSize(n = 10)
n |
Number of objects to show, Default: |
A data frame with the row names indicating the object name, the field 'Type' indicating the object type, 'Size' indicating the object size, and the columns 'Length/Rows' and 'Columns' indicating the object dimensions if applicable.
Bias Benito
if(interactive()){ #creating dummy objects x <- matrix(runif(100), 10, 10) y <- matrix(runif(10000), 100, 100) #reading their in-memory size misc_objectSize() }if(interactive()){ #creating dummy objects x <- matrix(runif(100), 10, 10) y <- matrix(runif(10000), 100, 100) #reading their in-memory size misc_objectSize() }
Prepared covariates often have special characters in their variable names which can or can not be used in formulas or cause errors for certain procedures. This function converts special characters (points, underscores or similar) of variable names into a species format.
misc_sanitizeNames(names)misc_sanitizeNames(names)
names |
A vector of sanitized character.
# Correct variable names vars <- c("Climate-temperature2015", "Elevation__sealevel", "Landuse.forest..meanshare") misc_sanitizeNames(vars)# Correct variable names vars <- c("Climate-temperature2015", "Elevation__sealevel", "Landuse.forest..meanshare") misc_sanitizeNames(vars)
This function serves as a general wrapper function to export a provided spatial gridded layer as multi-dimensional NetCDF file. It furthermore requires the specification of a list containing metadata information.
spl_exportNetCDF( obj, filename, global_meta = system.file("iiasa_meta.yaml", package = "BNRTools"), separate_meta = FALSE, ... )spl_exportNetCDF( obj, filename, global_meta = system.file("iiasa_meta.yaml", package = "BNRTools"), separate_meta = FALSE, ... )
obj |
A |
filename |
A |
global_meta |
A global metadata descriptor by default using IIASA standard
metadata (Default: |
separate_meta |
A |
... |
Any other metadata that should be overwritten or added to |
The default metadata is contained in "inst/iiasa_meta". See examples.
A support for 'stars' could be added.
Martin Jung
## Not run: # Load default metadata (loaded by default by function too) meta <- yaml::read_yaml(system.file("iiasa_meta.yaml", package = "BNRTools")) # Dummy raster obj <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 5, crs = terra::crs("WGS84"), val = runif(400) ) # Export spl_exportNetCDF(obj, filename = "test.nc", global_meta = meta, title = "Super cool analysis") ## End(Not run)## Not run: # Load default metadata (loaded by default by function too) meta <- yaml::read_yaml(system.file("iiasa_meta.yaml", package = "BNRTools")) # Dummy raster obj <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 5, crs = terra::crs("WGS84"), val = runif(400) ) # Export spl_exportNetCDF(obj, filename = "test.nc", global_meta = meta, title = "Super cool analysis") ## End(Not run)
A common issue in aggregating categorical rasters is that coastal and boundary
gridcells tend to decrease owing to aggregation effects. This can cause issues
of non-matching grid cells later on.
This simple function takes a categorical SpatRaster object and grows it
into no-data areas (those with NA values) within an optionally provided
distance.
spl_growGrid(x, iter = 2)spl_growGrid(x, iter = 2)
x |
A categorical |
iter |
A |
Only NA grid cells will be filled!
set.seed(42) ras <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 10, crs = NA) # Fill with dummy values but keep half as NA terra::values(ras) <- c(rep(NA,50),rbinom(terra::ncell(ras)/2, 10, 0.5)) # Convert to factor ras <- terra::as.factor(ras) assertthat::assert_that(terra::is.factor(ras)) ras_nona <- spl_growGrid(x = ras, iter = 10) terra::plot(ras_nona)set.seed(42) ras <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 10, crs = NA) # Fill with dummy values but keep half as NA terra::values(ras) <- c(rep(NA,50),rbinom(terra::ncell(ras)/2, 10, 0.5)) # Convert to factor ras <- terra::as.factor(ras) assertthat::assert_that(terra::is.factor(ras)) ras_nona <- spl_growGrid(x = ras, iter = 10) terra::plot(ras_nona)
Particular for large spatial files, it is often impractical to process the entire layer as single file. Instead such a file could be split in smaller chunks and processed as such.
A common issue then is to reconstruct a spatial file of the original extent.
This function takes a list of filenames as input and mosaics them
together using a 'vrt' file format.
spl_mosaicTiffs(files, ofname = NULL, tempdir = NULL, dt = "INT2S", ...)spl_mosaicTiffs(files, ofname = NULL, tempdir = NULL, dt = "INT2S", ...)
files |
A |
ofname |
A |
tempdir |
A |
dt |
A |
... |
Any other parameters passed to |
This function by default uses the 'gdalUtilities' R-package and tools
for most of the projections.
A SpatRaster or just a file.
Martin Jung
## Not run: # Get list of files ll <- list.files(path_to_folder) # Mosaic spl_mosaicTiffs(ll, "full_file.tif") ## End(Not run)## Not run: # Get list of files ll <- list.files(path_to_folder) # Mosaic spl_mosaicTiffs(ll, "full_file.tif") ## End(Not run)
This function replaces all NA values in a spatial gridded layer with
a fixed value such as for example 0. Accepted input layers are for SpatRaster from
the "terra" R-package and stars from the "stars" R-package.
spl_replaceGriddedNA(obj, value = 0, mask, verbose = FALSE)spl_replaceGriddedNA(obj, value = 0, mask, verbose = FALSE)
obj |
A |
value |
A fixed numeric value of which all |
mask |
An optional |
verbose |
Be chatty about what is processed (Default: |
Required inputs are a single "obj" gridded data object and a numeric value.
In addition an optional mask layer can be provided that to use a mask. In this case
all no-data values a replaced with the value in this mask.
A object of the same type as the input but with no-data values replaced with 'value'.
Martin Jung
# Example s <- terra::rast(system.file("ex/logo.tif", package="terra")) s[sample(1:terra::ncell(s), 100)] <- NA sfill <- spl_replaceGriddedNA(s, value = 100) terra::plot(sfill)# Example s <- terra::rast(system.file("ex/logo.tif", package="terra")) s[sample(1:terra::ncell(s), 100)] <- NA sfill <- spl_replaceGriddedNA(s, value = 100) terra::plot(sfill)
Resample two raster to the spatial resolution using aggregation or disaggregation.
spl_resampleRas(x, y, discrete = FALSE)spl_resampleRas(x, y, discrete = FALSE)
x |
A |
y |
A |
discrete |
|
set.seed(42) ras_a <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 20, crs = NA) ras_b <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 5, crs = NA) terra::values(ras_a) <- runif(n = terra::ncell(ras_a)) terra::values(ras_b) <- runif(n = terra::ncell(ras_b)) spl_resampleRas(x = ras_a, y = ras_b)set.seed(42) ras_a <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 20, crs = NA) ras_b <- terra::rast(ncol = 100, nrow = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, resolution = 5, crs = NA) terra::values(ras_a) <- runif(n = terra::ncell(ras_a)) terra::values(ras_b) <- runif(n = terra::ncell(ras_b)) spl_resampleRas(x = ras_a, y = ras_b)