Skip to content

Instantly share code, notes, and snippets.

@program--
Created December 20, 2020 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save program--/5e614ed6335e74ad392a3af2502bd14f to your computer and use it in GitHub Desktop.
Save program--/5e614ed6335e74ad392a3af2502bd14f to your computer and use it in GitHub Desktop.
Download zipped HUC 6-digit HAND packages from CFIM Data Repository
#' Download from CFIM
#' Download zipped HUC 6-digit HAND packages from CFIM Data Repository - https://cfim.ornl.gov/data/
#' @param huc6_codes character vector of HUC 6-digit codes, or "all" to download all available HUC 6-digit zip files.
#' @param dir character of directory path.
#' @param overwrite Overwrite already downloaded zip files? Defaults to FALSE.
#' @return No explicit return. Unzipped HUC 6-digit HAND packages can be found in `dir`
#' importsFrom RCurl getURL
#' importsFrom stringr str_extract_all
#' importsFrom magrittr %>%
download_hand_cfim <- function(huc6_codes = "all", dir, overwrite = FALSE, ...) {
# Create directory if it doesn't exist
if (!dir.exists(dir)) dir.create(dir)
# Get vector of HUC6 codes
all_huc6_codes <- scan("https://cfim.ornl.gov/data/list.HUC6.txt",
character(),
quote = "")
# Get all downloadable HUC6 codes via RCurl and subset
all_huc6_codes <- RCurl::getURL("https://cfim.ornl.gov/data/HAND/20200601/") %>%
stringr::str_extract_all("<a [^>]+>(.*?)</a>") %>%
sapply(substr, start = 22, stop = 27)
# Cast to vector and remove misc data
all_huc6_codes <- all_huc6_codes[, 1][-c(1:5)]
if (huc6_codes == "all") huc6_codes <- all_huc6_codes
# Don't redo ones already downloaded
if (!overwrite) {
huc6_codes <- huc6_codes[
!(huc6_codes %in% unname(sapply(
list.files("data/pull/"),
substr,
start = 0,
stop = 6
)))
]
}
# All HUC6 codes in "huc6_codes"
for (huc6_code in huc6_codes) {
if (!(huc6_code %in% all_huc6_codes)) {
warning(huc6_code, " not downloadable. Ensure it is valid. Skipping...")
next
}
# Create temp file
temp <- tempfile(
pattern = paste0(huc6_code, "-"),
fileext = ".zip",
tmpdir = dir
)
tryCatch({
# Download package from CFIM Data Repository
download.file(
paste0(
"https://cfim.ornl.gov/data/HAND/20200601/",
huc6_code,
".zip"
),
temp,
method = "libcurl"
)
unzip(temp, exdir = dir)
unlink(temp)
},
error = function(cond) {
message("Error at HUC6: ", huc6_code)
stop(cond)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment