## ----include = FALSE----------------------------------------------------------
  knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)

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

# get an empty store to add to later
mem_store <- MemoryStore$new()

class(mem_store)

# create a store in line when creating an empty array
demo_array <- zarr_create(c(1,2,3), store = NA)

demo_array_store <- demo_array$get_store()

class(demo_array_store)

demo_array_store$listdir()

# or create a store when creating a group to contain arrays
# notice that passing a path creates a directory store
store_path <- file.path(tempdir(), "demo.zarr")

demo_group <- zarr_create_group(store = store_path)

demo_group_store <- demo_group$get_store()

basename(demo_group_store$root)

class(demo_group_store)

demo_group_store$listdir()


## -----------------------------------------------------------------------------

demo_mem_store_group <- zarr_create_group(mem_store)

demo_mem_store_group$get_attrs()$key

demo_mem_store_group$get_attrs()$set_item("this is", "an attribute")

demo_mem_store_group$get_attrs()$to_list()

demo_mem_store_group$get_attrs()$del_item("this is")

demo_mem_store_group$get_attrs()$to_list()


## -----------------------------------------------------------------------------

demo_array_store$listdir()

demo_array$get_attrs()$set_item("this is", "array metadata")

demo_array$get_attrs()$to_list()

# notice that when we added the item, our demo array store got a .zattrs
demo_array_store$listdir()


## -----------------------------------------------------------------------------

zarr_volcano <- zarr_create_array(volcano, # the R array classic
                                  shape = dim(volcano),
                                  store = demo_group_store, # the store we want the array in
                                  path = "volcano") # the path we want the array stored in

demo_group_store$listdir()

zarr_volcano$get_shape()

all.equal(zarr_volcano$as.array(), 
          volcano) 


## -----------------------------------------------------------------------------
sub_zarr_volcano <- zarr_volcano$get_item(list(slice(1, 10), slice(1, 20)))

all.equal(sub_zarr_volcano$as.array(), 
          volcano[1:10, 1:20]) 

sub_zarr_volcano <- zarr_volcano[1:10, 1:20]

class(sub_zarr_volcano)

sub_zarr_volcano$shape

all.equal(sub_zarr_volcano$as.array(), 
          volcano[1:10, 1:20]) 


## -----------------------------------------------------------------------------

# this woll work once implemented?
# zarr_volcano[1:10, 1:20] <- zarr_volcano[1:10, 1:20] * 10

zarr_volcano$set_item(list(slice(1, 10), slice(1, 20)), 
                      zarr_volcano[1:10, 1:20]$as.array() * 10)


## -----------------------------------------------------------------------------

sub_zarr_volcano <- zarr_volcano$get_item(list(1, "..."))

sub_zarr_volcano$shape

sub_zarr_volcano <- zarr_volcano$get_item(list(":", 1))

sub_zarr_volcano$shape

sub_zarr_volcano <- zarr_volcano$get_item(list("..."))

sub_zarr_volcano$shape

sub_zarr_volcano <- zarr_volcano$get_item(list(slice(1, 20, 2), slice(1, 10, 1)))

sub_zarr_volcano$shape


