From 4522a3447f342ed67ab25dddff8fb702fafd93fe Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Mon, 2 Nov 2020 16:01:53 +0100 Subject: [PATCH 1/3] test From c04a45fd74035559b1e2146580c039c57dddfa68 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Wed, 25 Nov 2020 20:47:18 +0100 Subject: [PATCH 2/3] fix get_xname etc. if there are no dimensions --- psyplot/data.py | 23 +++++++++++++---------- tests/test_data.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/psyplot/data.py b/psyplot/data.py index 023ec3b..d76418a 100755 --- a/psyplot/data.py +++ b/psyplot/data.py @@ -1057,7 +1057,8 @@ def get_xname(self, var, coords=None): PsyPlotRuntimeWarning) return dimlist[0] # otherwise we return the coordinate in the last position - return var.dims[-1] + if var.dims: + return var.dims[-1] @docstrings.get_sections(base="CFDecoder.get_y", sections=[ 'Parameters', 'Returns']) @@ -1126,9 +1127,10 @@ def get_yname(self, var, coords=None): return dimlist[0] # otherwise we return the coordinate in the last or second last # position - if self.is_unstructured(var): - return var.dims[-1] - return var.dims[-2 if var.ndim > 1 else -1] + if var.dims: + if self.is_unstructured(var): + return var.dims[-1] + return var.dims[-2 if var.ndim > 1 else -1] @docstrings.get_sections(base="CFDecoder.get_z", sections=[ 'Parameters', 'Returns']) @@ -1200,12 +1202,13 @@ def get_zname(self, var, coords=None): PsyPlotRuntimeWarning) return dimlist[0] # otherwise we return the coordinate in the third last position - is_unstructured = self.is_unstructured(var) - icheck = -2 if is_unstructured else -3 - min_dim = abs(icheck) if 'variable' not in var.dims else abs(icheck-1) - if var.ndim >= min_dim and var.dims[icheck] != self.get_tname( - var, coords): - return var.dims[icheck] + if var.dims: + is_unstructured = self.is_unstructured(var) + icheck = -2 if is_unstructured else -3 + min_dim = abs(icheck) if 'variable' not in var.dims else abs(icheck-1) + if var.ndim >= min_dim and var.dims[icheck] != self.get_tname( + var, coords): + return var.dims[icheck] return None @docstrings.get_sections(base="CFDecoder.get_t", sections=[ diff --git a/tests/test_data.py b/tests/test_data.py index 0906f82..3f45524 100755 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -150,6 +150,46 @@ def check_ds(name): check_ds(circ_name) ds.close() + def test_xname_no_dims(self): + """Test the get_xname method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_dim('x')) + + def test_yname_no_dims(self): + """Test the get_yname method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_dim('y')) + + def test_zname_no_dims(self): + """Test the get_zname method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_dim('z')) + + def test_tname_no_dims(self): + """Test the get_tname method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_dim('t')) + + def test_xcoord_no_dims(self): + """Test the get_x method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_coord('x')) + + def test_ycoord_no_dims(self): + """Test the get_y method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_coord('y')) + + def test_zcoord_no_dims(self): + """Test the get_z method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_coord('z')) + + def test_tcoord_no_dims(self): + """Test the get_t method for a variable without dimensions""" + da = xr.DataArray(1) + self.assertIsNone(da.psy.get_coord('t')) + def _test_coord(self, func_name, name, uname=None, name2d=False, circ_name=None): def check_ds(name): From 84dcb3df201c1d0a7c473260ec9e260a486587c7 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Wed, 25 Nov 2020 20:51:33 +0100 Subject: [PATCH 3/3] Add xname-fix to changelog --- CHANGELOG.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 133ae0d..6107d2b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,10 @@ +v1.3.2 +====== +Fixed +----- +- The ``get_xname``-like methods of the decoder have been fixed if they get a + variable without any dimensions. See `#30 `__ + v1.3.1 ======