|
| 1 | +#' Get Continuous USGS Water Data |
| 2 | +#' |
| 3 | +#' @description `r get_description("continuous")` |
| 4 | +#' |
| 5 | +#' Currently, the services only allow up to 3 years of data to be requested with |
| 6 | +#' a single request. If no "time" is specified, the service will return the |
| 7 | +#' last single year of data. If this is a bottleneck, please check back |
| 8 | +#' for new direct download functions that are expected to be available sometime |
| 9 | +#' in 2026. |
| 10 | +#' |
| 11 | +#' @export |
| 12 | +#' @param monitoring_location_id `r get_params("continuous")$monitoring_location_id` |
| 13 | +#' @param parameter_code `r get_params("continuous")$parameter_code` |
| 14 | +#' @param time `r get_params("continuous")$time` |
| 15 | +#' @param value `r get_params("continuous")$value` |
| 16 | +#' @param unit_of_measure `r get_params("continuous")$unit_of_measure` |
| 17 | +#' @param approval_status `r get_params("continuous")$approval_status` |
| 18 | +#' @param last_modified `r get_params("continuous")$last_modified` |
| 19 | +#' @param time_series_id `r get_params("continuous")$time_series_id` |
| 20 | +#' @param qualifier `r get_params("continuous")$qualifier` |
| 21 | +#' @param statistic_id `r get_params("continuous")$statistic_id`. Note that |
| 22 | +#' for continuous data, the statistic_id is almost universally 00011. |
| 23 | +#' Requesting anything else will most-likely cause a timeout. |
| 24 | +#' @param properties A vector of requested columns to be returned from the query. |
| 25 | +#' Available options are: |
| 26 | +#' `r schema <- check_OGC_requests(endpoint = "continuous", type = "schema"); paste(names(schema$properties)[!names(schema$properties) %in% c("id", "internal_id")], collapse = ", ")` |
| 27 | +#' @param limit The optional limit parameter is used to control the subset of the |
| 28 | +#' selected features that should be returned in each page. The maximum allowable |
| 29 | +#' limit is 50000. It may be beneficial to set this number lower if your internet |
| 30 | +#' connection is spotty. The default (`NA`) will set the limit to the maximum |
| 31 | +#' allowable limit for the service. |
| 32 | +#' @param max_results The optional maximum number of rows to return. This value |
| 33 | +#' must be less than the requested limit. |
| 34 | +#' @param convertType logical, defaults to `TRUE`. If `TRUE`, the function |
| 35 | +#' will convert the data to dates and qualifier to string vector, and sepcifically |
| 36 | +#' order the returning data frame by time and monitoring_location_id. |
| 37 | +#' @examplesIf is_dataRetrieval_user() |
| 38 | +#' |
| 39 | +#' \donttest{ |
| 40 | +#' site <- "USGS-451605097071701" |
| 41 | +#' pcode <- "72019" |
| 42 | +#' |
| 43 | +#' uv_data_trim <- read_waterdata_continuous(monitoring_location_id = site, |
| 44 | +#' parameter_code = pcode, |
| 45 | +#' properties = c("value", |
| 46 | +#' "time")) |
| 47 | +#' |
| 48 | +#' uv_data <- read_waterdata_continuous(monitoring_location_id = site, |
| 49 | +#' parameter_code = pcode, |
| 50 | +#' time = "P2D") |
| 51 | +#' |
| 52 | +#' |
| 53 | +#' # Only return data that has been modified in last 7 days |
| 54 | +#' multi_site2 <- read_waterdata_continuous(monitoring_location_id = c("USGS-451605097071701", |
| 55 | +#' "USGS-14181500"), |
| 56 | +#' parameter_code = c("00060", "72019"), |
| 57 | +#' last_modified = "P7D") |
| 58 | +#' |
| 59 | +#' } |
| 60 | +read_waterdata_continuous <- function(monitoring_location_id = NA_character_, |
| 61 | + parameter_code = NA_character_, |
| 62 | + properties = NA_character_, |
| 63 | + time_series_id = NA_character_, |
| 64 | + approval_status = NA_character_, |
| 65 | + unit_of_measure = NA_character_, |
| 66 | + qualifier = NA_character_, |
| 67 | + statistic_id = NA_character_, |
| 68 | + value = NA, |
| 69 | + last_modified = NA_character_, |
| 70 | + time = NA_character_, |
| 71 | + limit = NA, |
| 72 | + max_results = NA, |
| 73 | + convertType = TRUE){ |
| 74 | + |
| 75 | + service <- "continuous" |
| 76 | + output_id <- "continuous_id" |
| 77 | + |
| 78 | + args <- mget(names(formals())) |
| 79 | + args[["skipGeometry"]] <- TRUE |
| 80 | + |
| 81 | + if(!is.na(statistic_id) & !all(statistic_id == "00011")){ |
| 82 | + warning("With few if any exceptions, statistic_id is always 00011 for continuous data, and requesting other statistic ids will likely return no data.") |
| 83 | + } |
| 84 | + |
| 85 | + return_list <- get_ogc_data(args, |
| 86 | + output_id, |
| 87 | + service) |
| 88 | + |
| 89 | + if(convertType){ |
| 90 | + return_list <- order_results(return_list, properties) |
| 91 | + return_list <- return_list[, names(return_list)[names(return_list)!= output_id]] |
| 92 | + if("time_series_id" %in% names(return_list)){ |
| 93 | + return_list <- return_list[, c( names(return_list)[names(return_list)!= "time_series_id"], |
| 94 | + "time_series_id")] |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return(return_list) |
| 99 | +} |
| 100 | + |
| 101 | +order_results <- function(return_list, properties){ |
| 102 | + |
| 103 | + if(all(is.na(properties)) | |
| 104 | + all(c("time", "monitoring_location_id") %in% properties)){ |
| 105 | + return_list <- return_list[order(return_list$time, |
| 106 | + return_list$monitoring_location_id), ] |
| 107 | + } else if ("time" %in% properties) { |
| 108 | + return_list <- return_list[order(return_list$time), ] |
| 109 | + } |
| 110 | + |
| 111 | + return(return_list) |
| 112 | +} |
| 113 | + |
0 commit comments