diff --git a/DESCRIPTION b/DESCRIPTION index 1700491ba8fcc927dfd059484765ebb3c939211b..0085fa4ff7fa1b79aa7e32ec2dccc172ef633dec 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,6 @@ Package: CSTools -Title: Assessing Skill of Climate Forecasts on Seasonal-to-Decadal - Timescales -Version: 5.0.0 +Title: Assessing Skill of Climate Forecasts on Seasonal-to-Decadal Timescales +Version: 5.0.1 Authors@R: c( person("Nuria", "Perez-Zanon", , "nuria.perez@bsc.es", role = "aut", comment = c(ORCID = "0000-0001-8568-3071")), person("Louis-Philippe", "Caron", , "louis-philippe.caron@bsc.es", role = "aut", comment = c(ORCID = "0000-0001-5221-0147")), @@ -42,20 +41,20 @@ Description: Exploits dynamical seasonal forecasts in order to provide contains process-based methods for forecast calibration, bias correction, statistical and stochastic downscaling, optimal forecast combination and multivariate verification, as well as basic and advanced tools to obtain - tailored products. This package was developed in the context of the ERA4CS - project MEDSCOPE and the H2020 S2S4E project and includes contributions from - ArticXchange project founded by EU-PolarNet 2. - Pérez-Zanón et al. (2022) . - Doblas-Reyes et al. (2005) . - Mishra et al. (2018) . - Sanchez-Garcia et al. (2019) . - Straus et al. (2007) . - Terzago et al. (2018) . - Torralba et al. (2017) . - D'Onofrio et al. (2014) . - Verfaillie et al. (2017) . - Van Schaeybroeck et al. (2019) . - Yiou et al. (2013) . + tailored products. This package was developed in the context of the 'ERA4CS' + project 'MEDSCOPE' and the 'H2020 S2S4E' project and includes contributions from + 'ArticXchange' project founded by 'EU-PolarNet 2'. + 'Pérez-Zanón et al. (2022) '. + 'Doblas-Reyes et al. (2005) '. + 'Mishra et al. (2018) '. + 'Sanchez-Garcia et al. (2019) '. + 'Straus et al. (2007) '. + 'Terzago et al. (2018) '. + 'Torralba et al. (2017) '. + 'D'Onofrio et al. (2014) '. + 'Verfaillie et al. (2017) '. + 'Van Schaeybroeck et al. (2019) '. + 'Yiou et al. (2013) '. Depends: R (>= 3.5.0), maps, @@ -92,3 +91,4 @@ License: GPL-3 Encoding: UTF-8 LazyData: true RoxygenNote: 7.2.0 +Config/testthat/edition: 3 diff --git a/NEWS.md b/NEWS.md index 29128b8d45dc1d417349e0ca54df2b2d3f86bea9..5474779afe4fcc46415e9906c5c974e2628e4580 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,157 +1,172 @@ -# CSTools 5.0.0 (Release date: 05-04-2023) +# CSTools 5.0.1 (Release date: 06-06-2023) +- Resubmit to CRAN because it was archived due to dependency issue + **Fixes** -- Correct vignettes: Analogs, MultiModelSkill and MultivarRMSE -- Add 'ncores' to s2dv function calls in CST_Anomaly -- Reduce computing time of examples and tests and improve documentation - -**New features** -- Add dat_dim parameter in CST_BiasCorrection and CST_Calibration -- New plotting function for case studies temporal visualisation: PlotWeeklyClim -- Deprecate indices in dim_anom parameter of CST_Anomaly -- Allow memb_dim to be NULL in QuantileMapping -- Uncomment tests in CST_MultivarRMSE due to correction of RMS in s2dv next release (released s2dv 1.4.0 21/03) -- New s2dv_cube object development for all the functions, unit tests, examples and vignettes -- New function CST_Subset similar to Subset with 's2dv_cubes' -- Improved CST_SaveExp function with new features -- New color set in PlotForecastPDF Vitigeoss colors -- New function CST_InsertDim - +- Standardize the coordinates of 's2dv_cube' by setting the coordinates to vectors with NULL dimensions +- Include a check for SaveExp output filenames that only contain date information +- In SaveExp the dates parameter is allowed to have additional time dimensions of length 1 +- Removed dot from the internal function named mergedatasets() used in CST_CategoricalEnsCombination + +**New Features** +- New parameter 'startdates' in CST_SaveExp used to manually decide the name of the output files + **Other** -- Added contribution from ArticXchange project due to PlotWeeklyClim -- Update NEWS.md with the correct format -- Change Licence - -# CSTools 4.1.1 (Release date: 10-11-2022) -**Fixes** -- CST_Analogs corrected input of ClimProjDiags::Subset() -- PlotCombinedMap corrected use of 'cex_bar_titles' parameter -- CST_Anomaly added 'memb_dim', 'dat_dim' and 'ftime_dim' and improved use for 'dim_anom' parameters - -# CSTools 4.1.0 (Release date: 25-10-2022) -**New features** -- Dependency on package 's2dverification' is changed to 's2dv' -- CST_BiasCorrection new parameters 'memb_dim', 'sdate_dim', 'ncores' -- CST_Calibration is able to calibrate forecast with new parameter 'exp_cor' -- CST_QuantileMapping uses cross-validation and provides option to remove NAs; new parameters 'memb_dim', 'sdate_dim', 'window_dim' and 'na.rm'; 'sample_dim' and 'sample_length' are removed -- s2dv_cube() new parameter 'time_dim' - -**Fixes** -- as.s2dv_cube() detects latitude and longitude structure in startR_array object -- Data correction: 'lonlat_data' is renamed to 'lonlat_temp'; 'lonlat_prec' is corrected by one-day shift -- Typo and parameter correction in vignette 'MostLikelyTercile_vignette' -- Figure and result correction in vignette 'RainFARM_vignette' -- PlotMostLikelyQuantileMap() works with s2dv::PlotLayout - -# CSTools 4.0.1 (Release date: 05-10-2021) -**New features** -- Dynamical Bias Correction method: `CST_ProxiesAttractors` and `CST_DynBiasCorrection` (optionally `Predictability`) -- CST_BiasCorrection and BiasCorrection allows to calibrate a forecast given the calibration in the hindcast by using parameter 'exp_cor'. -- Use cases -- CST_SaveExp includes parameter extra_string -- PlotCombinedMap includes parameter cex_bar_titles - -**Fixes** -- Calibration retains correlation absolute value -- Calibration fixed when cal.methodi == rpc-based, apply_to == sign, eval.method == 'leave-one-out' and the correlation is not significant -- PlotMostLikelyQuantileMap reoder latitudes of an array provided in 'dots' parameter. - -# CSTools 4.0.0 (Release date: 23-02-2021) -**New features** -- ADAMONT downscaling method: requires CST_AdamontAnalogs and CST_AdamontQQCor functions -- Analogs method using Predictors: requires training_analogs and CST_AnalogsPredictors -- PlotPDFsOLE includes parameters to modify legend style -- CST_RFSlope handless missing values in the temporal dimension and new 'ncores' parameter allows parallel computation -- CST_RFWeights accepts s2dv_cube objects as input and new 'ncores' paramenter allows parallel computation -- RFWeights is exposed to users -- CST_RainFARM accepts multi-dimensional slopes and weights and handless missing values in sample dimensions. -- QuantileMapping is exposed to users -- CST_MultiMetric includes 'rpss' metric and it is addapted to s2dv. -- PlotMostLikelyQuantileMap vignette -- PlotTriangles4Categories includes two parameters to adjust axis and margins -- CategoricalEnsCombination is exposed to users -- CST_SplitDims includes parameter 'insert_ftime' -- Analogs vignette -- Data Storage and retrieval vignette +- Switch to testthat version 3 +# CSTools 5.0.0 (Release date: 05-04-2023) **Fixes** -- PlotForecastPDF correctly displays terciles labels -- CST_SaveExp correctly save time units -- CST_SplitDims returns ordered output following ascending order provided in indices when it is numeric -- qmap library moved from Imports to Depends -- CST_QuantileMapping correctly handles exp_cor -- Figures resize option from vignettes has been removed -- Fix Analogs to work with three diferent criteria -- Vignette PlotForecastPDF updated plots -- Decrease package size compresing vignettes figures and removing areave_data sample - -# CSTools 3.1.0 (Release date: 02-07-2020) -**New features** -- EnsClustering vignette -- EnsClustering has a new parameter 'time_dim' -- CST_BiasCorrection has na.rm paramter -- CST_Anomaly allows to smooth the climatology with filter.span parameter -- PlotTriangles4Categories new plotting function to convert any 3-d numerical array to a grid of coloured triangles. -- CST_WeatherRegimes/WeatherRegimes and CST_RegimeAssign/RegimeAssign -- PlotPDFsOLE plots two probability density gaussian functions and the optimal linear estimation -- CST_RFTemp/RF_Temp functions available for downscaling temperature -- Weather Regimes vignette +- Correct vignettes: Analogs, MultiModelSkill and MultivarRMSE +- Add 'ncores' to s2dv function calls in CST_Anomaly +- Reduce computing time of examples and tests and improve documentation -**Fixes** -- CST_Anomaly handles exp, obs or both -- PlotForecastPDF vignette displays figures correctly -- Calibration function is exposed to users -- MultiMetric vignette fixed typo text description -- RainFARM checks 'slope' is not a vector -- DESCRIPTION specifies the minimum multiApply version required -- EnsClustering has a fixed 'closest_member' output -- PlotCombinedMap handles masks correctly -- CST_SaveExp uses multiApply and save time dimension correctly - -# CSTools 3.0.0 (Release date: 10-02-2020) **New features** -- CST_MergeDims and MergeDims -- Version working with R 3.4.2 -- PlotForecastPDF handles independent terciles, extremes and observations for each panel - -**Fixes** -- CST_Calibration handles missing values -- BEI functions handle missing values - -# CSTools 2.0.0 (Release date: 25-11-2019) +- Add dat_dim parameter in CST_BiasCorrection and CST_Calibration +- New plotting function for case studies temporal visualisation: PlotWeeklyClim +- Deprecate indices in dim_anom parameter of CST_Anomaly +- Allow memb_dim to be NULL in QuantileMapping +- Uncomment tests in CST_MultivarRMSE due to correction of RMS in s2dv next release (released s2dv 1.4.0 21/03) +- New s2dv_cube object development for all the functions, unit tests, examples and vignettes +- New function CST_Subset similar to Subset with 's2dv_cubes' +- Improved CST_SaveExp function with new features +- New color set in PlotForecastPDF Vitigeoss colors +- New function CST_InsertDim + +**Other** +- Added contribution from ArticXchange project due to PlotWeeklyClim +- Update NEWS.md with the correct format +- Change Licence + +# CSTools 4.1.1 (Release date: 10-11-2022) +**Fixes** +- CST_Analogs corrected input of ClimProjDiags::Subset() +- PlotCombinedMap corrected use of 'cex_bar_titles' parameter +- CST_Anomaly added 'memb_dim', 'dat_dim' and 'ftime_dim' and improved use for 'dim_anom' parameters + +# CSTools 4.1.0 (Release date: 25-10-2022) +**New features** +- Dependency on package 's2dverification' is changed to 's2dv' +- CST_BiasCorrection new parameters 'memb_dim', 'sdate_dim', 'ncores' +- CST_Calibration is able to calibrate forecast with new parameter 'exp_cor' +- CST_QuantileMapping uses cross-validation and provides option to remove NAs; new parameters 'memb_dim', 'sdate_dim', 'window_dim' and 'na.rm'; 'sample_dim' and 'sample_length' are removed +- s2dv_cube() new parameter 'time_dim' + +**Fixes** +- as.s2dv_cube() detects latitude and longitude structure in startR_array object +- Data correction: 'lonlat_data' is renamed to 'lonlat_temp'; 'lonlat_prec' is corrected by one-day shift +- Typo and parameter correction in vignette 'MostLikelyTercile_vignette' +- Figure and result correction in vignette 'RainFARM_vignette' +- PlotMostLikelyQuantileMap() works with s2dv::PlotLayout + +# CSTools 4.0.1 (Release date: 05-10-2021) +**New features** +- Dynamical Bias Correction method: `CST_ProxiesAttractors` and `CST_DynBiasCorrection` (optionally `Predictability`) +- CST_BiasCorrection and BiasCorrection allows to calibrate a forecast given the calibration in the hindcast by using parameter 'exp_cor' +- Use cases +- CST_SaveExp includes parameter extra_string +- PlotCombinedMap includes parameter cex_bar_titles + +**Fixes** +- Calibration retains correlation absolute value +- Calibration fixed when cal.methodi == rpc-based, apply_to == sign, eval.method == 'leave-one-out' and the correlation is not significant +- PlotMostLikelyQuantileMap reoder latitudes of an array provided in 'dots' parameter + +# CSTools 4.0.0 (Release date: 23-02-2021) **New features** -- CST_Analogs Analogs downscaling method, -- CST_MultiEOFS for multiple variables, -- Ensemble Clustering, -- Categorical Ensemble Combination, -- new Calibration methods included in CST_Calibration, -- Best Estimated Index method, -- CST_QuantileMapping, -- CST_SplitDim to split dimension, if it is a temporal dimension, it can be split by days, months and years or other inidices, -- creation and transformation to class 's2dv_cube', -- CST_SaveExp function for saving experiments to be loadable with CST_Load, -- Parallelization of RainFARM downscaling -- Adding unit tests using testthat for BEI and RainFarm functions -- New vignette Best Estimate Index -- Addapting CST_Load to use 'as.s2dv_cube' function -- Adding reference to S2S4E H2020 project into the DESCRIPTION file -- Adding NEWS.md file - -**Fixes** -- Minor fix in CST_BiasCorrection when checking parameter 'obs' -- Minor fix in data lonlat_prec to be of class 's2dv_cube' -- Minor fix in RainFARM vignette - -### CSTools 1.0.1 (Release date: 19-06-2019) -**Fixes and new features** -- Correcting test of PlotForecastPDF for compatibility with ggplot2 release -- New function PlotCombinedMap -- Adding reference to MEDSCOPE ERA4CS Project into the DESCRIPTION file -- Documentation minor fix in CST_RFWeights -- Minor fix in PlotMostLikelyQuantileMap for bar_titles -- MultiModelSkill vignette updated to use PlotCombinedMap - -### CSTools 1.0.0 (Release date: 24-04-2019) -- Features included: Load, Anomaly, MultiMetric, MultivarRMSE, Calibration, BiasCorrection, RainFARM Downscaling, PlotForecastPDF, PlotMostLikelyQuantileMap -- Three sample data: lonlat_data, lonlat_prec, areave_data -- Unit tests using testthat: BiasCorrection, Calibration, MultiMetric, PlotForecast -- Vignettes: MultiMetric, Multivar and RainFARM +- ADAMONT downscaling method: requires CST_AdamontAnalogs and CST_AdamontQQCor functions +- Analogs method using Predictors: requires training_analogs and CST_AnalogsPredictors +- PlotPDFsOLE includes parameters to modify legend style +- CST_RFSlope handless missing values in the temporal dimension and new 'ncores' parameter allows parallel computation +- CST_RFWeights accepts s2dv_cube objects as input and new 'ncores' paramenter allows parallel computation +- RFWeights is exposed to users +- CST_RainFARM accepts multi-dimensional slopes and weights and handless missing values in sample dimensions +- QuantileMapping is exposed to users +- CST_MultiMetric includes 'rpss' metric and it is addapted to s2dv +- PlotMostLikelyQuantileMap vignette +- PlotTriangles4Categories includes two parameters to adjust axis and margins +- CategoricalEnsCombination is exposed to users +- CST_SplitDims includes parameter 'insert_ftime' +- Analogs vignette +- Data Storage and retrieval vignette + +**Fixes** +- PlotForecastPDF correctly displays terciles labels +- CST_SaveExp correctly save time units +- CST_SplitDims returns ordered output following ascending order provided in indices when it is numeric +- qmap library moved from Imports to Depends +- CST_QuantileMapping correctly handles exp_cor +- Figures resize option from vignettes has been removed +- Fix Analogs to work with three diferent criteria +- Vignette PlotForecastPDF updated plots +- Decrease package size compresing vignettes figures and removing areave_data sample + +# CSTools 3.1.0 (Release date: 02-07-2020) +**New features** +- EnsClustering vignette +- EnsClustering has a new parameter 'time_dim' +- CST_BiasCorrection has na.rm paramter +- CST_Anomaly allows to smooth the climatology with filter.span parameter +- PlotTriangles4Categories new plotting function to convert any 3-d numerical array to a grid of coloured triangles +- CST_WeatherRegimes/WeatherRegimes and CST_RegimeAssign/RegimeAssign +- PlotPDFsOLE plots two probability density gaussian functions and the optimal linear estimation +- CST_RFTemp/RF_Temp functions available for downscaling temperature +- Weather Regimes vignette + +**Fixes** +- CST_Anomaly handles exp, obs or both +- PlotForecastPDF vignette displays figures correctly +- Calibration function is exposed to users +- MultiMetric vignette fixed typo text description +- RainFARM checks 'slope' is not a vector +- DESCRIPTION specifies the minimum multiApply version required +- EnsClustering has a fixed 'closest_member' output +- PlotCombinedMap handles masks correctly +- CST_SaveExp uses multiApply and save time dimension correctly + +# CSTools 3.0.0 (Release date: 10-02-2020) +**New features** +- CST_MergeDims and MergeDims +- Version working with R 3.4.2 +- PlotForecastPDF handles independent terciles, extremes and observations for each panel + +**Fixes** +- CST_Calibration handles missing values +- BEI functions handle missing values + +# CSTools 2.0.0 (Release date: 25-11-2019) +**New features** +- CST_Analogs Analogs downscaling method, +- CST_MultiEOFS for multiple variables, +- Ensemble Clustering, +- Categorical Ensemble Combination, +- new Calibration methods included in CST_Calibration, +- Best Estimated Index method, +- CST_QuantileMapping, +- CST_SplitDim to split dimension, if it is a temporal dimension, it can be split by days, months and years or other inidices, +- creation and transformation to class 's2dv_cube', +- CST_SaveExp function for saving experiments to be loadable with CST_Load, +- Parallelization of RainFARM downscaling +- Adding unit tests using testthat for BEI and RainFarm functions +- New vignette Best Estimate Index +- Addapting CST_Load to use 'as.s2dv_cube' function +- Adding reference to S2S4E H2020 project into the DESCRIPTION file +- Adding NEWS.md file + +**Fixes** +- Minor fix in CST_BiasCorrection when checking parameter 'obs' +- Minor fix in data lonlat_prec to be of class 's2dv_cube' +- Minor fix in RainFARM vignette + +### CSTools 1.0.1 (Release date: 19-06-2019) +**Fixes and new features** +- Correcting test of PlotForecastPDF for compatibility with ggplot2 release +- New function PlotCombinedMap +- Adding reference to MEDSCOPE ERA4CS Project into the DESCRIPTION file +- Documentation minor fix in CST_RFWeights +- Minor fix in PlotMostLikelyQuantileMap for bar_titles +- MultiModelSkill vignette updated to use PlotCombinedMap + +### CSTools 1.0.0 (Release date: 24-04-2019) +- Features included: Load, Anomaly, MultiMetric, MultivarRMSE, Calibration, BiasCorrection, RainFARM Downscaling, PlotForecastPDF, PlotMostLikelyQuantileMap +- Three sample data: lonlat_data, lonlat_prec, areave_data +- Unit tests using testthat: BiasCorrection, Calibration, MultiMetric, PlotForecast +- Vignettes: MultiMetric, Multivar and RainFARM diff --git a/R/CST_CategoricalEnsCombination.R b/R/CST_CategoricalEnsCombination.R index 86c40df1b4813dd653a8ea511ad1dd0582126bc0..3c92d587efb6aa4be76bf7137bf68aa557bc4e40 100644 --- a/R/CST_CategoricalEnsCombination.R +++ b/R/CST_CategoricalEnsCombination.R @@ -189,8 +189,8 @@ CategoricalEnsCombination <- function (fc, obs, cat.method, eval.method, amt.cat if (any(is.na(obs))) { warning("Parameter 'obs' contains NA values.") } - fc.merged <- merge.datasets(fc = fc) - amt.sdate=dim(fc.merged)["sdate"] + fc.merged <- mergedatasets(fc = fc) + amt.sdate = dim(fc.merged)["sdate"] target.dims <- c("member", "sdate") return.feat <- list(amt.cat = amt.cat) return.feat$dim <- c(amt.cat, amt.sdate) @@ -209,7 +209,7 @@ CategoricalEnsCombination <- function (fc, obs, cat.method, eval.method, amt.cat return(cat_fc_out) } -merge.datasets <- function(fc){ +mergedatasets <- function(fc) { dims.tmp <- dim(fc) dimnames.tmp <- dimnames(fc) names.dim.tmp <- names(dims.tmp) diff --git a/R/CST_SaveExp.R b/R/CST_SaveExp.R index 1c419be56453ef5e9515cd4a0b7130a4b0826ec4..2c363e46b1ae076a3bf50d1b2b3fd0e57f256c7b 100644 --- a/R/CST_SaveExp.R +++ b/R/CST_SaveExp.R @@ -28,7 +28,14 @@ #' dimension. #'@param 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. +#' dimension. +#'@param 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 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. #'@param single_file A logical value indicating if all object is saved in a #' 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 @@ -76,7 +83,8 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', ftime_dim = 'time', dat_dim = 'dataset', var_dim = 'var', memb_dim = 'member', - single_file = FALSE, extra_string = NULL) { + startdates = NULL, single_file = FALSE, + extra_string = NULL) { # Check 's2dv_cube' if (!inherits(data, 's2dv_cube')) { stop("Parameter 'data' must be of the class 's2dv_cube', ", @@ -124,13 +132,28 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', "only the first element will be used.") sdate_dim <- sdate_dim[1] } + } else if (length(dim(data$attrs$Dates)) == 1) { + sdate_dim <- 'sdate' + dim(data$data) <- c(sdate = 1, dim(data$data)) + data$dims <- dim(data$data) + dim(data$attrs$Dates) <- c(sdate = 1, dim(data$attrs$Dates)) + data$coords[[sdate_dim]] <- data$attrs$Dates[1] + } + # startdates + if (is.null(startdates)) { + startdates <- data$coords[[sdate_dim]] } else { - if (length(dim(data$attrs$Dates)) == 1) { - sdate_dim <- 'sdate' - dim(data$data) <- c(sdate = 1, dim(data$data)) - data$dims <- dim(data$data) - dim(data$attrs$Dates) <- c(sdate = 1, dim(data$attrs$Dates)) - data$coords[[sdate_dim]] <- data$attrs$Dates[1] + if (!is.character(startdates)) { + warning(paste0("Parameter 'startdates' is not a character string, ", + "it will not be used.")) + startdates <- data$coords[[sdate_dim]] + } + if (!is.null(sdate_dim)) { + if (dim(data$data)[sdate_dim] != length(startdates)) { + warning(paste0("Parameter 'startdates' doesn't have the same length ", + "as dimension '", sdate_dim,"', it will not be used.")) + startdates <- data$coords[[sdate_dim]] + } } } @@ -141,7 +164,7 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', varname = data$attrs$Variable$varName, metadata = data$attrs$Variable$metadata, Datasets = data$attrs$Datasets, - startdates = data$coords[[sdate_dim]], + startdates = startdates, dat_dim = dat_dim, sdate_dim = sdate_dim, ftime_dim = ftime_dim, var_dim = var_dim, memb_dim = memb_dim, @@ -173,8 +196,12 @@ CST_SaveExp <- function(data, destination = "./", sdate_dim = 'sdate', #' lists for each variable. #'@param Datasets A vector of character string indicating the names of the #' datasets. -#'@param startdates A vector of dates indicating the initialization date of each -#' simulations. +#'@param 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. #'@param 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 #' start date dimension. @@ -325,8 +352,7 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, # Spatial coordinates if (!any(dimnames %in% .KnownLonNames()) | !any(dimnames %in% .KnownLatNames())) { - warning("Spatial coordinate names do not match any of the names accepted by ", - "the package.") + warning("Spatial coordinates not found.") lon_dim <- NULL lat_dim <- NULL } else { @@ -433,17 +459,21 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, all(names(dim(Dates)) == c(sdate_dim, ftime_dim))) { if (is.null(startdates)) { startdates <- Subset(Dates, along = ftime_dim, 1, drop = 'selected') - } else if ((!inherits(startdates, "POSIXct") & !inherits(startdates, "Date")) && - (!is.character(startdates) | (all(nchar(startdates) != 10) & - all(nchar(startdates) != 8) & all(nchar(startdates) != 6)))) { + } else if ((!inherits(startdates, "POSIXct") & !inherits(startdates, "Date")) && + (!is.character(startdates) | (any(nchar(startdates) > 10) | any(nchar(startdates) < 1)))) { warning("Parameter 'startdates' should be a character string containing ", "the start dates in the format 'yyyy-mm-dd', 'yyyymmdd', 'yyyymm', ", - "'POSIXct' or 'Dates' class.") + "'POSIXct' or 'Dates' class. Files will be named with Dates instead.") startdates <- Subset(Dates, along = ftime_dim, 1, drop = 'selected') + if (!is.null(format(startdates, "%Y%m%d"))) { + startdates <- format(startdates, "%Y%m%d") + } } + } else if (all(dim(Dates)[!names(dim(Dates)) %in% c(ftime_dim, sdate_dim)] == 1)) { + dim(Dates) <- dim(Dates)[names(dim(Dates)) %in% c(ftime_dim, sdate_dim)] } else { stop("Parameter 'Dates' must have start date dimension and ", - "forecast time dimension.") + "forecast time dimension.") } } # startdates @@ -487,7 +517,7 @@ SaveExp <- function(data, destination = "./", Dates = NULL, coords = NULL, alldims <- c(dat_dim, var_dim, sdate_dim, lon_dim, lat_dim, memb_dim, ftime_dim) if (!all(dimnames %in% alldims)) { unknown_dims <- dimnames[which(!dimnames %in% alldims)] - warning("Detected unknown dimension: ", paste(unknown_dims, collapse = ', ')) + # warning("Detected unknown dimension: ", paste(unknown_dims, collapse = ', ')) memb_dim <- c(memb_dim, unknown_dims) alldims <- c(dat_dim, var_dim, sdate_dim, lon_dim, lat_dim, memb_dim, ftime_dim) } diff --git a/R/CST_Subset.R b/R/CST_Subset.R index 35717757c0679bec88b8cdc293b99a24425f4116..372e39812329c4cc06624b37a427086d5a971cf2 100644 --- a/R/CST_Subset.R +++ b/R/CST_Subset.R @@ -71,6 +71,12 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, stop("Parameter 'dat_dim' must be a character string.") } } + # Check indices + if (!is.list(indices)) { + if (length(along) == 1) { + indices <- list(indices) + } + } # Subset data x$data <- ClimProjDiags::Subset(x$data, along = along, @@ -85,12 +91,7 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, # Only rename coordinates that have not been dropped if (dim_name %in% names(x$dims)) { # Subset coordinate by indices - if (is.null(dim(x$coords[[dim_name]])) | length(dim(x$coords[[dim_name]])) == 1) { - x$coords[[dim_name]] <- .subset_with_attrs(x$coords[[dim_name]], index) - } else { - x$coords[[dim_name]] <- ClimProjDiags::Subset(x$coords[[dim_name]], along = dim_name, - indices = index) - } + x$coords[[dim_name]] <- .subset_with_attrs(x$coords[[dim_name]], indices = index) } } # Remove dropped coordinates @@ -117,17 +118,6 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, indices = index, drop = drop) } - if ((dim_name %in% names(x$dims)) && - (dim_name %in% names(x$attrs$Variable$metadata))) { - variable <- x$attrs$Variable$metadata[[dim_name]] - # Subset coords by indices - if (is.null(dim(variable)) | length(dim(variable)) == 1) { - x$attrs$Variable$metadata[[dim_name]] <- .subset_with_attrs(variable, index) - } else { - x$attrs$Variable$metadata[[dim_name]] <- ClimProjDiags::Subset(variable, along = dim_name, - indices = index) - } - } } # Remove metadata from variables that were dropped vars_to_keep <- na.omit(match(c(names(x$dims), (x$attrs$Variable$varName)), @@ -143,20 +133,33 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, indices = time_indices, drop = drop) } + # Subset metadata + for (variable in 1:length(names(x$attrs$Variable$metadata))) { + if (any(along %in% names(dim(x$attrs$Variable$metadata[[variable]])))) { + dim_along <- along[along %in% names(dim(x$attrs$Variable$metadata[[variable]]))] + index_along <- indices[[which(along == dim_along)]] + x$attrs$Variable$metadata[[variable]] <- .subset_with_attrs(x$attrs$Variable$metadata[[variable]], + along = dim_along, + indices = index_along, + drop = drop) + } + } return(x) } -# Function to subset vectors with attributes +# Function to subset with attributes .subset_with_attrs <- function(x, ...) { - l <- x[...] - x.dims <- names(dim(x)) + args_subset <- list(...) + if (is.null(dim(x)) | length(dim(x)) == 1) { + l <- x[args_subset[['indices']]] + } else { + l <- ClimProjDiags::Subset(x, along = args_subset[['along']], + indices = args_subset[['indices']], + drop = args_subset[['drop']]) + } attr.names <- names(attributes(x)) attr.names <- attr.names[attr.names != 'names'] attr.names <- attr.names[attr.names != 'dim'] attributes(l)[attr.names] <- attributes(x)[attr.names] - if (is.null(dim(l))) { - dim(l) <- length(l) - } - names(dim(l)) <- x.dims return(l) } diff --git a/R/as.s2dv_cube.R b/R/as.s2dv_cube.R index e39e5cadb2130830631c994291c8a30ec19a71f6..75d6a6da8fbed42cda36f42af09af68c1d1c69b1 100644 --- a/R/as.s2dv_cube.R +++ b/R/as.s2dv_cube.R @@ -21,33 +21,30 @@ #'functions with the prefix \code{CST} from CSTools and CSIndicators packages. #'The object is mainly a list with the following elements:\cr #'\itemize{ -#' \item{'data', array with named dimensions.} -#' \item{'dims', named vector of the data dimensions.} -#' \item{'coords', named list with elements of the coordinates corresponding to -#' the dimensions of the data parameter. If any coordinate is not provided, it -#' is set as an index vector with the values from 1 to the length of the -#' corresponding dimension. The attribute 'indices' indicates wether the -#' coordinate is an index vector (TRUE) or not (FALSE).} +#' \item{'data', array with named dimensions;} +#' \item{'dims', named vector of the data dimensions;} +#' \item{'coords', list of named vectors with the coordinates corresponding to +#' the dimensions of the data parameter;} #' \item{'attrs', named list with elements: #' \itemize{ #' \item{'Dates', array with named temporal dimensions of class 'POSIXct' -#' from time values in the data.} +#' from time values in the data;} #' \item{'Variable', has the following components: #' \itemize{ #' \item{'varName', character vector of the short variable name. It is #' usually specified in the parameter 'var' from the functions -#' Start and Load.} +#' Start and Load;} #' \item{'metadata', named list of elements with variable metadata. #' They can be from coordinates variables (e.g. longitude) or -#' main variables (e.g. 'var').} +#' main variables (e.g. 'var');} #' } #' } #' \item{'Datasets', character strings indicating the names of the -#' datasets.} +#' datasets;} #' \item{'source_files', a vector of character strings with complete paths -#' to all the found files involved in loading the data.} +#' to all the found files involved in loading the data;} #' \item{'when', a time stamp of the date issued by the Start() or Load() -#' call to obtain the data.} +#' call to obtain the data;} #' \item{'load_parameters', it contains the components used in the #' arguments to load the data from Start() or Load() functions.} #' } @@ -148,6 +145,7 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, obj_i$coords$lon <- as.vector(obj_i$attrs$lon) } else { obj_i$coords$lon <- obj_i$attrs$lon + dim(obj_i$coords$lon) <- NULL attr(obj_i$coords$lon, 'indices') <- FALSE } obj_i$attrs$Variable$metadata$lon <- obj_i$attrs$lon @@ -159,6 +157,7 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, obj_i$coords$lat <- as.vector(obj_i$attrs$lat) } else { obj_i$coords$lat <- obj_i$attrs$lat + dim(obj_i$coords$lat) <- NULL attr(obj_i$coords$lat, 'indices') <- FALSE } obj_i$attrs$Variable$metadata$lat <- obj_i$attrs$lat @@ -183,7 +182,7 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, if (isTRUE(remove_null)) { obj_i$attrs$load_parameters <- .rmNullObs(obj_i$attrs$load_parameters) } - obj_i <- obj_i[c('data','dims','coords','attrs')] + obj_i <- obj_i[c('data', 'dims', 'coords', 'attrs')] class(obj_i) <- 's2dv_cube' if (names(obs_exp)[[i]] == 'exp') { result$exp <- obj_i @@ -231,8 +230,8 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, result$coords[[i_coord]] <- as.vector(coord_in_fileselector[[i_coord]][[1]]) } else { result$coords[[i_coord]] <- coord_in_fileselector[[i_coord]][[1]] + attr(result$coords[[i_coord]], 'indices') <- FALSE } - if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- FALSE } else { result$coords[[i_coord]] <- 1:dims[i_coord] if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- TRUE @@ -246,14 +245,15 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, if (length(coord_in_common) == dims[i_coord]) { if (remove_attrs_coords) { if (inherits(coord_in_common, "POSIXct")) { - result$coords[[i_coord]] <- coord_in_common + result$coords[[i_coord]] <- 1:dims[i_coord] + attr(result$coords[[i_coord]], 'indices') <- TRUE } else { result$coords[[i_coord]] <- as.vector(coord_in_common) } } else { result$coords[[i_coord]] <- coord_in_common + attr(result$coords[[i_coord]], 'indices') <- FALSE } - if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- FALSE } else { result$coords[[i_coord]] <- 1:dims[i_coord] if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- TRUE @@ -273,8 +273,8 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, } } else { result$coords[[i_coord]] <- coord_in_dat + attr(result$coords[[i_coord]], 'indices') <- FALSE } - if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- FALSE } else { result$coords[[i_coord]] <- 1:dims[i_coord] if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- TRUE @@ -287,6 +287,7 @@ as.s2dv_cube <- function(object, remove_attrs_coords = FALSE, result$coords[[i_coord]] <- 1:dims[i_coord] if (!remove_attrs_coords) attr(result$coords[[i_coord]], 'indices') <- TRUE } + dim(result$coords[[i_coord]]) <- NULL } # attrs diff --git a/R/s2dv_cube.R b/R/s2dv_cube.R index 5f9c465b8490dda0c6635ba43cbd2a65b7f7d33c..f4e5be6689bc9f86cc4b55d4e37cf08bbc8b127b 100644 --- a/R/s2dv_cube.R +++ b/R/s2dv_cube.R @@ -12,11 +12,12 @@ #' #'@param data A multidimensional array with named dimensions, typically with #' dimensions: dataset, member, sdate, ftime, lat and lon. -#'@param coords A named list with elements of the coordinates corresponding to -#' 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 -#' provided, it is set as an index vector with the values from 1 to the length -#' of the corresponding dimension. +#'@param coords A list of named vectors with the coordinates corresponding to +#' the dimensions of the data parameter. If any coordinate has dimensions, they +#' will be set as NULL. If any coordinate is not provided, it is set as an +#' index vector with the values from 1 to the length of the corresponding +#' dimension. The attribute 'indices' indicates wether the coordinate is an +#' index vector (TRUE) or not (FALSE). #'@param varName A character string indicating the abbreviation of the variable #' name. #'@param metadata A named list where each element is a variable containing the @@ -36,33 +37,30 @@ #'@return The function returns an object of class 's2dv_cube' with the following #' elements in the structure:\cr #'\itemize{ -#' \item{'data', array with named dimensions.} -#' \item{'dims', named vector of the data dimensions.} -#' \item{'coords', named list with elements of the coordinates corresponding to -#' the dimensions of the data parameter. If any coordinate is not provided, it -#' is set as an index vector with the values from 1 to the length of the -#' corresponding dimension. The attribute 'indices' indicates wether the -#' coordinate is an index vector (TRUE) or not (FALSE).} +#' \item{'data', array with named dimensions;} +#' \item{'dims', named vector of the data dimensions;} +#' \item{'coords', list of named vectors with the coordinates corresponding to +#' the dimensions of the data parameter;} #' \item{'attrs', named list with elements: #' \itemize{ #' \item{'Dates', array with named temporal dimensions of class 'POSIXct' from -#' time values in the data.} +#' time values in the data;} #' \item{'Variable', has the following components: #' \itemize{ #' \item{'varName', with the short name of the loaded variable as specified -#' in the parameter 'var'.} -#' \item{''metadata', named list of elements with variable metadata. +#' in the parameter 'var';} +#' \item{'metadata', named list of elements with variable metadata. #' They can be from coordinates variables (e.g. longitude) or -#' main variables (e.g. 'var').} +#' main variables (e.g. 'var');} #' } #' } -#' \item{'Datasets', character strings indicating the names of the dataset.} -#' \item{'source_files', a vector of character strings with complete paths to -#' all the found files involved in loading the data.} -#' \item{'when', a time stamp of the date issued by the Start() or Load() call to -#' obtain the data.} -#' \item{'load_parameters', it contains the components used in the arguments to -#' load the data from Start() or Load() functions.} +#' \item{'Datasets', character strings indicating the names of the dataset;} +#' \item{'source_files', a vector of character strings with complete paths +#' to all the found files involved in loading the data;} +#' \item{'when', a time stamp of the date issued by the Start() or Load() +#' call to obtain the data;} +#' \item{'load_parameters', it contains the components used in the +#' arguments to load the data from Start() or Load() functions.} #' } #' } #'} @@ -144,6 +142,7 @@ s2dv_cube <- function(data, coords = NULL, varName = NULL, metadata = NULL, attr(coords[[i_coord]], 'indices') <- TRUE } } + dim(coords[[i_coord]]) <- NULL } else { coords <- sapply(names(dims), function(x) 1:dims[x]) for (i in 1:length(coords)) { diff --git a/data/lonlat_prec.rda b/data/lonlat_prec.rda index db5f1f06b3f4be4c9336de8d7281d3d43f8cc59e..f8212aac52a213aaa7c08b779de86cf77ecb0078 100644 Binary files a/data/lonlat_prec.rda and b/data/lonlat_prec.rda differ diff --git a/data/lonlat_temp.rda b/data/lonlat_temp.rda index 94db7af93b455d8899346cfd62977e051489bb4e..513be2c0c7b0634ab7cf7ecfd7afaa1231b00efe 100644 Binary files a/data/lonlat_temp.rda and b/data/lonlat_temp.rda differ diff --git a/man/CST_SaveExp.Rd b/man/CST_SaveExp.Rd index 032f01c4ca3b3ee0b6c46bd0c09ee9bd2652e5bd..f8918eb9aa88ca8fae91b62951a31590378b8f27 100644 --- a/man/CST_SaveExp.Rd +++ b/man/CST_SaveExp.Rd @@ -12,6 +12,7 @@ CST_SaveExp( dat_dim = "dataset", var_dim = "var", memb_dim = "member", + startdates = NULL, single_file = FALSE, extra_string = NULL ) @@ -45,6 +46,14 @@ dimension.} By default, it is set to 'member'. It can be NULL if there is no member dimension.} +\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 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{single_file}{A logical value indicating if all object is saved in a 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 diff --git a/man/SaveExp.Rd b/man/SaveExp.Rd index d0418131c288acd2626fb90b3b76c58cbca8a0ec..a9c0ac36d9e65636fcb938c60b9fb88404555bcb 100644 --- a/man/SaveExp.Rd +++ b/man/SaveExp.Rd @@ -47,8 +47,12 @@ lists for each variable.} \item{Datasets}{A vector of character string indicating the names of the datasets.} -\item{startdates}{A vector of dates indicating the initialization date of each -simulations.} +\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 diff --git a/man/as.s2dv_cube.Rd b/man/as.s2dv_cube.Rd index e80044f5dcd8455e91a1527b8ddbe57934df90e1..30f9abd40e6f2d9bd26143a4cc9d12a588d48d11 100644 --- a/man/as.s2dv_cube.Rd +++ b/man/as.s2dv_cube.Rd @@ -25,33 +25,30 @@ The function returns an 's2dv_cube' object to be easily used with functions with the prefix \code{CST} from CSTools and CSIndicators packages. The object is mainly a list with the following elements:\cr \itemize{ - \item{'data', array with named dimensions.} - \item{'dims', named vector of the data dimensions.} - \item{'coords', named list with elements of the coordinates corresponding to - the dimensions of the data parameter. If any coordinate is not provided, it - is set as an index vector with the values from 1 to the length of the - corresponding dimension. The attribute 'indices' indicates wether the - coordinate is an index vector (TRUE) or not (FALSE).} + \item{'data', array with named dimensions;} + \item{'dims', named vector of the data dimensions;} + \item{'coords', list of named vectors with the coordinates corresponding to + the dimensions of the data parameter;} \item{'attrs', named list with elements: \itemize{ \item{'Dates', array with named temporal dimensions of class 'POSIXct' - from time values in the data.} + from time values in the data;} \item{'Variable', has the following components: \itemize{ \item{'varName', character vector of the short variable name. It is usually specified in the parameter 'var' from the functions - Start and Load.} + Start and Load;} \item{'metadata', named list of elements with variable metadata. They can be from coordinates variables (e.g. longitude) or - main variables (e.g. 'var').} + main variables (e.g. 'var');} } } \item{'Datasets', character strings indicating the names of the - datasets.} + datasets;} \item{'source_files', a vector of character strings with complete paths - to all the found files involved in loading the data.} + to all the found files involved in loading the data;} \item{'when', a time stamp of the date issued by the Start() or Load() - call to obtain the data.} + call to obtain the data;} \item{'load_parameters', it contains the components used in the arguments to load the data from Start() or Load() functions.} } diff --git a/man/s2dv_cube.Rd b/man/s2dv_cube.Rd index ff302ccd93252143b387eb102991c3d1edb29caa..e17a460781b027e02e4fcf5b401a8c0ebbd7ddd6 100644 --- a/man/s2dv_cube.Rd +++ b/man/s2dv_cube.Rd @@ -20,11 +20,12 @@ s2dv_cube( \item{data}{A multidimensional array with named dimensions, typically with dimensions: dataset, member, sdate, ftime, lat and lon.} -\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 -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 -of the corresponding dimension.} +\item{coords}{A list of named vectors with the coordinates corresponding to +the dimensions of the data parameter. If any coordinate has dimensions, they +will be set as NULL. If any coordinate is not provided, it is set as an +index vector with the values from 1 to the length of the corresponding +dimension. The attribute 'indices' indicates wether the coordinate is an +index vector (TRUE) or not (FALSE).} \item{varName}{A character string indicating the abbreviation of the variable name.} @@ -52,33 +53,30 @@ stored in the end of 'attrs' element. Multiple elements are accepted.} The function returns an object of class 's2dv_cube' with the following elements in the structure:\cr \itemize{ - \item{'data', array with named dimensions.} - \item{'dims', named vector of the data dimensions.} - \item{'coords', named list with elements of the coordinates corresponding to - the dimensions of the data parameter. If any coordinate is not provided, it - is set as an index vector with the values from 1 to the length of the - corresponding dimension. The attribute 'indices' indicates wether the - coordinate is an index vector (TRUE) or not (FALSE).} + \item{'data', array with named dimensions;} + \item{'dims', named vector of the data dimensions;} + \item{'coords', list of named vectors with the coordinates corresponding to + the dimensions of the data parameter;} \item{'attrs', named list with elements: \itemize{ \item{'Dates', array with named temporal dimensions of class 'POSIXct' from - time values in the data.} + time values in the data;} \item{'Variable', has the following components: \itemize{ \item{'varName', with the short name of the loaded variable as specified - in the parameter 'var'.} - \item{''metadata', named list of elements with variable metadata. + in the parameter 'var';} + \item{'metadata', named list of elements with variable metadata. They can be from coordinates variables (e.g. longitude) or - main variables (e.g. 'var').} + main variables (e.g. 'var');} } } - \item{'Datasets', character strings indicating the names of the dataset.} - \item{'source_files', a vector of character strings with complete paths to - all the found files involved in loading the data.} - \item{'when', a time stamp of the date issued by the Start() or Load() call to - obtain the data.} - \item{'load_parameters', it contains the components used in the arguments to - load the data from Start() or Load() functions.} + \item{'Datasets', character strings indicating the names of the dataset;} + \item{'source_files', a vector of character strings with complete paths + to all the found files involved in loading the data;} + \item{'when', a time stamp of the date issued by the Start() or Load() + call to obtain the data;} + \item{'load_parameters', it contains the components used in the + arguments to load the data from Start() or Load() functions.} } } } diff --git a/tests/testthat/test-BEI_PDFBest.R b/tests/testthat/test-BEI_PDFBest.R index 55070e81341699ca5fca573cd25bfbd0cd1ca3b4..bf27a5aaabe57887ea9f9db403eac2997bfeda9f 100644 --- a/tests/testthat/test-BEI_PDFBest.R +++ b/tests/testthat/test-BEI_PDFBest.R @@ -1,4 +1,4 @@ -context("Generic tests") +############################################## test_that("basic use case", { index_obs<- c(2, 3, 0.6) dim(index_obs) <- c(time = 3) diff --git a/tests/testthat/test-BEI_Weights.R b/tests/testthat/test-BEI_Weights.R index 26347eb6c78f9e38d70a56ea837239ba06e1fc32..888b231e866cb45b916c0e2b5177476045085dde 100644 --- a/tests/testthat/test-BEI_Weights.R +++ b/tests/testthat/test-BEI_Weights.R @@ -1,4 +1,4 @@ -context("Generic tests") +############################################## test_that("basic use case", { index_weight <- 1 : (3 * 3) dim(index_weight) <- c(sdate = 3, member = 3) diff --git a/tests/testthat/test-CST_Analogs.R b/tests/testthat/test-CST_Analogs.R index 66e1c88a8026878285ee3a705014184e0319f88a..80e5da861cba5e430e3c5dd78b6c7f143d298156 100644 --- a/tests/testthat/test-CST_Analogs.R +++ b/tests/testthat/test-CST_Analogs.R @@ -1,5 +1,3 @@ -context("CSTools::CST_Analogs tests") - ############################################## # dat1 diff --git a/tests/testthat/test-CST_Anomaly.R b/tests/testthat/test-CST_Anomaly.R index a4c7f3f57778c5cfde40bb4b25a1a1490143d757..772352aa1e4280219b83f0dff812401731dceddb 100644 --- a/tests/testthat/test-CST_Anomaly.R +++ b/tests/testthat/test-CST_Anomaly.R @@ -1,5 +1,3 @@ -context("CSTools::CST_Anomaly tests") - ############################################## # dat set.seed(1) diff --git a/tests/testthat/test-CST_BEI_Weighting.R b/tests/testthat/test-CST_BEI_Weighting.R index 3a70f38aa20452884e95d338711c0daea432fb62..bf2579c0c9db96ad1277efaa27e43b3171799baa 100644 --- a/tests/testthat/test-CST_BEI_Weighting.R +++ b/tests/testthat/test-CST_BEI_Weighting.R @@ -1,4 +1,4 @@ -context("CSTools::CST_CST_BEI_Weighting tests") +############################################## # dat var_exp <- array(1:6, dim = c(sdate = 2, member = 3)) diff --git a/tests/testthat/test-CST_BiasCorrection.R b/tests/testthat/test-CST_BiasCorrection.R index 46f57ace7e5e861925901f05657a328fca5ed17b..ec9b596e889a56798bfab20e55df2e467eece00a 100644 --- a/tests/testthat/test-CST_BiasCorrection.R +++ b/tests/testthat/test-CST_BiasCorrection.R @@ -1,5 +1,3 @@ -context("CSTools::CST_BiasCorrection tests") - ############################################## # dat1 diff --git a/tests/testthat/test-CST_Calibration.R b/tests/testthat/test-CST_Calibration.R index e6aef6f641559e7a35a379bb0bd24d294a6110fc..64a09e5a6cbc1f93a9a1a28ed05c4598adae7d56 100644 --- a/tests/testthat/test-CST_Calibration.R +++ b/tests/testthat/test-CST_Calibration.R @@ -1,5 +1,3 @@ -context("CSTools::CST_Calibration tests") - ############################################## # dat diff --git a/tests/testthat/test-CST_CategoricalEnsCombination.R b/tests/testthat/test-CST_CategoricalEnsCombination.R index 2dfc4a036026d002f628018a140637f51466f2b5..a52f822b9a4a28ff8545caaa067e3587b3ea79dd 100644 --- a/tests/testthat/test-CST_CategoricalEnsCombination.R +++ b/tests/testthat/test-CST_CategoricalEnsCombination.R @@ -1,6 +1,5 @@ -context("CSTools::CST_CategoricalEnsCombination tests") - ############################################## + # dat1 dat_exp <- array(abs(rnorm(4*6*3*4*4))*275, dim = c(dataset = 1, member = 4, sdate = 6, ftime = 3, lat = 4, lon = 4)) diff --git a/tests/testthat/test-CST_EnsClustering.R b/tests/testthat/test-CST_EnsClustering.R index afbe1598d740bc649fc6579abd9effb048044afd..3415a4894b82d5e015437c220ffbc1ec98fc51d9 100644 --- a/tests/testthat/test-CST_EnsClustering.R +++ b/tests/testthat/test-CST_EnsClustering.R @@ -1,6 +1,5 @@ -context("CSTools::CST_EnsClustering tests") - ############################################## + # dat1 data <- rnorm(2 * 10 * 4 * 5 * 6 * 7) dim(data) <- c(dataset = 2, member = 10, diff --git a/tests/testthat/test-CST_InsertDim.R b/tests/testthat/test-CST_InsertDim.R index 39e5ade86562f5aaf1f00c61a10d54f479bd34c2..c4f4b1d81ee89304c0dfc65a0e3ae4fd6c0c8c2e 100644 --- a/tests/testthat/test-CST_InsertDim.R +++ b/tests/testthat/test-CST_InsertDim.R @@ -1,5 +1,3 @@ -context("CSTools::CST_InsertDim tests") - ############################################## exp <- lonlat_temp$exp ############################################## diff --git a/tests/testthat/test-CST_MergeDims.R b/tests/testthat/test-CST_MergeDims.R index 45bcd8e960248b8f3168047786c780dfe9f1b7cd..0717a3064d595dc117326ee80be445884095b3cf 100644 --- a/tests/testthat/test-CST_MergeDims.R +++ b/tests/testthat/test-CST_MergeDims.R @@ -1,4 +1,5 @@ -context("Generic tests") +############################################## + test_that("Sanity checks", { expect_error( CST_MergeDims(data = 1), diff --git a/tests/testthat/test-CST_MultiEOF.R b/tests/testthat/test-CST_MultiEOF.R index f4843231cc664f85c586e49862c123f3afcc1005..66a6352516148ddc6e88ae0148c3c404202f55ec 100644 --- a/tests/testthat/test-CST_MultiEOF.R +++ b/tests/testthat/test-CST_MultiEOF.R @@ -1,6 +1,3 @@ -context("CSTools::CST_MultiEOF tests") - - ############################################## # exp1, exp2, exp03 diff --git a/tests/testthat/test-CST_MultiMetric.R b/tests/testthat/test-CST_MultiMetric.R index 5c45e6a5da6590f7304920621e9efb67579809d8..952ec5b75ed6ff2c086b3c150d3fc9449303a00b 100644 --- a/tests/testthat/test-CST_MultiMetric.R +++ b/tests/testthat/test-CST_MultiMetric.R @@ -1,5 +1,3 @@ -context("CSTools::CST_MultiMetric") - ################################################################################ # dat diff --git a/tests/testthat/test-CST_MultivarRMSE.R b/tests/testthat/test-CST_MultivarRMSE.R index b9f93f6862a42efe4c956608f17b8b33c761cd9e..f7bc347c844773cf85c9c05e261bf73c528613cd 100644 --- a/tests/testthat/test-CST_MultivarRMSE.R +++ b/tests/testthat/test-CST_MultivarRMSE.R @@ -1,4 +1,4 @@ -context("CSTools::CST_MultivarRMSE tests") +############################################## # dat1 set.seed(1) diff --git a/tests/testthat/test-CST_ProxiesAttractor.R b/tests/testthat/test-CST_ProxiesAttractor.R index 65831b24a9fdd4e7beef7c625ed4b45577c44676..ce585e0b15eddd347c40ab1c74cd59d38c0817c4 100644 --- a/tests/testthat/test-CST_ProxiesAttractor.R +++ b/tests/testthat/test-CST_ProxiesAttractor.R @@ -1,5 +1,3 @@ -context("CSTools::CST_ProxiesAttractor tests") - ############################################## # dat1 diff --git a/tests/testthat/test-CST_QuantileMapping.R b/tests/testthat/test-CST_QuantileMapping.R index a086ab93d3de7f2f6a4187bec54b0c5e95596e9b..e12f1d9a87353abf17019441dfc87c6b199efd3a 100644 --- a/tests/testthat/test-CST_QuantileMapping.R +++ b/tests/testthat/test-CST_QuantileMapping.R @@ -1,5 +1,3 @@ -context("CSTools::CST_QuantileMapping tests") - ############################################## # dat1 diff --git a/tests/testthat/test-CST_RFSlope.R b/tests/testthat/test-CST_RFSlope.R index f08d3a591443fb5a3a79cd2bd89d8058b2441263..a4411b4fe6e4e2ef0f6826e571064c6d6b5cd26d 100644 --- a/tests/testthat/test-CST_RFSlope.R +++ b/tests/testthat/test-CST_RFSlope.R @@ -1,6 +1,5 @@ -context("CSTools::CST_RFSlope tests") - ############################################## + # dat1 data <- rnorm(2 * 10 * 4 * 5 * 6 * 7) dim(data) <- c(dataset = 2, member = 10, diff --git a/tests/testthat/test-CST_RFTemp.R b/tests/testthat/test-CST_RFTemp.R index 325b260becf999915a952fca004c4674335d1201..334a8909911e4b869fb2088f89940d43a64a524e 100644 --- a/tests/testthat/test-CST_RFTemp.R +++ b/tests/testthat/test-CST_RFTemp.R @@ -1,5 +1,3 @@ -context("CSTools::CST_RFTemp tests") - ############################################## # dat1 diff --git a/tests/testthat/test-CST_RFWeights.R b/tests/testthat/test-CST_RFWeights.R index 68b45830fd751f7d1c820ef83e7563e1c8f8d40d..bb11f33606fc09418de45c3fb8352ad1d022365a 100644 --- a/tests/testthat/test-CST_RFWeights.R +++ b/tests/testthat/test-CST_RFWeights.R @@ -1,6 +1,5 @@ -context("CSTools::CST_RFTWeights tests") - ############################################## + # dat1 data <- rnorm(2 * 10 * 4 * 5 * 6 * 6) dim(data) <- c(dataset = 2, member = 10, diff --git a/tests/testthat/test-CST_RainFARM.R b/tests/testthat/test-CST_RainFARM.R index c014cfdc68e2fdd046efe7221f95264ff74e1795..dcb7dc6f1ff6367ed753b9bb09b05251c1534359 100644 --- a/tests/testthat/test-CST_RainFARM.R +++ b/tests/testthat/test-CST_RainFARM.R @@ -1,6 +1,5 @@ -context("CSTools::CST_RFSlope tests") - ############################################## + # dat1 data <- rnorm(2 * 10 * 4 * 5 * 6 * 7) dim(data) <- c(dataset = 2, member = 10, diff --git a/tests/testthat/test-CST_RegimesAssign.R b/tests/testthat/test-CST_RegimesAssign.R index 456aaf8172200d8e4a603019d023007994483e44..f7c835c1c82230bdf88448495d986d069c797577 100644 --- a/tests/testthat/test-CST_RegimesAssign.R +++ b/tests/testthat/test-CST_RegimesAssign.R @@ -1,5 +1,3 @@ -context("CSTools::CST_RegimesAssign") - ############################################## test_that("1. Input checks", { diff --git a/tests/testthat/test-CST_SaveExp.R b/tests/testthat/test-CST_SaveExp.R index cb028d6a42ad8803cf8bce745ad1ae8cd7eb1cf1..7f6332703111445e711d9ad53a94d22c2d7ff43e 100644 --- a/tests/testthat/test-CST_SaveExp.R +++ b/tests/testthat/test-CST_SaveExp.R @@ -1,4 +1,3 @@ -context("CSTools::CST_SaveExp tests") ############################################## # cube0 @@ -94,6 +93,18 @@ test_that("1. Input checks: CST_SaveExp", { paste0("Parameter 'sdate_dim' is not found in 'data' dimension.") ) ) + # # startdates + # expect_warning( + # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, + # dat_dim = NULL, var_dim = NULL, startdates = 1), + # "Parameter 'startdates' is not a character string, it will not be used." + # ) + # expect_warning( + # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, + # dat_dim = NULL, var_dim = NULL, startdates = '20100101'), + # paste0("Parameter 'startdates' doesn't have the same length ", + # "as dimension '", sdate_dim,"', it will not be used.") + # ) # # metadata # expect_warning( # CST_SaveExp(data = cube1, ftime_dim = 'ftime', memb_dim = NULL, @@ -132,11 +143,6 @@ test_that("1. Input checks: CST_SaveExp", { "as NULL if there is no member dimension.") ) ) - # expect_warning( - # CST_SaveExp(data = cube2, memb_dim = c('member', 'ensemble'), - # ftime_dim = 'ftime', dat_dim = NULL, var_dim = NULL), - # paste0("Detected unknown dimension: test, test2") - # ) }) ############################################## @@ -201,9 +207,7 @@ test_that("1. Input checks", { # 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 coordinate names do not match any of the names accepted by ", - # "the package."), - # "Detected unknown dimension: test" + # paste0("Spatial coordinates not found.") # ) expect_error( SaveExp(data = dat1, varname = 1, ftime_dim = NULL, sdate_dim = NULL, diff --git a/tests/testthat/test-CST_SplitDim.R b/tests/testthat/test-CST_SplitDim.R index c2652477c71fc52616aa41e3fd799f5c02be1638..0c8f2e013e815c94c8d88157aca12e60b3f2b0cc 100644 --- a/tests/testthat/test-CST_SplitDim.R +++ b/tests/testthat/test-CST_SplitDim.R @@ -1,6 +1,3 @@ -context("CSTools::CST_SplitDim tests") - - ############################################## # dat1 diff --git a/tests/testthat/test-CST_Subset.R b/tests/testthat/test-CST_Subset.R index cb567279f715e40de2d3b5116c2a04961fbc94c1..2d270e38a1eb315f78f31ac191e9a0f5c7fcf29a 100644 --- a/tests/testthat/test-CST_Subset.R +++ b/tests/testthat/test-CST_Subset.R @@ -1,5 +1,3 @@ -context("CSTools::CST_Subset tests") - ############################################## library(startR) @@ -241,7 +239,7 @@ test_that("3. Output checks with Start", { ) expect_equal( dim(res11$coords$time), - c(sdate = 1, time = 2) + NULL ) expect_equal( dim(res11$attrs$source_files), diff --git a/tests/testthat/test-CST_WeatherRegimes.R b/tests/testthat/test-CST_WeatherRegimes.R index ebf8730de07460b2d92cde998ca211c6a9212503..796c551923623732e28b6ba0c00ebc8879d4ec59 100644 --- a/tests/testthat/test-CST_WeatherRegimes.R +++ b/tests/testthat/test-CST_WeatherRegimes.R @@ -1,6 +1,5 @@ -context("Generic tests") - ############################################## + # dat1 data <- rnorm(2 * 10 * 4 * 5 * 6 * 7) dim(data) <- c(dataset = 2, member = 10, diff --git a/tests/testthat/test-PlotForecastPDF.R b/tests/testthat/test-PlotForecastPDF.R index f39b9667c47c91db81f126028802896c23ada36b..9a831ede6c39bb061d3dd875e76ba28f0353f6b3 100644 --- a/tests/testthat/test-PlotForecastPDF.R +++ b/tests/testthat/test-PlotForecastPDF.R @@ -1,4 +1,5 @@ -context("Generic tests") +############################################## + test_that("Sanity checks", { expect_error( PlotForecastPDF(fcst, tercile.limits), diff --git a/tests/testthat/test-PlotPDFsOLE.R b/tests/testthat/test-PlotPDFsOLE.R index 91e61d08c2c7c08c67d32d70410c1ee9c68f9770..68f5ada2b4ad989f1f5e7d5e68bba79fe39d9ec8 100644 --- a/tests/testthat/test-PlotPDFsOLE.R +++ b/tests/testthat/test-PlotPDFsOLE.R @@ -1,4 +1,4 @@ -context("Generic tests") +############################################## test_that("Sanity checks", { pdf_1 <- c(1.1,0.6) diff --git a/tests/testthat/test-PlotTriangles4Categories.R b/tests/testthat/test-PlotTriangles4Categories.R index 8105cc91388aba7a1ac20f80041907a5d7a2de0d..40a8b25b0a2cedf474ba9139a2b35c4210df9643 100644 --- a/tests/testthat/test-PlotTriangles4Categories.R +++ b/tests/testthat/test-PlotTriangles4Categories.R @@ -1,4 +1,5 @@ -context("Generic tests") +############################################## + test_that("Sanity checks", { expect_error( PlotTriangles4Categories(data = 1:20), diff --git a/tests/testthat/test-PlotWeeklyClim.R b/tests/testthat/test-PlotWeeklyClim.R index e7886fbc6836b809ca039dc577a37a65fd42037b..19aae749329b91b207050448928ff7668558bb37 100644 --- a/tests/testthat/test-PlotWeeklyClim.R +++ b/tests/testthat/test-PlotWeeklyClim.R @@ -1,5 +1,3 @@ -context("CSTools::PlotWeeklyClim tests") - ############################################## # dat1 diff --git a/tests/testthat/test-as.s2dv_cube.R b/tests/testthat/test-as.s2dv_cube.R index 5d6303d583b53f61435a57d57917cc8eccbbf92a..f2343080560a11e72ab0f643b7c8df2d9dd33f63 100644 --- a/tests/testthat/test-as.s2dv_cube.R +++ b/tests/testthat/test-as.s2dv_cube.R @@ -1,5 +1,3 @@ -context("CSTools::as.s2dv_cube tests") - ############################################## library(startR) library(s2dv) @@ -53,6 +51,18 @@ test_that("2. Tests from Load()", { attributes(res1$coords$ftime), list(indices = TRUE) ) + expect_equal( + dim(res1$coords$lat), + NULL + ) + expect_equal( + dim(res1$coords$lon), + NULL + ) + expect_equal( + length(res1$coords$lat), + 6 + ) # Dates expect_equal( dim(res1$attrs$Dates), @@ -94,6 +104,14 @@ test_that("3. Tests from Load()", { unlist(res2$coords)[1:4], c(dataset = "ERA5", member = "1", sdate = "20180301", ftime = "1") ) + expect_equal( + dim(res2$coords$ftime), + NULL + ) + expect_equal( + length(res2$coords$lat), + 3 + ) # Dates expect_equal( dim(res2$attrs$Dates), @@ -176,6 +194,18 @@ test_that("5. Tests from Start()", { names(res4$coords), c("dat", "var", "sdate", "ensemble", "time", "latitude", "longitude") ) + expect_equal( + dim(res4$coords$dat), + NULL + ) + expect_equal( + dim(res4$coords$latitude), + NULL + ) + expect_equal( + length(res4$coords$latitude), + 10 + ) # Dates expect_equal( dim(res4$attrs$Dates), @@ -235,6 +265,10 @@ test_that("6. Tests from Start()", { names(res5$coords), c('dat', 'var', 'latitude', 'longitude', 'member', 'time', 'syear', 'sweek', 'sday') ) + expect_equal( + dim(res5$coords$longitude), + NULL + ) # Dates expect_equal( dim(res5$attrs$Dates), diff --git a/tests/testthat/test-s2dv_cube.R b/tests/testthat/test-s2dv_cube.R index 5737486ce4637670d829241c1a19a2e6114d414e..1088b4ce134d483432f6454ea34547ed0dc003ac 100644 --- a/tests/testthat/test-s2dv_cube.R +++ b/tests/testthat/test-s2dv_cube.R @@ -1,5 +1,3 @@ -context("CSTools::s2dv_cube tests") - ############################################## # dat1 @@ -127,9 +125,32 @@ test_that("2. Output checks", { attributes(object$coords$var), list(indices = TRUE) ) - }) ############################################## - +test_that("3. Output checks", { + dim(coords1$sdate) <- c(sdate = 5) + dim(coords1$var) <- c(var = 1) + suppressWarnings( + object <- s2dv_cube(data = dat1, coords = list(sdate = coords1[[1]]), varName = 'tas', + metadata = list(tas = list(level = '2m')), + Dates = dates1) + ) + expect_equal( + names(object$coords), + c('sdate', 'var') + ) + expect_equal( + dim(object$coords$sdate), + NULL + ) + expect_equal( + dim(object$coords$var), + NULL + ) + expect_equal( + length(object$coords$sdate), + 5 + ) +})