Skip to content

Commit 870a60e

Browse files
committed
Ensure base Variable class is used when assigning
1 parent 957b61e commit 870a60e

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

xarray/core/10908.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import dask.array
2+
import numpy as np
3+
4+
import xarray
5+
6+
x = np.arange(56)
7+
y = np.arange(100, 119)
8+
t = np.arange(2)
9+
z = np.arange(9)
10+
11+
# The first array
12+
projected = xarray.DataArray(
13+
dask.array.full((x.size, y.size), np.nan, chunks=(10, 10)),
14+
coords={"x": x, "y": y},
15+
name="projected",
16+
)
17+
18+
# The array whose values will be used
19+
original = xarray.DataArray(
20+
np.full((t.size, z.size), 12.345), coords={"t": t, "z": z}, dims=["t", "z"]
21+
).chunk({"t": 10})
22+
23+
t2x = np.linspace(0, x.size, t.size)
24+
z2y = np.linspace(y.min(), y.max(), z.size)
25+
tmp = t2x[..., None] @ z2y[None, ...]
26+
27+
# The array with the coordinate correspondence
28+
pp = np.full((t.size, z.size, 2), np.nan)
29+
pp[..., 0] = tmp / z2y.max()
30+
pp[..., 1] = tmp / t2x.max()
31+
32+
pixel_positions = xarray.DataArray(
33+
pp, coords={"t": t, "z": z, "nav": ["x", "y"]}, dims=["t", "z", "nav"]
34+
).chunk({"t": 10})
35+
36+
37+
def match_coordinates(projected, pixel_positions):
38+
coordinates = projected.sel(
39+
x=pixel_positions[..., 0], y=pixel_positions[..., 1], method="nearest"
40+
).coords
41+
return {"x": coordinates["x"], "y": coordinates["y"]}
42+
43+
44+
def place_pixel_on_grid(projected, pixel_positions, original):
45+
coordinates = match_coordinates(projected, pixel_positions)
46+
print(f"{coordinates=}")
47+
print(f"{projected.shape} -- {original.shape}")
48+
print(f"pixel_positions coords {pixel_positions.coords}")
49+
projected_chunk = projected.copy()
50+
projected_chunk.loc[{"x": coordinates["x"], "y": coordinates["y"]}] = original
51+
return projected_chunk
52+
53+
54+
xarray.map_blocks(
55+
place_pixel_on_grid, projected, args=[pixel_positions, original], template=projected
56+
).compute()

xarray/core/coordinates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ def create_coords_with_default_indexes(
12651265
variables.update(idx_vars)
12661266
all_variables.update(idx_vars)
12671267
else:
1268-
variables[name] = variable
1268+
variables[name] = variable.to_base_variable()
12691269

12701270
new_coords = Coordinates._construct_direct(coords=variables, indexes=indexes)
12711271

xarray/structure/merge.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
emit_user_level_warning,
2323
equivalent,
2424
)
25-
from xarray.core.variable import Variable, as_variable, calculate_dimensions
25+
from xarray.core.variable import (
26+
IndexVariable,
27+
Variable,
28+
as_variable,
29+
calculate_dimensions,
30+
)
2631
from xarray.structure.alignment import deep_align
2732
from xarray.util.deprecation_helpers import (
2833
_COMPAT_DEFAULT,
@@ -1218,7 +1223,11 @@ def dataset_update_method(dataset: Dataset, other: CoercibleMapping) -> _MergeRe
12181223
if c not in value.dims and c in dataset.coords
12191224
]
12201225
if coord_names:
1221-
other[key] = value.drop_vars(coord_names)
1226+
value = value.drop_vars(coord_names)
1227+
if isinstance(value.variable, IndexVariable):
1228+
variable = value.variable.to_base_variable()
1229+
value = value._replace(variable=variable)
1230+
other[key] = value
12221231

12231232
return merge_core(
12241233
[dataset, other],

0 commit comments

Comments
 (0)