diff --git a/siphon/catalog.py b/siphon/catalog.py index 878201537..f3703a175 100644 --- a/siphon/catalog.py +++ b/siphon/catalog.py @@ -4,6 +4,12 @@ from .metadata import TDSCatalogMetadata from .http_util import create_http_session, urlopen +try: + from urlparse import urljoin +except ImportError: + # Python 3 + from urllib.parse import urljoin + log = logging.getLogger("siphon.catalog") log.setLevel(logging.WARNING) @@ -104,7 +110,7 @@ def _process_dataset(self, element): self.datasets[ds.name] = ds def _process_catalog_ref(self, element): - catalog_ref = CatalogRef(element) + catalog_ref = CatalogRef(self.catalog_url, element) self.catalog_refs[catalog_ref.title] = catalog_ref def _process_metadata(self, element, tag_type): @@ -133,22 +139,31 @@ class CatalogRef(object): title : string Title of the catalogRef element """ - def __init__(self, element_node): + def __init__(self, base_url, element_node): r""" Initialize the catalogRef object. Parameters ---------- + base_url : String + URL to the base catalog that owns this reference element_node : Element An Element Tree Element representing a catalogRef node """ self.name = element_node.attrib["name"] - self.href = element_node.attrib["{http://www.w3.org/1999/xlink}href"] - if self.href[0] == '/': - self.href = self.href[1:] self.title = element_node.attrib["{http://www.w3.org/1999/xlink}title"] + # Resolve relative URLs + href = element_node.attrib["{http://www.w3.org/1999/xlink}href"] + self.href = urljoin(base_url, href) + + def follow(self): + r""" + Follow the reference, returning a new TDSCatalog + """ + return TDSCatalog(self.href) + class Dataset(object): r""" diff --git a/siphon/tests/test_catalog.py b/siphon/tests/test_catalog.py index a56b75bd4..4c8aed2e8 100644 --- a/siphon/tests/test_catalog.py +++ b/siphon/tests/test_catalog.py @@ -75,3 +75,9 @@ def test_html_link(self): 'grib/NCEP/RAP/CONUS_13km/catalog.html') cat = TDSCatalog(url) assert cat + + def test_catalog_follow(self): + url = 'http://thredds-test.unidata.ucar.edu/thredds/testDatasets.xml' + ref_name = 'TestFmrc' + cat = TDSCatalog(url).catalog_refs[ref_name].follow() + assert cat