-
Notifications
You must be signed in to change notification settings - Fork 141
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
Refactoring indexing and interpolation #1816
base: master
Are you sure you want to change the base?
Conversation
…erpolation method
Try block should only have retrieving the function, not its execution
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.
Thoughts on the below @erikvansebille ? Let me know if you want me to break this into separate PRs
parcels/_interpolation.py
Outdated
""" | ||
elif self.interp_method in ["linear", "bgrid_velocity", "bgrid_w_velocity", "partialslip", "freeslip"]: | ||
if self.interp_method == "bgrid_velocity": | ||
if self.gridindexingtype == "mom5": | ||
zeta = 1.0 | ||
else: | ||
zeta = 0.0 | ||
elif self.interp_method == "bgrid_w_velocity": | ||
eta = 1.0 | ||
xsi = 1.0 | ||
data = self.data[ti, zi, :, :] | ||
f0 = ( | ||
(1 - xsi) * (1 - eta) * data[yi, xi] | ||
+ xsi * (1 - eta) * data[yi, xi + 1] | ||
+ xsi * eta * data[yi + 1, xi + 1] | ||
+ (1 - xsi) * eta * data[yi + 1, xi] | ||
) | ||
if self.gridindexingtype == "pop" and zi >= self.grid.zdim - 2: | ||
# Since POP is indexed at cell top, allow linear interpolation of W to zero in lowest cell | ||
return (1 - zeta) * f0 | ||
data = self.data[ti, zi + 1, :, :] | ||
f1 = ( | ||
(1 - xsi) * (1 - eta) * data[yi, xi] | ||
+ xsi * (1 - eta) * data[yi, xi + 1] | ||
+ xsi * eta * data[yi + 1, xi + 1] | ||
+ (1 - xsi) * eta * data[yi + 1, xi] | ||
) | ||
if self.interp_method == "bgrid_w_velocity" and self.gridindexingtype == "mom5" and zi == -1: | ||
# Since MOM5 is indexed at cell bottom, allow linear interpolation of W to zero in uppermost cell | ||
return zeta * f1 | ||
else: | ||
return (1 - zeta) * f0 + zeta * f1 | ||
""" |
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.
This refactoring isn't as simple as "moving the code" like the other ones, as this needs to be separated into a couple interpolation functions. I'll add some tests to help with the refactor and make sure nothing changes
Also failing CI since I was midway through refactoring. 1c26ee4 has passing CI
parcels/tools/statuscodes.py
Outdated
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.
Some of the changes here are about removing the custom inits. This was initially done to enable an approach to add_note
(I opted for a different implementation of add_note
anyway which didn't rely on this though).
Having custom init's for exception classes isn't used much in Python (and imo needlessly limits the exception class), but I wouldn't say its an important change. The only place I'd see its necessary is if its important that the user can programmatically access the exact x,y,z of the interpolation failure (as this was previously saved as state on the exception) - but I don't see that as an important feature.
To help with refactoring
for more information, see https://pre-commit.ci
This PR refactors many of the indexing methods and interpolation methods out of
field.py
to make things more manageable and reduce the coupling with the Field class.This also makes the interpolation functions more easily testable by providing only the required data.
Draft PR for early feedback