Handling GRIB2 files with wgrib2 - conrad-blucher-institute/semaphore GitHub Wiki
To handle grib2 files and extract data from them, you need one of the utility tools that can load the files and query them. The two main ones out there are wgrib2 and degrib. This page provides information about wgrib2.
wgrib2 is a console app. You use it to execute queries against a grib2 encoded file. To use wgrib2, you have to download the source code and compile it. This can be done both on Linux and Windows. However, if you are using Windows, we recommend using WSL on windows and compiling wgrib2 under that WSL instance. Once you have wgrib2 compiled locally, you can use it from a terminal or invoke it from a script or program such as a python script.
Getting and compiling wgrib2
most of the steps in this section are from the WxGuy this post and this post and a little of this post but the below are the steps I ran when installing and running wgrib2 on my linux-based laptop.
- install all necessary dependencies
sudo apt-get install -y build-essential libaec-dev zlib1g-dev libcurl4-openssl-dev libboost-dev curl wget zip unzip bzip2 gfortran gcc g++
- download the source code to a temporary folder and extract it
wget https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/wgrib2.tgz
tar -xzvf wgrib2.tgz
- This will unzip the tar file into a subfolder called grib2. Make that the current folder. Before you can compile the code you need to set the C++ and the fortran compilers by running:
export CC=gcc
and
export FC=gfortran
- compile the code by running the make command
make
- if you get an error at that point about cmake not being installed, install it by running
sudo apt install cmake
-
...^...^... compiling that code takes a while ....
-
The compilation will create a wgrib2 subfolder (yeah that's a lot of nested subfolders). Test that everything compiled ok by checking the configuration:
wgrib2/wgrib2 -config
if you are still in the grib2 subfolder or
./wgrib2 -config
if you are in the wgrib2 subfolder
- at this point i recommend copying the wgrib2 executable into somewhere along your path, such as your user bin folder, so that you can invoke wgrib2 from anywhere including folders that contain grib2 files
sudo cp -rfv wgrib2/wgrib2 /usr/local/bin/wgrib2
- Might not be a bad idea to go clean up the folder that contains the tar file and the source files
Querying a grib2 file with wgrib2
Note that the grib2 format is both comprehensive and quite flexible and is used differently for different purpose. For example, one file may contain predictions for different weather elements, different locations, different reference times, and different lead time. Other files may have one of more of these fixed provide different entry only for say different locations and different lead times. Some files (all files???) also provide predictions for grids not exact location. This means that, when you request data for one or more location, wgrib2 will find the most relevant grid for the GPS coordinates and return that prediction together with information about the grid that the prediction is associated with.
wgrib2 querying basics
wgrib2 works by giving it the path to a gib2 file (the file does not have to have the grib2 extension) and then passing it some parameters that specify the data you are looking for in the file. For example, the command:
wgrib2 myfile.grib2 -match ':TMP:' -lon -97.7000 30.283
will return all entries in the file myfile.grib2 that both have the substring ':TMP:' and are for the given longitude and latitude. This example query can be run to get the temperature predictions for that location.
wgrib2 also provide parameters to filter out or include the information returned for each entry (e.g., reference time, forecasted time, units, even things like sensor elevation) as well as parameters to control how wgrib2 runs (e.g., single core vs multicores).
getting to know your grib2 files
When first working with a grib2 file generated from a new group, it is a good idea to run a few wgrib2 queries to get a feel for what the file contains. Below are some examples of queries for that purpose.
1. get one entry from the file
wgrip2 -V -d 10 myfile.grib2
this will spit out the default data from the 10th entry in the file. An NDFD grib2 file would return something like this:
10:238142:vt=2024090603:10 m above ground:29 hour fcst:WDIR Wind Direction (from which blowing) [deg]: ndata=86350:undef=16246:mean=80.8171:min=0:max=360 grid_template=30:winds(N/S): Lambert Conformal: (314 x 275) input WE|EW:SN output WE:SN res 0 Lat1 25.106399 Lon1 252.877075 LoV 265.000000 LatD 25.000000 Latin1 25.000000 Latin2 25.000000 LatSP -90.000000 LonSP 0.000000 North Pole (314 x 275) Dx 5079.406000 m Dy 5079.406000 m
2. get the number of grid points in the file
$ wgrib2 20240904_214816_ds.wdir.bin.grib2 -npts
1:80:npts=86350 2:23879:npts=86350 3:48827:npts=86350 4:76240:npts=86350 5:105356:npts=86350 6:134980:npts=86350 7:161231:npts=86350 ....
Note that the reason we are getting multiple results is because we have different lead times (and hence forecasted times). So wgrib2 returns the number of grid points per forecasted time, and in this case we they are all the same.
3. Get the name of all weather elements (aka variables) predicted in the file
$ wgrib2 20240904_214816_ds.wdir.bin.grib2 -var
1:80:WDIR 2:23879:WDIR 3:48827:WDIR 4:76240:WDIR 5:105356:WDIR 6:134980:WDIR 7:161231:WDIR 8:187200:WDIR
Getting specific data -- Examples
get all entries in a file for a given set of GPS coordinates and get the reference time, forecasted time, and weather element name
$ wgrib2 -T -ftime -var -lon -97.0733 27.84 20240904_214816_ds.wdir.bin.grib2 1:80:D=20240904220000:2 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=50 2:23879:D=20240904220000:5 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=50 3:48827:D=20240904220000:8 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=30 4:76240:D=20240904220000:11 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 5:105356:D=20240904220000:14 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=0 6:134980:D=20240904220000:17 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=0 7:161231:D=20240904220000:20 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 8:187200:D=20240904220000:23 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 9:212835:D=20240904220000:26 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 10:238142:D=20240904220000:29 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 11:262081:D=20240904220000:32 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 12:288219:D=20240904220000:35 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 13:313896:D=20240904220000:38 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=0 14:338063:D=20240904220000:41 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 15:357632:D=20240904220000:44 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=10 16:375789:D=20240904220000:47 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 17:393950:D=20240904220000:50 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=30 18:412641:D=20240904220000:53 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=30 19:434085:D=20240904220000:56 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=30 20:454878:D=20240904220000:59 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 21:476824:D=20240904220000:62 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 22:498872:D=20240904220000:65 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=20 23:518912:D=20240904220000:68 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=30 24:539015:D=20240904220000:71 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=40 25:558947:D=20240904220000:74 hour fcst:WDIR:lon=262.936436,lat=27.820085,val=40