---
title: "2. Creation and coercion"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{2. create a cubble object}
  %\VignetteEngine{knitr::rmarkdown}
   %\usepackage[utf8]{inputenc}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  warning = FALSE,
  message = FALSE
)

library(cubble)
```

This article demonstrates how to create a cubble from various types of data. We will create a cubble from:  

  - separate spatial and temporal tables
  - a `tibble` object
  - NetCDF data
  - an `stars` object
  - an `sftime` object 

# Create from separate spatial and temporal tables

In many cases, spatio-temporal data arrive in separate tables for analysis. For example, in climate data, analysts may initially receive station data containing geographic location information, recorded variables and their recording periods. They can then query the temporal variables using the stations of interest to obtain the relevant temporal data. Alternatively, analyses may begin as purely spatial or temporal, and analysts may obtain additional temporal or spatial data to expand the result to spatio-temporal.

The function `make_cubble()` composes a cubble object from a spatial table (`spatial`) and a temporal table (`temporal`), along with the three attributes `key`, `index`, and `coords` introduced in [1. The cubble class](cb1class.html). The following code creates the nested `cubble`: 

```{r}
make_cubble(spatial = stations, temporal = meteo,
            key = id, index = date, coords = c(long, lat))
```

The `coords` argument can be safely omitted if the spatial data is an sf object (e.g. `stations_sf`) . Similarly, if the temporal object is a tsibble (i.e. `meteo_ts`), you don't need to specify the `key` and `index` arguments. The class attributes from sf and tsibble will be carried over to the nested and long cubble:

```{r echo = TRUE}
(res <- make_cubble(spatial = stations_sf, temporal = meteo_ts))
class(res)
class(res$ts[[1]])
```
  
The vignette [3. Compatibility with tsibble and sf](cb3tsibblesf.html) will introduce more on the cubble's compatibility with tsibble and sf. 

# Coerce from foreign objects

## The `tibble` objects

The dataset `climate_flat` combines the spatial data, `stations`, with the temporal data, `meteo`, into a single tibble object. It can be coerced into a cubble using:

```{r}
climate_flat |> as_cubble(key = id, index = date, coords = c(long, lat))
```

## The NetCDF data

In `R`, there are several packages available for wrangling NetCDF data, including `ncdf4`, `RNetCDF`, and `tidync`. The code below converts a NetCDF object of class ncdf4 into a cubble object:

```{r echo = TRUE}
path <- system.file("ncdf/era5-pressure.nc", package = "cubble")
raw <- ncdf4::nc_open(path)
as_cubble(raw)
```

Sometimes, analysts may choose to read only a subset of the NetCDF data. In such cases, the `vars`, `long_range` and `lat_range` arguments can be used to subset the data based on the variable and the grid resolution:

```{r echo = TRUE, eval = FALSE}
as_cubble(raw, vars = "q",
          long_range = seq(-180, 180, 1), lat_range = seq(-90, 90, 1))
```

## The `stars` objects 
  
```{r}
tif <- system.file("tif/L7_ETMs.tif", package = "stars")
x <- stars::read_stars(tif)
as_cubble(x, index = band)
```

When the `dimensions` object is too complex for the `cubble` package to handle, a warning message will be generated.

## The `sftime` objects 

```{r}
dt <- climate_flat |> 
  sf::st_as_sf(coords = c("long", "lat"), crs = sf::st_crs("OGC:CRS84")) |> 
  sftime::st_as_sftime()
dt |> as_cubble(key = id, index = date)
```

