Prompted by both my own struggles with wgrib2 compilation and a plea on the rNOMADS email listserv, I’m going to describe how to compile and install wgrib2 on Mac OS.
First of all, some background: wgrib2 is an excellent utility written by Wesley Ebisuzaki at NOAA. It allows for a number of swift and stable operations on GRIB2 files (a common file format for weather and climate data). It is also a requirement for using grib files in rNOMADS (the function ReadGrib() in particular).
So here’s how to install it on Mac OS.
- Check to see if you have the right prerequisites installed. These are Command Line Tools for Xcode and
gcc. Not sure if you have them? Here’s how to find out:- Open a command prompt and type
gcc. You will see eithergcc: fatal error: no input filesor something else. If you see something else likeclang: fatal error: no input files, then gcc is not installed, and probably other things are missing too. Keep reading! - OK, so gcc is not installed. Install Command Line Tools for Xcode via these instructions.
- In order to get gcc, we also have to get homebrew. Install homebrew via these instructions.
- Now use homebrew to install gcc. Open a terminal and type
brew install gcc - Type
ls /usr/local/bin. Then look for gcc typealias gcc=gcc-Xwhere X is the number at the end of gcc (likegcc-7or something. - Now type
gcc. You should now seegcc-X: fatal error: no input files. If so, you’re good to go.
- Open a command prompt and type
- Download wgrib2 here (note download links are pretty far down the page).
- Untar the tarball somewhere, and roll up your sleeves.
cdinto the resultingwgribdirectory. - In the makefile, uncomment the lines
Also search for
#export cc=gcc
#export FC=gfortran
makefile.darwinin the makefile and uncomment the line containing it. Then be sure to comment out the following line so that the variable doesn’t get overwritten. You’ll see instructions to this effect in the makefile anyway. - Now we have to edit the included
libpngpackage, since it is untarred by the makefile and doesn’t inherit our compiler specifications in step 4. Ensuring that we’re in thewgribdirectory:
tar -xvf libpng-1.2.57.tar.gz
cd libpng-1.2.57/scripts/
now edit themakefile.darwinfile, changing
CC=cc
to
CC=gcc
Now, return to thewgribdirectory, and re-tar libpng!
tar -cf libpng-1.2.57.tar libpng-1.2.57
gzip libpng-1.2.57.tar
If it asks you if you want to replace the original tar.gz file, say “yes”. What we’ve done here is editedlibpngto make sure it uses the right compiler. - Finally, type
maketo build wgrib2.
If you are still having problems (for example, libaec complaining that there is no c compiler), make sure that all the compiler commands (gcc, cc, etc.) all point to something other than clang (the default compiler that comes with OSX). You may have to edit your bash_profile file to ensure this.
As always, contact me on the form below if you’re having unresolvable issues.















