'Accessing historical weather model data with rNOMADS

I am in interested in accessing historical NOAA model data, and have been using the rNOMADS package in R.

Most tutorials on this package focus on current data, for example this excellent one:

https://bovineaerospace.wordpress.com/2014/05/28/downloading-weather-sea-ice-and-wave-model-data-with-the-rnomads-package-in-r/

library(rNOMADS)
#A location near my house
lat <- 35.828304
lon <- -79.107467

#Find the latest Global Forecast System model run
model.urls <- GetDODSDates("gfs_0p50")
latest.model <- tail(model.urls$url, 1)
model.runs <- GetDODSModelRuns(latest.model)
latest.model.run <- tail(model.runs$model.run, 1)

#Figure out which forecasts to use
forecast.date <- as.POSIXlt(Sys.time(), tz = "UTC") 
abbrev <- "gfs_0p50"
## Not run:
forecasts <- GetClosestForecasts(abbrev = abbrev, forecast.date)

#Get nearest model nodes
lons <- seq(0, 359.5, by = 0.5)
lats <- seq(-90, 90, by = 0.5)
lon.diff <- abs(lon + 360 - lons)
lat.diff <- abs(lat - lats)
model.lon.ind <- which(lon.diff == min(lon.diff)) - 1 #NOMADS indexes at 0
model.lat.ind <- which(lat.diff == min(lat.diff)) - 1

#Subset model
time <- c(0,0) #Model status at initialization
lon.inds <- c(model.lon.ind - 2, model.lon.ind + 2)
lat.inds <- c(model.lat.ind - 2, model.lat.ind + 2)
variables <- c("ugrd10m", "vgrd10m") #E-W and N-S wind

wind.data <- DODSGrab(latest.model, latest.model.run, variables,
                      time, lon.inds, lat.inds)

profile <- BuildProfile(wind.data, lon, lat, spatial.average = TRUE, points = 4)

#Present results!
print(paste("At", profile[[1]]$forecast.date, "the East-West winds at Briar Chapel were going", sprintf("%.2f", profile[[1]]$profile.data[1, which(profile[[1]]$variables == "ugrd10m"), 1]),
            "meters per second, and the north-south winds were going", sprintf("%.2f", profile[[1]]$profile.data[1, which(profile[[1]]$variables == "vgrd10m"), 1]),
            "meters per second."))

This works fine on current model data. But I'd like historical data for a specific date and location

I tried to use the function "GetClosestForcasts" which seems to be what I need to do for historical data. But I get an error when running this code straight out of the vignette:

#Figure out which forecasts to use
forecast.date <- as.POSIXlt(Sys.time(), tz = "UTC") 
abbrev <- "gfs_0p50"
## Not run:
forecasts <- GetClosestForecasts(abbrev = abbrev, forecast.date)


Error in stringr::str_match_all(url.to.use, "\\d{10}")[[1]][1, 1] : 
  subscript out of bounds

Ideally, what I would like to do would be give both of these specific dates

forecast.date <- as.POSIXlt("2005-05-05", tz = "UTC")
forecast.date <- as.POSIXlt("2017-05-05", tz = "UTC")

And be able to run the same example as above for these dates and location, returning surface model data wind speed, temperature, and precipitation



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source