Create a temporary datarray with extra coords.
(ds: Dataset, y: Hashable, locals_: dict[str, Any])
| 723 | |
| 724 | |
| 725 | def _temp_dataarray(ds: Dataset, y: Hashable, locals_: dict[str, Any]) -> DataArray: |
| 726 | """Create a temporary datarray with extra coords.""" |
| 727 | from xarray.core.dataarray import DataArray |
| 728 | |
| 729 | coords = dict(ds[y].coords) |
| 730 | dims = set(ds[y].dims) |
| 731 | |
| 732 | # Add extra coords to the DataArray from valid kwargs, if using all |
| 733 | # kwargs there is a risk that we add unnecessary dataarrays as |
| 734 | # coords straining RAM further for example: |
| 735 | # ds.both and extend="both" would add ds.both to the coords: |
| 736 | valid_coord_kwargs = {"x", "z", "markersize", "hue", "row", "col", "u", "v"} |
| 737 | coord_kwargs = locals_.keys() & valid_coord_kwargs |
| 738 | for k in coord_kwargs: |
| 739 | key = locals_[k] |
| 740 | darray = ds.get(key) |
| 741 | if darray is not None: |
| 742 | coords[key] = darray |
| 743 | dims.update(darray.dims) |
| 744 | |
| 745 | # Trim dataset from unnecessary dims: |
| 746 | ds_trimmed = ds.drop_dims(ds.sizes.keys() - dims) # TODO: Use ds.dims in the future |
| 747 | |
| 748 | # The dataarray has to include all the dims. Broadcast to that shape |
| 749 | # and add the additional coords: |
| 750 | _y = ds[y].broadcast_like(ds_trimmed) |
| 751 | |
| 752 | return DataArray(_y, coords=coords) |
| 753 | |
| 754 | |
| 755 | @overload |
searching dependent graphs…