#'Save multidimensional R arrays into NetCDF files
#'Save multidimensional R arrays into NetCDF files
#'
#'
#'@author N. Manubens \email{nicolau.manubens@bsc.es}
#'@author N. Manubens \email{nicolau.manubens@bsc.es}
#'@description This function takes as input one or a list of multidimensional R arrays and stores them in a NetCDF file, using the \code{ncdf4} package. The full path and name of the resulting file must be specified. Metadata can be attached to the arrays and propagated into the NetCDF file in 3 possible ways:\cr
#'@description This function takes as input one or a list of multidimensional R
#'arrays and stores them in a NetCDF file, using the \code{ncdf4} package. The
#'full path and name of the resulting file must be specified. Metadata can be
#'attached to the arrays and propagated into the NetCDF file in 3 possible
#'ways:\cr
#' \itemize{
#' \itemize{
#' \item{Via the list names if a list of arrays is provided:}{Each name in the input list, corresponding to one multidimensional array, will be interpreted as the name of the variable it contains.\cr
#' \item{Via the list names if a list of arrays is provided:}{Each name in
#' the input list, corresponding to one multidimensional array, will be
#' interpreted as the name of the variable it contains.\cr
#' \item{Via the dimension names of each provided array:}{The dimension names of each of the provided arrays will be interpreted as names for the dimensions of the NetCDF files. Read further for special dimension names that will trigger special behaviours, such as 'time' and 'var'.\cr
#' \item{Via the dimension names of each provided array:}{The dimension names
#' of each of the provided arrays will be interpreted as names for the
#' dimensions of the NetCDF files. Read further for special dimension
#' names that will trigger special behaviours, such as 'time' and 'var'.
#' \item{Via the attribute 'variables' of each provided array:}{The arrays can be provided with metadata in an attribute named 'variables', which is expected to be a named list of named lists, where the names of the container list are the names of the variables present in the provided array, and where each sub-list contains metadata for each of the variables. The attribute names and values supported in the sub-lists must follow the same format the package \code{ncdf4} uses to represent the NetCDF file headers.\cr
#' \item{Via the attribute 'variables' of each provided array:}{The arrays
#' can be provided with metadata in an attribute named 'variables', which
#' is expected to be a named list of named lists, where the names of the
#' container list are the names of the variables present in the provided
#' array, and where each sub-list contains metadata for each of the
#' variables. The attribute names and values supported in the sub-lists
#' must follow the same format the package \code{ncdf4} uses to represent
#' the NetCDF file headers.\cr
#'E.g:\cr
#'E.g:\cr
#' \code{
#' \code{
#'a <- array(1:400, dim = c(5, 10, 4, 2))
#'a <- array(1:400, dim = c(5, 10, 4, 2))
...
@@ -26,6 +43,10 @@
...
@@ -26,6 +43,10 @@
#' tos = list(addOffset = 100,
#' tos = list(addOffset = 100,
#' scaleFact = 10,
#' scaleFact = 10,
#' dim = list(list(name = 'time',
#' dim = list(list(name = 'time',
#' unlim = FALSE))),
#' tas = list(addOffset = 100,
#' scaleFact = 10,
#' dim = list(list(name = 'time',
#' unlim = FALSE)))
#' unlim = FALSE)))
#' )
#' )
#'attr(a, 'variables') <- metadata
#'attr(a, 'variables') <- metadata
...
@@ -35,17 +56,29 @@
...
@@ -35,17 +56,29 @@
#' }
#' }
#' }
#' }
#'The special dimension names are 'var'/'variable' and 'time'.\cr
#'The special dimension names are 'var'/'variable' and 'time'.\cr
#'If a dimension is named 'var' or 'variable', \code{ArrayToNc} will interpret each array entry along such dimension corresponds to a separate new variable, hence will create a new variable inside the NetCDF file and will use it to store all the data in the provided array for the corresponding entry along the 'var'/'variable' dimension.\cr
#'If a dimension is named 'var' or 'variable', \code{ArrayToNc} will interpret
#'If a dimension is named 'time', by default it will be interpreted and built as an unlimited dimension. The 'time' dimension must be the last dimension of the array (the right-most). If a 'var'/'variable' dimension is present, the 'time' dimension can be also placed on its left (i.e. the one before the last dimension). The default behaviour of creating the 'time' as unlimited dimension can be disabled by setting manually the attribute \code{unlim = FALSE}, as shown in the previous example.\cr\cr
#'each array entry along such dimension corresponds to a separate new variable,
#'hence will create a new variable inside the NetCDF file and will use it to
#'store all the data in the provided array for the corresponding entry along the
#''var'/'variable' dimension.\cr
#'If a dimension is named 'time', by default it will be interpreted and built as
#'an unlimited dimension. The 'time' dimension must be the last dimension of the
#'array (the right-most). If a 'var'/'variable' dimension is present, the 'time'
#'dimension can be also placed on its left (i.e. the one before the last
#'dimension). The default behaviour of creating the 'time' as unlimited
#'dimension can be disabled by setting manually the attribute
#'\code{unlim = FALSE}, as shown in the previous example.\cr\cr
#'\code{a2nc} is an alias of \code{ArrayToNc}.
#'\code{a2nc} is an alias of \code{ArrayToNc}.
#'
#'
#'@param arrays One or a list of multidimensional data arrays. The list can be provided with names, which will be interpreted as variable names. The arrays can be provided with dimension names. The arrays can be provided with metadata in the attribute 'variables' (read section Description for details).
#'@param arrays One or a list of multidimensional data arrays. The list can be
#' provided with names, which will be interpreted as variable names. The arrays
#' can be provided with dimension names. The arrays can be provided with
#' metadata in the attribute 'variables' (read section Description for details).
#'@param file_path Path and name of the NetCDF file to be created.
#'@param file_path Path and name of the NetCDF file to be created.
#'
#'
#'@return This function returns NULL.
#'@return This function returns NULL.
#'
#'
#'@import ncdf4
#'@import ncdf4
#'@importFrom ClimProjDiags Subset
#'@importFrom stats setNames
#'@importFrom stats setNames
#'@examples
#'@examples
#' \dontrun{
#' \dontrun{
...
@@ -74,7 +107,8 @@
...
@@ -74,7 +107,8 @@
#'names(dim(a)) <- c('lat', 'lon', 'time', 'var')
#'names(dim(a)) <- c('lat', 'lon', 'time', 'var')
#'ArrayToNc(a, 'tmp.nc')
#'ArrayToNc(a, 'tmp.nc')
#'
#'
#'# The dimension 'var'/'variable' can be in any position and can have any length
#'# The dimension 'var'/'variable' can be in any position and can have any
\item{arrays}{One or a list of multidimensional data arrays. The list can be provided with names, which will be interpreted as variable names. The arrays can be provided with dimension names. The arrays can be provided with metadata in the attribute 'variables' (read section Description for details).}
\item{arrays}{One or a list of multidimensional data arrays. The list can be
provided with names, which will be interpreted as variable names. The arrays
can be provided with dimension names. The arrays can be provided with
metadata in the attribute 'variables' (read section Description for details).}
\item{file_path}{Path and name of the NetCDF file to be created.}
\item{file_path}{Path and name of the NetCDF file to be created.}
}
}
...
@@ -18,16 +21,26 @@ a2nc(arrays, file_path)
...
@@ -18,16 +21,26 @@ a2nc(arrays, file_path)
This function returns NULL.
This function returns NULL.
}
}
\description{
\description{
This function takes as input one or a list of multidimensional R arrays and stores them in a NetCDF file, using the \code{ncdf4} package. The full path and name of the resulting file must be specified. Metadata can be attached to the arrays and propagated into the NetCDF file in 3 possible ways:\cr
This function takes as input one or a list of multidimensional R
arrays and stores them in a NetCDF file, using the \code{ncdf4} package. The
full path and name of the resulting file must be specified. Metadata can be
attached to the arrays and propagated into the NetCDF file in 3 possible
ways:\cr
\itemize{
\itemize{
\item{Via the list names if a list of arrays is provided:}{Each name in the input list, corresponding to one multidimensional array, will be interpreted as the name of the variable it contains.\cr
\item{Via the list names if a list of arrays is provided:}{Each name in
the input list, corresponding to one multidimensional array, will be
interpreted as the name of the variable it contains.\cr
\item{Via the dimension names of each provided array:}{The dimension names of each of the provided arrays will be interpreted as names for the dimensions of the NetCDF files. Read further for special dimension names that will trigger special behaviours, such as 'time' and 'var'.\cr
\item{Via the dimension names of each provided array:}{The dimension names
of each of the provided arrays will be interpreted as names for the
dimensions of the NetCDF files. Read further for special dimension
names that will trigger special behaviours, such as 'time' and 'var'.
\cr
E.g:\cr
E.g:\cr
\code{
\code{
temperature <- array(rnorm(100 * 50 * 10), dim = c(100, 50, 10))
temperature <- array(rnorm(100 * 50 * 10), dim = c(100, 50, 10))
\item{Via the attribute 'variables' of each provided array:}{The arrays can be provided with metadata in an attribute named 'variables', which is expected to be a named list of named lists, where the names of the container list are the names of the variables present in the provided array, and where each sub-list contains metadata for each of the variables. The attribute names and values supported in the sub-lists must follow the same format the package \code{ncdf4} uses to represent the NetCDF file headers.\cr
\item{Via the attribute 'variables' of each provided array:}{The arrays
can be provided with metadata in an attribute named 'variables', which
is expected to be a named list of named lists, where the names of the
container list are the names of the variables present in the provided
array, and where each sub-list contains metadata for each of the
variables. The attribute names and values supported in the sub-lists
must follow the same format the package \code{ncdf4} uses to represent
the NetCDF file headers.\cr
E.g:\cr
E.g:\cr
\code{
\code{
a <- array(1:400, dim = c(5, 10, 4, 2))
a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
metadata <- list(
tos = list(addOffset = 100,
tos = list(addOffset = 100,
scaleFact = 10,
dim = list(list(name = 'time',
unlim = FALSE))),
tas = list(addOffset = 100,
scaleFact = 10,
scaleFact = 10,
dim = list(list(name = 'time',
dim = list(list(name = 'time',
unlim = FALSE)))
unlim = FALSE)))
...
@@ -52,8 +76,18 @@ ArrayToNc(a, 'tmp.nc')
...
@@ -52,8 +76,18 @@ ArrayToNc(a, 'tmp.nc')
}
}
}
}
The special dimension names are 'var'/'variable' and 'time'.\cr
The special dimension names are 'var'/'variable' and 'time'.\cr
If a dimension is named 'var' or 'variable', \code{ArrayToNc} will interpret each array entry along such dimension corresponds to a separate new variable, hence will create a new variable inside the NetCDF file and will use it to store all the data in the provided array for the corresponding entry along the 'var'/'variable' dimension.\cr
If a dimension is named 'var' or 'variable', \code{ArrayToNc} will interpret
If a dimension is named 'time', by default it will be interpreted and built as an unlimited dimension. The 'time' dimension must be the last dimension of the array (the right-most). If a 'var'/'variable' dimension is present, the 'time' dimension can be also placed on its left (i.e. the one before the last dimension). The default behaviour of creating the 'time' as unlimited dimension can be disabled by setting manually the attribute \code{unlim = FALSE}, as shown in the previous example.\cr\cr
each array entry along such dimension corresponds to a separate new variable,
hence will create a new variable inside the NetCDF file and will use it to
store all the data in the provided array for the corresponding entry along the
'var'/'variable' dimension.\cr
If a dimension is named 'time', by default it will be interpreted and built as
an unlimited dimension. The 'time' dimension must be the last dimension of the
array (the right-most). If a 'var'/'variable' dimension is present, the 'time'
dimension can be also placed on its left (i.e. the one before the last
dimension). The default behaviour of creating the 'time' as unlimited
dimension can be disabled by setting manually the attribute
\code{unlim = FALSE}, as shown in the previous example.\cr\cr
\code{a2nc} is an alias of \code{ArrayToNc}.
\code{a2nc} is an alias of \code{ArrayToNc}.
}
}
\examples{
\examples{
...
@@ -83,7 +117,8 @@ a <- array(1:1600, dim = c(10, 20, 4, 2))
...
@@ -83,7 +117,8 @@ a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNc(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# The dimension 'var'/'variable' can be in any position and can have any length
# The dimension 'var'/'variable' can be in any position and can have any