Sehaj Kaur
Sehaj Kaur

Reputation: 1

How to open grib2 file with xarray in python?

import xarray as xr
import cfgrib
import eccodes
import pandas as pd
data = xr.open_dataset(r"C:\Users\new\forecast_data.grib2", engine = "cfgrib")

I want to read this file using xarray. The error is:

unrecognized engine cfgrib must be one of: ['netcdf4', 'scipy', 'store']

eccodes is installed in my system.

I checked on Anaconda prompt

(nih) C:\Users\new>python -m eccodes selfcheck
C:\Users\new\anaconda3\envs\nih\Lib\site-packages\gribapi\__init__.py:23: UserWarning: ecCodes 2.31.0 or higher is recommended. You are running version 2.26.0
  warnings.warn(
Found: ecCodes v2.26.0.
Library: C:\Users\new\anaconda3\envs\nih\Library\bin\eccodes.dll
Definitions: /MEMFS/definitions
Samples: /MEMFS/samples
Your system is ready.

I want to read a grib2 file. I have previously worked with netCDF files. I need to read this grib2 file or convert it to netCDF so that I may open it using xarray. I tried pynio engine. It gave me the same result.

Upvotes: 0

Views: 703

Answers (1)

vht981230
vht981230

Reputation: 4936

From the documentation, looks like you can use cfgrib.open_datasets to convert grib2 file into an array of xarray.core.dataset.Dataset objects instead

import cfgrib
data = cfgrib.open_datasets(r"C:\Users\new\forecast_data.grib2")

Upvotes: 0

Related Questions