Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regridding to larger grid results in NaNs outside of data range #33

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/xarray_regrid/methods/conservative.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ def conservative_regrid_dataset(
da_attrs = [da.attrs for da in dataarrays]
coord_attrs = [data[coord].attrs for coord in data_coords]

mask = {}
# track which target coordinate values are not covered by the source grid
uncovered_target_grid = {}
for coord in coords:
mask[coord] = ((coords[coord] <= data[coord].max())
& (coords[coord] >= data[coord].min()))
uncovered_target_grid[coord] = (coords[coord] <= data[coord].max()) & (
coords[coord] >= data[coord].min()
)
Comment on lines +96 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does look a bit ugly now after formatting. You can add parenthesis to make it look better:

Suggested change
uncovered_target_grid[coord] = (coords[coord] <= data[coord].max()) & (
coords[coord] >= data[coord].min()
)
uncovered_target_grid[coord] = (
(coords[coord] <= data[coord].max()) &
(coords[coord] >= data[coord].min())
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. Your suggestion is what I originally had, but ruff switched it back after formatting. Do you think we should use our preferred format or do as ruff suggests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh odd. Well it's best to keep it formatted, despite it being a bit ugly.

Alternatively you could use the np.bitwise_and function:

uncovered_target_grid = np.bitwise_and(
    coords[coord] <= data[coord].max(), coords[coord] >= data[coord].min()
)

But I think this is acceptable.


target_coords = coords[coord].to_numpy()
source_coords = data[coord].to_numpy()
Expand All @@ -118,7 +120,7 @@ def conservative_regrid_dataset(

# Replace zeros outside of original data grid with NaNs
for coord in coords:
regridded = regridded.where(mask[coord])
regridded = regridded.where(uncovered_target_grid[coord])

regridded.attrs = attrs

Expand All @@ -141,8 +143,9 @@ def conservative_regrid_dataarray(
coord_attrs = [data[coord].attrs for coord in data_coords]

for coord in coords:
mask = ((coords[coord] <= data[coord].max())
& (coords[coord] >= data[coord].min()))
uncovered_target_grid = (coords[coord] <= data[coord].max()) & (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (formatting)

coords[coord] >= data[coord].min()
)

if coord in data.coords:
target_coords = coords[coord].to_numpy()
Expand All @@ -162,7 +165,7 @@ def conservative_regrid_dataarray(
data = apply_weights(data, weights, coord, target_coords)

# Replace zeros outside of original data grid with NaNs
data = data.where(mask)
data = data.where(uncovered_target_grid)

new_coords = [data[coord] for coord in data_coords]
for coord, attr in zip(new_coords, coord_attrs, strict=True):
Expand Down
7 changes: 4 additions & 3 deletions src/xarray_regrid/methods/most_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ def most_common(data: xr.Dataset, target_ds: xr.Dataset, time_dim: str) -> xr.Da
ds_regrid[coord] = target_ds[coord]

# Replace zeros outside of original data grid with NaNs
mask = ((target_ds[coord] <= data[coord].max())
& (target_ds[coord] >= data[coord].min()))
ds_regrid = ds_regrid.where(mask)
uncovered_target_grid = (target_ds[coord] <= data[coord].max()) & (
target_ds[coord] >= data[coord].min()
)
ds_regrid = ds_regrid.where(uncovered_target_grid)

ds_regrid[coord].attrs = coord_attrs[coord]

Expand Down
45 changes: 45 additions & 0 deletions tests/test_most_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def dummy_target_grid():
return create_regridding_dataset(new_grid)


@pytest.fixture
def oversized_dummy_target_grid():
new_grid = Grid(
north=48,
east=48,
south=-8,
west=-8,
resolution_lat=8,
resolution_lon=8,
)
return create_regridding_dataset(new_grid)


def test_most_common(dummy_lc_data, dummy_target_grid):
expected_data = np.array(
[
Expand Down Expand Up @@ -81,6 +94,38 @@ def test_most_common(dummy_lc_data, dummy_target_grid):
)


def test_oversized_most_common(dummy_lc_data, oversized_dummy_target_grid):
expected_data = np.array(
[
[np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN],
[np.NaN, 2, 2, 0, 0, 0, 0, np.NaN],
[np.NaN, 0, 0, 0, 0, 0, 0, np.NaN],
[np.NaN, 0, 0, 0, 0, 0, 0, np.NaN],
[np.NaN, 0, 0, 0, 0, 0, 0, np.NaN],
[np.NaN, 0, 0, 0, 0, 0, 0, np.NaN],
[np.NaN, 3, 3, 0, 0, 0, 1, np.NaN],
[np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN],
]
)

lat_coords = np.linspace(-8, 48, num=8)
lon_coords = np.linspace(-8, 48, num=8)

expected = xr.Dataset(
data_vars={
"lc": (["longitude", "latitude"], expected_data),
},
coords={
"longitude": (["longitude"], lon_coords),
"latitude": (["latitude"], lat_coords),
},
)
xr.testing.assert_equal(
dummy_lc_data.regrid.most_common(oversized_dummy_target_grid)["lc"],
expected["lc"],
)


def test_attrs_dataarray(dummy_lc_data, dummy_target_grid):
dummy_lc_data["lc"].attrs = {"test": "testing"}
da_regrid = dummy_lc_data["lc"].regrid.most_common(dummy_target_grid)
Expand Down
Loading