Skip to content

Commit 4bc8772

Browse files
authored
Merge pull request #334 from dopplershift/remote-open
Add mode option for remote_open()
2 parents 5cd1f16 + 9eef3c6 commit 4bc8772

File tree

3 files changed

+2289
-3
lines changed

3 files changed

+2289
-3
lines changed

siphon/catalog.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,18 +610,36 @@ def download(self, filename=None):
610610
with open(filename, 'wb') as outfile:
611611
outfile.write(infile.read())
612612

613-
def remote_open(self):
613+
def remote_open(self, mode='b', encoding='ascii', errors='ignore'):
614614
"""Open the remote dataset for random access.
615615
616616
Get a file-like object for reading from the remote dataset, providing random access,
617617
similar to a local file.
618618
619+
Parameters
620+
----------
621+
mode : 'b' or 't', optional
622+
Mode with which to open the remote data; 'b' for binary, 't' for text. Defaults
623+
to 'b'.
624+
625+
encoding : str, optional
626+
If ``mode`` is text, the encoding to use to decode the binary data into text.
627+
Defaults to 'ascii'.
628+
629+
errors : str, optional
630+
If ``mode`` is text, the error handling behavior to pass to `bytes.decode`.
631+
Defaults to 'ignore'.
632+
619633
Returns
620634
-------
621635
A random access, file-like object
622636
623637
"""
624-
return self.access_with_service('HTTPServer')
638+
fobj = self.access_with_service('HTTPServer')
639+
if mode == 't':
640+
from io import StringIO
641+
fobj = StringIO(fobj.read().decode(encoding, errors))
642+
return fobj
625643

626644
def remote_access(self, service=None, use_xarray=None):
627645
"""Access the remote dataset.
@@ -714,7 +732,7 @@ def access_with_service(self, service, use_xarray=None):
714732
import xarray as xr
715733
provider = xr.open_dataset
716734
except ImportError:
717-
raise ImportError('xarray to be installed if `use_xarray` is True.')
735+
raise ImportError('xarray needs to be installed if `use_xarray` is True.')
718736
else:
719737
try:
720738
from netCDF4 import Dataset as NC4Dataset

0 commit comments

Comments
 (0)