<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{ncdc vignette}
%\VignetteEncoding{UTF-8}
-->

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

NCDC vignette
======

## About the package

`rnoaa` is an R wrapper for many NOAA data types, including National Climatic Data Center (NCDC).

## Load rnoaa

```{r load}
library('rnoaa')
library('plyr')
```

## Get info on a station by specifying a datasetid, locationid, and stationid

```{r two}
ncdc_stations(datasetid='GHCND', locationid='FIPS:12017', stationid='GHCND:USC00084289')
```

## Search for data and get a data.frame

```{r three}
out <- ncdc(datasetid='NORMAL_DLY', datatypeid='dly-tmax-normal', startdate = '2010-05-01', enddate = '2010-05-10')
out$data
```

## Plot data, super simple, but it's a start

```{r six}
out <- ncdc(datasetid='NORMAL_DLY', stationid='GHCND:USW00014895', datatypeid='dly-tmax-normal', startdate = '2010-01-01', enddate = '2010-12-10', limit = 300)
ncdc_plot(out)
```

## More on plotting

### Example 1

Search for data first, then plot

```{r}
out <- ncdc(datasetid='GHCND', stationid='GHCND:USW00014895', datatypeid='PRCP', startdate = '2010-05-01', enddate = '2010-10-31', limit=500)
```

Default plot

```{r}
ncdc_plot(out)
```

Create 14 day breaks

```{r}
ncdc_plot(out, breaks="14 days")
```

One month breaks

```{r}
ncdc_plot(out, breaks="1 month", dateformat="%d/%m")
```

### Example 2

Search for data

```{r}
out <- ncdc(datasetid='GHCND', stationid='GHCND:USW00014895', datatypeid='PRCP',
            startdate = '2010-05-01', enddate = '2010-10-31', limit=500)
```

Make a plot, with 6 hour breaks, and date format with only hour

```{r}
ncdc_plot(out, breaks = "1 month", dateformat = "%d/%m")
```

## Combine many calls to noaa function

Search for two sets of data

```{r}
out1 <- ncdc(datasetid='GHCND', stationid='GHCND:USW00014895', datatypeid='PRCP', startdate = '2010-03-01', enddate = '2010-05-31', limit=500)

out2 <- ncdc(datasetid='GHCND', stationid='GHCND:USW00014895', datatypeid='PRCP', startdate = '2010-09-01', enddate = '2010-10-31', limit=500)
```

Then combine with a call to `ncdc_combine`

```{r}
df <- ncdc_combine(out1, out2)
head(df[[1]]); tail(df[[1]])
```

Then plot - the default passing in the combined plot plots the data together. In this case it looks kind of weird since a straight line combines two distant dates.

```{r}
ncdc_plot(df)
```

But we can pass in each separately, which uses `facet_wrap` in `ggplot2` to plot each set of data in its own panel.

```{r}
ncdc_plot(out1, out2, breaks="45 days")
```
