---
title: "Getting started with cubble"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{cubble}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup, echo = FALSE}
library(dplyr)
library(cubble)
library(ggplot2)
```


Global Historical Climatology Network (GHCN) provides daily climate measures from stations across the world. The dataset `tmax_hist` extracts the maximum temperature for `r nrow(tmax_hist)` Australian stations in the period 1970 - 1975 and 2016 - 2020 from the full Australian historical records starting from year 1969. `tmax_hist` is already in a cubble, with `id` as the key, `yearmonth` as the index, and `c(longitude, latitude)` as the coordinates. This vignette shows the basic manipulation of cubble in an example to compares the maximum temperature in these two periods for weather stations in Victoria, Australia.

# Spatial manipulation 

Victoria stations can be subset by the station number: Australia GHCN station number starts with "ASN00" and followed by the [Bureau of Meteorology (BOM) station number](http://www.bom.gov.au/climate/cdo/about/site-num.shtml), where the 2nd and 3rd digit (7th and 8th in the GHCN number) define the state of the station. Victoria stations start from 76 to 90 and filtering Victoria stations is an operation in the spatial dimension. Hence we want to use the nested form: 

```{r}
tmax <- tmax_hist %>% 
  filter(between(as.numeric(stringr::str_sub(id, 7, 8)), 76, 90))

tmax
```

# Temporal manipulation 

We would also like to summarise the daily recording into monthly to smooth the maximum temperature and since it is also a time dimension operation, we can keep using the long form:  

```{r}
tmax <-tmax %>% 
    face_temporal() %>% 
    mutate(
        month = lubridate::month(yearmonth),
        group = as.factor(ifelse(lubridate::year(yearmonth) > 2015, "2016 ~ 2020","1971 ~ 1975"))) %>%
  group_by(month, group)%>%  
  summarise(tmax = mean(tmax, na.rm = TRUE))

tmax
```

# Back to spatial

A data quality issue with the `rnoaa` data is that while it records the first and last year recorded of each series without the period of missingness. For example, station `ASN00085279` starts it first record in 1943, pauses for a period from 1946 to 1982, and then resumes it recording till today. We would like to remove stations like this by examining whether the summarised time series has a total of 24 months. This is a station-wise operation and `face_spatial()` can be used to convert the cubble back to the nested form: 

```{r}
tmax <- tmax %>%  face_spatial() %>%  filter(nrow(ts) == 24)
tmax
```

# The final step

In the glyph map we are about to create, the four variables (major and minor axis for x and y) need to be in the same table. In cubble, you can move time invariant variable into the long form with `unfold()`:

```{r}
tmax <- tmax %>%  face_temporal() %>%  unfold(latitude, longitude)
tmax
```

Now you have seen the basic wrangling with cubble, it is time to make some plots! 
The vignette [Making glyph map](glyph.html) will show you how to plot time series in the space using a glyph map. 

