Skip to content

Instantly share code, notes, and snippets.

@mbjoseph
Created July 25, 2016 20:25
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 mbjoseph/53f758eec42ef46dccf7037b7b5f5168 to your computer and use it in GitHub Desktop.
Save mbjoseph/53f758eec42ef46dccf7037b7b5f5168 to your computer and use it in GitHub Desktop.
# Script to get MCD14ML MODIS data
library(RCurl)
library(dplyr)
# fetch_MCD14ML() downloads all of the zipped data files for the
# MODIS MCD14ML data product
#
# args:
# dir: the name of a directory to store the zip files (string)
# overwrite: should existing data be overrwitten? (logical)
#
# returns:
# none, but downloads all of the zip files in "dir"
#
# example:
# fetch_MCD14ML('mcd_test')
fetch_MCD14ML <- function(dir, overwrite = FALSE) {
if (!dir.exists(dir)) {
dir.create(dir, recursive = TRUE)
}
ftp_prefix <- 'ftp://fire:burnt@fuoco.geog.umd.edu/modis/C6/mcd14ml/'
# make vector of filenames on ftp server
files <- getURL(ftp_prefix, dirlistonly = TRUE) %>%
strsplit(split = '\n') %>%
unlist()
# make ftp paths to each file
to_download <- lapply(files, FUN = function(x) paste0(ftp_prefix, x)) %>%
unlist()
# download each
destination_files <- file.path(dir, files)
pb <- txtProgressBar(max = length(destination_files))
for (i in seq_along(destination_files)) {
if (!file.exists(destination_files[i]) | overwrite) {
download.file(to_download[i], destination_files[i])
}
setTxtProgressBar(pb, i)
}
close(pb)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment