Skip to content

Commit 6acc6f2

Browse files
authored
Merge pull request #19 from Chilipp/master
CFDecoder: default to dataset coords if not found in given coords
2 parents 122f3a8 + 62ccc91 commit 6acc6f2

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ see `#17 <https://github.com/psyplot/psyplot/pull/17>`__
99
with psy.plot.mapplot('file.nc') as sp:
1010
sp.export('output.png')
1111

12-
sp will be closed automatically (see commit `ee7415b <https://github.com/psyplot/psyplot/commit/ee7415befce61247b5a08d9cfafab96ceb06f6f8>`__)
12+
sp will be closed automatically (see `#18 <https://github.com/psyplot/psyplot/pull/18>`__)
1313

1414
Changed
1515
-------
@@ -18,6 +18,8 @@ Changed
1818
* Specifying names in `x`, `y`, `t` and `z` attributes of the `CFDecoder` class
1919
now means that any other attribute (such as the `coordinates` or `axis` attribute)
2020
are ignored
21+
* If a given variable cannot be found in the provided coords to ``CFDecoder.get_variable_by_axis``,
22+
we fall back to the ``CFDecoder.ds.coords`` attribute, see `#19 <https://github.com/psyplot/psyplot/pull/19>`__
2123

2224

2325
v1.2.1

‎psyplot/data.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
905905
See Also
906906
--------
907907
get_x, get_y, get_z, get_t"""
908+
909+
def get_coord(cname, raise_error=True):
910+
try:
911+
return coords[cname]
912+
except KeyError:
913+
if cname not in self.ds.coords:
914+
if raise_error:
915+
raise
916+
return None
917+
ret = self.ds.coords[cname]
918+
try:
919+
idims = var.psy.idims
920+
except AttributeError: # got xarray.Variable
921+
idims = {}
922+
return ret.isel(**{d: sl for d, sl in idims.items()
923+
if d in ret.dims})
924+
908925
axis = axis.lower()
909926
if axis not in list('xyzt'):
910927
raise ValueError("Axis must be one of X, Y, Z, T, not {0}".format(
@@ -949,23 +966,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
949966
if axis == 'x':
950967
for cname in filter(lambda cname: re.search('lon', cname),
951968
coord_names):
952-
return coords[cname]
953-
return coords.get(coord_names[-1])
969+
return get_coord(cname)
970+
return get_coord(coord_names[-1], raise_error=False)
954971
elif axis == 'y' and len(coord_names) >= 2:
955972
for cname in filter(lambda cname: re.search('lat', cname),
956973
coord_names):
957-
return coords[cname]
958-
return coords.get(coord_names[-2])
974+
return get_coord(cname)
975+
return get_coord(coord_names[-2], raise_error=False)
959976
elif (axis == 'z' and len(coord_names) >= 3 and
960977
coord_names[-3] not in tnames):
961-
return coords.get(coord_names[-3])
978+
return get_coord(coord_names[-3], raise_error=False)
962979
elif axis == 't' and tnames:
963980
tname = next(iter(tnames))
964981
if len(tnames) > 1:
965982
warn("Found multiple matches for time coordinate in the "
966983
"coordinates: %s. I use %s" % (', '.join(tnames), tname),
967984
PsyPlotRuntimeWarning)
968-
return coords.get(tname)
985+
return get_coord(tname, raise_error=False)
969986

970987
@docstrings.get_sectionsf("CFDecoder.get_x", sections=[
971988
'Parameters', 'Returns'])

‎tests/test_data.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ def test_get_variable_by_axis(self):
346346
# close the dataset
347347
ds.close()
348348

349+
def test_get_variable_by_axis_02(self):
350+
"""Test the :meth:`CFDecoder.get_variable_by_axis` method with missing
351+
coordinates, see https://github.com/psyplot/psyplot/pull/19"""
352+
fname = os.path.join(bt.test_dir, 'icon_test.nc')
353+
with psyd.open_dataset(fname) as ds:
354+
ds['ncells'] = ('ncells', np.arange(ds.dims['ncells']))
355+
decoder = psyd.CFDecoder(ds)
356+
arr = ds.psy['t2m'].psy.isel(ncells=slice(3, 10))
357+
del arr['clon']
358+
xcoord = decoder.get_variable_by_axis(arr, 'x', arr.coords)
359+
self.assertEqual(xcoord.name, 'clon')
360+
self.assertEqual(list(xcoord.ncells), list(arr.ncells))
361+
349362
def test_plot_bounds_1d(self):
350363
"""Test to get 2d-interval breaks"""
351364
x = xr.Variable(('x', ), np.arange(1, 5))

0 commit comments

Comments
 (0)