Skip to content

Commit 4aa4cb1

Browse files
authored
Merge pull request psyplot#30 from Chilipp/fix/dimnames
Fix/dimnames
2 parents 36a23ce + 84dcb3d commit 4aa4cb1

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

CHANGELOG.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v1.3.2
2+
======
3+
Fixed
4+
-----
5+
- The ``get_xname``-like methods of the decoder have been fixed if they get a
6+
variable without any dimensions. See `#30 <https://github.com/psyplot/psyplot/pull/30>`__
7+
18
v1.3.1
29
======
310

psyplot/data.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,8 @@ def get_xname(self, var, coords=None):
10571057
PsyPlotRuntimeWarning)
10581058
return dimlist[0]
10591059
# otherwise we return the coordinate in the last position
1060-
return var.dims[-1]
1060+
if var.dims:
1061+
return var.dims[-1]
10611062

10621063
@docstrings.get_sections(base="CFDecoder.get_y", sections=[
10631064
'Parameters', 'Returns'])
@@ -1126,9 +1127,10 @@ def get_yname(self, var, coords=None):
11261127
return dimlist[0]
11271128
# otherwise we return the coordinate in the last or second last
11281129
# position
1129-
if self.is_unstructured(var):
1130-
return var.dims[-1]
1131-
return var.dims[-2 if var.ndim > 1 else -1]
1130+
if var.dims:
1131+
if self.is_unstructured(var):
1132+
return var.dims[-1]
1133+
return var.dims[-2 if var.ndim > 1 else -1]
11321134

11331135
@docstrings.get_sections(base="CFDecoder.get_z", sections=[
11341136
'Parameters', 'Returns'])
@@ -1200,12 +1202,13 @@ def get_zname(self, var, coords=None):
12001202
PsyPlotRuntimeWarning)
12011203
return dimlist[0]
12021204
# otherwise we return the coordinate in the third last position
1203-
is_unstructured = self.is_unstructured(var)
1204-
icheck = -2 if is_unstructured else -3
1205-
min_dim = abs(icheck) if 'variable' not in var.dims else abs(icheck-1)
1206-
if var.ndim >= min_dim and var.dims[icheck] != self.get_tname(
1207-
var, coords):
1208-
return var.dims[icheck]
1205+
if var.dims:
1206+
is_unstructured = self.is_unstructured(var)
1207+
icheck = -2 if is_unstructured else -3
1208+
min_dim = abs(icheck) if 'variable' not in var.dims else abs(icheck-1)
1209+
if var.ndim >= min_dim and var.dims[icheck] != self.get_tname(
1210+
var, coords):
1211+
return var.dims[icheck]
12091212
return None
12101213

12111214
@docstrings.get_sections(base="CFDecoder.get_t", sections=[

tests/test_data.py

+40
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@ def check_ds(name):
150150
check_ds(circ_name)
151151
ds.close()
152152

153+
def test_xname_no_dims(self):
154+
"""Test the get_xname method for a variable without dimensions"""
155+
da = xr.DataArray(1)
156+
self.assertIsNone(da.psy.get_dim('x'))
157+
158+
def test_yname_no_dims(self):
159+
"""Test the get_yname method for a variable without dimensions"""
160+
da = xr.DataArray(1)
161+
self.assertIsNone(da.psy.get_dim('y'))
162+
163+
def test_zname_no_dims(self):
164+
"""Test the get_zname method for a variable without dimensions"""
165+
da = xr.DataArray(1)
166+
self.assertIsNone(da.psy.get_dim('z'))
167+
168+
def test_tname_no_dims(self):
169+
"""Test the get_tname method for a variable without dimensions"""
170+
da = xr.DataArray(1)
171+
self.assertIsNone(da.psy.get_dim('t'))
172+
173+
def test_xcoord_no_dims(self):
174+
"""Test the get_x method for a variable without dimensions"""
175+
da = xr.DataArray(1)
176+
self.assertIsNone(da.psy.get_coord('x'))
177+
178+
def test_ycoord_no_dims(self):
179+
"""Test the get_y method for a variable without dimensions"""
180+
da = xr.DataArray(1)
181+
self.assertIsNone(da.psy.get_coord('y'))
182+
183+
def test_zcoord_no_dims(self):
184+
"""Test the get_z method for a variable without dimensions"""
185+
da = xr.DataArray(1)
186+
self.assertIsNone(da.psy.get_coord('z'))
187+
188+
def test_tcoord_no_dims(self):
189+
"""Test the get_t method for a variable without dimensions"""
190+
da = xr.DataArray(1)
191+
self.assertIsNone(da.psy.get_coord('t'))
192+
153193
def _test_coord(self, func_name, name, uname=None, name2d=False,
154194
circ_name=None):
155195
def check_ds(name):

0 commit comments

Comments
 (0)