Package 'BNRTools'

Title: A suite of convenience functions created by IIASA BNR Researchers
Description: What the package does (one paragraph).
Authors: Martin Jung [aut, cre] , Maximilian H.K. Hesselbarth [aut]
Maintainer: Martin Jung <jung@iiasa>
License: MIT + file LICENSE
Version: 0.1
Built: 2024-12-14 15:21:10 UTC
Source: https://github.com/iiasa/BNRTools

Help Index


Inverse of 'in' call

Description

Calculates the set of entries not present in the second vector. Added for convenience since this is not supported by default in R.

Usage

a %notin% b

Arguments

a

First vector object.

b

Second vector object.

Value

A vector of logical entries of 'a' not present in 'b'.

Author(s)

Martin Jung

Examples

# example code

Function to format a prepared GLOBIOM netCDF file for use in ibis.iSDM

Description

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.

Usage

conv_downscalr2ibis(
  fname,
  ignore = NULL,
  period = "all",
  template = NULL,
  shares_to_area = FALSE,
  use_gdalutils = FALSE,
  verbose = TRUE
)

Arguments

fname

A filename in character pointing to a GLOBIOM output in netCDF format.

ignore

A vector of variables to be ignored (Default: NULL).

period

A character limiting the period to be returned from the formatted data. Options include "reference" for the first entry, "projection" for all entries but the first, and "all" for all entries (Default: "reference").

template

An optional SpatRaster object towards which projects should be transformed.

shares_to_area

A logical on whether shares should be corrected to areas (if identified).

use_gdalutils

(Deprecated) logical on to use gdalutils hack-around.

verbose

logical on whether to be chatty.

Value

A SpatRaster stack with the formatted GLOBIOM predictors.

Author(s)

Martin Jung

Examples

## 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 size of objects in the R environment

Description

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.

Usage

misc_objectSize(n = 10)

Arguments

n

Number of objects to show, Default: 10

Value

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.

Author(s)

Bias Benito

Examples

if(interactive()){

 #creating dummy objects
 x <- matrix(runif(100), 10, 10)
 y <- matrix(runif(10000), 100, 100)

 #reading their in-memory size
 misc_objectSize()

}

Sanitize variable names

Description

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.

Usage

misc_sanitizeNames(names)

Arguments

names

A vector of character vectors to be sanitized.

Value

A vector of sanitized character.

Examples

# Correct variable names
vars <- c("Climate-temperature2015", "Elevation__sealevel", "Landuse.forest..meanshare")
misc_sanitizeNames(vars)

RExport a gridded raster to a NetCDF format

Description

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.

Usage

spl_exportNetCDF(
  obj,
  filename,
  global_meta = system.file("iiasa_meta.yaml", package = "BNRTools"),
  separate_meta = FALSE,
  ...
)

Arguments

obj

A SpatRaster object to be exported.

filename

A character with the output filename.

global_meta

A global metadata descriptor by default using IIASA standard metadata (Default: "iiasa_meta data").

separate_meta

A logical flag on whether the metadata should be written separately in "yaml" format (Default: FALSE).

...

Any other metadata that should be overwritten or added to "global_meta".

Details

The default metadata is contained in "inst/iiasa_meta". See examples.

Note

A support for 'stars' could be added.

Author(s)

Martin Jung

See Also

writeCDF, writeRaster

Examples

# 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")

Replace NA values in gridded layers with a fixed value.

Description

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.

Usage

spl_replaceGriddedNA(obj, value = 0, mask, verbose = FALSE)

Arguments

obj

A SpatRaster, SpatRasterDataset or stars object.

value

A fixed numeric value of which all NA values are to be replaced with (Default: 0).

mask

An optional SpatRaster object used instead of the value.

verbose

Be chatty about what is processed (Default: FALSE).

Details

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.

Value

A object of the same type as the input but with no-data values replaced with 'value'.

Author(s)

Martin Jung

Examples

# 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 raster

Description

Resample two raster to the spatial resolution using aggregation or disaggregation.

Usage

spl_resampleRas(x, y, discrete = FALSE)

Arguments

x

A SpatRaster to be resampled.

y

A SpatRaster to which x will be resampled

discrete

logical to specifiy if input raster has continitous or discrete values

Value

SpatRaster

See Also

aggregate, disagg

Examples

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)