---
title: "General Usage"
author: "Steffi LaZerte"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{weathercan}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(cache = FALSE)
options(tibble.max_extra_cols = 0, width = 90)
```


```{r, message = FALSE}
library(dplyr)
library(ggplot2)
library(weathercan)
```


## Stations

`weathercan` includes a data frame called `stations` which lists available stations and their details (including `station_id`.

```{r}
head(stations)
```

You can look through this data frame directly, or you can use the `stations_search` function:

```{r}
stations_search("Kamloops")
```

You can narrow down your search by specifying time intervals (options are "hour", "day", or "month"):

```{r}
stations_search("Kamloops", interval = "hour")
```

You can specify more than one interval:

```{r}
stations_search("Kamloops", interval = c("hour", "month"))
```


You can also search by proximity. These results include a new column `distance` specifying the distance in km from the coordinates:

```{r}
stations_search(coords = c(50.667492, -120.329049), dist = 20, interval = "hour")
```

We can also perform more complex searches using `filter()` function from the `dplyr` package:
```{r search_crit}
BCstations <- stations %>%
  filter(prov %in% c("BC")) %>%
  filter(interval == "hour") %>%
  filter(lat > 49 & lat < 49.5) %>%
  filter(lon > -119 & lon < -116) %>%
  filter(start <= 2002) %>%
  filter(end >= 2016)
BCstations

## weather_dl() accepts numbers so we can create a vector to input into weather:
stn_vector <- BCstations$station_id 
stn_vector
```

Searching a bleeding edge updated list of stations:

```{r, eval = FALSE}
s <- stations_dl() # Download complete stations list
stations_search("Saskatoon", stn = s) # Specify the new stations list to search
```


## Weather

Once you have your `station_id`(s) you can download weather data:

```{r}
kam <- weather_dl(station_ids = 51423, start = "2016-01-01", end = "2016-02-15")
                    
kam
```

You can also download data from multiple stations at once:

```{r}
kam.pg <- weather_dl(station_ids = c(48248, 51423), start = "2016-01-01", end = "2016-02-15")
                    
kam.pg
```


And plot it:

```{r, fig.height=6, fig.width=12}
ggplot(data = kam.pg, aes(x = time, y = temp, group = station_name, colour = station_name)) +
  theme(legend.position = "top") +
  geom_line() +
  theme_minimal()
```

Or you can use the vector created above: 

```{r vector_input}
stn_vec_df <- weather_dl(station_ids = stn_vector, start = "2016-01-01", end = "2016-02-15")

stn_vec_df
```

For more information on the data flags, see the [Flags vignette](flags.html), for more information on units and terms, see the [Terms and Units vignette](glossary.html).
