From 0d09c49c14e6cb865ba7cbb3feb232b2dcb08808 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Mon, 6 Aug 2018 16:50:04 -0600 Subject: [PATCH] ENH: Add the option to return xarray to catalog helpers (Fixes #229) For OPeNDAP and CDMRemote. --- siphon/catalog.py | 35 ++++++++++++++++++++--------- siphon/tests/test_catalog_access.py | 9 ++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/siphon/catalog.py b/siphon/catalog.py index 0ae912d73..3d2ff5cfc 100644 --- a/siphon/catalog.py +++ b/siphon/catalog.py @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/siphon/tests/test_catalog_access.py b/siphon/tests/test_catalog_access.py index fb3420cc1..e21c82c84 100644 --- a/siphon/tests/test_catalog_access.py +++ b/siphon/tests/test_catalog_access.py @@ -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."""