Skip to content

Commit

Permalink
Merge pull request #243 from dopplershift/xarray-access
Browse files Browse the repository at this point in the history
ENH: Add the option to return xarray from catalog helpers (Fixes #229)
  • Loading branch information
dopplershift authored Aug 14, 2018
2 parents 6771749 + 0d09c49 commit 4910d1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
35 changes: 25 additions & 10 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def remote_open(self):
"""
return self.access_with_service('HTTPServer')

def remote_access(self, service=None):
def remote_access(self, service=None, use_xarray=None):
"""Access the remote dataset.
Open the remote dataset and get a netCDF4-compatible `Dataset` object providing
Expand All @@ -619,7 +619,7 @@ def remote_access(self, service=None):
if service not in (CaseInsensitiveStr('CdmRemote'), CaseInsensitiveStr('OPENDAP')):
raise ValueError(service + ' is not a valid service for remote_access')

return self.access_with_service(service)
return self.access_with_service(service, use_xarray)

def subset(self, service=None):
"""Subset the dataset.
Expand Down Expand Up @@ -651,7 +651,7 @@ def subset(self, service=None):

return self.access_with_service(service)

def access_with_service(self, service):
def access_with_service(self, service, use_xarray=None):
"""Access the dataset using a particular service.
Return an Python object capable of communicating with the server using the particular
Expand All @@ -670,14 +670,29 @@ def access_with_service(self, service):
"""
service = CaseInsensitiveStr(service)
if service == 'CdmRemote':
from .cdmr import Dataset as CDMRDataset
provider = CDMRDataset
if use_xarray:
from .cdmr.xarray_support import CDMRemoteStore
try:
import xarray as xr
provider = lambda url: xr.open_dataset(CDMRemoteStore(url)) # noqa: E731
except ImportError:
raise ImportError('CdmRemote access needs xarray to be installed.')
else:
from .cdmr import Dataset as CDMRDataset
provider = CDMRDataset
elif service == 'OPENDAP':
try:
from netCDF4 import Dataset as NC4Dataset
provider = NC4Dataset
except ImportError:
raise ImportError('OPENDAP access requires netCDF4-python to be installed.')
if use_xarray:
try:
import xarray as xr
provider = xr.open_dataset
except ImportError:
raise ImportError('xarray to be installed if `use_xarray` is True.')
else:
try:
from netCDF4 import Dataset as NC4Dataset
provider = NC4Dataset
except ImportError:
raise ImportError('OPENDAP access needs netCDF4-python to be installed.')
elif service in self.ncssServiceNames:
from .ncss import NCSS
provider = NCSS
Expand Down
9 changes: 9 additions & 0 deletions siphon/tests/test_catalog_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def test_dataset_remote_access_cdmr(nids_url):
assert ds.title == 'Nexrad Level 3 Data'


@recorder.use_cassette('cat_to_cdmr')
def test_dataset_remote_access_cdmr_xarray(nids_url):
"""Test using the remote_access method to request CDMR using xarray."""
cat = TDSCatalog(nids_url)
ds = cat.datasets[0].remote_access(use_xarray=True)
assert not ds.variables
assert ds.attrs['title'] == 'Nexrad Level 3 Data'


@recorder.use_cassette('cat_to_open')
def test_dataset_download(nids_url):
"""Test using the download method to download entire dataset."""
Expand Down

0 comments on commit 4910d1e

Please sign in to comment.