tests/testthat/test-resample.R

test_that("resampling reproduces the measured levels", {
    z <- c(0, 12.5, 25, 50)
    v <- array(0, c(4L, 2L, 2L))
    for (k in seq_along(z)) v[k, , ] <- z[k]           # a field equal to its depth
    f <- vol_field(v, lon = c(0, 1), lat = c(0, 1), depth = z)
    u <- vol_resample(f)
    expect_equal(u$depth, seq(0, 50, by = 12.5))
    # a linear field is reproduced exactly by linear interpolation
    for (k in seq_along(u$depth)) expect_equal(unique(as.numeric(u$values[k, , ])),
                                               u$depth[k])
})

test_that("the default spacing keeps every measured depth", {
    expect_equal(volumr:::default_dz(c(0, 12.5, 25, 37.5, 50, 62.5, 75, 87.5,
                                       100, 125, 150, 175, 200, 225, 250)), 12.5)
    expect_equal(volumr:::default_dz(c(0, 10, 20, 30)), 10)
})

test_that("a spacing that would drop a measured depth is refused", {
    f <- vol_field(array(1, c(3L, 2L, 2L)), c(0, 1), c(0, 1), c(0, 10, 25))
    expect_error(vol_resample(f, dz = 10), "absent from a grid")
    expect_s3_class(vol_resample(f, dz = 10, check = FALSE), "vol_field")
})

test_that("a field that is already uniform survives a round trip", {
    f <- vol_field(array(runif(3 * 2 * 2), c(3L, 2L, 2L)), c(0, 1), c(0, 1),
                   c(0, 10, 20))
    expect_equal(vol_resample(f)$values, f$values)
})

test_that("a depth axis that starts below zero is not extrapolated to zero", {
    # Regression: depths 5, 15, 25 were refused by the default call, and a grid
    # forced from zero extrapolated a level above the first measured depth.
    f <- vol_field(array(rep(c(5, 15, 25), 4), c(3L, 2L, 2L)), c(0, 1), c(0, 1),
                   c(5, 15, 25))
    u <- vol_resample(f)
    expect_equal(u$depth, c(5, 15, 25))
    g <- vol_field(array(rep(c(5, 15, 30), 4), c(3L, 2L, 2L)), c(0, 1), c(0, 1),
                   c(5, 15, 30))
    v <- vol_resample(g)
    expect_equal(v$depth, seq(5, 30, by = 5))
    expect_equal(as.numeric(v$values[, 1, 1]), seq(5, 30, by = 5))
})

test_that("a single level is returned unchanged and bad arguments are refused", {
    f <- vol_field(array(1:4, c(1L, 2L, 2L)), c(0, 1), c(0, 1), 0)
    expect_identical(vol_resample(f), f)
    g <- vol_field(array(1, c(3L, 2L, 2L)), c(0, 1), c(0, 1), c(0, 10, 25))
    expect_error(vol_resample(g, dz = -1), "positive")
    expect_error(vol_resample(g, zmax = -5), "no shallower")
})

test_that("a measured level keeps its value where the next level has none", {
    # Regression: the first level was computed from the interval to the second,
    # so a column that ends above the second measured depth lost its surface
    # value, and the next interpolated level must stay empty there.
    v <- array(NA_real_, c(3L, 1L, 2L))
    v[, 1, 1] <- c(4, NA, NA)          # a column that ends above 12.5 m
    v[, 1, 2] <- c(1, 2, 3)
    f <- vol_field(v, lon = c(0, 1), lat = 0, depth = c(0, 12.5, 37.5))
    u <- vol_resample(f)
    expect_equal(u$depth, c(0, 12.5, 25, 37.5))
    expect_identical(u$values[1, 1, 1], 4)
    expect_true(all(is.na(u$values[2:4, 1, 1])))
    expect_identical(u$values[c(1, 2, 4), 1, 2], c(1, 2, 3))
    expect_equal(u$values[3, 1, 2], 2.5)
})

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.