---
title: "Basics"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Basics}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Load the package

```{r}
library(pizzarr)
```

## Create an empty ZarrArray

```{r}
(a <- array(data=1:20, dim=c(2, 10)))

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

## Create a ZarrArray based on a base R array

```{r}
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)

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

print(s2$data)

```

## Create nested ZarrGroups and ZarrArrays

```{r}
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())

print(a$get_shape())
```

## List arrays in a zarr root group

```{r}
root <- pizzarr_sample("fixtures/v2/data.zarr")

z <- zarr_open(root)

class(z)

store <- z$get_store()

class(store)

print(store$listdir())
```

## Open a ZarrArray from a DirectoryStore (convenience)

```{r}
root <- pizzarr_sample("fixtures/v2/data.zarr")

g <- zarr_open_group(root)
a <- g$get_item("1d.contiguous.lz4.i2")

print(a$get_shape())
```

## Open a ZarrArray from a DirectoryStore

```{r}
root <- pizzarr_sample("fixtures/v2/data.zarr")
    
store <- DirectoryStore$new(root)
g <- ZarrGroup$new(store)
a <- g$get_item("1d.contiguous.lz4.i2")

print(a$get_shape())
```

## Get attributes from a root group and ZarrArray

```{r}
root <- pizzarr_sample("dog.ome.zarr")

z <- zarr_open(root)

class(z)

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

names(attrs)

lengths(attrs$omero)

z$get_store()$listdir()

a <- z$get_item("4")

class(a)

a$get_attrs()$to_list()
```
```{r, include=FALSE}
if(clean) unlink(sample_dir, recursive = TRUE)
```