[R] Skip error in downloading file in loop

Miluji Sb milujisb at gmail.com
Sat Oct 21 17:01:02 CEST 2017


I am trying to download data from NASA web-service.

I am using the following code;

for( i in 1:8) {
  target1 <- paste0("
https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/access/timeseries.cgi?variable=NLDAS:NLDAS_FORA0125_H.002:TMP2m&location=GEOM:POINT(
",
                    cities[i, "lon_nldas"],
                    ",%20", cities[i,"lat_nldas"],

")&startDate=1980-01-01T00&endDate=2016-12-31T23&type=asc2")
  target2 <- paste0("/Users/dasgupta/Dropbox (FEEM)/Flu Paper/climate
data/temperature/nldas/",    # change for whatever destination directory
you may prefer.
                    cities[i,"city"], "_",
                    cities[i,"state"], ".csv")
  download.file(url=target1, destfile=target2,  method = "libcurl")
}

Any time the coordinates provided is out data coverage, the loop fails and
stops. Is there any way to force R to skip the error and continue with the
rest of the download?

My data looks like this:

dput(droplevels(head(cities, 8)))
structure(list(city = structure(1:8, .Label = c("Boston", "Bridgeport",
"Cambridge", "Fall River", "Hartford", "Lowell", "Lynn", "New Bedford"
), class = "factor"), state = structure(c(2L, 1L, 2L, 2L, 1L,
2L, 2L, 2L), .Label = c(" CT ", " MA "), class = "factor"), lon_nldas =
c(-71.05673836,
-73.19126922, -71.1060061, -71.14364822, -72.67401574, -71.31283992,
-70.82521832, -70.80586599), lat_nldas = c(42.35866236, 41.18188268,
42.36679363, 41.69735342, 41.76349276, 42.64588413, 42.46370197,
41.63767375)), .Names = c("city", "state", "lon_nldas", "lat_nldas"
), row.names = c(NA, 8L), class = "data.frame")

Any help will be appreciated. Thank you very much!

Sincerely,

Milu

	[[alternative HTML version deleted]]



More information about the R-help mailing list