Created
August 16, 2019 23:52
-
-
Save ryan-hill/9fbb5e305643979fbdf92967aeadb1c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#FTP location | |
ftpdir <- 'ftp://newftp.epa.gov/EPADataCommons/ORD/NHDPlusLandscapeAttributes/StreamCat/States/' | |
#Your output directory | |
out_dir <- 'D:/Lixo/' | |
#Desired table (change to name of desired table) | |
tables <- c('NLCD2001RipBuf100_CA','Kffact_CA','ImperviousSurfaces2001_CA') | |
#Get URL, split returned list, select out only desired tables by name ('table' above) | |
library(RCurl) | |
url_list <- getURL(ftpdir, dirlistonly = TRUE) | |
url_list <- strsplit(url_list, split = '\r\n')[[1]] | |
url_list <- url_list[grep(paste(tables,collapse="|"), url_list)] | |
#Loop through files on FTP, download, and write out as .csv | |
for(i in 1:length(url_list)){ | |
print(url_list[i]) | |
temp <- tempfile() | |
download.file(paste0(ftpdir, url_list[i]), temp) | |
#replace .zip with .csv in file name | |
csv_file <- gsub('.zip', '.csv', url_list[i]) | |
#read in csv | |
tmp_metric <- read.csv(unz(temp, csv_file)) | |
print(head(tmp_metric)) | |
write.csv(tmp_metric, paste0(out_dir, csv_file), row.names = F) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment