-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
enable loading remote hdf5 files #2782
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
Changes from 6 commits
08aba0b
8ec34a6
b88b06e
48b23b6
4a7e560
2aa7349
1a4c4f3
94a3afe
7e82959
c067fa0
c99e8a6
73c022e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,8 +170,8 @@ def open_dataset(filename_or_obj, group=None, decode_cf=True, | |
| Strings and Path objects are interpreted as a path to a netCDF file | ||
| or an OpenDAP URL and opened with python-netCDF4, unless the filename | ||
| ends with .gz, in which case the file is gunzipped and opened with | ||
| scipy.io.netcdf (only netCDF3 supported). File-like objects are opened | ||
| with scipy.io.netcdf (only netCDF3 supported). | ||
| scipy.io.netcdf (only netCDF3 supported). Byte-strings or file-like | ||
| objects are opened by scipy.io.netcdf (netCDF3) or h5py (netCDF4/HDF). | ||
| group : str, optional | ||
| Path to the netCDF4 group in the given file to open (only works for | ||
| netCDF4 files). | ||
|
|
@@ -310,17 +310,9 @@ def maybe_decode_store(store, lock=False): | |
| if isinstance(filename_or_obj, backends.AbstractDataStore): | ||
| store = filename_or_obj | ||
| ds = maybe_decode_store(store) | ||
| elif isinstance(filename_or_obj, str): | ||
|
|
||
| if (isinstance(filename_or_obj, bytes) and | ||
| filename_or_obj.startswith(b'\x89HDF')): | ||
| raise ValueError('cannot read netCDF4/HDF5 file images') | ||
| elif (isinstance(filename_or_obj, bytes) and | ||
| filename_or_obj.startswith(b'CDF')): | ||
| # netCDF3 file images are handled by scipy | ||
| pass | ||
| elif isinstance(filename_or_obj, str): | ||
| filename_or_obj = _normalize_path(filename_or_obj) | ||
| elif isinstance(filename_or_obj, str): | ||
| filename_or_obj = _normalize_path(filename_or_obj) | ||
|
|
||
| if engine is None: | ||
| engine = _get_default_engine(filename_or_obj, | ||
|
|
@@ -352,11 +344,25 @@ def maybe_decode_store(store, lock=False): | |
| with close_on_error(store): | ||
| ds = maybe_decode_store(store) | ||
| else: | ||
| if engine is not None and engine != 'scipy': | ||
| raise ValueError('can only read file-like objects with ' | ||
| "default engine or engine='scipy'") | ||
| # assume filename_or_obj is a file-like object | ||
| store = backends.ScipyDataStore(filename_or_obj) | ||
| if engine not in [None, 'scipy', 'h5netcdf']: | ||
| raise ValueError('can only read bytes or file-like objects with ' | ||
| "engine = None, 'scipy', or 'h5netcdf'") | ||
| else: | ||
| if isinstance(filename_or_obj, bytes): | ||
| filename_or_obj = BytesIO(filename_or_obj) | ||
| # read first bytes of file-like object to determine engine | ||
| magic_number = filename_or_obj.read(8) | ||
| filename_or_obj.seek(0) | ||
| if magic_number.startswith(b'CDF'): | ||
| store = backends.ScipyDataStore(filename_or_obj, | ||
| **backend_kwargs) | ||
| elif magic_number.startswith(b'\211HDF\r\n\032\n'): | ||
| store = backends.H5NetCDFStore(filename_or_obj, group=group, | ||
| lock=lock, **backend_kwargs) | ||
| else: | ||
| print(magic_number) | ||
|
jhamman marked this conversation as resolved.
Outdated
|
||
| raise ValueError("file-like object is not a netCDF file: {}" | ||
| .format(filename_or_obj)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little concerned about giving users an error message about file-like objects if they passed in a Ideally this should give a useful error message, something like: |
||
| ds = maybe_decode_store(store) | ||
|
|
||
| # Ensure source filename always stored in dataset object (GH issue #2550) | ||
|
|
@@ -383,8 +389,8 @@ def open_dataarray(filename_or_obj, group=None, decode_cf=True, | |
| Strings and Paths are interpreted as a path to a netCDF file or an | ||
| OpenDAP URL and opened with python-netCDF4, unless the filename ends | ||
| with .gz, in which case the file is gunzipped and opened with | ||
| scipy.io.netcdf (only netCDF3 supported). File-like objects are opened | ||
| with scipy.io.netcdf (only netCDF3 supported). | ||
| scipy.io.netcdf (only netCDF3 supported). Byte-strings or file-like | ||
| objects are opened by scipy.io.netcdf (netCDF3) or h5py (netCDF4/HDF). | ||
| group : str, optional | ||
| Path to the netCDF4 group in the given file to open (only works for | ||
| netCDF4 files). | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.