python-3.xpygrib

Recent Pygrib fail to retrieve ecmwf grib2 data within latitude and longitude


Previously I had no problem to retrieve rainfall data from real-time forecast ecmwf using this command;

1    import pygrib
2    import numpy as np
3    
4    source='./10-Download-ECMWF/'
5    grbs = pygrib.open(source+'10-data.grib2')  
6    grb2 = grbs.message(1) ### message start from 1, message 1=rainfall, mesage2=temp    
7    data, lats, lons = grb2.data(lat1=-10, lat2=20, lon1=90, lon2=130) 

Old data downloaded in my archive still can be used but after new download in June 2023 onwards the problem begins and showing this messages;

RuntimeError: b'Function not yet implemented'  #### the problem mentioned at line 7

Anyone got ideas?. Thanks in advance.


Solution

  • first of all try to upgrade your pygrib

    pip install --upgrade pygrib
    

    and if the issue persists,

    you can try another alternative library called pygrib2, so first install this :

    pip install ecmwf-api-client
    

    and now

    from ecmwfapi import ECMWFDataServer
    import pygrib2
    
    # ECMWF API credentials
    server = ECMWFDataServer(url="https://api.ecmwf.int/v1")
    
    # parameters for data retrieval
    dataset = "cams_gfas"
    date = "2023-06-01/to/2023-06-30"
    time = "00:00/06:00/12:00/18:00"
    area = "10/-10/20/130"
    target = "output.grib"
    
    # retrieve the GRIB file
    server.retrieve({
        "dataset": dataset,
        "date": date,
        "time": time,
        "area": area,
        "target": target,
        "format": "grib"
    })
    
    #open the downloaded GRIB file and retrieve the rainfall data, latitude, and longitude values
    grbs = pygrib2.open("output.grib")
    grb = grbs[1]  # Assuming rainfall is in the first message
    data = grb.values
    lats, lons = grb.latlons()
    
    #use the retrieved data as needed
    print(data)
    print(lats)
    print(lons)
    

    good luck !