Skip to content

Commit 12358b3

Browse files
committed
Warn instead of error when all world points are on the same edge
1 parent a0d436a commit 12358b3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ndcube/utils/cube.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from astropy.wcs.wcsapi import BaseHighLevelWCS, BaseLowLevelWCS, HighLevelWCSWrapper, SlicedLowLevelWCS
99

1010
from ndcube.utils import wcs as wcs_utils
11+
from ndcube.utils.exceptions import warn_user
1112

1213
__all__ = [
1314
"get_crop_item_from_points",
@@ -191,8 +192,9 @@ def get_crop_item_from_points(points, wcs, crop_by_values, keepdims):
191192
combined_points_array_idx[axis] = combined_points_array_idx[axis] + [index]
192193
# Define slice item with which to slice cube.
193194
item = []
195+
ambiguous = []
194196
result_is_scalar = True
195-
for axis_indices in combined_points_array_idx:
197+
for axis_num, axis_indices in enumerate(combined_points_array_idx):
196198
if axis_indices == []:
197199
result_is_scalar = False
198200
item.append(slice(None))
@@ -201,12 +203,17 @@ def get_crop_item_from_points(points, wcs, crop_by_values, keepdims):
201203
min_idx = int(np.floor(min(axis_indices) + 0.5))
202204
max_idx = int(np.ceil(max(axis_indices) - 0.5)) + 1
203205
if min_idx == max_idx:
204-
raise ValueError("Input points cause cube to be cropped to zero size along a pixel axis.")
206+
ambiguous.append(axis_num)
207+
max_idx += 1
205208
if max_idx - min_idx == 1 and not keepdims:
206209
item.append(min_idx)
207210
else:
208211
item.append(slice(min_idx, max_idx))
209212
result_is_scalar = False
213+
if ambiguous:
214+
warn_user("Input points all lie on the same pixel edge of array "
215+
+ (f"axis {ambiguous[0]}, " if len(ambiguous) == 1 else f"axes {ambiguous}, ")
216+
+ "so the crop is ambiguous. The pixel 'greater than' each edge is returned.")
210217
# If item will result in a scalar cube, raise an error as this is not currently supported.
211218
if result_is_scalar:
212219
raise ValueError("Input points causes cube to be cropped to a single pixel. "

0 commit comments

Comments
 (0)