@@ -1932,11 +1932,12 @@ def _discontiguity_in_bounds(self, rtol=1e-5, atol=1e-8):
19321932 * contiguous: (boolean)
19331933 True if there are no discontiguities.
19341934 * diffs: (array or tuple of arrays)
1935- The diffs along the bounds of the coordinate. If self is a 2D
1936- coord of shape (Y, X), a tuple of arrays is returned, where the
1937- first is an array of differences along the x-axis, of the shape
1938- (Y, X-1) and the second is an array of differences along the
1939- y-axis, of the shape (Y-1, X).
1935+ A boolean array or tuple of boolean arrays which are true where
1936+ there are discontiguities between neighbouring bounds. If self is
1937+ a 2D coord of shape (Y, X), a pair of arrays is returned, where
1938+ the first is an array of differences along the x-axis, of the
1939+ shape (Y, X-1) and the second is an array of differences along
1940+ the y-axis, of the shape (Y-1, X).
19401941
19411942 """
19421943 self ._sanity_check_bounds ()
@@ -1945,39 +1946,65 @@ def _discontiguity_in_bounds(self, rtol=1e-5, atol=1e-8):
19451946 contiguous = np .allclose (
19461947 self .bounds [1 :, 0 ], self .bounds [:- 1 , 1 ], rtol = rtol , atol = atol
19471948 )
1948- diffs = np .abs (self .bounds [:- 1 , 1 ] - self .bounds [1 :, 0 ])
1949+ diffs = ~ np .isclose (
1950+ self .bounds [1 :, 0 ], self .bounds [:- 1 , 1 ], rtol = rtol , atol = atol
1951+ )
19491952
19501953 elif self .ndim == 2 :
19511954
19521955 def mod360_adjust (compare_axis ):
19531956 bounds = self .bounds .copy ()
19541957
19551958 if compare_axis == "x" :
1956- upper_bounds = bounds [:, :- 1 , 1 ]
1957- lower_bounds = bounds [:, 1 :, 0 ]
1959+ # Extract the pairs of upper bounds and lower bounds which
1960+ # connect along the "x" axis. These connect along indices
1961+ # as shown by the following diagram:
1962+ #
1963+ # 3---2 + 3---2
1964+ # | | | |
1965+ # 0---1 + 0---1
1966+ upper_bounds = np .stack (
1967+ (bounds [:, :- 1 , 1 ], bounds [:, :- 1 , 2 ])
1968+ )
1969+ lower_bounds = np .stack (
1970+ (bounds [:, 1 :, 0 ], bounds [:, 1 :, 3 ])
1971+ )
19581972 elif compare_axis == "y" :
1959- upper_bounds = bounds [:- 1 , :, 3 ]
1960- lower_bounds = bounds [1 :, :, 0 ]
1973+ # Extract the pairs of upper bounds and lower bounds which
1974+ # connect along the "y" axis. These connect along indices
1975+ # as shown by the following diagram:
1976+ #
1977+ # 3---2
1978+ # | |
1979+ # 0---1
1980+ # + +
1981+ # 3---2
1982+ # | |
1983+ # 0---1
1984+ upper_bounds = np .stack (
1985+ (bounds [:- 1 , :, 3 ], bounds [:- 1 , :, 2 ])
1986+ )
1987+ lower_bounds = np .stack (
1988+ (bounds [1 :, :, 0 ], bounds [1 :, :, 1 ])
1989+ )
19611990
19621991 if self .name () in ["longitude" , "grid_longitude" ]:
19631992 # If longitude, adjust for longitude wrapping
19641993 diffs = upper_bounds - lower_bounds
1965- index = diffs > 180
1994+ index = np . abs ( diffs ) > 180
19661995 if index .any ():
19671996 sign = np .sign (diffs )
19681997 modification = (index .astype (int ) * 360 ) * sign
19691998 upper_bounds -= modification
19701999
1971- diffs_between_cells = np .abs (upper_bounds - lower_bounds )
1972- cell_size = lower_bounds - upper_bounds
1973- diffs_along_axis = diffs_between_cells > (
1974- atol + rtol * cell_size
2000+ diffs_along_bounds = ~ np .isclose (
2001+ upper_bounds , lower_bounds , rtol = rtol , atol = atol
19752002 )
1976-
1977- points_close_enough = diffs_along_axis <= (
1978- atol + rtol * cell_size
2003+ diffs_along_axis = np .logical_or (
2004+ diffs_along_bounds [0 ], diffs_along_bounds [1 ]
19792005 )
1980- contiguous_along_axis = np .all (points_close_enough )
2006+
2007+ contiguous_along_axis = ~ np .any (diffs_along_axis )
19812008 return diffs_along_axis , contiguous_along_axis
19822009
19832010 diffs_along_x , match_cell_x1 = mod360_adjust (compare_axis = "x" )
0 commit comments