From fa28ff17c306072149456fa8894164cad9385482 Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 23 Oct 2019 15:43:04 +0200 Subject: [PATCH 01/11] Function to create s2dv_cube objects --- NAMESPACE | 1 + R/as.s2dv_cube.R | 137 ++++++++++++++++++++++++++++++++++++++++++++ man/as.s2dv_cube.Rd | 45 +++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 R/as.s2dv_cube.R create mode 100644 man/as.s2dv_cube.Rd diff --git a/NAMESPACE b/NAMESPACE index ef0bfd71..0cfa7d60 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(PlotForecastPDF) export(PlotMostLikelyQuantileMap) export(RFSlope) export(RainFARM) +export(as.s2dv_cube) import(ggplot2) import(multiApply) import(ncdf4) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R new file mode 100644 index 00000000..e15b712c --- /dev/null +++ b/R/as.s2dv_cube.R @@ -0,0 +1,137 @@ +#'Convert seasonal to decadal data to a 's2dv_cube' class +#' +#'@description This function allows to convert data to 's2dv_cube' class 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 partial 's2dv_cube' objects and for each expected missed parameter a warning message will be displayed. +#' +#'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es} +#' +#'@param data an array with any number of named dimensions, tipically and 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 longitudes 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, 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 vertor with the member names (i.e.: "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 the Load() call to obtain the data was issued. +#'@param source_files a vector of character strings with complete paths to all the found files involved in the Load() call. +#' +#'@return a object of class 's2dv_cube'. +#' +#'@seealso \code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} +#'@examples +#'exp <- 1:100 +#'dim(exp) <- c(lat = 2, time = 10, lon = 5) +#'exp <- as.s2dv_cube(data = exp) +#'class(exp) +#'@export +as.s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, + Dates = 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 a ", + "unknown location.") + } + } + if (is.null(lat)) { + 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 a ", + "unknown location.") + } + } + if (is.null(Variable)) { + warning("Parameter 'Variable' is not provided so the metadata", + "of 's2dv_cube' object will be incomplete.") + } + if (is.null(Datasets)) { + warning("Parameter 'Datasets' is not provided so the metadata", + "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 a ", + "unknown time period.") + } + } + if (is.null(when)) { + 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", + "of 's2dv_cube' object will be incomplete.") + } + if (!is.null(Variable)) { + if (!is.list(Variable)) { + Variable <- list(Variable) + } + if (names(Variable)[1] != 'VarName' | names(Variable)[2] != 'level') { + warning("The name of the first elment of parameter 'Variable' is ", + "expected to be 'varName' and the second 'level'.") + } + if (!is.character(Variable[[1]])) { + warning("The element 'Varname' of parameter 'Variable' must be ", + "a character.") + } + } + # 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 (!is.null(lat)) { + if (any(names(dims) %in% c('lat', 'latitude'))) { + if (dims[(names(dims) %in% c('lat', 'latitude'))] != length(lon) & + dims[(names(dims) %in% c('lat', 'longitude'))] != 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("Paramter '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) { + stop("The length of the element in parameter 'Dates' must ", + "be equal.") + } + 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'.") + } + } + } + object <- list(data = data, lon = lon, lat = lat, Variable = Variable, + Datasets = Datasets, Dates = Dates, when = when, + source_files = source_files) + class(object) <- 's2dv_cube' + return(object) +} diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd new file mode 100644 index 00000000..021d2396 --- /dev/null +++ b/man/as.s2dv_cube.Rd @@ -0,0 +1,45 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as.s2dv_cube.R +\name{as.s2dv_cube} +\alias{as.s2dv_cube} +\title{Convert seasonal to decadal data to a 's2dv_cube' class} +\usage{ +as.s2dv_cube(data, lon = NULL, lat = NULL, Variable = NULL, + Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) +} +\arguments{ +\item{data}{an array with any number of named dimensions, tipically and object output from CST_Load,with the following dimensions: dataset, member, sdate, ftime, lat and lon.} + +\item{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.} + +\item{lat}{an array with one dimension containing the longitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection.} + +\item{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, if it is not required it could be set as NULL.} + +\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 vertor with the member names (i.e.: "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{when}{a time stamp of the date the Load() call to obtain the data was issued.} + +\item{source_files}{a vector of character strings with complete paths to all the found files involved in the Load() call.} +} +\value{ +a object of class 's2dv_cube'. +} +\description{ +This function allows to convert data to 's2dv_cube' class 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 partial 's2dv_cube' objects and for each expected missed parameter a warning message will be displayed. +} +\examples{ +exp <- 1:100 +dim(exp) <- c(lat = 2, time = 10, lon = 5) +exp <- as.s2dv_cube(data = exp) +class(exp) +} +\author{ +Perez-Zanon Nuria, \email{nuria.perez@bsc.es} +} +\seealso{ +\code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} +} + -- GitLab From fc48e1e8b2ad39a3384c242d9dafcd0276cbc68a Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 23 Oct 2019 19:26:03 +0200 Subject: [PATCH 02/11] Examples added --- R/as.s2dv_cube.R | 53 +++++++++++++++++++++++++++++++++++++++------ man/as.s2dv_cube.Rd | 47 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 11 deletions(-) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index e15b712c..2940c3cd 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -17,10 +17,49 @@ #' #'@seealso \code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} #'@examples -#'exp <- 1:100 -#'dim(exp) <- c(lat = 2, time = 10, lon = 5) -#'exp <- as.s2dv_cube(data = exp) -#'class(exp) +#'exp_original <- 1:100 +#'dim(exp_original) <- c(lat = 2, time = 10, lon = 5) +#'exp1 <- as.s2dv_cube(data = exp_original) +#'class(exp1) +#'exp2 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) +#'class(exp2) +#'exp3 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m')) +#'class(exp3) +#'exp4 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +#'class(exp4) +#'exp5 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET") +#'class(exp5) +#'exp6 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET", +#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) +#'class(exp6) +#'exp7 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET", +#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), +#' Datasets = list( +#' exp1 = list(InitializationsDates = list(Member_1 = "01011990", +#' Members = "Member_1")))) +#'class(exp7) +#'dim(exp_original) <- c(dataset = 1, member = 1, sdate = 2, ftime = 5, lat = 2, lon = 5) +#'exp8 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +#'class(exp8) #'@export as.s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) { @@ -76,7 +115,7 @@ as.s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets if (!is.list(Variable)) { Variable <- list(Variable) } - if (names(Variable)[1] != 'VarName' | names(Variable)[2] != 'level') { + if (names(Variable)[1] != 'varName' | names(Variable)[2] != 'level') { warning("The name of the first elment of parameter 'Variable' is ", "expected to be 'varName' and the second 'level'.") } @@ -97,8 +136,8 @@ as.s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets } if (!is.null(lat)) { if (any(names(dims) %in% c('lat', 'latitude'))) { - if (dims[(names(dims) %in% c('lat', 'latitude'))] != length(lon) & - dims[(names(dims) %in% c('lat', 'longitude'))] != 1) { + 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'.") } diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd index 021d2396..3690153f 100644 --- a/man/as.s2dv_cube.Rd +++ b/man/as.s2dv_cube.Rd @@ -31,10 +31,49 @@ a object of class 's2dv_cube'. This function allows to convert data to 's2dv_cube' class 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 partial 's2dv_cube' objects and for each expected missed parameter a warning message will be displayed. } \examples{ -exp <- 1:100 -dim(exp) <- c(lat = 2, time = 10, lon = 5) -exp <- as.s2dv_cube(data = exp) -class(exp) +exp_original <- 1:100 +dim(exp_original) <- c(lat = 2, time = 10, lon = 5) +exp1 <- as.s2dv_cube(data = exp_original) +class(exp1) +exp2 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) +class(exp2) +exp3 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m')) +class(exp3) +exp4 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +class(exp4) +exp5 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET") +class(exp5) +exp6 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET", + source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) +class(exp6) +exp7 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET", + source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), + Datasets = list( + exp1 = list(InitializationsDates = list(Member_1 = "01011990", + Members = "Member_1")))) +class(exp7) +dim(exp_original) <- c(dataset = 1, member = 1, sdate = 2, ftime = 5, lat = 2, lon = 5) +exp8 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +class(exp8) } \author{ Perez-Zanon Nuria, \email{nuria.perez@bsc.es} -- GitLab From 8936559cf1e8db37595f05d5d7c0c68c5ef6fb01 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 24 Oct 2019 12:47:41 +0200 Subject: [PATCH 03/11] renameing the function --- NAMESPACE | 2 +- R/{as.s2dv_cube.R => s2dv_cube.R} | 70 +++++++++++++------------- man/as.s2dv_cube.Rd | 84 ------------------------------- man/s2dv_cube.Rd | 84 +++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 120 deletions(-) rename R/{as.s2dv_cube.R => s2dv_cube.R} (67%) delete mode 100644 man/as.s2dv_cube.Rd create mode 100644 man/s2dv_cube.Rd diff --git a/NAMESPACE b/NAMESPACE index 0cfa7d60..41917b0e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,7 +14,7 @@ export(PlotForecastPDF) export(PlotMostLikelyQuantileMap) export(RFSlope) export(RainFARM) -export(as.s2dv_cube) +export(s2dv_cube) import(ggplot2) import(multiApply) import(ncdf4) diff --git a/R/as.s2dv_cube.R b/R/s2dv_cube.R similarity index 67% rename from R/as.s2dv_cube.R rename to R/s2dv_cube.R index 2940c3cd..f11558ba 100644 --- a/R/as.s2dv_cube.R +++ b/R/s2dv_cube.R @@ -1,6 +1,6 @@ -#'Convert seasonal to decadal data to a 's2dv_cube' class +#'Creation of a 's2dv_cube' object #' -#'@description This function allows to convert data to 's2dv_cube' class 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 partial 's2dv_cube' objects and for each expected missed parameter a warning message will be displayed. +#'@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} #' @@ -19,49 +19,49 @@ #'@examples #'exp_original <- 1:100 #'dim(exp_original) <- c(lat = 2, time = 10, lon = 5) -#'exp1 <- as.s2dv_cube(data = exp_original) +#'exp1 <- s2dv_cube(data = exp_original) #'class(exp1) -#'exp2 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) +#'exp2 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) #'class(exp2) -#'exp3 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m')) +#'exp3 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m')) #'class(exp3) -#'exp4 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m'), -#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), -#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +#'exp4 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) #'class(exp4) -#'exp5 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m'), -#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), -#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), -#' when = "2019-10-23 19:15:29 CET") +#'exp5 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET") #'class(exp5) -#'exp6 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m'), -#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), -#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), -#' when = "2019-10-23 19:15:29 CET", -#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) +#'exp6 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET", +#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) #'class(exp6) -#'exp7 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m'), -#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), -#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), -#' when = "2019-10-23 19:15:29 CET", -#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), -#' Datasets = list( -#' exp1 = list(InitializationsDates = list(Member_1 = "01011990", -#' Members = "Member_1")))) +#'exp7 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), +#' when = "2019-10-23 19:15:29 CET", +#' source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), +#' Datasets = list( +#' exp1 = list(InitializationsDates = list(Member_1 = "01011990", +#' Members = "Member_1")))) #'class(exp7) #'dim(exp_original) <- c(dataset = 1, member = 1, sdate = 2, ftime = 5, lat = 2, lon = 5) -#'exp8 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), -#' Variable = list(varName = 'tas', level = '2m'), -#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), -#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +#'exp8 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), +#' Variable = list(varName = 'tas', level = '2m'), +#' Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), +#' end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) #'class(exp8) #'@export -as.s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, +s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) { if (is.null(data) | !is.array(data) | is.null(names(dim(data)))) { diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd deleted file mode 100644 index 3690153f..00000000 --- a/man/as.s2dv_cube.Rd +++ /dev/null @@ -1,84 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/as.s2dv_cube.R -\name{as.s2dv_cube} -\alias{as.s2dv_cube} -\title{Convert seasonal to decadal data to a 's2dv_cube' class} -\usage{ -as.s2dv_cube(data, lon = NULL, lat = NULL, Variable = NULL, - Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) -} -\arguments{ -\item{data}{an array with any number of named dimensions, tipically and object output from CST_Load,with the following dimensions: dataset, member, sdate, ftime, lat and lon.} - -\item{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.} - -\item{lat}{an array with one dimension containing the longitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection.} - -\item{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, if it is not required it could be set as NULL.} - -\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 vertor with the member names (i.e.: "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{when}{a time stamp of the date the Load() call to obtain the data was issued.} - -\item{source_files}{a vector of character strings with complete paths to all the found files involved in the Load() call.} -} -\value{ -a object of class 's2dv_cube'. -} -\description{ -This function allows to convert data to 's2dv_cube' class 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 partial 's2dv_cube' objects and for each expected missed parameter a warning message will be displayed. -} -\examples{ -exp_original <- 1:100 -dim(exp_original) <- c(lat = 2, time = 10, lon = 5) -exp1 <- as.s2dv_cube(data = exp_original) -class(exp1) -exp2 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) -class(exp2) -exp3 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m')) -class(exp3) -exp4 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m'), - Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), - end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) -class(exp4) -exp5 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m'), - Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), - end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), - when = "2019-10-23 19:15:29 CET") -class(exp5) -exp6 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m'), - Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), - end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), - when = "2019-10-23 19:15:29 CET", - source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) -class(exp6) -exp7 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m'), - Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), - end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), - when = "2019-10-23 19:15:29 CET", - source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), - Datasets = list( - exp1 = list(InitializationsDates = list(Member_1 = "01011990", - Members = "Member_1")))) -class(exp7) -dim(exp_original) <- c(dataset = 1, member = 1, sdate = 2, ftime = 5, lat = 2, lon = 5) -exp8 <- as.s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), - Variable = list(varName = 'tas', level = '2m'), - Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), - end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) -class(exp8) -} -\author{ -Perez-Zanon Nuria, \email{nuria.perez@bsc.es} -} -\seealso{ -\code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} -} - diff --git a/man/s2dv_cube.Rd b/man/s2dv_cube.Rd new file mode 100644 index 00000000..5cc6a524 --- /dev/null +++ b/man/s2dv_cube.Rd @@ -0,0 +1,84 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/s2dv_cube.R +\name{s2dv_cube} +\alias{s2dv_cube} +\title{Creation of a 's2dv_cube' object} +\usage{ +s2dv_cube(data, lon = NULL, lat = NULL, Variable = NULL, + Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) +} +\arguments{ +\item{data}{an array with any number of named dimensions, tipically and object output from CST_Load,with the following dimensions: dataset, member, sdate, ftime, lat and lon.} + +\item{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.} + +\item{lat}{an array with one dimension containing the longitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection.} + +\item{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, if it is not required it could be set as NULL.} + +\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 vertor with the member names (i.e.: "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{when}{a time stamp of the date the Load() call to obtain the data was issued.} + +\item{source_files}{a vector of character strings with complete paths to all the found files involved in the Load() call.} +} +\value{ +a 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. +} +\examples{ +exp_original <- 1:100 +dim(exp_original) <- c(lat = 2, time = 10, lon = 5) +exp1 <- s2dv_cube(data = exp_original) +class(exp1) +exp2 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50)) +class(exp2) +exp3 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m')) +class(exp3) +exp4 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +class(exp4) +exp5 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET") +class(exp5) +exp6 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET", + source_files = c("/path/to/file1.nc", "/path/to/file2.nc")) +class(exp6) +exp7 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999)), + when = "2019-10-23 19:15:29 CET", + source_files = c("/path/to/file1.nc", "/path/to/file2.nc"), + Datasets = list( + exp1 = list(InitializationsDates = list(Member_1 = "01011990", + Members = "Member_1")))) +class(exp7) +dim(exp_original) <- c(dataset = 1, member = 1, sdate = 2, ftime = 5, lat = 2, lon = 5) +exp8 <- s2dv_cube(data = exp_original, lon = seq(-10, 10, 5), lat = c(45, 50), + Variable = list(varName = 'tas', level = '2m'), + Dates = list(start = paste0(rep("01", 10), rep("01", 10), 1990:1999), + end = paste0(rep("31", 10), rep("01", 10), 1990:1999))) +class(exp8) +} +\author{ +Perez-Zanon Nuria, \email{nuria.perez@bsc.es} +} +\seealso{ +\code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} +} + -- GitLab From 80dff63b1f903a678385b37e63b2d4e20ac678d9 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 24 Oct 2019 17:50:13 +0200 Subject: [PATCH 04/11] Adding function as.s2dv_cube --- NAMESPACE | 1 + R/CST_Load.R | 71 +---------------------- R/as.s2dv_cube.R | 133 ++++++++++++++++++++++++++++++++++++++++++++ R/s2dv_cube.R | 2 +- man/as.s2dv_cube.Rd | 50 +++++++++++++++++ man/s2dv_cube.Rd | 2 +- 6 files changed, 187 insertions(+), 72 deletions(-) create mode 100644 R/as.s2dv_cube.R create mode 100644 man/as.s2dv_cube.Rd diff --git a/NAMESPACE b/NAMESPACE index 41917b0e..50c19d9c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(PlotForecastPDF) export(PlotMostLikelyQuantileMap) export(RFSlope) export(RainFARM) +export(as.s2dv_cube) export(s2dv_cube) import(ggplot2) import(multiApply) diff --git a/R/CST_Load.R b/R/CST_Load.R index 66535583..76d5deb3 100644 --- a/R/CST_Load.R +++ b/R/CST_Load.R @@ -37,75 +37,6 @@ #' } CST_Load <- function(...) { exp <- Load(...) - - if (is.null(exp) || (is.null(exp$mod) && is.null(exp$obs))) { - stop("The s2dverification::Load call did not return any data.") - } - - obs <- exp - obs$mod <- NULL - exp$obs <- NULL - names(exp)[[1]] <- 'data' - names(obs)[[1]] <- 'data' - - remove_matches <- function(v, patterns) { - if (length(v) > 0) { - matches <- c() - for (pattern in patterns) { - matches <- c(matches, which(grepl(pattern, v))) - } - if (length(matches) > 0) { - v <- v[-matches] - } - } - v - } - - harmonize_patterns <- function(v) { - matches <- grepl('.*\\.nc$', v) - if (sum(!matches) > 0) { - match_indices <- which(!matches) - v[match_indices] <- sapply(v[match_indices], function(x) paste0(x, '*')) - } - v <- glob2rx(v) - v <- gsub('\\$.*\\$', '*', v) - v - } - - if (!is.null(obs$data)) { - obs$Datasets$exp <- NULL - obs$Datasets <- obs$Datasets$obs - obs_path_patterns <- sapply(obs$Datasets, function(x) attr(x, 'source')) - obs_path_patterns <- harmonize_patterns(obs_path_patterns) - } - - if (!is.null(exp$data)) { - exp$Datasets$obs <- NULL - exp$Datasets <- exp$Datasets$exp - exp_path_patterns <- sapply(exp$Datasets, function(x) attr(x, 'source')) - exp_path_patterns <- harmonize_patterns(exp_path_patterns) - } - - if (!is.null(obs$data) && !is.null(exp$data)) { - obs$source_files <- remove_matches(obs$source_files, - exp_path_patterns) - obs$not_found_files <- remove_matches(obs$not_found_files, - exp_path_patterns) - - exp$source_files <- remove_matches(exp$source_files, - obs_path_patterns) - exp$not_found_files <- remove_matches(exp$not_found_files, - obs_path_patterns) - } - - result <- list() - if (!is.null(exp$data)) { - class(exp) <- 's2dv_cube' - result$exp <- exp - } - if (!is.null(obs$data)) { - class(obs) <- 's2dv_cube' - result$obs <- obs - } + result <- as.s2dv_cube(exp) result } diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R new file mode 100644 index 00000000..52070514 --- /dev/null +++ b/R/as.s2dv_cube.R @@ -0,0 +1,133 @@ +#'Conversion of 'startR_array' or 'list' objects to 's2dv_cube' +#' +#'This function converts data loaded using startR package or s2dverification Load function into a 's2dv_cube' object. +#' +#'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es} +#'@author Nicolau Manubens, \email{nicolau.manubens@bsc.es} +#' +#'@param object a object of class 'startR_array' generated function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package. +#' +#'@return The function returns a 's2dv_cube' object to be easily used with functions \code{CST} from CSTools package. +#' +#'@seealso \code{\link{s2dv_cube}}, \code{\link[s2dverification]{Load}}, \code{\link[startR]{Start}} and \code{\link{CST_Load}} +#'@examples +#'\donttest{ +#'repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' +#'data <- Start(dat = repos, +#' var = 'tas', +#' sdate = c('20170101', '20180101'), +#' ensemble = indices(1:20), +#' time = 'all', +#' latitude = 'all', +#' longitude = indices(1:40), +#' return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'), +#' retrieve = TRUE) +#'data <- as.s2dv_cube(data) +#'class(data) +#'startDates <- c('20001101', '20011101', '20021101', +#' '20031101', '20041101', '20051101') +#'data <- Load(var = 'tas', exp = 'system5c3s', +#' nmember = 15, sdates = startDates, +#' leadtimemax = 3, latmin = 27, latmax = 48, +#' lonmin = -12, lonmax = 40, output = 'lonlat') +#'data <- as.s2dv_cube(data) +#'class(data) +#'} +#'@export +as.s2dv_cube <- function(object) { + if (is.list(object)) { + if (is.null(object) || (is.null(object$mod) && is.null(object$obs))) { + stop("The s2dverification::Load call did not return any data.") + } + obs <- object + obs$mod <- NULL + object$obs <- NULL + names(object)[[1]] <- 'data' + names(obs)[[1]] <- 'data' + remove_matches <- function(v, patterns) { + if (length(v) > 0) { + matches <- c() + for (pattern in patterns) { + matches <- c(matches, which(grepl(pattern, v))) + } + if (length(matches) > 0) { + v <- v[-matches] + } + } + v + } + + harmonize_patterns <- function(v) { + matches <- grepl('.*\\.nc$', v) + if (sum(!matches) > 0) { + match_indices <- which(!matches) + v[match_indices] <- sapply(v[match_indices], function(x) paste0(x, '*')) + } + v <- glob2rx(v) + v <- gsub('\\$.*\\$', '*', v) + v + } + + if (!is.null(obs$data)) { + obs$Datasets$exp <- NULL + obs$Datasets <- obs$Datasets$obs + obs_path_patterns <- sapply(obs$Datasets, function(x) attr(x, 'source')) + obs_path_patterns <- harmonize_patterns(obs_path_patterns) + } + + if (!is.null(exp$data)) { + object$Datasets$obs <- NULL + object$Datasets <- object$Datasets$exp + exp_path_patterns <- sapply(object$Datasets, function(x) attr(x, 'source')) + exp_path_patterns <- harmonize_patterns(exp_path_patterns) + } + + if (!is.null(obs$data) && !is.null(object$data)) { + obs$source_files <- remove_matches(obs$source_files, + exp_path_patterns) + obs$not_found_files <- remove_matches(obs$not_found_files, + exp_path_patterns) + + object$source_files <- remove_matches(object$source_files, + obs_path_patterns) + object$not_found_files <- remove_matches(object$not_found_files, + obs_path_patterns) + } + + result <- list() + if (!is.null(object$data)) { + class(object) <- 's2dv_cube' + result$exp <- object + } + if (!is.null(obs$data)) { + class(obs) <- 's2dv_cube' + result$obs <- obs + } + + } else if (class(object) == 'startR_array') { + result <- list() + result$data <- as.vector(object) + dim(result$data) <- dim(object) + result$lon <- attributes(object)$Variables$dat1$longitude + result$lat <- attributes(object)$Variables$dat1$latitude + vars <- which(names(attributes(object)$Variables$common) != 'time') + if (length(vars) > 1) { + warning("More than one variable has been provided and ", + "only the first one will be used.") + vars <- vars[1] + } + Variable <- list() + Variable$varName <- names(attributes(object)$Variables$common)[vars] + attr(Variable, 'variable') <- attributes(object)$Variables$common[[vars]] + result$Variable <- Variable + result$Dates <- attributes(object)$Variables$common$time + result$when <- Sys.time() + result$source_files <- as.vector(attributes(object)$Files) + result$load_parameters <- attributes(object)$FileSelectors + class(result) <- 's2dv_cube' + } else { + stop("The class of parameter 'object' is not implemented", + " to be converted into 's2dv_cube' class yet.") + } + result +} diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index f11558ba..6e004a36 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -13,7 +13,7 @@ #'@param when a time stamp of the date the Load() call to obtain the data was issued. #'@param source_files a vector of character strings with complete paths to all the found files involved in the Load() call. #' -#'@return a object of class 's2dv_cube'. +#'@return The function returns a object of class 's2dv_cube'. #' #'@seealso \code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} #'@examples diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd new file mode 100644 index 00000000..018ec550 --- /dev/null +++ b/man/as.s2dv_cube.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as.s2dv_cube.R +\name{as.s2dv_cube} +\alias{as.s2dv_cube} +\title{Conversion of 'startR_array' or 'list' objects to 's2dv_cube'} +\usage{ +as.s2dv_cube(object) +} +\arguments{ +\item{object}{a object of class 'startR_array' generated function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package.} +} +\value{ +The function returns a 's2dv_cube' object to be easily used with functions \code{CST} from CSTools package. +} +\description{ +This function converts data loaded using startR package or s2dverification Load function into a 's2dv_cube' object. +} +\examples{ +\donttest{ +repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' +data <- Start(dat = repos, + var = 'tas', + sdate = c('20170101', '20180101'), + ensemble = indices(1:20), + time = 'all', + latitude = 'all', + longitude = indices(1:40), + return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'), + retrieve = TRUE) +data <- as.s2dv_cube(data) +class(data) +startDates <- c('20001101', '20011101', '20021101', + '20031101', '20041101', '20051101') +data <- Load(var = 'tas', exp = 'system5c3s', + nmember = 15, sdates = startDates, + leadtimemax = 3, latmin = 27, latmax = 48, + lonmin = -12, lonmax = 40, output = 'lonlat') +data <- as.s2dv_cube(data) +class(data) +} +} +\author{ +Perez-Zanon Nuria, \email{nuria.perez@bsc.es} + +Nicolau Manubens, \email{nicolau.manubens@bsc.es} +} +\seealso{ +\code{\link{s2dv_cube}}, \code{\link[s2dverification]{Load}}, \code{\link[startR]{Start}} and \code{\link{CST_Load}} +} + diff --git a/man/s2dv_cube.Rd b/man/s2dv_cube.Rd index 5cc6a524..26942574 100644 --- a/man/s2dv_cube.Rd +++ b/man/s2dv_cube.Rd @@ -25,7 +25,7 @@ s2dv_cube(data, lon = NULL, lat = NULL, Variable = NULL, \item{source_files}{a vector of character strings with complete paths to all the found files involved in the Load() call.} } \value{ -a object of class 's2dv_cube'. +The function returns a 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. -- GitLab From 7342e7fd5791e96ce6d693ea2239355e28ea5e0f Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 24 Oct 2019 18:03:39 +0200 Subject: [PATCH 05/11] Dontrun examples for as.s2dv.cube --- R/as.s2dv_cube.R | 3 ++- man/as.s2dv_cube.Rd | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index 52070514..e9981738 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -11,7 +11,8 @@ #' #'@seealso \code{\link{s2dv_cube}}, \code{\link[s2dverification]{Load}}, \code{\link[startR]{Start}} and \code{\link{CST_Load}} #'@examples -#'\donttest{ +#'\dontrun{ +#'library(startR) #'repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' #'data <- Start(dat = repos, #' var = 'tas', diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd index 018ec550..49eca17b 100644 --- a/man/as.s2dv_cube.Rd +++ b/man/as.s2dv_cube.Rd @@ -16,7 +16,8 @@ The function returns a 's2dv_cube' object to be easily used with functions \code This function converts data loaded using startR package or s2dverification Load function into a 's2dv_cube' object. } \examples{ -\donttest{ +\dontrun{ +library(startR) repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' data <- Start(dat = repos, var = 'tas', -- GitLab From 44e54aab0c10c10448ee062d88dacf683b922669 Mon Sep 17 00:00:00 2001 From: dverfail Date: Fri, 25 Oct 2019 09:48:31 +0200 Subject: [PATCH 06/11] Minor text edits for as.s2dv_cube.R --- R/as.s2dv_cube.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index e9981738..8e7c5bd8 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -5,7 +5,7 @@ #'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es} #'@author Nicolau Manubens, \email{nicolau.manubens@bsc.es} #' -#'@param object a object of class 'startR_array' generated function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package. +#'@param object an object of class 'startR_array' generated from function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package. #' #'@return The function returns a 's2dv_cube' object to be easily used with functions \code{CST} from CSTools package. #' -- GitLab From ac83ec48ca0b4f46b1242afedf14193e42ff2faf Mon Sep 17 00:00:00 2001 From: dverfail Date: Fri, 25 Oct 2019 10:10:14 +0200 Subject: [PATCH 07/11] Text edits in s2dv_cube.R --- R/s2dv_cube.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index 6e004a36..6850e2ad 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -4,16 +4,16 @@ #' #'@author Perez-Zanon Nuria, \email{nuria.perez@bsc.es} #' -#'@param data an array with any number of named dimensions, tipically and object output from CST_Load,with the following dimensions: dataset, member, sdate, ftime, lat and lon. +#'@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 longitudes and attributes: dim, cdo_grid_name, first_lat, last_lat 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, 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 vertor with the member names (i.e.: "Member_1") +#'@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 the Load() call to obtain the data was issued. #'@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 a object of class 's2dv_cube'. +#'@return The function returns an object of class 's2dv_cube'. #' #'@seealso \code{\link[s2dverification]{Load}} and \code{\link{CST_Load}} #'@examples @@ -73,7 +73,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = warning("Parameter 'lon' is not provided but data contains a ", "longitudinal dimension.") } else { - warning("Parameter 'lon' is not provided so the data is from a ", + warning("Parameter 'lon' is not provided so the data is from an ", "unknown location.") } } @@ -82,7 +82,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = warning("Parameter 'lat' is not provided but data contains a ", "latitudinal dimension.") } else { - warning("Parameter 'lat' is not provided so the data is from a ", + warning("Parameter 'lat' is not provided so the data is from an ", "unknown location.") } } @@ -91,7 +91,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = "of 's2dv_cube' object will be incomplete.") } if (is.null(Datasets)) { - warning("Parameter 'Datasets' is not provided so the metadata", + warning("Parameter 'Datasets' is not provided so the metadata", "of 's2dv_cube' object will be incomplete.") } if (is.null(Dates)) { @@ -99,7 +99,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = warning("Parameter 'Dates' is not provided but data contains a ", "temporal dimension.") } else { - warning("Parameter 'Dates' is not provided so the data is from a ", + warning("Parameter 'Dates' is not provided so the data is from an ", "unknown time period.") } } @@ -145,7 +145,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = } if (!is.null(Dates)) { if (!is.list(Dates)) { - stop("Paramter 'Dates' must be a list.") + stop("Parameter 'Dates' must be a list.") } else { if (length(Dates) > 2) { warning("Parameter 'Dates' is a list with more than 2 ", @@ -158,7 +158,7 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = } if (length(Dates[[1]]) != length(Dates[[2]]) & length(Dates) == 2) { - stop("The length of the element in parameter 'Dates' must ", + stop("The length of the elements in parameter 'Dates' must ", "be equal.") } time_dims <- dims[names(dims) %in% c('sdate', 'time', 'ftime')] -- GitLab From 82fe8023ed0f99c864a4ade8ba0bc75368174eae Mon Sep 17 00:00:00 2001 From: dverfail Date: Fri, 25 Oct 2019 10:13:29 +0200 Subject: [PATCH 08/11] Update of documentation for s2dv_cube.R --- R/s2dv_cube.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index 6850e2ad..1a403c50 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -7,10 +7,10 @@ #'@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, 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 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 the Load() call to obtain the data was issued. +#'@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'. -- GitLab From 1c13cfc7f0a02f836ea21b779954a00aa560fce6 Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 25 Oct 2019 17:27:21 +0200 Subject: [PATCH 09/11] Adding warning when creating two objects 'exp' and 'obs' --- R/as.s2dv_cube.R | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index 8e7c5bd8..9e404ed1 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -104,7 +104,17 @@ as.s2dv_cube <- function(object) { class(obs) <- 's2dv_cube' result$obs <- obs } - + if (is.list(result)) { + if (is.null(result$exp)) { + result <- result$obs + } else if (is.null(result$obs)) { + result <- result$exp + } else { + warning("The output is a list of two 's2dv_cube' objects", + " corresponding to 'exp' and 'obs'.") + } + } + } else if (class(object) == 'startR_array') { result <- list() result$data <- as.vector(object) -- GitLab From d664bf9ec12b437d48258caf6fd9c8b638ea8481 Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 28 Oct 2019 10:14:44 +0100 Subject: [PATCH 10/11] Adding sdates info to level Dataset to as.s2dv_cube --- R/as.s2dv_cube.R | 17 ++++++++++++++--- man/as.s2dv_cube.Rd | 2 +- man/s2dv_cube.Rd | 12 ++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index 9e404ed1..c8e05365 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -76,7 +76,7 @@ as.s2dv_cube <- function(object) { obs_path_patterns <- harmonize_patterns(obs_path_patterns) } - if (!is.null(exp$data)) { + if (!is.null(object$data)) { object$Datasets$obs <- NULL object$Datasets <- object$Datasets$exp exp_path_patterns <- sapply(object$Datasets, function(x) attr(x, 'source')) @@ -131,7 +131,17 @@ as.s2dv_cube <- function(object) { Variable$varName <- names(attributes(object)$Variables$common)[vars] attr(Variable, 'variable') <- attributes(object)$Variables$common[[vars]] result$Variable <- Variable - result$Dates <- attributes(object)$Variables$common$time + dims <- dim(object) + if (any(c('sdate', 'sdates') %in% names(dims))) { + n_sdates <- dims[which(names(dims) == 'sdate' | names(dims) == 'sdates')] + sdates <- attributes(object)$Variables$common$time[1 : n_sdates] + } else { + sdates <- attributes(object)$Variables$common$time[1] + } + Dataset <- list(list(InitializationDates = list(Member_1 = sdates))) + names(Dataset) <- list(deparse(substitute(object))) + result$Datasets <- Dataset + result$Dates$start <- attributes(object)$Variables$common$time result$when <- Sys.time() result$source_files <- as.vector(attributes(object)$Files) result$load_parameters <- attributes(object)$FileSelectors @@ -139,6 +149,7 @@ as.s2dv_cube <- function(object) { } else { stop("The class of parameter 'object' is not implemented", " to be converted into 's2dv_cube' class yet.") - } + } result + } diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd index 49eca17b..13a2a296 100644 --- a/man/as.s2dv_cube.Rd +++ b/man/as.s2dv_cube.Rd @@ -7,7 +7,7 @@ as.s2dv_cube(object) } \arguments{ -\item{object}{a object of class 'startR_array' generated function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package.} +\item{object}{an object of class 'startR_array' generated from function \code{Start} from startR package (version 0.1.3 from earth.bsc.es/gitlab/es/startR) or a list output from function \code{Load} from s2dverification package.} } \value{ The function returns a 's2dv_cube' object to be easily used with functions \code{CST} from CSTools package. diff --git a/man/s2dv_cube.Rd b/man/s2dv_cube.Rd index 26942574..48af7bbb 100644 --- a/man/s2dv_cube.Rd +++ b/man/s2dv_cube.Rd @@ -8,24 +8,24 @@ s2dv_cube(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = NULL, Dates = NULL, when = NULL, source_files = NULL) } \arguments{ -\item{data}{an array with any number of named dimensions, tipically and object output from CST_Load,with the following dimensions: dataset, member, sdate, ftime, lat and lon.} +\item{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.} \item{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.} -\item{lat}{an array with one dimension containing the longitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection.} +\item{lat}{an array with one dimension containing the latitudes and attributes: dim, cdo_grid_name, first_lat, last_lat and projection.} -\item{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, if it is not required it could be set as NULL.} +\item{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.} -\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 vertor with the member names (i.e.: "Member_1")} +\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{when}{a time stamp of the date the Load() call to obtain the data was issued.} +\item{when}{a time stamp of the date issued by the Load() call to obtain the data.} \item{source_files}{a vector of character strings with complete paths to all the found files involved in the Load() call.} } \value{ -The function returns a object of class '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. -- GitLab From ddfd732a191b1e3230374cdf2a05c87c123544fc Mon Sep 17 00:00:00 2001 From: dverfail Date: Mon, 28 Oct 2019 18:12:09 +0100 Subject: [PATCH 11/11] Update error messages in s2dv_cube.R --- R/s2dv_cube.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index 1a403c50..89720119 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -87,11 +87,11 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = } } if (is.null(Variable)) { - warning("Parameter 'Variable' is not provided so the metadata", + warning("Parameter 'Variable' is not provided so the metadata ", "of 's2dv_cube' object will be incomplete.") } if (is.null(Datasets)) { - warning("Parameter 'Datasets' is not provided so the metadata", + warning("Parameter 'Datasets' is not provided so the metadata ", "of 's2dv_cube' object will be incomplete.") } if (is.null(Dates)) { @@ -104,11 +104,11 @@ s2dv_cube <- function(data, lon = NULL, lat = NULL, Variable = NULL, Datasets = } } 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)) { -- GitLab