-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b88c73
Added mask to replace zeros outside of original data grid with NaNs
kjdoore e09e09b
Masked regridded regions outside of data range with NaN
kjdoore 81272ef
Linting fixes
kjdoore 66c7a73
Most common NaN test
kjdoore d97d797
Updated CHANGELOG
kjdoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
) | ||
|
||
target_coords = coords[coord].to_numpy() | ||
source_coords = data[coord].to_numpy() | ||
|
@@ -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 | ||
|
||
|
@@ -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()) & ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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 asruff
suggests?There was a problem hiding this comment.
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:But I think this is acceptable.