knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"

)

library(kableExtra)

Installing deqalcrit

Install deqalcrit from github by running this code:

devtools::install_github('TravisPritchardODEQ/deqalcrit')

Only Oregon DEQ users with properly setup access to use the AWQMSdata backage will be able to use all features. Other users will be able to use the Al_crit_calculator(), Al_default_DOC(), and Al_default_criteria() functions, as those do not need access to AWQMS.

Included functions

| Function | Purpose | |--------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------| | al_anc_query(al_df) | Queries AWQMS to get ancillary data needed to calculate Al criteria. | | al_combine_ancillary(al_df, ancillary_df) | Combines Al data and output from al_get_ancillary in preparation to calculate criteria. | | al_crit_calculator(df, ph_col = "pH", hardness_col = "Hardness", DOC_col = "DOC", lat_col = "Lat_DD", long_col = "Long_DD", verbose = FALSE) | Calculates aluminum criteria based on EPA's 2018 national recommended freshwater aquatic life criteria for aluminum. | | al_default_DOC(lat, long) | Looks up default DOC values used in calculating Al criteria. | | al_default_criteria(lat, long, type) | Looks up default acute or chronic Al criteria values. |

Using deqalcritpackage

This section illustrates a sample workflow for calculating aluminum criteria.

First, lets load some needed packages:

library(deqalcrit)
library(AWQMSdata)

Next, get some Al data out of AWQMS. In this example, we are going to use data from the Surface Water Ambient Monitoring program at station 10917-ORDEQ, which is on the Pudding River. We will use the AWQMSdata package to query Oregon's AWQMS database from the backend. To install and setup this package, see https://github.com/TravisPritchardODEQ/AWQMSdata

al_data_AWQMS <- AWQMSdata::AWQMS_Data(char = "Aluminum",
                                       media = 'Water',
                                       project = 'Surface Water Ambient Monitoring',
                                       station = '10917-ORDEQ')

This dataframe is very wide, so for illustration purposes, let's get rid of some columns we do not need. Columns that need to be present for the calculator to work are:

If these columns don't exist, the ancillary data joins won't work.

al_data <- al_data_AWQMS %>%
  dplyr::select(MLocID, AU_ID, Lat_DD, Long_DD, SampleStartDate, SampleStartTime,
                SampleMedia, SampleSubmedia, Char_Name, Char_Name, Sample_Fraction,
                Result_Numeric,Result_Operator,  Result_Unit )

That gets us:

kbl(al_data) %>%
  kable_paper() %>%
  scroll_box(width = "700px", height = "500px")

Next, we need to get all the ancillary data. The al_get_ancillary() function will query AWQMS and fetch all ancillary data needed to determine the criteria. It is a wrapper around the AWQMS_Data() function that queries the ancillary parameters from monitoring locations in the aluminum dataset. The date range for the query is the min/max date of the entire dataset. This supplies much more data than needed, however, it was the easiest to implement and the code will discard unneeded data later.

ancillary_data<- deqalcrit::al_anc_query(al_data)

The parameters that are queried are:

cat(paste('-', al_ancillary_params), sep = '\n')

Now, we have to run some calculations on these values to get the final DOC, pH, and Hardness values. And then join them to the Aluminum dataset. al_combine_ancillary() will perform the calculations identified in the implementation document and then perform the join. The calculations are a bit complex, but here is what is going on:

  1. Convert all ug/L units to mg/L
  2. If we have dissolved and total fractions at the same date and time, keep the dissolved fraction, and drop the total fraction. If we only have total fraction, keep the total.
  3. Convert data format from "Long" to "Wide"
  4. If we only have TOC and not DOC, convert TOC to DOC by multiplying by 0.83
  5. If we don't have DOC or TOC, look up the default DOC value by lat/long using the published map service at https://arcgis.deq.state.or.us/arcgis/rest/services/WQ/OR_Ecoregions_Aluminum/MapServer.
  6. If we don't have Hardness, use calcium and magnesium. If we don't have one of those, use specific conductance.
  7. Split DOC, Hardness and pH into separate dataframes.
  8. Join each of the three dataframes into the aluminum dataset. Ancillary data must be collected on the same day. If more than 1 daily result for an ancillary parameter, only keep the one that is nearest in time to the aluminum data.
al_data_joined <- deqalcrit::al_combine_ancillary(al_df = al_data,
                                       ancillary_df = ancillary_data)

That gets us:

kbl(al_data_joined) %>%
  kable_paper() %>%
  scroll_box(width = "700px", height = "500px")

At this point, calculating the criteria is as easy as:

al_criteria <- deqalcrit::al_crit_calculator(al_data_joined)
kbl(al_criteria) %>%
  kable_paper() %>%
  scroll_box(width = "700px", height = "500px")

If the aluminum result is missing a value for DOC, pH, or hardness; it will lookup the default criteria values using the lat/long and querying the GIS web service at https://arcgis.deq.state.or.us/arcgis/rest/services/WQ/OR_Ecoregions_Aluminum/MapServer

And if we wanted the extra information provided in EPA's criteria calculation:

al_criteria_extra <- deqalcrit::al_crit_calculator(al_data_joined, verbose = TRUE)
kbl(al_criteria_extra) %>%
  kable_paper() %>%
  scroll_box(width = "700px", height = "500px")

Additional tools

These functions use DEQ's map services to lookup default DOC and criteria values. These functions send lat/long information to the map server and returns default values based on geographic location.

deqalcrit::al_default_DOC(45.23366, -122.7502)

deqalcrit::al_default_criteria(45.23366, -122.7502, type = "Chronic")

deqalcrit::al_default_criteria(45.23366, -122.7502, type = "Acute")

deqalcrit::al_default_criteria(45.23366, -122.7502, type = "Ecoregion")


TravisPritchardODEQ/deqalcrit documentation built on Dec. 18, 2021, 5:13 p.m.