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

Add mode option for remote_open() #334

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,36 @@ def download(self, filename=None):
with open(filename, 'wb') as outfile:
outfile.write(infile.read())

def remote_open(self):
def remote_open(self, mode='b', encoding='ascii', errors='ignore'):
"""Open the remote dataset for random access.

Get a file-like object for reading from the remote dataset, providing random access,
similar to a local file.

Parameters
----------
mode : 'b' or 't', optional
Mode with which to open the remote data; 'b' for binary, 't' for text. Defaults
to 'b'.

encoding : str, optional
If ``mode`` is text, the encoding to use to decode the binary data into text.
Defaults to 'ascii'.

errors : str, optional
If ``mode`` is text, the error handling behavior to pass to `bytes.decode`.
Defaults to 'ignore'.

Returns
-------
A random access, file-like object

"""
return self.access_with_service('HTTPServer')
fobj = self.access_with_service('HTTPServer')
if mode == 't':
from io import StringIO
fobj = StringIO(fobj.read().decode(encoding, errors))
return fobj

def remote_access(self, service=None, use_xarray=None):
"""Access the remote dataset.
Expand Down Expand Up @@ -714,7 +732,7 @@ def access_with_service(self, service, use_xarray=None):
import xarray as xr
provider = xr.open_dataset
except ImportError:
raise ImportError('xarray to be installed if `use_xarray` is True.')
raise ImportError('xarray needs to be installed if `use_xarray` is True.')
else:
try:
from netCDF4 import Dataset as NC4Dataset
Expand Down
Loading