Mac OS Monterey – Ventura – Intel & Apple Silicon M1 M2
Posted July 23, 2023
Updated 07-27-2023 with advise for compilation errors
Improved instructions for wgrib2 v 3.1.x Intel & Apple Silicon
Introduction to wgrib2 utility – About wgrib2
To work efficiently with very large weather GRIB2 data files, the wgrib2 utility was developed by several NOAA data scientists, especially Wesley Ebisuzaki. It is an essential command line-based C and fortran-based program used to index and manipulate weather data grib2 files.
Most commonly, it’s used to extract specific weather parameters and to create GRIB2 files that cover smaller geographic areas.
Most applications a Mac user might use are graphical interface based (GUI) and are already compiled by the app’s developer; they just need to be installed on your Mac.
wgrib2, on the other hand, runs on the Mac’s underlying Unix -Darwin foundation and its operation is dependent on your Mac’s specific directory structure and the versions and locations of other code libraries (“dependencies”) installed on your Mac.
As a result, wgrib2 is supplied as as folder of code, scripts, and code libraries that are downloaded and then compiled for your specific machine before it is able to run. This tutorial will explain how to do this.
From my previous install/compile tutorial, installing wgrib2 on a Mac used to be a very complex and tedious adventure, fraught with problems, starts and stops. But it could be accomplished.
I’m happy to report that wgrib2 (now in version 3.1.2 ) can now be installed and compiled relatively easily on an Intel or Apple Silicon Mac.
A special thank you to a reader, “Davis Zhang“, who alerted me to me to the existence of a brief Japanese language install guide which showed that wgrib2 version 3.x can now be installed with a significant reduction in complexity on the latest versions of Mac OS.
I’ve successfully installed wgrib2 v. 3.1.2 on an Intel Mac running Monterey on an Apple Silicon M-1 Mac running Ventura using the instructions I will be providing below.
As was the case my previous install/compile tutorial, this tutorial is designed for true novices with limited experience using Terminal and command lines. It’s probably more detailed than many people need, but I’m sure there are some of you that will appreciate the hand-holding.
Overview of the steps
- Download wgrib2 from the NOAA web site; rename the folder
- Uncompress or ‘untar’ the wgrib2 download
- Download and install Apple Developer Tools
- Download HomeBrew package installer needed to install required library “dependencies”
- Using HomeBrew, install various library dependencies – gcc, gfortran, cmake. Confirm proper directory structure and PATHs using ‘brew doctor‘ command. Check specific version of gcc installed.
- Edit the script called ‘makefile‘
- Move a copy of your grib2 folder to the directory: /usr/local/
- Run the ‘makefile‘ script using a Terminal command to compile wgrib2 into an executable.
- Create a symlink of the installed wgrib2 in your /usr/local/bin/ directory so that it can easily be called using Terminal.
So here we go—
1- Download wgrib2 from the NOAA web site
The latest versions of wgrib2 code from NOAA and many README documents are available from this web site:
As of this writing, the latest version is 3.1.2. (Download the version WITH libraries)
Updated 07/27/23— I recommend using version 3.1.1. If installing 3.1.2, please see the note below.
You’ll likely find the wgrib2.tgz.v3.1x package in your Downloads folder.
Move the wgrib2.tgz.v3 file from your Downloads folder to your Desktop
2-Uncompress the wgrib2.tgz download
On your Desktop, rename the wgrib2.tgz.v3.1.2 file to simply wgrib2.tgz by right clicking or option-clicking on the file and select rename. Confirm yes when it asks you to change the file type. This should be the filename—
After renaming the file, double click on the wgrib2.tgz file. The file should untar (unzip, uncompress) and a folder called grib2 will be on your Desktop.
We’ll get back to this folder shortly.
3-Download and install Apple Developer Tools
You may have X-code already installed on your computer. If so, you will still need to install Apple Developer Tools.
Launch Terminal (in your Applications/Utilities Folder )
enter the following command and press return
After Developer Tools downloads and installs, it will launch. You MUST agree to the terms of the license agreement or things won’t go well.
Verify that you’ve successfully installed Xcode Command Line Tools using Terminal by entering this command and press return
$ xcode-select -p
You should see a confirmation showing where it’s installed
If you’re a novice, I strongly recommend that you quit Terminal after each major step and then restart the Terminal program. It avoids certain issues of being in the wrong directory, etc.
4- Download and Install HomeBrew
Homebrew is a “package installer” and is used to download and install certain dependency libraries of code, used by many utilities including wgrib2. We’ll be using Homebrew to install the dependent compiler packages such as gcc, gfortran and cmake.
Go to https://brew.sh/ and follow the relatively simple instructions to install HomeBrew by copying a command from its web site home page, then opening your Terminal program and pasting the copied command into the Terminal window.
•Copy the install command from the Homebrew web site.
•Paste it into Terminal.
HomeBrew will install.
After installation, enter the command below in Terminal and press return
This will check that everything is in its proper place. It will also do tests that may alert you to issues that may affect compilation and install of wgrib2. Running this command may result in recommendations of other commands to fix links, symlinks and conflicts. I do suggest that you follow its recommendations and fix things it finds.
You can run “brew doctor” any time again to see other recommendations. Using the Terminal File Menu, you can open a second window in Terminal to copy and paste HomeBrew’s recommendation commands to fix and solve any issues.
5-Using HomeBrew, install various library dependencies – gcc, gfortran, cmake
Now we’re going to install specific libraries that wgrib2 compilation will need.
Install gcc (installs gfortran automatically at the same time)
Enter the command below in Terminal and press return
brew install gcc
gcc should then be installed.
Check to see where it’s installed, enter the following command in Terminal and press return—
and terminal should show this, if gcc is installed:
Installation of gcc includes automatic installation of gfortran.
To check to see where gfortran was installed, enter the following command in Terminal and press return—
and if it’s installed, Terminal will reply:
Finally, we’ll use HomeBrew to install cmake.
Enter the command below in Terminal and press return—
brew install cmake
It should be installed and you can check using the “which cmake’ command.
Here’s an important point—
We need to know what version of gcc HomeBrew has just installed. The usual command to determine the gcc version will reveal that Apple has installed the ‘clang’ version when Developer Tools were installed.
Enter the following command in Terminal and press return—
You’ll see the following—
Apple clang version 14.0.0 (clang-1400.0.29.202)
Thread model: posix
We don’t want to use this clang version when compiling wgrib2
Let’s find out the version that’s been installed by HomeBrew. It’s in the /usr/local/opt folder.
Restart Terminal, enter the following command and press return:
then enter the command below and press return
You’ll see the listing (ls) of what’s in the opt directory
We can see that the version installed by HomeBrew in my opt directory is gcc-13
Depending upon the current release version of gcc, yours may differ. For illustration purposes, we’ll use gcc-13, the version installed here, when we edit the ‘make’ script file.
When we modify the the ‘makefile’ file script, be certain to use the version that was installed in your computer.
So to this point, we’ve installed Apple Developer Tools, gcc gfortran and cmake.
–> Now we’re going to return to the grib2 folder that’s on your Desktop that we had uncompressed earlier.
6-Edit the wgrib2 installer “make” file.
In the Finder, go to the folder grib2 downloaded and uncompressed earlier on your Desktop.
Open the grib2 folder on your Desktop. Inside you’ll see a file called ‘makefile‘.
We’re going to edit this the file using a text editor application.
Using any text editor, open the ‘makefile‘ file. Right click on the file and select “open with”. You can use TextEdit. I’ll be using BBedit to edit the makefile —
About line 154 or so (depends on the wgrib2 version) you will find under #Main development platform: linux with gcc/gfortran:
We are going to make the two lines referring to gcc and gfortran here active (by uncommenting) the lines (meaning, removing the preceding hashtag -#-) and editing to indicate the version of gcc that was installed with HomeBrew earlier.
Using your text editor, change these two lines:
# export CC=gcc
# export FC=gfortran
Notice that the leading # has been removed from each line and the version number of gcc installed by Homebrew (gcc-13) has been edited in.
–>When the edits have been completed, remember to SAVE the changes to the makefile from your text editor’s File menu.
Now close the grib2 folder on your Desktop.
7- Copy your grib2 folder and paste to /usr/local/
Using the Finder, select the grib2 folder on your Desktop and select the Copy command from the Edit Menu, or use the Command-c shortcut.
Using another command from the Finder, select the Go Menu and select Go to Folder…
Enter this path:
This will show a normally hidden folder on your Mac, the /usr/local/ folder.
PASTE the grib2 folder that you copied from Desktop into this folder. (Command -v)
After pasting, you should see a copy of the grib2 folder in that /usr/local directory—
We’re almost there!
8- Run the ‘makefile’ compiler script in super user mode.
We’re ready to compile and install wgrib2.
Make sure you’re running as an administrator level user on your Mac.
Enter the following command and press return:
Then enter the following command and press return:
You’ll be prompted for your user password. Enter it and press return.
You should see a bunch of gibberish scroll past on the Terminal window as it compiles and installs. It may take as much as 10 or more minutes for it to complete. Longer is usually a good sign.
When it completes, it should NOT have any error messages at the end. If it was successful, it won’t tell you. You just won’t see any errors at the end.
If you see error messages at the end, something is amiss. I’ll explore troubleshooting in another tutorial.
9- Create a Symlink
Quit Terminal. Restart Terminal again
We’re going to create a symlink in your /usr/local/bin folder so that Terminal can easily call wgrib2.
Enter the following command in Terminal and press return:
sudo ln -s /usr/local/grib2/wgrib2/wgrib2 /usr/local/bin
Enter your user password when prompted. Press return.
A symlink will have been created.
Now, let’s check if wgrib2 has been installed.
In Terminal, enter the following command in Terminal and press return—
You should see the response
To see what wgrib2 can do, just enter this in Terminal and press return:
You should see all the commands wgrib2 can accept scroll in the Terminal window.
To see wgrib2 in action, use the terminal command on any .grib2 file you have to see its message contents—
wgrib2 -s mygribfile.grib2
Use of wgrib2 is an entire other tutorial. Look for it in coming months. Use the tips found here to use wgrib2— https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/tricks.cheap see the "tricks" links at https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/
Questions or comments? Use the Feedback form to email me.
#wgrib2 #install wgrib2 on MacOS