Skip to content

Commit

Permalink
Replace .interp with faster .sel in masking method
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-andersson committed Sep 10, 2023
1 parent faa5a23 commit 85faea3
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions deepsensor/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,16 @@ def mask_coord_array_normalised(
"""
if mask_da is None:
return coord_arr
mask_da = mask_da.astype(float) # Temporarily convert to float for interpolation
mask_da = mask_da.interp(
{"x1": xr.DataArray(coord_arr[0]), "x2": xr.DataArray(coord_arr[1])},
method="nearest",
kwargs=dict(fill_value=None, bounds_error=False),
).data.astype(
bool
) # Shape `coord_arr.shape[1]`, False if point is outside mask
mask_da = mask_da.astype(bool)
x1, x2 = xr.DataArray(coord_arr[0]), xr.DataArray(coord_arr[1])
mask_da = mask_da.sel(x1=x1, x2=x2, method="nearest")
return coord_arr[:, mask_da]


def da1_da2_same_grid(da1: xr.DataArray, da2: xr.DataArray) -> bool:
"""Check if da1 and da2 are on the same grid
Note: da1 and da2 are assumed normalised by DataProcessor.
Note: da1 and da2 are assumed to have been normalised by DataProcessor.
"""
x1equal = np.array_equal(da1["x1"].values, da2["x1"].values)
x2equal = np.array_equal(da1["x2"].values, da2["x2"].values)
Expand Down

0 comments on commit 85faea3

Please sign in to comment.