## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4.2, dpi = 110, out.width = "100%")
library(volumr)

## ----field--------------------------------------------------------------------
data(med_mhw)
med_mhw

## ----summary------------------------------------------------------------------
s <- summary(med_mhw)
head(s$levels, 4)

## ----coverage, fig.height = 3.6-----------------------------------------------
plot(med_mhw, type = "coverage")

## ----resample-----------------------------------------------------------------
u <- vol_resample(med_mhw)
u$depth

## ----encode-------------------------------------------------------------------
e <- vol_encode(u)
e

## ----encode-plot, fig.height = 3.6--------------------------------------------
plot(e)

## ----compare------------------------------------------------------------------
lin <- vol_encode(u, transfer = "linear")
c(sqrt = e$transfer$rms_error, linear = lin$transfer$rms_error)

## ----payload------------------------------------------------------------------
dir <- file.path(tempdir(), "med_payload")
p <- vol_payload(med_mhw, dir, display = list(vmin = 15L, vmax = 60L,
                                              cmap = "matter", gamma = 1),
                 title = "Mediterranean marine heatwaves",
                 subtitle = "Cumulative intensity, 0 to 250 m.")
p

## ----payload-size, fig.height = 3.6-------------------------------------------
plot(p, type = "size")

## ----decoded, fig.height = 4.2------------------------------------------------
plot(p, type = "level", depth = 50)

## ----standalone---------------------------------------------------------------
page <- vol_standalone(p, path = file.path(tempdir(), "med_volume.html"))
sz <- c(payload = sum(file.size(file.path(p$dir, p$files))), page = file.size(page))
sz

## ----native, eval = FALSE-----------------------------------------------------
# vol_standalone(vol_example_payload(), path = "med.html")

## ----widget, eval = requireNamespace("htmlwidgets", quietly = TRUE)-----------
small <- vol_payload(med_mhw, file.path(tempdir(), "small"), step = 2L,
                     display = list(vmin = 15L, vmax = 60L, cmap = "matter",
                                    gamma = 1))
vol_widget(small, height = 420,
           options = list(slab = TRUE, slabT = 25, slabZ = 50, od = 9, ow = 0,
                          elev = 22, azim = -55))

## ----digests------------------------------------------------------------------
p$manifest$slices[[1]][c("file", "depth", "sha256")]
substr(med_mhw$source$sha256[1:2], 1, 16)

## ----reproducible-------------------------------------------------------------
a <- vol_payload(med_mhw, tempfile("a"), created = "2026-01-01T00:00:00+00:00")
b <- vol_payload(med_mhw, tempfile("b"), created = "2026-01-01T00:00:00+00:00")
identical(vapply(a$manifest$slices, function(s) s$sha256, ""),
          vapply(b$manifest$slices, function(s) s$sha256, ""))

## ----own, eval = FALSE--------------------------------------------------------
# f <- vol_field(values, lon = lon, lat = lat, depth = depth,
#                name = "temperature", unit = "degC")
# vol_standalone(vol_payload(f, "payload"), path = "temperature.html")

