---
title: "Beach Pathogens"
csl: stylefile.csl
bibliography: refs.bib
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Beach Pathogens}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE, message = F, warning = F}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = F, warning = F, fig.align = 'center')

library(peptools)
library(ggplot2)

mptyps <- c("CartoDB.Positron", "CartoDB.DarkMatter", "OpenStreetMap", "Esri.WorldImagery", "OpenTopoMap")
```

Regular monitoring of swimming beach pathogens is conducted by Suffolk County Department of Health Services (SCDHS).  As noted on the Suffolk County website (https://www.suffolkcountyny.gov/Departments/Health-Services/Environmental-Quality/Ecology/Beach-Monitoring-Program), water quality at swimming beaches can be adversely affected by episodic sources such as stormwater runoff, wastewater discharges from boats or land-based septic systems, and fecal material from pets and wildlife. To provide information on the potential presence of pathogens public in swimming areas that may be impacted by such contamination, the Suffolk County Department of Health Services (SCDHS) conducts a comprehensive bathing beach water quality monitoring program from May through September each year. Sampling is performed by SCDHS staff, with analyses conducted by the Department’s accredited Public and Environmental Health Laboratory (PEHL). Data are available [here](https://gisportal.suffolkcountyny.gov/gis/home/item.html?id=025cb4dadb57413980dbd7e760b94da8).

Information from this monitoring program can be summarized to communicate the relative risk of exposure to pathogens at bathing beaches in the Peconic Estuary. Functions in the peptools package can be used to import the pathogen data provided by SCHDS and PEHL, analyze relative exceedances of pathogen criterion, and plot the results in an easily interpretable format.  This vignette describes use of these functions.

For package installation instructions, please see the [Introduction](https://tbep-tech.github.io/peptools/articles/Introduction.html) vignette. 

## Data import and processing

The `beaches` data object includes 31 beaches that are relevant for assessing pathogen exposure risk for the Peconic Estuary Program.

```{r}
beaches
```

The beaches can be mapped as follows:

```{r}
library(mapview)
mapview(beaches)
```

The pathogen data for these beaches can be imported using the `read_pepent()` function. This function retrieves data directly from an ArcGIS REST service available at <https://gis.suffolkcountyny.gov/hosted/rest/services/Hosted/Beach_Water_Quality_Data/FeatureServer/>. The API is queried by beach names in the `beaches` data object. The queries are done individually for each beach to not exceed the 2000 record limit. A spreadhseet including the same data can also be downloaded from [here](https://gisportal.suffolkcountyny.gov/gis/home/item.html?id=025cb4dadb57413980dbd7e760b94da8). 

The import function is used as follows: 

```{r, eval = F}
entdat <- read_pepent()
head(entdat)
```
```{r, echo = F}
head(entdat)
```

The data can also be imported from a local file using the `path` argument. 

```{r, eval = F}
entdat <- read_pepent(path = '~/Desktop/pathogen_data.xlsx')
```

The raw data includes concentrations of *Enterococcus* bacteria as cfu/100 mL for swimming beaches in Suffolk County.  The fields include beach name (`Name`), field number (`FieldNum`), collection date and time (`Date` as Eastern Standard Time), bacteria concentration (`value`), and `status` showing if the observation was above or below detection (indicated as `>` or `<`).  

The function `anlz_entpep()` summarizes the imported data at each beach to quantify instances when bacterial concentrations were at risk of impacting human health. For Suffolk County, all bathing beaches are ranked by relative risk of exposure to harmful bacteria. Factors considered in the ranking include pollution threats (outfalls, discharges, runoff, marinas, etc.), beach location, historical monitoring data, and beach use.  Most beaches in the Peconic Estuary are Tier 3 (lowest tier). For the peptools package, the *Enterococcus* data were used to count the number of instances at each beach when concentrations were above 104 cfu/ml.  Although this does not mean a beach was closed, relative exceedances provide a coarse measure of potential risk of exposure to harmful pathogens. 

The `anlz_entpep()` function estimates this exposure risk by counting the number of instances in a year when concentrations at a beach exceeded the threshold for each 24 hour period in the dataset.  The results show `samples` for number of days sampled each year and number of `exceedances` for the samples. Only the 28 relevant beaches for the Peconic Estuary are returned.

```{r}
anlz_entpep(entdat)
```

The `anlz_entpep()` function includes an optional arguments for the threshold (`thr`).  The default values are 104 cfu/100 mL, which can easily be changed. Here we use a threshold of 50 cfu/100 mL.

```{r}
anlz_entpep(entdat, thr = 50)
```

## Plotting results

A summary graphic can be plotted using the `show_entmatrix()` function. This creates an annual reporting matrix for the relative risk of pathogen exposure at 28 selected beaches in the Peconic Estuary. Tracking the number of exceedances of bacterial concentrations provides a framework for identifying locations that may need management actions to remediate potential public health issues.  

```{r, fig.height = 7, fig.width = 7, fig.cap = 'Exceedances of *Enterococcus* concentrations at Peconic Estuary bathing beaches for the years 2010 through 2023.  Values are number of samples exceeding.'}
show_entmatrix(entdat)
```

The `anlz_entpep()` function is used internally in `show_entmatrix()`, such that the optional argument for the threshold (`thr`) also applies in the plotting function.  Appropriate thresholds should be identified. Potential alternatives can be viewed by using a different value for the `thr` argument. 

```{r, fig.height = 7, fig.width = 7, fig.cap = 'Exceedances of *Enterococcus* concentrations at Peconic Estuary bathing beaches for the years 2010 through 2023.  A different option is used for the threshold argument.'}
show_entmatrix(entdat, thr = 35)
```

The matrix is also a `ggplot` object and its layout can be changed using `ggplot` elements. Note the use of `txtsz = NULL` to remove the text labels. 

```{r, fig.height = 4, fig.width = 8}
show_entmatrix(entdat, txtsz = NULL) +
  scale_x_discrete(expand = c(0,0), breaks = c(2000:2023)) + 
  coord_flip() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 7))
```

