ecmwf / cfgrib

A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes
Apache License 2.0
409 stars 77 forks source link

cfgrib.open_datasets no longer opens pathlib.Path files: 'PosixPath' object is not iterable #282

Closed blaylockbk closed 2 years ago

blaylockbk commented 2 years ago

cfgrib 0.9.10.0 hits an error when attempting to open a file where the filename is expressed as a pathlib.Path object instead of a string. This is not a problem in cfgrib 0.9.9.1.

import cfgrib
from pathlib import Path

this_file = Path("/path/to/my/grib2/file.grib2")
cfgrib.open_datasets(this_file)

TypeError: 'PosixPath' object is not iterable

The same is true with xarray: xr.open_dataset(this_file, engine='cfgrib')

blaylockbk commented 2 years ago

I'm not sure why this broke since 0.9.9.1, but is this on the right path to resolving this?

Replace https://github.com/ecmwf/cfgrib/blob/a0d9763ae5c14b0b180c15ac1090b695e437554c/cfgrib/xarray_plugin.py#L35-L36 with

import pathlib

if isinstance(filename, (str, pathlib.PurePath))
    opener = dataset.open_file 
iainrussell commented 2 years ago

Hi @blaylockbk ,

Many thanks for bringing this to our attention. Indeed your fix is absolutely correct! I implemented it myself, but in fact ended up doing exactly the same as your suggestion. The difference in the most recent version is that cfgrib can take more complex objects than strings and paths, and the code assumed that the input was 'string or complex object' but that was not quite sufficient.

Cheers, Iain

blaylockbk commented 2 years ago

Thanks for the quick fix, @iainrussell