diff --git a/cf/mixin/fielddomain.py b/cf/mixin/fielddomain.py index 1e9e1f67e1..867c39ec81 100644 --- a/cf/mixin/fielddomain.py +++ b/cf/mixin/fielddomain.py @@ -18,7 +18,7 @@ bounds_combination_mode, normalize_slice, ) -from ..query import Query +from ..query import Query, wi from ..units import Units logger = logging.getLogger(__name__) @@ -447,43 +447,84 @@ def _indices(self, config, data_axes, ancillary_mask, kwargs): anchor0 = value.value[1] anchor1 = value.value[0] - a = self.anchor(axis, anchor0, dry_run=True)["roll"] - b = self.flip(axis).anchor(axis, anchor1, dry_run=True)[ - "roll" - ] - - size = item.size - if abs(anchor1 - anchor0) >= item.period(): - if value.operator == "wo": - set_start_stop = 0 - else: - set_start_stop = -a - - start = set_start_stop - stop = set_start_stop - elif a + b == size: - b = self.anchor(axis, anchor1, dry_run=True)["roll"] - if (b == a and value.operator == "wo") or not ( - b == a or value.operator == "wo" - ): - set_start_stop = -a - else: - set_start_stop = 0 - - start = set_start_stop - stop = set_start_stop +# if value.operator == "wi": + if item.increasing: + a= value.value[0] + else: + a = value.value[1] + + offset = self.anchor(axis, a, dry_run=True)["roll"] + nperiod = self.anchor(axis, a, dry_run=True)["nperiod"] + item = item + nperiod + item = item.roll(0, offset) + n = np.roll(np.arange(item.size), offset, 0) + print(n, (item == value).array, item.array, value) + + if value.operator == "wi": + n = n[item == value] + if not n.size: + raise ValueError( + f"No indices found from: {identity}={value!r}" + ) + + start = n[0] + stop = n[-1] + 1 else: - if value.operator == "wo": - start = b - size - stop = -a + size + n = n[item == wi(*value.value)] + if n.size == item.size: + raise ValueError( + f"No indices found from: {identity}={value!r}" + ) + + start = n[-1] + 1 + stop = start - n.size + print(n) + print ('start, stop=',start, stop) + + if False: #else: + a = self.anchor(axis, anchor0, dry_run=True)["roll"] + b = self.flip(axis).anchor(axis, anchor1, dry_run=True)[ + "roll" + ] + print('a, b=', a, b) + size = item.size + if abs(anchor1 - anchor0) >= item.period(): + if value.operator == "wo": + start = 0 + stop = 0 + else: + start = -a + stop = -a + size + elif a + b == size: + b = self.anchor(axis, anchor1, dry_run=True)["roll"] + if value.operator == "wi": + start = -a + stop = -a + size + elif (b == a and value.operator == "wo") or not ( + b == a or value.operator == "wo" + ): + start = -a + stop = -a + else: + start = 0 + stop = 0 + + # start = set_start_stop + # stop = set_start_stop else: - start = -a - stop = b - size - - if start == stop == 0: - raise ValueError( - f"No indices found from: {identity}={value!r}" - ) + if value.operator == "wo": + print('HERE0.9') + start = b - size + stop = -a + size + else: + print('HERE1') + start = -a + stop = b - size + + #if start == stop == 0: + # raise ValueError( + # f"No indices found from: {identity}={value!r}" + # ) index = slice(start, stop, 1) diff --git a/cf/test/test_Field.py b/cf/test/test_Field.py index 04517ed4f2..39b4f2a986 100644 --- a/cf/test/test_Field.py +++ b/cf/test/test_Field.py @@ -1163,7 +1163,7 @@ def test_Field_insert_dimension(self): with self.assertRaises(ValueError): f.insert_dimension(1, "qwerty") - def test_Field_indices(self): + def test_Field__aaa__indices(self): f = cf.read(self.filename)[0] array = np.ma.array(f.array, copy=False) @@ -1214,6 +1214,8 @@ def test_Field_indices(self): (x == [-80, -40, 0, 40, 80, 120, 160, 200, 240.0]).all() ) + print(f) + print(f.dimension_coordinate('X').array, f.cyclic()) with self.assertRaises(ValueError): # No X coordinate values lie inside the range [90, 100] f.indices(grid_longitude=cf.wi(90, 100))