## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)
sample_dir <- tools::R_user_dir("pizzarr")
clean <- !dir.exists(sample_dir)

## -----------------------------------------------------------------------------
library(pizzarr)

has_zarrs <- pizzarr:::.pizzarr_env$zarrs_available

## -----------------------------------------------------------------------------
pizzarr_compiled_features()

## -----------------------------------------------------------------------------
pizzarr:::.pizzarr_env$zarrs_available

## -----------------------------------------------------------------------------
pizzarr_upgrade()

## ----eval=has_zarrs-----------------------------------------------------------
# v2_root <- pizzarr_sample("fixtures/v2/data.zarr")
# 
# # Root group
# zarrs_node_exists(v2_root, "")

## ----eval=has_zarrs-----------------------------------------------------------
# # An array within the store
# zarrs_node_exists(v2_root, "1d.contiguous.lz4.i2")

## ----eval=has_zarrs-----------------------------------------------------------
# # A path that does not exist
# zarrs_node_exists(v2_root, "does_not_exist")

## ----eval=has_zarrs-----------------------------------------------------------
# v3_root <- pizzarr_sample("fixtures/v3/data.zarr")
# 
# zarrs_node_exists(v3_root, "")

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(v2_root)
# zarrs_close_store(v2_root)

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(v3_root)

## ----eval=has_zarrs-----------------------------------------------------------
# v2_root <- pizzarr_sample("fixtures/v2/data.zarr")
# zarrs_open_array_metadata(v2_root, "1d.contiguous.raw.i2")

## ----eval=has_zarrs-----------------------------------------------------------
# v3_root <- pizzarr_sample("fixtures/v3/data.zarr")
# zarrs_open_array_metadata(v3_root, "1d.contiguous.gzip.i2")

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(v2_root)
# zarrs_close_store(v3_root)

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_runtime_info()

## ----eval=has_zarrs-----------------------------------------------------------
# # View current settings
# pizzarr_config()
# 
# # Set codec concurrency to 2 parallel operations per read/write
# pizzarr_config(concurrent_target = 2L)
# zarrs_runtime_info()$codec_concurrent_target

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_set_codec_concurrent_target(2L)
# zarrs_runtime_info()$codec_concurrent_target

## ----eval=has_zarrs-----------------------------------------------------------
# d <- tempfile("zarrs_vignette_")
# z <- zarr_create(store = d, shape = c(100L, 50L), chunks = c(10L, 10L),
#                  dtype = "<f8")
# z$set_item("...", array(as.double(seq_len(5000)), dim = c(100, 50)))
# 
# # Re-open and read a subset --- zarrs handles the chunk I/O
# z2 <- zarr_open(store = d)
# result <- z2$get_item(list(slice(1L, 10L), slice(1L, 5L)))
# dim(result$data)

## ----eval=has_zarrs-----------------------------------------------------------
# result <- zarrs_get_subset(d, "", list(c(0L, 10L), c(0L, 5L)), NULL)
# str(result)

## ----eval=has_zarrs-----------------------------------------------------------
# result <- zarrs_get_subset(d, "", list(c(0L, 10L), c(0L, 5L)), 1L)
# length(result$data)

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(d)
# unlink(d, recursive = TRUE)

## ----eval=has_zarrs-----------------------------------------------------------
# # V3 array with gzip compression
# d <- tempfile("zarrs_create_vignette_")
# z <- zarr_create(store = d, shape = c(20L, 10L), chunks = c(10L, 10L),
#                  dtype = "<f8", zarr_format = 3L)
# z

## ----eval=has_zarrs-----------------------------------------------------------
# # Confirm V3 metadata was written
# file.exists(file.path(d, "zarr.json"))

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(d)
# unlink(d, recursive = TRUE)

## ----eval=has_zarrs-----------------------------------------------------------
# d <- tempfile("zarrs_create_direct_")
# dir.create(d)
# 
# meta <- zarrs_create_array(
#   store_url = d,
#   array_path = "",
#   shape = c(100L, 50L),
#   chunks = c(10L, 10L),
#   dtype = "float64",
#   codec_preset = "gzip",
#   fill_value = 0.0,
#   attributes_json = "{}",
#   zarr_format = 3L
# )
# str(meta)

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_set_subset(d, "", list(c(0L, 10L), c(0L, 5L)),
#                  as.double(1:50), NULL)
# result <- zarrs_get_subset(d, "", list(c(0L, 10L), c(0L, 5L)), NULL)
# head(result$data)

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(d)
# unlink(d, recursive = TRUE)

## ----eval=has_zarrs-----------------------------------------------------------
# d <- tempfile("zarrs_write_vignette_")
# z <- zarr_create(store = d, shape = c(20L, 10L), chunks = c(10L, 10L),
#                  dtype = "<f8")
# 
# # set_item dispatches to zarrs when eligible
# z$set_item("...", array(as.double(1:200), dim = c(20, 10)))
# 
# # Read back to confirm
# z2 <- zarr_open(store = d)
# result <- z2$get_item(list(slice(1L, 5L), slice(1L, 3L)))
# result$data

## ----eval=has_zarrs-----------------------------------------------------------
# # Overwrite rows 3-7, columns 1-2
# z$set_item(list(slice(3L, 7L), slice(1L, 2L)),
#            array(rep(-1.0, 10), dim = c(5, 2)))
# 
# result <- z2$get_item(list(slice(1L, 10L), slice(1L, 3L)))
# result$data

## ----eval=has_zarrs-----------------------------------------------------------
# # Write 10 values to the first row (0-based range [0, 1) x [0, 10))
# zarrs_set_subset(d, "", list(c(0L, 1L), c(0L, 10L)),
#                    as.double(101:110), NULL)
# 
# result <- zarrs_get_subset(d, "", list(c(0L, 1L), c(0L, 10L)), NULL)
# result$data

## ----eval=has_zarrs-----------------------------------------------------------
# zarrs_close_store(d)
# unlink(d, recursive = TRUE)

## ----eval=has_zarrs && ("http_sync" %in% pizzarr_compiled_features())---------
# url <- "https://raw.githubusercontent.com/DOI-USGS/rnz/main/inst/extdata/bcsd.zarr"
# 
# z <- zarr_open(store = HttpStore$new(url))
# 
# # zarrs handles the HTTP reads + parallel decompression
# pr <- z$get_item("pr")
# pr

## ----eval=has_zarrs && ("http_sync" %in% pizzarr_compiled_features())---------
# # Read a subset --- zarrs fetches only the chunks that overlap
# result <- pr$get_item(list(slice(1L, 3L), slice(1L, 5L), slice(1L, 5L)))
# dim(result$data)

## ----eval=has_zarrs && ("http_sync" %in% pizzarr_compiled_features())---------
# meta <- zarrs_open_array_metadata(url, "pr")
# str(meta[c("shape", "dtype", "zarr_format")])

## ----eval=has_zarrs && ("http_sync" %in% pizzarr_compiled_features())---------
# # Read a single element (first along each dimension)
# ranges <- lapply(seq_along(meta$shape), function(i) c(0L, 1L))
# result <- zarrs_get_subset(url, "pr", ranges, NULL)
# result$data

## ----eval=has_zarrs && ("http_sync" %in% pizzarr_compiled_features())---------
# zarrs_close_store(url)

## ----eval=has_zarrs-----------------------------------------------------------
# "http_sync" %in% pizzarr_compiled_features()

## ----eval=has_zarrs && ("s3" %in% pizzarr_compiled_features())----------------
# # OME-Zarr bonsai dataset on AWS Open Data (V2, zstd, uint8)
# s3_url <- "s3://ome-zarr-scivis/v0.4/64x0/bonsai.ome.zarr"
# 
# # Read array metadata
# meta <- zarrs_open_array_metadata(s3_url, "scale0/bonsai")
# str(meta[c("shape", "dtype", "zarr_format")])

## ----eval=has_zarrs && ("s3" %in% pizzarr_compiled_features())----------------
# # Read a small subset (first 4x4x4 corner)
# result <- zarrs_get_subset(s3_url, "scale0/bonsai",
#                            list(c(0L, 4L), c(0L, 4L), c(0L, 4L)), NULL)
# str(result)

## ----eval=has_zarrs && ("s3" %in% pizzarr_compiled_features())----------------
# zarrs_close_store(s3_url)

## ----eval=has_zarrs && ("blosc" %in% pizzarr_compiled_features())-------------
# # Pangeo ECCO ocean basins (V2, blosc/lz4, float32)
# gcs_url <- "https://storage.googleapis.com/pangeo-data/ECCO_basins.zarr"
# 
# meta <- zarrs_open_array_metadata(gcs_url, "basin_mask")
# cat("Shape:", paste(meta$shape, collapse = " x "), "\n")
# cat("Dtype:", meta$dtype, "\n")

## ----eval=has_zarrs && ("blosc" %in% pizzarr_compiled_features())-------------
# # Read a single basin mask slice
# result <- zarrs_get_subset(gcs_url, "basin_mask",
#                            list(c(0L, 1L), c(0L, 90L), c(0L, 90L)), NULL)
# cat("Slice dimensions:", paste(result$shape, collapse = " x "), "\n")

## ----eval=has_zarrs && ("blosc" %in% pizzarr_compiled_features())-------------
# zarrs_close_store(gcs_url)

## ----include=FALSE------------------------------------------------------------
if (clean) unlink(sample_dir, recursive = TRUE)

