diff --git a/.Rbuildignore b/.Rbuildignore index fa596e707601da63df8c53cf4f087a70a953dbea..b2d8e5fcebca62bff5e5380a881580283874cd54 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,4 +5,4 @@ ./.nc$ .*^(?!data)\.RData$ .*\.gitlab-ci.yml$ -^tests$ +#^tests$ diff --git a/DESCRIPTION b/DESCRIPTION index 52999f2a9e329e9db763e3e2f5e482329bd4dc7b..454397ae87dd6a1590d78364134efc1760534780 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Authors@R: c( person("Francesc", "Roura-Adserias", , "francesc.roura@bsc.es", role = "ctb"), person("Silvia", "Terzago", , "s.terzago@isac.cnr.it", role = "ctb"), person("Danila", "Volpi", , "d.volpi@isac.cnr.it", role = "ctb"), + person("Victoria", "Agudetse", , "victoria.agudetse@bsc.es", role = "ctb"), person("BSC-CNS", role = c("cph"))) Description: Exploits dynamical seasonal forecasts in order to provide information relevant to stakeholders at the seasonal timescale. The package diff --git a/NAMESPACE b/NAMESPACE index 34851f66cb1eef519700cde1b5194b2c3469ae31..26042662c9fdc2331a96685f7122e30b37cdcea0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -120,4 +120,4 @@ importFrom(utils,head) importFrom(utils,read.table) importFrom(utils,tail) importFrom(verification,verify) -useDynLib(CSTools, .registration = TRUE) +useDynLib(CSTools) diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index 897201194fb98b2c7f9c764bd45c472c10918f32..5a208a3de99970345881f277707587f89fb024c1 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -1,17 +1,42 @@ #'Creation of a 's2dv_cube' object #' -#'@description This function allows to create a 's2dv_cube' object by passing information through its parameters. This function will be needed if the data hasn't been loaded using CST_Load or has been transformed with other methods. A 's2dv_cube' object has many different components including metadata. This function will allow to create 's2dv_cube' objects even if not all elements are defined and for each expected missed parameter a warning message will be returned. +#'@description This function allows to create a 's2dv_cube' object by passing +#'information through its parameters. This function will be needed if the data +#'hasn't been loaded using CST_Load or has been transformed with other methods. +#'A 's2dv_cube' object has many different components including metadata. This +#'function will allow to create 's2dv_cube' objects even if not all elements +#'are defined and for each expected missed parameter a warning message will be +#'returned. #' #'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es} #' -#'@param data an array with any number of named dimensions, typically an object output from CST_Load, with the following dimensions: dataset, member, sdate, ftime, lat and lon. -#'@param lon an array with one dimension containing the longitudes and attributes: dim, cdo_grid_name, data_across_gw, array_across_gw, first_lon, last_lon and projection. -#'@param lat an array with one dimension containing the latitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection. -#'@param Variable a list of two elements: \code{varName} a character string indicating the abbreviation of a variable name and \code{level} a character string indicating the level (e.g., "2m"), if it is not required it could be set as NULL. -#'@param Datasets a named list with the dataset model with two elements: \code{InitiatlizationDates}, containing a list of the start dates for each member named with the names of each member, and \code{Members} containing a vector with the member names (e.g., "Member_1") -#'@param Dates a named list of two elements: \code{start}, an array of dimensions (sdate, time) with the POSIX initial date of each forecast time of each starting date, and \code{end}, an array of dimensions (sdate, time) with the POSIX final date of each forecast time of each starting date. -#'@param when a time stamp of the date issued by the Load() call to obtain the data. -#'@param source_files a vector of character strings with complete paths to all the found files involved in the Load() call. +#'@param data an array with any number of named dimensions, typically an object +#' output from CST_Load, with the following dimensions: dataset, member, sdate, +#'ftime, lat and lon. +#'@param lon an array with one dimension containing the longitudes and +#'attributes: dim, cdo_grid_name, data_across_gw, array_across_gw, first_lon, +#'last_lon and projection. +#'@param lat an array with one dimension containing the latitudes and +#'attributes: dim, cdo_grid_name, first_lat, last_lat and projection. +#'@param Variable a list of two elements: \code{varName} a character string +#'indicating the abbreviation of a variable name and \code{level} a character +#'string indicating the level (e.g., "2m"), if it is not required it could be +#' set as NULL. +#'@param Datasets a named list with the dataset model with two elements: +#'\code{InitiatlizationDates}, containing a list of the start dates for each +#'member named with the names of each member, and \code{Members} containing a +#'vector with the member names (e.g., "Member_1") +#'@param Dates a named list of one to two elements: The first element, +#'\code{start}, is an array of dimensions (sdate, time) with the POSIX initial +#'date of each forecast time of each starting date. The second element, +#'\code{end} (optional), is an array of dimensions (sdate, time) with the POSIX +# final date of each forecast time of each starting date. +#'@param time_dims a vector of strings containing the names of the temporal +#'dimensions found in \code{data}. +#'@param when a time stamp of the date issued by the Load() call to obtain the +#'data. +#'@param source_files a vector of character strings with complete paths to all +#'the found files involved in the Load() call. #' #'@return The function returns an object of class 's2dv_cube'. #' @@ -62,29 +87,29 @@ #'class(exp8) #'@export s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, - Dates = NULL, when = NULL, source_files = NULL) { + Dates = NULL, time_dims = NULL, when = NULL, source_files = NULL) { if (is.null(data) | !is.array(data) | is.null(names(dim(data)))) { stop("Parameter 'data' must be an array with named dimensions.") } dims <- dim(data) if (is.null(lon)) { - if (!any(c('lon', 'longitude') %in% names(dims))) { - warning("Parameter 'lon' is not provided but data contains a ", - "longitudinal dimension.") - } else { - warning("Parameter 'lon' is not provided so the data is from an ", - "unknown location.") - } + if (any(c('lon', 'longitude') %in% names(dims))) { + warning("Parameter 'lon' is not provided but data contains a ", + "longitudinal dimension.") + } else { + warning("Parameter 'lon' is not provided so the data is from an ", + "unknown location.") + } } if (is.null(lat)) { - if (!any(c('lat', 'latitude') %in% names(dims))) { - warning("Parameter 'lat' is not provided but data contains a ", + if (any(c('lat', 'latitude') %in% names(dims))) { + warning("Parameter 'lat' is not provided but data contains a ", "latitudinal dimension.") - } else { - warning("Parameter 'lat' is not provided so the data is from an ", + } else { + warning("Parameter 'lat' is not provided so the data is from an ", "unknown location.") - } + } } if (is.null(Variable)) { warning("Parameter 'Variable' is not provided so the metadata ", @@ -95,20 +120,28 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = "of 's2dv_cube' object will be incomplete.") } if (is.null(Dates)) { - if (!any(c('time', 'ftime', 'sdate') %in% names(dims))) { - warning("Parameter 'Dates' is not provided but data contains a ", - "temporal dimension.") - } else { - warning("Parameter 'Dates' is not provided so the data is from an ", - "unknown time period.") - } + if (!is.null(time_dims)) { + if (any(time_dims %in% names(dims))) { + warning("Parameter 'Dates' is not provided but data contains a ", + "temporal dimension.") + } else { + warning("Data does not contain any of the temporal dimensions ", + "in 'time_dims'.") + } + } else if (any(c('time', 'ftime', 'sdate') %in% names(dims))) { + warning("Parameter 'Dates' is not provided but data contains a ", + "temporal dimension.") + } else { + warning("Parameter 'Dates' is not provided so the data is from an ", + "unknown time period.") + } } if (is.null(when)) { - warning("Parameter 'when' is not provided so the metadata ", + warning("Parameter 'when' is not provided so the metadata ", "of 's2dv_cube' object will be incomplete.") } if (is.null(source_files)) { - warning("Parameter 'source_files' is not provided so the metadata ", + warning("Parameter 'source_files' is not provided so the metadata ", "of 's2dv_cube' object will be incomplete.") } if (!is.null(Variable)) { @@ -116,7 +149,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = Variable <- list(Variable) } if (names(Variable)[1] != 'varName' | names(Variable)[2] != 'level') { - warning("The name of the first elment of parameter 'Variable' is ", + warning("The name of the first element of parameter 'Variable' is ", "expected to be 'varName' and the second 'level'.") } if (!is.character(Variable[[1]])) { @@ -126,51 +159,62 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = } # Dimensions comparison if (!is.null(lon)) { - if (any(names(dims) %in% c('lon', 'longitude'))) { - if (dims[(names(dims) %in% c('lon', 'longitude'))] != length(lon) & - dims[(names(dims) %in% c('lon', 'longitude'))] != 1) { - stop("Length of parameter 'lon' doesn't match the length of ", - "longitudinal dimension in parameter 'data'.") - } + if (any(names(dims) %in% c('lon', 'longitude'))) { + if (dims[(names(dims) %in% c('lon', 'longitude'))] != length(lon) & + dims[(names(dims) %in% c('lon', 'longitude'))] != 1) { + stop("Length of parameter 'lon' doesn't match the length of ", + "longitudinal dimension in parameter 'data'.") } + } } if (!is.null(lat)) { - if (any(names(dims) %in% c('lat', 'latitude'))) { - if (dims[(names(dims) %in% c('lat', 'latitude'))] != length(lat) & - dims[(names(dims) %in% c('lat', 'latitude'))] != 1) { - stop("Length of parameter 'lat' doesn't match the length of ", - "latitudinal dimension in parameter 'data'.") - } + if (any(names(dims) %in% c('lat', 'latitude'))) { + if (dims[(names(dims) %in% c('lat', 'latitude'))] != length(lat) & + dims[(names(dims) %in% c('lat', 'latitude'))] != 1) { + stop("Length of parameter 'lat' doesn't match the length of ", + "latitudinal dimension in parameter 'data'.") } + } } if (!is.null(Dates)) { - if (!is.list(Dates)) { - stop("Parameter 'Dates' must be a list.") - } else { - if (length(Dates) > 2) { - warning("Parameter 'Dates' is a list with more than 2 ", - "elements and only the first two will be used.") - Dates <- Dates[1 : 2] - } - if (names(Dates)[1] != 'start' | names(Dates)[2] != 'end') { - warning("The name of the first element of parameter 'Dates' ", - "is expected to be 'start' and the second 'end'.") - } - if (length(Dates[[1]]) != length(Dates[[2]]) & - length(Dates) == 2) { + if (!is.list(Dates)) { + stop("Parameter 'Dates' must be a list.") + } else { + if (length(Dates) > 2) { + warning("Parameter 'Dates' is a list with more than 2 ", + "elements and only the first two will be used.") + Dates <- Dates[1 : 2] + } + if (names(Dates)[1] != 'start') { + warning("The name of the first element of parameter 'Dates' ", + "is expected to be 'start'.") + } + if (length(Dates) == 2) { + if (names(Dates)[2] != 'end') { + warning("The name of the second element of parameter 'Dates' ", + "is expected to be 'end'.") + } + if (length(Dates[[1]]) != length(Dates[[2]])) { stop("The length of the elements in parameter 'Dates' must ", "be equal.") } + } + if (!is.null(time_dims)) { + time_dims <- dims[names(dims) %in% time_dims] + } else { + warning("Parameter 'time_dims' is not provided, assigning 'sdate', ", + "'time' and 'ftime' as default time dimension names.") time_dims <- dims[names(dims) %in% c('sdate', 'time', 'ftime')] - if (prod(time_dims) != length(Dates[[1]])) { - stop("The length of the temporal dimension doesn't match ", - " with the length of elements in parameter 'Dates'.") - } - } + } + if (prod(time_dims) != length(Dates[[1]])) { + stop("The length of the temporal dimension doesn't match ", + "the length of elements in parameter 'Dates'.") + } + } } object <- list(data = data, lon = lon, lat = lat, Variable = Variable, - Datasets = Datasets, Dates = Dates, when = when, - source_files = source_files) + Datasets = Datasets, Dates = Dates, time_dims = time_dims, + when = when, source_files = source_files) class(object) <- 's2dv_cube' return(object) } diff --git a/man/s2dv_cube.Rd b/man/s2dv_cube.Rd index b0ce896613c52a27a1e5fb0039526dbd757b6b4c..8ac06d78f1a2baf6e14fba6ef37c65d2adce9b1a 100644 --- a/man/s2dv_cube.Rd +++ b/man/s2dv_cube.Rd @@ -11,6 +11,7 @@ s2dv_cube( Variable = NULL, Datasets = NULL, Dates = NULL, + time_dims = NULL, when = NULL, source_files = NULL ) @@ -26,7 +27,9 @@ s2dv_cube( \item{Datasets}{a named list with the dataset model with two elements: \code{InitiatlizationDates}, containing a list of the start dates for each member named with the names of each member, and \code{Members} containing a vector with the member names (e.g., "Member_1")} -\item{Dates}{a named list of two elements: \code{start}, an array of dimensions (sdate, time) with the POSIX initial date of each forecast time of each starting date, and \code{end}, an array of dimensions (sdate, time) with the POSIX final date of each forecast time of each starting date.} +\item{Dates}{a named list of one to two elements: The first element, \code{start}, is an array of dimensions (sdate, time) with the POSIX initial date of each forecast time of each starting date. The second element, \code{end} (optional), is an array of dimensions (sdate, time) with the POSIX final date of each forecast time of each starting date.} + +\item{time_dims}{a vector of strings containing the names of the temporal dimensions found in \code{data}.} \item{when}{a time stamp of the date issued by the Load() call to obtain the data.} @@ -36,7 +39,13 @@ s2dv_cube( The function returns an object of class 's2dv_cube'. } \description{ -This function allows to create a 's2dv_cube' object by passing information through its parameters. This function will be needed if the data hasn't been loaded using CST_Load or has been transformed with other methods. A 's2dv_cube' object has many different components including metadata. This function will allow to create 's2dv_cube' objects even if not all elements are defined and for each expected missed parameter a warning message will be returned. +This function allows to create a 's2dv_cube' object by passing +information through its parameters. This function will be needed if the data +hasn't been loaded using CST_Load or has been transformed with other methods. +A 's2dv_cube' object has many different components including metadata. This +function will allow to create 's2dv_cube' objects even if not all elements +are defined and for each expected missed parameter a warning message will be +returned. } \examples{ exp_original <- 1:100 diff --git a/src/CSTools.so b/src/CSTools.so index b5e81187254aa25073268fa773029085c71c0779..b7629601725e5c3340bc5a7a4c3cee09088c4d6d 100755 Binary files a/src/CSTools.so and b/src/CSTools.so differ