wgrib2 v3.7 Install for Older Intel Macs- Quick Guide

#wgrib2 #Intel Mac installation wgrib2 v 3.7


(If you’re upgrading from older wgrib2 3.1.x, be sure to remove the grib2 folder in the /opt/homebrew and place it on your desktop. Also delete the symlink in your /usr/local/bin folder. You can always copy it back if you have problems with this upgrade; but you’ll need to create a new symlink in the bin folder if you decide to revert back.)

These instructions and commands are for older INTEL Macs only. See the main tutorial for Apple Silicon installation. The chief difference is a different directory usage. Intel Macs: /usr/local/ Apple Silicon: /opt

This quick guide uses bash shell, not z shell. Switch to bash shell

chsh -s /bin/bash

Edit your .bash_profile with this command so that it recognizes homebrew—

echo 'eval "$(/usr/local/bin/brew shellenv)")"' >> ~/.bash_profile source ~/.bash_profile



Install Apple Command Line tools :

 xcode-select --install

Install Homebrew package Manager https://brew.sh/

Using Homebrew, install the following-

brew install gcc

brew install cmake

brew install zlib libpng

brew install openblas

brew install lapack

brew install jasper

brew install libaec

brew install pkg-config

After installing gcc, you must check inside the /usr/local/bin folder to see which version of gcc is installed. (e.g. gcc-15) If you have multiple versions, uninstall gcc and then reinstall.

Download the three packages needed: NCEPLIBS-ip, NCEPLIBS-g2c, NCEPLIBS-wgrib2 from Github. Run one command below at a time.

cd ~/Downloads
git clone https://github.com/NOAA-EMC/NCEPLIBS-ip.git


git clone https://github.com/NOAA-EMC/NCEPLIBS-g2c.git


git clone https://github.com/NOAA-EMC/NCEPLIBS-wgrib2.git



Edit any options in the CmakeLists.txt file of two of the packages so that these particular options are as shown below (g2c and wgrib2). The other options can be left as the default values.

NCEPLIBS-ip— No CmakeLists.txt edits needed



NCEPLIBS-g2c—

option(USE_PNG “Use PNG library” ON)
option(USE_Jasper “Use Jasper library” ON)
option(USE_OpenJPEG “Use OpenJPEG library” OFF)
option(USE_AEC “Use LibAEC library” ON)
option(BUILD_SHARED_LIBS “Build shared libraries” ON)
option(BUILD_STATIC_LIBS “Build static libraries” ON)
option(BUILD_G2C “Build the g2c file-based API.” ON)


NCEPLIBS-wgrib2—

option(USE_IPOLATES “Use Ipolates” off)    Change to ON
option (USE_G2CLIB_HIGH “Use g2c high-level decoder (-g2clib 2)?”   Change to ON
option (USE_G2CLIB_LOW “Use g2c high-level decoder (-g2clib 2)?”   Change to ON
option(USE_AEC “Use AEC to enable…) ON
option(USE_OPENMP “Use OpenMP?” Change to ON)

Copy the three package folders you downloaded above, after making teh edits to the CmakeLists.txt, which should be in your Downloads folder.

Paste them in the the directory /usr/local. Use the Finder command Command-Shift-G to enter the /usr/local

Run and install each of the three packages build and compile commands. There are several commands groups in each block below. Copy and paste each set of commands one at a time. The commands are separated by spaces. Do NOT copy and paste the entire block of commands at once.

cd /usr/local/NCEPLIBS-ip


sudo mkdir build && cd build


export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
export LDFLAGS="-L/usr/local/opt/openblas/lib"
export CPPFLAGS="-I/usr/local/opt/openblas/include"


sudo cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/opt/NCEPLIBS-ip \
-DBUILD_SHARED_LIBS=ON \
-DBLAS_LIBRARIES=/usr/local/opt/openblas/lib/libblas.dylib \
-DLAPACK_LIBRARIES=/usr/local/opt/lapack/lib/liblapack.dylib \
-DCMAKE_OSX_SYSROOT=$SDKROOT



sudo make -j$(sysctl -n hw.logicalcpu)


sudo make install
## NCEPLIBS-g2c

cd /usr/local/NCEPLIBS-g2c


sudo mkdir build && cd build


export JASPER_INCLUDE_DIR=/usr/local/opt/include/jasper
export JASPER_LIBRARIES=/usr/local/opt/lib/libjasper.dylib
export JPEG_INCLUDE_DIR=/usr/local/opt/include
export JPEG_LIBRARY=/usr/local/opt/lib/libjpeg.dylib


sudo cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/opt/NCEPLIBS-g2c


sudo make -j$(sysctl -n hw.logicalcpu)


sudo make install

## NCEPLIBS-wgrib2
## Uses gcc-15; If necessary, change to your gcc
## version.

cd /usr/local/NCEPLIBS-wgrib2


sudo mkdir build && cd build


sudo cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/opt/NCEPLIBS-wgrib2 \
-DCMAKE_PREFIX_PATH="/usr/local/opt/NCEPLIBS-ip;/usr/local/opt/NCEPLIBS-g2c" \
-DCMAKE_C_COMPILER=/usr/local/bin/gcc-15 \
-DCMAKE_CXX_COMPILER=/usr/local/bin/g++-15 \
-DCMAKE_Fortran_COMPILER=/usr/local/bin/gfortran-15 \
-DUSE_OPENMP=ON \
-DUSE_IPOLATES=ON \
-DUSE_G2CLIB_HIGH=ON \
-DUSE_G2CLIB_LOW=ON \
-DUSE_AEC=ON \
-DCMAKE_C_FLAGS="-fopenmp" \
-DCMAKE_CXX_FLAGS="-fopenmp" \
-DCMAKE_EXE_LINKER_FLAGS="-L$(brew --prefix gcc)/lib/gcc/15 -lgomp"


sudo make -j$(sysctl -n hw.logicalcpu)


sudo make install

8. Create symlink for wgrib2

ln -s /usr/local/opt/NCEPLIBS-wgrib2/bin/wgrib2 /usr/local/bin

9. Create $PATH edits in your .bash_profile (I’m using bash shell here)

export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-wgrib2/lib:$DYLD_LIBRARY_PATH"
echo 'export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-wgrib2/lib:$DYLD_LIBRARY_PATH"' >> ~/.bash_profile source ~/.bash_profile
export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-ip/build/install/include_4:$DYLD_LIBRARY_PATH"
echo 'export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-ip/build/install/include_4:$DYLD_LIBRARY_PATH"' >> ~/.bash_profile source ~/.bash_profile
export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-g2c/lib:$DYLD_LIBRARY_PATH"
echo 'export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-g2c/lib:$DYLD_LIBRARY_PATH"' >> ~/.bash_profile source ~/.bash_profile
export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-ip/lib:$DYLD_LIBRARY_PATH"
echo 'export DYLD_LIBRARY_PATH="/usr/local/opt/NCEPLIBS-ip/lib:$DYLD_LIBRARY_PATH"' >> ~/.bash_profile source ~/.bash_profile

When you are using wgrib2, to invoke the optional multi-thread/multi-processing, precede the wgrib2 command with the instruction below which leaves 2 cores available for other tasks:

RESERVE_CORES=2; TOTAL_CORES=$(sysctl -n hw.logicalcpu); if (( TOTAL_CORES > RESERVE_CORES )); then export OMP_NUM_THREADS=$((TOTAL_CORES-RESERVE_CORES)); else export OMP_NUM_THREADS=1; fi; export OMP_DYNAMIC=TRUE OMP_WAIT_POLICY=passive; echo "OpenMP threads: $OMP_NUM_THREADS (reserving $RESERVE_CORES of $TOTAL_CORES cores)"


Weather… and Other Things 'Up in the Air'