Commit 800de6e4 authored by Nicolau Manubens's avatar Nicolau Manubens
Browse files

Redistributed functions.

parent d908e2f9
......@@ -318,3 +318,5 @@ ArrayToNetCDF <- function(arrays, file_path) {
nc_close(ncdf_object)
invisible(NULL)
}
a2nc <- ArrayToNetCDF
NcClose <- function(file_object) {
result <- NULL
try({
result <- nc_close(file_object)
})
invisible(result)
}
NcOpen <- function(file_path) {
result <- NULL
try({
result <- nc_open(file_path)
}, silent = TRUE)
result
}
NetCDFReadDims <- function(file_to_read, var_names = NULL) {
file_opener <- nc_open
file_closer <- nc_close
close <- FALSE
if (is.character(file_to_read)) {
file_object <- file_opener(file_to_read)
file_path <- file_to_read
close <- TRUE
} else if (grepl('^ncdf', class(file_to_read))) {
file_object <- file_to_read
file_path <- file_object$filename
} else {
stop("Either the path to a NetCDF file or a ncdf object must be provided as 'file_to_read'.")
}
# Check var_names
if (!is.null(var_names)) {
if (!is.character(var_names)) {
stop("Parameter 'var_names' must be a vector of character strings or NULL.")
}
}
dims <- NULL
if (!is.null(file_object)) {
extra_dimvars <- NULL
# Create all variables that are 'dimvars'
for (dim_name in names(file_object$dim)) {
if (file_object$dim[[dim_name]]$create_dimvar) {
new_var <- list(name = dim_name, ndims = 1,
size = file_object$dim[[dim_name]]$len,
units = file_object$dim[[dim_name]]$units,
dim = list(file_object$dim[[dim_name]]))
file_object$var[[dim_name]] <- new_var
file_object$nvars <- file_object$ncars + 1
extra_dimvars <- c(extra_dimvars, dim_name)
}
}
if (is.null(var_names)) {
var_names <- names(file_object$var)
}
for (var_name in var_names) {
if (!(var_name %in% names(file_object$var))) {
stop("Could not find the variable '", var_name, "' in the file.")
}
found_dims <- file_object$var[[var_name]]$size
names(found_dims) <- sapply(file_object$var[[var_name]]$dim, '[[', 'name')
new_dim <- c(var = 1)
found_dims <- c(new_dim, found_dims)
if (!is.null(dims)) {
dims <- .MergeArrayDims(dims, found_dims)
dims <- pmax(dims[[1]], dims[[2]])
} else {
dims <- found_dims
}
}
}
if (close) {
file_closer(file_object)
}
dims
}
library(ncdf4)
library(abind)
source('~/easyNCDF/R/Utils.R')
NcOpen <- function(file_path) {
result <- NULL
try({
result <- nc_open(file_path)
}, silent = TRUE)
result
}
NcClose <- function(file_object) {
result <- NULL
try({
result <- nc_close(file_object)
})
invisible(result)
}
NetCDFReadDims <- function(file_to_read, var_names = NULL) {
file_opener <- nc_open
file_closer <- nc_close
close <- FALSE
if (is.character(file_to_read)) {
file_object <- file_opener(file_to_read)
file_path <- file_to_read
close <- TRUE
} else if (grepl('^ncdf', class(file_to_read))) {
file_object <- file_to_read
file_path <- file_object$filename
} else {
stop("Either the path to a NetCDF file or a ncdf object must be provided as 'file_to_read'.")
}
# Check var_names
if (!is.null(var_names)) {
if (!is.character(var_names)) {
stop("Parameter 'var_names' must be a vector of character strings or NULL.")
}
}
dims <- NULL
if (!is.null(file_object)) {
extra_dimvars <- NULL
# Create all variables that are 'dimvars'
for (dim_name in names(file_object$dim)) {
if (file_object$dim[[dim_name]]$create_dimvar) {
new_var <- list(name = dim_name, ndims = 1,
size = file_object$dim[[dim_name]]$len,
units = file_object$dim[[dim_name]]$units,
dim = list(file_object$dim[[dim_name]]))
file_object$var[[dim_name]] <- new_var
file_object$nvars <- file_object$ncars + 1
extra_dimvars <- c(extra_dimvars, dim_name)
}
}
if (is.null(var_names)) {
var_names <- names(file_object$var)
}
for (var_name in var_names) {
if (!(var_name %in% names(file_object$var))) {
stop("Could not find the variable '", var_name, "' in the file.")
}
found_dims <- file_object$var[[var_name]]$size
names(found_dims) <- sapply(file_object$var[[var_name]]$dim, '[[', 'name')
new_dim <- c(var = 1)
found_dims <- c(new_dim, found_dims)
if (!is.null(dims)) {
dims <- .MergeArrayDims(dims, found_dims)
dims <- pmax(dims[[1]], dims[[2]])
} else {
dims <- found_dims
}
}
}
if (close) {
file_closer(file_object)
}
dims
}
NetCDFToArray <- function(file_to_read, vars_to_read, inner_indices,
drop_var_dim = FALSE, unlist = TRUE) {
file_opener <- NcOpen
......
\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}
\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