This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geomet.R
97 lines (73 loc) · 3.34 KB
/
geomet.R
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
# Copyright 2018 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
#' Collections available in the GeoMet webservice
#'
#' Outputs a tibble of the collections currently available via the webservice.
#'
#' @examples
#' geomet_collection()
#'
#' @export
geomet_collection <- function() {
cli <- crul::HttpClient$new(url = base_url())
res <- cli$get()
txt <- res$parse("UTF-8")
parsed <- jsonlite::fromJSON(txt)
parsed_tibble <- dplyr::as_tibble(parsed$collections)
parsed_tibble$crs <- as.character(parsed_tibble$crs)
parsed_tibble[, c("title", "name", "description", "extent", "crs", "links")]
}
#' Query GeoMet for data
#'
#' Query geomet webservice for ECCC data
#'
#' @param parameter One of \code{geomet_collection()$name}
#' @param station_number A meteorological/hydrometric station number.
#' @param as_spatial Convert to sf object. Defaults to TRUE.
#' @param start_date Accepts either YYYY-MM-DD. If this argument is left missing, the function will return all data prior to \code{end_date}. If both are NULL all
#' available data is returned.
#' @param end_date Accepts either YYYY-MM-DD. If this argument is left missing, the function will return all data after \code{start_date}. If both are NULL all
#' available data is returned.
#' @param page_limit Default to 500. You likely will not need to change this value. This represents the maximum number of records that can be requested
#' for each pagination request.
#' @param verbose Defaults to FALSE. Do you want the URLs used to make the GET call to be printed?
#'
#' @export
#'
#' @examples
#' geomet_data(parameter = "hydrometric-daily-mean", station_number = "10PC003", start_date = "1983-07-04")
#'
#' ## Get all ahccd-stations
#' geomet_data("ahccd-stations")
#'
#' geomet_data("ahccd-monthly", station_number = "1171020")
geomet_data <- function(parameter, station_number = NULL, as_spatial = TRUE , start_date = NULL, end_date = NULL, page_limit = 500, verbose = FALSE){
## Argument Checks
#stop_if_all_args_null()
is_there_internet()
check_date_format(start_date, end_date)
date_range <- handle_dates(start_date = start_date, end_date = end_date)
## Refine query list
query_list <- list(STATION_NUMBER = multi_stations(station_number),
time = date_range)
## Drop any NULLS from the list
query_list <- Filter(Negate(is.null), query_list)
cli <- geomet_client(geomet_param = parameter)
## Total number of records available
number_record <- num_matched_records(query_list, cli)
cc <- crul::Paginator$new(client = cli, by = "query_params", limit_param = "limit",
offset_param = "startindex", limit = number_record, limit_chunk = page_limit)
cc$get(query = query_list)
if(verbose) cat("Paginated URLs:\n", paste0(cc$url_fetch(),"\n"))
txt <- cc$parse("UTF-8")
parse_response(txt, as_spatial)
}