NcToArray {easyNCDF} | R Documentation |
Read Names of Variables in a NetCDF File
Description
Reads the names of the variables in a NetCDF file and returns them as a vector of character strings.
Usage
NcToArray(
file_to_read,
dim_indices = NULL,
vars_to_read = NULL,
drop_var_dim = FALSE,
unlist = TRUE,
expect_all_indices = FALSE,
allow_out_of_range = TRUE
)
Arguments
file_to_read |
Path to the file to be read or a NetCDF object as returned by |
dim_indices |
Named list with numeric vectors of indices to take for each dimension. The names should correspond to the dimension names which to take the indices for. Non-consecutive indices can be specified. If |
vars_to_read |
This parameter is a shortcut to (and has less priority than) specifying the requested variable names via |
drop_var_dim |
Whether to drop the 'var' dimension this function assumes (read description). If multiple variables are requested in a vector and |
unlist |
Whether to merge the resulting array variables into a single array if possible (default) or not. Otherwise a list with as many arrays as requested variables is returned. |
expect_all_indices |
Whether the function should stop if indices are not provided for all the dimensions of any of the requested variables (TRUE) rather than assuming that all the indices are requested for the unspecified dimensions (FALSE). By default the later is done (FALSE). |
allow_out_of_range |
Whether to allow indices out of range (simply disregard them) or to stop if indices out of range are found. |
Value
Vector of character strings with the names of the variables in the NetCDF file.
Author(s)
N. Manubens, nicolau.manubens@bsc.es
Examples
# Create an array from R
file_path <- tempfile(fileext = '.nc')
a <- array(1:9, dim = c(member = 3, time = 3))
# Store into a NetCDF twice, as two different variables
ArrayToNc(list(var_1 = a, var_2 = a + 1), file_path)
# Read the dimensions and variables in the created file
fnc <- NcOpen(file_path)
fnc_dims <- NcReadDims(fnc)
var_names <- NcReadVarNames(fnc)
# Read the two variables from the file into an R array
a_from_file <- NcToArray(fnc, vars_to_read = var_names)
NcClose(fnc)
# Check the obtained array matches the original array
print(a)
print(a_from_file[1, , ])