From 3769aecd93bd10ee28a8037f1bc8f9b994917623 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Mon, 20 Apr 2020 17:17:45 +0200 Subject: [PATCH] add decoder parameter to the check_data method of plotmethods --- CHANGELOG.rst | 4 +++- psyplot/project.py | 17 +++++++++++++++-- tests/test_project.py | 16 ++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fb3b227..47b6356 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `__ + see `#20 `__. Furthermore, the + `check_data` method of the various plotmethods now also accept a `decoder` + parameter, see `#22 `__ * `psyplot.data.open_dataset` now decodes grid_mappings attributes, see `#17 `__ * psyplot projects now support the with syntax, e.g. something like:: diff --git a/psyplot/project.py b/psyplot/project.py index 5273e05..1c182a2 100755 --- a/psyplot/project.py +++ b/psyplot/project.py @@ -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 @@ -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 ------- @@ -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( diff --git a/tests/test_project.py b/tests/test_project.py index 0d64e60..210cebb 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -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')) @@ -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')