Skip to content

Commit

Permalink
Handle custom coords correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lbdreyer authored and pp-mo committed Aug 14, 2018
1 parent 723d45b commit 0b8baf5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
15 changes: 8 additions & 7 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __new__(cls, name_or_coord, minimum, maximum,
'groupby_point, groupby_slice')


def _discontinuity_in_2d_bounds(bds, abs_tol=1e-4):
def _discontiguity_in_2d_bounds(bds, abs_tol=1e-4):
"""
Check bounds of a 2-dimensional coordinate are contiguous
Args:
Expand All @@ -156,7 +156,7 @@ def _discontinuity_in_2d_bounds(bds, abs_tol=1e-4):
absolute difference along the y axis
"""
# Check form is (ny, nx, 4)
# Check bds has the shape (ny, nx, 4)
if not bds.ndim == 3 and bds.shape[2] == 4:
raise ValueError('2D coordinates must have 4 bounds per point '
'for 2D coordinate plotting')
Expand Down Expand Up @@ -1066,14 +1066,15 @@ def is_contiguous(self, rtol=1e-05, atol=1e-08):
if self.has_bounds():
self._sanity_check_contiguous()
if self.ndim == 1:
return np.allclose(self.bounds[1:, 0], self.bounds[:-1, 1],
rtol=rtol, atol=atol)
contiguous = np.allclose(self.bounds[1:, 0],
self.bounds[:-1, 1],
rtol=rtol, atol=atol)
elif self.ndim == 2:
allclose, _, _ = _discontinuity_in_2d_bounds(self.bounds,
contiguous, _, _ = _discontiguity_in_2d_bounds(self.bounds,
abs_tol=atol)
return allclose
else:
return False
contiguous = False
return contiguous

def contiguous_bounds(self):
"""
Expand Down
33 changes: 13 additions & 20 deletions lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import matplotlib.transforms as mpl_transforms
import numpy as np
import numpy.ma as ma
import warnings

import iris.cube
import iris.analysis.cartography as cartography
Expand All @@ -65,7 +64,7 @@ def names(coords):
if isinstance(coord, int):
result.append('dim={}'.format(coord))
else:
result.append(cube.coord.name())
result.append(coord.name())
return ', '.join(result)

def as_coord(coord):
Expand Down Expand Up @@ -127,8 +126,6 @@ def get_span(coord):
# Note the use of `reversed` to convert from the X-then-Y
# convention of the end-user API to the V-then-U convention used by
# the plotting routines.
coords = names(coords)

plot_coords = list(reversed(coords))
return PlotDefn(plot_coords, transpose)

Expand Down Expand Up @@ -296,7 +293,7 @@ def _check_contiguity_and_bounds(coord, data, abs_tol=1e-4, transpose=False):
bounds = coord.bounds

both_dirs_contiguous, diffs_along_x, diffs_along_y = \
iris.coords._discontinuity_in_2d_bounds(bounds, abs_tol=abs_tol)
iris.coords._discontiguity_in_2d_bounds(bounds, abs_tol=abs_tol)

if not both_dirs_contiguous:

Expand All @@ -322,11 +319,6 @@ def _check_contiguity_and_bounds(coord, data, abs_tol=1e-4, transpose=False):
' discontiguity occurs. Not able to create a'
' suitable mesh to give to'
' Matplotlib'.format(coord.name()))
else:
msg = 'The bounds of the {} coordinate are not contiguous. ' \
'However, data is masked where the discontiguity occurs ' \
'so plotting anyway.'.format(coord.name())
warnings.warn(msg)


def _draw_2d_from_bounds(draw_method_name, cube, *args, **kwargs):
Expand All @@ -344,16 +336,17 @@ def _draw_2d_from_bounds(draw_method_name, cube, *args, **kwargs):
two_dim_contig_atol = kwargs.pop('two_dim_coord_contiguity_atol',
1e-4)
for coord in plot_defn.coords:
if coord.ndim == 2 and coord.has_bounds():
try:
_check_contiguity_and_bounds(coord, data=cube.data,
abs_tol=two_dim_contig_atol)
except ValueError:
if _check_contiguity_and_bounds(coord, data=cube.data,
abs_tol=two_dim_contig_atol,
transpose=True)\
if hasattr(coord, 'has_bounds'):
if coord.ndim == 2 and coord.has_bounds():
try:
_check_contiguity_and_bounds(coord, data=cube.data,
abs_tol=two_dim_contig_atol)
except ValueError:
if _check_contiguity_and_bounds(
coord, data=cube.data,
abs_tol=two_dim_contig_atol, transpose=True) \
is True:
plot_defn.transpose = True
plot_defn.transpose = True

if _can_draw_map(plot_defn.coords):
result = _map_common(draw_method_name, None, iris.coords.BOUND_MODE,
Expand All @@ -374,7 +367,7 @@ def _draw_2d_from_bounds(draw_method_name, cube, *args, **kwargs):
string_axes = {}

for coord, axis_name, data_dim in zip([u_coord, v_coord],
['plotxaxis', 'yaxis'],
['xaxis', 'yaxis'],
[1, 0]):
if coord is None:
values = np.arange(data.shape[data_dim] + 1)
Expand Down

0 comments on commit 0b8baf5

Please sign in to comment.