ncdf2raster: Imports and converts NetCDF files to...

View source: R/conversion_functions.R

ncdf2rasterR Documentation

Imports and converts NetCDF files to 'RasterLayer','RasterStack', and 'data.frame'

Description

This function imports netCDF files to R 'RasterLayer','RasterStack', and 'data.frame'.

Usage

ncdf2raster(
  pth,
  flip = NULL,
  transpose = FALSE,
  time = NULL,
  origin = "1901-01-01",
  spatial = NULL,
  varName = NULL,
  fun = NULL,
  temporal_fun = NULL,
  crs = "+init=EPSG:4326",
  ...
)

Arguments

pth

a path to a NetCDF file with the extension .nc or .nc4

flip

character "x", "y" or NULL. If not set to NULL, the resulting array is being flipped to the defined direction.

transpose

logical If TRUE, the resulting array is being transposed (defaults to TRUE).

time

If not set to NULL, defines temporal subset (see Details).

origin

temporal origin of the input NetCDF, defaults to 1901-01-01 (Optional; see Details).

spatial

If not set to NULL, defines spatial subset (see Details).

varName

If not set to NULL, defines spatial subset (see Details).

fun

function for spatial summarize.

temporal_fun

character One of the following: c("sum", "mean", "sd", "cv")

crs

proj4 string input to raster::crs() used to construct the output RasterLayer

...

additional arguments to function provided to the fun argument.

attrs

logical if TRUE, variable and global attributes are returned

Details

The function can imports 2 or 3-dimensional single/multi-variable NetCDF files into R. It provides users with temporal and spatial subset and summary options.

The spatial argument accepts a point coordinate data.frame (x, y) or a RasterLayer mask as input. If points are provided, the function returns a data.frame with the value for each point, time, and variable. A RasterLayer input return the masked map by default. The user can use a weight RasterLayer as a spatial mask, so the result is the multiplication of the values by the weights.

The time argument accepts one or two (from, to) points in time as a numeric/integer index or as Date. In the former case, the i to j th time points are being extracted from the file. When Date is provided it is first being converted to days since the defined origin, which is later searched within the values of the file's time dimension. origin should fit units defined in the file's time dimension.

Summarizing the result can be applied spatially with the fun and ... argument, or temporally with the temporal_fun argument. Temporal summary applies pre-defined statistical transformation (e.g., sum, mean, sd, and cv, coefficient of variance) to every grid-cell on the x-y plane across time points, thus it converts 3 dimensional array to 2 dimensional. Spatial summary applies user defined statistical transformation (e.g., sum, mean, sd) to every time point, resulting in a data.frame with the var, time, and summarized value. The user can apply both summaries at the same time.

Value

a data.frame, a RasterLayer, a RasterStack, or a named list

Examples

## Not run: 
PUT EXAMPLE HERE

## End(Not run)

dof1985/cwatm4r documentation built on Nov. 8, 2022, 7:30 p.m.