pygrib

2.1.8 · active · verified Wed Apr 15

Pygrib is a Python module designed for reading and writing GRIB (General Regularly-distributed Information in Binary form) files, which is the World Meteorological Organization (WMO) standard format for weather data. It provides a high-level interface to the ECMWF ECCODES C library, supporting both GRIB edition 1 and edition 2. The library is currently at version 2.1.8 and sees active development with regular releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a GRIB file, read a message, and extract key information such as data values, latitudes, and longitudes. Note that a valid GRIB file is required to run this example effectively. The example includes a placeholder for a GRIB file path and a warning if a dummy file is used.

import pygrib
import os

# Create a dummy GRIB file for demonstration purposes
# In a real scenario, you would have an actual GRIB file.
# This example can't create a GRIB file from scratch due to pygrib's limitations,
# so we simulate reading one.

# For actual usage, replace 'sample.grib2' with your file path.
# If you don't have one, consider downloading a small sample GRIB file from a meteorological data source.
# Example: A minimalist GRIB file (not functional for this code but demonstrates intent)

# For this example, assume 'sample.grib2' exists and contains at least one message.
# Placeholder for a real GRIB file path
grib_file_path = os.environ.get('GRIB_SAMPLE_PATH', 'sample.grib2')

# Create a dummy file if it doesn't exist for the example to run without FileNotFoundError
if not os.path.exists(grib_file_path):
    print(f"Warning: '{grib_file_path}' not found. Cannot run quickstart without a GRIB file.\n" \
          "Please set the GRIB_SAMPLE_PATH environment variable or create 'sample.grib2'.")
    # Optionally create a minimal (non-functional) placeholder to prevent immediate crash
    with open(grib_file_path, 'wb') as f:
        f.write(b'GRIB-DUMMY-FILE') # Not a valid GRIB file, but prevents FileNotFoundError


try:
    grbs = pygrib.open(grib_file_path)

    # Read the first message
    # .read() returns a list of messages, so we take the first element.
    grb = grbs.read(1)[0]

    print(f"GRIB Message 1: {grb}")
    print(f"Short Name: {grb.shortName}")
    print(f"Parameter Name: {grb.name}")
    print(f"Units: {grb.units}")
    print(f"Valid Date: {grb.validDate}")

    # Get data values (as a numpy array)
    data = grb.values
    print(f"Data shape: {data.shape}")
    print(f"Data min/max: {data.min()}/{data.max()}")

    # Get latitudes and longitudes
    lats, lons = grb.latlons()
    print(f"Latitudes shape: {lats.shape}, Longitudes shape: {lons.shape}")

    grbs.close()

except Exception as e:
    print(f"An error occurred: {e}")
    if 'not a GRIB file' in str(e):
        print("This often means the GRIB_SAMPLE_PATH points to an invalid or empty file.")
    if os.path.exists(grib_file_path) and os.path.getsize(grib_file_path) < 100: # heuristic for dummy file
        print("The quickstart created a dummy file; please provide a real GRIB file for full functionality.")

view raw JSON →