Repository navigation
Expand file tree
/
Copy pathEQ_AUsMLs.R
More file actions
113 lines (100 loc) · 5.2 KB
/
Copy pathEQ_AUsMLs.R
File metadata and controls
113 lines (100 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#' Query and return ATTAINS Assessment Units with Monitoring Locations data via Expert Query web
#' services
#'
#' @param api_key Character string. Users must supply their unique api key to access Expert
#' Query web services. To obtain an api, submit the form at:
#' https://owapps.epa.gov/expertquery/api-key-signup
#' @param au_name Character string. The name assigned to an Assessment Unit by the Organization.
#' @param au_status Character string. The current condition or status of an Assessment Unit.
#' Options are "Active", "Historical" or "Retired". Default = "Active".
#' @param auid Character string. A unique identifier assigned to an Assessment Unit by the
#' Organization. Default = NULL.
#' @param mon_loc_id Character string. Unique identifier for the monitoring location.
#' @param mon_loc_org Character string. The unique identifier assigned to the Organization that
#' conducted the monitoring. For all possible options see the WQP domain list for Organizations
#' here: https://cdx.epa.gov/wqx/download/DomainValues/Organization_CSV.zip.
#' @param org_id Character string. A unique identifier assigned to the Organization. Options can
#' be viewed with EQ_DomainValues("org_id"). Default = NULL.
#' @param org_name Character string. A unique name assigned to the Organization. Options can
#' be viewed with EQ_DomainValues("org_name"). Default = NULL.
#' @param region Numeric (integer). Integer from 1 to 10 to identify the EPA region of interest.
#' See https://www.epa.gov/aboutepa/regional-and-geographic-offices for options. Default = NULL.
#' @param report_cycle Character string. The Integrated Reporting cycle of the data. Format is
#' "YYYY" or "latest", which will select the most recent available cycle. Default = "latest".
#' @param statecode Character string. FIPS state alpha code that identifies a state (e.g.
#' statecode = "DE" for Delaware). See https://www.waterqualitydata.us/Codes/statecode for options.
#' Default = NULL.
#' @param use_class Character string. The Use Class assigned to an Assessment Unit. Options are
#' "CULTURAL_USE", "DRINKING_WATER_USE", "ECOLOGICAL USE", "FISHCONSUMPTION_USE", "OTHER_USE",
#' and "RECREATION_USE". Default = NULL.
#' @param water_type Character string. An Assessment Unit must have at least one water type, and it
#' may have multiple water types. Options can be viewed with EQ_DomainValues("water_type"). Default
#' = NULL.
#'
#' @return A data frame of ATTAINS Assessment Units with Monitoring Locations with the columns
#' "objectId", "region", "state", "organizationType", "organizationId", "organizationName",
#' "waterType", "useClassName", "monitoringLocationId", "monitoringLocationOrgId",
#' "assessmentUnitId", "assessmentUnitName", "assessmentUnitStatus", "reportingCycle",
#' "locationDescrption", "monitoringLocationDataLink", "sizeSource", "sourceScale", "waterSize",
#' and "waterSizeUnits".
#'
#' @export
#'
#' @examples
#' \dontrun{
#'
#' # to run examples add your api key below
#' # to get an API key: https://owapps.epa.gov/expertquery/api-key-signup
#' testkey <- "YOURAPIKEY"
#'
#' # Alaska Assessment Units with Monitoring Locations
#' AK_aus_mls <- EQ_AUsMLs(statecode = "AK",
#' api_key = testkey)
#'
#' # Red Lake Band of Chippewa Indians (Minnesota) Assessment Units with Monitoring Locations
#' RedLake_aus_mls <- EQ_AUsMLs(org_id = "REDLAKE",
#' api_key = testkey)
#' }
#'
EQ_AUsMLs <- function(api_key = NULL, au_name = NULL, au_status = "Active", auid = NULL,
mon_loc_id = NULL, mon_loc_org = NULL, org_id = NULL, org_name = NULL,
region = NULL, report_cycle = "latest", statecode = NULL, use_class = NULL,
water_type = NULL) {
# check for api key
if (is.null(api_key)) {
stop("EQ_AUsMLs: An api key is required to access EQ web services.")
}
# get param crosswalk for building query
params.cw <- EQ_ExtractParams(extract = "au_mls")
# get default params from EQ_Assessments
default.params <- EQ_DefaultParams(EQ_AUsMLs) |>
# format for building body
EQ_FormatParams()
#create df of user entered params
get.user <- as.list(match.call()[-1])
# evaluate user params
user.evals <- lapply(get.user, eval, envir = parent.frame())
# create df and format params
user.params <- as.data.frame(t(user.evals), stringsAsFactors = FALSE) |>
tidyr::pivot_longer(dplyr::everything(), names_to = "param") |>
EQ_FormatParams()
# remove intermediate objects
rm(get.user, user.evals)
# compare default and user params to build df of all params and values for body
params.df <- EQ_CompareParams(default = default.params, user = user.params)
# remove intermediate objects
rm(user.params, default.params)
# create post bodies
post.bodies <- EQ_CreateBody(comp.params = params.df, crosswalk = params.cw, extract = "au_mls")
# create post headers
post.headers <- EQ_CreateHeader(key = api_key)
# query EQ (check number of rows before download, stop if it exceeds max rows)
query.df <- EQ_PostAndContent(
headers = post.headers,
body.list = post.bodies,
extract = "au_mls"
)
# should rows where ml is NA be filtered out?
rm(params.cw, params.df, post.bodies, post.headers)
return(query.df)
}