Im sure this can be optimised but give this Base R solution a try (note this is not tested due to the size of files):
# Store the parent url of files we want to download:
# base_url => character scalar
base_url <- "https://www.star.nesdis.noaa.gov/pub/corp/scsb/wguo/data/Blended_VH_4km/geo_TIFF/"
# Read in the html: html_string => character vector
html_string <- readLines(
base_url
)
# Store the range of years to search for,
# flattened into a regex pattern:
# rng => character scalar
rng <- paste0(
seq(
1981,
2011,
by = 1
),
collapse = "|"
)
# Create a vector of urls requiring download:
# input_urls => character vector
input_urls <- Filter(
function(y){
grepl(
rng,
y
)
},
unlist(
lapply(
strsplit(
html_string,
"href\\="
),
function(x){
ir <- paste0(
base_url,
unlist(
strsplit(
gsub(
'^"',
'',
gsub(
"(\\w+VCI\\.tif).*",
"\\1",
noquote(x)
)
),
"\\<\\/td\\>\\<td\\>\\<a"
)
)
)[2]
}
)
)
)
# Store the desired output folder here:
# dir_path => character scalar
dir_path <- paste0(
getwd(),
"/geo_tifs"
)
# Function to create a directory if it doesnt already
# exist: r_mkdir => function()
r_mkdir <- function(dir_path){
if(dir.exists(dir_path)){
invisible()
}else{
dir.create(dir_path)
}
}
# Create the directory if it doesnt already exist:
# directory => stdout(file system)
r_mkdir(dir_path)
# Generate a character vector of output file paths:
# output_file_paths => character vector
output_file_paths <- vapply(
strsplit(tmp, "geo_TIFF"),
function(x){
paste0(dir_path, x[2])
},
character(1)
)
# Download the files to the output paths:
# .tif files => stdout(file path)
lapply(
seq_along(output_file_paths),
function(i){
download.file(
input_urls[i],
output_file_paths[i],
quiet = TRUE,
cacheOK = FALSE
)
}
)