examples/AU_runoff_QLD.R

library(xml2)

p <- GET("https://water-monitoring.information.qld.gov.au/wgen/sites.rs.anon.xml") %>% content()
xs <- xml_find_all(p, "//site")
info <- map(
  c("station", "grpkeys", "grpvals", "grpvalsdesc", "latdec", "lngdec", "shortname", "stname") %>% set_names(., .),
  ~ xml_attr(xs, .x)
) %>% as.data.table()

sites <- info$station
outdir <- "Runoff_QLD"

temp <- foreach(site = sites, i = icount()) %do% {
  runningId(i)
  outfile <- glue("{outdir}/{site}_20210822.zip")

  if (!file.exists(outfile)) {
    tryCatch(
      {
        url <- glue("https://water-monitoring.information.qld.gov.au/cgi/webhyd.pl?co={site}&t=rscf_org&v=140.00_140.00_ATQ&vn=Stream%20Discharge&ds=ATQ&p=All%20data,18000101000000,18000101000000,period,1&pp=&r=rate&o=Download,download&i=Daily,Day,1&catid=rs&{timestamp()}")
        p <- GET(url) %>% content()
        link <- xml_find_first(p, "//p") %>%
          xml_text() %>%
          fromJSON() %>%
          .[[1]]

        outfile <- paste0(outdir, "/", basename(link))
        download.file(link, outfile, mode = "wb")
      },
      error = function(e) {
        message(sprintf("%s", e$message))
      }
    )
  }
}

## 2. closed
p <- GET("https://water-monitoring.information.qld.gov.au/wgen/sites.hd.anon.xml") %>% content()
xs <- xml_find_all(p, "//site")
info <- map(
  c("station", "grpkeys", "grpvals", "grpvalsdesc", "latdec", "lngdec", "shortname", "stname") %>% set_names(., .),
  ~ xml_attr(xs, .x)
) %>% as.data.table()


return_fields <- c(
  "station_name",
  "station_longname",
  "station_no",
  "station_id",
  "station_latitude",
  "station_longitude",
  "station_carteasting",
  "station_cartnorthing",
  "stationparameter_name",
  "station_georefsystem",
  "catchment_no",
  "catchment_id",
  "catchment_name",
  "site_no",
  "site_id",
  "site_name",
  "parametertype_id",
  "parametertype_name",
  "object_type",
  "custom_attributes"
)

info <- get_station_list("Water Course Discharge", return_fields = return_fields)
rpkgs/curlR documentation built on Jan. 30, 2023, 7:26 p.m.