Compute basic statistics for gridded (NetCDF or raster) data
Source:R/basic_stats_nc.R
basic_stats_nc.RdComputes a comprehensive set of over 30 summary statistics for every grid
cell in a spatial time series. The function accepts an sxts object,
a Raster* object, or a NetCDF file path with variable name. When
filename and varname are supplied, the data are first imported
via nc2xts and additional arguments are forwarded to that
function. For raster inputs the layer names are parsed for dates,
and the raster values are converted to an xts matrix. The
returned raster has one layer per statistic; layer names correspond to the
rows of the basic_stats output.
Usage
basic_stats_nc(
data = NULL,
filename = NA,
varname = NA,
ignore_zeros = FALSE,
zero_threshold = 0.01,
...
)Arguments
- data
An
sxtsobject, or aRaster*object. Leave asNULLwhen supplyingfilenameandvarnameinstead.- filename
A NetCDF file name to import. Ignored when
datais supplied directly.- varname
The name of the variable to extract from
filename.- ignore_zeros
Logical. If
TRUE, values at or belowzero_thresholdare excluded from distributional statistics. Default isFALSE.- zero_threshold
Numeric threshold below which values are treated as zero. Default is
0.01.- ...
Additional arguments passed to
nc2xtswhenfilenameandvarnameare provided.
Value
A RasterBrick with one layer per statistic. The layer names
correspond to the statistic labels (e.g. "Mean", "StDev",
"Skewness", "Pdr", etc.). The spatial reference (projection
and extent) is inherited from the input.
Examples
if (FALSE) { # \dontrun{
# From an sxts object (e.g. output of nc2xts)
s <- nc2xts(filename = "precip.nc", varname = "pr")
r <- basic_stats_nc(data = s, ignore_zeros = TRUE)
raster::plot(r[["Mean"]])
# Direct from NetCDF (shortcut — nc2xts is called internally)
r <- basic_stats_nc(filename = "precip.nc", varname = "pr",
ignore_zeros = TRUE)
} # }