Python interface to the ecCodes GRIB and BUFR decoder/encoder library for meteorological data processing
—
Comprehensive exception hierarchy for handling all types of errors that can occur during GRIB/BUFR processing, from file access issues to data validation failures.
class EcCodesError(Exception):
"""Base exception for all ecCodes errors."""
class GribInternalError(EcCodesError):
"""Internal error in GRIB processing."""
class BufferTooSmallError(EcCodesError):
"""Buffer size insufficient for operation."""class FileNotFoundError(EcCodesError):
"""File not found or cannot be opened."""
class IOProblemError(EcCodesError):
"""Input/output operation failed."""
class InvalidFileError(EcCodesError):
"""File format is invalid or corrupted."""
class PrematureEndOfFileError(EcCodesError):
"""Unexpected end of file encountered."""class KeyValueNotFoundError(EcCodesError):
"""Requested key not found in message."""
class InvalidKeyValueError(EcCodesError):
"""Key value is invalid or out of range."""
class ReadOnlyError(EcCodesError):
"""Attempted to modify read-only key."""
class WrongArraySizeError(EcCodesError):
"""Array size mismatch in operation."""class InvalidMessageError(EcCodesError):
"""Message format is invalid."""
class DecodingError(EcCodesError):
"""Error decoding message data."""
class EncodingError(EcCodesError):
"""Error encoding message data."""
class UnsupportedEditionError(EcCodesError):
"""Unsupported GRIB/BUFR edition."""class OutOfMemoryError(EcCodesError):
"""Insufficient memory for operation."""
class NullHandleError(EcCodesError):
"""Invalid or null message handle."""
class InvalidArgumentError(EcCodesError):
"""Invalid argument passed to function."""import eccodes
from eccodes import EcCodesError, FileNotFoundError, KeyValueNotFoundError
def safe_read_grib(filename):
try:
with open(filename, 'rb') as f:
msg = eccodes.codes_grib_new_from_file(f)
if msg is None:
print("No messages found in file")
return None
# Safe key access
try:
param_id = eccodes.codes_get(msg, 'paramId')
level = eccodes.codes_get(msg, 'level')
print(f"Parameter {param_id} at level {level}")
except KeyValueNotFoundError as e:
print(f"Key not found: {e}")
eccodes.codes_release(msg)
except FileNotFoundError:
print(f"File not found: {filename}")
except InvalidFileError:
print(f"Invalid file format: {filename}")
except EcCodesError as e:
print(f"ecCodes error: {e}")
# Usage
safe_read_grib('forecast.grib')Install with Tessl CLI
npx tessl i tessl/pypi-eccodes