# Working with LakeCat data

## 1 Accessing and working with LakeCat

The `StreamCatTools` package was designed to simplify the use of StreamCat data in R, leveraging the new API for StreamCat. We now have functionality in the StreamCat API for accessing and working with LakeCat data and have added functions in `StreamCatTools` to access LakeCata data in R using the API.

We can actually pull data into R from LakeCat by simply passing a URL to extract from json. We have to hard-wire parameters and are limited in the number of records returned through a `GET` request.

    res <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/metrics?name=pcturbmd2006&areaOfInterest=cat&comid=22538788")
    res$items

## 2 List LakeCat API parameters

List LakeCat parameters: Get a list of available LakeCat values for certain parameters using the `lc_get_params` function (right now just metric names and areas of interest for this function) via the API

    library(StreamCatTools)
    region_params <- lc_get_params(param='areaOfInterest')

    name_params <- lc_get_params(param='metric_names')

    print(paste0('region parameters are: ', paste(region_params,collapse = ', ')))
    #> [1] "region parameters are: cat, ws"
    print(paste0('A selection of available LakrCat metrics include: ',paste(name_params[1:10],collapse = ', ')))
    #> [1] "A selection of available LakrCat metrics include: agkffact, al2o3, bfi, canaldens, cao, cbnf, clay, coalminedens, compstrgth, damdens"

We can also see what metrics are available for what areas of interest and what years using the `sc_get_params` function (which returns a tibble of information about StreamCat metrics):

    var_info <- lc_get_params(param='variable_info')
    head(var_info)
    #> # A tibble: 6 × 13
    #>   category     metric aoi   year  short_description long_description units  dsid
    #>   <chr>        <chr>  <chr> <chr> <chr>             <chr>            <chr> <dbl>
    #> 1 Anthropogen… NABD_… Cat,… <NA>  NABD Dam Density  Density of geor… Coun…    33
    #> 2 Anthropogen… NABD_… Cat,… <NA>  NABD NID Reservo… Volume all rese… Cubi…    33
    #> 3 Anthropogen… NABD_… Cat,… <NA>  NABD Normal Rese… Volume all rese… Cubi…    33
    #> 4 Natural      Preci… Cat,… <NA>  Surplus Precipit… This dataset re… Kilo…    75
    #> 5 Natural      Rckde… Cat,… <NA>  Mean Bedrock Dep… Mean depth (cm)… Cent…    56
    #> 6 Soils        agkff… Cat,… <NA>  Ag Soil Erodibil… Mean of STATSGO… Unit…    28
    #> # ℹ 5 more variables: dataset <chr>, source_name <chr>, source_URL <chr>,
    #> #   ACTIVE <dbl>, DSNAME <chr>

Look up the display name or names for a metric using the `lc_fullname` function via the API

    metric='pcthbwet2016'
    fullname <- lc_fullname(metric)
    fullname
    #> [1] "Herbaceous Wetland Percentage 2016"

    metric='pctdecid2019,fert'
    fullname <- lc_fullname(metric)
    fullname
    #> character(0)

## 3 Get Waterbody COMIDs

In this example we use the `lc_get_comid` function to find COMIDs for a set of example lake locations we load into R.`lc_get_comid` is just a simple wrapper for `get_waterbodies` in the nhdplusTools R package. We can then use the COMIDs we derive for our lake locations to get LakeCat metrics for these lakes as we show in after this.

    dd <- data.frame(x = c(-89.198,-114.125,-122.044),
                     y = c(45.502,47.877,43.730)) |>
      sf::st_as_sf(coords = c('x', 'y'), crs = 4326)

    comids <- lc_get_comid(dd)

## 4 Get data for COMIDs

In this example we access several variables, for several areas of interest, and for several COMIDs using the `lc_get_data` function. We’ll show using both the COMIDS we derived with `lc_get_comid` function in previous chunk as well as with known COMIDS for NHDPlus waterbodies. Loads data into a tibble we can view.

    df <- lc_get_data(metric='pcturbmd2006,damdens', aoi='cat,ws', comid=comids)
    knitr::kable(df)

comid | damdenscat | damdensws | pcturbmd2006cat | pcturbmd2006ws
--- | --- | --- | --- | ---
23756298 | 0.0000000 | 0.0000000 | 0.0293228 | 0.0293228
120054065 | 0.0000000 | 0.0007270 | 0.4348181 | 0.1451495
167120863 | 0.0243986 | 0.0243986 | 0.2086078 | 0.2086078

    df <- lc_get_data(metric='pcturbmd2006,damdens', aoi='cat,ws', comid='23783629,23794487,23812618')
    knitr::kable(df)

comid | damdenscat | damdensws | pcturbmd2006cat | pcturbmd2006ws
--- | --- | --- | --- | ---
23783629 | 0.0192097 | 0.0008875 | 0.2091942 | 0.0213267
23794487 | 0.0000000 | 0.2433268 | 0.1958225 | 0.8832762
23812618 | 0.0320852 | 0.0071455 | 0.0115507 | 0.0032155

## 5 Get data for county

In this example we access a couple variables at the watershed scale for the area of interest of a county (Benton County in this case) using the `sc_get_data` function.

    df <- lc_get_data(metric='pctwdwet2006', aoi='ws', county='41003')
    knitr::kable(head(df))

comid | pctwdwet2006ws
--- | ---
23768945 | 0.1662971
23768987 | 0.0000000
23888156 | 0.4161465
23768895 | 0.1344990
23769007 | 1.0194538
23768947 | 0.5524862

## 6 Get NLCD data

In this example we access National Land Cover Dataset (NLCD) data for 2019, just at the catchment level for several COMIDs using the `lc_nlcd` function. Loads data into a tibble we can view.

    df <- lc_nlcd(comid='23783629,23794487,23812618', year='2019', aoi='ws')
    knitr::kable(df)

comid | pctbl2019ws | pctconif2019ws | pctcrop2019ws | pctdecid2019ws | pctgrs2019ws | pcthay2019ws | pcthbwet2019ws | pctice2019ws | pctmxfst2019ws | pctow2019ws | pctshrb2019ws | pcturbhi2019ws | pcturblo2019ws | pcturbmd2019ws | pcturbop2019ws | pctwdwet2019ws
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
23783629 | 1.5601274 | 82.8417931 | 0.00000 | 0.0627022 | 3.3679487 | 0.0007988 | 0.0228444 | 0.2318785 | 0.1064740 | 1.500061 | 9.2003748 | 0.0027956 | 0.0838692 | 0.0232438 | 0.8217587 | 0.1733297
23794487 | 0.0000000 | 0.4014892 | 31.64465 | 1.8176509 | 0.1167968 | 42.3096576 | 11.9132783 | 0.0000000 | 3.0878166 | 1.744653 | 0.2043945 | 0.2627929 | 3.6718009 | 1.0146726 | 1.6351559 | 0.1751953
23812618 | 0.0006431 | 84.9302559 | 0.00000 | 0.0623806 | 0.9241336 | 0.0000000 | 0.5498498 | 0.0000000 | 0.0032155 | 4.111976 | 5.2984945 | 0.0000000 | 0.0533772 | 0.0032155 | 1.8714187 | 2.1910391