rsm_apply: rsm_apply

View source: R/nc_apply.R

rsm_applyR Documentation

rsm_apply

Description

applies a function to RSM netcdf data

Usage

rsm_apply(
  data,
  dates = NULL,
  cellMap = NULL,
  variable = NULL,
  mesh,
  cellIDs = NULL,
  yearBegin = 1,
  yearLength = 12,
  aggregation = "yr",
  includeMean = TRUE,
  func = function(x) {
     mean(x, na.rm = TRUE)
 }
)

Arguments

data

target object. function is applied to each row. If an 'rsm' object is provided, the object is used for 'cellMap' and 'dates' arguments

dates

a POSIXlt vector of dates in the ncdf data

cellMap

if all cellIDs are not used, a cellMap (from ncvar_get(nc_cop, "cellmap") or loadRSM) should be provided here. This links cellIDs to the correct row in the netcdf. The first row of a cellMap must have the CellID.

variable

name of variable extracted from the netCDF by loadRSM()

mesh

the spatial object to join output to. There must be a column named 'CellId' (case sensitive) in the mesh.

cellIDs

which cellIDs to use in analysis? A numeric vector or NULL to use all cellIDs in the mesh. There must be a column named 'CellId' (case sensitive) in the mesh.

yearBegin

first month of year.

yearLength

length of "year" (units = months).

aggregation

Temporal scale used for aggregating data: 'yr', 'mo', 'da'. Note: returning daily data will require a substantial amount of RAM and may crash R.

includeMean

if TRUE, a column is included that averages across all non-ID columns in the dataset

func

function applied to the daily time series of data for each cell.

Value

output is a terra SpatVector polygon object with the values returned by "func" applied to each year and each cell

Examples


## Not run: 
altq <- loadRSM(ncdf_address = "D:/data/models/COP/ALTQ/globalmonitors.nc")

# 6719 features
copMesh <- vect(system.file("extdata/gis/COP_mesh", "mesh.shp", package="RSM"),"mesh")

### a simple function for discontinuous hydroperiod
hp_discont <- function(x, threshold = 0) {
outDat      <- length(which(as.numeric(x[!is.na(x)]) >= threshold))
return(outDat)
}

hp.altq <- rsm_apply(data = altq,
yearBegin = 1,
yearLength = 12,
mesh = copMesh,
func = function(x) {hp_discont(x, threshold = 0)})


## End(Not run)



troyhill/RSM documentation built on Aug. 23, 2022, 2:40 p.m.