NcReadDims.Rd 1.67 KB
Newer Older
\name{NcReadDims}
\alias{NcReadDims}
\title{
Read Dimensions of a NetCDF File
}
\description{
Reads the dimension names and sizes of a set of variables in a NetCDF file, using the package \code{ncdf4}. The different variables in the file are considered to be stored along a dimension called 'var', so reading the dimensions of a variable 'foo' with dimensions 'lat' and 'lon' would result in a vector with the format c('var' = 1, 'lat' = n_lats, 'lon' = n_lons).
}
\usage{
NcReadDims(file_to_read, var_names = NULL)
}
\arguments{
  \item{file_to_read}{
Path to the file to be read or a NetCDF object as returned by \code{easyNCDF::NcOpen} or \code{ncdf4::nc_open}.
  }
  \item{var_names}{
Vector of character strings with the names of the variables which to read the dimensions for. If multiple variables are requested, their dimensions will be merged and returned in a single vector.
  }
}
\value{
Named numeric vector with the names and sizes of the dimensions for the requested variables.
}
\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, , ])
}
\author{
History:\cr
0.0  -  2017-03  (N. Manubens, \email{nicolau.manubens at bsc.es})  -  Original code
}
\keyword{datagen}