Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TDS5 NCSS fixes #168

Merged
merged 4 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do .format?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, logging is old-style only.

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