0

I'm trying to download a bunch of NetCDF files that contain daily data using R. Because I need data for multiple years, I wrote a loop to download the files but I am getting the message that only the first element of 'destfile' argument is used and only the first file is being downloaded.

file_base <- paste0("https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/access/avhrr-only/199801/")

yrs=c("1998")
mon=c("01")
day=("01","02","03")

for (y in yrs){
    for (m in mon){
        for (d in day){
            ymd <- paste0(yrs,mon,day)
            fn_url <- paste0 (file_base,"avhrr-only-v2.",ymd,".nc")
            fn <- paste0("avhrr-only-v2",ymd,".nc")
    download.file(url=paste0(file_base), destfile=fn, method="auto", quiet=TRUE, mode="wb")
        }
    }
}

I've looked at Download multiple files using "download.files" function and the two others hyperlinked within that thread, but I still can't figure out why I'm getting the warning message and how to get R to download multiple files. I'm still somewhat new to R so any help/tips would be great. Thank you!

Alex
  • 3
  • 2
  • 2
    `ymd <- paste0(yrs,mon,day)` should be `ymd <- paste0(y, m, d)`. It's less confusing to make the filenames before iterating, though, which makes this easier to translate to `lapply` or `Map`, should you later want to make a list of data frames – alistaire Jan 14 '19 at 05:31

1 Answers1

0
file_base <- "https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/access/avhrr-only/199801/avhrr-only-v2."
dates <- paste0("199801", sapply(as.character(1:31), function(x) if(nchar(x) == 1) paste0(0, x) else x, simplify = T))

for(date in dates){
  download.file(url = paste0(file_base, date, ".nc"),
                destfile = paste0("avhrr-only-v2", date, ".nc"),
                method = "auto", 
                quiet = T,
                mode = "wb")
}
Brigadeiro
  • 2,649
  • 13
  • 30
  • What does function(x) do? And does nchar(x) == 1 mean if the date has only one digit then add a 0 in front? – Alex Jan 15 '19 at 06:15
  • Yes, exactly. See ?sapply for information on the function(x) part (in short, it tells R what to do to teach element of the vector 1:31. – Brigadeiro Jan 16 '19 at 17:21