R/ahccd.R

Defines functions eccc_ahccd_data eccc_ahccd_stns

Documented in eccc_ahccd_data eccc_ahccd_stns

#' eccc_ahccd_stns
#'
#' @description Get Adjusted Homogenized Canadian Climate Data (AHCCD) station
#' metadata
#' @param query (Optional) List of queryables. This allows for more complicated
#' queries. See \code{eccc_queryables("ahccd-stations")}
#' @return \code{tibble} containing AHCCD station metadata
#' @export
#' @examples
#' eccc_ahccd_stns()
eccc_ahccd_stns <- function(query) {
  query_path <- "collections/ahccd-stations/items"

  # Init empty query list
  if (missing(query)) {
    query <- list()
  }

  req <- eccc_request(path = query_path, query = query)

  if (!length(req$content$features)) {
    stop("No data available for selected stations/dates.")
  }

  parsed_req <- eccc_paginate(req)

  dplyr::select(parsed_req, -type)
}

#' eccc_ahccd_data
#'
#' @description Get data for Adjusted Homogenized Canadian Climate Data (AHCCD)
#' stations
#' @param station_number AHCCD station identifier, see \code{eccc_ahccd_stns}
#' @param period One of "month" (default), "year", "season" or "trends"
#' @param query (Optional) List of queryables. This allows for more complicated
#' queries. Specifying `station_number` will override `query`.
#' See \code{eccc_queryables("ahccd-annual")}
#' @return \code{tibble} containing AHCCD values for dates
#' @export
#' @examples
#' eccc_ahccd_data(
#'   station_number = "3011120",
#'   period = "year"
#' )
eccc_ahccd_data <- function(station_number, period = "month", query) {
  check_missing <- c(
    missing(station_number),
    missing(query)
  )

  if (all(check_missing)) {
    stop("Must provide either ``station_number`` or ``query``")
  }

  if (!period %in% c("month", "year", "season", "trends")) {
    stop("`period` must be 'month', 'year', 'season' or 'trends'")
  }

  query_path <- switch(period,
    "month" = "collections/ahccd-monthly/items",
    "year" = "collections/ahccd-annual/items",
    "season" = "collections/ahccd-seasonal/items",
    "trends" = "collections/ahccd-trends/items"
  )

  # Init empty query list
  if (missing(query)) {
    query <- list()
  }

  # Add station number
  if (!missing(station_number)) {
    query[["station_id__id_station"]] <- paste(station_number, collapse = "/")
  }

  req <- eccc_request(path = query_path, query = query)

  if (!length(req$content$features)) {
    stop("No data available for selected stations/dates.")
  }

  parsed_req <- eccc_paginate(req)

  dplyr::select(parsed_req, -type)
}
rywhale/ecccgeometR documentation built on Dec. 22, 2021, 8:19 p.m.