R/rfca_naefs_parameters.R

Defines functions rfca_naefs_parameters

Documented in rfca_naefs_parameters

#' rfca_naefs_parameters
#' @description Retrieves forecast from Environment Canada for
#' specified site(s)
#' @return Data frame containing metadata for parameters included in NAEFS forecast. Fields returned are
#' \item{parameter}{The parameter short code, can be used with `rfca_naefs_forecast()`}
#' \item{parameter_desc}{Short text description of parameter (English)}
#' \item{parameter_desc_fr}{Short text description of parameter (French)}
#' \item{units}{The units for the parameter}
#' @export
#'

rfca_naefs_parameters <- function(){
  base_url <- "http://dd.weatheroffice.gc.ca/ensemble/doc/naefs/xml/elements.xml"

  params_raw <- xml2::read_xml(base_url)

  param_dat <- lapply(1:length(xml2::xml_children(params_raw)), function(x) {
    current_child <- xml2::xml_children(params_raw)[[x]]

    # Construct data frame
    data.frame(
      parameter = xml2::xml_attr(current_child, "code"),
      parameter_desc = xml2::xml_attr(current_child, "title_english"),
      parameter_desc_fr = xml2::xml_attr(current_child, "titre_francais"),
      units = xml2::xml_attr(current_child, "unit_english"),
      stringsAsFactors = FALSE
    )
  })

  param_dat <- do.call(rbind, param_dat)

  return(param_dat)
}
rywhale/rforecastca documentation built on May 4, 2019, 7:38 a.m.