setAccess.R 2.71 KB
Newer Older
Read, Jordan S.'s avatar
Read, Jordan S. committed
1
2
3
4
5
#'Set data endpoint
#'
#'access Indicate which dataRetrieval access code
#' you want to use options: \code{c('public','internal')}
#'
Laura A DeCicco's avatar
Laura A DeCicco committed
6
7
8
9
10
11
12
#' @param access code for data access. Options are: "public","internal","cooperator", or "USGS".
#' \itemize{ 
#' \item{"internal" represents Access=3 ...for a single water science center}
#' \item{"USGS" represents Access=2 ...for all water science centers}
#' \item{"cooperator" represents Access=1}
#' \item{"public" represents Access=0, public access}
#' }
Read, Jordan S.'s avatar
Read, Jordan S. committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#'
#'@author Luke Winslow, Jordan S Read
#'
#'@examples
#'
#'\dontrun{
#'setAccess('internal')
#'
#'setAccess('public')
#'
#'}
#'
#' @export
setAccess = function(access="public"){
27
access = match.arg(access, c('public','internal','cooperator','USGS'))
Read, Jordan S.'s avatar
Read, Jordan S. committed
28
29
  
  if(access=="internal"){
30
    pkg.env$access = '3'
Read, Jordan S.'s avatar
Read, Jordan S. committed
31
    message('setting access to internal')
Laura A DeCicco's avatar
Laura A DeCicco committed
32
33
34
35
36
37
38
39
  } else if(access=="cooperator"){
    pkg.env$access = '1'
    message('setting access to cooperator')
  } else if(access=="USGS"){
    pkg.env$access = '2'
    message('setting access to all USGS Water Science Centers')    
  } else {
    pkg.env$access = NULL
40
    message('setting access to public')
Read, Jordan S.'s avatar
Read, Jordan S. committed
41
42
  }
  
43
  pkg.env$site = "https://waterservices.usgs.gov/nwis/site/"
Laura A DeCicco's avatar
Laura A DeCicco committed
44
  pkg.env$iv = "https://nwis.waterservices.usgs.gov/nwis/iv/"
45
46
47
48
49
50
51
  pkg.env$dv =  "https://waterservices.usgs.gov/nwis/dv/"
  pkg.env$gwlevels = "https://waterservices.usgs.gov/nwis/gwlevels/"
  pkg.env$measurements = "https://waterdata.usgs.gov/nwis/measurements/"
  pkg.env$peak = "https://nwis.waterdata.usgs.gov/usa/nwis/peak/"
  pkg.env$rating = "https://waterdata.usgs.gov/nwisweb/get_ratings/"
  pkg.env$qwdata = "https://nwis.waterdata.usgs.gov/nwis/qwdata"
  pkg.env$stat = "https://waterservices.usgs.gov/nwis/stat/"
Laura A DeCicco's avatar
Laura A DeCicco committed
52
53
  pkg.env$useNat = "https://waterdata.usgs.gov/nwis/water_use"
  # NOTE: state water use are still in: constructUseURL
54

Laura A DeCicco's avatar
Laura A DeCicco committed
55
56
57
  pkg.env$wqpData = "https://www.waterqualitydata.us/Result/search"
  pkg.env$wqpStation = "https://www.waterqualitydata.us/Station/search"
  
58
  options(Access.dataRetrieval = access)
Read, Jordan S.'s avatar
Read, Jordan S. committed
59
60
}

61
62
63
64
65
66
67
68
drURL <- function(base.name, ..., arg.list=NULL){
  queryString <- drQueryArgs(..., arg.list=arg.list)
  #to do: add something to check for redundant params
  
  return(paste0(pkg.env[[base.name]], '?', queryString))
}

drQueryArgs <- function(..., arg.list){
Laura A DeCicco's avatar
Laura A DeCicco committed
69
  dots <- list(...)
Laura A DeCicco's avatar
vapply    
Laura A DeCicco committed
70
  dots <- dots[!vapply(X=dots,FUN=is.null,FUN.VALUE = TRUE)]
Laura A DeCicco's avatar
Laura A DeCicco committed
71
72
  
  args <- append(expand.grid(dots, stringsAsFactors = FALSE), arg.list)
73
74
75
76
77
78
79
80
81
  # get the args into name=value strings
  keyValues <- paste0(names(args),unname(lapply(args, function(x) paste0('=',x[[1]]))))
  return(paste(keyValues, collapse='&'))
}

appendDrURL <- function(url, ..., arg.list=NULL){
  
  queryString <- drQueryArgs(..., arg.list=arg.list)
  return(paste0(url, "&", queryString))
Read, Jordan S.'s avatar
Read, Jordan S. committed
82
}