Skip to content
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
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Added
different input types for the decoder. You can pass an instance of the
``CFDecoder`` class, a sub class of ``CFDecoder``, or keyword arguments
that are used to initialize the decoder,
see `#20 <https://github.com/psyplot/psyplot/pull/20>`__
see `#20 <https://github.com/psyplot/psyplot/pull/20>`__. Furthermore, the
`check_data` method of the various plotmethods now also accept a `decoder`
parameter, see `#22 <https://github.com/psyplot/psyplot/pull/22>`__
* `psyplot.data.open_dataset` now decodes grid_mappings attributes,
see `#17 <https://github.com/psyplot/psyplot/pull/17>`__
* psyplot projects now support the with syntax, e.g. something like::
Expand Down
17 changes: 15 additions & 2 deletions psyplot/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ def docs(self, *args, **kwargs):
return self.plotter_cls.show_docs(*args, **kwargs)

@docstrings.dedent
def check_data(self, ds, name, dims):
def check_data(self, ds, name, dims, decoder=None, *args, **kwargs):
"""
A validation method for the data shape

Expand All @@ -1872,6 +1872,10 @@ def check_data(self, ds, name, dims):
dimensions of this plot method
is_unstructured: bool or list of bool
True if the corresponding array is unstructured.
decoder: :class:`psyplot.data.CFDecoder`, dict or a list of them
The decoders to use per array. Dictionaries are parsed as keyword
arguments to the :meth:`psyplot.data.CFDecoder.get_decoder`
method

Returns
-------
Expand All @@ -1880,10 +1884,19 @@ def check_data(self, ds, name, dims):
if isinstance(name, six.string_types):
name = [name]
dims = [dims]
decoders = [decoder]
else:
dims = list(dims)
decoders = list(decoder if decoder is not None else [None])
variables = [ds[safe_list(n)[0]] for n in name]
decoders = [CFDecoder.get_decoder(ds, var) for var in variables]
if decoders is None:
decoders = [CFDecoder.get_decoder(ds, var) for var in variables]
else:
for i, (decoder, var) in enumerate(zip(decoders, variables)):
if decoder is None:
decoder = {}
if isinstance(decoder, dict):
decoders[i] = CFDecoder.get_decoder(ds, var, **decoder)
default_slice = slice(None) if self._default_slice is None else \
self._default_slice
for i, (dim_dict, var, decoder) in enumerate(zip(
Expand Down
16 changes: 14 additions & 2 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,10 +1492,17 @@ def check_data(cls, name, dims, is_unstructured):
checks, messages = super(TestPlotter, cls).check_data(
name, dims, is_unstructured)
self.assertEqual(name, ['t2m'])

for n, d in zip(name, dims):
if test_x: # changed x-coordinate
removed = {'lev', 'time'}
else:
removed = {'lev', 'lon'}
self.assertEqual(len(d),
len(set(ds.t2m.dims) - {'lev', 'lon'}))
self.assertEqual(set(d), set(ds.t2m.dims) - {'lev', 'lon'})
len(set(ds.t2m.dims) - removed))
self.assertEqual(set(d), set(ds.t2m.dims) - removed)

test_x = False

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

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

psy.plot.test_plotter.check_data(ds, 't2m', {'lev': 3})

test_x = True
psy.plot.test_plotter.check_data(ds, 't2m', {'lev': 3},
{'x': {'time'}})

psy.unregister_plotter('test_plotter')


Expand Down