Python module for reading/writing GRIB files using ECMWF ECCODES library
npx @tessl/cli install tessl/pypi-pygrib@2.1.0A high-level Python interface to the ECMWF ECCODES C library for reading and writing GRIB (GRIdded Binary) meteorological data files. pygrib provides comprehensive functionality for accessing GRIB file contents, extracting meteorological data arrays, querying metadata and parameters, and performing limited GRIB file modifications.
pip install pygrib or conda install -c conda-forge pygribimport pygribimport pygrib
import numpy as np
# Open a GRIB file
grbs = pygrib.open('weather_data.grb')
# Iterate through all messages
for grb in grbs:
print(grb)
# Select specific messages by criteria
temperature_msgs = grbs.select(shortName='t', level=500)
for grb in temperature_msgs:
# Get the data array
data = grb['values']
# Get lat/lon coordinates
lats, lons = grb.latlons()
print(f"Temperature at 500mb: {data.min():.1f} to {data.max():.1f} K")
grbs.close()pygrib's architecture centers around three main classes:
The library integrates tightly with NumPy for efficient data handling and supports various GRIB grid types including regular/irregular lat/lon, Gaussian, Lambert conformal, stereographic, and Mercator projections.
Core functionality for opening, reading, and managing GRIB files. Provides iterator-based access with file-like methods for navigation and message retrieval.
class open:
def __init__(self, filepath_or_buffer): ...
def close(self): ...
def read(self, msgs=None): ...
def readline(self): ...
def seek(self, msg, from_what=0): ...
def tell(self): ...Methods for accessing individual messages, iterating through files, and selecting messages based on criteria. Supports flexible filtering with scalars, sequences, and functions.
class open:
def message(self, N): ...
def select(self, **kwargs): ...
def rewind(self): ...
def fromstring(gribstring): ...Functionality for extracting meteorological data arrays, coordinate grids, and metadata from GRIB messages. Handles various grid types and supports data subsetting.
class gribmessage:
def latlons(self): ...
def data(self, lat1=None, lat2=None, lon1=None, lon2=None): ...
def keys(self): ...
def has_key(self, key): ...
def __getitem__(self, key): ...High-performance indexing system for fast message searching and retrieval. Creates optimized indexes on GRIB keys for efficient filtering of large files.
class index:
def __init__(self, filename, *args): ...
def select(self, **kwargs): ...
def write(self, filename): ...
def close(self): ...Limited capabilities for modifying existing GRIB messages and writing them to new files. Supports changing metadata keys and data values.
class gribmessage:
def __setitem__(self, key, value): ...
def tostring(self): ...
def reload(grb): ...Helper functions for coordinate transformations, date/time conversions, grid operations, and library configuration.
def redtoreg(redgrid_data, lonsperlat, missval=None): ...
def julian_to_datetime(jd): ...
def datetime_to_julian(d): ...
def gaulats(nlats): ...
def tolerate_badgrib_on(): ...
def tolerate_badgrib_off(): ...__version__: str = "2.1.6"
"""pygrib package version string"""
class open:
"""GRIB file iterator object"""
messages: int # Total number of messages
messagenumber: int # Current position
name: str # Filename
closed: bool # File status
has_multi_field_msgs: bool # Multi-field message flag
class gribmessage:
"""Individual GRIB message object"""
messagenumber: int # Message number in file
projparams: dict # Proj4 projection parameters
expand_reduced: bool # Grid expansion setting
fcstimeunits: str # Forecast time units
analDate: datetime # Analysis date
validDate: datetime # Valid forecast date
class index:
"""GRIB index for fast searching"""
keys: list # Indexed key names
types: list # Key type declarations