diff --git a/.Rbuildignore b/.Rbuildignore index 19a1af65ac5ae3e96e68397e9d01cc76b0c9e8ac..f6430a4587524c56d276b040b1318e3561853bcf 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,4 +9,5 @@ README\.md$ \..*\.RData$ vignettes .gitlab-ci.yml -^tests$ +# unit tests should be ignored when building the package for CRAN +#^tests$ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cbe09a22ac03d801b1ae1a2c0f07cc6c4b59f8ef..fe0e593fa26ac3cc245f147f4f57ce743aa8f863 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,4 +7,4 @@ build: # - module load CDO - R CMD build --resave-data . - R CMD check --as-cran --no-manual --run-donttest s2dv_*.tar.gz -# - R -e 'covr::package_coverage()' + - R -e 'covr::package_coverage()' diff --git a/R/MeanDims.R b/R/MeanDims.R index 7d3cb445487f60aefb8020f2cb598c28d2777fd2..8d5d77b045876324d3c81c27311ee1809c0b4504 100644 --- a/R/MeanDims.R +++ b/R/MeanDims.R @@ -54,17 +54,22 @@ MeanDims <- function(data, dims, na.rm = FALSE) { if (!is.logical(na.rm) | length(na.rm) > 1) { stop("Parameter 'na.rm' must be one logical value.") } - ############################### # Calculate MeanDims - if (length(dims) == length(dim(data))) { + dim_data <- dim(data) + + if (length(dims) == length(dim_data)) { data <- mean(data, na.rm = na.rm) } else { if (is.character(dims)) { - dims <- which(names(dim(data)) %in% dims) + dims <- which(names(dim_data) %in% dims) } - pos <- (1:length(dim(data)))[-dims] + pos <- (1:length(dim_data))[-dims] data <- apply(data, pos, mean, na.rm = na.rm) + + if (is.null(dim(data))) { + data <- array(data, dim = dim_data[-dims]) + } } return(data) } diff --git a/R/RatioSDRMS.R b/R/RatioSDRMS.R index 4d833d7ff12e05085c9bea899e8950ada803b4fd..b74fbb494798eba739d07396053275e32b504486 100644 --- a/R/RatioSDRMS.R +++ b/R/RatioSDRMS.R @@ -183,7 +183,7 @@ RatioSDRMS <- function(exp, obs, dat_dim = 'dataset', memb_dim = 'member', if (pval) { F <- (enosd[jexp] * std[jexp]^2 / (enosd[jexp] - 1)) / (enorms * rms^2 / (enorms - 1)) - if (!is.na(F) & !is.na(enosd) & !is.na(enorms) & enosd > 2 && enorms > 2) { + if (!is.na(F) & !is.na(enosd[jexp]) & !is.na(enorms) & enosd > 2 & enorms > 2) { p.val[jexp, jobs] <- 1 - pf(F, enosd[jexp] - 1, enorms - 1) } else { ratiosdrms[jexp, jobs] <- NA diff --git a/R/Season.R b/R/Season.R index d56aa1637fd77204db76061492a7d14fea9e8a87..086dc529471fb69a385f9c9b21eefc233c29b3c1 100644 --- a/R/Season.R +++ b/R/Season.R @@ -136,6 +136,9 @@ Season <- function(data, time_dim = 'ftime', monini, moninf, monsup, res <- apply(data, c(1:length(dim(data)))[-time_dim_ind], .Season, monini = monini, moninf = moninf, monsup = monsup, method = method, na.rm = na.rm) + if (is.null(dim(res))) { + res <- array(res, dim = dim(data)[-time_dim_ind]) + } if (length(dim(res)) < length(dim(data))) { res <- InsertDim(res, posdim = 1, lendim = 1, name = time_dim) } else { diff --git a/R/Utils.R b/R/Utils.R index e0e1f91fddd2bcd61460149d49060ec65ab4d018..76ef63a63d1510cadf6974f44185a74823e69f7e 100644 --- a/R/Utils.R +++ b/R/Utils.R @@ -1671,9 +1671,9 @@ if (type == 'dcpp') { fyear_dim <- 'fyear' - data <- s2dv::Season(data = data, time_dim = fmonth_dim, - monini = monini, moninf = 1, monsup = 12, - method = mean, na.rm = na.rm) + data <- Season(data = data, time_dim = fmonth_dim, + monini = monini, moninf = 1, monsup = 12, + method = mean, na.rm = na.rm) names(dim(data))[which(names(dim(data))==fmonth_dim)] <- fyear_dim if (identical(indices_for_clim, FALSE)) { ## data is already anomalies diff --git a/tests/testthat/test-AMV.R b/tests/testthat/test-AMV.R index 562605b6cf04efc348c27456fdbbc63250306b7b..9adfaefa51211858c4b475b5446b1ea4ef0298c2 100644 --- a/tests/testthat/test-AMV.R +++ b/tests/testthat/test-AMV.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - AMV(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - AMV(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,7 +169,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^17, - -3.549383, + -4.884981, tolerance = 0.00001 ) @@ -187,7 +177,7 @@ test_that("3. Output checks: dat2", { indices_for_clim = 2:3) expect_equal( mean(res2_2)*10^16, - 8.616545, + 8.837375, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - -4.437162, + -1.554312, tolerance = 0.00001 ) @@ -233,7 +223,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - 2.220446, + -1.554312, tolerance = 0.00001 ) diff --git a/tests/testthat/test-Ano_CrossValid.R b/tests/testthat/test-Ano_CrossValid.R index 60bff8c70a1752c795664cc2acebed6dc9c81413..b66fc5fbf54113b71826e7adf79d014890c003e0 100644 --- a/tests/testthat/test-Ano_CrossValid.R +++ b/tests/testthat/test-Ano_CrossValid.R @@ -79,8 +79,7 @@ test_that("1. Input checks", { # exp and obs (2) expect_error( Ano_CrossValid(exp1, array(1:20, dim = c(dataset = 1, member = 2, sdate = 4, ftime = 2))), - paste0("Parameter 'exp' and 'obs' must have same length of ", - "all dimension expect 'dat_dim'.") + "Parameter 'exp' and 'obs' must have the same length of all dimensions except 'dat_dim'." ) diff --git a/tests/testthat/test-GMST.R b/tests/testthat/test-GMST.R index d6e3a4f882f02d38fa5d22164b7b7fc66398191b..01ab792c26a2145d7794659a81922d4de933ef39 100644 --- a/tests/testthat/test-GMST.R +++ b/tests/testthat/test-GMST.R @@ -171,18 +171,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data_tas' or 'data_tos' dimension." ) - expect_error( - GMST(data_tas = dat_tas2, data_tos = dat_tos2, data_lats = lat, data_lons = lon, - mask_sea_land = mask_sea_land, sea_value = sea_value, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - GMST(data_tas = dat_tas2, data_tos = dat_tos2, data_lats = lat, data_lons = lon, - mask_sea_land = mask_sea_land, sea_value = sea_value, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data_tas' or 'data_tos' dimension." - ) }) @@ -224,7 +212,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( range(res2_1), - c(-2.028458, 2.028458), + c(-2, 2), tolerance = 0.00001 ) @@ -241,7 +229,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^16, - 2.842084, + 0, tolerance = 0.00001 ) @@ -281,7 +269,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^16, - -8.52686, + -5.684949, tolerance = 0.00001 ) diff --git a/tests/testthat/test-GSAT.R b/tests/testthat/test-GSAT.R index bd53e8ab062e232098cdcdb0d0db4e33dd912919..2d7d7e00c20f20d16288208e9ce72f12e2fbe454 100644 --- a/tests/testthat/test-GSAT.R +++ b/tests/testthat/test-GSAT.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - GSAT(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - GSAT(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,7 +169,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^16, - 2.841867, + 2.841954, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^15, - 1.136842, + 0.2841954, tolerance = 0.00001 ) diff --git a/tests/testthat/test-Persistence.R b/tests/testthat/test-Persistence.R index 2ce2dec47fe1872a183c46338b3fbcf946bef5b5..4eabe836734c3ecf8cd636facb8a9e4b69ffb6ff 100644 --- a/tests/testthat/test-Persistence.R +++ b/tests/testthat/test-Persistence.R @@ -8,7 +8,7 @@ dim(dat1) <- c(member = 1, time = 70, lat = 6, lon = 7) dates1 <- seq(1920, 1989, 1) start1 <- 1961 end1 <- 1990 -res <- Persistence(obs1, dates = dates1, start = 1961, end = 1990, ft_start = 1, +res <- Persistence(dat1, dates = dates1, start = 1961, end = 1990, ft_start = 1, nmemb = 40) #dat2: day diff --git a/tests/testthat/test-SPOD.R b/tests/testthat/test-SPOD.R index 6d23a59745158a7d3c36807244cf96ceb453608c..8b56c721dc618409e5c14084262eb71aa07566af 100644 --- a/tests/testthat/test-SPOD.R +++ b/tests/testthat/test-SPOD.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,15 +169,15 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^16, - 2.754588, + 2.087219, tolerance = 0.00001 ) res2_2 <- SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', indices_for_clim = 2:3) expect_equal( - mean(res2_2)*10^16, - 2.645063, + mean(res2_2)*10^18, + 8.881784, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - 5.3553, + -12.4345, tolerance = 0.00001 ) @@ -233,7 +223,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - 7.549517, + -8.881784, tolerance = 0.00001 ) diff --git a/tests/testthat/test-TPI.R b/tests/testthat/test-TPI.R index 3b584c6f5a3eaf5d038f9f20a04c00fcdfe2e39f..b663c40bd1e36b4792da5adb4fa95e7ea1aae7a8 100644 --- a/tests/testthat/test-TPI.R +++ b/tests/testthat/test-TPI.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - TPI(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - TPI(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -184,7 +174,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^17, - -2.220099, + -2.664535, tolerance = 0.00001 ) @@ -192,7 +182,7 @@ test_that("3. Output checks: dat2", { indices_for_clim = 2:3) expect_equal( mean(res2_2)*10^16, - 3.840678, + 4.618528, tolerance = 0.00001 ) @@ -200,7 +190,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - -2.288135, + 0.8881784, tolerance = 0.00001 ) @@ -238,7 +228,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - -1.360023, + 0, tolerance = 0.00001 )