## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)

## -----------------------------------------------------------------------------
library(pizzarr)

## -----------------------------------------------------------------------------
path <- file.path(tempdir(), "example.zarr")

# Create a persistent array backed by a DirectoryStore
z <- zarr_open_array(
  store = path, mode = "w",
  shape = c(5, 10), chunks = c(5, 5), dtype = "<f4"
)

# Write data
z$set_item("...", array(1:50, dim = c(5, 10)))

z$get_shape()

## -----------------------------------------------------------------------------
z2 <- zarr_open_array(store = path, mode = "r")

z2$get_shape()

z2$get_item("...")$data

## -----------------------------------------------------------------------------
save_path <- file.path(tempdir(), "saved.zarr")

# Save an R array directly
zarr_save_array(save_path, zarr_create_array(
  data = volcano, shape = dim(volcano), dtype = "<f8"
))

# Reopen
z3 <- zarr_open_array(save_path, mode = "r")

all.equal(z3$as.array(), volcano)

## -----------------------------------------------------------------------------
z_zstd <- zarr_create(
  shape = c(100, 100), dtype = "<f4",
  compressor = ZstdCodec$new(level = 3)
)

z_zstd$get_compressor()$get_config()

## -----------------------------------------------------------------------------
z_gzip <- zarr_create(
  shape = c(100, 100), dtype = "<f4",
  compressor = GzipCodec$new(level = 5)
)

z_gzip$get_compressor()$get_config()

## -----------------------------------------------------------------------------
z_blosc <- zarr_create(
  shape = c(100, 100), dtype = "<f4",
  compressor = BloscCodec$new(cname = "lz4", clevel = 5, shuffle = TRUE)
)

z_blosc$get_compressor()$get_config()

## -----------------------------------------------------------------------------
z_none <- zarr_create(
  shape = c(100, 100), dtype = "<f4",
  compressor = NA
)

is.na(z_none$get_compressor())

## -----------------------------------------------------------------------------
z <- zarr_create(
  shape = c(5, 10), chunks = c(5, 5),
  dtype = "<i4", fill_value = 0L,
  compressor = "default"
)

z$set_item("...", array(1:50, dim = c(5, 10)))

z$get_shape()

# Grow the array
z$resize(10, 20)

z$get_shape()

# Original data is preserved in the top-left corner
z[1:5, 1:10]$data

# New region is filled with fill_value
z[6:10, 1:5]$data

## -----------------------------------------------------------------------------
z$resize(3, 4)

z$get_shape()

z$get_item("...")$data

## -----------------------------------------------------------------------------
z <- zarr_create(
  shape = c(3, 4), chunks = c(3, 4),
  dtype = "<i4", fill_value = 0L
)

z$set_item("...", array(1:12, dim = c(3, 4)))

z$as.array()

## -----------------------------------------------------------------------------
new_rows <- array(13:20, dim = c(2, 4))

z$append(new_rows)

z$get_shape()

z$as.array()

## -----------------------------------------------------------------------------
new_cols <- array(21:30, dim = c(5, 2))

z$append(new_cols, axis = 2)

z$get_shape()

z$as.array()

## -----------------------------------------------------------------------------
words <- c("alpha", "bravo", "charlie", "delta")

z_str <- zarr_create_array(
  data = array(words, dim = length(words)),
  shape = length(words), dtype = "|O",
  object_codec = VLenUtf8Codec$new()
)

z_str$get_item("...")$data

z_str$get_filters()

## -----------------------------------------------------------------------------
z <- zarr_create_array(
  data = matrix(1:30, nrow = 5, ncol = 6),
  shape = c(5, 6), dtype = "<i4"
)

z$as.array()

## -----------------------------------------------------------------------------
# Select rows 1-3, columns 2-4
z[1:3, 2:4]$data

## -----------------------------------------------------------------------------
z$get_orthogonal_selection(list(c(0L, 2L, 4L), zb_slice(0, 6)))$data

## -----------------------------------------------------------------------------
row_mask <- c(TRUE, FALSE, TRUE, FALSE, TRUE)

z$get_orthogonal_selection(list(row_mask, zb_slice(0, 6)))$data

## -----------------------------------------------------------------------------
oi <- z$get_oindex()

oi$get_item(list(c(0L, 4L), c(1L, 3L, 5L)))$data

## -----------------------------------------------------------------------------
z[seq(1, 5, 2), seq(1, 6, 3)]$data

## -----------------------------------------------------------------------------
# All rows, column 1
z$get_item(list(":", 1))$data

# Row 1, all columns
z$get_item(list(1, "..."))$data

## ----include = FALSE----------------------------------------------------------
unlink(file.path(tempdir(), "example.zarr"), recursive = TRUE)
unlink(file.path(tempdir(), "saved.zarr"), recursive = TRUE)

