---
title: "The Structure of LAGOSNE"
author: "Samantha Oliver"
date: "`r Sys.Date()`"
output: 
  html_document:
      fig_caption: yes
---

## LAGOSNE Overview
The Lake Multi-Scaled Geospatial and Temporal Database (LAGOSNE) is a 
combination of three data modules (LAGOSlocus, LAGOSlimno, LAGOSgeo) designed to be of use for researchers and managers to [insert line from paper]. The database includes information about lakes in a lake-rich region of 17 states in the United States, including Minnesota, Iowa, Wisconsin, Illinois, Missouri, Michigan, Indiana, Ohio, Pennsylvania, New York, New Jersey, Connecticut, New Hampshire, Rhode Island, Massachusetts, Vermont, and Maine. LAGOSNE contains a complete census of all lakes greater than 4 hectares in the region with supporting ecological context information. Additionally, for a subset of lakes, LAGOSNE contains water quality data. The LAGOSNE package was built so that future data users could easily retrieve and manipulate the data, as well as easily access metadata. 

LAGOSNE is the product of many thousands of human hours over the course of 
data collection and integration. The nitty gritty details of such efforts (which we highly recommend!) can be found in two locations. The details describing how LAGOSNE was built, including sources and metric derivations, can be found in Soranno et al. 2015. The details of the data themselves, along with the data, can be found in Soranno et al. 2017. 

## Accessing LAGOSNE
Following [these instructions](https://github.com/cont-limno/LAGOSNE), install the LAGOSNE package and download the data from the online repository. The data are open-access and stored online as flat files [location]. The data only need to be downloaded once, and will be stored locally at the location returned by `rappdirs::user_data_dir`

Now that the data are stored locally, you can import them as a list.
```{r eval=TRUE, message=FALSE}
library(LAGOSNE)
# Import LAGOSNE data into R
dt <- lagosne_load(version = "1.087.1")
```

## LAGOSNE Structure
As previously noted, LAGOSNE is comprised of three modules with multiple tables within each module. When LAGOSNE is imported using the above method, the tables are stored in a list, and you can get information about each table in a few ways:
```{r }
names(dt)
```

```{r load printr, echo=FALSE,message=FALSE,results='hide'}
loadNamespace("printr")
```

```{r Read metadata for individual tables}
help.search("datasets", package = "LAGOSNE")
```

```{r unload printr, echo=FALSE}
unloadNamespace("printr")
```

Below, we describe how each table fits within each module. 

### LAGOSlimno
LAGOSlimno is an integration of preexisting water quality data from disparate sources, including state, federal, tribal, and university organizations. Water chemistry (various species of nitrogen, phosphorus, and carbon) and transparency (Secchi disk measurement) are the main features of the `epi.nutr` table, but also include metadata about those measurements. `?epi.nutr` gives the names of all variables and metadata (including units). Because many unique sampling events only included Secchi depth, a separate table of all Secchi measurements is included (see `?secchi`). `epi.nutr` does contain Secchi observations that are in `secchi`, but only if additional water quality measurements were taken during the same sampling event.

Each observation of LAGOSlimno is a discrete sampling event, where one or more water quality parameters were measured. Sampling events are tied to lakes that have a unique LAGOSNE and National Hydrography Dataset (NHD) identifier, and lakes can have single or multiple obervations of each parameter. There are 12,934 and 12,712 unique lakes in `epi.nutr` and `secchi`, respectively, distributed across the 17-state region. 

### LAGOSgeo
LAGOSgeo is an integration of information describing the air, water, and 
land (hereafter, ecological context) surrounding all lakes greater than or equal to 4 hectares in surface area in the LAGOSNE 17-state region. LAGOSgeo contains three themes of data that describe the ecological context of each lake: CHAG (climate, hyrology, atmospheric deposition, and surficial geology), CONN (stream, wetland, and lake surficial hydrologic connectivity), and LULC (land use/land cover). Some of these metrics have temporal coverage (e.g., atmospheric deposition) whereas others represent average conditions over a period of time (e.g., 30-year climate normals). The source data and metric derivation for 

Each theme of LAGOSgeo was calculated at one or more of the nine spatial classifications (see `?classifications`). The table names of each classification-theme combination are formatted as  [classification].[theme] (e.g., state.chag, hu4.conn, iws.lulc). Each theme and table within LAGOSgeo is connected to a help file and can be viewed by calling either the theme or table name (e.g., `?state.chag` or `?chag`). 

#### Spatial Classifications Layers
In additional to the multiple tables describing ecological context at each spatial classification, we have provided the polygons for each spatial classification. [change to future location of shape files - for now link to HU4 files that were put on Github as part of the time series manuscript]
```{r, message = FALSE, eval=FALSE}
library(rgdal)

# get huc4 polygons from github
load(url("https://github.com/limnoliver/CSI-Nutrient-Time-Series/blob/72c8269902e53c7ec6a2cfbe13a0239d13062dc8/Data/huc4.RData?raw=true"))
plot(huc4, lty = 1, lwd=1, border=TRUE, col = "lightgray")
```

### LAGOSlocus
LAGOSlocus provides a connection between all tables, as it contains metadata for the census population of lakes (all lakes greater than or equal to one hectare; n = `r nrow(dt$lakes_limno)`) in the LAGOSNE region, with corresponding unique identifiers for the zone of each spatial classification in which the lake is located. Lakes were identified through the National Hydrography Dataset, and the table also includes data about the lake polygons themselves, including surface area and perimeter. For a subset of lakes where data were available, lake depth (mean and/or maximum) is also included. 
