...@@ -104,7 +104,7 @@ res <- RFTemp(t, lon, lat, o, lono, lato, xlim = c(4, 8), ylim = c(43, 46), ...@@ -104,7 +104,7 @@ res <- RFTemp(t, lon, lat, o, lono, lato, xlim = c(4, 8), ylim = c(43, 46),
\references{ \references{
Method described in ERA4CS MEDSCOPE milestone M3.2: Method described in ERA4CS MEDSCOPE milestone M3.2:
High-quality climate prediction data available to WP4 here: High-quality climate prediction data available to WP4 here:
\ url{https://www.medscope-project.eu/the-project/deliverables-reports/} \url{https://www.medscope-project.eu/the-project/deliverables-reports/}
and in H2020 ECOPOTENTIAL Deliverable No. 8.1: and in H2020 ECOPOTENTIAL Deliverable No. 8.1:
High resolution (1-10 km) climate, land use and ocean change scenarios here: High resolution (1-10 km) climate, land use and ocean change scenarios here:
\url{https://ec.europa.eu/research/participants/documents/downloadPublic?documentIds=080166e5b6cd2324&appId=PPGMS}. \url{https://ec.europa.eu/research/participants/documents/downloadPublic?documentIds=080166e5b6cd2324&appId=PPGMS}.
......
...@@ -7,19 +7,23 @@ ...@@ -7,19 +7,23 @@
SaveExp( SaveExp(
data, data,
destination = "./", destination = "./",
Dates = NULL,
coords = NULL, coords = NULL,
Dates = NULL,
time_bounds = NULL,
startdates = NULL,
varname = NULL, varname = NULL,
metadata = NULL, metadata = NULL,
Datasets = NULL, Datasets = NULL,
startdates = NULL,
dat_dim = "dataset",
sdate_dim = "sdate", sdate_dim = "sdate",
ftime_dim = "time", ftime_dim = "time",
var_dim = "var",
memb_dim = "member", memb_dim = "member",
dat_dim = "dataset",
var_dim = "var",
drop_dims = NULL,
single_file = FALSE, single_file = FALSE,
extra_string = NULL extra_string = NULL,
global_attrs = NULL,
units_hours_since = FALSE
) )
} }
\arguments{ \arguments{
...@@ -28,15 +32,30 @@ SaveExp( ...@@ -28,15 +32,30 @@ SaveExp(
\item{destination}{A character string indicating the path where to store the \item{destination}{A character string indicating the path where to store the
NetCDF files.} NetCDF files.}
\item{Dates}{A named array of dates with the corresponding sdate and forecast
time dimension.}
\item{coords}{A named list with elements of the coordinates corresponding to \item{coords}{A named list with elements of the coordinates corresponding to
the dimensions of the data parameter. The names and length of each element the dimensions of the data parameter. The names and length of each element
must correspond to the names of the dimensions. If any coordinate is not must correspond to the names of the dimensions. If any coordinate is not
provided, it is set as an index vector with the values from 1 to the length provided, it is set as an index vector with the values from 1 to the length
of the corresponding dimension.} of the corresponding dimension.}
\item{Dates}{A named array of dates with the corresponding sdate and forecast
time dimension. If there is no sdate_dim, you can set it to NULL.
It must have ftime_dim dimension.}
\item{time_bounds}{(Optional) A list of two arrays of dates containing
the lower (first array) and the upper (second array) time bounds
corresponding to Dates. Each array must have the same dimensions as Dates.
If 'Dates' parameter is NULL, 'time_bounds' are not used. It is NULL by
default.}
\item{startdates}{A vector of dates that will be used for the filenames
when saving the data in multiple files (single_file = FALSE). It must be a
vector of the same length as the start date dimension of data. It must be a
vector of class \code{Dates}, \code{'POSIXct'} or character with lenghts
between 1 and 10. If it is NULL, the coordinate corresponding the the start
date dimension or the first Date of each time step will be used as the name
of the files. It is NULL by default.}
\item{varname}{A character string indicating the name of the variable to be \item{varname}{A character string indicating the name of the variable to be
saved.} saved.}
...@@ -47,17 +66,6 @@ lists for each variable.} ...@@ -47,17 +66,6 @@ lists for each variable.}
\item{Datasets}{A vector of character string indicating the names of the \item{Datasets}{A vector of character string indicating the names of the
datasets.} datasets.}
\item{startdates}{A vector of dates that will be used for the filenames
when saving the data in multiple files. It must be a vector of the same
length as the start date dimension of data. It must be a vector of class
\code{Dates}, \code{'POSIXct'} or character with lenghts between 1 and 10.
If it is NULL, the first Date of each time step will be used as the name of
the files. It is NULL by default.}
\item{dat_dim}{A character string indicating the name of dataset dimension.
By default, it is set to 'dataset'. It can be NULL if there is no dataset
dimension.}
\item{sdate_dim}{A character string indicating the name of the start date \item{sdate_dim}{A character string indicating the name of the start date
dimension. By default, it is set to 'sdate'. It can be NULL if there is no dimension. By default, it is set to 'sdate'. It can be NULL if there is no
start date dimension.} start date dimension.}
...@@ -66,42 +74,74 @@ start date dimension.} ...@@ -66,42 +74,74 @@ start date dimension.}
dimension. By default, it is set to 'time'. It can be NULL if there is no dimension. By default, it is set to 'time'. It can be NULL if there is no
forecast time dimension.} forecast time dimension.}
\item{memb_dim}{A character string indicating the name of the member
dimension. By default, it is set to 'member'. It can be NULL if there is no
member dimension.}
\item{dat_dim}{A character string indicating the name of dataset dimension.
By default, it is set to 'dataset'. It can be NULL if there is no dataset
dimension.}
\item{var_dim}{A character string indicating the name of variable dimension. \item{var_dim}{A character string indicating the name of variable dimension.
By default, it is set to 'var'. It can be NULL if there is no variable By default, it is set to 'var'. It can be NULL if there is no variable
dimension.} dimension.}
\item{memb_dim}{A character string indicating the name of the member dimension. \item{drop_dims}{(optional) A vector of character strings indicating the
By default, it is set to 'member'. It can be NULL if there is no member dimension names of length 1 that need to be dropped in order that they don't
dimension.} appear in the netCDF file. Only is allowed to drop dimensions that are not
used in the computation. The dimensions used in the computation are the ones
specified in: sdate_dim, ftime_dim, dat_dim, var_dim and memb_dim. It is
NULL by default.}
\item{single_file}{A logical value indicating if all object is saved in a \item{single_file}{A logical value indicating if all object is saved in a
unique file (TRUE) or in separated directories (FALSE). When it is FALSE, single file (TRUE) or in multiple files (FALSE). When it is FALSE,
the array is separated for Datasets, variable and start date. It is FALSE the array is separated for datasets, variable and start date. When there are
by default.} no specified time dimensions, the data will be saved in a single file by
default. The output file name when 'single_file' is TRUE is a character
string containing: '<var>_<first_sdate>_<last_sdate>.nc'; when it is FALSE,
it is '<var>_<sdate>.nc'. It is FALSE by default.}
\item{extra_string}{(Optional) A character string to be included as part of
the file name, for instance, to identify member or realization. When
single_file is TRUE, the 'extra_string' will substitute all the default
file name; when single_file is FALSE, the 'extra_string' will be added
in the file name as: '<var>_<extra_string>_<sdate>.nc'. It is NULL by
default.}
\item{global_attrs}{(Optional) A list with elements containing the global
attributes to be saved in the NetCDF.}
\item{extra_string}{A character string to be include as part of the file name, \item{units_hours_since}{(Optional) A logical value only available for the
for instance, to identify member or realization. It would be added to the case: Dates have forecast time and start date dimension, single_file is
file name between underscore characters.} TRUE and 'time_bounds' is NULL. When it is TRUE, it saves the forecast time
with units of 'hours since'; if it is FALSE, the time units will be a number
of time steps with its corresponding frequency (e.g. n days, n months or n
hours). It is FALSE by default.}
} }
\value{ \value{
Multiple or single NetCDF files containing the data array.\cr Multiple or single NetCDF files containing the data array.\cr
\item{\code{single_file = TRUE}}{ \item{\code{single_file is TRUE}}{
All data is saved in a single file located in the specified destination All data is saved in a single file located in the specified destination
path with the following name: path with the following name (by default):
<variable_name>_<extra_string>_<first_sdate>_<last_sdate>.nc. Multiple '<variable_name>_<first_sdate>_<last_sdate>.nc'. Multiple variables
variables are saved separately in the same file. The forecast time units are saved separately in the same file. The forecast time units
is extracted from the frequency of the time steps (hours, days, months). are calculated from each start date (if sdate_dim is not NULL) or from
The first value of forecast time is 1. If no frequency is found, the units the time step. If 'units_hours_since' is TRUE, the forecast time units
will be 'hours since' each start date and the time steps are assumed to be will be 'hours since <each start date>'. If 'units_hours_since' is FALSE,
equally spaced. the forecast time units are extracted from the frequency of the time steps
(hours, days, months); if no frequency is found, the units will be ’hours
since’. When the time units are 'hours since' the time ateps are assumed to
be equally spaced.
} }
\item{\code{single_file = FALSE}}{ \item{\code{single_file is FALSE}}{
The data array is subset and stored into multiple files. Each file The data array is subset and stored into multiple files. Each file
contains the data subset for each start date, variable and dataset. Files contains the data subset for each start date, variable and dataset. Files
with different variables and Datasets are stored in separated directories with different variables and datasets are stored in separated directories
within the following directory tree: destination/Dataset/variable/. within the following directory tree: 'destination/Dataset/variable/'.
The name of each file will be: The name of each file will be by default: '<variable_name>_<sdate>.nc'.
<variable_name>_<extra_string>_<sdate>.nc. The forecast time units are calculated from each start date (if sdate_dim
is not NULL) or from the time step. The forecast time units will be 'hours
since <each start date>'.
} }
} }
\description{ \description{
...@@ -112,19 +152,17 @@ from StartR package. If the original 's2dv_cube' object has been created from ...@@ -112,19 +152,17 @@ from StartR package. If the original 's2dv_cube' object has been created from
} }
\examples{ \examples{
\dontrun{ \dontrun{
data <- lonlat_temp$exp$data data <- lonlat_temp_st$exp$data
lon <- lonlat_temp$exp$coords$lon lon <- lonlat_temp_st$exp$coords$lon
lat <- lonlat_temp$exp$coords$lat lat <- lonlat_temp_st$exp$coords$lat
coords <- list(lon = lon, lat = lat) coords <- list(lon = lon, lat = lat)
Datasets <- lonlat_temp$exp$attrs$Datasets Datasets <- lonlat_temp_st$exp$attrs$Datasets
varname <- 'tas' varname <- 'tas'
Dates <- lonlat_temp$exp$attrs$Dates Dates <- lonlat_temp_st$exp$attrs$Dates
destination = './' metadata <- lonlat_temp_st$exp$attrs$Variable$metadata
metadata <- lonlat_temp$exp$attrs$Variable$metadata SaveExp(data = data, coords = coords, Datasets = Datasets, varname = varname,
SaveExp(data = data, destination = destination, coords = coords, Dates = Dates, metadata = metadata, single_file = TRUE,
Datasets = Datasets, varname = varname, Dates = Dates, ftime_dim = 'ftime', var_dim = 'var', dat_dim = 'dataset')
metadata = metadata, single_file = TRUE, ftime_dim = 'ftime',
var_dim = NULL)
} }
} }
......
...@@ -9,7 +9,9 @@ SplitDim( ...@@ -9,7 +9,9 @@ SplitDim(
split_dim = "time", split_dim = "time",
indices, indices,
freq = "monthly", freq = "monthly",
new_dim_name = NULL new_dim_name = NULL,
dates = NULL,
return_indices = FALSE
) )
} }
\arguments{ \arguments{
...@@ -28,6 +30,13 @@ the length in which to subset the dimension.} ...@@ -28,6 +30,13 @@ the length in which to subset the dimension.}
\item{new_dim_name}{A character string indicating the name of the new \item{new_dim_name}{A character string indicating the name of the new
dimension.} dimension.}
\item{dates}{An optional parameter containing an array of dates of class
'POSIXct' with the corresponding time dimensions of 'data'. It is NULL
by default.}
\item{return_indices}{A logical value that if it is TRUE, the indices
used in splitting the dimension will be returned. It is FALSE by default.}
} }
\description{ \description{
This function split a dimension in two. The user can select the This function split a dimension in two. The user can select the
......
...@@ -85,7 +85,7 @@ data <- as.s2dv_cube(data) ...@@ -85,7 +85,7 @@ data <- as.s2dv_cube(data)
} }
} }
\seealso{ \seealso{
\code{\link{s2dv_cube}}, \code{\link[s2dv]{Load}}, \code{\link{s2dv_cube}}, \code{\link{CST_Start}},
\code{\link[startR]{Start}} and \code{\link{CST_Load}} \code{\link[startR]{Start}} and \code{\link{CST_Load}}
} }
\author{ \author{
......
...@@ -28,15 +28,15 @@ extracted from forecast data available in the MEDSCOPE internal archive. ...@@ -28,15 +28,15 @@ extracted from forecast data available in the MEDSCOPE internal archive.
lonmin <- 6 lonmin <- 6
lonmax <- 9 lonmax <- 9
lonlat_prec_st <- CST_Start(dat = path, lonlat_prec_st <- CST_Start(dataset = path,
var = 'prlr', var = 'prlr',
member = indices(1:6), member = startR::indices(1:6),
sdate = sdates, sdate = sdates,
ftime = 121:151, ftime = 121:151,
lat = values(list(latmin, latmax)), lat = startR::values(list(latmin, latmax)),
lat_reorder = Sort(decreasing = TRUE), lat_reorder = startR::Sort(decreasing = TRUE),
lon = values(list(lonmin, lonmax)), lon = startR::values(list(lonmin, lonmax)),
lon_reorder = CircularSort(0, 360), lon_reorder = startR::CircularSort(0, 360),
synonims = list(lon = c('lon', 'longitude'), synonims = list(lon = c('lon', 'longitude'),
lat = c('lat', 'latitude'), lat = c('lat', 'latitude'),
ftime = c('time', 'ftime'), ftime = c('time', 'ftime'),
......
...@@ -29,15 +29,15 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses ...@@ -29,15 +29,15 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses
lonmin <- -12 lonmin <- -12
latmax <- 48 latmax <- 48
latmin <- 27 latmin <- 27
lonlat_temp_st$exp <- CST_Start(dat = repos_exp, lonlat_temp_st$exp <- CST_Start(dataset = repos_exp,
var = 'tas', var = 'tas',
member = indices(1:15), member = startR::indices(1:15),
sdate = sdates, sdate = sdates,
ftime = indices(1:3), ftime = startR::indices(1:3),
lat = values(list(latmin, latmax)), lat = startR::values(list(latmin, latmax)),
lat_reorder = Sort(decreasing = TRUE), lat_reorder = startR::Sort(decreasing = TRUE),
lon = values(list(lonmin, lonmax)), lon = startR::values(list(lonmin, lonmax)),
lon_reorder = CircularSort(0, 360), lon_reorder = startR::CircularSort(0, 360),
synonims = list(lon = c('lon', 'longitude'), synonims = list(lon = c('lon', 'longitude'),
lat = c('lat', 'latitude'), lat = c('lat', 'latitude'),
member = c('member', 'ensemble'), member = c('member', 'ensemble'),
...@@ -45,6 +45,7 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses ...@@ -45,6 +45,7 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses
return_vars = list(lat = NULL, return_vars = list(lat = NULL,
lon = NULL, ftime = 'sdate'), lon = NULL, ftime = 'sdate'),
retrieve = TRUE) retrieve = TRUE)
dates <- c(paste0(2000, c(11, 12)), paste0(2001, c('01', 11, 12)), dates <- c(paste0(2000, c(11, 12)), paste0(2001, c('01', 11, 12)),
paste0(2002, c('01', 11, 12)), paste0(2003, c('01', 11, 12)), paste0(2002, c('01', 11, 12)), paste0(2003, c('01', 11, 12)),
paste0(2004, c('01', 11, 12)), paste0(2005, c('01', 11, 12)), 200601) paste0(2004, c('01', 11, 12)), paste0(2005, c('01', 11, 12)), 200601)
...@@ -52,23 +53,26 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses ...@@ -52,23 +53,26 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses
dates <- as.POSIXct(dates, format = '%Y%m%d', 'UTC') dates <- as.POSIXct(dates, format = '%Y%m%d', 'UTC')
dim(dates) <- c(ftime = 3, sdate = 6) dim(dates) <- c(ftime = 3, sdate = 6)
dates <- t(dates)
names(dim(dates)) <- c('sdate', 'ftime')
path.obs <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$date$.nc' path.obs <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$date$.nc'
lonlat_temp_st$obs <- CST_Start(dat = path.obs, lonlat_temp_st$obs <- CST_Start(dataset = path.obs,
var = 'tas', var = 'tas',
date = unique(format(dates, '%Y%m')), date = unique(format(dates, '%Y%m')),
ftime = values(dates), ftime = startR::values(dates),
ftime_across = 'date', ftime_across = 'date',
ftime_var = 'ftime', ftime_var = 'ftime',
merge_across_dims = TRUE, merge_across_dims = TRUE,
split_multiselected_dims = TRUE, split_multiselected_dims = TRUE,
lat = values(list(latmin, latmax)), lat = startR::values(list(latmin, latmax)),
lat_reorder = Sort(decreasing = TRUE), lat_reorder = startR::Sort(decreasing = TRUE),
lon = values(list(lonmin, lonmax)), lon = startR::values(list(lonmin, lonmax)),
lon_reorder = CircularSort(0, 360), lon_reorder = startR::CircularSort(0, 360),
synonims = list(lon = c('lon', 'longitude'), synonims = list(lon = c('lon', 'longitude'),
lat = c('lat', 'latitude'), lat = c('lat', 'latitude'),
ftime = c('ftime', 'time')), ftime = c('ftime', 'time')),
transform = CDORemapper, transform = startR::CDORemapper,
transform_extra_cells = 2, transform_extra_cells = 2,
transform_params = list(grid = 'r360x181', transform_params = list(grid = 'r360x181',
method = 'conservative'), method = 'conservative'),
...@@ -77,6 +81,16 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses ...@@ -77,6 +81,16 @@ next. Note that `CST_Start` internally calls `startR::Start` and then uses
lat = NULL, lat = NULL,
ftime = 'date'), ftime = 'date'),
retrieve = TRUE) retrieve = TRUE)
library(lubridate)
dates_exp <- lonlat_temp_st$exp$attrs$Dates
lonlat_temp_st$exp$attrs$Dates <- floor_date(ymd_hms(dates_exp), unit = "months")
dim(lonlat_temp_st$exp$attrs$Dates) <- dim(dates_exp)
dates_obs <- lonlat_temp_st$obs$attrs$Dates
lonlat_temp_st$obs$attrs$Dates <- floor_date(ymd_hms(dates_obs), unit = "months")
dim(lonlat_temp_st$obs$attrs$Dates) <- dim(dates_obs)
} }
} }
\author{ \author{
......
...@@ -4,10 +4,12 @@ ...@@ -4,10 +4,12 @@
\alias{print.s2dv_cube} \alias{print.s2dv_cube}
\title{Print method for s2dv_cube objects} \title{Print method for s2dv_cube objects}
\usage{ \usage{
\method{print}{s2dv_cube}(x) \method{print}{s2dv_cube}(x, ...)
} }
\arguments{ \arguments{
\item{x}{An 's2dv_cube' object} \item{x}{An 's2dv_cube' object.}
\item{...}{Additional arguments of print function.}
} }
\description{ \description{
This is an S3 method of the generic 'print' for the class 's2dv_cube'. When This is an S3 method of the generic 'print' for the class 's2dv_cube'. When
......
...@@ -18,7 +18,7 @@ s2dv_cube( ...@@ -18,7 +18,7 @@ s2dv_cube(
} }
\arguments{ \arguments{
\item{data}{A multidimensional array with named dimensions, typically with \item{data}{A multidimensional array with named dimensions, typically with
dimensions: dataset, member, sdate, ftime, lat and lon.} dimensions: dataset, member, sdate, time, lat and lon.}
\item{coords}{A list of named vectors with the coordinates corresponding to \item{coords}{A list of named vectors with the coordinates corresponding to
the dimensions of the data parameter. If any coordinate has dimensions, they the dimensions of the data parameter. If any coordinate has dimensions, they
...@@ -84,7 +84,7 @@ elements in the structure:\cr ...@@ -84,7 +84,7 @@ elements in the structure:\cr
\description{ \description{
This function allows to create an 's2dv_cube' object by passing This function allows to create an 's2dv_cube' object by passing
information through its parameters. This function will be needed if the data 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. hasn't been loaded using CST_Start or has been transformed with other methods.
An 's2dv_cube' object has many different components including metadata. This An 's2dv_cube' object has many different components including metadata. This
function will allow to create 's2dv_cube' objects even if not all elements 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 are defined and for each expected missed parameter a warning message will be
...@@ -134,7 +134,7 @@ exp8 <- s2dv_cube(data = exp_original, coords = coords, ...@@ -134,7 +134,7 @@ exp8 <- s2dv_cube(data = exp_original, coords = coords,
class(exp8) class(exp8)
} }
\seealso{ \seealso{
\code{\link[s2dv]{Load}} and \code{\link{CST_Load}} \code{\link[s2dv]{Load}} and \code{\link{CST_Start}}
} }
\author{ \author{
Perez-Zanon Nuria, \email{nuria.perez@bsc.es} Perez-Zanon Nuria, \email{nuria.perez@bsc.es}
......
...@@ -8,7 +8,9 @@ obs1 <- c(rnorm(1:180), exp1 * 1.2) ...@@ -8,7 +8,9 @@ obs1 <- c(rnorm(1:180), exp1 * 1.2)
dim(obs1) <- c(time = 10, lat = 4, lon = 5) dim(obs1) <- c(time = 10, lat = 4, lon = 5)
time_obsL1 <- paste(rep("01", 10), rep("01", 10), 1994 : 2003, sep = "-") time_obsL1 <- paste(rep("01", 10), rep("01", 10), 1994 : 2003, sep = "-")
dim(time_obsL1) <- c(time = 10)
time_expL1 <- "01-01-1994" time_expL1 <- "01-01-1994"
dim(time_expL1) <- c(time = 1)
lon1 <- seq(0, 20, 5) lon1 <- seq(0, 20, 5)
lat1 <- seq(0, 15, 4) lat1 <- seq(0, 15, 4)
coords = list(lat = lat1, lon = lon1) coords = list(lat = lat1, lon = lon1)
...@@ -30,18 +32,15 @@ test_that("1. Input checks: CST_Analogs", { ...@@ -30,18 +32,15 @@ test_that("1. Input checks: CST_Analogs", {
# Check 's2dv_cube' # Check 's2dv_cube'
expect_error( expect_error(
CST_Analogs(expL = 1, obsL = 1), CST_Analogs(expL = 1, obsL = 1),
paste0("Parameter 'expL' and 'obsL' must be of the class 's2dv_cube', ", paste0("Parameter 'expL' and 'obsL' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_Analogs(expL = exp, obsL = obs, expVar = 1), CST_Analogs(expL = exp, obsL = obs, expVar = 1),
paste0("Parameter 'expVar' must be of the class 's2dv_cube', ", paste0("Parameter 'expVar' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_Analogs(expL = exp, obsL = obs, obsVar = 1), CST_Analogs(expL = exp, obsL = obs, obsVar = 1),
paste0("Parameter 'obsVar' must be of the class 's2dv_cube', ", paste0("Parameter 'obsVar' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
# Check 'obsL' object structure # Check 'obsL' object structure
...@@ -144,7 +143,7 @@ test_that("3. Output checks" , { ...@@ -144,7 +143,7 @@ test_that("3. Output checks" , {
) )
expect_equal( expect_equal(
names(res), names(res),
c('data', 'coords', 'attrs') c('data', 'coords', 'attrs', 'dims')
) )
expect_equal( expect_equal(
dim(res$data), dim(res$data),
......
...@@ -49,7 +49,7 @@ test_that("1. Input checks", { ...@@ -49,7 +49,7 @@ test_that("1. Input checks", {
# s2dv_cube # s2dv_cube
expect_error( expect_error(
CST_Anomaly(exp = 1, obs = 1), CST_Anomaly(exp = 1, obs = 1),
"Parameter 'exp' and 'obs' must be of the class 's2dv_cube', as output by CSTools::CST_Load." "Parameter 'exp' and 'obs' must be of the class 's2dv_cube'."
) )
# exp and obs # exp and obs
expect_error( expect_error(
...@@ -58,7 +58,7 @@ test_that("1. Input checks", { ...@@ -58,7 +58,7 @@ test_that("1. Input checks", {
) )
expect_error( expect_error(
CST_Anomaly(exp = exp2, obs = obs), CST_Anomaly(exp = exp2, obs = obs),
"Parameter 'exp' and 'obs' must have same dimension names in element 'data'." "Parameter 'dat_dim' is not found in 'exp' dimensions."
) )
# dim_anom # dim_anom
expect_error( expect_error(
...@@ -87,10 +87,6 @@ test_that("1. Input checks", { ...@@ -87,10 +87,6 @@ test_that("1. Input checks", {
CST_Anomaly(exp = exp, obs = obs, memb_dim = 1), CST_Anomaly(exp = exp, obs = obs, memb_dim = 1),
"Parameter 'memb_dim' must be a character string." "Parameter 'memb_dim' must be a character string."
) )
expect_error(
names(CST_Anomaly(exp4, obs4, dim_anom = 'sdates')),
"Parameter 'memb_dim' is not found in 'exp' or in 'obs' dimension."
)
# filter_span # filter_span
expect_warning( expect_warning(
CST_Anomaly(exp = exp, obs = obs, filter_span = 'a'), CST_Anomaly(exp = exp, obs = obs, filter_span = 'a'),
...@@ -98,8 +94,8 @@ test_that("1. Input checks", { ...@@ -98,8 +94,8 @@ test_that("1. Input checks", {
) )
# dat_dim # dat_dim
expect_error( expect_error(
names(CST_Anomaly(exp4, obs4, dim_anom = 'sdates', memb_dim = 'members')), CST_Anomaly(exp4, obs4, dim_anom = 'sdates', memb_dim = 'members', dat_dim = 1),
"Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension in element 'data'. Set it as NULL if there is no dataset dimension." "Parameter 'dat_dim' must be a character vector."
) )
# ftime_dim # ftime_dim
expect_error( expect_error(
......
...@@ -108,9 +108,8 @@ test_that("1. Input checks", { ...@@ -108,9 +108,8 @@ test_that("1. Input checks", {
) )
expect_warning( expect_warning(
CST_BiasCorrection(exp = exp2, obs = obs2), CST_BiasCorrection(exp = exp2, obs = obs2),
"Parameter 'obs' contains NA values",
"Parameter 'exp' contains NA values." "Parameter 'exp' contains NA values."
) )
# exp_cor # exp_cor
expect_error( expect_error(
CST_BiasCorrection(exp = exp1, obs = obs1, exp_cor = exp_cor1, sdate_dim = 'time'), CST_BiasCorrection(exp = exp1, obs = obs1, exp_cor = exp_cor1, sdate_dim = 'time'),
......
...@@ -91,9 +91,7 @@ test_that("1. Input checks", { ...@@ -91,9 +91,7 @@ test_that("1. Input checks", {
) )
expect_warning( expect_warning(
CST_Calibration(exp = exp2, obs = obs2, exp_cor = exp2), CST_Calibration(exp = exp2, obs = obs2, exp_cor = exp2),
"Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values."
"Parameter 'exp' contains NA values.",
"Parameter 'exp_cor' contains NA values."
) )
# exp_cor # exp_cor
expect_error( expect_error(
......
...@@ -26,7 +26,8 @@ obs2$data[1, 1, 2, 1, 1, 1] <- NA ...@@ -26,7 +26,8 @@ obs2$data[1, 1, 2, 1, 1, 1] <- NA
test_that("Sanity checks", { test_that("Sanity checks", {
expect_error( expect_error(
CST_CategoricalEnsCombination(exp = 1), CST_CategoricalEnsCombination(exp = 1),
"Parameter 'exp' and 'obs' must be of the class 's2dv_cube', " paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
"as output by CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_CategoricalEnsCombination(obs = 1), CST_CategoricalEnsCombination(obs = 1),
...@@ -83,6 +84,6 @@ test_that("Sanity checks", { ...@@ -83,6 +84,6 @@ test_that("Sanity checks", {
) )
expect_warning( expect_warning(
CST_CategoricalEnsCombination(exp = exp2, obs = obs2), CST_CategoricalEnsCombination(exp = exp2, obs = obs2),
"Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values." "Parameter 'exp' contains NA values."
) )
}) })
\ No newline at end of file
##############################################
test_that("1. Input checks", {
expect_error(
CST_ChangeDimNames(1),
"Parameter 'data' must be an object of class 's2dv_cube'."
)
expect_error(
CST_ChangeDimNames(lonlat_prec_st, 1, 'bbb'),
paste0("Parameter 'original_names' must be a character string or a ",
"vector of character strings.")
)
expect_error(
CST_ChangeDimNames(lonlat_prec_st, 'aaa', 1),
paste0("Parameter 'new_names' must be a character string or a ",
"vector of character strings.")
)
expect_error(
CST_ChangeDimNames(lonlat_prec_st, 'aaa', c('aaa', 'bbb')),
paste0("The number of dimension names in 'new_names' must be the same ",
"as in 'original_names'.")
)
expect_error(
CST_ChangeDimNames(lonlat_prec_st, 'aaa', 'bbb'),
paste0("Some of the dimensions in 'original_names' could not be found in ",
"'data'.")
)
})
##############################################
test_that("2. Output checks", {
exp <- CST_ChangeDimNames(lonlat_temp_st$exp,
original_names = c("lon", 'ftime', 'sdate'),
new_names = c("lons", 'ftimes', 'sdates'))
# dims
expect_equal(
dim(exp$data),
c(dataset = 1, var = 1, member = 15, sdates = 6, ftimes = 3, lat = 22, lons = 53)
)
expect_equal(
exp$dims,
dim(exp$data)
)
expect_equal(
as.vector(exp$data),
as.vector(lonlat_temp_st$exp$data)
)
# coords
expect_equal(
names(exp$coords),
c("dataset", "var", "member", "sdates", "ftimes", "lat", "lons")
)
# dim Dates
expect_equal(
dim(exp$attrs$Dates),
c(sdates = 6, ftimes = 3)
)
# variable metadata
expect_equal(
names(exp$attrs$Variable$metadata),
c("lat", "lons", "ftimes", "tas" )
)
# source_files
expect_equal(
dim(exp$attrs$source_files),
c(dataset = 1, var = 1, sdates = 6)
)
# Dates 'dim' attribute
dat <- CST_ChangeDimNames(lonlat_prec,
original_names = c("lon", 'ftime', 'sdate', 'member'),
new_names = c("lons", 'ftimes', 'sdates', 'members'))
expect_equal(
as.vector(lonlat_prec$data),
as.vector(dat$data)
)
expect_equal(
attributes(dat$attrs$Dates)$dim,
c(ftimes = 31, sdates = 3)
)
expect_equal(
attributes(exp$attrs$Dates)$dim,
c(sdates = 6, ftimes = 3)
)
expect_equal(
as.vector(dat$attrs$Dates),
as.vector(lonlat_prec$attrs$Dates)
)
# attribute dimensions
expect_equal(
attributes(dat$data)$dimensions,
names(dim(dat$data))
)
expect_equal(
attributes(exp$data)$dimensions,
NULL
)
})
\ No newline at end of file
############################################## ##############################################
test_that("Sanity checks", { # data1
data1 <- list(data = 1:10)
class(data1) <- 's2dv_cube'
# data2
data <- 1 : 20
dim(data) <- c(time = 20)
data2 <- list(data = data)
data2$dims <- dim(data)
data2$coords <- list(time = 1:20)
attr(data2$coords$time, 'indices') <- TRUE
class(data2) <- 's2dv_cube'
# exp
exp <- 1 : 20
dim(exp) <- c(time = 10, lat = 2)
exp <- list(data = exp)
class(exp) <- 's2dv_cube'
# data3
data3 <- data2
names(dim(data3$data)) <- 'Dim1'
data3$dims <- dim(data3$data)
names(data3$coords) <- 'Dim1'
##############################################
test_that("1. Sanity checks", {
expect_error( expect_error(
CST_MergeDims(data = 1), CST_MergeDims(data = 1),
paste0("Parameter 'data' must be of the class 's2dv_cube', ", paste0("Parameter 'data' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")) )
data <- list(data = 1:10)
class(data) <- 's2dv_cube'
expect_error( expect_error(
CST_MergeDims(data = data), CST_MergeDims(data = data1),
paste0("Parameter 'data' must have dimensions.")) paste0("Parameter 'data' must have dimensions.")
)
data <- 1 : 20
dim(data) <- c(time = 20)
data <- list(data = data)
class(data) <- 's2dv_cube'
expect_error( expect_error(
CST_MergeDims(data = data), CST_MergeDims(data = data2),
"Parameter 'merge_dims' must match with dimension names in parameter 'data'.") "Parameter 'merge_dims' must match with dimension names in parameter 'data'."
)
expect_error( expect_error(
CST_MergeDims(data = data, merge_dims = 1), CST_MergeDims(data = data2, merge_dims = 1),
paste0("Parameter 'merge_dims' must be a character vector indicating the names", paste0("Parameter 'merge_dims' must be a character vector indicating the ",
" of the dimensions to be merged.")) "names of the dimensions to be merged.")
)
expect_error( expect_error(
CST_MergeDims(data = data, merge_dims = 'time'), CST_MergeDims(data = data2, merge_dims = 'time'),
"Parameter 'merge_dims' must be of length two.") "Parameter 'merge_dims' must be of length two."
)
expect_error( expect_error(
CST_MergeDims(data = data, merge_dims = c('time', 'sdates')), CST_MergeDims(data = data2, merge_dims = c('time', 'sdates')),
paste0("Parameter 'merge_dims' must match with dimension ", paste0("Parameter 'merge_dims' must match with dimension ",
"names in parameter 'data'.")) "names in parameter 'data'.")
)
})
##############################################
exp <- 1 : 20 test_that("2. Output checks", {
dim(exp) <- c(time = 10, lat = 2)
exp <- list(data = exp)
class(exp) <- 's2dv_cube'
expect_equal( expect_equal(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat')), data) CST_MergeDims(data = exp, merge_dims = c('time', 'lat')),
data2
)
expect_warning( expect_warning(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat', 'lon')), CST_MergeDims(data = exp, merge_dims = c('time', 'lat', 'lon')),
paste0("Only two dimensions can be merge, only the first two dimension", paste0("Only two dimensions can be merge, only the first two dimension",
" will be used. To merge further dimensions consider to use this ", " will be used. To merge further dimensions consider to use this ",
"function multiple times.")) "function multiple times.")
)
expect_warning( expect_warning(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), rename_dim = c('lat', 'lon')), CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
rename_dim = c('lat', 'lon')),
paste0("Parameter 'rename_dim' has length greater than 1 and only the ", paste0("Parameter 'rename_dim' has length greater than 1 and only the ",
"first element will be used.")) "first element will be used.")
names(dim(data$data)) <- 'Dim1' )
expect_equal( expect_equal(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), rename_dim = 'Dim1'), CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
data) rename_dim = 'Dim1'),
data3
)
expect_equal( expect_equal(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
rename_dim = 'Dim1', na.rm = TRUE), data) rename_dim = 'Dim1', na.rm = TRUE),
data3
exp$data[1,] <- NA )
data <- c(2 : 10, 12 : 20)
dim(data) <- c(Dim1 = 18)
data <- list(data = data)
class(data) <- 's2dv_cube'
expect_equal( expect_equal(
CST_MergeDims(data = exp, merge_dims = c('time', 'lat'), CST_MergeDims(data = exp, merge_dims = c('time', 'lat'),
rename_dim = 'Dim1', na.rm = TRUE), data) rename_dim = 'Dim1', na.rm = TRUE),
data3
)
}) })
##############################################
\ No newline at end of file
...@@ -156,7 +156,7 @@ test_that("2. Output checks", { ...@@ -156,7 +156,7 @@ test_that("2. Output checks", {
memb_dim = 'members', sdate_dim = 'sdates') memb_dim = 'members', sdate_dim = 'sdates')
expect_equal( expect_equal(
names(res1), names(res1),
c('data', 'coords', 'attrs') c('data', 'coords', 'attrs', 'dims')
) )
expect_equal( expect_equal(
dim(res1$data), dim(res1$data),
......
...@@ -113,8 +113,7 @@ test_that("1. Sanity checks", { ...@@ -113,8 +113,7 @@ test_that("1. Sanity checks", {
# s2dv_cube # s2dv_cube
expect_error( expect_error(
CST_QuantileMapping(exp = 1), CST_QuantileMapping(exp = 1),
paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_QuantileMapping(exp = exp1), CST_QuantileMapping(exp = exp1),
...@@ -122,13 +121,11 @@ test_that("1. Sanity checks", { ...@@ -122,13 +121,11 @@ test_that("1. Sanity checks", {
) )
expect_error( expect_error(
CST_QuantileMapping(exp = exp1, obs = 1), CST_QuantileMapping(exp = exp1, obs = 1),
paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_QuantileMapping(exp = exp1, obs = obs1, exp_cor = 1), CST_QuantileMapping(exp = exp1, obs = obs1, exp_cor = 1),
paste0("Parameter 'exp_cor' must be of the class 's2dv_cube', as output ", paste0("Parameter 'exp_cor' must be of the class 's2dv_cube'.")
"by CSTools::CST_Load.")
) )
# exp and obs # exp and obs
expect_error( expect_error(
......
...@@ -31,8 +31,10 @@ cube3 <- cube1 ...@@ -31,8 +31,10 @@ cube3 <- cube1
# dat0 # dat0
dates0 <- as.Date('2022-02-01', format = "%Y-%m-%d") dates0 <- as.Date('2022-02-01', format = "%Y-%m-%d")
dim(dates0) <- c(sdate = 1) dim(dates0) <- c(sdate = 1)
# dat1 # dat1
dat1 <- array(1, dim = c(test = 1)) dat1 <- array(1, dim = c(test = 1))
# dat2 # dat2
dat2 <- array(1:5, dim = c(sdate = 5, lon = 4, lat = 4, ftime = 1)) dat2 <- array(1:5, dim = c(sdate = 5, lon = 4, lat = 4, ftime = 1))
coords2 <- list(sdate = c('20000101', '20010102', '20020103', '20030104', '20040105'), coords2 <- list(sdate = c('20000101', '20010102', '20020103', '20030104', '20040105'),
...@@ -43,14 +45,38 @@ dates2 <- c('20000101', '20010102', '20020103', '20030104', '20040105') ...@@ -43,14 +45,38 @@ dates2 <- c('20000101', '20010102', '20020103', '20030104', '20040105')
dates2 <- as.Date(dates2, format = "%Y%m%d", tz = "UTC") dates2 <- as.Date(dates2, format = "%Y%m%d", tz = "UTC")
dim(dates2) <- c(sdate = 5, ftime = 1) dim(dates2) <- c(sdate = 5, ftime = 1)
# dat3 (without sdate dim)
dat3 <- array(1:5, dim = c(lon = 4, lat = 4, ftime = 2))
coords3 <- list(sdate = c('20000101', '20010102'),
var = 'tas',
lon = 1.:4.,
lat = 1.:4.)
dates3 <- c('20000101', '20010102')
dates3 <- as.Date(dates3, format = "%Y%m%d", tz = "UTC")
dim(dates3) <- c(ftime = 2)
# dat4 (without ftime dim)
dat4 <- array(1:5, dim = c(sdate = 2, lon = 4, lat = 4))
coords4 <- list(sdate = c('20000101', '20010102'),
var = 'tas',
lon = 1.:4.,
lat = 1.:4.)
dates4 <- c('20000101', '20010102')
dates4 <- as.Date(dates4, format = "%Y%m%d", tz = "UTC")
dim(dates4) <- c(sdate = 2)
# dates5 (Dates with extra dimensions)
dates5 <- c('20000101', '20010102', '20010102', '20010102')
dates5 <- as.Date(dates5, format = "%Y%m%d", tz = "UTC")
dim(dates5) <- c(ftime = 2, test = 1, test2 = 2)
############################################## ##############################################
test_that("1. Input checks: CST_SaveExp", { test_that("1. Input checks: CST_SaveExp", {
# s2dv_cube # s2dv_cube
expect_error( expect_error(
CST_SaveExp(data = 1), CST_SaveExp(data = 1),
paste0("Parameter 'data' must be of the class 's2dv_cube', ", paste0("Parameter 'data' must be of the class 's2dv_cube'.")
"as output by CSTools::CST_Load.")
) )
# structure # structure
expect_error( expect_error(
...@@ -64,14 +90,6 @@ test_that("1. Input checks: CST_SaveExp", { ...@@ -64,14 +90,6 @@ test_that("1. Input checks: CST_SaveExp", {
CST_SaveExp(data = cube0), CST_SaveExp(data = cube0),
paste0("Level 'attrs' must be a list with at least 'Dates' element.") paste0("Level 'attrs' must be a list with at least 'Dates' element.")
) )
# cube0$attrs <- NULL
# cube0$attrs$Dates <- dates2
# expect_warning(
# CST_SaveExp(data = cube0, sdate_dim = c('sdate', 'sweek'),
# ftime_dim = 'ftime', memb_dim = NULL, dat_dim = NULL,
# var_dim = NULL, single_file = FALSE),
# paste0("Element 'coords' not found. No coordinates will be used.")
# )
# sdate_dim # sdate_dim
suppressWarnings( suppressWarnings(
...@@ -80,54 +98,24 @@ test_that("1. Input checks: CST_SaveExp", { ...@@ -80,54 +98,24 @@ test_that("1. Input checks: CST_SaveExp", {
paste0("Parameter 'sdate_dim' must be a character string.") paste0("Parameter 'sdate_dim' must be a character string.")
) )
) )
# expect_warning(
# CST_SaveExp(data = cube1, sdate_dim = c('sdate', 'sweek'),
# ftime_dim = 'ftime', memb_dim = NULL, dat_dim = NULL,
# var_dim = NULL),
# paste0("Parameter 'sdate_dim' has length greater than 1 and ",
# "only the first element will be used.")
# )
suppressWarnings( suppressWarnings(
expect_error( expect_error(
CST_SaveExp(data = cube1, sdate_dim = 'a', ftime_dim = 'ftime'), CST_SaveExp(data = cube1, sdate_dim = 'a', ftime_dim = 'ftime'),
paste0("Parameter 'sdate_dim' is not found in 'data' dimension.") paste0("Parameter 'sdate_dim' is not found in 'data' dimension.")
) )
) )
# # startdates # startdates
# expect_warning( # expect_warning(
# CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, startdates = 1), # dat_dim = NULL, var_dim = NULL, startdates = 1),
# "Parameter 'startdates' is not a character string, it will not be used." # paste0("Parameter 'startdates' doesn't have the same length ",
# "as dimension 'sdate', it will not be used.")
# ) # )
# expect_warning( # expect_warning(
# CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, startdates = '20100101'), # dat_dim = NULL, var_dim = NULL, startdates = '20100101'),
# paste0("Parameter 'startdates' doesn't have the same length ", # paste0("Parameter 'startdates' doesn't have the same length ",
# "as dimension '", sdate_dim,"', it will not be used.") # "as dimension '", 'sdate',"', it will not be used.")
# )
# # metadata
# expect_warning(
# CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL),
# paste0("No metadata found in element Variable from attrs.")
# )
cube1$attrs$Variable$metadata <- 'metadata'
expect_error(
CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
dat_dim = NULL, var_dim = NULL),
paste0("Element metadata from Variable element in attrs must be a list.")
)
cube1$attrs$Variable$metadata <- list(test = 'var')
# expect_warning(
# CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL),
# paste0("Metadata is not found for any coordinate.")
# )
cube1$attrs$Variable$metadata <- list(var = 'var')
# expect_warning(
# CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL),
# paste0("Metadata is not found for any variable.")
# ) # )
# memb_dim # memb_dim
suppressWarnings( suppressWarnings(
...@@ -143,6 +131,13 @@ test_that("1. Input checks: CST_SaveExp", { ...@@ -143,6 +131,13 @@ test_that("1. Input checks: CST_SaveExp", {
"as NULL if there is no member dimension.") "as NULL if there is no member dimension.")
) )
) )
# metadata
cube1$attrs$Variable$metadata <- 'metadata'
expect_error(
CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL,
dat_dim = NULL, var_dim = NULL),
paste0("Element metadata from Variable element in attrs must be a list.")
)
}) })
############################################## ##############################################
...@@ -167,22 +162,50 @@ test_that("1. Input checks", { ...@@ -167,22 +162,50 @@ test_that("1. Input checks", {
) )
# Dates # Dates
expect_error( expect_error(
SaveExp(data = array(1, dim = c(a = 1)), Dates = 'a'), SaveExp(data = array(1, dim = c(a = 1)), Dates = 'a', sdate_dim = NULL,
memb_dim = NULL, ftime_dim = 'a', dat_dim = NULL, var_dim = NULL),
paste0("Parameter 'Dates' must be of 'POSIXct' or 'Dates' class.") paste0("Parameter 'Dates' must be of 'POSIXct' or 'Dates' class.")
) )
expect_error( expect_error(
SaveExp(data = array(1, dim = c(a = 1)), SaveExp(data = array(1, dim = c(time = 1, sdate = 1, member = 1)),
dat_dim = NULL, var_dim = NULL,
Dates = as.Date('2022-02-01', format = "%Y-%m-%d")), Dates = as.Date('2022-02-01', format = "%Y-%m-%d")),
paste0("Parameter 'Dates' must have dimension names.") paste0("Parameter 'Dates' must have dimension names.")
) )
# # varname # drop_dims
# expect_warning( # expect_warning(
# SaveExp(data = dat2, coords = coords2, # SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')), # metadata = list(tas = list(level = '2m')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL, # Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL), # dat_dim = NULL, var_dim = NULL, drop_dims = 1),
# paste0("Parameter 'varname' is NULL. It will be assigned to 'X'.") # paste0("Parameter 'drop_dims' must be character string containing ",
# "the data dimension names to be dropped. It will not be used.")
# ) # )
# expect_warning(
# SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, drop_dims = 'time'),
# paste0("Parameter 'drop_dims' must be character string containing ",
# "the data dimension names to be dropped. It will not be used.")
# )
# expect_warning(
# SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, drop_dims = 'sdate'),
# paste0("Parameter 'drop_dims' can only contain dimension names ",
# "that are of length 1. It will not be used.")
# )
# expect_warning(
# SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, drop_dims = 'ftime'),
# paste0("Parameter 'drop_dims' contains dimensions used in the ",
# "computation. It will not be used.")
# )
# varname
suppressWarnings( suppressWarnings(
expect_error( expect_error(
SaveExp(data = dat2, coords = coords2, varname = 1, SaveExp(data = dat2, coords = coords2, varname = 1,
...@@ -191,30 +214,67 @@ test_that("1. Input checks", { ...@@ -191,30 +214,67 @@ test_that("1. Input checks", {
"Parameter 'varname' must be a character." "Parameter 'varname' must be a character."
) )
) )
# # coords # varname, metadata, spatial coords, unknown dim
# expect_warning(
# SaveExp(data = dat2, coords = list(sdate = coords2[[1]]),
# varname = 'tas', metadata = list(tas = list(level = '2m')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL),
# "Coordinate 'lon' is not provided and it will be set as index in element coords.",
# "Coordinate 'lat' is not provided and it will be set as index in element coords.",
# "Coordinate 'ftime' is not provided and it will be set as index in element coords."
# )
# # varname, metadata, spatial coords, unknown dim
# expect_warning(
# SaveExp(data = dat1, ftime_dim = NULL, sdate_dim = NULL, memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, single_file = TRUE),
# "Parameter 'varname' is NULL. It will be assigned to 'X'.",
# "Parameter 'metadata' is not provided so the metadata saved will be incomplete.",
# paste0("Spatial coordinates not found.")
# )
expect_error( expect_error(
SaveExp(data = dat1, varname = 1, ftime_dim = NULL, sdate_dim = NULL, SaveExp(data = dat1, varname = 1, ftime_dim = NULL, sdate_dim = NULL,
memb_dim = NULL, dat_dim = NULL, var_dim = NULL), memb_dim = NULL, dat_dim = NULL, var_dim = NULL),
paste0("Parameter 'varname' must be a character string with the ", paste0("Parameter 'varname' must be a character string with the ",
"variable names.") "variable names.")
) )
# ftime_dim
expect_error(
SaveExp(data = dat4, coords = coords4,
metadata = list(tas = list(level = '2m')),
Dates = dates4, ftime_dim = 'ftime', memb_dim = NULL,
dat_dim = NULL, var_dim = NULL),
paste0("Parameter 'ftime_dim' is not found in 'data' dimension.")
)
# Dates dimension check
# expect_warning(
# SaveExp(data = dat4, coords = coords4,
# metadata = list(tas = list(level = '2m')),
# Dates = NULL, ftime_dim = NULL, memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL),
# paste0("Dates must be provided if 'data' must be saved in separated files. ",
# "All data will be saved in a single file.")
# )
# Without ftime and sdate
expect_error(
SaveExp(data = dat3, coords = coords3,
metadata = list(tas = list(level = '2m')),
Dates = dates5, ftime_dim = 'ftime', memb_dim = NULL,
dat_dim = NULL, var_dim = NULL, sdate_dim = NULL),
paste0("Parameter 'Dates' can have only 'sdate_dim' and 'ftime_dim' ",
"dimensions of length greater than 1.")
)
# expect_warning(
# SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')),
# startdates = c(paste(1:11, collapse = '')),
# Dates = dates2, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, sdate_dim = 'sdate'),
# paste0("Parameter 'startdates' should be a character string containing ",
# "the start dates in the format 'yyyy-mm-dd', 'yyyymmdd', 'yyyymm', ",
# "'POSIXct' or 'Dates' class. Files will be named with Dates instead.")
# )
# expect_warning(
# SaveExp(data = dat2, coords = coords2,
# metadata = list(tas = list(level = '2m')),
# Dates = NULL, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, sdate_dim = 'sdate'),
# paste0("Dates must be provided if 'data' must be saved in separated files. ",
# "All data will be saved in a single file.")
# )
# (dat3) Without sdate_dim
# expect_warning(
# SaveExp(data = dat3, coords = coords3,
# metadata = list(tas = list(level = '2m')),
# Dates = NULL, ftime_dim = 'ftime', memb_dim = NULL,
# dat_dim = NULL, var_dim = NULL, sdate_dim = NULL,
# extra_string = 'nosdate3.nc', single_file = FALSE),
# paste0("Dates must be provided if 'data' must be saved in separated files. ",
# "All data will be saved in a single file.")
# )
}) })
############################################## ##############################################
...@@ -10,6 +10,7 @@ indices1 <- c(rep(1,5), rep(2,5), rep (3, 5), rep(4, 5)) ...@@ -10,6 +10,7 @@ indices1 <- c(rep(1,5), rep(2,5), rep (3, 5), rep(4, 5))
output1 <- matrix(data1$data, nrow = 5, ncol = 4) output1 <- matrix(data1$data, nrow = 5, ncol = 4)
names(dim(output1)) <- c('time', 'monthly') names(dim(output1)) <- c('time', 'monthly')
output1 <- list(data = output1) output1 <- list(data = output1)
output1$dims <- dim(output1$data)
class(output1) <- 's2dv_cube' class(output1) <- 's2dv_cube'
exp_cor <- 1 : 20 exp_cor <- 1 : 20
...@@ -21,6 +22,7 @@ class(exp_cor) <- 's2dv_cube' ...@@ -21,6 +22,7 @@ class(exp_cor) <- 's2dv_cube'
output2 <- matrix(data1$data, nrow = 5, ncol = 4) output2 <- matrix(data1$data, nrow = 5, ncol = 4)
names(dim(output2)) <- c('time', 'index') names(dim(output2)) <- c('time', 'index')
output2 <- list(data = output2) output2 <- list(data = output2)
output2$dims <- dim(output2$data)
class(output2) <- 's2dv_cube' class(output2) <- 's2dv_cube'
time2 <- c(seq(ISOdate(1903, 1, 1), ISOdate(1903, 1, 4), "days"), time2 <- c(seq(ISOdate(1903, 1, 1), ISOdate(1903, 1, 4), "days"),
...@@ -41,6 +43,7 @@ output3 <- c(data3$data, rep(NA, 4)) ...@@ -41,6 +43,7 @@ output3 <- c(data3$data, rep(NA, 4))
dim(output3) <- c(time = 8, monthly = 3) dim(output3) <- c(time = 8, monthly = 3)
result3 <- data3 result3 <- data3
result3$data <- output3 result3$data <- output3
result3$dims <- dim(result3$data)
# dat4 # dat4
data4 <- list(data = array(rnorm(10), dim = c(sdate = 2, lon = 5))) data4 <- list(data = array(rnorm(10), dim = c(sdate = 2, lon = 5)))
...@@ -51,8 +54,7 @@ class(data4) <- 's2dv_cube' ...@@ -51,8 +54,7 @@ class(data4) <- 's2dv_cube'
test_that("1. Input checks", { test_that("1. Input checks", {
expect_error( expect_error(
CST_SplitDim(data = 1), CST_SplitDim(data = 1),
paste0("Parameter 'data' must be of the class 's2dv_cube', as output by ", paste0("Parameter 'data' must be of the class 's2dv_cube'.")
"CSTools::CST_Load.")
) )
expect_error( expect_error(
CST_SplitDim(data = data1), CST_SplitDim(data = data1),
...@@ -93,38 +95,62 @@ test_that("2. Output checks", { ...@@ -93,38 +95,62 @@ test_that("2. Output checks", {
############################################## ##############################################
# test_that("3. Output checks: sample data", { test_that("3. Output checks: sample data", {
# output <- lonlat_temp$exp$data output <- lonlat_temp$exp$data
# output <- abind(output[, , , 1, ,], output[, , , 2, ,], output[, , , 3, ,], along = 5) output <- abind(output[, , , 1, ,], output[, , , 2, ,], output[, , , 3, ,], along = 5)
# dim(output) <- c(dataset = 1, member = 15, sdate = 6, ftime = 1, dim(output) <- c(dataset = 1, member = 15, sdate = 6, ftime = 1,
# lat = 22, lon = 53, monthly = 3) lat = 22, lon = 53, monthly = 3)
# result <- lonlat_temp$exp result <- lonlat_temp$exp
# result$data <- output result$data <- output
# expect_equal( result$attrs$Dates <- s2dv::Reorder(result$attrs$Dates, c('sdate', 'ftime'))
# CST_SplitDim(data = lonlat_temp$exp, split_dim = 'ftime'), dim(result$attrs$Dates) <- c(ftime = 1, sdate = 6, monthly = 3)
# result result$dims <- c(dataset = 1, member = 15, sdate = 6, ftime = 1, lat = 22,
# ) lon = 53, monthly = 3)
# expect_equal( attributes(result$attrs$Dates)$end <- NULL
# dim(CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', expect_equal(
# freq = 5)$data), CST_SplitDim(data = lonlat_temp$exp, split_dim = 'ftime', ftime_dim = 'ftime'),
# c(dataset = 1, member = 5, sdate = 6, ftime = 3, lat = 22, result
# lon = 53, index = 3) )
# ) expect_equal(
# expect_warning( dim(CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member',
# CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', freq = 5, freq = 5)$data),
# new_dim_name = c('a', 'b')), c(dataset = 1, member = 5, sdate = 6, ftime = 3, lat = 22,
# paste0("Parameter 'new_dim_name' has length greater than 1 ", lon = 53, index = 3)
# "and only the first elemenst is used.") )
# ) expect_warning(
# expect_error( CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', freq = 5,
# CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', freq = 5, new_dim_name = c('a', 'b')),
# new_dim_name = 3), paste0("Parameter 'new_dim_name' has length greater than 1 ",
# "Parameter 'new_dim_name' must be character string" "and only the first elemenst is used.")
# ) )
# expect_equal( expect_error(
# dim(CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member', freq = 5,
# freq = 5, new_dim_name = 'wt')$data), new_dim_name = 3),
# c(dataset = 1, member = 5, sdate = 6, ftime = 3, lat = 22, "Parameter 'new_dim_name' must be character string"
# lon = 53, wt = 3) )
# ) expect_equal(
# }) dim(CST_SplitDim(data = lonlat_temp$exp, split_dim = 'member',
freq = 5, new_dim_name = 'wt')$data),
c(dataset = 1, member = 5, sdate = 6, ftime = 3, lat = 22,
lon = 53, wt = 3)
)
})
##############################################
test_that("4. Output checks II", {
res <- CST_SplitDim(lonlat_prec_st, indices = lonlat_prec_st$attrs$Dates[,1],
split_dim = 'sdate', freq = 'year', return_indices = T)
expect_equal(
names(res),
c('data', 'indices')
)
expect_equal(
res$dims,
dim(res$data)
)
expect_equal(
all(names(dim(res$data$attrs$Dates)) %in% names(res$data$dims)),
TRUE
)
})
...@@ -50,6 +50,26 @@ test_that("2. Output checks: CST_Subset", { ...@@ -50,6 +50,26 @@ test_that("2. Output checks: CST_Subset", {
names(res1$coords), names(res1$coords),
c("member", "ftime", "lon") c("member", "ftime", "lon")
) )
## lat
expect_equal(
res1$coords$lat,
NULL
)
## lon
expect_equal(
as.vector(res1$coords$lon),
c(6, 7)
)
## sdate
expect_equal(
res1$coords$sdate,
NULL
)
## member
expect_equal(
as.vector(res1$coords$member),
c(1,2)
)
# Check attrs # Check attrs
expect_equal( expect_equal(
names(res1$attrs), names(res1$attrs),
...@@ -117,8 +137,8 @@ suppressWarnings( ...@@ -117,8 +137,8 @@ suppressWarnings(
sdate = c('20170101'), sdate = c('20170101'),
ensemble = indices(1), ensemble = indices(1),
time = indices(1:3), time = indices(1:3),
lat = indices(1:10), lat = indices(1:2),
lon = indices(1:10), lon = indices(1:2),
synonims = list(lat = c('lat', 'latitude'), synonims = list(lat = c('lat', 'latitude'),
lon = c('lon', 'longitude')), lon = c('lon', 'longitude')),
return_vars = list(time = 'sdate', return_vars = list(time = 'sdate',
...@@ -146,7 +166,7 @@ test_that("3. Output checks with Start", { ...@@ -146,7 +166,7 @@ test_that("3. Output checks with Start", {
# Check dimensions # Check dimensions
expect_equal( expect_equal(
dim(res8$data), dim(res8$data),
c(dat = 1, var = 1, sdate = 1, ensemble = 1, time = 3, lat = 10, lon = 2) c(dat = 1, var = 1, sdate = 1, ensemble = 1, time = 3, lat = 2, lon = 2)
) )
expect_equal( expect_equal(
dim(res8$data), dim(res8$data),
...@@ -154,7 +174,7 @@ test_that("3. Output checks with Start", { ...@@ -154,7 +174,7 @@ test_that("3. Output checks with Start", {
) )
expect_equal( expect_equal(
dim(res10$data), dim(res10$data),
c(time = 3, lat = 10, lon = 2) c(time = 3, lat = 2, lon = 2)
) )
# Check coordinates # Check coordinates
expect_equal( expect_equal(
...@@ -227,7 +247,7 @@ test_that("3. Output checks with Start", { ...@@ -227,7 +247,7 @@ test_that("3. Output checks with Start", {
var_dim = 'var', drop = 'non-selected') var_dim = 'var', drop = 'non-selected')
expect_equal( expect_equal(
dim(res11$data), dim(res11$data),
c(dat = 1, var = 1, time = 2, lat = 10, lon = 2) c(dat = 1, var = 1, time = 2, lat = 2, lon = 2)
) )
expect_equal( expect_equal(
names(res11$coords), names(res11$coords),
......