# Accessing ensemble weather models with rNOMADS

November 24, 2016
By

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Downloading a weather model and using it to predict stuff is all well and good – but what are the sources of error?  Specifically, how accurate is the atmospheric parametrization?   Luckily, NOMADS provides sets of models with slightly different initialization values.  The result is a set of realizations that captures the variability of the model product.  Here’s how to get that product, and examples of what you can do with it.

```library(rNOMADS)

#Get the latest ensemble model run
model.urls &lt;- GetDODSDates("gens")
latest.model &lt;- tail(model.urls\$url, 1)
model.runs &lt;- GetDODSModelRuns(latest.model)
model.run &lt;- tail(model.runs\$model.run[grepl("all",
model.runs\$model.run)], 1)

#Define region of interest: Chapel Hill, NC
lon &lt;- -79.052104
lat &lt;- 35.907553

#Set up DODS model grid indices
#Use GetDODSModelRunInfo() to figure out how to do this for other products
lons &lt;- seq(0, 359, by = 1)
lats &lt;- seq(-90, 90, by = 1)

lon.diff &lt;- abs(lon + 360 - lons)
lat.diff &lt;- abs(lat - lats)

model.lon.ind &lt;- which(lon.diff == min(lon.diff)) - 1
model.lat.ind &lt;- which(lat.diff == min(lat.diff)) - 1

#Analysis(?) model only
time &lt;- c(0, 0)
#Longitude grid
node.lon  &lt;- c(model.lon.ind - 2, model.lon.ind + 2)
#Latitude grid
node.lat  &lt;- c(model.lat.ind - 2, model.lat.ind + 2)
#Temperature, wind speeds, and geopotential height
variables &lt;- c("tmpprs", "ugrdprs", "vgrdprs", "hgtprs")
levels    &lt;- c(0, 25) #All available levels
ensemble &lt;- c(0, 20)  #All available ensembles

model.data &lt;- DODSGrab(latest.model, model.run, variables,
time, node.lon, node.lat, levels = levels, ensemble = ensemble)

#Plot temperature variability
plot(c(-65, 25), c(0, 35), xlab = "Temperature (C)",
ylab = "Geopotential Height (km)", type = 'n')

for(k in ((ensemble:ensemble + 1))) {
model.data.sub &lt;- SubsetNOMADS(model.data, ensemble = c(k),
variables = c("hgtprs", "tmpprs"))
profile &lt;- BuildProfile(model.data.sub, lon, lat)
points(profile[]\$profile.data[,
which(profile[]\$variables == "tmpprs"), 1] - 272.15,
profile[]\$profile.data[,
which(profile[]\$variables == "hgtprs"), 1] / 1000)
}

#Plot winds
zonal.wind &lt;- NULL
merid.wind &lt;- NULL
height     &lt;- NULL

for(k in ((ensemble:ensemble + 1))) {
ensemble = c(k), variables = c("hgtprs",
"ugrdprs", "vgrdprs"))
profile &lt;- BuildProfile(model.data.sub, lon, lat)
hgt     &lt;- profile[]\$profile.data[,
which(profile[]\$variables == "hgtprs"),]
ugrd    &lt;- profile[]\$profile.data[,
which(profile[]\$variables == "ugrdprs"),]
vgrd    &lt;- profile[]\$profile.data[,
which(profile[]\$variables == "vgrdprs"),]

synth.hgt &lt;- seq(min(hgt),
max(hgt), length.out = 1000)
ugrd.spline &lt;- splinefun(hgt, ugrd, method = "natural")
vgrd.spline &lt;- splinefun(hgt, vgrd, method = "natural")
zonal.wind[[k]] &lt;- ugrd.spline(synth.hgt)
merid.wind[[k]] &lt;- vgrd.spline(synth.hgt)
height[[k]] &lt;- synth.hgt
}

dev.new()
PlotWindProfile(zonal.wind, merid.wind, height, lines = TRUE,
points = FALSE, elev.circles = c(0, 15000, 30000),
elev.labels = c(0, 15, 30), radial.lines = seq(45, 360, by = 45),
colorbar = TRUE, invert = FALSE,
point.cex = 2, pch = 19, lty = 1, lwd = 1,
height.range = c(0, 30000), colorbar.label = "Wind Speed (m/s)")

```

Here’s atmospheric profiles from GFS ensemble runs on November 24, 2016 for Chapel Hill, NC. Temperature is quite variable at low altitudes (several degrees Celsius) and at 5 km above sea level (a couple degrees). Ensemble runs are more consistent for the rest of the atmosphere. Temperature profile above the Geology Department at UNC Chapel Hill for November 24, 2016 at 18:00 UTC.

Wind speeds are pretty consistent, at least at the resolution of the color scale I’m using.  Wind direction varies by a few degrees below 15 km, but becomes quite a bit less reliable between 15 and 30 km altitude.  This is one reason that our solar balloon trajectories are so hard to predict.  The other reason is because we still don’t really understand the physics of solar balloon flight, but that’s a subject for another post.   R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.