```{r}
library(rstac)
library(gdalcubes)
library(stars)
library(tmap)
library(dplyr)
library(gifski)
```

```{r}
library(spData)
box_ca <- spData::us_states |> filter(NAME == "California") |> st_bbox()
```

```{r}
box <- c(xmin=-122.51, ymin=37.71, xmax=-122.36, ymax=37.81) 

items <-
  stac("https://planetarycomputer.microsoft.com/api/stac/v1") |>
  stac_search(collections = "io-biodiversity",
              bbox = c(box_ca),
              limit = 100) |>
  post_request() |> 
  items_sign(sign_planetary_computer())

items$features
```

```{r}
col <- stac_image_collection(items$features, asset_names = c("data"))

cube <- cube_view(srs ="EPSG:4326",
                  extent = list(t0 = "2017-01-01", t1 = "2020-12-31",
                                left = box_ca[1], right = box_ca[3],
                                top = box_ca[4], bottom = box_ca[2]),
                  dx = 0.0001, dy = 0.0001, dt = "P1Y") # dt = temporal slice

data <-  raster_cube(col, cube)
```

```{r}
data |> gdalcubes::slice_time("2017-01-01") |> plot()
```