Skip to content

Commit

Permalink
ENH: Add latest attribute to catalog
Browse files Browse the repository at this point in the history
This gives access to the latest dataset. Partially addresses Unidata#137.
  • Loading branch information
dopplershift committed Jul 21, 2017
1 parent b4a3930 commit 49e7057
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
58 changes: 15 additions & 43 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ def _process_datasets(self):
else:
self.datasets.pop(dsName)

@property
def latest(self):
"""Get the latest dataset, if available."""
for service in self.services:
if service.is_resolver():
latest_cat = self.catalog_url.replace('catalog.xml', 'latest.xml')
return TDSCatalog(latest_cat).datasets[0]
raise AttributeError('"latest" not available for this catalog')


class CatalogRef(object):
"""
Expand Down Expand Up @@ -580,8 +589,7 @@ class SimpleService(object):
"""

def __init__(self, service_node):
"""
Initialize the Dataset object.
"""Initialize the Dataset object.
Parameters
----------
Expand All @@ -594,6 +602,10 @@ def __init__(self, service_node):
self.base = service_node.attrib['base']
self.access_urls = {}

def is_resolver(self):
"""Return whether the service is a resolver service."""
return self.service_type.lower() == 'resolver'


class CompoundService(object):
"""Hold information about compound services.
Expand Down Expand Up @@ -644,29 +656,6 @@ def _find_base_tds_url(catalog_url):
return catalog_url


def _get_latest_cat(catalog_url):
"""Get the latest dataset catalog from the supplied top level dataset catalog url.
Parameters
----------
catalog_url : str
The URL of a top level data catalog
Returns
-------
TDSCatalog
A TDSCatalog object containing the information from the latest dataset
"""
cat = TDSCatalog(catalog_url)
for service in cat.services:
if service.service_type.lower() == 'resolver':
latest_cat = cat.catalog_url.replace('catalog.xml', 'latest.xml')
return TDSCatalog(latest_cat)

log.error('ERROR: "latest" service not enabled for this catalog!')


def get_latest_access_url(catalog_url, access_method):
"""Get the data access url to the latest data using a specified access method.
Expand All @@ -688,21 +677,4 @@ def get_latest_access_url(catalog_url, access_method):
but not always.
"""
latest_cat = _get_latest_cat(catalog_url)
if latest_cat != '':
if len(list(latest_cat.datasets.keys())) > 0:
latest_ds = []
for lds_name in latest_cat.datasets:
lds = latest_cat.datasets[lds_name]
if access_method in lds.access_urls:
latest_ds.append(lds.access_urls[access_method])
if len(latest_ds) == 1:
latest_ds = latest_ds[0]
return latest_ds
else:
log.error('ERROR: More than one latest dataset found '
'this case is currently not suppored in '
'siphon.')
else:
log.error('ERROR: More than one access url matching the '
'requested access method...clearly this is an error')
return TDSCatalog(catalog_url).latest.access_urls[access_method]
9 changes: 9 additions & 0 deletions siphon/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def test_get_latest():
assert latest_url


@recorder.use_cassette('latest_rap_catalog')
def test_latest_attribute():
"""Test using the catalog latest attribute."""
url = ('http://thredds-test.unidata.ucar.edu/thredds/catalog/'
'grib/NCEP/RAP/CONUS_13km/catalog.xml')
cat = TDSCatalog(url)
assert cat.latest.name == 'RR_CONUS_13km_20150527_0100.grib2'


@recorder.use_cassette('top_level_cat')
def test_tds_top_catalog():
"""Test parsing top-level catalog."""
Expand Down

0 comments on commit 49e7057

Please sign in to comment.