-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add logic for remote or local files in NetCDFSource #93
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ccfa3e
add can_be_local logic
scottyhq 592855e
add basic remote read test
scottyhq c59f25b
add a bunch of tests
scottyhq 25a9255
fix upstream test env
scottyhq 7d3670d
add local s3 tests
scottyhq c85ed42
use localhost for s3 test, boto3 from conda-forge
scottyhq dc2ec1f
add fake aws credentials
scottyhq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: test_env | ||
channels: | ||
- conda-forge | ||
dependencies: | ||
- python=3.9 | ||
- aiohttp | ||
- boto3 | ||
- flask | ||
- h5netcdf | ||
- intake | ||
- netcdf4 | ||
- pip | ||
- pydap | ||
- pytest | ||
- rasterio | ||
- s3fs | ||
- scikit-image | ||
- xarray | ||
- zarr | ||
- pip: | ||
- rangehttpserver | ||
- moto[s3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Tests that read public data over the internet | ||
import intake | ||
import pytest | ||
import xarray as xr | ||
import s3fs | ||
import gcsfs | ||
|
||
# RasterIOSource | ||
def test_open_rasterio_http(): | ||
prefix = 'https://landsat-pds.s3.us-west-2.amazonaws.com/L8/139/045' | ||
image = 'LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF' | ||
url = f'{prefix}/{image}' | ||
source = intake.open_rasterio(url, | ||
chunks=dict(band=1)) | ||
ds = source.to_dask() | ||
assert isinstance(ds, xr.core.dataarray.DataArray) | ||
|
||
|
||
def test_open_rasterio_s3(): | ||
bucket = 's3://landsat-pds' | ||
key = 'L8/139/045/LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF' | ||
url = f'{bucket}/{key}' | ||
source = intake.open_rasterio(url, | ||
chunks=dict(band=1), | ||
storage_options = dict(anon=True)) | ||
ds = source.to_dask() | ||
assert isinstance(ds, xr.core.dataarray.DataArray) | ||
|
||
|
||
# NETCDFSource | ||
def test_open_netcdf_gs(): | ||
bucket = 'gs://ldeo-glaciology' | ||
key = 'bedmachine/BedMachineAntarctica_2019-11-05_v01.nc' | ||
url = f'{bucket}/{key}' | ||
source = intake.open_netcdf(url, | ||
chunks=3000, | ||
xarray_kwargs=dict(engine='h5netcdf'), | ||
) | ||
ds = source.to_dask() | ||
assert isinstance(ds._file_obj, xr.backends.h5netcdf_.H5NetCDFStore) | ||
assert isinstance(ds, xr.core.dataarray.Dataset) | ||
|
||
def test_open_netcdf_s3(): | ||
bucket = 's3://its-live-data.jpl.nasa.gov' | ||
key = 'icesat2/alt06/rel003/ATL06_20181230162257_00340206_003_01.h5' | ||
url = f'{bucket}/{key}' | ||
source = intake.open_netcdf(url, | ||
xarray_kwargs=dict(group='gt1l/land_ice_segments', engine='h5netcdf'), | ||
storage_options=dict(anon=True), | ||
) | ||
ds = source.to_dask() | ||
assert isinstance(ds._file_obj, xr.backends.h5netcdf_.H5NetCDFStore) | ||
assert isinstance(ds, xr.core.dataarray.Dataset) | ||
|
||
|
||
def test_open_netcdf_s3_simplecache(): | ||
bucket = 's3://its-live-data.jpl.nasa.gov' | ||
key = 'icesat2/alt06/rel003/ATL06_20181230162257_00340206_003_01.h5' | ||
url = f'simplecache::{bucket}/{key}' | ||
source = intake.open_netcdf(url, | ||
xarray_kwargs=dict(group='gt1l/land_ice_segments', engine='h5netcdf'), | ||
storage_options=dict(s3={'anon': True}), | ||
) | ||
ds = source.to_dask() | ||
assert isinstance(ds._file_obj, xr.backends.h5netcdf_.H5NetCDFStore) | ||
assert isinstance(ds, xr.core.dataarray.Dataset) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is the key line. This is what I do to make http paths "openable" in my xarray / fsspec code. But @martindurant has suggested it's not a good practice to drag around open file objects without explicitly closing them.
I'm very curious to hear Martin's view of whether this is kosher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it doesn't feel right, but does make the tests pass! seems like we could end up with lots of unclosed files. The only other idea that's coming to mind is having to add code to xarray such that if a
OpenFile
instance is passed, a context manager gets used... But I'm definitely out of my depth hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will keep hold of the buffer until the file instance is cleaner up, but that's OK; the random-access version of the http file-like does not keep a socket open and doesn't need explicitly to be closed. The overall file system instance does hold a session open, but that should clean up on interpreter shutdown without problem.
The only time I could see this being a problem is where the path is something like an SSH tunnel (sftp) - but the garbage collection of a file-like is always supposed to work the same as calling
close()
or exiting a context.Short story: I think it's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has introduced a bug when using a list of url.
Whilst I understand the motivation for moving towards file-like objects - I wonder if the NetCDFSource should defer to the backend drivers provided by XArray to resolve local/remote/opendap URLs. Since the recent changes to fsspec for async it seems that something is broke for opendap endpoints (see fsspec/filesystem_spec#525).
Regardless perhaps the code should use fsspec.open_files instead to allow for a list of URLs:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xarray is not using fsspec for opendap endpoints, unless I am very confused
Totally agree that we should support a list, regardless.