or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

data-extraction.mdfile-operations.mdindex-operations.mdindex.mdmessage-access.mdmessage-modification.mdutility-functions.md
tile.json

tessl/pypi-pygrib

Python module for reading/writing GRIB files using ECMWF ECCODES library

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pygrib@2.1.x

To install, run

npx @tessl/cli install tessl/pypi-pygrib@2.1.0

index.mddocs/

pygrib

A 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.

Package Information

  • Package Name: pygrib
  • Language: Python
  • Installation: pip install pygrib or conda install -c conda-forge pygrib

Core Imports

import pygrib

Basic Usage

import 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()

Architecture

pygrib's architecture centers around three main classes:

  • open: GRIB file iterator providing sequential and random access to messages
  • gribmessage: Individual GRIB message containing meteorological data and metadata
  • index: Fast search index for efficient message filtering by key/value criteria

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.

Capabilities

File Operations

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): ...

File Operations

Message Access and Selection

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): ...

Message Access

Data Extraction

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): ...

Data Extraction

Index Operations

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): ...

Index Operations

Message Modification

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): ...

Message Modification

Utility Functions

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(): ...

Utility Functions

Types

__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