Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
blaylockbk opened this issue Feb 9, 2022 · 3 comments

Comments

@blaylockbk
Copy link

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
Copy link
Author

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

Replace

if isinstance(filename, str):
opener = dataset.open_file
with

import pathlib

if isinstance(filename, (str, pathlib.PurePath))
    opener = dataset.open_file 

@iainrussell
Copy link
Member

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
Copy link
Author

Thanks for the quick fix, @iainrussell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants