-
Notifications
You must be signed in to change notification settings - Fork 415
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
Frontogenesis should not return nan #3678
Comments
Note that in my original case (in which I used
The code above is based on simplifying my original code to its essence, but there could be two ways the frontogenesis calculation is going bad here. |
Assuming the warnings are coming from frontogenesis rather than one of the functions it calls, the only division is here: MetPy/src/metpy/calc/kinematics.py Line 570 in b00b5d0
where the denominator is the magnitude of the gradient of the potential temperature. Assuming you've checked the grid spacing to ensure that doesn't go to zero anywhere, that one would be due to some 3x3 box having a constant potential temperature. One way to avoid follow-on effects would be to set the angle to zero at locations where the magnitude of the gradient is zero, that is: beta[mag_theta == 0] = 0 Gaussian smoothing would almost certainly make the potential temperature field non-constant, so the calculation can move to the next function call, the arcsine, where it complains of input values outside of dir_theta = np.arctan2(ddy_theta/mag_theta, ddx_theta/mag_theta)
beta = np.arcsin(-np.cos(dir_theta - psi)) or beta = -(np.pi/2 - (dir_theta - psi)) with some logic to wrap the result into MetPy/src/metpy/calc/kinematics.py Line 572 in b00b5d0
I suspect using the cosine double-angle identity below might be the best way to do things, especially if you also use where Does one of these changes fix things for you? |
Great sleuthing @DWesl ! There are two different ways I am getting a nan, so I am setting up two test cases to step through with For test case 1, it is indeed division by zero in the calculation of beta[mag_theta == 0] = 0 takes care of this one. For test case 2, where I had done the gaussian smoothing (so the temperatures are no longer identical), something else is happening, but I am out of time at the moment to investigate. I'll return to this when I get the chance. |
In my second test case, the problem is that the arcsin argument is ever so slightly greater than 1 due to round-off error. Using |
What went wrong?
The
frontogenesis
function sometimes returns nans, but it should be more careful and never do so (assuming the input data doesn't contain nans). Given the warning, I assume overflow is occurring during the computation of the angle beta, but I haven't investigated further yet.Operating System
Linux
Version
latest commit on main branch
Python Version
3.12.1
Code to Reproduce
Errors, Traceback, and Logs
/home/decker/local/miniconda3/envs/met_course/lib/python3.12/site-packages/pint/facets/plain/quantity.py:998: RuntimeWarning: invalid value encountered in divide
magnitude = magnitude_op(new_self._magnitude, other._magnitude)
The text was updated successfully, but these errors were encountered: