How to install rNOMADS with GRIB file support on Windows

Two years ago, I wrote a software package for R called “rNOMADS” that interfaces with online weather and sea ice model repositories to gather data in real time, for free.  The data are delivered in two ways: a simple, pure R, cross platform interface using GrADS-DODS, and binary files in GRIB format.  The one issue with GRIB is that this format can’t be read directly into R; it requires the external program “wgrib2.”  Installing rNOMADS with GRIB support for Linux is covered in this post (Mac OS is probably similar).  I thought GRIB support for Windows was impossible until the guy who runs this blog casually told me he’d figured it out.  So, I finally got around to trying it myself, and I’m happy to say I got it to work!  Here’s how:

Step 1:

Download the most recent wgrib2.exe version and all DLL files from Wesley Ebisuzaki’s web site:  Click here.

Step 2:

Make a directory somewhere on your computer.   I chose:  C:\Program Files\wgrib2

Copy the .exe and the .dll files to this directory.

Step 3:

Append the directory path to the Windows PATH variable.  Find out how to do this here.

Edit: a user points out that sometimes this doesn’t work.  If not, change the path variable in R using Sys.setenv():

Sys.setenv("PATH" = "C:/Program Files/wgrib2")

 

Step 4:  If R is open, close and reopen it.  Then try the following command in the R interpreter:
system("wgrib2")

If you get a bunch of text that looks like the image below, you’ve succeeded.  You can start reading GRIB files with rNOMADS!

Output of wgrib2 call from R.

Output of wgrib2 call from R.

Advertisements

How to install rNOMADS with GRIB support on Linux

About 6 months ago, I wrote a package for the R programming language called rNOMADS.  It interfaces with the National Oceanic and Atmospheric Association’s Operational Archive and Distribution System (NOMADS), comprising everything from global weather forecast models, to high resolution regional models, to wave and sea ice models.  rNOMADS uses R to download requested data from the NOMADS servers using two different methods: direct ascii data retrieval and GRIB file retrieval.  GRIB is a binary file format that can’t be read into R directly – it requires external programs that must be installed separately. This post describes how to install rNOMADS with GRIB support, which is available for Linux systems only.  Windows and Mac users should use the ascii download routines; I will write a tutorial and post it shortly.

rNOMADS allows R users to make some spectacular pictures (see below) but I also hope that it will lead to greater availability of these valuable resources to data analysts all over the world.  Already, rNOMADS is being used in an online sailboat racing game and to get data for predicting solar and wind energy output.

World temperature at 2 m above ground, generated using rNOMADS.

World temperature at 2 m above ground, generated using rNOMADS.

Step 1

The first step is to make sure R is configured correctly. You can skip the reinstallation (step 2) if need be, but I recommend getting r-base-dev anyway for all the libraries it has.

su
apt-get install r-base-dev
apt-get build-dep r-base
exit

Step 2

Then, I build R from the source. You can skip this method if you’ve already done this.

You can download the latest version  here.Get the tar.gz file next to the bullet point that says “Latest Release.”
Navigate to the file and type:

tar -xvf R-3.0.3.tar.gz
cd R-3.0.3
./configure
make
su
make install
exit

Step 3

Now we get dependencies for specific packages that rNOMADS requires.

su
apt-get install libxml2-dev
apt-get install libcurl4-openssl-dev
apt-get install libboost-dev
exit

Step 4

Now, we install wgrib2 and wgrib and add them to our path.
Download wgrib2 here.
Download wgrib here.
Then:

tar -xvf wgrib2.tgz
cd grib2/
make
su
mv wgrib2/ /usr/local
exit

mkdir wgrib
mv wgrib.tar wgrib
cd wgrib
tar -xvf wgrib.tar
make
su
mkdir /usr/local/wgrib
mv * /usr/local/wgrib
exit


su
gedit /etc/environment

add “/usr/local/wgrib2” and “/usr/local/wgrib” to the end of the PATH variable, then close /etc/environment and close the superuser terminal.
Next:

source /etc/environment

Step 5

Now we install rNOMADS and another package called GEOmap so that we can build and test the example vignette.

su
R

Now we are in the R interpreter, as root. Type:

install.packages('rNOMADS')
install.packages('GEOmap')

Step 6

If everything went well, you now have rNOMADS! Test rNOMADS (and learn how it works) by building the example vignette. First, download the source here and navigate to the saved file. Then, type:

R CMD Sweave rNOMADS_grib_examples.Rnw
pdflatex rNOMADS_grib_examples.tex

The latest R release has been causing issues with Sweave; if you don’t see any figures in your PDF or if the document compilation fails, try running Sweave using the commands described here.

This should create a PDF file called “rNOMADS_grib_examples.pdf” with code and images. It should look similar to this one.