Read and write grib/grib2 data using Python


    Pygrib, PyNIO, and Iris are already installed on Carter and Conte clusters with Python v2.7.2. 
    Simply type 'module load python' in your terminal, which will load Python version 2.7.2 by default.


Read grib/grib2 data with pygrib module

First, import the pygrib module (and other modules you may want)

import pygrib

Open a grib/grib2 file

file = 'rap_130_20131206_0500_001.grb2' #example filename

gr =

Print an inventory of file

#print inventory
for g in gr:
    print g

        The process will print out each record stored in the file, similar to the output you would see from using the wgrib/wgrib2 programs.

To print some more detailed variable attributes, such as variable name, level, coordinate level, valid date, etc. :

for g in gr:
  print g.typeOfLevel, g.level,, g.validDate, g.analDate, g.forecastTime

When using pygrib (which uses ECMWF grip API), there a number of different keywords that you can use. Many of these will be specific to the content within the grib message (e.g. see If you want to know which keys are available, , you can use grib_dump filename.grb to dump the contents of the grib file (see: and see all of the possible keywords.

Examples of other keywords:


Get a list of N grib messages

To obtain a list of the first N number of grib messages, we can use read method. For example, to get a list of the first 10 grib messages:

grbmsgs =

To get a list of grib messages that have a certain name, level, etc. we can use the select method with optional keyword arguments. For example, if we want to obtain a list of all the grib messages that consist of temperature on pressure levels, we could do this:

tmpmsgs ='Temperature',typeOfLevel='isobaricInhPa')

Get a grib message associated with a certain record number

#get message 38 (temp @1000mb in this example)
msg = gr[38] #get record number 38
print msg

    output will looksomething like this:

38:Temperature:K (instant):lambert:isobaricInhPa:level 100000 Pa:fcst time 1 hrs:from 20131206050

Note: this will not the values associated with the variable!       

Get values associated with the grib message

    Now, we want to get the values that are associated with the grib message we have just read in
temp_vals = msg.values

    Values are read into a NumPy array named temp_vals. We may now access information about the array, such as the shape and max/min values, as is normally done with numpy arrays.

print temp_vals.shape
print np.amax(temp_vals),np.amin(temp_vals)

    To get latitude/longitude values associated with the grib message, we use the latlons() method:
lats,lons = msg.latlons()

Grib2 Decode

    If you have read in a grib2 file, and you want to obtain information about the encoding specifics, you must import the ncepgrib2 module, specifically the Grib2Decode class. Ncepgrib2 is a wrapper around the NCEP grib2 C library.

#import Grib2Decode from ncepgrib2
from ncepgrib2 import Grib2Decode

#get info about grib2 section specifications
grb = Grib2Decode(msg.tostring(),gribmsg=True)
print grb

This will result in something like this:

bitmap_indicator_flag = 255
center_wmo_code = kwbc
data_representation_template = [1131507924         -3          0          9          0          0        255]
data_representation_template_number = 40
discipline_code = 0
earthRmajor = 6371229.0
earthRminor = 6371229.0
grid_definition_info = [     0 151987      0      0     30]
grid_definition_template = [        6         0         0         0         0         0         0       451       337  16281000 233862000         8  25000000 265000000  13545000  13545000         0        64  25000000  25000000         0         0]
grid_definition_template_number = 30
gridlength_in_x_direction = 13545.0
gridlength_in_y_direction = 13545.0
has_local_use_section = False
identification_section = [   7    0    2    1    1 2013   12    6    5    0    0    0    1]
latitude_first_gridpoint = 16.281
longitude_first_gridpoint = 233.862
number_of_data_points_to_unpack = 151987
originating_center = US National Weather Service - NCEP (WMC)
points_in_x_direction = 451
points_in_y_direction = 337
product_definition_template = [     0      0      2      0    105      0      0      1      1    100      0 100000    255      0      0]
product_definition_template_number = 0
proj4_lat_0 = 25.0
proj4_lat_1 = 25.0
proj4_lat_2 = 25.0
proj4_lon_0 = 265.0
proj4_proj = lcc
scanmodeflags = [0, 1, 0, 0]
shape_of_earth = Spherical

These values are important describe how the file is encoded and can be very useful for encoding a grib2 file for the first time (using an existing grib2 file as an example). If you do not want to print out the entire summary, you can print out each encoding separately. For example:

print "grid id section:",grb.identification_section

would result in:

grid id section: [   7    0    2    1    1 2013   12    6    5    0    0    0    1]

Read grib/grib2 with PyNIO

Reading grib/grib2 format data with PyNIO is very simple; it is exactly the same as reading netcdf data with PyNIO. The benefit of reading grib data with Nio versus using pygrib lies with the "under-the-hood" capabilities of Nio that collect dimensions. That is, instead of reading one record/variable at a time as required by pygrib, Nio gathers all of those variables that have the same dimensions. For example, if you wanted the temperature for all pressure levels within a grib file, you would have to read in each temperature record at each level separately. With Nio, the levels would be gathered into one dimension

Read grib/grib2 with IRIS

Write grib2 with ncepgrib2 module

First step, import the ncepgrib2 module, specifically the Grib2Encode class

from ncepgrib2 import Grib2Encode

Identification Section

Grid Identification

Product Identification

Tutorial by: Kimberly Hoogewind