Helper class for creating Xarray indexes based on coordinate transforms. - wraps a :py:class:`CoordinateTransform` instance - takes care of creating the index (lazy) coordinates - supports point-wise label-based selection - supports exact alignment only, by comparing indexes based o
| 1484 | |
| 1485 | |
| 1486 | class CoordinateTransformIndex(Index): |
| 1487 | """Helper class for creating Xarray indexes based on coordinate transforms. |
| 1488 | |
| 1489 | - wraps a :py:class:`CoordinateTransform` instance |
| 1490 | - takes care of creating the index (lazy) coordinates |
| 1491 | - supports point-wise label-based selection |
| 1492 | - supports exact alignment only, by comparing indexes based on their transform |
| 1493 | (not on their explicit coordinate labels) |
| 1494 | |
| 1495 | .. caution:: |
| 1496 | This API is experimental and subject to change. Please report any bugs or surprising |
| 1497 | behaviour you encounter. |
| 1498 | """ |
| 1499 | |
| 1500 | transform: CoordinateTransform |
| 1501 | |
| 1502 | def __init__( |
| 1503 | self, |
| 1504 | transform: CoordinateTransform, |
| 1505 | ): |
| 1506 | self.transform = transform |
| 1507 | |
| 1508 | def create_variables( |
| 1509 | self, variables: Mapping[Any, Variable] | None = None |
| 1510 | ) -> IndexVars: |
| 1511 | from xarray.core.variable import Variable |
| 1512 | |
| 1513 | new_variables = {} |
| 1514 | |
| 1515 | for name in self.transform.coord_names: |
| 1516 | # copy attributes, if any |
| 1517 | attrs: Mapping[Hashable, Any] | None |
| 1518 | |
| 1519 | if variables is not None and name in variables: |
| 1520 | var = variables[name] |
| 1521 | attrs = var.attrs |
| 1522 | else: |
| 1523 | attrs = None |
| 1524 | |
| 1525 | data = CoordinateTransformIndexingAdapter(self.transform, name) |
| 1526 | new_variables[name] = Variable(self.transform.dims, data, attrs=attrs) |
| 1527 | |
| 1528 | return new_variables |
| 1529 | |
| 1530 | def isel( |
| 1531 | self, indexers: Mapping[Any, int | slice | np.ndarray | Variable] |
| 1532 | ) -> Index | None: |
| 1533 | # TODO: support returning a new index (e.g., possible to re-calculate the |
| 1534 | # the transform or calculate another transform on a reduced dimension space) |
| 1535 | return None |
| 1536 | |
| 1537 | def sel( |
| 1538 | self, labels: dict[Any, Any], method=None, tolerance=None |
| 1539 | ) -> IndexSelResult: |
| 1540 | from xarray.core.dataarray import DataArray |
| 1541 | from xarray.core.variable import Variable |
| 1542 | |
| 1543 | if method != "nearest": |
no outgoing calls
searching dependent graphs…