How to Install and Compile wgrib2 on Mac OS X

Last Updated

Introduction – About wgrib2

To work with weather data GRIB files efficiently, the wgrib2 utility, developed by NOAA data scientist Wesley Ebisuzai, is an essential command line program to have.

While there are several quick online guides to compiling and installing on Linux and Windows machines, I found that the online instructions for installing and compiling wgrib2  on Mac OS X (10.14.6 Mojave) were good, but not good enough for me to be successful.

Here’s the main page with multiple links from NOAA about wgrib2.

https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/

It has all the links on the subject, including installation instructions (but not enough detail for a Mac install.)

“wgrib can slice and dice grib1 files. wgrib2 is more like four drawers of kitchen utensils as well as the microwave and blender. This kitchen miracle was done by a more developer friendly design and the contributions of many people.”

Over many months and prior to posting these instructions, I ran into compilation errors repeatedly.   I could find little help online and only a very limited number of online articles specific to Mac installation.  I was just about ready to give up when I finally figured out ‘the tricks’ needed for it to compile on Mac OS X 10.14.6.  These are included in the instructional that follows.

I’m posting this instructional so that others will not be daunted by the process of installing wgrib2 on their Mac.

Installing Instructions Part I:

There are two main online instruction sources for compiling and installing wgrib2 on Mac OS X and I am extremely grateful to their authors for their posts:

How to install wgrib2 in OSX

https://rda.ucar.edu/datasets/ds083.2/software/wgrib2_install+.txt

Individually these two links provided the essential flow of installation and compilation of wgrib2 sequence on the Mac.

However, their instructions only got me part way there. I still had compilation errors.

Much of what I’m going to review is from the first link above (“How to install wgrib2 on OSX  “Bovine Aerospace”) but there will be important changes that I found essential for the install to work.  In this instructional, I will use mostly Mac-like approaches.  Terminal commands like chmod, etc won’t be used.

The Mac OS version I am using is 10.14.6 (Mojave).

Here goes:

First, make sure you’re running as an Administrator on your Mac.  While many of the downloads don’t require it, you won’t be able to run essential Terminal commands without Administrator and “super user” permissions.  You can check if you’re an administrator by going to System Preferences>> Users & Groups on your Mac and make sure you’re running as an Administrator.

Now, let’s download the wgrib code:

The latest versions of wgrib2 code from NOAA and  README documents are available from this web site:

https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/

>>However, I recommend you use this direct ftp link below to get wgrib2—  You can use a browser to download it using FTP.

ftp://ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/wgrib2.tgz

You’ll likely find the wgrib2.tgz package in your Downloads folder. Leave it there for now.

Xcode, Command Line Tools, gcc and gfortran need to be installed before installing wgrib2.

Download Xcode.  Xcode can be downloaded and installed by registering as a developer at Apple (no charge) and downloading from the Apple developer’s site:

https://developer.apple.com/

Install Xcode as you would any program you download for your Mac.

Run the Xcode program once to have the license verified.

Next, download and install the Xcode “Command Line Tools” using Terminal:

Launch Terminal.

enter the following command:   

xcode-select --install

After it completes, I recommend launching  Xcode again and agreeing to the terms of service.  (The Command line tools won’t run unless you’ve agreed to the License and Terms of service. )

Verify that you’ve successfully installed Xcode Command Line Tools using Terminal by entering this command:

$ xcode-select -p

You should see a confirmation showing where it’s installed

iMac$ xcode-select -p
/Applications/Xcode.app/Contents/Developer

Next, check that gcc ( a C compiler) has been installed.  For some reason, mine wasn’t .   Use Terminal to check if it’s there:  Enter “which gcc” in Terminal.

which gcc

If it’s installed, it will respond with the directory it’s in:

/usr/local/bin/gcc

If it doesn’t show /usr/local/bin/gcc, it’s not installed.

gcc can be installed with a “package installer” called HomeBrew.  HomeBrew will also need to install other things, so now’s the time to download and install HomeBrew.

Go to https://brew.sh/ and follow the relatively simple instructions to install HomeBrew.  (Not always so simple.  I had to install as an adminstrator and issue the command using the administer name of your computer after the su command.

su AdminNameYouUse

Use Terminal to install HomeBrew:

Copy the command from the HomeBrew page.

Then, paste it into Terminal.  HomeBrew will install.

After you’ve installed HomeBrew, restart your machine.

Go back into Terminal and issue the following command to install gcc:

brew install gcc

gcc should then be installed.

Check to see where it’s installed,  Enter the following command

 which gcc

and terminal should show this, if gcc is installed:

/usr/local/bin/gcc

While you’re at it, check to see if another compiler, gfortran, is installed by entering this in Terminal:

which gfortran

and if it’s installed, Terminal will reply:

/usr/local/bin/gfortran

Additionally, use Homebrew to install  “gmake”

brew install gmake

So  we’ve installed Xcode, Command Line Tools, HomeBrew,  gcc,  gfortran and gmake before installing wgrib2.

One more thing.  Let’s check the exact name of  the installed versions in your /usr/local/bin directory:

Using the Finder, select the GO menu
Select   Go to Folder….

Enter the path to your bin folder:

/usr/local/bin

You should see the alias versions of gcc and gfortran that were installed in the local bin directory:In my installation it was gcc-9 and gfortran-9.

This turned out to be important.  Later on, we’ll be modifying the scripts for installation of wgrib2.  We will need to use the exact version (gcc-9, gfortran-9)  in the script.

We’ve completed the first part.

The wgrib2.tgz Package

Next, we’re going to deal with the wgrib2.tgz package that was downloaded earlier and is likely in your Downloads folder.

Make a copy of the wgrib2.tgz package and move it to your desktop.

(We’re going to do the work initially on the Desktop, although advanced users might want to put it directly in /usr/local/, but let’s keep it simple.

Open the Terminal program. At the prompt, type these two commands

cd ~/Desktop
tar -xzvf wgrib2.tgz

This will “untar”  (“unzip” if you’re used to Windows and Mac) the install package.

When untarred, you should see a new folder named grib2 on your Desktop.

In the Finder, open the grib2 folder:  You will see all the libraries and scripts needed to compile and install wgrib2.

The folder, grib2 shows what’s inside the tarball of wgrib2.tgz   click for larger image

The  arrows point to key files that we will be modifying to get this to install.  Let’s first work on the makefile  (as shown with the arrow.)

Using a text editor, open the file called makefile in this folder.  You can use TextEdit or any simple text editor.  (I’m using a program called Atom which shows the line numbers.)

This makefile needs to edited.  See the areas shown below:

The following changes need to be made:

Find the lines shown  (lines 104 and 105)  and uncomment the lines  (delete the preceding hashtag). You will  change them from this

#export CC=gcc
#export FC=gfortran

to this using your TextEditor. Be certain to add the version of gcc and gfortran to the lines below as shown.   It should look like this:

export CC=gcc-9
export FC=gfortran-9

Notice that I am including the version number (gcc-9)   (gfortran-9) that we saw in your usr/local/bin directory in the earlier part of these instructions.   If your version is different, use the one that you have.

Additionally, I found that we need to edit another parameter in the makefile, not mentioned in other online tutorials for the Mac.  The text is shown in the above graphic of the opened makefile  (line 134)

Edit this using your TextEditor and change this

USE_PNG=1

to this:

USE_PNG=0

There are two more changes we need to make.

Find the line (line #666;  it’s far down.  Use your  text editor search to find it in the file; Search on the word “.darwin”)

Click for larger image
# for OSX
# export LDFLAGS="-L${lib}" && cd "${pngdir}" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE} -f scripts/makefile.darwin

and just uncomment it (remove the hashtag) so that it looks like this:

# for OSX
  export LDFLAGS="-L${lib}" && cd "${pngdir}" && export CPPFLAGS="${wCPPFLAGS}" && ${MAKE} -f scripts/makefile.darwin

Also, below these lines (line 668-669), you need to ADD a hashtag (#) to comment (inactivate) the next line.

Change this:

# for everybody else
export LDFLAGS="-L${lib}" && cd "${pngdir}" && export CFLAGS="-DPNG_USER_WIDTH_MAX=200000000L -I${cwd}/include" && ./configure --disable-shared --prefix=${cwd} && ${MAKE} check install

to the following:  (Simply add a hashtag, as  shown in orange)

# for everybody else
# export LDFLAGS="-L${lib}" && cd "${pngdir}" && export CFLAGS="-DPNG_USER_WIDTH_MAX=200000000L -I${cwd}/include" && ./configure --disable-shared --prefix=${cwd} && ${MAKE} check install

We’ve made the necessary changes in the makefile.  Select Save from your TextEditor menu to save the changes.

Editing the libpng-1.2.57.tar.gz

Now, we need to edit a compilation library that’s in the same folder as the the makefile we just worked on.   The file is:

libpng-1.2.57.tar.gz

We’re going to do it initially with Terminal to untar the tarball.

You might want to quit and then to restart Terminal again to make sure you’re in the correct directory.  After restart, enter:

cd ~/Desktop/grib2

Then enter

tar -xvf libpng-1.2.57.tar.gz

We can make the changes we need to make with the Terminal text editor, nano, but I think it’s easier for Mac users to just work with the file. So quit Terminal.  (In Terminal it would cd ~/Desktop/grib2/libpng-1.2.57/scripts but we’re doing this the Mac way.)

Go to the Finder Desktop and find the new folder that was created inside the grib2 folder called libpng-1.2.57  ( Desktop/grib2/libpng-1.2.57)Open that folder (libpng-1.2.57)  to find the scripts folder

Inside the scripts folder is a file called makefile.darwin:

Let’s edit this makefile.darwin with TextEditor.

Using the search in TextEditor, find the line CC=ccChange

CC=cc

to

CC=gcc-9

Save this change with your TextEditor save command.

Now we’re going to re-tar this file back to a compressed file.

Start Terminal.  At the prompt, enter:

cd ~/Desktop/grib2/

Then enter:

tar -cf libpng-1.2.57.tar libpng-1.2.57

Then enter:

gzip libpng-1.2.57.tar

You will see the following:

libpng-1.2.57.tar.gz already exists -- do you wish to overwrite (y or n)?

Type  y  to overwrite the file.

Congratulations, we’re almost there.

Close the grib2 folder on your Desktop.  Select copy from the Edit Menu and copy the grib2 folder.

Using the Finder “Go” menu,  select  Go to Folder…

Enter :

 /usr/local/

Paste the grib2 folder into the local directory. Your local folder should look like this after pasting. Next we are going to alter the permissions of this folder and its contents.  We could do this quicker with Terminal and the chmod command, but let’s do it the Mac way.

Select the grib2 folder.  Right click to Get Info. In the Get Info window: Unlock the padlock, then using the plus symbol, add the administrator and make it Read & Write.

Using the gearwheel, select “apply to enclosed items” from the dropdown.   Close the padlock to save.

We’ve done the changes needed. Congratulations.   In doing it this way, you should  still have copy of the grib2 folder on your Desktop with all of the changes we’ve made.   We can use this again and copy it to the local folder if we find it doesn’t compile right.

Final Step:  Compiling wgrib2

Ok, the now the moment we’ve been waiting for.

Remember, you need to be an Administrator of your computer for this to work.

Boot up Terminal.

Enter superuser mode using the su command and your username as an administrator.  You will be prompted to enter your password.

su yourcomuteradministratorname
cd /usr/local/grib2/

Now use the “make” command as a superuser.

sudo make

If everything is going well, you should see Terminal display the progress of the compilation.  It may take several minutes.

If no errors occur at the end, you did it!

To make wgrib2 easier to use (while you’re still in the grib2 directory), enter:

ln -s ~/local/grib2/wgrib2/wgrib2 ~/local/bin

You don’t need to remain as an Administrator to use wgrib2.

 

Leave a Reply

Your email address will not be published.

Storm Forecasts & Other Things "up in the air".