Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Merge branch 'develop' of https://github.com/ecmwf-lab/ecml-tools int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
floriankrb committed Mar 13, 2024
2 parents e9cfc9f + 5b7b2de commit 0910153
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions ecml_tools/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ def __init__(self, forward, mask):
super().__init__(forward)
assert len(forward.shape) == 4, "Grids must be 1D for now"
self.mask = mask
self.axis = 3

@cached_property
def shape(self):
Expand All @@ -882,26 +883,27 @@ def latitudes(self):
def longitudes(self):
return self.forward.longitudes[self.mask]

@debug_indexing
def __getitem__(self, index):
if isinstance(index, (int, slice)):
index = (index, slice(None), slice(None), slice(None))
return self._get_tuple(index)
if isinstance(index, tuple):
return self._get_tuple(index)

result = self.forward[index]
# We don't support subsetting the grid values
assert result.shape[-1] == len(self.mask), (result.shape, len(self.mask))

return result[..., self.mask]

@debug_indexing
@expand_list_indexing
def _get_tuple(self, index):
assert self.axis >= len(index) or index[self.axis] == slice(
None
), f"No support for selecting a subset of the 1D values {index}"
index, changes = index_to_slices(index, self.shape)

# In case index_to_slices has changed the last slice
index, _ = update_tuple(index, self.axis, slice(None))

result = self.forwards[index]
result = result[:, :, :, self.mask]

return apply_index_to_slices_changes(result, changes)
index, previous = update_tuple(index, self.axis, slice(None))
result = self.forward[index]
result = result[..., self.mask]
result = result[..., previous]
result = apply_index_to_slices_changes(result, changes)
return result


class Thinning(Masked):
Expand Down Expand Up @@ -1017,7 +1019,7 @@ def __getitem__(self, index):
def _get_tuple(self, index):
assert self.axis >= len(index) or index[self.axis] == slice(
None
), f"No support for selecting a subset of the 1D values {index}"
), f"No support for selecting a subset of the 1D values {index} ({self.tree()})"
index, changes = index_to_slices(index, self.shape)

# In case index_to_slices has changed the last slice
Expand Down

0 comments on commit 0910153

Please sign in to comment.