Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. # Script to get MCD14ML MODIS data
  2. library(RCurl)
  3. library(dplyr)
  4.  
  5. # fetch_MCD14ML() downloads all of the zipped data files for the
  6. # MODIS MCD14ML data product
  7. #
  8. # args:
  9. # dir: the name of a directory to store the zip files (string)
  10. # overwrite: should existing data be overrwitten? (logical)
  11. #
  12. # returns:
  13. # none, but downloads all of the zip files in "dir"
  14. #
  15. # example:
  16. # fetch_MCD14ML('mcd_test')
  17.  
  18. fetch_MCD14ML <- function(dir, overwrite = FALSE) {
  19. if (!dir.exists(dir)) {
  20. dir.create(dir, recursive = TRUE)
  21. }
  22.  
  23. ftp_prefix <- 'ftp://fire:burnt@fuoco.geog.umd.edu/modis/C6/mcd14ml/'
  24.  
  25. # make vector of filenames on ftp server
  26. files <- getURL(ftp_prefix, dirlistonly = TRUE) %>%
  27. strsplit(split = '\n') %>%
  28. unlist()
  29.  
  30. # make ftp paths to each file
  31. to_download <- lapply(files, FUN = function(x) paste0(ftp_prefix, x)) %>%
  32. unlist()
  33.  
  34. # download each
  35. destination_files <- file.path(dir, files)
  36. pb <- txtProgressBar(max = length(destination_files))
  37. for (i in seq_along(destination_files)) {
  38. if (!file.exists(destination_files[i]) | overwrite) {
  39. download.file(to_download[i], destination_files[i])
  40. }
  41. setTxtProgressBar(pb, i)
  42. }
  43. close(pb)
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement