From 00db4f96bc93db84d3c67e77338bd75884d51f4c Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 22 Mar 2023 15:54:40 +0100 Subject: [PATCH] Improve code and documentation --- R/CST_SaveExp.R | 48 ++++++++++++++++++++++++++++++---------------- man/CST_SaveExp.Rd | 27 ++++++++++++++++++-------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/R/CST_SaveExp.R b/R/CST_SaveExp.R index dfcc7799..91b0a78c 100644 --- a/R/CST_SaveExp.R +++ b/R/CST_SaveExp.R @@ -37,14 +37,25 @@ #' for instance, to identify member or realization. It would be added to the #' file name between underscore characters. #' -#'@return If single_file is TRUE only one file is created. If single_file is -#'FALSE multiple files are created. When multiple files are created, each file -#'contains the data subset for each start date, variable and dataset. Files -#'with different variables and Datasets are stored in separated directories. -#'The path will be created with the name of the variable and each start date. -#'NetCDF file for each starting date are saved into the -#' folder tree: \cr -#' destination/Dataset/variable/. +#'@return Multiple or single NetCDF files containing the data array.\cr +#'\item{\code{single_file = TRUE}}{ +#' All data is saved in a single file located in the specified destination +#' path with the following name: +#' ___.nc. Multiple +#' variables are saved separately in the same file. The forecast time units +#' is extracted from the frequency of the time steps (hours, days, months). +#' The first value of forecast time is 1. If no frequency is found, the units +#' will be 'hours since' each start date and the time steps are assumed to be +#' equally spaced. +#'} +#'\item{\code{single_file = FALSE}}{ +#' The data array is subset and stored into multiple files. Each file +#' contains the data subset for each start date, variable and dataset. Files +#' with different variables and Datasets are stored in separated directories +#' within the following directory tree: destination/Dataset/variable/. +#' The name of each file will be: +#' __.nc. +#'} #' #'@seealso \code{\link[startR]{Start}}, \code{\link{as.s2dv_cube}} and #'\code{\link{s2dv_cube}} @@ -634,7 +645,6 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, longname = sdate_dim, create_dimvar = TRUE)) names(new_dim) <- sdate_dim defined_dims <- c(defined_dims, new_dim) - extra_info_dim[[sdate_dim]] <- list(sdates = paste(sdates, collapse = ', ')) first_sdate <- sdates[1] last_sdate <- sdates[length(sdates)] # ftime definition @@ -646,14 +656,15 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, if (all(diff(differ_ftime_subset/24) == 1)) { # daily values dim_time <- list(ncdim_def(name = ftime_dim, units = 'days', - vals = differ_ftime_subset/24, calendar = 'proleptic_gregorian', + vals = round(differ_ftime_subset/24) + 1, + calendar = 'proleptic_gregorian', longname = ftime_dim, unlim = TRUE)) names(dim_time) <- ftime_dim defined_dims <- c(defined_dims, dim_time) } else if (all(diff(differ_ftime_subset/24) %in% c(28, 29, 30, 31))) { # monthly values dim_time <- list(ncdim_def(name = ftime_dim, units = 'months', - vals = round(differ_ftime_subset/730), + vals = round(differ_ftime_subset/730) + 1, calendar = 'proleptic_gregorian', longname = ftime_dim, unlim = TRUE)) names(dim_time) <- ftime_dim @@ -661,7 +672,7 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, } else { # other frequency dim_time <- list(ncdim_def(name = ftime_dim, units = 'hours', - vals = differ_ftime_subset, + vals = differ_ftime_subset + 1, calendar = 'proleptic_gregorian', longname = ftime_dim, unlim = TRUE)) names(dim_time) <- ftime_dim @@ -671,8 +682,11 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, warning("Time steps are not equal for all start dates. Only ", "forecast time values for the first start date will be saved ", "correctly.") - dim_time <- list(ncdim_def(name = ftime_dim, units = paste('hours since', paste(sdates, collapse = ', ')), - vals = differ_ftime_subset, calendar = 'proleptic_gregorian', + dim_time <- list(ncdim_def(name = ftime_dim, + units = paste('hours since', + paste(sdates, collapse = ', ')), + vals = differ_ftime_subset, + calendar = 'proleptic_gregorian', longname = ftime_dim, unlim = TRUE)) names(dim_time) <- ftime_dim defined_dims <- c(defined_dims, dim_time) @@ -748,10 +762,10 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, gsub("-", "", last_sdate)), collapse = '_'), ".nc") } else { - file_name <- paste0(paste(c(varname, + file_name <- paste0(paste(c(varname, extra_string, gsub("-", "", first_sdate), - gsub("-", "", last_sdate), - extra_string), collapse = '_'), ".nc") + gsub("-", "", last_sdate)), + collapse = '_'), ".nc") } full_filename <- file.path(destination, file_name) file_nc <- nc_create(full_filename, defined_vars) diff --git a/man/CST_SaveExp.Rd b/man/CST_SaveExp.Rd index 067bbae9..032f01c4 100644 --- a/man/CST_SaveExp.Rd +++ b/man/CST_SaveExp.Rd @@ -55,14 +55,25 @@ for instance, to identify member or realization. It would be added to the file name between underscore characters.} } \value{ -If single_file is TRUE only one file is created. If single_file is -FALSE multiple files are created. When multiple files are created, each file -contains the data subset for each start date, variable and dataset. Files -with different variables and Datasets are stored in separated directories. -The path will be created with the name of the variable and each start date. -NetCDF file for each starting date are saved into the - folder tree: \cr - destination/Dataset/variable/. +Multiple or single NetCDF files containing the data array.\cr +\item{\code{single_file = TRUE}}{ + All data is saved in a single file located in the specified destination + path with the following name: + ___.nc. Multiple + variables are saved separately in the same file. The forecast time units + is extracted from the frequency of the time steps (hours, days, months). + The first value of forecast time is 1. If no frequency is found, the units + will be 'hours since' each start date and the time steps are assumed to be + equally spaced. +} +\item{\code{single_file = FALSE}}{ + The data array is subset and stored into multiple files. Each file + contains the data subset for each start date, variable and dataset. Files + with different variables and Datasets are stored in separated directories + within the following directory tree: destination/Dataset/variable/. + The name of each file will be: + __.nc. +} } \description{ This function allows to divide and save a object of class -- GitLab