ajdawson / windspharm

A Python library for spherical harmonic computations on vector winds.
http://ajdawson.github.io/windspharm
MIT License
82 stars 36 forks source link

divergence is not right? #109

Closed Yefee closed 5 years ago

Yefee commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

Close the issue.