Skip to content

Commit d979e4c

Browse files
authored
Merge pull request #22 from Chilipp/master
NEW FEATURE: add decoder parameter to the check_data method of plotmethods
2 parents e5e3fc7 + 3769aec commit d979e4c

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Added
66
different input types for the decoder. You can pass an instance of the
77
``CFDecoder`` class, a sub class of ``CFDecoder``, or keyword arguments
88
that are used to initialize the decoder,
9-
see `#20 <https://github.com/psyplot/psyplot/pull/20>`__
9+
see `#20 <https://github.com/psyplot/psyplot/pull/20>`__. Furthermore, the
10+
`check_data` method of the various plotmethods now also accept a `decoder`
11+
parameter, see `#22 <https://github.com/psyplot/psyplot/pull/22>`__
1012
* `psyplot.data.open_dataset` now decodes grid_mappings attributes,
1113
see `#17 <https://github.com/psyplot/psyplot/pull/17>`__
1214
* psyplot projects now support the with syntax, e.g. something like::

‎psyplot/project.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ def docs(self, *args, **kwargs):
18571857
return self.plotter_cls.show_docs(*args, **kwargs)
18581858

18591859
@docstrings.dedent
1860-
def check_data(self, ds, name, dims):
1860+
def check_data(self, ds, name, dims, decoder=None, *args, **kwargs):
18611861
"""
18621862
A validation method for the data shape
18631863
@@ -1872,6 +1872,10 @@ def check_data(self, ds, name, dims):
18721872
dimensions of this plot method
18731873
is_unstructured: bool or list of bool
18741874
True if the corresponding array is unstructured.
1875+
decoder: :class:`psyplot.data.CFDecoder`, dict or a list of them
1876+
The decoders to use per array. Dictionaries are parsed as keyword
1877+
arguments to the :meth:`psyplot.data.CFDecoder.get_decoder`
1878+
method
18751879
18761880
Returns
18771881
-------
@@ -1880,10 +1884,19 @@ def check_data(self, ds, name, dims):
18801884
if isinstance(name, six.string_types):
18811885
name = [name]
18821886
dims = [dims]
1887+
decoders = [decoder]
18831888
else:
18841889
dims = list(dims)
1890+
decoders = list(decoder if decoder is not None else [None])
18851891
variables = [ds[safe_list(n)[0]] for n in name]
1886-
decoders = [CFDecoder.get_decoder(ds, var) for var in variables]
1892+
if decoders is None:
1893+
decoders = [CFDecoder.get_decoder(ds, var) for var in variables]
1894+
else:
1895+
for i, (decoder, var) in enumerate(zip(decoders, variables)):
1896+
if decoder is None:
1897+
decoder = {}
1898+
if isinstance(decoder, dict):
1899+
decoders[i] = CFDecoder.get_decoder(ds, var, **decoder)
18871900
default_slice = slice(None) if self._default_slice is None else \
18881901
self._default_slice
18891902
for i, (dim_dict, var, decoder) in enumerate(zip(

‎tests/test_project.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,10 +1492,17 @@ def check_data(cls, name, dims, is_unstructured):
14921492
checks, messages = super(TestPlotter, cls).check_data(
14931493
name, dims, is_unstructured)
14941494
self.assertEqual(name, ['t2m'])
1495+
14951496
for n, d in zip(name, dims):
1497+
if test_x: # changed x-coordinate
1498+
removed = {'lev', 'time'}
1499+
else:
1500+
removed = {'lev', 'lon'}
14961501
self.assertEqual(len(d),
1497-
len(set(ds.t2m.dims) - {'lev', 'lon'}))
1498-
self.assertEqual(set(d), set(ds.t2m.dims) - {'lev', 'lon'})
1502+
len(set(ds.t2m.dims) - removed))
1503+
self.assertEqual(set(d), set(ds.t2m.dims) - removed)
1504+
1505+
test_x = False
14991506

15001507
ds = psy.open_dataset(bt.get_file('test-t2m-u-v.nc'))
15011508

@@ -1504,6 +1511,11 @@ def check_data(cls, name, dims, is_unstructured):
15041511
default_dims={'x': 1}, default_slice=slice(1, 3))
15051512

15061513
psy.plot.test_plotter.check_data(ds, 't2m', {'lev': 3})
1514+
1515+
test_x = True
1516+
psy.plot.test_plotter.check_data(ds, 't2m', {'lev': 3},
1517+
{'x': {'time'}})
1518+
15071519
psy.unregister_plotter('test_plotter')
15081520

15091521

0 commit comments

Comments
 (0)