Hi ESDS community. I've been working on a script for vertically integration moisture flux (NCL application -- here). At one point in this script we have to calculate divergence. The NCL fx used is uv2dvf
that uses spherical harmonics in the divergence calculation. Has anyone here used that function before? I would love to learn more about how using spherical harmonics improves the calculation and if it is worth writing a Python method that does this. Thanks!
@Julia Kent I haven't used that function before, but we've had some previous discussion on zulip related to spherical harmonics, for example the gradsf
function with @Alea Kootz
Here's the thread -- apologies if you've already seen this and it's not helpful.
Thanks Katie! It looks like you were ultimately after a gradient function that used spherical harmonics? It doesn't look like our gradient
fx specifies that it uses those -- so what did you end up using? The gradient seems only one step away from making the divergence function.
I played around with a couple different options, the windspharm package, and some non-spherical harmonics options like numpy.gradient
and metpy.calc.gradient
. @Alea Kootz also provided some code to calculate spherical harmonics in python, as I recall.
How different do you expect your results when using spherical harmonics and not?
I don't think they were that different for my specific case, but it's been a while since I thought about this. Happy to forward emails or share notebooks if that would be helpful.
Thanks Katie. That's actually a big help. I'd appreciate any notebooks or materials you could send.
MetPy completed a big refactor last fall to take the earth's curvature into account for these functions, with their example here https://unidata.github.io/MetPy/latest/examples/calculations/Divergence.html#sphx-glr-examples-calculations-divergence-py you could also check out the issue thread here https://github.com/Unidata/MetPy/issues/893 which details the associated PRs that pushed it back the finish line and differences between taking the curvature into account vs. ignoring it.
I'm getting some errors using Metpy's divergence that I don't fully understand yet. Numpy's gradient is working for me though.
@Julia Kent Feel free to open an issue/discussion. If we can get to the data, it's easy for us to take a look.
Hi Julia, I did compare using spherical harmonics and using gradient function to calculate the divergence on spherical coordinate for ERA-Interim 6-hourly data. I am happy to share my code and some plots if you are interested. In short, the main difference for vertically integrated moisture flux is near the fronts.
I am definitely interested in that. Thanks Ho-Hsuan! Could you point me to a workflow? And potentially a sample file you've calculated vertically integrated moisture flux with? That would be a huge help thanks!
Last updated: May 16 2025 at 17:14 UTC