# Basics

## Load the package

    library(pizzarr)

## Create an empty ZarrArray

    (a <- array(data=1:20, dim=c(2, 10)))
    #>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    #> [1,]    1    3    5    7    9   11   13   15   17    19
    #> [2,]    2    4    6    8   10   12   14   16   18    20

    z <- zarr_create_empty(shape=dim(a), dtype="<f4")

## Create a ZarrArray based on a base R array

    z <- zarr_create_array(data = a, shape=dim(a), dtype="<f4", fill_value=NA)

    # R-like one-based slicing
    s1 <- z$get_item(list(slice(1, 2), slice(1, 5)))

    print(s1$data)
    #>      [,1] [,2] [,3] [,4] [,5]
    #> [1,]    1    3    5    7    9
    #> [2,]    2    4    6    8   10

    # Python-like zero-based slicing
    s2 <- z$get_item(list(zb_slice(0, 2), zb_slice(0, 5)))

    print(s2$data)
    #>      [,1] [,2] [,3] [,4] [,5]
    #> [1,]    1    3    5    7    9
    #> [2,]    2    4    6    8   10

## Create nested ZarrGroups and ZarrArrays

    g1 <- zarr_create_group()
    g2 <- g1$create_group("foo")
    g3 <- g2$create_group("bar")

    data <- array(data=1:10, dim=c(2, 5))
    a <- g3$create_dataset("baz", data=data, shape=dim(data))

    print(a$get_name())
    #> [1] "/foo/bar/baz"

    print(a$get_shape())
    #> [1] 2 5

## List arrays in a zarr root group

    root <- pizzarr_sample("fixtures/v2/data.zarr")

    z <- zarr_open(root)

    class(z)
    #> [1] "ZarrGroup" "R6"

    store <- z$get_store()

    class(store)
    #> [1] "DirectoryStore" "Store"          "R6"

    print(store$listdir())
    #>  [1] ".zgroup"                       "1d.chunked.i2"
    #>  [3] "1d.chunked.ragged.i2"          "1d.contiguous.S7"
    #>  [5] "1d.contiguous.U13.be"          "1d.contiguous.U13.le"
    #>  [7] "1d.contiguous.U7"              "1d.contiguous.b1"
    #>  [9] "1d.contiguous.blosc.i2"        "1d.contiguous.blosc.vlen-utf8"
    #> [11] "1d.contiguous.f4.be"           "1d.contiguous.f4.le"
    #> [13] "1d.contiguous.f8"              "1d.contiguous.i4"
    #> [15] "1d.contiguous.lz4.i2"          "1d.contiguous.raw.i2"
    #> [17] "1d.contiguous.raw.vlen-utf8"   "1d.contiguous.u1"
    #> [19] "1d.contiguous.zlib.i2"         "1d.contiguous.zstd.i2"
    #> [21] "2d.chunked.U7"                 "2d.chunked.blosc.vlen-utf8"
    #> [23] "2d.chunked.i2"                 "2d.chunked.ragged.i2"
    #> [25] "2d.chunked.raw.vlen-utf8"      "2d.contiguous.i2"
    #> [27] "3d.chunked.i2"                 "3d.chunked.mixed.i2.C"
    #> [29] "3d.chunked.mixed.i2.F"         "3d.contiguous.i2"

## Open a ZarrArray from a DirectoryStore (convenience)

    root <- pizzarr_sample("fixtures/v2/data.zarr")

    g <- zarr_open_group(root)
    a <- g$get_item("1d.contiguous.lz4.i2")
    #> Warning in initialize(...): Lz4Codec is deprecated because the 'qs' package was
    #> archived on CRAN. Use BloscCodec$new(cname = 'lz4') for LZ4 compression
    #> instead. Lz4Codec will be removed in a future version of pizzarr.

    print(a$get_shape())
    #> [1] 4

## Open a ZarrArray from a DirectoryStore

    root <- pizzarr_sample("fixtures/v2/data.zarr")

    store <- DirectoryStore$new(root)
    g <- ZarrGroup$new(store)
    a <- g$get_item("1d.contiguous.lz4.i2")
    #> Warning in initialize(...): Lz4Codec is deprecated because the 'qs' package was
    #> archived on CRAN. Use BloscCodec$new(cname = 'lz4') for LZ4 compression
    #> instead. Lz4Codec will be removed in a future version of pizzarr.

    print(a$get_shape())
    #> [1] 4

## Get attributes from a root group and ZarrArray

    root <- pizzarr_sample("dog.ome.zarr")

    z <- zarr_open(root)

    class(z)
    #> [1] "ZarrGroup" "R6"

    attrs <- z$get_attrs()$to_list()

    names(attrs)
    #> [1] "multiscales" "omero"

    lengths(attrs$omero)
    #> channels     name    rdefs  version
    #>        3        1        0        1

    z$get_store()$listdir()
    #> [1] ".zattrs" ".zgroup" "0"       "1"       "2"       "3"       "4"

    a <- z$get_item("4")

    class(a)
    #> [1] "ZarrArray" "R6"

    a$get_attrs()$to_list()
    #> named list()