How to install wgrib2 in OSX

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.

  1.  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:
    1. Open a command prompt and type gcc.  You will see eithergcc: fatal error: no input files or something else.  If you see something else like clang: fatal error: no input files, then gcc is not installed, and probably other things are missing too.  Keep reading!
    2. OK, so  gcc is not installed.  Install Command Line Tools for Xcode via these instructions.
    3. In order to get gcc, we also have to get homebrew.  Install homebrew via these instructions.
    4. Now use homebrew to install gcc.  Open a terminal and typebrew install gcc
    5. Type ls /usr/local/bin. Then look for gcc type alias gcc=gcc-X where X is the number at the end of gcc (like gcc-7 or something.
    6. Now type gcc. You should now see gcc-X: fatal error: no input files. If so, you’re good to go.
  2. Download wgrib2 here (note download links are pretty far down the page).
  3. Untar the tarball somewhere, and roll up your sleeves.  cd into the resulting wgrib directory.
  4.  In the makefile, uncomment the lines

    #export cc=gcc
    #export FC=gfortran
    Also search for makefile.darwin in 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.
  5. Now we have to edit the included libpng package, since it is untarred by the makefile and doesn’t inherit our compiler specifications in step 4. Ensuring that we’re in the wgrib directory:

    tar -xvf libpng-1.2.57.tar.gz
    cd libpng-1.2.57/scripts/

    now edit the makefile.darwin file, changing

    CC=cc

    to

    CC=gcc

    Now, return to the wgrib directory, 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 edited libpng to make sure it uses the right compiler.
  6. Finally, type make to 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.

Advertisements

4 thoughts on “How to install wgrib2 in OSX

  1. Pingback: How to install wgrib2 in OSX | A bunch of data

  2. Pingback: How to install wgrib2 in OSX – Mubashir Qasim

  3. Hi, would you please comment on the package’s capability in parsing the SOILW type of variables in GFS forecast (it has 4 layers: 0-0.1 m, 0.1-0.4m, 0.4-1m, and 1-2m below surface). I am only able to extract the last layer in this case. I know this post is not about the usage of the package. I have other variables working, such as TMAX, TMIN, etc. Thanks in advance!

  4. Thanks, although it is very detailed, your desciption could be a bit more specific for idiots like me.
    for example: “Also search for makefile.darwin in 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.”
    is pretty unclear to me. Should I uncomment:
    for makefile -f scripts/makefile.darwin
    and comment:
    SHELL=/bin/sh
    ?

    thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s