Commit 7bce3c0c authored by Nicolau Manubens's avatar Nicolau Manubens
Browse files

Added documentation and tidied up.

parent 594d119f
ArrayToNetCDF <- function(arrays, file_path) {
ArrayToNc <- function(arrays, file_path) {
# Check parameter arrays.
if (is.array(arrays)) {
arrays <- list(arrays)
......@@ -319,4 +319,4 @@ ArrayToNetCDF <- function(arrays, file_path) {
invisible(NULL)
}
a2nc <- ArrayToNetCDF
a2nc <- ArrayToNc
NetCDFReadDims <- function(file_to_read, var_names = NULL) {
NcReadDims <- function(file_to_read, var_names = NULL) {
file_opener <- nc_open
file_closer <- nc_close
close <- FALSE
......
NetCDFToArray <- function(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) {
NcToArray <- function(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) {
file_opener <- NcOpen
file_closer <- NcClose
file_dim_reader <- NetCDFReadDims
......@@ -291,4 +291,4 @@ NetCDFToArray <- function(file_to_read, dim_indices = NULL, vars_to_read = NULL,
result_list
}
nc2a <- NetCDFToArray
nc2a <- NcToArray
\name{ArrayToNetCDF}
\alias{ArrayToNetCDF}
\name{ArrayToNc}
\alias{ArrayToNc}
\alias{a2nc}
\title{Save multidimensional R arrays into NetCDF files}
\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
......@@ -7,7 +7,7 @@
\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
E.g:\cr
\code{
ArrayToNetCDF(arrays = list(temperature = array(1:9, c(3, 3))),
ArrayToNc(arrays = list(temperature = array(1:9, c(3, 3))),
file_path = 'example.nc')
}
}
......@@ -16,7 +16,7 @@ E.g:\cr
\code{
temperature <- array(rnorm(100 * 50 * 10), dim = c(100, 50, 10))
names(dim(temperature)) <- c('longitude', 'latitude', 'time')
ArrayToNetCDF(list(temperature = temperature), file_path = 'example.nc')
ArrayToNc(list(temperature = temperature), file_path = 'example.nc')
}
}
\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
......@@ -31,17 +31,17 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
}
}
}
The special dimension names are 'var'/'variable' and 'time'.\cr
If a dimension is named 'var' or 'variable', \code{ArrayToNetCDF} 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 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{ArrayToNetCDF}.
\code{a2nc} is an alias of \code{ArrayToNc}.
}
\usage{
ArrayToNetCDF(arrays, file_path)
ArrayToNc(arrays, file_path)
a2nc(arrays, file_path)
}
\arguments{
......@@ -52,57 +52,57 @@ a2nc(arrays, file_path)
\examples{
\dontrun{
# Minimal use case
ArrayToNetCDF(array(1:9, c(3, 3)), 'tmp.nc')
ArrayToNc(array(1:9, c(3, 3)), 'tmp.nc')
# Works with arrays of any number of dimensions
ArrayToNetCDF(array(1:27, c(3, 3, 3)), 'tmp.nc')
ArrayToNc(array(1:27, c(3, 3, 3)), 'tmp.nc')
# Arrays can also be provided in [named] lists
ArrayToNetCDF(list(tos = array(1:27, c(3, 3, 3))), 'tmp.nc')
ArrayToNc(list(tos = array(1:27, c(3, 3, 3))), 'tmp.nc')
# Or with dimension names
# 'var' dimension name will generate multiple variables in the
# resulting NetCDF file
a <- array(1:27, dim = c(3, 3, 3))
names(dim(a)) <- c('lon', 'lat', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# 'variable' as dimension name will do the same
a <- array(1:27, dim = c(3, 3, 3))
names(dim(a)) <- c('lon', 'lat', 'variable')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# The 'time' dimension will be built as unlimited dimension, by default
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Putting the 'time' dimension in a position which is not the last, or the one
# right before 'var'/'variable' will crash. Unlimited dimension must be in the
# last position
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('time', 'lat', 'lon', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'time', 'lon', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# The dimension 'var'/'variable' can be in any position and can have any length
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'var', 'lon', 'time')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Multiple arrays can be provided in a list
a <- array(1:400, dim = c(5, 10, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, a), 'tmp.nc')
ArrayToNc(list(a, a), 'tmp.nc')
# If no dimension names are given to an array, new names will be automatically
# generated
a <- array(1:400, dim = c(5, 10, 4, 2))
b <- array(1:400, dim = c(5, 11, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, b), 'tmp.nc')
ArrayToNc(list(a, b), 'tmp.nc')
# If two arrays use a same dimension but their lengths differ, the function
# will crash
......@@ -110,7 +110,7 @@ a <- array(1:400, dim = c(5, 10, 4, 2))
b <- array(1:400, dim = c(5, 11, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
names(dim(b)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, b), 'tmp.nc')
ArrayToNc(list(a, b), 'tmp.nc')
# Metadata can be provided for each variable in each array, via the
# attribute 'variables'. In this example the metadata is empty.
......@@ -121,7 +121,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Variable names can be manually specified
a <- array(1:400, dim = c(5, 10, 4, 2))
......@@ -131,7 +131,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Units can be specified
a <- array(1:400, dim = c(5, 10, 4, 2))
......@@ -141,7 +141,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# addOffset and scaleFactor can be specified
a <- array(1:400, dim = c(5, 10, 4, 2))
......@@ -153,7 +153,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Unlimited dimensions can be manually created
a <- array(1:400, dim = c(5, 10, 4, 2))
......@@ -169,7 +169,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'unlimited', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# A 'time' dimension can be built without it necessarily being unlimited
a <- array(1:400, dim = c(5, 10, 4, 2))
......@@ -185,7 +185,7 @@ metadata <- list(
)
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')
ArrayToNc(a, 'tmp.nc')
# Multiple arrays with data for multiple variables can be saved into a
# NetCDF file at once.
......@@ -203,7 +203,7 @@ dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')
ArrayToNc(list(tos, lon, lat), 'tmp.nc')
}
}
\author{
......
\name{Season}
\alias{Season}
\name{NcClose}
\alias{NcClose}
\title{
Computes Seasonal Means
Close a NEtCDF File
}
\description{
Computes seasonal means on timeseries organized in a array of any number of dimensions up to 10 dimensions where the time dimension is one of those 10 dimensions.
Close a \code{ncdf4} open connection to a file.
}
\usage{
Season(var, posdim = 4, monini, moninf, monsup)
NcClose(file_object)
}
\arguments{
\item{var}{
Array containing the timeseries along one of its dimensions.
}
\item{posdim}{
Dimension along which to compute seasonal means = Time dimension
}
\item{monini}{
First month of the time series: 1 to 12.
}
\item{moninf}{
Month when to start the seasonal means: 1 to 12.
}
\item{monsup}{
Month when to stop the seasonal means: 1 to 12.
\item{file_object}{
NetCDF object as returned by \code{ncdf4::nc_open}.
}
}
\value{
Array with the same dimensions as var except along the posdim dimension whose length corresponds to the number of seasons. Partial seasons are not accounted for.
The result of \code{ncdf4::nc_close}.
}
\examples{
# Load sample data as in Load() example:
example(Load)
leadtimes_dimension <- 4
initial_month <- 11
mean_start_month <- 12
mean_stop_month <- 2
season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
PlotAno(season_means_mod, season_means_obs, startDates,
toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'),
legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps')
}
\author{
History:\cr
0.1 - 2011-03 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr
1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to CRAN
0.0 - 2017-03 (N. Manubens, \email{nicolau.manubens at bsc.es}) - Original code
}
\keyword{datagen}
\name{Season}
\alias{Season}
\name{NcOpen}
\alias{NcOpen}
\title{
Computes Seasonal Means
Open a NetCDF File
}
\description{
Computes seasonal means on timeseries organized in a array of any number of dimensions up to 10 dimensions where the time dimension is one of those 10 dimensions.
Silently opens a NetCDF file with \code{ncdf4::nc_open}. Returns NULL on failure.
}
\usage{
Season(var, posdim = 4, monini, moninf, monsup)
NcOpen(file_path)
}
\arguments{
\item{var}{
Array containing the timeseries along one of its dimensions.
\item{file_path}{
Character string with the path to the file to be opened.
}
\item{posdim}{
Dimension along which to compute seasonal means = Time dimension
}
\item{monini}{
First month of the time series: 1 to 12.
}
\item{moninf}{
Month when to start the seasonal means: 1 to 12.
}
\item{monsup}{
Month when to stop the seasonal means: 1 to 12.
}
}
\value{
Array with the same dimensions as var except along the posdim dimension whose length corresponds to the number of seasons. Partial seasons are not accounted for.
A NetCDF object as returned by \code{ncdf4::nc_open} or NULL on failure.
}
\examples{
# Load sample data as in Load() example:
example(Load)
leadtimes_dimension <- 4
initial_month <- 11
mean_start_month <- 12
mean_stop_month <- 2
season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
PlotAno(season_means_mod, season_means_obs, startDates,
toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'),
legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps')
}
\author{
History:\cr
0.1 - 2011-03 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr
1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to CRAN
0.0 - 2017-03 (N. Manubens, \email{nicolau.manubens at bsc.es}) - Original code
}
\keyword{datagen}
\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{
}
\author{
History:\cr
0.0 - 2017-03 (N. Manubens, \email{nicolau.manubens at bsc.es}) - Original code
}
\keyword{datagen}
\name{NcToArray}
\alias{NcToArray}
\alias{nc2a}
\title{
Read a NetCDF File Into an R Array
}
\description{
Reads one or a set of variables together with metadata items from a NetCDF file into an R array. Indices to retrieve (not necessarily consecutive) can be specified for each of the dimensions. Depending on the format of the request, the variables will be merged in into a single extended array or returned in a list with an array for each variable. The different variables in the file are considered to be stored along a dimension called 'var', so reading a variable 'foo' with dimensions 'lat' and 'lon' would result in an array with the dimensions c('var' = 1, 'lat' = n_lats, 'lon' = n_lons).
}
\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{
\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{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 \code{expect_all_indices = FALSE} (default), it is not mandatory to specify the indices for all (or even any of) the dimensions. In that case all the indices along such dimensions will be read in. If \code{expect_all_indices = TRUE}, then indices for all the dimensions have to be specified for the function to return a data array. In that case, \code{NA} can be used to request all indices for a dimension if desired.
\cr\cr
Since this function considers the variables in a NetCDF file are stored along a 'var' dimension, indices for the (actually non-existing) 'var'/'variable' dimension can be specified. They can be specified in 3 ways:\cr
- A vector of numeric indices: e.g. \code{list(var = c(1, 3, 5))} to take the 1st, 3rd and 5th found variables.\cr
- A vector of character strings with variable names: e.g. \code{list(var = c('foo', 'bar'))}.\cr
- A list of vectors with numeric indices or character strings: e.g. \code{list(var = list(c(1, 3, 'foo'), c(2, 'bar')))}\cr
Vectors with combined numeric indices and character strings are accepted.\cr
Whereas the first two options will return a single extended array with the merged variables, the second option will return a list with an array for each requested variable.
}
\item{drop_var_dim}{
Whether to drop the 'var' dimension this function assumes (read description). If multiple variables are requested in a vector and \code{unlist = TRUE}, the drop won't be performed (not possible).
}
\item{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.
}
\item{expect_all_indices}{
Whether the function should stop if indices are not provided for all the dimensions of any of the requested variables rather than assuming that all the indices are requested for the unspecified dimensions. By default the later is done.
}
\item{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{
Array or list of arrays with the data for one or more than one of the requested variables (depending on the parameters). The dimensions are named. The arrays contain the attribute 'variables' with the metadata items found in the NetCDF file.
}
\examples{
}
\author{
History:\cr
0.0 - 2017-03 (N. Manubens, \email{nicolau.manubens at bsc.es}) - Original code
}
\keyword{datagen}
\name{Season}
\alias{Season}
\title{
Computes Seasonal Means
}
\description{
Computes seasonal means on timeseries organized in a array of any number of dimensions up to 10 dimensions where the time dimension is one of those 10 dimensions.
}
\usage{
Season(var, posdim = 4, monini, moninf, monsup)
}
\arguments{
\item{var}{
Array containing the timeseries along one of its dimensions.
}
\item{posdim}{
Dimension along which to compute seasonal means = Time dimension
}
\item{monini}{
First month of the time series: 1 to 12.
}
\item{moninf}{
Month when to start the seasonal means: 1 to 12.
}
\item{monsup}{
Month when to stop the seasonal means: 1 to 12.
}
}
\value{
Array with the same dimensions as var except along the posdim dimension whose length corresponds to the number of seasons. Partial seasons are not accounted for.
}
\examples{
# Load sample data as in Load() example:
example(Load)
leadtimes_dimension <- 4
initial_month <- 11
mean_start_month <- 12
mean_stop_month <- 2
season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
PlotAno(season_means_mod, season_means_obs, startDates,
toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'),
legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps')
}
\author{
History:\cr
0.1 - 2011-03 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr
1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to CRAN
}
\keyword{datagen}
\name{Season}
\alias{Season}
\title{
Computes Seasonal Means
}
\description{
Computes seasonal means on timeseries organized in a array of any number of dimensions up to 10 dimensions where the time dimension is one of those 10 dimensions.
}
\usage{
Season(var, posdim = 4, monini, moninf, monsup)
}
\arguments{
\item{var}{
Array containing the timeseries along one of its dimensions.
}
\item{posdim}{
Dimension along which to compute seasonal means = Time dimension
}
\item{monini}{
First month of the time series: 1 to 12.
}
\item{moninf}{
Month when to start the seasonal means: 1 to 12.
}
\item{monsup}{
Month when to stop the seasonal means: 1 to 12.
}
}
\value{
Array with the same dimensions as var except along the posdim dimension whose length corresponds to the number of seasons. Partial seasons are not accounted for.
}
\examples{
# Load sample data as in Load() example:
example(Load)
leadtimes_dimension <- 4
initial_month <- 11
mean_start_month <- 12
mean_stop_month <- 2
season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month,
mean_start_month, mean_stop_month)
PlotAno(season_means_mod, season_means_obs, startDates,
toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'),
legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps')
}
\author{
History:\cr
0.1 - 2011-03 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr
1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to CRAN
}
\keyword{datagen}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment