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

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = TRUE
)
## setup vcr not sure I am doing this right.
library(vcr)
#invisible(vcr::vcr_configure(
#  dir = "../tests/fixtures",
#  write_disk_path = NULL
#))
vcr::setup_knitr(dir = "_vcr", record = "once")

```



# About

The Assessment, Total Maximum Daily Load (TMDL) Tracking and Implementation System (ATTAINS) is the U.S. Environmental Protection Agency (EPA) database used to track information provided by states about water quality assessments conducted under the Clean Water Act. The assessments are conducted every two years to evaluate if the nation's water bodies meet water quality standards. States are required to take Actions (TMDLs or other efforts) on water bodies that do not meet standards. Public information in ATTAINS is made available through webservices and provided as JSON files. rATTAINS facilitates accessing this data with various functions that provide raw JSON or formatted "tidy" data for each of the ATTAINS webservice endpoints. More information about Clean Water Act assessment and reporting is available through the [EPA](https://www.epa.gov/waterdata/attains-program-information). For alternative methods of accessing the same data, see ["How's My Waterway" webpage](https://mywaterway.epa.gov/) for interactive data exploration or the [ArcGIS MapService](https://gispub.epa.gov/arcgis/rest/services/OW/ATTAINS_Assessment/MapServer) for spatial data.

# Functions

## Summary Services

The EPA provides two summary service endpoint that provide summaries of assessed uses by the organization identifier or by hydrologic unit code (HUC). For example, to return a summary of assessed uses by the state of Tennessee the following function is used:


```{r label=state_summary, message=FALSE, use_cassette=TRUE}
library(rATTAINS)
x <- state_summary(organization_id = "TDECWR",
                   reporting_cycle = "2024")
x
```



The HUC12 service operates similarly but provides data summarized by area, specifically HUC12 units. For example:



```{r huc12, message=FALSE, use_cassette=TRUE}
x <- huc12_summary("020700100204")
x
```


`huc12_summary()` returns a list of tibbles with different summaries of information. Using the above example: 
- `x$huc_summary` provides a summary of HUC area, and the area and percentage of catchment assessed as good, unknown, or impaired. 
- `x$au_summary` provides a tibble with the unique identifiers for the assessment units (or distinct sections of waterbodies) within the queried HUC12. 
- `x$ir_summary` provides a simple summary of the area of the catchment classified under different Integrated Report Categories. 
- `x$status_summary` provides a summary of the overall status within the HUC12. 
- `x$use_group_summary` provides a summary of use attainment bu use group within the HUC12.
- `x$use_summary` breaks the use summary down further by the use name.
- `x$param_summary` provides the same information for parameter groups. 
- `x$res_plan_summary` and `x$vision_plan_summary` provides a summary of the amount of the watershed covered by particular types of restoration plans or vision plan, such as TMDLs. 

## Domains

Each function has a number of allowable arguments and associated values. In order to explore what values you might be interested in querying, the Domain Value service provides information about allowable options. This is mapped to the `domain_values()` function. When used without any arguments you get a full list of possible "domains." These are typically searchable parameters used in all the functions in rATTAINS. Note that the domain names returned by these service are not a one to one match with the argument names used in rATTAINS. It is typically fairly easy to figure out which ones match up to which arguments.

For example if I want to find out the possible organization identifiers to query by:


```{r domain_values, use_cassette=TRUE}
x <- domain_values(domain_name = "OrgStateCode")
x
```



The function returns a variable with the state codes and the possible parameter values as the context variable. Similarly if I want to look up possible Use Names that are utilized by the Texas Commission on Environmental Quality:

```{r domain_context, use_cassette=TRUE}
x <- domain_values(domain_name = "UseName", context = "TCEQMAIN")
x

```



## Other Services

 - `assessment_units()` : provides information about assessment units by the specified argument parameters. 
 
 - `assessments()` provides information about assessment decisions by the specified argument parameters.
 
 - `actions()` provides information about Actions (such as TMDLs, 4B Actions, or similar) that have been finalized by the specified argument parameters. 
 
 - `plans()` is similiar to actions but provides information about finalized Actions and assessment units by HUC8. 
 
 - `surveys()` provides information about organization conducted statistical surveys about water quality assessment results.
 
## JSON Files

By default, all the functions rATTAINS return one or more "tidy" dataframes. These dataframe are created by attempting to flatten the nested JSON data returned by the webservice. This does require some opinionated decisions on what constitutes flat data, and at what variable data should be flattened to. We recognize that the dataframe output might not meet user needs. There if you would prefer to parse the JSON data yourself, use the `tidy=FALSE` argument to return an unparsed JSON string. A number of R packages are available to parse and flatten JSON data to prepare it for analysis.

 
# Notes

The U.S. EPA is the data provider for this public information. rATTAINS and the author are not affiliated with the EPA. Questions about the package functionality should be directed to the package author. Questions about the webservice or underlying data should be directed to the U.S. EPA.  Please do not abuse the webservice using this package. 
