convert_xyspace: Expand/collapse between separate and combined x and y...

View source: R/functions_netCDF.R

convert_xyspaceR Documentation

Expand/collapse between separate and combined x and y dimensions

Description

Expand/collapse between separate and combined x and y dimensions

Usage

convert_xyspace(
  grid,
  data,
  locations,
  locations_crs = sf::st_crs(locations),
  data_str = c("xyzt", "xyt", "xyz", "xy"),
  direction = c("expand", "collapse")
)

Arguments

grid

An object that describes a gridded xy-space. Regular, rectangular grids are the only currently supported type. This can be

  • a raster::RasterLayer object,

  • a stars::stars object,

  • a file name pointing to such a raster on disk;

  • a list, such as the one produced by get_xyspace.

  • an object with coordinate values for all gridcell centers that can be passed to as_points;

The crs of the grid coordinate values must match the one of the data locations.

data

A numeric array or vector (optional). A vector is converted to a one-column matrix.

locations

An object from which x and y coordinate values can be extracted that describe the xy locations of each data row, e.g., a matrix or data.frame or a spatial points object inheriting from sf or Spatial*.

locations_crs

An object which is a crs or from which one can be derived that describes the crs of locations.

data_str

A character string describing the dimensions of data where "xy" stands for x and y spatial dimensions if the spatial structure is gridded, while "s" stands for site if the spatial structure are discrete points; z stands for a vertical dimension; and t stands for a temporal dimension.

direction

A character string. The direction of the operation.

Value

A copy of data with one dimension added/removed.

Details

The expanding direction expects that the first dimension of data matches the first dimension of locations. In this case, locations and locations_crs will be passed to as_points.

Notes

The use of data_str currently refers to the "expanded" state unlike how the argument is used by other functions. This may change in future versions of this function.

Examples

tmp_nc <- create_example_netCDFs(tempdir(), "xyt", "timeseries")
data_xyt <- read_netCDF(tmp_nc[["xyt"]], "array", xy_names = c("x", "y"))

# Collapse x-y-t into xy-t format
res_collapsed <- convert_xyspace(
  grid = data_xyt[["xyspace"]],
  data = data_xyt[["data"]],
  locations = expand.grid(data_xyt[["xyspace"]][c("x", "y")]),
  data_str = "xyt",
  direction = "collapse"
)

# Expand xy-t into x-y-t format
res_expanded <- convert_xyspace(
  grid = data_xyt[["xyspace"]],
  data = res_collapsed,
  locations = expand.grid(data_xyt[["xyspace"]][c("x", "y")]),
  data_str = "xyt",
  direction = "expand"
)

# Round trip (using all grid locations) recovers data (but not names)
all.equal(data_xyt[["data"]], res_expanded, check.attributes = FALSE)

# Clean up
unlink(unlist(tmp_nc))


DrylandEcology/rSW2st documentation built on Jan. 10, 2024, 6:22 p.m.