Skip to content

Commit

Permalink
Merge pull request #42 from dopplershift/cleanup
Browse files Browse the repository at this point in the history
Clean-ups before release.
  • Loading branch information
dopplershift committed May 14, 2015
2 parents 625bb92 + f7fadee commit 5b4b97a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ MetPy is still in an early stage of development, and as such
just for fun, many things may still change as we work through
design issues.

Important Links
---------------

- Source code repository: https://github.com/MetPy/MetPy
- HTML Documentation (stable release): http://metpy.readthedocs.org/en/stable/
- HTML Documentation (development): http://metpy.readthedocs.org/en/latest/
- Issue tracker: http://github.com/Metpy/MetPy/issues

Dependencies
------------
Other required packages:

- Numpy
- Scipy
- Matplotlib
- Pint
32 changes: 21 additions & 11 deletions examples/notebooks/NEXRAD_Level_3_File.ipynb

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions metpy/plots/ctables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import glob
import os.path
import posixpath
Expand All @@ -7,6 +8,12 @@
TABLE_EXT = '.tbl'


def _parse(s):
if hasattr(s, 'decode'):
s = s.decode('ascii')
return ast.literal_eval(s)


def read_colortable(fobj):
r'''Read colortable information from a file
Expand All @@ -25,7 +32,7 @@ def read_colortable(fobj):
A list of the RGB color values, where each RGB color is a tuple of 3 floats in the
range of [0, 1].
'''
return [mcolors.colorConverter.to_rgb(eval(line)) for line in fobj]
return [mcolors.colorConverter.to_rgb(_parse(line)) for line in fobj]


class ColortableRegistry(dict):
Expand Down Expand Up @@ -78,7 +85,7 @@ def add_colortable(self, fobj, name):

self[name] = read_colortable(fobj)

def get_with_limits(self, name, start, step):
def get_with_steps(self, name, start, step):
r'''Get a colortable from the registry with a corresponding norm.
Builds a `matplotlib.colors.BoundaryNorm` using `start`, `step`, and
Expand All @@ -100,10 +107,12 @@ def get_with_limits(self, name, start, step):
from the number of entries matching the colortable, and the colortable itself.
'''

import numpy as np
cmap = mcolors.ListedColormap(self[name])
boundaries = np.linspace(start, step * cmap.N, cmap.N)
return mcolors.BoundaryNorm(boundaries, cmap.N), cmap
from numpy import arange

# Need one more boundary than color
num_steps = len(self[name]) + 1
boundaries = arange(start, step * num_steps, step)
return self.get_with_boundaries(name, boundaries)

def get_with_boundaries(self, name, boundaries):
r'''Get a colortable from the registry with a corresponding norm.
Expand All @@ -123,7 +132,7 @@ def get_with_boundaries(self, name, boundaries):
The boundary norm based on `boundaries`, and the colortable itself.
'''

cmap = mcolors.ListedColormap(self[name])
cmap = self.get_colortable(name)
return mcolors.BoundaryNorm(boundaries, cmap.N), cmap

def get_colortable(self, name):
Expand All @@ -140,7 +149,7 @@ def get_colortable(self, name):
The colortable corresponding to `name`
'''

return mcolors.ListedColormap(dict.get(self, name))
return mcolors.ListedColormap(self.get(name), name=name)


registry = ColortableRegistry()
Expand Down
48 changes: 36 additions & 12 deletions metpy/plots/tests/test_ctables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,56 @@
from io import StringIO
buffer_args = dict(buffering=1)

import numpy as np
from nose.tools import eq_
from metpy.plots.ctables import ColortableRegistry


class TestColortableRegistry(object):
def setUp(self): # noqa
self.reg = ColortableRegistry()

def test_package_resource(self):
reg = ColortableRegistry()
reg.scan_resource('metpy.plots', 'nexrad_tables')
assert 'cc_table' in reg
self.reg.scan_resource('metpy.plots', 'nexrad_tables')
assert 'cc_table' in self.reg

def test_scan_dir(self):
with tempfile.NamedTemporaryFile(mode='w', dir='.', suffix='.tbl',
**buffer_args) as fobj:
fobj.write('"red"\n"lime"\n"blue"\n')
reg = ColortableRegistry()
reg.scan_dir(os.path.dirname(fobj.name))
self.reg.scan_dir(os.path.dirname(fobj.name))
name = os.path.splitext(os.path.basename(fobj.name))[0]

assert name in reg
eq_(reg[name], [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)])
assert name in self.reg
eq_(self.reg[name], [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)])

def test_read_file(self):
fobj = StringIO('(0., 0., 1.0)\n"red"\n"#0000FF"')

reg = ColortableRegistry()
reg.add_colortable(fobj, 'test_table')

assert 'test_table' in reg
assert reg['test_table'] == [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)]
self.reg.add_colortable(fobj, 'test_table')

assert 'test_table' in self.reg
assert self.reg['test_table'] == [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)]

def test_get_colortable(self):
true_colors = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0)]
self.reg['table'] = true_colors

table = self.reg.get_colortable('table')
eq_(table.N, 2)
eq_(table.colors, true_colors)

def test_get_steps(self):
self.reg['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]
norm, cmap = self.reg.get_with_steps('table', 5., 10.)
eq_(cmap(norm(np.array([6.]))).tolist(), [[0.0, 0.0, 1.0, 1.0]])
eq_(cmap(norm(np.array([14.9]))).tolist(), [[0.0, 0.0, 1.0, 1.0]])
eq_(cmap(norm(np.array([15.1]))).tolist(), [[1.0, 0.0, 0.0, 1.0]])
eq_(cmap(norm(np.array([26.]))).tolist(), [[0.0, 1.0, 0.0, 1.0]])

def test_get_boundaries(self):
self.reg['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]
norm, cmap = self.reg.get_with_boundaries('table', [0., 8., 10., 20.])
eq_(cmap(norm(np.array([7.]))).tolist(), [[0.0, 0.0, 1.0, 1.0]])
eq_(cmap(norm(np.array([9.]))).tolist(), [[1.0, 0.0, 0.0, 1.0]])
eq_(cmap(norm(np.array([10.1]))).tolist(), [[0.0, 1.0, 0.0, 1.0]])
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def run(self):
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Scientific/Engineering',
Expand Down

0 comments on commit 5b4b97a

Please sign in to comment.