---
title: "Reading WW Data"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Reading WW Data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# Attach Package

Attach the package. If it is not installed or you are developing, use `load_all` from the
`devtools` package. Otherwise:

```{r setup}
library(openww)
```

# Read ODS File

The open WW data is distributed in ODS (Open Document Spreadsheet) format. This can be read in Excel,
Libre Office, and other spreadsheet programs. Functions in the package will read and re-format 
sheets from the file.

A sample spreadsheet is included in the package.

```{r ods_file}
ods_file = system.file("extdata","Final_EMHP_Wastewater_Data_January_2022.ods",package="openww")
```

## Daily Data

The daily data can be read with `read_daily_ww_ods()`:

```{r read_data}
ww_daily = read_daily_ww_ods(ods_file)
head(ww_daily)
summary(ww_daily)
```

The spreadsheet is a full table of sites in rows and dates in columns. When observations are not made the 
cell is blank. When observations are taken but are below the threshold level of detection then the cell 
contains the text `"tLOD"`. In the converted data read here, un-made observations are excluded, and
below-threshold measurements are recorded as `NA`.

## Weekly Data

The weekly data can be read with `read_weekly_ww_ods()`:

```{r read_weekly}
ww_weekly = read_weekly_ww_ods(ods_file)
head(ww_weekly)
summary(ww_weekly)
```

# Spatial Data

## Reading

The package files includes a geopackage of spatial data. One layer in this file
is the point locations of the treatment works.

```{r spatial}
library(sf)
sites_gpkg = system.file("extdata","sites.gpkg",package="openww")
stw = st_read(sites_gpkg, "sites", quiet=TRUE)

head(stw)
summary(stw)
```

## Merging and Plotting

```{r plotday}
## take a subset of one day
day_7_4 = ww_daily[ww_daily$date=="2021-07-04",]

## merge with spatial by common column "Site_code":
day_7_4 = st_as_sf(merge(day_7_4, stw))

## transform concentration to log
day_7_4$log_conc = log(day_7_4$conc)

## plot
plot(day_7_4[,"log_conc"], pch=19, cex=0.5)
```

Better maps with context can be done with the `tmap` package.
