Skip to content

Commit

Permalink
Merge pull request #168 from dopplershift/tds5-fixes
Browse files Browse the repository at this point in the history
TDS5 NCSS fixes
  • Loading branch information
dopplershift authored Nov 3, 2017
2 parents 7c3df4f + e7a95b2 commit 03d1047
Show file tree
Hide file tree
Showing 4 changed files with 2,797 additions and 21 deletions.
2 changes: 0 additions & 2 deletions siphon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions

__all__ = ['catalog', 'testing', 'http_util', 'upperair']
34 changes: 21 additions & 13 deletions siphon/ncss_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import print_function

import logging
import re

import numpy as np

Expand All @@ -14,6 +15,13 @@
log.addHandler(logging.StreamHandler())


def _without_namespace(tagname):
"""Remove the xml namespace from a tag name."""
if '}' in tagname:
return tagname.rsplit('}', 1)[-1]
return tagname


class _Types(object):
@staticmethod
def handle_typed_values(val, type_name, value_type):
Expand Down Expand Up @@ -69,16 +77,14 @@ def handle_typed_values(val, type_name, value_type):
"""
if value_type in ['byte', 'short', 'int', 'long']:
try:
val = val.split()
val = list(map(int, val))
val = [int(v) for v in re.split('[ ,]', val) if v]
except ValueError:
log.warning('Cannot convert %s to int. Keeping type as str.', val)
log.warning('Cannot convert "%s" to int. Keeping type as str.', val)
elif value_type in ['float', 'double']:
try:
val = val.split()
val = list(map(float, val))
val = [float(v) for v in re.split('[ ,]', val) if v]
except ValueError:
log.warning('Cannot convert %s to float. Keeping type as str.', val)
log.warning('Cannot convert "%s" to float. Keeping type as str.', val)
elif value_type == 'boolean':
try:
# special case for boolean type
Expand Down Expand Up @@ -191,6 +197,14 @@ def handle_featureDataset(element): # noqa
def handle_variable(self, element):
return self.handle_grid(element)

def lookup(self, handler_name):
handler_name = 'handle_' + _without_namespace(handler_name)
if handler_name in dir(self):
return getattr(self, handler_name)
else:
msg = 'cannot find handler for element {}'.format(handler_name)
log.warning(msg)


class NCSSDataset(object):
"""Hold information contained in the dataset.xml NCSS document.
Expand Down Expand Up @@ -245,7 +259,6 @@ def __init__(self, element):
"""
self._types = _Types()
self._types_methods = _Types.__dict__

self.gridsets = {}
self.variables = {}
Expand Down Expand Up @@ -279,12 +292,7 @@ def __init__(self, element):
delattr(self, thing)

def _get_handler(self, handler_name):
handler_name = 'handle_' + handler_name
if handler_name in self._types_methods:
return getattr(self._types, handler_name)
else:
msg = 'cannot find handler for element {}'.format(handler_name)
log.warning(msg)
return self._types.lookup(handler_name)

def _parse_element(self, element):
element_name = element.tag
Expand Down
Loading

0 comments on commit 03d1047

Please sign in to comment.