Python module for reading/writing GRIB files using ECMWF ECCODES library
—
Core functionality for opening, reading, and managing GRIB files. The open class provides iterator-based access with file-like methods for navigation and message retrieval.
Creates a GRIB file iterator that can read from file paths, PathLike objects, or BufferedReader streams.
class open:
def __init__(self, filepath_or_buffer):
"""
Create GRIB file iterator.
Parameters:
- filepath_or_buffer: str, os.PathLike, or io.BufferedReader
Path to GRIB file or open file buffer
"""Usage example:
# Open from file path
grbs = pygrib.open('/path/to/weather.grb')
# Open from PathLike object
from pathlib import Path
grbs = pygrib.open(Path('/path/to/weather.grb'))
# Open from BufferedReader
with open('/path/to/weather.grb', 'rb') as f:
grbs = pygrib.open(f)Access basic information about the GRIB file.
class open:
@property
def messages(self) -> int:
"""Total number of GRIB messages in file"""
@property
def messagenumber(self) -> int:
"""Current message position (0-based)"""
@property
def name(self) -> str:
"""GRIB filename"""
@property
def closed(self) -> bool:
"""Whether file is closed"""
@property
def has_multi_field_msgs(self) -> bool:
"""Whether file contains multi-field messages"""Methods for reading GRIB messages from the file.
class open:
def read(self, msgs=None):
"""
Read multiple GRIB messages.
Parameters:
- msgs: int, optional
Number of messages to read. If None, reads all remaining messages.
Returns:
List of gribmessage objects
"""
def readline(self):
"""
Read next GRIB message from current position.
Returns:
gribmessage object
"""Usage example:
grbs = pygrib.open('weather.grb')
# Read all messages
all_messages = grbs.read()
# Read first 5 messages
grbs.seek(0)
first_five = grbs.read(5)
# Read one message at a time
grbs.seek(0)
first_msg = grbs.readline()
second_msg = grbs.readline()File-like methods for positioning within the GRIB file.
class open:
def tell(self) -> int:
"""
Get current message position.
Returns:
Current message number (0-based)
"""
def seek(self, msg: int, from_what: int = 0):
"""
Seek to specified message position.
Parameters:
- msg: int, message position
- from_what: int, reference point
0 = absolute position (default)
1 = relative to current position
2 = relative to end of file
"""
def rewind(self):
"""Reset iterator to beginning of file (same as seek(0))"""Usage example:
grbs = pygrib.open('weather.grb')
# Navigate to specific positions
grbs.seek(10) # Go to 11th message (0-based)
grbs.seek(5, 1) # Move 5 messages forward
grbs.seek(-3, 2) # Go to 3rd from last message
# Check current position
pos = grbs.tell() # Returns current message number
# Reset to beginning
grbs.rewind()Properly close GRIB files to free resources.
class open:
def close(self):
"""Close GRIB file and free resources"""
def __enter__(self):
"""Context manager entry"""
def __exit__(self, atype, value, traceback):
"""Context manager exit with automatic cleanup"""Usage example:
# Manual closing
grbs = pygrib.open('weather.grb')
# ... work with file ...
grbs.close()
# Context manager (recommended)
with pygrib.open('weather.grb') as grbs:
# ... work with file ...
pass # Automatically closedThe open class supports Python's iterator protocol for easy message iteration.
class open:
def __iter__(self):
"""Return iterator object"""
def __next__(self):
"""Get next message in iteration"""
def __len__(self) -> int:
"""Return total number of messages"""Usage example:
grbs = pygrib.open('weather.grb')
# Iterate through all messages
for grb in grbs:
print(grb)
# Get total count
total_messages = len(grbs)
# Convert to list
all_messages = list(grbs)Install with Tessl CLI
npx tessl i tessl/pypi-pygrib