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 @@ -9,7 +9,7 @@ see `#17 <https://github.com/psyplot/psyplot/pull/17>`__
with psy.plot.mapplot('file.nc') as sp:
sp.export('output.png')

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

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


v1.2.1
Expand Down
29 changes: 23 additions & 6 deletions psyplot/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
See Also
--------
get_x, get_y, get_z, get_t"""

def get_coord(cname, raise_error=True):
try:
return coords[cname]
except KeyError:
if cname not in self.ds.coords:
if raise_error:
raise
return None
ret = self.ds.coords[cname]
try:
idims = var.psy.idims
except AttributeError: # got xarray.Variable
idims = {}
return ret.isel(**{d: sl for d, sl in idims.items()
if d in ret.dims})

axis = axis.lower()
if axis not in list('xyzt'):
raise ValueError("Axis must be one of X, Y, Z, T, not {0}".format(
Expand Down Expand Up @@ -949,23 +966,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
if axis == 'x':
for cname in filter(lambda cname: re.search('lon', cname),
coord_names):
return coords[cname]
return coords.get(coord_names[-1])
return get_coord(cname)
return get_coord(coord_names[-1], raise_error=False)
elif axis == 'y' and len(coord_names) >= 2:
for cname in filter(lambda cname: re.search('lat', cname),
coord_names):
return coords[cname]
return coords.get(coord_names[-2])
return get_coord(cname)
return get_coord(coord_names[-2], raise_error=False)
elif (axis == 'z' and len(coord_names) >= 3 and
coord_names[-3] not in tnames):
return coords.get(coord_names[-3])
return get_coord(coord_names[-3], raise_error=False)
elif axis == 't' and tnames:
tname = next(iter(tnames))
if len(tnames) > 1:
warn("Found multiple matches for time coordinate in the "
"coordinates: %s. I use %s" % (', '.join(tnames), tname),
PsyPlotRuntimeWarning)
return coords.get(tname)
return get_coord(tname, raise_error=False)

@docstrings.get_sectionsf("CFDecoder.get_x", sections=[
'Parameters', 'Returns'])
Expand Down
13 changes: 13 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ def test_get_variable_by_axis(self):
# close the dataset
ds.close()

def test_get_variable_by_axis_02(self):
"""Test the :meth:`CFDecoder.get_variable_by_axis` method with missing
coordinates, see https://github.com/psyplot/psyplot/pull/19"""
fname = os.path.join(bt.test_dir, 'icon_test.nc')
with psyd.open_dataset(fname) as ds:
ds['ncells'] = ('ncells', np.arange(ds.dims['ncells']))
decoder = psyd.CFDecoder(ds)
arr = ds.psy['t2m'].psy.isel(ncells=slice(3, 10))
del arr['clon']
xcoord = decoder.get_variable_by_axis(arr, 'x', arr.coords)
self.assertEqual(xcoord.name, 'clon')
self.assertEqual(list(xcoord.ncells), list(arr.ncells))

def test_plot_bounds_1d(self):
"""Test to get 2d-interval breaks"""
x = xr.Variable(('x', ), np.arange(1, 5))
Expand Down