#' download_CLASS
#' Adapted from Sean's much more elegant batch_processing script.
#' 
#'Used for routine acquisition of GOES AOD ann ADP files from a CLASS subscription. The function
#' (really a script) will open an ftp location, find all of the netcdf FDC files,
#' and downlad all of those files that are not already found in the download_path. 
#'
#' @param url Subscription FTP location (e.g.,
#'   "ftp://ftp.class.ncdc.noaa.gov/sub/sraffuse1/53704/")
#' @param download_path Local path where netcdf files are stored
#' 
#' @examples download_CLASS_AOD_ADP("ftp://ftp.class.ncdc.noaa.gov/sub/susan.m.one1/53704/",
#'  "/Users/susan/GOES-16/CLASS/Download_CLASS/")
#url <- "ftp://ftp.class.ncdc.noaa.gov/sub/susan.m.one1/53704/"
#download_path <- "/Users/susan/GOES-16/CLASS/Download_CLASS/"
#download_CLASS_AOD_ADP(url, download_path)

download_CLASS_AOD_ADP <- function(url, download_path) {

  # Set the type of files to download.
  # AOD for Full Disk (F), CONUS (C), mesoscale domain 1 (M1), mesoscale domain 2 (M@).
  # ADP for Full Disk (F), CONUS (C), mesoscale domain 1 (M1), mesoscale domain 2 (M@).
  #type = c("AODF", "AODC", "AODM1", "AODM2", "ADPF", "ADPC", "ADPM1", "ADPM2")
  type = c("AODC")
  filenames <- RCurl::getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)
  filenames <- unlist(strsplit(filenames, "\n"))
  files <- filenames[grepl(glob2rx("*.nc"), filenames)] # ncdf files only

  # loop through each of the file types.
  for (myp in 1:length(type)){

    files_type <- files[grepl(type[myp], files)]
    download_path_type <- paste(download_path, type[myp], "/", sep = "")
    for(filename in files_type){
      # Only process the file if it doesn't exist locally (we've already done those)
      if (file.exists(paste(download_path_type, filename, sep = ""))) {
        print(paste0("Skipping ", filename))
      } else {
        print(paste0("Processing ", filename))
        download.file(paste0(url, filename), paste0(download_path_type, filename), mode = "wb",
                      quiet = TRUE)
      }  
    }
  }
}
