J. Win.
J. Win.

Reputation: 6771

Recursively ftp download, then extract gz files

I have a multiple-step file download process I would like to do within R. I have got the middle step, but not the first and third...

# STEP 1 Recursively find all the files at an ftp site 
# ftp://prism.oregonstate.edu//pub/prism/pacisl/grids
all_paths <- #### a recursive listing of the ftp path contents??? ####

# STEP 2 Choose all the ones whose filename starts with "hi"
all_files <- sapply(sapply(strsplit(all_paths, "/"), rev), "[", 1)
hawaii_log <- substr(all_files, 1, 2) == "hi"
hi_paths <- all_paths[hawaii_log]
hi_files <- all_files[hawaii_log]

# STEP 3 Download & extract from gz format into a single directory
mapply(download.file, url = hi_paths, destfile = hi_files)
## and now how to extract from gz format?

Upvotes: 3

Views: 7686

Answers (3)

Martin Morgan
Martin Morgan

Reputation: 46876

For part 1, RCurl might be helpful. The getURL function retrieves one or more URLs; dirlistonly lists the contents of the directory without retrieving the file. The rest of the function creates the next level of url

library(RCurl)
getContent <- function(dirs) {
    urls <- paste(dirs, "/", sep="")
    fls <- strsplit(getURL(urls, dirlistonly=TRUE), "\r?\n")
    ok <- sapply(fls, length) > 0
    unlist(mapply(paste, urls[ok], fls[ok], sep="", SIMPLIFY=FALSE),
           use.names=FALSE)
}

So starting with

dirs <- "ftp://prism.oregonstate.edu//pub/prism/pacisl/grids"

we can invoke this function and look for things that look like directories, continuing until done

fls <- character()
while (length(dirs)) {
    message(length(dirs))
    urls <- getContent(dirs)
    isgz <- grepl("gz$", urls)
    fls <- append(fls, urls[isgz])
    dirs <- urls[!isgz]
}

we could then use getURL again, but this time on fls (or elements of fls, in a loop) to retrieve the actual files. Or maybe better open a url connection and use gzcon to decompress and process on the file. Along the lines of

con <- gzcon(url(fls[1], "r"))
meta <- readLines(con, 7)
data <- scan(con, integer())

Upvotes: 7

Chao Zhong
Chao Zhong

Reputation: 31

ftp.root <- where are the files
dropbox.root <- where to put the files

#=====================================================================
#   Function that downloads files from URL
#=====================================================================

fdownload <- function(sourcelink) { 

  targetlink <- paste(dropbox.root, substr(sourcelink, nchar(ftp.root)+1, 
nchar(sourcelink)), sep = '')

  # list of contents
  filenames <- getURL(sourcelink, ftp.use.epsv = FALSE, dirlistonly = TRUE)
  filenames <- strsplit(filenames, "\n")
  filenames <- unlist(filenames)

  files <- filenames[grep('\\.', filenames)]  
  dirs <- setdiff(filenames, files)
  if (length(dirs) != 0) {
    dirs <- paste(sourcelink, dirs, '/', sep = '')
  }  

  # files
  for (filename in files) {

    sourcefile <- paste(sourcelink, filename, sep = '')
    targetfile <- paste(targetlink, filename, sep = '')

    download.file(sourcefile, targetfile)
  }

  # subfolders
  for (dirname in dirs) {

    fdownload(dirname)
  }
}

Upvotes: 3

mdsumner
mdsumner

Reputation: 29525

I can read the contents of the ftp page if I start R with the internet2 option. I.e.

C:\Program Files\R\R-2.12\bin\x64\Rgui.exe --internet2

(The shortcut to start R on Windows can be modified to add the internet2 argument - right-click /Properties /Target, or just run that at the command line - and obvious on GNU/Linux).

The text on that page can be read like this:

 download.file("ftp://prism.oregonstate.edu//pub/prism/pacisl/grids", "f.txt")
 txt <- readLines("f.txt")

It's a little more work to parse out the Directory listings, then read them recursively for the underlying files.

## (something like)
dirlines <- txt[grep("Directory <A HREF=", txt)]

## split and extract text after "grids/"
split1 <- sapply(strsplit(dirlines, "grids/"), function(x) rev(x)[1])

## split and extract remaining text after "/"
sapply(strsplit(split1, "/"), function(x) x[1])
[1] "dem"    "ppt"    "tdmean" "tmax"   "tmin"  

It's about here that this stops seeming very attractive, and gets a bit laborious so I would actually recommend a different option. There would no doubt be a better solution perhaps with RCurl, and I would recommend learning to use and ftp client for you and your user. Command line ftp, anonymous logins, and mget all works pretty easily.

The internet2 option was explained for a similar ftp site here:

https://stat.ethz.ch/pipermail/r-help/2009-January/184647.html

Upvotes: 5

Related Questions