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

divergence is not right? #109

Closed
Yefee opened this issue Jun 9, 2019 · 4 comments
Closed

divergence is not right? #109

Yefee opened this issue Jun 9, 2019 · 4 comments

Comments

@Yefee
Copy link

Yefee commented Jun 9, 2019

Dear all,

I found the divergence calculation has some weird behaviors.
Here I use the example data to calculate the divergence in two ways. They are largely equal except over high latitudes.

import xarray as xr
from windspharm.xarray import VectorWind
from windspharm.examples import example_data_path


### compute the divergence suing example data
ds = xr.open_mfdataset([example_data_path(f)
                        for f in ('uwnd_mean.nc', 'vwnd_mean.nc')])
uwnd = ds['uwnd']
vwnd = ds['vwnd']
w = VectorWind(uwnd, vwnd)
div = w.divergence()

dudx, _ = w.gradient(uwnd)
_, dvdy = w.gradient(vwnd)
div1 = dudx + dvdy

### They are the same!
fig, ax = plt.subplots(1, 2, figsize=(8, 4))
div[5].plot(ax=ax[0])
div1[5].plot(ax=ax[1])

example

However, for my test data, it obviously fails.

ds = xr.open_dataset('0temp_data/test3.nc')
w_uvh = VectorWind(ds.UH, ds.VH)
div2 = w_uvh.divergence()

dudx1, _ = w_uvh.gradient(ds.UH)
_, dvdy1 = w_uvh.gradient(ds.VH)
div3 = dudx1 + dvdy1

# They are not equal!
fig, ax = plt.subplots(1, 2, figsize=(8, 4))
div2.plot(ax=ax[0])
div3.plot(ax=ax[1])

test

Check my test data here.
test3.nc.zip

Any thoughts?

Best

@ajdawson
Copy link
Owner

I think you should consider applying spectral truncation before computing the divergence, the gradients are small compared to the fields and the result is a noisy divergence field (i.e. use the truncation= keyword argument when calling divergence).

I would not expect the gradient operators to reproduce the result of divergence, and even in your first example they do not match very well away from the equator. A truncated input to divergence will do a better job.

I also noticed the units of your field are unusual, values are of the order 1e11. This should not actually cause a problem though.

@Yefee
Copy link
Author

Yefee commented Jun 14, 2019

Thanks, @ajdawson !

After applying a truncation to the calculation, the divergence field becomes much better.

But I have a further question, different truncation returns different results. Then what is the optimal truncation I can use?

@ajdawson
Copy link
Owner

I don't think there is an optimal truncation as such. Truncation is just filtering out smaller scale variations in the original field (smoothing them) before computing divergence. Choice of truncation depends somewhat on what scale features you are interested in.

@Yefee
Copy link
Author

Yefee commented Jun 16, 2019

Thanks! I now feel more confident to use the truncation.

Close the issue.

@Yefee Yefee closed this as completed Jun 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants