Just about everyone is familiar with weather maps. There are many situations where it is useful to combine the underlying numerical weather data with other types of information. Accessing the weather data is a necessary first step.
The output from the U.S. National Centers for Environmental Prediction (NCEP) Global Forecast System (GFS) is freely available. The surface resolution of the model is ≈ 0.3º× 0.3°. The model runs every 6 hours, producing forecasts at 3-hourly intervals extending out to 16 days. As an example of output from GFS, the map (below) shows the predicted average temperature at 2 metres over the entire globe for the next 24 hr (date of this post). The map shows predicted cold conditions in Europe, and the continuing heatwave in Australia.
How the map was made
GFS forecasts are in a format called GRIB2. According to Wikipedia, “GRIB (GRIdded Binary) is a mathematically concise data format commonly used in meteorology to store historical and forecast weather data.” GRIB files contain physical fields such as temperature, humidity etc defined on a spatial grid, as well as boundary conditions such as vegetation type and elevation. The data might be assimilated from observations, or output from a forecast model.
The first step is to translate the GRIB into a raster format such as netcdf which can be read in R. For example, the GRIB2 file gfs.2009121700/gfs.t00z.sfluxgrbf03.grib2 contains the 3-hr forecast surface data on 17 Dec 2009 produced at 00 UTC (midnight universal time). An inventory of the data contained in this file can be seen here. Download this forecast as temp.grb
loc=file.path("ftp://ftp.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.2009121700/gfs.t00z.sfluxgrbf03.grib2")
download.file(loc,"temp.grb",mode="wb")
To read temp.grb a utility called wgrib2 needs to be installed on your system. Then data such as land fraction can extracted into a netcdf file LAND.nc using the R shell command
shell("wgrib2 -s temp03.grb | grep :LAND: | wgrib2 -i temp00.grb -netcdf LAND.nc",intern=T)
The ncdf package can now be used to read the contents of LAND.nc.
library(ncdf)
landFrac <-open.ncdf("LAND.nc")
land <- get.var.ncdf(landFrac,"LAND_surface")
x <- get.var.ncdf(landFrac,"longitude")
y <- get.var.ncdf(landFrac,"latitude")
The 1152×576 matrix land takes values 1 for land and 0 for water (sea-ice is 1). x and y are the longitude and latitude of the non-uniform GFS grid.
2m temperature data can be read in the same way. The average of the first 8 forecasts was called t2m.mean and plotted using image.plot() from the fields package:
library(fields)
rgb.palette <- colorRampPalette(c("snow1","snow2","snow3","seagreen","orange","firebrick"), space = "rgb")#colors
image.plot(x,y,t2m.mean,col=rgb.palette(200),axes=F,main=as.expression(paste("GFS 24hr Average 2M Temperature",day,"00 UTC",sep="")),axes=F,legend.lab="o C")
contour(x,y,land,add=TRUE,lwd=1,levels=0.99,drawlabels=FALSE,col="grey30") #add land outline
Great post Joe!
I’ve had some fun trying to get install some of the libraries from source until I realised MacPorts made everything so much easier!
Here’s my modified version based on your example – http://braz.blogspot.com/2009/12/r-for-global-climate-forecasting.html
Merry Christmas and let me know if you are around town again as I’ve heard that LIT might still have a few places in their LEAP programme, which starts a new intake in February.
Very clear. Fantastic!
I tried to create an automated system to publish on the website of amateur weather station of Alba (CN) Italy (http://www.meteoalba.com/wxgfs.php).
I have also taken some advice from blog Braz (thanks Eoin Brazil) …
thanks and happy holidays!
Thank you for this wonderful presentation.
Hi
Thank you for your code. I tried to use wgrib script as you post . However, there was error when run in R but it worked in dos prompt.
This is my script
cmd = paste(wgrib 20090416_0000.grb | grep \”:UGRD:\” | wgrib 20090416_0000.grb -i -V -text -o wgrib.output”)
Status = system(cmd,intern=T)
Output
Status
[1] “argument: | ????”
[2] “argument: grep ????”
[3] “argument: :UGRD: ????”
[4] “argument: | ????”
[5] “argument: wgrib ????”
[6] “argument: 20090416_0000.grb ????”
I don’t know the details of your setup. Try replacing “wgrib” by the correct absolute path e.g. something like C:/Progra~1/wgrib/bin/wgrib.exe
regards
The latest posting above was from 2013. The ncep ftp site has data file from 2014. What grib2 data file should I use. I tried using any .grib2 file but the wgrib2 returns a status of 123. Help from anyone would be much appreciated.
Eddie
You have a minor typo in the command:
shell(“wgrib2 -s temp03.grb | grep :LAND: | wgrib2 -i temp00.grb -netcdf LAND.nc”,intern=T)
Should be:
shell(“wgrib2 -s temp03.grb | grep :LAND: | wgrib2 -i temp03.grb -netcdf LAND.nc”,intern=T)
This piece of writing presents clear idea designed for the
new visitors of blogging, that really how to do running a blog.
Incredible! This blog looks exactly like mmy old one!
It’s on a totally different subject but it hhas pretty mch thee same layout and design. Outstanding choice of colors!
Your post partly inspired me to develop rNOMADS – an R package that interfaces with NOMADS to quickly download and process weather data. Check it out and let me know what you think!
Hi, Im trying to follow your instructions but i miss in this part:
“To read temp.grb a utility called wgrib2 needs to be installed on your system. Then data such as land fraction can extracted into a netcdf file LAND.nc using the R shell command”
I don`t understand how to install wgrib2. I m using windows 7 64bits. Please give me a hand. Im not an expert in R. Im meteorologyst, and id like to make my own meteorological charts.
Thank you
Alejandro Pooli